formRef = null;
loanRef = null;
downPaymentRef = null;
payoffRef = null;
priceRef = null;
monthlyRef = null;
termRef = null;
interestRef = null;

function getLoan() {
	return ( loanRef != null ? loanRef.value : "" );
}

function setLoan( value ) {
	if( loanRef != null ) {
		loanRef.value = value;
	}
}

function getDownPayment() {
	return ( downPaymentRef != null ? downPaymentRef.value : "" );
}

function getPayoff() {
	return ( payoffRef != null ? payoffRef.value : "" );
}

function getPrice() {
	return ( priceRef != null ? priceRef.value : "" );
}

function getMonthly() {
	return ( monthlyRef != null ? monthlyRef.value : "" );
}

function setMonthly( value ) {
	if( monthlyRef != null ) {
		monthlyRef.value = value;
	}
}

function getInterest() {
	return ( interestRef != null ? interestRef.value : "" );
}

function getTerm() {
	return ( termRef != null ? termRef.options[termRef.selectedIndex].value : "" );
}

// Call this method first, before anything else is done
function initForm( ref, loan, downPayment, payoff, price, monthly, term, interest ) 
{

	formRef = ref;
	loanRef = formRef.elements[loan];
	downPaymentRef = formRef.elements[downPayment];
	payoffRef = formRef.elements[payoff];
	priceRef = formRef.elements[price];
	monthlyRef = formRef.elements[monthly];
	termRef = formRef.elements[term];
	interestRef = formRef.elements[interest];
}


function checkNumber( input )
{
	// blank field reset to default value.
	if( input.value == '' ) {
		input.value = input.defaultValue;
	}

	// remove any user added commas
	input.value = removeCommas( input.value+"" );

	// place a warning on the status line if the field contains invalid data
	window.status = '';
	msg = "This field requires numeric 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 != '.') ) {
			input.focus();
			input.value = "0";
			input.select();
			window.status = msg;
		}
	}
	addInput( input );
	input.value = insertCommas( input.value );
}

function addInput( input ) {
	setLoan( removeCommas( getPrice() ) * 1 );
	calcMonthly();
}

// Make a number a nice comma delimited number.
function insertCommas( m ) {
	if ( !m ) m = 0;

	var money = m + "";
	var decimals = "";

	dotIndex = money.indexOf(".");
	if (dotIndex > -1) {
		decimals = money.substr(dotIndex);
		money = money. substr(0, dotIndex);
	}

	var offset = 0;
	var output = "";
	var isNegative = false;
	
	for(var i = 0; i < money.length; i++) {
		c = money.charAt(money.length-1 - i);
		if( c == '$' || c == ',' || c == '-') {
			if( c == '-' )
				isNegative = true;
			offset++;
			continue;
		}
		if ((i > 0) && (((i-offset) % 3) == 0)) output = "," + output;

		output = c + output;
	}

	if( (decimals != null) && (decimals != "") && (decimals.length < 3) ) {
		decimals += "0" + (decimals.length == 1) ? "0" : "";
	}
	
	return ( isNegative ? "-" : "" )  + output + decimals;
}

// Remove all commas from a passed string.
function removeCommas( string ) {
	var newnum;
	var ch;
	newnum = "";

	var input = new String( string );

	for( var i = 0; i < input.length; i++ ) {
			ch = input.charAt( i );
			if( ch != "," ) {
					newnum += ch;
			}
	}
	return parseFloat( newnum );
}

function checkNumPeriods( input ) {
	input.value = removeCommas( input.value );
	input.value = parseFloat( input.value );

	if( input.value == null || input.value.length == 0 || input.value == "NaN" ) {
		input.value = "";
	} else {
		if( parseFloat( input.value ) <= 0 ) {
			input.value = "";
		}
	}
	calcMonthly();
	input.value = insertCommas( input.value );
}

function checkTotal( input ) {
	input.value = removeCommas( input.value );
	input.value = parseFloat( input.value );
	if( input.value == null || input.value.length == 0 || input.value == "NaN" ) {
		input.value = "";
	} else {
		if( parseFloat( input.value ) < 0 ) {
			input.value = "";
		}
	}
	calcMonthly();
	input.value = insertCommas( input.value );
}

// Verify the interest rate is a reasonable number, or set it to one.
function checkInterest( input ) 
{
	input.value = parseFloat(input.value);
	
	if (input.value == null || input.value.length == 0 || input.value == "NaN" || input.value < 0 || input.value > 100) {
		input.value = 9;
	}
	calcMonthly();
}

function calcMonthly() 
{

	if( termRef && downPaymentRef && payoffRef && interestRef ) {
		termRef.disabled = false;
		downPaymentRef.disabled = false;
		payoffRef.disabled = false;
		interestRef.disabled = false;
		if( getInterest() == "" || getTerm() == "" || getLoan() == "") {
			setMonthly( "" );
			//window.status = "bad value?";
		} else {
		
			i = removeCommas( getInterest() ) / 100;
			if( i > 0 ) {
				i = i / 12;
				n = removeCommas( getTerm() ) * 12;
				tmp = ( 1 / i ) * ( 1 - Math.pow( 1 / (1 + i), n ) );
				tmp = ( removeCommas( getLoan() ) - removeCommas( getDownPayment() ) + removeCommas( getPayoff() ) ) / tmp;

				tmp = Math.round( tmp * 100 ) / 100;

				if( tmp == null || tmp.length == 0 || tmp == "NaN" ) {
					setMonthly( "" );
				} else {
					setMonthly( insertCommas( tmp ) );
				}
			} else {
				nummonths  = removeCommas( getTerm() ) * 12;
				loanamount = removeCommas( getLoan() );

				monthlypayment = Math.round( (loanamount / nummonths) * 100 ) / 100;
				setMonthly( insertCommas( monthlypayment ) );
			}
		}
	}
}

function disableCalc() {

	setMonthly( "N/A" );
	
	if( termRef ) {
		termRef.disabled = true;
	}
	if( downPaymentRef ) {
		downPaymentRef.disabled = true;
	}
	if( payoffRef ) {
		payoffRef.disabled = true;
	}
	if( interestRef ) {
		interestRef.disabled = true;
	}
}

function selectField( field )
{
	field.select();
}
