var page = document.location.href;

function url_parameters(which){
        var params = location.search;

        if (params.length==0)
        return "";

        params = params.substring(1, params.length); // get rid of ?

        var pairs = params.split("&");

        for (var i=0; i<pairs.length; i++)
        {
                var pair_values = pairs[i].split("=");
                if (pair_values[0] == which)
                {
                        return pair_values[1];
                }
        }
        return "";
}

function delineate(str){
theleft = str.indexOf("=") + 1;
theright = str.lastIndexOf("&");
return(str.substring(theleft, theright));
}

var propertyprice=url_parameters("price");
var rates = new Array();
rates[0]=url_parameters("r1");
rates[1]=url_parameters("r2");
rates[2]=url_parameters("r3");
//alert(propertyprice);

function countme() {
	var propertyPrice  = $('propertyPrice').value;
	if (isNaN(propertyPrice)) {
          return;
         }
	var deposit =$('deposit').value;
	
	if (isNaN(deposit)) {
          return;
        }
	$('mortgageRequired').value = (propertyPrice) - (deposit);	
} 

function decimalPlaces(numberToFix, noDecimalPlaces ) {
	var div = Math.pow(10,noDecimalPlaces);
	numberToFix = Math.round(numberToFix* div) /div;
	return numberToFix;
}
function calculateme() {
	var status = true;	
	 if ($('propertyPrice').value == "" || isNaN($('propertyPrice').value)) {
		alert("You must enter a Property Price");
		status = false;
		$('propertyPrice').focus();
		return;
	}
        if ($('mortgageRequired').value == "" || isNaN($('mortgageRequired').value)) {

		alert("You must enter a Mortgage Amount");
		status = false;
		$('mortgageRequired').focus();
		return;
	}
        if ($('loanDuration').value == "" || isNaN($('loanDuration').value)) {
		alert("You must enter the Mortgage Duration (measured in years)");
		status = false;
		$('loanDuration').focus();
		return;
	}
	if ($('rate').value == "" || isNaN($('rate').value)) { 
		alert("You must enter the Mortgage Rate");
		status = false;
		$('rate').value.focus();
		return;
	}
	
        if (isNaN($('deposit').value)) {
                alert("The Deposit Amount must be a number");
                status = false;
               $('deposit').focus();
                return;
        }
	
if (status == true){
	var mortgageRequired = $('mortgageRequired').value;
	
	var rate = $('rate').value;
	
	rate = rate/100;
	rate = rate/12;
	//calculate interest portion monthly payment to 2 decimal places
	var interest= ((rate*12)*(mortgageRequired)*1)/12;	
	$('interest').value = decimalPlaces(interest,2);
	
	//calculate interest & capital monthly payment to 2 decimal places
	var loanDuration = $('loanDuration').value;
	loanDuration = loanDuration *12;
	var capitalInterest =decimalPlaces((mortgageRequired *(Math.pow((1+rate), loanDuration ))*rate)/(Math.pow((1+rate),loanDuration)-1),2);
		
	capitalInterest = parseFloat(capitalInterest);
	$('capitalInterest').value = capitalInterest;
	}
}