// MORTGAGE BORROWING CALCULATOR
// HOW MUCH CAN I BORROW?

function borrowShowResults() { 
	var income1 = $('#borrowCalculator #income1').val();
	income1 = income1.replace(/\,/g,'');
	var income2 = $('#borrowCalculator #income2').val();
	income2 = income2.replace(/\,/g,'');
	
	
  // If first income not entered return an error
	if (income1 == '') {
		$('#borrowCalculator #income1').css('border', '1px solid #cc0000'); 
		alert('Please enter annual income for the first applicant');
		return false;
		}
  // If second income not entered calculate loanAmount for a single applicant
	else if (income2 == '') 
		  { 
			  totalIncome = Math.round(parseInt(income1));
			  if (totalIncome >= 10000 && totalIncome < 20000) {loanAmount = totalIncome*3;}
			  else if (totalIncome >= 20000 && totalIncome < 45000) {loanAmount = totalIncome*3.5;}
			  else if (totalIncome >= 45000) {loanAmount = totalIncome*4;}
			  else if (isNaN(totalIncome)) {alert('Please enter valid amount.'); return false}
			  else {loanAmount = 0; alert('Sorry we can\'t lend you at this time.'); return false}
		  }
  // If two incomes are entered calculate loanAmount based on two applicants total income
	else {
		  totalIncome = Math.round(parseInt(income1) + parseInt(income2));
		  if (totalIncome >= 10000 && totalIncome < 20000) loanAmount = totalIncome*3.5;
		  else if (totalIncome >= 20000 && totalIncome < 45000) loanAmount = totalIncome*4;
		  else if (totalIncome >= 45000) loanAmount = totalIncome*4.5;
		  else if (isNaN(totalIncome)) {alert('Please enter valid amount.'); return false}
		  else {loanAmount = 0; alert('Sorry we can\'t lend you at this time.'); return false}
		  
	}
		  $('#borrowCalculator #income1').css('border', '1px solid #999'); 
		  $('#borrowCalculator #income2').css('border', '1px solid #999'); 
		  if (!isNaN(loanAmount) && loanAmount!=0)
		  {
			$('.borrowResultsPopup #borrowCalcResults').html('&pound;' + addCommas(Math.round(loanAmount)));
			Cufon.replace('h1');
			$('.borrowResultsPopup').show('slow');
			$('.borrowResultsContainer').show();
		  }
		  else if (loanAmount!=0) alert('Please enter numeric values and try again');
		
//		  $('html').css("overflow-y","hidden");
}
function borrowCloseResults() {
	$('.borrowResultsPopup').hide();
	$('.borrowResultsContainer').hide();
//		  $('html').css("overflow-y","scroll");
}
function borrowClearFields() {
	$('#borrowCalculator #income1').val('');
	$('#borrowCalculator #income2').val('');
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
	// $cmignore 
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	// $/cmignore 
	}
	return x1 + x2;
}
function borrowValidateNumber(field)
{
	incomeAmount = $(field).val();
	incomeAmount = incomeAmount.replace(/\,/g,'');
	incomeAmountWithCommas = addCommas(incomeAmount);
	$(field).val(incomeAmountWithCommas); 
}
