$(function() {

//declare local variables
var first_name, last_name, email, phone, zip, city,state,address, error_messages, search_zip, company_name;


//reference the address-related input fields
first_name = $("#contractorForm  input[@name=first_name]");
last_name = $("#contractorForm  input[@name=last_name]");
email = $("#contractorForm  input[@name=email]");
phone = $("#contractorForm  input[@name=phone]");
zip = $("#contractorForm  input[@name=zip]");
address = $("#contractorForm  input[@name=address]");
city = $("#contractorForm  input[@name=city]");
state = $("#contractorForm  select[@name=state]");
company_name = $("#contractorForm  input[@name=company_name]");

$("#contractorForm").submit(function(){
    //prepare the form data to be sent
    error_messages=new Array();
    if (validate()) {
        return true;
    } else {

        //set the trigger that hides the error message when the field is modified
        $("#contractorForm :input").each(function(){
            $(this).focus(function(){ hide_error(this)});
        });
        
        return false;
    }
    
});

//hide the error message when the field is selected
var hide_error=function(field){
    var name=$(field).attr("name");
    var obj = $("#"+error_messages[name]);
    if (obj.length){
        obj.hide('fast');
    }
    $(field).unbind("focus");

}

function trimString(strIn) {
	if (/^\s/.test(strIn)) { strIn = strIn.replace(/^\s{1,}/, ""); }
	if (/\s$/.test(strIn)) { strIn = strIn.replace(/\s{1,}$/, ""); }
	return strIn;
}

function getParsedPhoneStr(strIn) {
	strIn = strIn.replace(/\.|-| |\(|\)/g,"");
	return strIn;
}

function isValidEntirePhone(strPhone) {
	var bparsedStr = trimString(strPhone);
	var parsedStr = getParsedPhoneStr(bparsedStr);
	if (parsedStr.length != 10) { return false; }
	if (/^[01]/.test(parsedStr)) { return false; }
	var npa = parsedStr.substring(0,3);
	if (!isValidPhoneNPA(npa)) {
		return false;
	}
	var nxx = parsedStr.substring(3,6);
	if (!isValidPhoneNXX(nxx)) {
		return false;
	}
	
	return true;
}

function isValidPhoneNPA(strNPA) {
	if (strNPA.length < 3) { return false; }
	if (/[^0-9]/.test(strNPA)) { return false; }
	if (/^[01]/.test(strNPA)) { return false; }
	if ("200,222,300,333,400,444,500,555,600,666,700,777,900,999".indexOf(strNPA) != -1) { return false; }
	return true;
}

function isValidPhoneNXX(strNXX) {
	if (strNXX.length < 3) { return false; }
	if (/[^0-9]/.test(strNXX)) { return false; }
	if (/^[01]/.test(strNXX)) { return false; }
	return true;
}

function isValidFirstName(strName) {
	if(strName.length == 0) { return false; }
	if (/[^A-Za-z -]/.test(strName)) { return false; }
	return true;
}

function isValidLastName(strName) {
	if(strName.length == 0) { return false; }
	if (/[^A-Za-z -]/.test(strName)) { return false; }
	return true;
}

function isValidAddress(strAddress) {
	if (strAddress.length < 3) { return false; }
	if (strAddress.replace(/[^0-9]/g, "").length < 1) { return false; }
	if (strAddress.replace(/[^A-Za-z]/g, "").length < 1) { return false; }
	return true;
}

function isValidCity(strCity) {
	if (strCity.length < 2) { return false; }
	if (/[^A-Za-z -,]/.test(strCity)) { return false; }
	return true;
}

function isValidZip(strZip) {
 	if(strZip.length != 5) { return false; }
	if(/[^0-9]/.test(strZip)) { return false; }
	return true;
}

function isValidState(strState) {
 	if(strState.length != 2) { return false; }
	return true;
}

function isValidCompanyName(strName) {
	if(strName.length == 0) { return false; }
	return true;
}



function v(str) {
	return "Invalid " + str + "\n";
}

function validate()
{

    var dbg = 0;
	var boolReturn = true;
	var strErrMessage = "";
												
	// Test for blank first name.
	if(!isValidFirstName(first_name.val()))
	{
		boolReturn = false;
		$("#first_name_invalid").show('slow');
		error_messages['first_name'] = 'first_name_invalid';
	}
	// Test for blank phone.
	if(!isValidLastName(last_name.val()))
	{
		boolReturn = false;
		$("#last_name_invalid").show('slow');
		error_messages['last_name'] = 'last_name_invalid';
	}
	
		// Test for blank email name.
//	if( !(/^\w+([\.\-]\w+)*@\w+([\.\-]\w+)*\.\w{2,4}$/.test(objVal.value)) )
	if(!isValidEmail(email.val()))
	{
		boolReturn = false;
		$("#email_invalid").show('slow');
		error_messages['email'] = 'email_invalid';
	}
	
	if(!isValidCity(city.val()))
	{
	    boolReturn = false;
		$("#city_invalid").show('slow');
		error_messages['city'] = 'city_invalid';
	}

	if( state.val() == 0 )
	{
	    boolReturn = false;
		$("#state_invalid").show('slow');
		error_messages['state'] = 'state_invalid';
	}

	if(!isValidZip(zip.val()))
	{
	    boolReturn = false;
		$("#zip_invalid").show('slow');
		error_messages['zip'] = 'zip_invalid';
	}
	
	if(!isValidAddress(address.val()))
	{
	    boolReturn = false;
		$("#address_invalid").show('slow');
		error_messages['address'] = 'address_invalid';
	}

	if(!isValidState(state.val()))
	{
	    boolReturn = false;
		$("#state_invalid").show('slow');
		error_messages['state'] = 'state_invalid';
	}
	
	if(!isValidEntirePhone(phone.val()))
	{
	    boolReturn = false;
		$("#phone_invalid").show('slow');
		error_messages['phone'] = 'phone_invalid';
	}
	
	if(!isValidCompanyName(company_name.val()))
	{
	    boolReturn = false;
		$("#company_name_invalid").show('slow');
		error_messages['company_name'] = 'company_name_invalid';
	}
	
	if(!isValidTargetZips())
	{
	    boolReturn = false;
		$("#search_zip_empty").show('slow');
		error_messages['search_zip[1]'] = 'search_zip_empty';
		error_messages['search_zip[2]'] = 'search_zip_empty';
		error_messages['search_zip[3]'] = 'search_zip_empty';
		error_messages['search_zip[4]'] = 'search_zip_empty';
		error_messages['search_zip[5]'] = 'search_zip_empty';
	}
    
	return(boolReturn);
}

function isValidEmail(strEmail) {

	if (strEmail.length < 5) { return false; }

	if (!strEmail.match(/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/)) { return false; }
	
	return true;
}

function isValidTargetZips() {
    var is_inputted = false;
    
    for (i=1; i<6; i++) {
        if ($('#search_zip_'+i).val()) {
            is_inputted = true;
            break;
        }
    }
    
    return is_inputted;
}

$('#coupon_code_label').click(function() {
	$('#coupon_code_label').hide();
	$('#coupon_code').show();
});


})

