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  = document.getElementById('propertyPrice').value;
	if (isNaN(propertyPrice)) {
          return;
         }


	var deposit =document.getElementById('deposit').value;
	
	if (isNaN(deposit)) {
          return;
        }

	document.getElementById('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 (document.getElementById('propertyPrice').value == "" || isNaN(document.getElementById('propertyPrice').value)) {
		alert("You must enter a Property Price");
		status = false;
		document.getElementById('propertyPrice').focus();
		return;
	}
        if (document.getElementById('mortgageRequired').value == "" || isNaN(document.getElementById('mortgageRequired').value)) {

		alert("You must enter a Mortgage Amount");
		status = false;
		document.getElementById('mortgageRequired').focus();
		return;
	}
		loanDuration = $('loanDuration').value;
		if ($('loanDuration')){
			tmp = loanDuration.split(" ");
			loanDuration = (tmp[0]);
		}
        if (loanDuration == "" || isNaN(loanDuration)) {
		alert("You must enter the Mortgage Duration (measured in years)");
		status = false;
		document.getElementById('loanDuration').focus();
		return;
	}
	
	if (document.getElementById('rate').value == "" || isNaN(document.getElementById('rate').value)) { 
		alert("You must enter the Mortgage Rate");
		status = false;
		document.getElementById('rate').value.focus();
		return;
	}
	
        if (isNaN(document.getElementById('deposit').value)) {
                alert("The Deposit Amount must be a number");
                status = false;
               document.getElementById('deposit').focus();
                return;
        }



	
if (status == true)
{
	var mortgageRequired = document.getElementById('mortgageRequired').value;
	
	var rate = document.getElementById('rate').value;
	
	rate = rate/100;
	rate = rate/12;
	//calculate interest portion monthly payment to 2 decimal places
	var interest= ((rate*12)*(mortgageRequired)*1)/12;	
	//document.getElementById('interest').value = decimalPlaces(interest,2);
	
	//calculate interest & capital monthly payment to 2 decimal places
	var loanDuration = document.getElementById('loanDuration').value;
	tmp = loanDuration.split(" ");
	loanDuration = (tmp[0]);
	
	loanDuration = loanDuration *12;
	var capitalInterest =decimalPlaces((mortgageRequired *(Math.pow((1+rate), loanDuration ))*rate)/(Math.pow((1+rate),loanDuration)-1),2);
		
	capitalInterest = parseFloat(capitalInterest);
	//document.getElementById('capitalInterest').value = capitalInterest;
	//alert($('repaymentType1').checked);
	//alert($('repaymentType2').checked);
		if ($('repaymentType1').checked==true){
			$('result').value = decimalPlaces(interest,2);
		} else if($('repaymentType2').checked==true){
			$('result').value = capitalInterest;
		}
		//moreText = '<strong>For a more detailed mortgage quote</strong>, <strong>please </strong><a target="_parent" href="http://dotfs.waidev4.com/staging/other_content/request_advice.php"><strong>contact us</strong></a><strong> today</strong>.</p>';
		//$('contactUsText').innerHTML=moreText;
	}
}

function calculate(number) {

 hValue = number;
 frm = document.feesave.oneperc;
 frm.value = formatCurrency((hValue * 0.01) *1.175);
 frm = document.feesave.twoperc;
 frm.value = formatCurrency((hValue * 0.015) *1.175);
 frm = document.feesave.threeperc;
 frm.value = formatCurrency((hValue * 0.025) *1.175);

}

function formatCurrency(num) {
 num = num.toString().replace(/\$|\,/g,'');
 if(isNaN(num))
  num = "0";
  sign = (num == (num = Math.abs(num)));
  num = Math.floor(num*100+0.50000000001);
  num = Math.floor(num/100).toString();
  for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
   num = num.substring(0,num.length-(4*i+3))+','+
   num.substring(num.length-(4*i+3));
  return (((sign)?'':'-') + num);
}
