function    MM_GetNumberedString ( str_Source )
{
   str_Source  =  str_Source.replace ( /^\s+|\s+$/, '' );

   var   str_Result        =  "";
   var   str_Digits        =  "0123456789";

   for  (  var i = 0;  i < str_Source.length;  ++ i  )
   {
      var   str_CurrChar   =  str_Source.charAt ( i );

      if    (    ( str_Digits.indexOf(str_CurrChar) != -1 )    ||    (  ( str_CurrChar == "." )  &&  ( str_Result.indexOf(".") == -1 )  )    )
         str_Result       +=  "" + str_CurrChar;

      else if  (  ( str_CurrChar != "$" )  &&  ( str_CurrChar != "," )  )
         break;
   }

   return   str_Result;
}

//------------------------------------------------------------------------------------------------------------------------------------------------------------------

function    MM_GetNumberFromString ( str_Source )
{
   var   str_Number  =  MM_GetNumberedString ( str_Source ); 

   return   ( str_Number == "" )  ?  0  :  Number ( str_Number );
}

//------------------------------------------------------------------------------------------------------------------------------------------------------------------

function    MM_ValidateInputText ( obj_Input, int_AfterPoint, dec_Min, dec_Max, boo_UseCommas, boo_EmptyValueAllowed, str_AlertText, boo_AlertAllowed )
{
   var   str_Source  =  MM_GetNumberedString ( obj_Input.value );
   var   dec_Source  =  ( str_Source == "" )  ?  0  :  parseFloat ( str_Source );

   if  (  ( str_Source == "" )  ||  ( dec_Source < dec_Min )  ||  ( dec_Source > dec_Max )  )
   {
      if    (    boo_AlertAllowed    &&    (  ( str_Source != "" )  ||  ( ! boo_EmptyValueAllowed )  )    )
      {
         alert ( "Please enter " + str_AlertText + " in range between " + dec_Min + " and " + dec_Max + "." );
         obj_Input.focus();
      }

      obj_Input.value   =  "";

      return   false;
   }
   
   var   dec_SourceInt  =  Math.floor ( dec_Source );
   var   str_Result     =  "" + dec_SourceInt;

   if  (  int_AfterPoint  >  0  )
   {
      var   str_Fraction   =  ""  +  Math.round  (  ( dec_Source - dec_SourceInt )  *  Math.pow ( 10, int_AfterPoint )  );

      while  (  str_Fraction.length  <  int_AfterPoint  )
         str_Fraction   =  "0"  +  str_Fraction;

      str_Result    +=  "."  +  str_Fraction;
   }

   if  (  boo_UseCommas  )
   {
      var   int_DotPosition   =  str_Result.indexOf ( "." );

      str_Source              =  str_Result;
      str_Result              =  (  int_DotPosition  !=  -1  )    ?    str_Source.substring (    int_DotPosition )    :    "";
      str_Source              =  (  int_DotPosition  !=  -1  )    ?    str_Source.substring ( 0, int_DotPosition )    :    str_Source;

      for  (  var i = str_Source.length - 1;  i >= 0;  -- i  )
      {
         var   n     =  str_Source.length - i;
         str_Result  =  (    (  (i>0)  &&  ( (n%3) == 0 )  )    ?    ","    :    ""    )    +    str_Source.charAt ( i )  +  str_Result;
      }
   }

   obj_Input.value   =  str_Result;

   return   boo_AlertAllowed;
}

//------------------------------------------------------------------------------------------------------------------------------------------------------------------

