if (document.images)
{
	var properties0 = new Image();
	properties0.src  = "images/imgTabProperties.gif";
	var properties1 = new Image();
	properties1.src  = "images/imgTabPropertiesOn.gif";
	var search0 = new Image();
	search0.src = "images/imgTabSearchMLS.gif";
	var search1 = new Image();
	search1.src = "images/imgTabSearchMLSOn.gif";
	var calculator0 = new Image();
	calculator0.src = "images/imgTabCalculator.gif";
	var calculator1 = new Image();
	calculator1.src = "images/imgTabCalculatorOn.gif";
	var value0 = new Image();
	value0.src = "images/imgTabHomeValue.gif";
	var value1 = new Image();
	value1.src = "images/imgTabHomeValueOn.gif";
}


function rollOut(imgName) 
{
    if (document.images) 
    {
        document[imgName].src = eval(imgName+"0.src");
    }
}

function rollIn(imgName)
{
    if (document.images)
    {
        document[imgName].src = eval(imgName+"1.src");
    }
}

function isEmail(string) 
{
	if (!string)
	{
		return false;
	}
	
	var iChars = "*|,\":<>[]{}`\';()&$#%";
	
	for (var i = 0; i < string.length; i++) 
	{
		if (iChars.indexOf(string.charAt(i)) != -1)
		{
			return false;
		}
	}
	return true;
}                      
                    
function isReady(sForm, arrFields) 
{
    for (var i = 0; i < arrFields.length; i++) 
    {      
    	var sFieldName = arrFields[i][0];
    	var sFieldType = arrFields[i][1];
    	var sFieldLabel = arrFields[i][2];    	
    	var sField = sForm[sFieldName];
    	
    	if (sFieldType == "text")
    	{
    		if (sField.value == "")
    		{
    			alert('Please enter ' + sFieldLabel);
    			sField.focus();
        	return false;
    		}
    	}
    	else if (arrFields[i][1] == "email")
    	{
    		if (sField.value == "" || !isEmail(sField.value))
    		{
    			alert('Please enter ' + sFieldLabel);
    			sField.focus();
        	return false;
    		}
    	}
    	else if (arrFields[i][1] == "select")
    	{
    		
    	}
    }
    
    return true;
}

function showContent(sContent)
{
	var objLocation;
	var sElement;
	
	if(document.getElementById)
	{
		sElement = "document.getElementById('" + sContent + "')";
	}
	else if(document.all)
	{
		sElement = "document.all['" + sContent + "']";
	}
	
	objLocation = eval(sElement);
	
	if (objLocation)
	{
		objLocation.style.display = '';
		objLocation.style.visibility = 'visible';
	}
}

function hideContent(sContent)
{
	var objLocation;
	var sElement;
	
	if(document.getElementById)
	{
		sElement = "document.getElementById('" + sContent + "')";
	}
	else if(document.all)
	{
		sElement = "document.all['" + sContent + "']";
	}
	
	objLocation = eval(sElement);
	
	if (objLocation)
	{
		objLocation.style.display = 'none';
		objLocation.style.visibility = 'hidden';
	}
}

function getBaloonAmount( ) {
	/* 

		B: baloon amount	

   		A: monthly payment
		P: loan amount
		i: interest
		n: loan period
		b: baloon months
		PU: period unit: n is Y or M

		j : current month
		LP: remaining loan amount
		PP: loan amount paid this month
		II: interest paid this month
		Nn: months remaining
		sd: start date of loan
		py: payments per year

	*/
	if (checkForMissingData(['A','P','i','n'])) { return; }
	var A,P,i,n;
	var j,LP,PP,II,Nn,sd,py,B;
	f = document.form;
	A = Number(f.A.value);
	P = Number(f.P.value);
	i = Number(f.i.value);
	n = Number(f.n.value);
	b = Number(f.b.value);
	LP = P;
	Nn = n;
	b = b*12;
	sd = new Date( );
	py = 12;
	for(j=1;j<=b;j++){
		sd.setMonth(sd.getMonth()+1);
		II=LP*i/py/100;
		LP+=II;
		PP=A-II;
		LP-=A;
	}
	B = LP;
	return B;
}

