function displayNone(div){
      document.getElementById(div).style.display = 'none';
}

function calcSteps(stepId){

var form_name = "homepageForm";
var carryOn = true;
var errorDiv = "errorPopup";
var errorMsg = "";

switch(stepId){

case 1:

    var amountToBorrow =  document.forms[form_name].amountToBorrow.value;
    var propertyValue =  document.forms[form_name].propertyValue.value;

if( amountToBorrow.length == 0 ){
	document.getElementById('amountToBorrow').style.border = '1px solid #669700';
	errorMsg+= "<li>amount to borrow</li>";
	carryOn = false;
}
else{
	document.getElementById('amountToBorrow').style.border = '1px solid #c3c3c3';
}

if( propertyValue.length == 0 ){
	document.getElementById('propertyValue').style.border = '1px solid #669700';
	errorMsg+= "<li>property value</li>";
	carryOn = false;
}
else{
	document.getElementById('propertyValue').style.border = '1px solid #c3c3c3';
}


break;

case 2:

    var fname =  document.forms[form_name].fname.value;
    var lname =  document.forms[form_name].lname.value;

if( fname.length == 0 ){
	document.getElementById('fname').style.border = '1px solid #669700';
	errorMsg+= "<li>first name</li>";
	carryOn = false;
}
else{
	document.getElementById('fname').style.border = '1px solid #c3c3c3';
}

if( lname.length == 0 ){
	document.getElementById('lname').style.border = '1px solid #669700';
	errorMsg+= "<li>last name</li>";
	carryOn = false;
}
else{
	document.getElementById('lname').style.border = '1px solid #c3c3c3';
}

break;

case 3:

    var telephone =  document.forms[form_name].telephone.value;
    var email =  document.forms[form_name].email.value;

if( telephone.length == 0 ){
	document.getElementById('telephone').style.border = '1px solid #669700';
	errorMsg+= "<li>telephone number</li>";
	carryOn = false;
}
else{
	document.getElementById('telephone').style.border = '1px solid #c3c3c3';
}

if( email.length == 0 ){
	document.getElementById('email').style.border = '1px solid #669700';
	errorMsg+= "<li>email</li>";
	carryOn = false;
}
else{
	document.getElementById('email').style.border = '1px solid #c3c3c3';
}

var validationRule = new RegExp(/^[^@]+@([-\w]+\.)+[A-Za-z]{2,4}$/);

if (!validationRule.test(email)){ 
	document.getElementById('email').style.border = '1px solid #669700';
	errorMsg+= "<li>email address not valid</li>";
	carryOn = false;
}

else{
	document.getElementById('email').style.border = '1px solid #c3c3c3';
}


break;

}

if(carryOn == true && stepId < 3){

divRef = "calc_container_" + stepId;

document.getElementById(divRef).style.display = 'none';

divRef = "calc_container_" + ( stepId + 1 );

document.getElementById(divRef).style.display = 'block';

}

else if(carryOn == true && stepId == 3){

// submit form

document.forms[form_name].submit();

}

else{

// return errors

var content = "<div id=\"errorPopupHeader\">&nbsp;</div><div id=\"errorPopupContent\"><p style=\"padding-left: 15px;\">Please correct the following errors;<ul>"+errorMsg+"</ul><div></p><div style=\"float: right; width: 34px; text-align: right; margin-right: 15px;\"><a onclick=\"return displayNone('"+errorDiv+"');\" style=\"cursor: pointer\"><img src=\"images/ok_button.gif\" alt=\"OK\" /></a></div></div>";
// popupLoad(errorDiv, 200, content);

var popUpWidth = 321;
var popUpHeight = 50;

popUp = document.getElementById(errorDiv);

var scrollingLeft = Math.round((document.documentElement.clientWidth/2)-(popUp.style.width/2)) - (popUpWidth / 2);
var scrollingTop = Math.round((document.documentElement.clientHeight/2)-(popUp.style.height/2)+document.documentElement.scrollTop) - (popUpHeight / 2);

popUp.innerHTML=content;
popUp.style.display = 'block';
popUp.style.zIndex = '1';
popUp.style.top = scrollingTop+'px';
popUp.style.left = scrollingLeft+"px";

return false;

}

}

