function doSum(a){ 
   a.value = check(a.value);
}
function check(a) 
{
   var pest = 0;
   var b = "";
   for(i=0;i<=a.length;i++)
   {
   var u = a.charAt(i);
      if((u>="0"&&u<="9")||u==".")
      {
      	if(u=="."){
      	var pest = pest+1
      	if(pest==2){break;}}
var b = b + u;    	}}
return b;
}

function checkNumber(quest,input, min, max, msg)
{

    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;
        }
    }
    if(input.value!="")
    {
    var num = 0 + str
    if (num < min || max < num) {
    var sendq = "You have entered " + input.value + ". Please enter a number between " + min + " and " + max + ".";
  alert(sendq);

        return false;
    }
    input.value = str;
    return true;
	}
	

}


function computeField(quest,input,min,max,msage)
{
    doSum(input); 
    checkNumber(quest,input,min,max,msage);     
}        

function roundPen(n)
{
if(n > 0){
pennies = n*100;
pennies = Math.round(pennies);
strPennies = "" + pennies;
len = strPennies.length;

return strPennies.substring(0, len - 2) + "." + strPennies.substring(len -2, len);
}
else return 0;
}


function calcMax(){
var errorMsg;

if((document.maxcalc.totinc.value == null || document.maxcalc.totinc.value.length == 0)|| (document.maxcalc.totinc.value < 10000 || document.maxcalc.totinc.value > 1000000)){
errorMsg = 'Please enter your Annual Family Income as a number between 10000 and 1000000.';}


if((document.maxcalc.protax.value == null || document.maxcalc.protax.value.length == 0)|| (document.maxcalc.protax.value < 100 || document.maxcalc.protax.value > 50000)){
errorMsg = 'Please enter your Annual Property Taxes as a number between 100 and 50000.';}

if((document.maxcalc.proheat.value == null || document.maxcalc.proheat.value.length == 0)|| (document.maxcalc.proheat.value < 20 || document.maxcalc.proheat.value > 1500)){
errorMsg = 'Please enter your Monthly Heating Costs/Condo Fees as a number between 20 and 1500.';}

if((document.maxcalc.debt.value == null || document.maxcalc.debt.value.length == 0)|| (document.maxcalc.debt.value < 0 || document.maxcalc.debt.value > 5000)){
errorMsg = 'Please enter your Minimum Monthly Payments for Loans/Credit Cards as a number between 0 and 5000.';}

if((document.maxcalc.second.value == null || document.maxcalc.second.value.length == 0)|| (document.maxcalc.second.value < 0 || document.maxcalc.second.value > 5000)){
errorMsg = 'Please enter your Monthly Secondary Financing Payment as a number between 0 and 5000.';}

if((document.maxcalc.rate.value == null || document.maxcalc.rate.value.length == 0)|| (document.maxcalc.rate.value < 2 || document.maxcalc.rate.value > 25)){
errorMsg = 'Please enter your Interest Rate as a number between 2.0 and 25.0.';}

    if(!errorMsg)
    {
        var RATE = document.maxcalc.rate.value/100;
        var income = document.maxcalc.totinc.value;
        var tax = document.maxcalc.protax.value;
        var heat = document.maxcalc.proheat.value*12;
        var debt = document.maxcalc.debt.value*12;
        var second = document.maxcalc.second.value*12;
        var compound = 2/12;
        var monTime = 25 * 12;
        var yrRate = RATE/2;
        var rdefine    = Math.pow((1.0 + yrRate),compound)-1.0;
        var purchcompound = Math.pow((1.0 + rdefine),monTime);


        var maxgdsr =.32;
        var maxtdsr =.42;


        var GDSPAY = (maxgdsr*income) - tax - heat - second;
        var TDSPAY = (maxtdsr*income) - tax - heat - second - debt;

        var PAYMENT = (GDSPAY<TDSPAY) ? GDSPAY/12 : TDSPAY/12;
        var MORTGAGE = (0 +((PAYMENT*(purchcompound-1.0))/rdefine))/purchcompound;


        document.maxcalc.amt.value = '$'+roundPen(MORTGAGE);
        document.maxcalc.pay.value = '$'+roundPen(PAYMENT);
        return false;
    }
    else
    {
        alert(errorMsg);
        return false;
    }
}
