	function validated(string) {
	    for (var i=0, output='', valid=".1234567890"; i<string.length; i++)
	       if (valid.indexOf(string.charAt(i)) != -1)
	          output += string.charAt(i)
	    return output;
	} 
    
	function checkForZero(field) {
        if (field.value == 0 || field.value.length == 0) {
            alert ("This field can't be 0!");
            field.focus(); }
        else
	    calculatePayment(field.form);
    }
	
    function cmdCalc_Click(form) {
        if (form.price.value == 0 || form.price.value.length == 0) {
            alert ("The Price field can't be 0!");
            form.price.focus(); }
        else if (form.ir.value == 0 || form.ir.value.length == 0) {
            alert ("The Interest Rate field can't be 0!");
            form.ir.focus(); }
        else if (form.term.value == 0 || form.term.value.length == 0) {
            alert ("The Term field can't be 0!");
            form.term.focus(); }
        else
            calculatePayment(form);
	
    }

    function calculatePayment(form) {
       princ = form.price.value;
       intRate = (form.ir.value/100) / 12;
       months = form.term.value * 12;
        a = Math.floor((princ*intRate)/(1-Math.pow(1+intRate,(-1*months)))*100)/100;
  
  
  
  Num = "" + eval("a");
dec = Num.indexOf(".");
end = ((dec > -1) ? "" + Num.substring(dec,Num.length) : ".00");
Num = "" + parseInt(Num);
var temp1 = "";
var temp2 = "";
if (checkNum(Num) == 0) {
alert("This does not appear to be a valid number.  Please try again.");
}
else { 
if (end.length == 2) end += "0";
if (end.length == 1) end += "00";
if (end == "") end += ".00";
var count = 0;
for (var k = Num.length-1; k >= 0; k--) {
var oneChar = Num.charAt(k);
if (count == 3) {
temp1 += ",";
temp1 += oneChar;
count = 1;
continue;
}
else {
temp1 += oneChar;
count ++;
}
}
for (var k = temp1.length-1; k >= 0; k--) {
var oneChar = temp1.charAt(k);
temp2 += oneChar;
}
temp2 = "$" + temp2 + end;
total = eval("a = '" + temp2 + "';");
mp=document.getElementById('monthlypayment');
removeAllChildNodes(mp);
st=document.createElement('strong');
tn=document.createTextNode('Estimated Monthly Payment:');
st.appendChild(tn);
mp.appendChild(st);
brk=document.createElement('br');
mp.appendChild(brk);
mp.appendChild(document.createTextNode(total));
dv=document.createElement('div');
dv.setAttribute('style','font-size:10px;width:160px;margin:8px auto 0 auto;');
tn=document.createTextNode('Calculation does not include any property tax, insurance and/or HOA fees that may be imposed on the property.');
dv.appendChild(tn);
mp.appendChild(dv);
mp.setAttribute('style','height:auto;border:1px solid black;');
}

  
}

function removeAllChildNodes(node) {
	if (node && node.hasChildNodes && node.removeChild) {
		while (node.hasChildNodes()) {
			node.removeChild(node.firstChild);
		}
	}
}

function checkNum(data) {      // checks if all characters 
var valid = "0123456789.";     // are valid numbers or a "."
var ok = 1; var checktemp;
for (var i=0; i<data.length; i++) {
checktemp = "" + data.substring(i, i+1);
if (valid.indexOf(checktemp) == "-1") return 0; }
return 1;
}

function	clearform()
{
document.temps.pmt.value = "";
document.temps.price.value = "";
document.temps.ir.value = "";
document.temps.term.value = "";
}