function calcStepBack(stepId){

divRef = "calc_container_" + ( stepId + 1 );

document.getElementById(divRef).style.display = 'none';

divRef = "calc_container_" + ( stepId );

document.getElementById(divRef).style.display = 'block';

}

function closeRow(eid){

element = 'element_' + eid;
row = 'row_' + eid;

document.getElementById(row).style.display = 'none';
document.getElementById(element).style.height = '0px';

}

function showBackground(eid){

cell1 = 'cell_' + eid + '_1';
cell2 = 'cell_' + eid + '_2';
cell3 = 'cell_' + eid + '_3';
cell4 = 'cell_' + eid + '_4';
cell5 = 'cell_' + eid + '_5';
cell6 = 'cell_' + eid + '_6';
cell7 = 'cell_' + eid + '_7';
img = 'img_' + eid;

document.getElementById(cell1).style.backgroundImage = "url(../images/best_buy_td_bg.gif)";
document.getElementById(cell1).style.backgroundRepeat="repeat-x";

document.getElementById(cell2).style.backgroundImage = "url(../images/best_buy_td_bg.gif)";
document.getElementById(cell2).style.backgroundRepeat="repeat-x";

document.getElementById(cell3).style.backgroundImage = "url(../images/best_buy_td_bg.gif)";
document.getElementById(cell3).style.backgroundRepeat="repeat-x";

document.getElementById(cell4).style.backgroundImage = "url(../images/best_buy_td_bg.gif)";
document.getElementById(cell4).style.backgroundRepeat="repeat-x";

document.getElementById(cell5).style.backgroundImage = "url(../images/best_buy_td_bg.gif)";
document.getElementById(cell5).style.backgroundRepeat="repeat-x";

document.getElementById(cell6).style.backgroundImage = "url(../images/best_buy_td_bg.gif)";
document.getElementById(cell6).style.backgroundRepeat="repeat-x";

document.getElementById(cell7).style.backgroundImage = "url(../images/best_buy_td_bg.gif)";
document.getElementById(cell7).style.backgroundRepeat="repeat-x";

document.getElementById(img).src="images/more-details-green.gif";

}

function hideBackground(eid,imgColor){

cell1 = 'cell_' + eid + '_1';
cell2 = 'cell_' + eid + '_2';
cell3 = 'cell_' + eid + '_3';
cell4 = 'cell_' + eid + '_4';
cell5 = 'cell_' + eid + '_5';
cell6 = 'cell_' + eid + '_6';
cell7 = 'cell_' + eid + '_7';
img = 'img_' + eid;

document.getElementById(cell1).style.backgroundImage = 'none';
document.getElementById(cell2).style.backgroundImage = 'none';
document.getElementById(cell3).style.backgroundImage = 'none';
document.getElementById(cell4).style.backgroundImage = 'none';
document.getElementById(cell5).style.backgroundImage = 'none';
document.getElementById(cell6).style.backgroundImage = 'none';
document.getElementById(cell7).style.backgroundImage = 'none';

document.getElementById(img).src='images/more-details'+imgColor+'.gif';

}

function calcPaymentsOnRate(mortgageTerm,interestRate,loanRequired){

var termMonths = mortgageTerm * 12;
var rate2 = interestRate / 1200;
var calc1a = 1+rate2;
var calc1 = Math.pow(calc1a,termMonths);
var calc2 = loanRequired * rate2;
var calc3 = (1-(1/calc1));
var monthlyPayment = calc2 / calc3;

return monthlyPayment;

}