function amortize( ) {
	/* 
		M: selected month
		Y: selected year

   		A: monthly payment
		P: loan amount
		i: interest
		n: loan period
		b: baloon start month 

		j : current month
		LP: remaining loan amount
		PP: loan amount paid this month
		II: interest paid this month
		Nn: months remaining
		sd: start date of loan
		py: payments per year

	*/
	if (checkForMissingData(['A','P','i','n'])) { return; }
	var A,P,i,n,b;
	var j,LP,PP,II,Nn,sd,py;
	var txt = '';
	f = document.form;
	A = Number(f.A.value);
	P = Number(f.P.value);
	i = Number(f.i.value);
	n = Number(f.n.value);
	b = Number(f.b.value);
	LP = P;
	b = b*12;
	n = n*12;	
	if (b) {
		Nn = b;
	}
	else {
		Nn = n;
	}
	py = 12;
	txt+='<p><b>Amortization Start:</b></p>';
	txt+='<table width="100%" cellpadding="0" cellspacing="0" border="0"><tr><td bgcolor="#999999"><img src="images/imgSpacer.gif" height="1" width="1" alt=""></td></tr></table>';
	txt+='<table width="100%" cellpadding="0" cellspacing="0" border="0">';
	txt+='<tr><td width="1" bgcolor="#999999"><img src="images/imgSpacer.gif" height="1" width="1" alt=""></td><td width="10" bgcolor="#FFFFFF">&nbsp;</td><td class="bodyTextSm" bgcolor="#FFFFFF"><b>Month</td><td class="bodyTextSm" bgcolor="#FFFFFF"><b>Payment</b></td><td class="bodyTextSm" bgcolor="#FFFFFF"><b>Interest</b></td><td class="bodyTextSm" bgcolor="#FFFFFF"><b>Principal</b></td><td class="bodyTextSm" bgcolor="#FFFFFF"><b>Balance</b></td><td width="1" bgcolor="#999999"><img src="images/imgSpacer.gif" height="1" width="1" alt=""></td></tr>';
	for(j=1;j<=Nn;j++){
		II=LP*i/py/100;
		LP+=II;
		PP=A-II;
		LP-=A;
		
		var sColor = 'FFFFFF';
		if (j%2 != 0)
		{
			sColor = 'EFEFEF';
		}	
		
		txt+='<tr>';
		txt+='<td width="1" bgcolor="#999999"><img src="images/imgSpacer.gif" height="1" width="1" alt=""></td>';
		txt+='<td width="10" bgcolor="#'+sColor+'">&nbsp;</td>';
		txt+='<td bgcolor="#'+sColor+'" class="bodyTextSm">'+j+"</td>";
		txt+='<td bgcolor="#'+sColor+'" class="bodyTextSm">'+convertVariableToMoney(A)+'</td>';
		txt+='<td bgcolor="#'+sColor+'" class="bodyTextSm">'+convertVariableToMoney(II)+'</td>';
		txt+='<td bgcolor="#'+sColor+'" class="bodyTextSm">'+convertVariableToMoney(PP)+'</td>';
		txt+='<td bgcolor="#'+sColor+'" class="bodyTextSm">'+convertVariableToMoney(LP)+'</td>';
		txt+='<td width="1" bgcolor="#999999"><img src="images/imgSpacer.gif" height="1" width="1" alt=""></td></tr>';
	}
	txt+='</table>';
	txt+='<table width="100%" cellpadding="0" cellspacing="0" border="0"><tr><td bgcolor="#999999"><img src="images/imgSpacer.gif" height="1" width="1" alt=""></td></tr></table>';
	
	var sBrowserName = navigator.userAgent.toLowerCase(); 
	
	var bIE = (sBrowserName.indexOf('msie') != -1);
	
	if (bIE)
	{
		var amortSection = amortStart;
	}
	else
	{
		var amortSection = document.getElementById('amortStart');
	}
	amortSection.innerHTML = txt;
}

function convertVariablesToIntegers( ) {
	f = document.form;
	f.P.value=f.P.value.replace(/[^\d\.]/g,"");
	f.i.value=f.i.value.replace(/[^\d\.]/g,"");
	f.n.value=f.n.value.replace(/[^\d\.]/g,"");
	f.A.value=f.A.value.replace(/[^\d\.]/g,"");
	f.F.value=f.F.value.replace(/[^\d\.]/g,"");
	f.b.value=f.b.value.replace(/[^\d\.]/g,"");
	f.price.value=f.price.value.replace(/[^\d\.]/g,"");
	f.percentage.value=f.percentage.value.replace(/[^\d\.]/g,"");
}

