// JavaScript Document
var PV = null;
var MV = null;
var Rt = null;
var change = null;
var R = null;
var property = null;
var mortgage = null;
var v1 = null;
var v2 = null;
var v3 = null;
var e1 = null;
var e3 = null;
var index = null;

function checkEquityBounds()
{	
	checkForEmptyFields();
	if ( !checkNumber( document.EquityCalc.property , 1 , 9999999 , "Property value" ) ||
		!checkNumber( document.EquityCalc.owing , .001 , 9999999 , "Mortgage amount" ) ||
		!checkNumber( document.EquityCalc.growth , 1 , 1000 , "Annual growth" ) ) 
	{
		//return true;
	}
}
	
function computeEquity(form) 
{
	HouseValueCalculations();
	EquityCalculations();
}
	
function extractValues()
{
	var pv, mv;
	pv = document.EquityCalc.property.value;
	pv = pv.split(",");
	pv = pv.join("");
	property = pv;
	mv = document.EquityCalc.owing.value;
	mv = mv.split(",");
	mv = mv.join("");
	mortgage = mv;
	
	PV = parseFloat(pv);
	MV = parseFloat(mv);
	Rt = parseFloat(document.EquityCalc.growth.value);
	change = parseFloat(document.EquityCalc.rtt.value);
	R = (Rt/100)*change;
	document.EquityCalc.property.value = dollarsCents(property);
	document.EquityCalc.owing.value = dollarsCents(mortgage);
}
	
function checkForEmptyFields()
{
	extractValues();
	
	if(PV == null || PV.length == 0 || isNaN(PV))
	{
		document.EquityCalc.property.focus();
	}
	else if(MV == null || MV.length == 0 || isNaN(MV))
	{
		document.EquityCalc.owing.focus(); 
	}
	else if(Rt == null || Rt == 0 || isNaN(Rt)) 
	{
		document.EquityCalc.growth.focus();
	}
	else{
		computeEquity();
	}
}
	
function HouseValueCalculations()
{		
	v1 = (( PV * R ) + PV ).toFixed(0); 
	document.EquityCalc.y1.value = dollarsCents( v1 );
	v2 = (( v1 * R ) + parseFloat( v1 )); 
	v3 = (( v2 * R ) + v2 ).toFixed(0); 
	document.EquityCalc.y3.value = dollarsCents( v3 );
}
	
function EquityCalculations()
{
	e1 = ( v1 - MV ).toFixed(0);
	document.EquityCalc.te.value = dollarsCents( e1 );
	e3 = ( v3 - MV ).toFixed(0);
	document.EquityCalc.te3.value = dollarsCents(e3);	
	greenAndPink();
}
	
function greenAndPink()
{
	if (document.EquityCalc.te.value <= 0)
		document.EquityCalc.te.style.backgroundColor = 'fda1a3';
	else
		document.EquityCalc.te.style.backgroundColor = 'd6fec8';

	if (document.EquityCalc.te3.value <= 0)
		document.EquityCalc.te3.style.backgroundColor = 'fda1a3';
	else
		document.EquityCalc.te3.style.backgroundColor = 'd6fec8';
}
	
	
function checkNumber( input , min , max , msg ) 
{
	msg = msg + " has invalid data: " + input.value;
	var str = input.value;
	for ( var i = 0 ; i < str.length ; i++ ) 
	{
		var ch = str.substring( i , i + 1 )
		if ( ( ch < "0" || "9" < ch ) && ch != '.' ) 
		{
			alert(msg);
			return false;
		}
	}
	var number = 0 + input.value
	if ( number < min || max < number ) {
		alert( msg + ". Value should be in range: " + min + " - " + max );
		return false;
	}
	input.value = str;
	return true;
}	

function dollarsCents( N ) 
{
	S = new String( N );
	var i = S.indexOf( '.' );
	if ( i != -1 ) {
		S = S.substr( 0, i+3 );
		if ( S.length-i < 3 )
			S = S + '0';
	}
	return S;
}

function isRbSelected(lIndex)
{
	index = lIndex;
	var b = document.EquityCalc.rate[index].value;

	document.EquityCalc.rtt.value = dollarsCents(b);
}
