$(document).ready(function(){
	$('#leaveSite').html('<div id="leaveSiteContent"></div>');
	$("a").each(function(){
		var link = $(this);
		var href = this.href;
		var title = this.title;
		var host = window.location.host.replace(/^(([^\/]+?\.)*)([^\.]{4,})((\.[a-z]{1,4})*)$/, '$3$4');
		if (this.href != null) {
			if ( (this.href.match(/^http/)) && (! this.href.match(host)) && (! this.href.match(/^javascript/)) && (! this.href.match(".pdf")) ) {
/*				link.addClass('thickbox');
				link.attr('href', '#TB_inline?height=200&width=300&inlineId=leaveSite');
				link.click(function () {
					$('#leaveSiteContent').html('<br /><center><span class="dataLabel">You are exiting MakingHomeAffordable.gov<br /><br />Thank you for visiting our site. Click below to visit the external link you\'ve requested:</span></center><center><br /><br /><a href="' + href + '" title="' + title  + '">' + title + '</a><br /><br />We hope your visit was informative and enjoyable.</center>');
				}); */
			}
		}
	});

	/*popup initializations*/
	tb_init('a.thickbox');
	tb_init('a.videobox');

	/*redirection pages*/
	$("#eligibilityForm").validate({
			submitHandler: function(form) {
			var allYes = $(".eligibility:checked").length;
			window.location = (allYes == 4 ? 'refinance_yes.html' : 'other_options_refinance.html');
			return false;
		}
	});
	$("#modificationEligibilityForm").validate({
			submitHandler: function(form) {
			var allYes = $(".eligibility:checked").length;
			window.location = (allYes == 5 ? 'modification_yes.html' : 'other_options_modification.html');
			return false;
		}
	});

	/*calculators*/
	$("#mortgageCalculator").validate({
		submitHandler: function(form) {
			calculate();
			return false;
		}
	});
	$("#reductionEstimator").validate({
			submitHandler: function(form) {
			calculateReduction();
			return false;
		}
	});
	$("#monthlyHousing").validate({
			submitHandler: function(form) {
			//calculateHousing();
			calculateReduction();
			return false;
		}
	});
	$("#monthlyHousingper").validate({
			submitHandler: function(form) {
			calculateHousingper();
			return false;
		}
	});
	$("#w2method").validate({
			submitHandler: function(form) {
			calculatew2();tb_remove();
			return false;
		}
	});

	$("#backtopage").validate({
			submitHandler: function(form) {
			tb_remove();
			return false;
		}
	});
	$("#monthlyGross").validate({
			submitHandler: function(form) {
			calculateMonthly();tb_remove();
			return false;
		}
	});
});