function checkForMissingData(inputs) {
	for (var i=0;i<inputs.length;i++) {
		input = inputs[i];
		if (document.form[input].value) {
			continue;
		}
		else {
			setMissingDataError( );
			return 1;
		}
	}
	unsetMissingDataError( );
	return 0;
}

function checkForPaymentLessThanPresentValue( ) {
	/* 
   		A: monthly payment
		P: loan amount
	*/
	var A, P;
	f = document.form;
	A = f.A.value;
	P = f.P.value;
	if (Number(A) > Number(P)) {
		setPaymentLessThanPresentValueError( );
		return 0;
	}
	else {
		unsetPaymentLessThanPresentValueError( );
		return 1;
	}
}

function setMissingDataError( ) {
	e = document.getElementById('errorBox');
	c = document.getElementById('requiredInputMissingError');
	e.style.display	= 'block';
	c.style.display = 'block';
}

function setPaymentLessThanPresentValueError( ) {
	e = document.getElementById('errorBox');
	c = document.getElementById('paymentLessThanPresentValueError');
	e.style.display	= 'block';
	c.style.display = 'block';
}

function unsetMissingDataError( ) {
	e = document.getElementById('errorBox');
	c = document.getElementById('requiredInputMissingError');
	e.style.display	= 'none';
	c.style.display = 'none';
}

function unsetPaymentLessThanPresentValueError( ) {
	e = document.getElementById('errorBox');
	c = document.getElementById('paymentLessThanPresentValueError');
	e.style.display	= 'none';
	c.style.display = 'none';
}

function setResults( ) {
	/* 
   		RA,A: monthly payment 
		RP,P: loan amount
		Ri,i: interest
		Rn,n: loan period
		RF,F: future value
		RI: interest amount
		RT: total amount
		B: baloon amount
	*/
		
	showContent('resultFields');	
	
	f = document.form;
	f.RA.value = convertVariableToMoney(f.A.value);
	f.RP.value = convertVariableToMoney(f.P.value);
	f.Ri.value = round(f.i.value);
	f.Rn.value = round(f.n.value);
	f.RF.value = convertVariableToMoney(f.F.value);
	var A,P,r,RI,RT,B,price,percentage, showAmort;
	A = f.A.value;
	P = f.P.value;
	n = f.n.value;
	b = f.b.value;
	price = f.price.value;
	percentage = f.percentage.value;
	showAmort = f.amort.checked;
	
	n = n*12;	
	if (b) {
		B = getBaloonAmount( );
		RT = A * b + B;
	}
	else {
		B = Number(0.00);
		RT = A * n;
	}
	RI = RT - P;
	f.B.value = convertVariableToMoney(B);
	f.RI.value = convertVariableToMoney(RI);
	f.RT.value = convertVariableToMoney(RT);
	f.rprice.value = convertVariableToMoney(price);
	f.rpercentage.value = percentage;
	
	if (percentage < 20)
	{
		showContent('pmi');
	}
	else
	{
		hideContent('pmi');
	}
	
	if (showAmort)
	{
		showContent('amortStart');
		amortizeCommand();
	}
	else
	{
		hideContent('amortStart');
	}
	
}


function commify(variable) {
	/*
		Commify function not copyright Steven Alyari.  Instead:

		This JavaScript takes an input number and adds commas in the proper places.
		As needed by its original purpose, also adds a dollar.
		Copyright 1998, David Turley <dturley@pobox.com>
		Feel free to use and build on this code as long as you include this notice.
		Last Modified March 3, 1998 

		Newly revised by Steven Alyari: 2004
	*/
	var Num = String(variable);
	var newNum = "";
	var newNum2 = "";
	var count = 0;

	//check for decimal number
	if (Num.indexOf('.') != -1){ //number ends with a decimal point
	if (Num.indexOf('.') == Num.length-1){
		Num += "00";
	}
	if (Num.indexOf('.') == Num.length-2){ //number ends with a single digit
		Num += "0";
	}
	var a = Num.split(".");
	Num = a[0]; //the part we will commify
	var end = a[1] //the decimal place we will ignore and add back later
	}
	else {var end = "00";}

	//this loop actually adds the commas
	for (var k = Num.length-1; k >= 0; k--){
		var oneChar = Num.charAt(k);
		if (count == 3){
			newNum += ",";
			newNum += oneChar;
			count = 1;
			continue;
		}
		else {
			newNum += oneChar;
			count ++;
		}
	} //but now the string is reversed!
	//re-reverse the string
	for (var k = newNum.length-1; k >= 0; k--){
		var oneChar = newNum.charAt(k);
		newNum2 += oneChar;
	}

	// add dollar sign and decimal ending from above
	newNum2 = "$" + newNum2 + "." + end;
	return newNum2;	
}

