

function convertToSterling(from, to) {
	validateDecimalSeperator(document.getElementById(from));
	var rate = document.getElementById("rate").value;
	var euro = document.getElementById(from).value;
	var sterling = euro / rate;
	sterling = sterling.toFixed(2);
	if(isNaN(sterling)) sterling = "0.00";
	document.getElementById(to).value = sterling;
}

function convertToEuro(from, to) {
	validateDecimalSeperator(document.getElementById(from));
	var rate = document.getElementById("rate").value;
	var sterling = document.getElementById(from).value;
	var euro = sterling * rate;
	euro = euro.toFixed(2);
	if(isNaN(euro)) euro = "0.00";
	document.getElementById(to).value = euro;
}

function validateDecimalSeperator(elem) {
	if(elem.value.indexOf(',') != -1) {
		elem.value = elem.value.replace(",","");
	} 
	var x = 0;
	if(isNaN(elem.value)) {
		elem.style.border = strErrorBorder;	
	} else {
		elem.style.border = origBorder;	
	}
}

function replaceCharAt(str, index, newvalue) {
	var val = str.substr(0,index) + newvalue + str.substr(index+1);
	return val;
}
	

function countChars(haystack, needle) {
	var offset = 0;
	var needleCount = 0;
	for( pos=haystack.indexOf(needle); pos != -1; pos=haystack.indexOf(needle, offset+1) ) {
		offset = pos;	
		needleCount++;
	}
	return needleCount;
} 


function CalculateMortgage() {

/*
	validateDecimalSeperator(document.getElementById("purchase_price_euro"));
	validateDecimalSeperator(document.getElementById("purchase_price_sterling"));
	validateDecimalSeperator(document.getElementById("amount_required_euro"));
	validateDecimalSeperator(document.getElementById("amount_required_sterling"));
*/
	PurchaseExpence_e();
	PurchaseExpence_s();
	MortgageExpence_e();
	MortgageExpence_s();
	MonthlyPayment_e();
	MonthlyPayment_s();
	return false;
}


function PurchaseExpence_e(){

	var price_e = document.getElementById("purchase_price_euro").value;
	var purchase_expence_e = price_e * 0.10;
	purchase_expence_e = purchase_expence_e.toFixed(2);
	if(isNaN(purchase_expence_e)) purchase_expence_e = "0.00";		
	document.getElementById("expence_purchase_euro").value = purchase_expence_e;
	return false;
}
function PurchaseExpence_s(){
	var price_s = document.getElementById("purchase_price_sterling").value;
	var purchase_expence_s = price_s * 0.10;
	purchase_expence_s = purchase_expence_s.toFixed(2);
	if(isNaN(purchase_expence_s)) purchase_expence_s = "0.00";	
	document.getElementById("expence_purchase_sterling").value = purchase_expence_s;
	return false;
}

function MortgageExpence_e(){
	//alert(document.getElementById("amount_required_euro").value);
	var mortgage_e = document.getElementById("amount_required_euro").value;
	var mortgage_expence_e = mortgage_e * 0.03;
	mortgage_expence_e = mortgage_expence_e.toFixed(2);
	if(isNaN(mortgage_expence_e)) mortgage_expence_e = "0.00";
	document.getElementById("expence_mortgage_euro").value = mortgage_expence_e;
	return false;
}
function MortgageExpence_s(){
	var mortgage_s = document.getElementById("amount_required_sterling").value;
	var mortgage_expence_s = mortgage_s * 0.03;
	mortgage_expence_s = mortgage_expence_s.toFixed(2);
	if(isNaN(mortgage_expence_s)) mortgage_expence_s = "0.00";
	document.getElementById("expence_mortgage_sterling").value = mortgage_expence_s;
	return false;
}

function MonthlyPayment_e(){
	var mortgage_e = document.getElementById("amount_required_euro").value;
	var interest = document.getElementById("interest").value;
	var years = document.getElementById("term_mortgage").value;
	var monthly_payment_e;
	//alert(years);
	if(document.getElementById("mortgage_type").value == "Repayment") {
		monthly_payment_e = getRepaymentMonthlyPayment(interest,mortgage_e, years);
	} else {
		monthly_payment_e = getInterestOnlyMonthlyPayment(interest, mortgage_e, years);	
	};
	monthly_payment_e = monthly_payment_e.toFixed(2);
	if(isNaN(monthly_payment_e)|| monthly_payment_e == "Infinity") monthly_payment_e = "0.00";
	document.getElementById("result_euro").value = monthly_payment_e;
	
	return false;
}

function MonthlyPayment_s(){
	var mortgage_s = document.getElementById("amount_required_sterling").value;
	var interest = document.getElementById("interest").value;
	var years = document.getElementById("term_mortgage").value;
	var monthly_payment_s;
	if(document.getElementById("mortgage_type").value == "Repayment") {
		monthly_payment_s = getRepaymentMonthlyPayment(interest,mortgage_s, years);
	} else {
		monthly_payment_s = getInterestOnlyMonthlyPayment(interest, mortgage_s, years);	
	};	
	monthly_payment_s = monthly_payment_s.toFixed(2);
	if(isNaN(monthly_payment_s) || monthly_payment_s == "Infinity") monthly_payment_s = "0.00";
	document.getElementById("result_sterling").value = monthly_payment_s;
	return false;
}

/* Calculate the interest only mortgage for given rate, required amount and period */
function getInterestOnlyMonthlyPayment(interest, amount, years) {
	var monthly_payment = (amount * (interest / 100) * years)  / (years * 12);
	return monthly_payment;
}

/* Calculate the repayment mortgage for given rate, required amount and period */
function getRepaymentMonthlyPayment(interest, amount, years) {
	var factor = 1 + (interest/1200);
	var v = Math.pow( factor, (years * 12) );
	var monthly_payment = (amount * interest *  v) / (1200 * (v - 1) );
 	return monthly_payment;
}

