//JavaScript Document
function instant_rate_quote_form(frmObj)
{
	var frm_instant_rate_quote_array = new Array();
	frm_instant_rate_quote_array.push("required,my_first_name,Please enter your first name.");
	frm_instant_rate_quote_array.push("required,my_last_name,Please enter your last name.");
	frm_instant_rate_quote_array.push("valid_phone,my_daytime_phone,Please enter valid daytime phone.");
	frm_instant_rate_quote_array.push("valid_phone,my_evening_phone,Please enter valid eveningtime phone.");
	frm_instant_rate_quote_array.push("required,my_email_address,Please enter your email address.")
	frm_instant_rate_quote_array.push("valid_email,my_email_address,Please enter valid email address.");
	frm_instant_rate_quote_array.push("required,amount1,Please enter 1st loan amount.");
	frm_instant_rate_quote_array.push("required,txtvalue,Please enter home's value.");
	if (frmObj.verification_code)
		frm_instant_rate_quote_array.push("required,verification_code,Please enter the verification code in the text box below.");
	return 	validateFields(frmObj,frm_instant_rate_quote_array);
}

function showhide(id)
{
	if (document.getElementById)
	{
		obj	= document.getElementById(id);
		if (obj.style.display == "none")
		{
			obj.style.display	= "";
		}
		else
		{
			obj.style.display	= "none";
		}
	}
}

function display(obj,id1)
{
	txt											= obj.options[obj.selectedIndex].value;
	document.getElementById(id1).style.display	= 'none';
	if (txt.match(id1))
	{
		document.getElementById(id1).style.display	= '';
	}
}

function getNum(val)
{
	val	= val.replace(/[^0-9.]/g,'');
	val	= stripDuplicateChars(val,'.',1,0);
	return val;
}

function stripDuplicateChars(str, strip, n, s)
{
	var count	= 0;
	var stripped= str.substring(0, s);
	var chr;
	for (var i = s; i < str.length; i++)
	{
		chr	= str.substring(i, i+1);
		if (chr == strip)
		{
			count++;
			if (count<n+1)
			{
				stripped	= stripped + chr;
			}
		}
		else
		{
			stripped	= stripped + chr;
		}
	}
	return stripped;
}

function ltvcltv()
{
	var amount1		= getNum(document.getElementById("amount1").value);
	var amount2		= getNum(document.getElementById("amount2").value);
	var txtvalue	= getNum(document.getElementById("txtvalue").value);
	
	if(txtvalue > 0) {
		
		var ltv		= amount1 / txtvalue * 100; //roundNum
		var cltv	= amount2 / txtvalue * 100 + ltv; //roundNum
		
		if(ltv > 0 && ltv < 1000)
		{
			document.getElementById("lbl_ltv").innerHTML	= ltv.toFixed(2);
			if(ltv > 80 && ltv <= 100)
			{
				document.getElementById("option_pmi").style.display = '';
			}
			else
			{
				document.getElementById("option_pmi").style.display = 'none';
			}
		}
		else
		{
			document.getElementById("lbl_ltv").innerHTML		= '?';
		}
		
		if(cltv > 0 && cltv < 1000)
		{
			document.getElementById("lbl_cltv").innerHTML		= cltv.toFixed(2);

		}
		else
		{
			document.getElementById("lbl_cltv").innerHTML		= '?';
		}
	}
	else
	{
		document.getElementById("lbl_ltv").innerHTML		= '?';
		document.getElementById("lbl_cltv").innerHTML		= '?';
	}
}


$(function(){
			$('.submitControlInstant').click(function (){
														if ($("#my_first_name").length)
														{
															if ($("#my_first_name").val()=='First Name' ||  $.trim($("#my_first_name").val())=='')
															{
																alert ('Please enter your first name.');
																$("#my_first_name").focus();
																return false;
															}
														}

														if ($("#my_last_name").length)
														{
															if ($("#my_last_name").val()=='Last Name' ||  $.trim($("#my_last_name").val())=='')
															{
																alert ('Please enter your last name.');
																$("#my_last_name").focus();
																return false;
															}
														}
														
														if ($("#my_email_address").length)
														{
															if ($("#my_email_address").val()=='Email Address' ||  $.trim($("#my_email_address").val())=='')
															{
																alert ('Please enter your email address.');
																$("#my_email_address").focus();
																return false;
															}
															if(!isValidEmail($.trim($('#my_email_address').val())))
															{
																alert ('Please enter a valid email address.');
																$("#my_email_address").focus();
																return false;
															}
														}
														var pmi = '';
														$('input[name~="pmi"]:checked').each(function(i){
															pmi = $(this).val();
														});

														if  ($('input[name~="instant_form_url"]').length)
												   		{
															var formURL	= $('input[name~="instant_form_url"]').val();
															$.post(
																   formURL, {	my_first_name		: $('#my_first_name').val(),
																				my_last_name		: $('#my_last_name').val(),
																				my_daytime_phone	: $('#my_daytime_phone').val(),
																				my_evening_phone	: $('#my_evening_phone').val(),
																				my_email_address	: $('#my_email_address').val(),
																				amount1				: $('#amount1').val(),
																				amount2				: $('#amount2').val(),
																				txtvalue			: $('#txtvalue').val(),
																				pmi					: pmi,
																				purpose				: $('#purpose').val(),
																				occupancy			: $('#occupancy').val(),
																				property_type		: $('#property_type').val(),
																				credit				: $('#credit').val(),
																				stateSelect			: $('#stateSelect').val(),
																				countySelect		: $('#countySelect').val(),
																				documentation		: $('#documentation').val(),
																				escrow				: $('#escrow').val(),
																				lock				: $('#lock').val(),
																				program_wanted		: $('#program_wanted').val(),
																				am_type				: $('#am_type').val(),
																				hdn_report_id		: $('#hdn_report_id').val(),
																				hdn_auto_resp_id	: $('#hdn_auto_resp_id').val(),
																				hdn_send_report		: $('#hdn_send_report').val(),
																				report_title		: $('#report_title').val()
																			}, function(data) {	
																								if(data==1)
																									alert ('There is some issue in the form validation. Please recheck your fields.');
																								else if(data==2)
																									alert ('You\'ve already subscribed for this report using the same email address. Please check your email address and enter a new valid email address to continue.');
																								else if(data==3)
																									top.location.href	= $('input[name~="instant_thank_you_url"]').val();
																									
																		 });
														}
													});
	   });
