function numerify(text){
period = 0

for (i=0; i< text.length; i++){
check = parseInt(text.charAt(i))
check = check.toString()

if (((check) == 'NaN')){
		if(text.charAt(i) == '.')
		{
			if (period == 1){
			text = text.substring(0,i) + text.substring((i+1),(text.length))
			period = 1
		}
		continue
		}
		if (i==0)
			{
			text = text.substring(1,(text.length))
			continue
			}
		if (i==(text.length-1))
			{
			text = text.substring(0,(text.length-1))
			continue
			}
		text = text.substring(0,i) + text.substring((i+1),(text.length))
		}
}
//document.Calculator.Results.value = parseFloat(text)
document.getElementById("Results").value = parseFloat(text)
return(parseFloat(text))
}

function Dollar_It(text){
dollar = text.toString()

dec_place = dollar.indexOf(".",0)

if (dec_place ==-1){
dollar = dollar + ".00"
return(dollar)
}
else if((dec_place +1) == dollar.length){
dollar = dollar + "00"
return(dollar)
}
else if ((dec_place+2) == dollar.length){
dollar = dollar + "0"
return(dollar)
}
else{
dollar = dollar.substring(0,dec_place+3)
return(dollar)
}

}

function CalcForm(){
/*
d_rate  = parseFloat(numerify(document.Calculator.dealer_rate.value)/100)
d_term  = parseFloat(numerify(document.Calculator.dealer_term.value))
cu_rate = parseFloat(numerify(document.Calculator.cu_rate.value)/100)
cu_term = parseFloat(numerify(document.Calculator.cu_term.value))
amount  = parseFloat(numerify(document.Calculator.finance.value))
rebate  = parseFloat(numerify(document.Calculator.rebate.value))
*/
alert("hello");
d_rate  = parseFloat(numerify(document.getElementById("dealer_rate").value)/100)
d_term  = parseFloat(numerify(document.getElementById("dealer_term").value))
cu_rate = parseFloat(numerify(document.getElementById("cu_rate").value)/100)
cu_term = parseFloat(numerify(document.getElementById("cu_term").value))
amount  = parseFloat(numerify(document.getElementById("finance").value))
rebate  = parseFloat(numerify(document.getElementById("rebate").value))


cu_month = Math.pow( (1 + (cu_rate / 12)) , (0-(cu_term * 12)) )
cu_month = 1 - cu_month
cu_month = (cu_rate / 12) / cu_month
cu_month = (amount - rebate) * cu_month

cu_total = (cu_month * (cu_term * 12))

d_month = Math.pow( (1 + (d_rate / 12)) , (0-(d_term * 12)) )
d_month = 1 - d_month
d_month = (d_rate / 12) / d_month
d_month = d_month * amount 

d_total = (d_month * (d_term * 12))

//display results
//document.Calculator.cu_month.value      = Dollar_It(cu_month)
//document.Calculator.dealer_month.value  = Dollar_It(d_month)

document.getElementById("cu_month").value      = Dollar_It(cu_month)
document.getElementById("dealer_month").value  = Dollar_It(d_month)

if (d_total >= cu_total)
	{
	//document.Calculator.Results.value =
	document.getElementById("Results").value= 
	'Good news! United Maryland Employees FCU loan--plus rebate--will save you $' +
	Dollar_It((d_total - cu_total)) +
	' over the course of the loan.'
	
	}
else
	{
	//document.Calculator.Results.value = 
	document.getElementById("Results").value = 
	'If you can afford $' + Dollar_It(d_month) +
	' every month, the dealer rate will save $' +
	Dollar_It((cu_total - d_total)) +
	'--over the course of the loan. Contact the folks at your credit union for a monthly payment you can afford.'
	}
}
