// Begin Spreadsheet calc
function placeFocus() {
if (document.forms.length > 0) {
var field = document.forms[0];
for (i = 0; i < field.length; i++) {
if ((field.elements[i].type == "text") || (field.elements[i].type == "textarea") || (field.elements[i].type.toString().charAt(0) == "s")) {
document.forms[0].elements[i].focus();
break;
         }
      }
   }
}
//  End -->

ns4 = (document.layers)? true:false
ie4 = (document.all)? true:false

function show(id){
	if (ns4) document.layers[id].visibility = "show"
	else if (ie4) document.all[id].style.visibility = "visible"
}

function hide(id){
	if (ns4) document.layers[id].visibility = "hide"
	else if (ie4) document.all[id].style.visibility = "hidden"
}


function checkForZero(field)
{
    if (field.value == 0 || field.value.length == 0) {
        alert ("This field can't be 0!");
        field.focus(); }
    else
        calculatePayment(field.form);
}


function cmdCalc_Click(form)
{
    if (form.purchprice1.value == 0 || form.purchprice1.value.length == 0) {
        alert ("The Price field can't be 0!");
        form.purchprice1.focus(); }
	else if (form.closingcost1.value == 0 || form.closingcost1.value.length == 0) {
        alert ("The Closing Cost field can't be 0!");
        form.closingcost1.focus(); }
    else if (form.intrate1.value == 0 || form.intrate1.value.length == 0) {
        alert ("The Interest Rate field can't be 0!");
        form.intrate1.focus(); }
    else if (form.amort1.value == 0 || form.amort1.value.length == 0) {
        alert ("The Term field can't be 0!");
        form.amort1.focus(); }
    else
        calculatePayment(form);
}

function calculatePayment(form){
	 
	cost = parseInt(form.purchprice1.value,10) + parseInt(form.closingcost1.value,10); 
	tcash = parseInt(form.downpay1.value,10) + parseInt(form.closingcost1.value,10);
	downper = Math.round(((form.downpay1.value/form.purchprice1.value)*100)*100)/100;
	cash = form.rent_month1.value - form.condo_fees1.value - form.taxes1.value - form.manage_fee1.value;
	princ = form.purchprice1.value - form.downpay1.value;    
intRate = (form.intrate1.value/100) / 12;
    months = form.amort1.value * 12;
anncash = form.net_cashflow1.value * 12;
		appr = Math.round(form.purchprice1.value * (form.apprec_percent1.value/100)*100)/100;
reduc = Math.round((((1/form.amort1.value)/2) * princ)*100)/100;
	form.pmt1.value = Math.floor((princ*intRate)/(1-Math.pow(1+intRate,(-1*months)))*100)/100;
    
	form.total_cost1.value = cost;
	form.cash1.value = tcash;
	form.downpay_percent1.value = downper;
	form.mortgage_amt1.value = princ;
    form.cash_recd1.value = cash;
	form.net_cashflow1.value = Math.round((cash - form.pmt1.value)*100)/100;
	form.apprec1.value = appr;
	form.cashflow1.value = Math.round((form.net_cashflow1.value * 12)*100)/100;
	form.princ_reduce1.value = reduc;
	form.netgain1.value = Math.round((anncash + appr + reduc)*100)/100;
	form.gain_percent1.value = Math.round(((form.netgain1.value/tcash)*100)*100)/100;


cost2 = parseInt(form.purchprice2.value,10) + parseInt(form.closingcost2.value,10); 
	tcash2 = parseInt(form.downpay2.value,10) + parseInt(form.closingcost2.value,10);
	downper2 = Math.round(((form.downpay2.value/form.purchprice2.value)*100)*100)/100;
	cash2 = form.rent_month2.value - form.condo_fees2.value - form.taxes2.value - form.manage_fee2.value;
	princ2 = form.purchprice2.value - form.downpay2.value;
    intRate2 = (form.intrate2.value/100) / 12;
    months2 = form.amort2.value * 12;
	appr2 = Math.round(form.purchprice2.value * (form.apprec_percent2.value/100)*100)/100;
	anncash2 = form.net_cashflow2.value * 12;
	reduc2 = Math.round((((1/form.amort2.value)/2) * princ2)*100)/100;
    form.pmt2.value = Math.floor((princ2*intRate2)/(1-Math.pow(1+intRate2,(-1*months2)))*100)/100;
    
	form.total_cost2.value = cost2;
	form.cash2.value = tcash2;
	form.downpay_percent2.value = downper2;
	form.mortgage_amt2.value = princ2;
    form.cash_recd2.value = cash2;
	form.net_cashflow2.value = Math.round((cash2 - form.pmt2.value)*100)/100;
	form.apprec2.value = appr2;
	form.cashflow2.value = Math.round((form.net_cashflow2.value * 12)*100)/100;
	form.princ_reduce2.value = reduc2;
	form.netgain2.value = Math.round((appr2 + anncash2 + reduc2)*100)/100;
	form.gain_percent2.value = Math.round(((form.netgain2.value/tcash2)*100)*100)/100;
}


//-->


//function negreturn(form)
//{
//if (form.net_cashflow1.value < 0 || form.net_cashflow2.value < 0){
//alert ("Negative cash flow. Consider if this is your intent. Will this improve with higher rent, lower mortgage payments or lower expenses in the future ? Or are you OK with it, as you assume that the possible asset appreciation will offset slight negative cash-flow ? Consider lowering your mortgage amount ! Think positive. A slight negative cash-flow is likely OK for some people, but not for others.");
//}
//else if (form.net_cashflow1.value > 0.1 || form.net_cashflow2.value > 0.1){
//	show('comm');
//}
//
//else 
//	hide('comm');
//
//
//}

//-->


// message shown when user tries to change calculated fields
//function no_effect() {
	//alert("Currently Prestigious is preparing a property for sale with unit a sale price between $80,000 & $120,000 CDN ($55,000 to $80,000 USD).")
//}
function no_effect1a() {
	alert("This is a calculated value and can't be changed.")
}