var totalamt;
var today = new Date();
var expires = new Date(today.getTime() + (56 * 86400000));

function Set_Cookie(name,value,expires,path,domain,secure) {
    document.cookie = name + "=" +escape(value) +
        ( (expires) ? ";expires=" + expires.toGMTString() : "") +
        ( (path) ? ";path=" + path : "") +
        ( (domain) ? ";domain=" + domain : "") +
        ( (secure) ? ";secure" : "");
}

function Get_Cookie(name) {
    var start = document.cookie.indexOf(name+"=");
    var len = start+name.length+1;
    if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
    if (start == -1) return null;
    var end = document.cookie.indexOf(";",len);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(len,end));
}


function Delete_Cookie(name,path,domain) {
    if (Get_Cookie(name)) document.cookie = name + "=" +
        ( (path) ? ";path=" + path : "") +
        ( (domain) ? ";domain=" + domain : "") +
        ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}


function stripCurrency(n){
	n = n.toString().replace(/\_|\,|\s/g,'');
	return n;
}


function formatCurrency(n) {
	// remove sign and comma
	n = n.toString().replace(/\_|\,/g,'');
	if(isNaN(n)){n = "0";}
	sign = (n == (n = Math.abs(n)));
	// round up
	n = Math.floor(n*100+0.50000000001);
	n = Math.floor(n/100).toString();
	for (var i=0; i<Math.floor((n.length-(1+i))/3);i++){
		n = n.substring(0,n.length-(4*i+3))+','+ n.substring(n.length-(4*i+3));
	}
	return (((sign)?'':'-') + n);
}


function calculate(){
    /*
	var solo = 5;
	var joint = 4;
	var income1 = stripCurrency(document.getElementById("mortgage_calculator_income1").value);
	var income2 = stripCurrency(document.getElementById("mortgage_calculator_income2").value);
	if(isNaN(income1) || isNaN(income2)){
		alert("Please enter correct number");
	}else{
		if(income2 == 0 || income2 == ""){
			totalamt = income1 * solo;
		}else{
			totalamt1 = income1 * solo;
			totalamt = (parseInt(income1)+parseInt(income2)) * joint;
			if(totalamt1 > totalamt){
				totalamt = totalamt1;
			}
		}
		document.getElementById("mortgage_calculator_loanamount").innerHTML = "&pound;"+formatCurrency(totalamt);
		Set_Cookie("2mloanamount",formatCurrency(totalamt),expires,'/');
	}
	*/
	var income1 = stripCurrency(document.getElementById("mortgage_calculator_income1").value);
	var income2 = stripCurrency(document.getElementById("mortgage_calculator_income2").value);
	var income3 = stripCurrency(document.getElementById("mortgage_calculator_income3").value);
    var totalamt = 0; 
    
    income1 = parseInt(income1);
    income2 = parseInt(income2);
    income3 = parseInt(income3);
    
    if (income1 > 0 && income2 > 0 && income3 > 0) {
        totalamt = income1*3 + income2 + income3;
    } else if (income1 > 0 && income2 > 0) {
        totalamt = income1*3 + income2;
    } else if (income1 > 0 && income3 > 0) {
        totalamt = income1*4 + income3;
    } else if (income1 > 0) {
        totalamt = income1*4;
    } else {
        totalamt = 0;
    }
	document.getElementById("mortgage_calculator_loanamount").innerHTML = "&pound;"+formatCurrency(totalamt);
	Set_Cookie("2mloanamount",formatCurrency(totalamt),expires,'/');
}

function calculate2() {
	var r_payment = document.getElementById("mortgage_calculator_result_payment");
	var r_saving = document.getElementById("mortgage_calculator_result_saving");
	var r_potential = document.getElementById("mortgage_calculator_result_potential");
	var balance = 1*stripCurrency(document.getElementById("mortgage_calculator_balance").value);
	var payment = 1*stripCurrency(document.getElementById("mortgage_calculator_payment").value);
	var term = parseInt(document.getElementById("mortgage_calculator_term").value);
	
	var tmp = (balance + 5000) * (3.99/1200);
	var tmp1 = payment - tmp;
	var tmp2 = term * tmp1 * 12;
	
	r_payment.innerHTML = "&pound;"+formatCurrency(tmp);
	r_saving.innerHTML = "&pound;"+formatCurrency(tmp1);
	r_potential.innerHTML = "&pound;"+formatCurrency(tmp2);
}

function reset_calculator(){
	var income1 = document.getElementById("mortgage_calculator_income1");
	var income2 = document.getElementById("mortgage_calculator_income2");
	var income3 = document.getElementById("mortgage_calculator_income3");
	var loanamount = document.getElementById("mortgage_calculator_loanamount");
	income1.value = '';
	income2.value = '';
	income3.value = '';
	loanamount.innerHTML = '';
}

function reset_calculator2(){
	var f1 = document.getElementById("mortgage_calculator_result_payment");
	var f2 = document.getElementById("mortgage_calculator_result_saving");
	var f3 = document.getElementById("mortgage_calculator_result_potential");
	var f4 = document.getElementById("mortgage_calculator_balance");
	var f5 = document.getElementById("mortgage_calculator_payment");
	var f6 = document.getElementById("mortgage_calculator_term");
	f1.innerHTML = '';
	f2.innerHTML = '';
	f3.innerHTML = '';
    f4.value = '';
    f5.value = '';
    f6.value = '';
}

function get_loanamount(){
	var loanamt = Get_Cookie('2mloanamount');
	if (loanamt != null) {
		document.getElementById('mortgage_principal').value = loanamt;
	}
}

