/******************************************************************************
 * Javascript for the iframe lead form
 *****************************************************************************/
var iframe = {};

/******************************************************************************
 * variables
 *****************************************************************************/
iframe.bSubmitAttempted = false;

/******************************************************************************
 * handleFormSubmit();
 *****************************************************************************/
iframe.handleFormSubmit = function () {
	try {
		if (!iframe.validateForm()) {
			$("#cmd_insert_lead").attr("disabled","");
			$("#cmd_cart_insert_lead").attr("disabled","");
			return false;
		} else {
			$("input").attr("readonly", "readonly");
			$(".processing").show();
			$("#cmd_insert_lead").attr("disabled","true");
			$("#cmd_cart_insert_lead").attr("disabled","true");
			return true;
		}
	} catch (e) {
		publishException(e);
	}		
};

/******************************************************************************
 * iframe.validateForm();
 *****************************************************************************/
iframe.validateForm = function () {
	try {
		var bIsValid = true;
		
		// validate all required fields	
		if (!lead_form.validateLeadFormEmailAddress()) 			bIsValid = false;
		if (!lead_form.validateTelephone($("#lf_telephone_1"))) bIsValid = false;
		if (!lead_form.validateTelephone($("#lf_telephone_2"))) bIsValid = false;
		if (!lead_form.validateComments()) 						bIsValid = false;
		if (!lead_form.validateRegion())						bIsValid = false;
		if (!lead_form.validatePostalCode()) 					bIsValid = false;
		if (!lead_form.validateDesiredInvestment()) 			bIsValid = false;
		if (!lead_form.validateDesiredLocation()) 				bIsValid = false;
		
		// validate any required fields	
		if (!lead_form.checkRequiredFields()) 					bIsValid = false;
	
		return bIsValid;
	} catch (e) {
		publishException(e);
	}	
};

/******************************************************************************
 * add items in the user's shopping cart into the lead form
 *****************************************************************************/
iframe.addCartItems = function() {
	try {

		$("#dom_cart_contents").hide();
				
		// get the cookie containing the items in the shopping cart
		var sCookie = readCookie("selected_product_ids");
		sCookie 	= (sCookie != null) ? sCookie.decode() : "";
	
		// if the cookie is empty we have nothing to do
		if (sCookie.indexOf("::") < 0) {	
			return;
		} else {
		
			// we dont' want to do this on the request list submit form
			if ($("input[name=lf_lead_source_id]").eq(0).val() == "13") {
				return;
			}
			
			// get the primary product id
			var iProductId = $("input[name='lf_product_ids[]']").eq(0).val();
		
			// loop through the cookie
			aProducts = sCookie.split("|");
			
			// clear the container div
			$("#request_list_items").empty();
		
			if (aProducts.length > 0) {
			
				for (i=0; i<aProducts.length; i++) {
				
					aTmp = aProducts[i].split("::");
		
					if (iProductId != aTmp[0]) {
		
						// add the checkboxes to the container div
						elRequestListItem = $('<div class="request_list_item"></div>');
						elCheckbox = $('<input checked=\"checked\" type="checkbox" style="margin: 0px 5px 0 0 ;" name="lf_cart_product_ids[]" id="cart_id_' + aTmp[0] + '" value="' + aTmp[0] + '" />');   
						$(elRequestListItem).append(elCheckbox);
						$(elRequestListItem).append('<label for="cart_id_' + aTmp[0] + '">' + aTmp[1] + '</label>');
						$("#dom_cart_contents_items").append(elRequestListItem);
	
					}	
				}
				$("#dom_cart_contents").show();
			} else {
				$("#dom_cart_contents").hide();
			}		
		}
	} catch (e) {
		publishException(e);
	}		
}

/******************************************************************************
 * If the contents of the leadform changes we will need to resize the lead form
 *****************************************************************************/
iframe.resizeIFrame = function() {
	try {
		var iHeight = $(document.body).height();
		$('#LeadForm', parent.document.body).css("height", (iHeight + 100).toString() + "px");
	} catch (e) {
		publishException(e);
	}
}	

