
function makeNumValid( p_value ) {
                        var strchar;
                        var strctr;
                        var strlen;
                        var instr = p_value;
                        var newstr;
                        var tempstr;

                                                if( instr.indexOf('.') != instr.lastIndexOf('.') ) 
                                        return(-1); 

                                                newstr = removeCommas( instr );

                                                strlen = newstr.length;
                        tempstr = '';
                        for( strctr=0; strctr <strlen; strctr++) {
                strchar = newstr.charAt(strctr);
                if( (strchar == '.') || (strchar == '0') || (strchar == '1') || (strchar == '2') 
                                         || (strchar == '3') || (strchar == '4') || (strchar == '5') || (strchar == '6') 
                                         || (strchar == '7') || (strchar == '8') || (strchar == '9')) 
                                                        tempstr = tempstr + strchar;
                        }
                        newstr = tempstr;

                        if( newstr == "" ) 
                                        return(0);
                        else 
                                        return(newstr); 
        }
        
                function roundFloat( s_value ) {
                        var newval;

                        s_value = parseFloat(s_value);
                        newval = (Math.floor(s_value*100))/100;
                        newval = newval + "";

                        if ( newval.indexOf('.') == -1 ) 
                                        newval = newval + ".00"; 

                        return(newval);
        }
        
                function removeCommas( t_value ) {
                        var ctr;
                        var newstr = t_value;
                        var commapos;
                        var strlen = t_value.length - 1;

                        commapos = newstr.indexOf(',');
                        while ( commapos != -1 ) {
                                        newstr = newstr.substring(0,commapos) +  
                                                                         newstr.substring(commapos + 1, newstr.length );

                commapos = newstr.indexOf(',');
                         }
                         return(newstr);
        }
                function addCommas( u_value ) {
                        var ctr;
                        var decCtr=0;
                        var decIndex;
                        var decIncr = 3;
                        var newstr = u_value;
                        var strlen = u_value.length;

                        decIndex = u_value.indexOf('.');
                        if ( decIndex == -1 ) 
                                        decIndex = strlen; 

                        for ( ctr = decIndex-1; ctr > 0; ctr-- ) {
                                        decCtr++;
                                        if ( decCtr == 3 ) { 
                                                        newstr = newstr.substring(0,ctr) +
                                                                                         "," +
                                                                                         newstr.substring(ctr, strlen );
                                                        strlen++;
                                                        decCtr = 0; 
                                        }
                        }

                 return(newstr);
        }
        
                function isDigit( v_value ) {
                        if ( (v_value >= 0) || (v_value <= 9) ) 
                                        return(true);
                        else 
                                        return(false);
        }

                function resetTextField( textField ) {
                        textField.value = roundFloat("0");
                        textField.focus();
                        textField.select();
        }
  var v_MonthlyPaymentHold='';  // hold montly payment for onchange event
    function calcMonthlyPayment() {
      var v_HomePrice,
          v_InterestRate,
          v_DownPayment,
          v_InterestRate,
          v_Term,
          v_MonthlyPayment,
          v_Principle,
          v_formName = document.calc;

    // check values used to compute montly payment
    if ( confirmTermValues() ) {
            v_HomePrice = roundFloat( makeNumValid( v_formName.HomePrice.value ) );
      if ( v_formName.DownPayment.value == '' ) {
        v_DownPayment = 0;
      } else {
        v_DownPayment = roundFloat( makeNumValid( v_formName.DownPayment.value ) );
      }
      v_InterestRate = makeNumValid( v_formName.InterestRate.value ) / 100;
      v_Term = makeNumValid( v_formName.Term.value );
      v_Principle = v_HomePrice - v_DownPayment;
            if ( v_Term == "" || v_Term == 0 ) 
          v_MonthlyPayment = v_Principle;
      else {
          v_MonthlyRate = v_InterestRate/12;
          if ( v_MonthlyRate != 0 ) 
              v_MonthlyPayment = Math.floor((v_Principle*v_MonthlyRate)/(1-Math.pow((1+v_MonthlyRate),
                                            (-1*(v_Term*12))))*100)/100;
          else  // no monthly rate
              v_MonthlyPayment = Math.floor((v_Principle / (v_Term*12)) * 100) / 100;

                    if ( isNaN(v_MonthlyPayment) || v_MonthlyPayment <0 ) 
              v_MonthlyPayment = 0;
      }

            v_formName.MonthlyPayment.value = addCommas(roundFloat(v_MonthlyPayment));
      v_MonthlyPaymentHold = v_formName.MonthlyPayment.value; 
    }
  }

    function initializeCalc() {
      var form = document.calc;

      form.HomePrice.value = addCommas(roundFloat(form.HomePrice.value));
  }

    function confirmTermValues() {
    var v_form = document.calc,
        v_HomePrice = v_form.HomePrice.value,
        v_DownPayment = v_form.DownPayment.value,
        v_InterestRate = v_form.InterestRate.value,
        v_Term = v_form.Term.value,
        v_tHomePrice,
        v_tDownPayment,
        t_tInterestRate,
        v_tTerm,
        v_errorflag = 0,
        v_errorstr,
        v_returnval;
    // get values from fields  
    v_tHomePrice = parseFloat(makeNumValid(v_HomePrice));
    v_tDownPayment = parseFloat(makeNumValid(v_DownPayment));
    v_tInterestRate = parseFloat(makeNumValid(v_InterestRate));
    v_tTerm = Math.round(makeNumValid(v_Term)); 
    if ( v_tHomePrice == '' || isNaN(v_tHomePrice) ) {
      v_form.HomePrice.value = 0;
      v_tHomePrice = 0;
    }
    if ( v_tDownPayment <0 ) { // N3.x fix; -1 bleeds thru when 0
      v_tDownPayment = 0;
    }
    if ( v_tInterestRate == '' || isNaN(v_tInterestRate) ) { 
      v_tInterestRate = 0;
    }
    // Home price <0 or  > 100,000,000
    if ( v_tHomePrice >= 100000000 || v_tHomePrice <= 0 ) {
      v_errorstr = "Sorry.  $" + v_HomePrice + " is an invalid price.\nPlease enter a price between $0 and $100,000,000.00"; 
      v_form.HomePrice.value = '';
      v_errorflag = 1;
    // Down payment <0 or > home price 
    } else if ( v_tDownPayment <0 || v_tDownPayment > v_tHomePrice ) {
      v_errorstr = "Sorry.  Your down payment of $" + v_DownPayment + " must\nbe less than your home price of $" + v_HomePrice + ".";
      v_form.DownPayment.value = '';
      v_errorflag = 1;
    // Interest rate <1 or > 25
    } else if ( v_tInterestRate <1 || v_tInterestRate > 25 ) {
      v_errorstr = "Sorry.  An interest rate of " + v_InterestRate + " is invalid.\nPlease enter a value between 1% and 25%.";
      v_form.InterestRate.value = '';
      v_errorflag = 1;
    // Term <5 ; textfield limited to 2 chars so not > 99 by default
    }
    else if ( v_tTerm <5 ) {
      v_errorstr = "Sorry.  Your term of " + v_Term + " is invalid.\nPlease enter a minimum value of 5 years.";
      v_form.Term.value = '';
      v_errorflag = 1;
    }

    // display error and clear result
    if ( v_errorflag ) {
      alert ( v_errorstr );
      clearMonthlyPayment();
      v_returnval = 0;
    } else {
      v_returnval = 1;
    }

   return ( v_returnval );
  }

  function clearMonthlyPayment () {
     var v_form = document.calc;

     v_form.MonthlyPayment.value = '';
  }

