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() {

	removeChars();	
	
	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(popup) {
	var status = true;
	
	removeChars();
	
	 if (price == "" || isNaN(price)) {
		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('MyRate').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)
{
	if (document.getElementById('MyRate')) {
		if (document.getElementById('MyRate').value =='E.g. 5.25'){
			document.getElementById('MyRate').value='';
		}
	}
	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;
		}
		if (popup)
			moreText = '<strong>For a more detailed mortgage quote, <a target="_parent" href="javascript:void(window.opener.location=\'http://dotfs.waidev4.com/site/other_content/request_advice.php\');">why not contact dot today?</a></strong></p>';
		else
			moreText = '<strong>For a more detailed mortgage quote</strong>, <strong>please </strong><a target="_parent" href="http://dotfs.waidev4.com/site/other_content/request_advice.php"><strong>contact us</strong></a><strong> today</strong>.</p>';
		$('contactUsText').innerHTML=moreText;
	}
}

function removeChars(){
	
	if (document.getElementById('propertyPrice')) {
		price = document.getElementById('propertyPrice').value;
		var re = new RegExp("[^0-9]", "g");
		price = price.replace(re, "");
		document.getElementById('propertyPrice').value = price;
		
	}
	if (document.getElementById('mortgageRequired')) {
		price = document.getElementById('mortgageRequired').value;
		var re = new RegExp("[^0-9]", "g");
		price = price.replace(re, "");
		document.getElementById('mortgageRequired').value = price;
		
	}

	if (document.getElementById('MyRate')) {
		rate = document.getElementById('MyRate').value;
		if (rate=="E.g. 5.25") {rate = "";}
		rr = rate.replace("E.g.","");
		var re = new RegExp("[^0-9\.]", "g");
		rate = rr.replace(re, "");
		document.getElementById('MyRate').value = rate;
		if (rate !="")
		document.getElementById('rate').value = rate;
	}	
	
	if (document.getElementById('deposit')) {
		rate = document.getElementById('deposit').value;
		var re = new RegExp("[^0-9]", "g");
		rate = rate.replace(re, "");
		document.getElementById('deposit').value = rate;
	}
}