function calcSavings(){

var type = document.forms["savingsCalc"].mortgageType.value;

var loanRequired =  document.forms["savingsCalc"].mortgageBalance.value;
var newRate =  document.forms["savingsCalc"].newRate.value;

var mortgageTerm =  document.forms["savingsCalc"].mortgageTerm.value;
var mortgagePayments =  document.forms["savingsCalc"].mortgagePayments.value;
var mortgageRate =  document.forms["savingsCalc"].mortgageRate.value;

// strip commas

loanRequired = loanRequired.replace(/,/g,"");
mortgagePayments = mortgagePayments.replace(/,/g,"");

if(type == 'interest'){

var monthlyPayment = loanRequired * (newRate / 100) / 12;

if(mortgagePayments.length == 0 && mortgageRate.length > 0){

mortgagePayments = loanRequired * (mortgageRate / 100) / 12;

}

}

if(type == 'repayment'){

var monthlyPayment = calcPaymentsOnRate(mortgageTerm,newRate,loanRequired);

if(mortgagePayments.length == 0 && mortgageRate.length > 0){

mortgagePayments = calcPaymentsOnRate(mortgageTerm,mortgageRate,loanRequired);

}

}

document.getElementById("result1a").innerHTML = '&pound;' + CommaFormatted(monthlyPayment.toFixed(2));

var monthlySavings = mortgagePayments - monthlyPayment;

var annualSavings = monthlySavings * 12;

if(monthlySavings < 0){

document.getElementById("result1b").innerHTML = '<span style="color: #ff0000;">&pound;' + CommaFormatted(monthlySavings.toFixed(2)) + '</span>';
document.getElementById("result1c").innerHTML = '<span style="color: #ff0000;">&pound;' + CommaFormatted(annualSavings.toFixed(2)) + '</span>';
document.getElementById("negativeResult").innerHTML = '<span style="width: 100%; text-align: center; font-size: 0.6875em;">According to the information you have entered the remortgage will increase your monthly payments, <a href="contact-remortgage-made-easy.htm">contact us</a> to discuss alternative options*</span>';

}

else{

document.getElementById("result1b").innerHTML = '&pound;' + CommaFormatted(monthlySavings.toFixed(2));
document.getElementById("result1c").innerHTML = '&pound;' + CommaFormatted(annualSavings.toFixed(2));

document.getElementById("negativeResult").innerHTML = '';

}

}

function calcBorrowings(lowerRate,higherRate){

var salary1 =  document.forms["borrowCalc"].salary1.value;
var salary2 =  document.forms["borrowCalc"].salary2.value;

// strip commas

salary1 = salary1.replace(/,/g,"");
salary2 = salary2.replace(/,/g,"");


salary1 = parseInt(salary1);
salary2 = parseInt(salary2);

if(isNaN(salary1) == false && isNaN(salary2) == false){
var combined = salary1 + salary2;
}

else if(isNaN(salary1) == false){
var combined = salary1;
}

else{
alert("Please enter required values!");
return false;
}

var lower = combined * lowerRate;
var higher = combined * higherRate;

document.getElementById("result2a").innerHTML = '&pound;' + CommaFormatted(lower.toFixed(2));
document.getElementById("result2b").innerHTML = '&pound;' + CommaFormatted(higher.toFixed(2));

}

function calcSavings2(){

var type = document.forms["savingsCalc2"].mortgageType.value;

var loanRequired =  document.forms["savingsCalc2"].mortgageBalance.value;
var newRate =  document.forms["savingsCalc2"].newRate.value;

var mortgageTerm =  document.forms["savingsCalc2"].mortgageTerm.value;

// strip commas
loanRequired = loanRequired.replace(/,/g,"");

if(type == 'interest'){

var monthlyPayment = loanRequired * (newRate / 100) / 12;

}

if(type == 'repayment'){

var monthlyPayment = calcPaymentsOnRate(mortgageTerm,newRate,loanRequired);

}

document.getElementById("result1a2").innerHTML = '&pound;' + CommaFormatted(monthlyPayment.toFixed(2));

}

function hideDivs(totalDivs){

var div;

totalDivs = totalDivs - 1;

for(i=1;i<=totalDivs;i++){

div = 'element_'+i;

if(document.getElementById(div)){
document.getElementById(div).style.height = '0px';
}

}

}

function CommaFormatted(amount)
{
	var delimiter = ","; // replace comma if desired
	var a = amount.split('.',2)
	var d = a[1];
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	if(d.length < 1) { amount = n; }
	else { amount = n + '.' + d; }
	amount = minus + amount;
	return amount;
}