function convertVariablesToMonies( ) {
	/* 
   		RA: monthly payment 
		RP: loan amount
		Ri: interest
		Rn: loan period
		RF: future value
		RI: interest amount
		RT: total amount
	*/
	/*
	f = document.form;
	f.RF.value = convertVariableToMoney(f.RF.value);
	f.RA.value = convertVariableToMoney(f.RA.value);
	f.RI.value = convertVariableToMoney(f.RI.value);
	f.RT.value = convertVariableToMoney(f.RT.value);
	*/	
}

function round(variable) {
	return Math.round(variable * 100) / 100;
}

function convertVariableToMoney(variable) {
	if (variable==null) {
		return variable;
	}
	else {
		variable = round(variable);
		variable = commify(variable);
		return variable;
	}
}

function solveForPayment( ) {
	/* 
   		A: monthly payment (solving)
		P: loan amount
		i: interest
		n: loan period
	*/
	if (checkForMissingData(['i','n', 'price', 'percentage'])) { return; }
	
	var A, P, i, n, price, percentage;
	f = document.form;
	percentage = f.percentage.value/100;
	price = f.price.value;
	P = price - (percentage * price);
	f.P.value = P;
	i = f.i.value / 12 / 100;
	n = f.n.value;
	pow = Math.pow(1+i,n*12);
	A = P*((i*pow)/(pow - 1));
	f.A.value = A;
	setResults( );
}

function applyNewtonRaphsonMethodForInterestRate(i,A,P,n) {
	/* 
   		A: monthly payment 
		P: loan amount
		i: guessed interest 
		n: loan period
		i2 = next guessed interst (solving)
		Fi: interest function
		Fii: interest function derivative
	*/
	var Fi,Fii,i2;
	Fi = 1 - (Math.pow((1+i),(-n))) - ((P/A) * i);
	Fii = (n*(Math.pow((1+i),(-n-1)))) - (P/A);
	i2 = i - (Fi/Fii);
	return i2
}

function solveForInterestRate( ) {
	/* 
   		A: monthly payment 
		P: loan amount
		i: interest (solving)
		n: loan period
	*/
	if (checkForMissingData(['P','A','n'])) { return; }
	else if (!checkForPaymentLessThanPresentValue( ))  { return; }
	var A, P, i, n;
	var diff, newdiff,temp;
	f = document.form;
	A = f.A.value;
	P = f.P.value;
	n = f.n.value;
	n = n*12;	
	diff = 10000;
	i = 0.1;
	A = new Number(A);
	P = new Number(P);
	n = new Number(n);
	i = new Number(i);
	i = 0.1;
	for (var k=0;k<20;k++) {
		temp = i;
		i = applyNewtonRaphsonMethodForInterestRate(i,A,P,n);
		newdiff = Math.abs(i-temp);
		if (diff <= newdiff) { 
			break; 
		}
		diff = newdiff;
	}
	f.i.value = i * 100 * 12;
	setResults( );
}

function applyNewtonRaphsonMethod(x,p,n) {
	/* 
   		A,x: monthly payment 
		P,p: loan amount
		i: interest (solving)
		n: loan period 
	*/
	var k = 0;
	var i = new Array( );
	var diff,diff2;
	i[k] = 0.003846;
	for (k=0;k<100;k++) {
	//	diff = Math.abs(i[k+1]-i[k]);
		t1 = (x*(1 - (i[k]*p/x)));
		t2 = (n*i[k] + Math.log(1 - (i[k]*p/x)));
		t3 = (((x*n)*(1-i[k]*p/x)) - p);
		i[k+1] = i[k] - ((t1*t2)/t3);
	//	diff2 = Math.abs(i[k+1] - i[k]);
	//	if (diff2 > diff) {
	//		break;
	//	}
	}
	return i[5]*12;
}