/******************************************************************************
 * Add the iframe namespace to the lead_form namespace
 *****************************************************************************/
lead_form.iframe = iframe;

/******************************************************************************
 * Wireup Events
 *****************************************************************************/
$(document).ready(function () {

	// These don't work right so not using them right now
	// $("select",".lead_form_field").mouseover(lead_form.handleSelectMouseover);
	// $("select",".lead_form_field").mouseout(lead_form.handleSelectMouseout);

	// validate lead form on submit
 	$("#frm_lead").submit(iframe.handleFormSubmit);
 	$("#lf_country").change(lead_form.handleCountryChange);
	$("#lf_last_name").blur(function () { if (iframe.bSubmitAttempted) lead_form.checkRequiredField($(this).parent()) });
	$("#lf_street_1").blur(function () { if (iframe.bSubmitAttempted) lead_form.checkRequiredField($(this).parent()) });
	$("#lf_city").blur(function () { if (iframe.bSubmitAttempted) lead_form.checkRequiredField($(this).parent()) });
	$("#lf_country").blur(function () { if (iframe.bSubmitAttempted) lead_form.checkRequiredField($(this).parent()) });
	$("#lf_email_address").blur(function () { if (iframe.bSubmitAttempted) lead_form.validateLeadFormEmailAddress });
	$("#lf_telephone_1").blur(function () { if (iframe.bSubmitAttempted) lead_form.validateTelephone(this) });
	$("#lf_telephone_2").blur(function () { if (iframe.bSubmitAttempted) lead_form.validateTelephone(this) });
	$("#lf_postal_code").blur(function () { if (iframe.bSubmitAttempted) lead_form.validatePostalCode });
	$("#lf_region").blur(function () { if (iframe.bSubmitAttempted) lead_form.validateRegion });
	$("#lf_region_select").blur(function () { if (iframe.bSubmitAttempted) lead_form.validateRegion });
	$("#lf_comments").blur(function () { if (iframe.bSubmitAttempted) lead_form.validateComments });
	$("#lf_investment_range_id").blur(function () { if (iframe.bSubmitAttempted) lead_form.validateDesiredInvestment });
	$("#lf_desired_location_id").blur(function () {  if (iframe.bSubmitAttempted) lead_form.validateDesiredLocation });
	$("#lf_country").blur(function () {
		 if (iframe.bSubmitAttempted) lead_form.validateRegion();
		 if (iframe.bSubmitAttempted) lead_form.validatePostalCode();
	}).change(function () {
		lead_form.formatRegionField(iframe.bSubmitAttempted);
		lead_form.validateRegion();
		 if (iframe.bSubmitAttempted) lead_form.validatePostalCode();		
	});
	$("#lf_is_discovery_day_lead").click(lead_form.handleCheckDiscoveryDays);
	lead_form.handleCheckDiscoveryDays();
	
	
	// pre-populate the form w/ existing cookie values
	$('#frm_lead :input').each(function () {
	
	 	// pre-set the value for the country
	 	if ($("#lf_country").val() == "") {
	 		if($("#lf_default_country_id").val() > 0) {
				$("#lf_country").val($("#lf_default_country_id").val());
			}
		}	

		var fieldValue = readCookie($(this).attr('name'));
		if (fieldValue) {
			$(this).val(unescape(decodeURIComponent(fieldValue.replace(/\+/g, " "))));
		}
	});
	
	// discovery days 
	$("#discovery_days").click(function () {
		$("#dday_mini_info").toggle("fast", function () {
			lead_form.iframe.resizeIFrame();
		});
		
		return false;
	});	
	
	// maybe trigger the country change manually once to set things in motion
	lead_form.handleCountryChange();	
	
	// add in the cart items
	lead_form.iframe.addCartItems();
	
	// resize the iframe if necessary
	lead_form.iframe.resizeIFrame();
	
	$("#cmd_insert_lead").click(function() {
		if (_gaq) {
			_gaq.push(['_trackEvent', 'Lead Form', 'Submit Clicked', 'Profile']);
		}
	});
});
