
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)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}	

document.observe("dom:loaded", function () {

	function calcbudgetsearch(ev) {
		
		fields = {
			base		: parseFloat($F("creditstatus")) || 0,
			term		: parseInt($F("term")) || 0,
			amount		: 0,
			deposit		: parseFloat($F("deposit")) || 0,
			budget		: parseFloat($F("budget")) || 0
		};

		fields.loanAmount		= fields.amount - fields.deposit;
		fields.interestPA		= (fields.loanAmount/100) * fields.base;
		fields.interestTotal	= fields.interestPA * (fields.term/12);
		fields.totalToPay		= fields.loanAmount + fields.interestTotal;
		fields.monthlyPayment	= fields.totalToPay / fields.term;

		fields.amountToSpend = (function () {
			var pct = 100 / (fields.base + 100);
			var val = (fields.budget || 0) * fields.term;
			for (var i=0;i<fields.term/12;i++) val = val * pct
			return val;
		})() + fields.deposit;

		//$("linksearch").href = "/sbb.html?budgetflag=1&deposit=" + fields.deposit + "&budget=" + fields.budget + "&status=" + fields.base + "&term=" + fields.term + "&priceupto=" + Math.round(fields.amountToSpend)		
		
		//$("linksearch").href = "/search?budgetflag=1&deposit=" + fields.deposit + "&budget=" + fields.budget + "&status=" + fields.base + "&term=" + fields.term + "&pt=" + Math.round(fields.amountToSpend)
		
		var tempPriceTo = Math.round(fields.amountToSpend);
		
		var validPrices = new Array();
	
		validPrices[0] = 0;
   		validPrices[1] = 500;
    	validPrices[2] = 1000;
    	validPrices[3] = 2000;
    	validPrices[4] = 3000;
    	validPrices[5] = 4000;
	    validPrices[6] = 5000;
	    validPrices[7] = 6000;
		validPrices[8] = 7000;
 	   	validPrices[9] = 8000;
	    validPrices[10] = 9000;
 	   	validPrices[11] = 10000;
    	validPrices[12] = 11000;
   		validPrices[13] = 12000;
    	validPrices[14] = 13000;
    	validPrices[15] = 14000;
    	validPrices[16] = 15000;
    	validPrices[17] = 16000;
    	validPrices[18] = 17000;
   		validPrices[19] = 18000;
    	validPrices[20] = 19000;
   		validPrices[21] = 20000;
    	validPrices[22] = 22000;
    	validPrices[23] = 25000;
    	validPrices[24] = 27000;
    	validPrices[25] = 30000;
    	validPrices[26] = 35000;
    	validPrices[27] = 40000;
    	validPrices[28] = 50000;
    	validPrices[29] = 75000;
    	validPrices[30] = 100000;
    	validPrices[31] = 250000;
    	validPrices[32] = 500000;
    	validPrices[33] = 1000000;
		
		$("priceto").value = "0";
		
		for (i=0;i<validPrices.length;i++)
		{
			if(tempPriceTo > validPrices[i])
			{
				if(i+1 <= validPrices.length)
				{
					$("priceto").value = validPrices[i+1];
				}
				else
				{
					$("priceto").value = validPrices[i];
				}				
			}
		}
				
		
		
		if (Math.round(fields.amountToSpend) == 0 ) {
			$("budgetresult").innerHTML = "Your Budget is &pound; ???"
		} 
		else {
			$("budgetresult").innerHTML = "Your Budget is &pound;<strong>" + addCommas(Math.round(fields.amountToSpend)) + " &nbsp; </strong>"
		}

		return fields;
	}

	//$("creditRatingLink").observe("click", function (ev) {
		//window.open("http://www.visitcars.co.uk/car-advice/credit-rating-chart.html?pop=1", "creditRating", "width=696,height=412,resizable=no,scrollbars=no")
		//ev.stop()
	//})
	
	$("deposit").observe("keyup", calcbudgetsearch)
	$("budget").observe("keyup", calcbudgetsearch)
	$("term").observe("change", calcbudgetsearch)
	$("creditstatus").observe("change", calcbudgetsearch)
	
	calcbudgetsearch()	

})