/******************************************************************************
 * Javascript for the Video Popup Form Page
 *****************************************************************************/
var popup = {};

/******************************************************************************
 * variables
 *****************************************************************************/
popup.bSubmitAttempted = false;
popup.sMessage = "";

/******************************************************************************
 * handleFormSubmit();
 *****************************************************************************/
popup.handleFormSubmit = function () {
	try {
		if (!popup.validateForm()) {	
			alert(LEAD_FORM_ERROR_ALERT + '\n\n' + popup.sMessage);
			return false;
		} else {
			$("input").attr("readonly", "readonly");
			$(".processing").show();
			return true;
		}	
	} catch (e) {
		publishException(e);
	}		
};

/******************************************************************************
 * popup.validateForm();
 *****************************************************************************/
popup.validateForm = function () {
	try {
		popup.bSubmitAttempted = true;

		var bIsValid = true;
		
		// validate each field
		var sFirstName 			= $("#lf_first_name").val().trim();
		var sLastName 			= $("#lf_first_name").val().trim();
		var sEmailAddress 		= $("#lf_email_address").val().trim();
		var sTelephone1 		= $("#lf_telephone_1").val().trim();
		var sCountry			= $("#lf_country").val().trim();
		var sStreet1			= $("#lf_street_1").val().trim();
		var sCity				= $("#lf_city").val().trim();
		var sPostalCode			= $("#lf_postal_code").val().trim();
		var iInvestmentRangeId	= $("#lf_investment_range_id").val().trim();
		var iDesiredLocationId	= $("#lf_desired_location_id").val().trim();
		
		if ($("#lf_region_select").length > 0) {
			var sRegion				= $("#lf_region_select").val().trim();
		} else {
			var sRegion				= $("#lf_region").val().trim();
		}
		
		popup.sMessage = "";	
		if (sFirstName.lengh == 0 || sLastName.length == 0) {
			popup.sMessage += "          * " + LEAD_FORM_ERROR_REQUIRED_FULL_NAME + "\n";
			bIsValid = false;
		}
		if (sEmailAddress.length == 0) {
			popup.sMessage += "          * " + LEAD_FORM_ERROR_REQUIRED_EMAIL_ADDRESS + "\n";
			bIsValid = false;			
		} else if (!isValidEmail(sEmailAddress)) {
			popup.sMessage += "          * " + LEAD_FORM_ERROR_INVALID_EMAIL_ADDRESS + "\n";
			bIsValid = false;
		}	
		if (sTelephone1.length == 0) {
			popup.sMessage += "          * " + LEAD_FORM_ERROR_REQUIRED_TELEPHONE + "\n";
			bIsValid = false;			
		} else {
			var iTelephoneValidation = isValidTelephoneFormat(sTelephone1, sCountry);
			if (iTelephoneValidation == 1) {				
				popup.sMessage += "          * " + LEAD_FORM_ERROR_INVALID_TELEPHONE + "\n";
				bIsValid = false;
			} 			
		}			
		if (sStreet1.length == 0) {
			popup.sMessage += "          * " + LEAD_FORM_ERROR_REQUIRED_STREET + "\n";
			bIsValid = false;
		}		
		if (sCity.length == 0) {
			popup.sMessage += "          * " + LEAD_FORM_ERROR_REQUIRED_CITY + "\n";
			bIsValid = false;
		}	
		
		// validate region based on country
		if (sCountry == "221" && sRegion == "") {
			// us
			popup.sMessage += "          * State is required.\n";
			bIsValid = false;		
		} else if (sCountry == "37" && sRegion == "") {
			// canada
			popup.sMessage += "          * Province/Territory is required.\n";
			bIsValid = false;	
		} else if (sCountry == "194" && sRegion == "") {
			// spain
			popup.sMessage += "          * Por favor indicar la Provincia.\n";
			bIsValid = false;
		}
			
		if (lead_form.isPostCodeRequired(sCountry) && sPostalCode.length == 0) {
			popup.sMessage += "          * " + LEAD_FORM_ERROR_REQUIRED_POSTAL_CODE + "\n";
			bIsValid = false;
		}	
		if (sCountry.length == 0) {
			popup.sMessage += "          * " + LEAD_FORM_ERROR_REQUIRED_COUNTRY + "\n";
			bIsValid = false;
		}		
		if (iInvestmentRangeId.length == 0) {
			popup.sMessage += "          * " + LEAD_FORM_ERROR_REQUIRED_CAPITAL_TO_INVEST + "\n";
			bIsValid = false;
		} else {
							
			// grab the required amount from a hidden field in the form
			var iRequiredInvestmentId	= $("#lf_minimum_investment_id").val();
		
			// check the required investment
			if (iInvestmentRangeId < iRequiredInvestmentId) {
				var sMessage = $(".required_message",$("#lf_investment_range_id").parent()).eq(0).val();
				popup.sMessage += "          * " + sMessage + "\n";
				bIsValid = false;
			}	
		}	
		
		
		if (iDesiredLocationId.length == 0) {
			popup.sMessage += "          * " + LEAD_FORM_ERROR_REQUIRED_DESIRED_LOCATION + "\n";
			bIsValid = false;
		}			
		
		// validate required custom fields
		$(".lead_form_field.custom.required").each(function () {
						
			if ($("input[type=radio]",this).length > 0) {
				// we want to make sure one of them is checked
				bIsValid = ($('input[type=radio]:checked',this).length > 0) ? true : false;
			}
			if ($("input:checkbox",this).length > 0) {		
				// we want to make sure one of them is checked
				bIsValid = ($('input[type=checkbox]:checked',this).length > 0) ? true : false;						
			}
			
			// validate text areas
			$("textarea",this).each(function () {
				bIsValid = ($(this).val().trim() == "") ? false : true;	
			});
			
			// validate select boxes
			$("select",this).each(function () {
				bIsValid = ($("select",this).eq(0).val().trim() == "") ? false : true;
			});
			
			// just grab the value
			$("input[type=text]",this).each(function () {
				bIsValid = ($(this).val().trim() == "") ? false : true;	
			});			
			
			// if it is missing, add a message
			if (!bIsValid) {
				// grab the required field message (a hidden field)
				var sMessage = $(".required_message",this).eq(0).val();
				popup.sMessage += "          * " + sMessage + "\n";
				bIsValid = false;				
			}		
		});	
		

		return bIsValid;
	} catch (e) {
		publishException(e);
	}	
};

/******************************************************************************
 * Add the popup namespace to the lead_form namespace
 *****************************************************************************/
lead_form.leads = popup;

/******************************************************************************
 * Wireup Events
 *****************************************************************************/
$(document).ready(function () {

	// validate forgot password form on submit
 	$("#frm_lead").submit(popup.handleFormSubmit);
 	$("#lf_country").change(lead_form.handleCountryChange);
 	
	// pre-populate the form w/ existing cookie values
	$('#frm_lead :input').each(function () {
		var fieldValue = readCookie($(this).attr('name'));
		if (fieldValue) {
			$(this).val(unescape(decodeURIComponent(fieldValue.replace(/\+/g, " "))));
		}
	});
	
 	// pre-set the value for the country
	$("#lf_country").val($("#lf_default_country_id").val());
	
	// maybe trigger the country change manually once to set things in motion
	lead_form.handleCountryChange();

});
