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').attr('value');
	if (isNaN(propertyPrice)) {
          return;
         }
	var deposit =$('#deposit').attr('value');
	
	if (isNaN(deposit)) {
          return;
        }
	$('#mortgageRequired').attr('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').attr('value') == "" || isNaN($('#propertyPrice').attr('value'))) {
		alert("You must enter a Property Price "+$('#propertyPrice').attr('value'));
		status = false;
		$('#propertyPrice').focus();
		return;
	}
        if ($('#mortgageRequired').attr('value') == "" || isNaN($('#mortgageRequired').attr('value'))) {

		alert("You must enter a Mortgage Amount");
		status = false;
		$('#mortgageRequired').focus();
		return;
	}
        if ($('#loanDuration').attr('value') == "" || isNaN($('#loanDuration').attr('value'))) {
		alert("You must enter the Mortgage Duration (measured in years)");
		status = false;
		$('#loanDuration').focus();
		return;
	}
	if ($('#rate').attr('value') == "" || isNaN($('#rate').attr('value'))) { 
		alert("You must enter the Mortgage Rate");
		status = false;
		$('#rate').attr('value').focus();
		return;
	}
	
        if (isNaN($('#deposit').attr('value'))) {
                alert("The Deposit Amount must be a number");
                status = false;
               $('#deposit').focus();
                return;
        }
	
if (status == true){
	var mortgageRequired = $('#mortgageRequired').attr('value');
	
	var rate = $('#rate').attr('value');
	
	rate = rate/100;
	rate = rate/12;
	//calculate interest portion monthly payment to 2 decimal places
	var interest= ((rate*12)*(mortgageRequired)*1)/12;	
	$('#interest').attr('value', decimalPlaces(interest,2));
	
	//calculate interest & capital monthly payment to 2 decimal places
	var loanDuration = $('#loanDuration').attr('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').attr('value', capitalInterest);
	}
}
