// JavaScript Document
var formBuyItem = 3;
var prevFormId, prevElementId;

var formArray = new Array("homes", "plots", "commercial", "home-rentals", "commercial-rentals", "home-srtl");
var formArrayC = new Array("buy", "rent", "srtl");


function displayForm(formId, formInd) {	
	i = 0; 
	end = formArray.length;	
	
	if(formInd == 'rent'){			
			i = formBuyItem;
            	end = 5;
         
		}
	if(formInd == 'buy'){
            i = 0;
			end = formBuyItem;
			}
			
   	if(formInd == 'srtl'){
			i = 5;
            end = formArray.length;	
			}			
			
	for (i; i < end; i++) {
		document.getElementById('search-'+formArray[i]).style.display = 'none';
		document.getElementById('innerM-'+formArray[i]).className = 'menuLink';
	}
	document.getElementById('search-'+formId).style.display = 'block';
	document.getElementById('innerM-'+formId).className = 'current';
 
   
}

function displayFormC(formId){
	
	for (var i = 0; i < formArrayC.length; i++) {		
		document.getElementById('topM-'+formArrayC[i]).className = 'menuLink';
		document.getElementById('topM-'+formArrayC[i]).firstChild.className = 'nMenuWrap';
		document.getElementById('properties2-'+formArrayC[i]).style.display = 'none';
	}
	
	document.getElementById('topM-'+formId).className = 'current';	
	document.getElementById('topM-'+formId).firstChild.className = 'actMenuWrap';
	document.getElementById('properties2-'+formId).style.display = 'block';
	 
}
 

 
 
function init()
{
    mcalc(document.form_mcalc);   
 
}

// format floating point number to show dollars and cents
function currency2str(f)
{
    var str = "";
    var pos = 1;

    if (f < 0) // negative number
    {
        f = -f
        pos = 0;
    }  

    // extract euros and cents
    var euros = Math.floor(f);
    var cents   = Math.round(100*(f - euros));
    if (cents == 100) 
    {
        euros += 1;
        cents = 0;
    }

    if (pos == 0) str += "("; // show as negative

    if (cents == 0)
        str += euros + ".00";
    else if (cents < 10)
        str += euros + ".0" + cents;
    else
        str += euros + "." + cents;

    if (pos == 0) str += ")"; // show as negative

    return str;
}

// compute the size of a periodic payment to amortize a loan
function monthly_payment( p, apr, n )
{
    var i = apr/1200;
    var m = p*i*Math.pow((1 + i), n)/(Math.pow((1 + i), n) - 1);

    return (0.01*Math.ceil(100*m));   // round up
}

// total interest and payment size for a loan
function mcalc(form_mcalc)
{    
  
    var principal = parseFloat(form_mcalc.principal.value) - parseFloat(form_mcalc.downpayment.value) ;
    var int_rate  = parseFloat(form_mcalc.interestrate.value);
    var months1   = parseInt(form_mcalc.years.value)*12;
    var payment   = monthly_payment( principal, int_rate, months1 ) ;
    var interest  = parseFloat("0");

    // compute total interest paid
    while (principal > 0.0)
    {
        var ip    = 0.01*Math.round(principal*int_rate/12);
        principal = principal + ip - payment;
        interest += ip;
    }

	 
	var amount = currency2str(payment);
	if(amount =="NaN.NaN") form_mcalc.payment.value   = "N/A"
	 else    form_mcalc.payment.value   = amount ;
   //form_mcalc.interest1.value = currency2str(interest);
}



function mcalc2(form_mcalc)
{    
 
    var principal = parseFloat(document.getElementById('principal').value) - parseFloat(document.getElementById('downpayment').value) ;
    var int_rate  = parseFloat(document.getElementById('interestrate').value);
    var months1   = parseInt(document.getElementById('years').value)*12;
    var payment   = monthly_payment( principal, int_rate, months1 ) ;
    var interest  = parseFloat("0");
 
    // compute total interest paid
    while (principal > 0.0)
    {
        var ip    = 0.01*Math.round(principal*int_rate/12);
        principal = principal + ip - payment;
        interest += ip;
    }

	 
	var amount = currency2str(payment);
	if(amount =="NaN.NaN") document.getElementById('payment').value  = "N/A"
	 else    document.getElementById('payment').value  = amount ;
 
}