function solveForMonths( ) {
	/* 
   		A: monthly payment 
		P: loan amount
		i: interest
		n: loan period (solving) 
	*/
	if (checkForMissingData(['P','i','A'])) { return; }
	else if (!checkForPaymentLessThanPresentValue( ))  { return; }
	var A, P, i, n;
	f = document.form;
	A = f.A.value;
	P = f.P.value;
	i = f.i.value / 12 / 100;
	n = Math.log(A/(A-(P*i))) / Math.log(1+i);
	n = n/12;
	f.n.value = n;
	setResults( );
}

function solveForPresentValue( ) {
	/* 
   		A: monthly payment 
		P: loan amount (solving)
		i: interest
		n: loan period
	*/
	if (checkForMissingData(['A','i','n'])) { return; }
	var A, P, i, n;
	f = document.form;
	A = f.A.value;
	i = f.i.value / 12 / 100;
	n = f.n.value;
	n = n*12;
	pow = Math.pow(1+i,n);
	P = (A*(pow - 1))/(i*pow);
	f.P.value = P;
	setResults( );
}

function solveForFutureValue( ) {
	/* 
		P: loan amount 
		i: interest
		n: loan period
		F: future value (solving)
	*/
	if (checkForMissingData(['P','i','n'])) { return; }
	var P, i, n, F;
	f = document.form;
	P = f.P.value;
	i = f.i.value / 12 / 100;
	n = f.n.value;
	n = n*12;
	pow = Math.pow(1+i,n);
	F = P * pow;
	f.F.value = F;
	setResults( );
}

function main(arg) {
	switch(arg) {
		case 'Init': init( ); break;
		case 'SwitchSolveFor': switchSolveForCommand( ); break;
		case 'Solve': solveCommand( ); break;
		case 'Amortize': solveCommand( );  amortizeCommand( ); break;
	}
}

var solveForInfoBoxState;

function disableAmortized( ) {
	e = document.form.M.disabled=true;
	e = document.form.Y.disabled=true;
}

function init( ) {
	switchSolveForCommand( );
	setAmortizeDefaultDate( );
}

function setAmortizeDefaultDate( ) {
	var date = new Date( );
	f = document.form;
	f.Y.selectedIndex = date.getYear( ) - 80;
	f.M.selectedIndex = date.getMonth( );
}

function switchSolveForCommand( ) {
	var solveFor;
	unsetMissingDataError( );
	f = document.form;
	solveFor = f.solveFor.value;
	switch(solveFor) {
		case 'A': setSolveForPaymentInfo( ); break;
		case 'P': setSolveForPresentValueInfo( ); break;
		case 'i': setSolveForInterestRateInfo( ); break;
		case 'n': setSolveForMonthsInfo( ); break;
		case 'F': setSolveForFutureValueInfo( ); break;
		default: break;
	}
}

function resetSetSolveForInfo(newId) {
	var id;
	if (!solveForInfoBoxState) {
		id="solveForPaymentInfo";
	}
	else {
		id=solveForInfoBoxState;
	}
	e = document.getElementById(id);
	e.style.display = "none";
	solveForInfoBoxState = newId;
}

function setSolveForPaymentInfo( ) { 
	var id = "solveForPaymentInfo";
	resetSetSolveForInfo(id);
	e = document.getElementById(id);
	e.style.display = "block";
}
function setSolveForPresentValueInfo( ) { 
	var id = "solveForPresentValueInfo";
	resetSetSolveForInfo(id);
	e = document.getElementById(id);
	e.style.display = "block";
}
function setSolveForInterestRateInfo( ) { 
	var id = "solveForInterestRateInfo";
	resetSetSolveForInfo(id);
	e = document.getElementById(id);
	e.style.display = "block";
}
function setSolveForMonthsInfo() { 
	var id = "solveForMonthsInfo";
	resetSetSolveForInfo(id);
	e = document.getElementById(id);
	e.style.display = "block";
}
function setSolveForFutureValueInfo( ) { 
	var id = "solveForFutureValueInfo";
	resetSetSolveForInfo(id);
	e = document.getElementById(id);
	e.style.display = "block";
}

function amortizeCommand( ) {
	amortize( );
}

function solveCommand( ) {
	var solveFor;
	f = document.form;
	convertVariablesToIntegers( );
	solveFor = f.solveFor.value;
	switch(solveFor) {
		case 'A': solveForPayment( ); break;
		case 'P': solveForPresentValue( ); break;
		case 'i': solveForInterestRate( ); break;
		case 'n': solveForMonths( ); break;
		case 'F': solveForFutureValue( ); break;
		default: solveForInterestRate( ); break;
	}
	convertVariablesToMonies( );
}