function calculate() {
	if ($('#income').length == 0) {
		return false;
	}
	var reg = /^\d+$/;
	//var payment = $('#payment')[0].value;
	var income = $('#income')[0].value;
	if (!income) {
		return false;
	}
	if (reg.test(income)) {
		var postPayment = Math.round(0.31 * income);
		//var savings = payment-postPayment;
		//calculate results based on numbers
		//$('#preDTI').html(Math.round((payment / income)*100) + '%');
		$('#postPayment').html('$'+postPayment);
		//$('#postIncome').html('$'+income);
		//$('#postDTI').html(Math.round((postPayment / income)* 100) + '%');
		//$('#savings').html((savings > 0 ? '$'+savings : '<span style="color:#FF0000;">$' + Math.abs(savings) +'</span>'));
	}

	if (($('#monthlyPayment').length == 0) || ($('#monthlyHomeowners').length == 0) || ($('#monthlyTaxes').length == 0)) {
		//alert("dsda");
		return false;
	}


	var reg = /^\d+$/;
	var monthlyPayment = $('#monthlyPayment')[0].value;
	var monthlyHomeowners = $('#monthlyHomeowners')[0].value;
	var monthlyTaxes = $('#monthlyTaxes')[0].value;
	var Monthlyassociationfee=$('#monthly_ass_fees')[0].value;

	// Start 05/20/09 La

	//if (!monthlyPayment || !monthlyHomeowners || !monthlyTaxes) {
	//	return false;
	//}

	if (!monthlyPayment) {
		return false;
	}

	if(!monthlyHomeowners)
	{
		monthlyHomeowners=0;
	}

	if(!monthlyTaxes)
	{
		monthlyTaxes=0;
	}

	// END 05/20/09 La

	if(!Monthlyassociationfee)
	{
		Monthlyassociationfee=0;
	}
	if (reg.test(monthlyPayment) && reg.test(monthlyHomeowners) && reg.test(monthlyTaxes)) {
		//totalMonthlyPayment = monthlyPayment + monthlyHomeowners + monthlyTaxes
		var totalMonthlyPayment = Math.round(Number(monthlyPayment) + Number(Monthlyassociationfee) + Number(monthlyHomeowners/12) + Number(monthlyTaxes/12));
		//var postPaymenttotal = Math.round(0.31 * totalMonthlyPayment);
		//var savings = payment-postPayment;
		//calculate results based on numbers
		//$('#preDTI').html(Math.round((payment / income)*100) + '%');

		$('#postPaymenttotalmonthly').html('$'+totalMonthlyPayment);
		//$('#payment')[0].value = totalMonthlyPayment;
	}
	if(totalMonthlyPayment>postPayment)
	{
	// Start 05/20/09 La

	// 05/22/09 Remove as per Dan
	//$('#resulttext').html('<span class="highlightBlue">Your Monthly Housing Expense</span> is greater than your<span class="highlightBlue"> Mortgage Payment Guideline</span>, so you may be eligible for the Home Affordable Modification');

	// 05/22/09 Put in html document as per dan
	//$('#resulttext1').html('\
	//	<h2><p style="margin-top:20px; font-size:xx-large; line-height:100%">Yes!</p></h2>\
	//	<p style="font-size:larger; line-height:100%">\
	//	<span class="highlightBlue">Your Monthly Housing Expense is</span>\
	//	<span class="highlightYellow"><i>greater</i></span><span class="highlightBlue"> than 31% of your Monthly Gross Income.  \
	//		You may answer yes to question 5.\
	//		<p>Click the button below to finish your Step 1 eligibility questionnaire</p>\
	//	</span></p>\
	//<br><div class="buttonContainer" style="width:500px"><a href="javascript:history.go(-1)" ><img id="return" onmouseout="buttonRollOff("return",4)" onmouseover="buttonRollOn("return",5)" src="images/return_off.png" alt="calculate" /></a></div>');

	fireEvent(document.getElementById("res"),'click');
	}
	else
	{

	// 05/22/09 Remove as per Dan
	//	$('#resulttext').html('<span class="highlightYellow">No.</span> <span class="highlightBlue">Your Monthly Housing Expense is </span><span class="highlightYellow"><i>not</i></span><span class="highlightBlue"> greater than 31% of your gross income.\
	//		You must answer No to question 5.<br>Click the button below to finish your Step 1 eligibility questionnaire</span><br><div class="buttonContainer"><a href="javascript:history.go(-1)" ><img id="return" onmouseout="buttonRollOff("return",4)" onmouseover="buttonRollOn("return",5)" src="images/return_off.png" alt="calculate" /></a></div>');

	// 05/22/09 Put in html document as per dan
	//$('#resulttext1').html('<h2><p style="margin-top:10px; font-size:x-large; line-height:100%">No.</p></h2>\
	//			<p style="font-size:larger; line-height:100%">\
    //            <span class="highlightBlue">Your Monthly Housing Expense is </span>\
    //            <span class="highlightYellow"><i>not</i></span>\
    //            <span class="highlightBlue">greater than 31% of your gross income. Your answer to question 5 is no. </span>\
	//			<p style="font-size:larger; line-height:100%"><span class="highlightBlue">Click the button\
	//			  below to finish your Step 1 eligibility questionnaire</span><br>\
	//			<div class="buttonContainer" style="width:500px">\
	//				<a href="javascript:history.go(-1)" ><img id="return" onmouseout="buttonRollOff("return",4)" onmouseover="buttonRollOn("return",5)" src="images/return_off.png" alt="calculate" />\
	//				</a>\
	//			</div>');

	fireEvent(document.getElementById("res1"),'click');

	// END LA
	}
	return false;
}

function calculateAll() {
	calculateMonthly();
	calculateHousing();
	calculateReduction();
}

