/* POPUP WINDOW */

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

/* CALCULATOR SCRIPTS */
function calculate_loan() {
	// Coded by Henric Blomgren, 04 May 2006, for Digital Routes Ltd. henric@digitalroutes.co.uk

	var property_value = document.getElementById("value").value; // Take the inputted data and put in into a javascript variable (property value)
	var loan_amount	= document.getElementById("principal").value; // Take the inputted data and put in into a javascript variable (loan amount)
	var interest_rate = document.getElementById("interest").value; // Take the inputted data and put in into a javascript variable (interest rate)
	var years = document.getElementById("years").value; // Take the inputted data and put in into a javascript variable (years)

	if(isNaN(property_value) || property_value == "") { // Check so that each field is filled in & numerical (property value)
		alert("Your property value needs to be numeric, please use dots (.) as decimals.");
		return false; // Fail
	} else if(isNaN(loan_amount) || loan_amount == "") { // Check so that each field is filled in & numerical (loan amount)
		alert("Your loan amount needs to be numeric, please use dots (.) as decimals.");
		return false; // Fail
	} else if(isNaN(interest_rate) || interest_rate == "") { // Check so that each field is filled in & numerical (interest rate)
		alert("Your interest rate needs to be numeric, please use dots (.) as decimals.");
		return false; // Fail
	} else if(isNaN(years) || years == "") { // Check so that each field is filled in & numerical (years)
		alert("Your amount of years needs to be numeric.");
		return false; // Fail
	}

	var D4, D5, D6, D12, D7, D9;
	var Topform,Botform, x, y;

	D5 = interest_rate/100;

	D7 = years;
	D9 = interest_rate;
	Topform = loan_amount*(D5/12);
	x=(1+(D5/365));
	y=((D7*365)*-1);
	Botform = 1-Math.pow(x,y);

	var one_hundred	= parseFloat(loan_amount)/100; // Divide the total loan amount by a hundred to get 1% of the loan for multiplication
	var new_interest_yearly = parseFloat(interest_rate) * one_hundred; // Multiply the interest rate by 1% of the total loan value
	var new_interest_overall = parseFloat(years) * new_interest_yearly; // Multiply the number of years with the above value
	var new_interest_monthly = (new_interest_overall / parseFloat(years)) / 12; // Divide the above with the number of years
	var ltv	= Math.floor(loan_amount/property_value*10000)/100;
	var repayment_yearly = parseFloat(loan_amount) / parseFloat(years); // Divide the loan amount with the total amount of years

	document.getElementById("newyears").value = years; // Return the number of years & insert it into the correct field (by id)
	document.getElementById("monthly_interest").value = (Math.round(new_interest_monthly*100))/100;; // Divide & return the interests & insert it into the correct field (by id)
	document.getElementById("monthlyrepay").value = (Math.round(100*Topform/Botform))/100; // Divide the repayments into months & add the interest, then return it & insert it into the correct field (by id)
	document.getElementById("ltv").value = ltv;

	return true; // Finish up
}