function    MM_MortgageEvaluation ()
{
   var   obj_Form          =  document.MortgageEvaluationForm;
   var   arr_Prefixes      =  Array ( "inpDownPayment_", "inpFirstMortgage_", "inpCMHCPremium_", "inpTotalFinancing_", "inpMortgagePayment_", "inpMonthlyExpenses_", "inpTotalPayment_", "inpIncomeRequired_" );

   var   boo_AlertAllowed  =  MM_ValidateInputText ( obj_Form.inpAskingPrice,  2, 1000, 999999999, true,  false, "Asking Price",  true             );
         boo_AlertAllowed  =  MM_ValidateInputText ( obj_Form.inpInterestRate, 3, 1,    100,       false, false, "Interest Rate", boo_AlertAllowed );
         boo_AlertAllowed  =  MM_ValidateInputText ( obj_Form.inpHeating,      2, 0,    99999,     true,  true,  "Heating",       boo_AlertAllowed );
         boo_AlertAllowed  =  MM_ValidateInputText ( obj_Form.inpTaxes,        2, 0,    99999,     true,  true,  "Taxes",         boo_AlertAllowed );
         boo_AlertAllowed  =  MM_ValidateInputText ( obj_Form.inpCondoFees,    2, 0,    99999,     true,  true,  "Condo Fees",    boo_AlertAllowed );
         boo_AlertAllowed  =  MM_ValidateInputText ( obj_Form.inpGDS,          2, 1,    100,       false, false, "GDS",           boo_AlertAllowed );

   var   dec_AskingPrice   =  MM_GetNumberFromString  (  obj_Form . inpAskingPrice  . value  );
   var   dec_InterestRate  =  MM_GetNumberFromString  (  obj_Form . inpInterestRate . value  );
   var   dec_Amortization  =  MM_GetNumberFromString  (  obj_Form . selAmortization . value  );
   var   dec_Heating       =  MM_GetNumberFromString  (  obj_Form . inpHeating      . value  );
   var   dec_Taxes         =  MM_GetNumberFromString  (  obj_Form . inpTaxes        . value  );
   var   dec_CondoFees     =  MM_GetNumberFromString  (  obj_Form . inpCondoFees    . value  );
   var   dec_GDS           =  MM_GetNumberFromString  (  obj_Form . inpGDS          . value  );

   for  (  var i = 1;  i <= 4;  ++ i  )
   {
      var   obj_PercentDown   =  obj_Form [ "cboPercentDown_" + i ];

      MM_ValidateInputText ( obj_PercentDown, 0, 0, 100, false, false, "Percent Down", false );

      if  (  obj_PercentDown.value  !=  ""  )
         boo_AlertAllowed  =  false;
   }

   for  (  var i = 1;  i <= 4;  ++ i  )
   {
      var   obj_PercentDown   =  obj_Form [ "cboPercentDown_" + i ];
      var   arr_Values        =  Array ( "", "", "", "", "", "", "", "" );
            boo_AlertAllowed  =  MM_ValidateInputText ( obj_PercentDown, 0, 0, 100, false, false, "Percent Down", boo_AlertAllowed );

      if    (    (  obj_PercentDown.value  !=  ""  )    &&    ( obj_Form.inpAskingPrice.value != "" )    &&    ( obj_Form.inpInterestRate.value != "" )    &&    ( obj_Form.inpGDS.value != "" )    )
      {
         var   dec_PercentDown         =  MM_GetNumberFromString  ( obj_PercentDown.value  );
         var   dec_Base                =  1  +  dec_InterestRate / 200;
         var   dec_CMHC_Rate_Premium   =  0;
         var   dec_CMHC_Rate           =  0;

              if  (  dec_Amortization  ==  30  )         dec_CMHC_Rate_Premium   =  0.2;
         else if  (  dec_Amortization  ==  35  )         dec_CMHC_Rate_Premium   =  0.4;

              if  (  dec_PercentDown   <    5  )         dec_CMHC_Rate           =  2.75 + dec_CMHC_Rate_Premium;
         else if  (  dec_PercentDown   <   10  )         dec_CMHC_Rate           =  2.75 + dec_CMHC_Rate_Premium;
         else if  (  dec_PercentDown   <   15  )         dec_CMHC_Rate           =  2    + dec_CMHC_Rate_Premium;
         else if  (  dec_PercentDown   <   20  )         dec_CMHC_Rate           =  1.75 + dec_CMHC_Rate_Premium;

         var   dec_DownPayment      =  dec_AskingPrice  *  dec_PercentDown  /  100;
         var   dec_FirstMortgage    =  dec_AskingPrice  -  dec_DownPayment;
         var   dec_CMHCPremium      =  dec_CMHC_Rate  *  dec_FirstMortgage  /  100;
         var   dec_TotalFinancing   =  dec_FirstMortgage + dec_CMHCPremium;
         var   dec_MortgagePayment  =  dec_TotalFinancing    *    (  Math.pow ( dec_Base, 1/6 )  -  1  )    /    (  1  -  Math.pow ( dec_Base, -dec_Amortization*12/6 )  );
         var   dec_MonthlyExpenses  =  ( dec_Heating + dec_Taxes + dec_CondoFees )  /  12;
         var   dec_TotalPayment     =  dec_MonthlyExpenses  +  dec_MortgagePayment;
         var   dec_IncomeRequired   =  dec_TotalPayment  *  1200  /  dec_GDS;

               arr_Values           =  Array ( dec_DownPayment, dec_FirstMortgage, dec_CMHCPremium, dec_TotalFinancing, dec_MortgagePayment, dec_MonthlyExpenses, dec_TotalPayment, dec_IncomeRequired );
      }

      for  (  var j  in  arr_Prefixes  )
      {
         var   obj_Control    =  obj_Form  [  arr_Prefixes [ j ]  +  i  ];
         var   val_Value      =  arr_Values [ j ];

         obj_Control.value    =  val_Value;

         MM_ValidateInputText ( obj_Control, 2, val_Value, val_Value, true, true, "", false );
      }
   }
}