function calculateMonthly() {
	if ($('#monthlyIncome').length == 0) {
		return false;
	}
	var reg = /^\d+$/;
	var monthlyIncome = $('#monthlyIncome')[0].value;
	if (!monthlyIncome) {
		return false;
	}
	if (reg.test(monthlyIncome)) {
		//estimatedMonthlyIncome = 1.25 * monthlyIncome
		$('#estimatedMonthlyIncome').html('$'+Math.round(1.25 * monthlyIncome));
		$('#income')[0].value = Math.round(1.25 * monthlyIncome);
	}
	$('#monthlyIncomew2')[0].value = "";
	$('#estimatedMonthlyIncomew2').html('$');
	return false;
}
function calculatew2() {
	if ($('#monthlyIncomew2').length == 0) {
		return false;
	}
	var reg = /^\d+$/;
	var monthlyIncomew2 = $('#monthlyIncomew2')[0].value;
	if (!monthlyIncomew2) {
		return false;
	}

	if (reg.test(monthlyIncomew2)) {
		//estimatedMonthlyIncome = 1.25 * monthlyIncome
		$('#estimatedMonthlyIncomew2').html('$'+Math.round(monthlyIncomew2 / 12));
		$('#income')[0].value = Math.round(monthlyIncomew2 / 12);
	}
	$('#monthlyIncome')[0].value = "";
	$('#estimatedMonthlyIncome').html('$');
	//document.monthlyGross.getElementById("monthlyIncome").value="";
	return false;
}
function calculateHousing() {
	//if (($('#monthlyPayment').length == 0) || ($('#monthlyHomeowners').length == 0) || ($('#monthlyTaxes').length == 0)) {
		//alert("dsda");
		//return false;
	//}


	var reg = /^\d+$/;
	var monthlyPayment = $('#monthlyPayment')[0].value;
	var monthlyHomeowners = $('#monthlyHomeowners')[0].value;
	var monthlyTaxes = $('#monthlyTaxes')[0].value;
	var Monthlyassociationfee=$('#monthly_ass_fees')[0].value;
	if (!monthlyPayment)  {
		monthlyPayment=0;
	}
	if(!monthlyHomeowners)
	{
		monthlyHomeowners=0;
	}
	if(!monthlyTaxes)
	{
		monthlyTaxes=0;
	}
	if(!Monthlyassociationfee)
	{
		Monthlyassociationfee=0;
	}
	if (reg.test(monthlyPayment) && reg.test(monthlyHomeowners) && reg.test(monthlyTaxes)) {
		//totalMonthlyPayment = monthlyPayment + monthlyHomeowners + monthlyTaxes
		var totalMonthlyPayment = Math.round(Number(monthlyPayment) + Number(Monthlyassociationfee) + Number(monthlyHomeowners/12) + Number(monthlyTaxes/12));
		$('#totalMonthlyPayment').html('$'+totalMonthlyPayment);
		$('#payment')[0].value = totalMonthlyPayment;

	}
	return false;
}

function calculateHousingper() {
	if (($('#monthlyPayment').length == 0) || ($('#monthlyHomeowners').length == 0) || ($('#monthlyTaxes').length == 0)) {
		//alert("dsda");
		return false;
	}


	var reg = /^\d+$/;
	var monthlyPayment = $('#monthlyPayment')[0].value;
	var monthlyHomeowners = $('#monthlyHomeowners')[0].value;
	var monthlyTaxes = $('#monthlyTaxes')[0].value;
	var Monthlyassociationfee=$('#monthly_ass_fees')[0].value;
	if (!monthlyPayment || !monthlyHomeowners || !monthlyTaxes) {
		return false;
	}
	if(!Monthlyassociationfee)
	{
		Monthlyassociationfee=0;
	}
	if (reg.test(monthlyPayment) && reg.test(monthlyHomeowners) && reg.test(monthlyTaxes)) {
		//totalMonthlyPayment = monthlyPayment + monthlyHomeowners + monthlyTaxes
		var totalMonthlyPayment = Math.round(Number(monthlyPayment) + Number(Monthlyassociationfee) + Number(monthlyHomeowners/12) + Number(monthlyTaxes/12));
		var postPaymenttotal = Math.round(0.31 * totalMonthlyPayment);
		//var savings = payment-postPayment;
		//calculate results based on numbers
		//$('#preDTI').html(Math.round((payment / income)*100) + '%');

		$('#postPaymenttotalmonthly').html('$'+postPaymenttotal);
		//$('#payment')[0].value = totalMonthlyPayment;
	}
	return false;
}

function calculateReduction() {

	if ($('#payment').length == 0 || $('#income').length == 0) {
		return false;
	}
	var reg = /^\d+$/;
	var payment = $('#payment')[0].value;
	var income = $('#income')[0].value;
	if (!payment || !income) {
		return false;
	}
	if (reg.test(payment) && reg.test(income)) {
		var postPayment = Math.round(0.31 * income);
		var savings = payment-postPayment;
		$('#dti').html(Math.round((payment / income)*100) + '%');
		$('#postPayment').html('$'+postPayment);
		$('#savings').html((savings > 0 ? '$'+savings : '$0'));
		$('#savingsMessage').html((savings > 0 ? '' : 'Modification will not lower your payment.'));

		//Start LA 05/20/09
		fireEvent(document.getElementById("eres"),'click');
		//END LA 05/20/09

	}


	//$('#results').style.display='none';
//$('#results').style.display='none';

	return false;
}

//LA

function fireEvent(obj,evt){

	var fireOnThis = obj;
	if( document.createEvent ) {
	  var evObj = document.createEvent('MouseEvents');
	  evObj.initEvent( evt, true, false );
	  fireOnThis.dispatchEvent(evObj);
	} else if( document.createEventObject ) {
	  fireOnThis.fireEvent('on'+evt);
	}
}
