// **********************************************************************************************
// Name     : JScript functions 
// Cr Date  : 23-Sep-2009 
// Author   : Guda Joseph 
// Purpose  : JScript functions for Loan Calculator (Reno / Edu) 
// History  : 
// ==============================================================================================
// 23-Sep-2009  Guda Joseph     intial creation 
// 12-Nov-2009  Guda Joseph     modified to show interest pay during study 
// 26-Nov-2009  Guda Joseph     modified to validate 5k-15k is 5yrs for edu  
// 04-Mar-2011  Guda Joseph     update the revised int rates of Reno loan  
// 24-Nov-2011  Guda Joseph     modified with new variable rate package for reno (IT_Req_110170)  
// 
// ***********************************************************************************************

function openPage(page) {
     OpenWin = this.open(page, "CtrlWindow", 
     "scrollbars=no,resizable=no,height=490,width=520");
    }; 

function display() {
    var Today = new Date();
    var hours = Today.getHours();
    var min = Today.getMinutes();
    var sec = Today.getSeconds();
    var Time = ((hours > 12) ? hours - 12 :(hours == 0) ? 12 :hours);
    Time += ((min < 10) ? ":0" : ":") + min;
    Time += ((sec < 10) ? ":0" : ":") + sec;
    Time += (hours >= 12) ? " PM" : " AM";
    this.clockForm.clock.value = Time;
	setTimeout("display()",1000);
  };  

function isEmpty(str)
  {
    if( str == null || str == "")
      return true;
    var returnValue = true;
    var len = str.length;
    for( i=0; i<len; i++ )
    {
      if( str.charAt(i) != " ")
        returnValue = false;
    }
    return returnValue;
  }; 

function toTwoDecimal(argInput)
  {
    var valLong = argInput + 0.9;
    valLong    += "";
    var valPos  = valLong.indexOf( '.' );
    valLong     = valLong.substring( 0, valPos); // + 3;
    return valLong;
  }; 
  
  
 function checkValuesReno()
  {    
   //alert ("inside checkvaluesReno"); 
   document.reno.MthRepayment.value = 0.00;
   var invalid = " "; 
   if (isEmpty(document.reno.LnAmt.value))
    {
      alert("Please Enter Loan Amount!");
      document.reno.LnAmt.value = ""; 
      document.reno.LnAmt.focus();
      return false;
    }
   var LoanAmount = document.reno.LnAmt.value;
    for (var i=0;i<LoanAmount.length;i++)
    {
       if(LoanAmount.charAt(i) == "."){
       alert("No Decimal Point for Loan Amount.");
       document.reno.LnAmt.focus();
       return false;
        }
    }
    if(document.reno.LnAmt.value.indexOf(invalid) > -1){
      alert("Please Enter valid Loan Amount!");
      document.reno.LnAmt.focus();
      return false;
    } 
    if (isEmpty(document.reno.LnPeriod.value))
    {
      alert("Please Enter Loan Tenure!");
      document.reno.LnPeriod.value = "";
      document.reno.LnPeriod.focus(); 
      return false;
    }
    var LoanPeriod = document.reno.LnPeriod.value;
    for (var i=0;i<LoanPeriod.length;i++)
    {
       if(LoanPeriod.charAt(i) == "."){
       alert("No Decimal Point for Loan Tenure.");
       document.reno.LnPeriod.focus(); 
       return false;
        }
    }
    if(document.reno.LnPeriod.value.indexOf(invalid) > -1){
      alert("Please Enter valid Loan Tenure!");
      document.reno.LnPeriod.focus(); 
      return false;
    }
    
    // more validations or checks 
    if (document.reno.LnAmt.value < 10000)
    {
      alert("Please note that the minimum loan amount is S$10,000");
      document.reno.LnAmt.value = 10000; 
      document.reno.LnAmt.focus(); 
      return false;
    }
    if (document.reno.LnAmt.value > 30000)
    {
      alert("Please note that the maximum loan amount is S$30,000");
      document.reno.LnAmt.value = 30000; 
      document.reno.LnAmt.focus();
      return false;
    }
    if (document.reno.LnAmt.value > parseInt(document.reno.LnAmt.value))
    {
      alert("No Decimal Point for Loan Amount.");
      document.reno.LnAmt.focus();
      return false;
    }
    if (document.reno.LnPeriod.value < 1)
    {
      alert("Please note that the minimum loan tenure is 1 year.");
      document.reno.LnPeriod.value = 1; 
      document.reno.LnPeriod.focus(); 
      return false;
    }
    if (document.reno.LnPeriod.value > 5)
    {
      alert("Please note that the maximum loan tenure is 5 years.");
      document.reno.LnPeriod.value = 5; 
      document.reno.LnPeriod.focus(); 
      return false;
    }
    if (document.reno.LnPeriod.value > parseInt(document.reno.LnPeriod.value))
    {
      alert("No Decimal Point for Loan Tenure.");
      document.reno.LnPeriod.focus(); 
      return false;
    }
        
   return true;
  }; // end of checkValuesReno 
  
  
  function checkValuesEdu()
   {
    //alert ("inside checkvalues edu"); 
    document.edu.MthRepayment.value = 0.00;
    document.edu.dMthRepayment.value = 0.00;
     var invalid = " "; 
     if (isEmpty(document.edu.LnAmt.value))
     {
      alert("Please Enter Loan Amount!");
      document.edu.LnAmt.value = ""; 
      document.edu.LnAmt.focus();
      return false;
     }
     var LoanAmount = document.edu.LnAmt.value;
     for (var i=0;i<LoanAmount.length;i++)
     {
       if(LoanAmount.charAt(i) == "."){
       alert("No Decimal Point for Loan Amount.");
       document.edu.LnAmt.focus();
       return false;
        }
     }
     if(document.edu.LnAmt.value.indexOf(invalid) > -1){
      alert("Please Enter valid Loan Amount!");
      document.edu.LnAmt.focus();
      return false;
     } 
     if (isEmpty(document.edu.LnPeriod.value))
     {
      alert("Please Enter Loan Tenure!");
      document.edu.LnPeriod.value = "";
      document.edu.LnPeriod.focus(); 
      return false;
     }
     var LoanPeriod = document.edu.LnPeriod.value;
     for (var i=0;i<LoanPeriod.length;i++)
     {
       if(LoanPeriod.charAt(i) == "."){
       alert("No Decimal Point for Loan Tenure.");
       document.edu.LnPeriod.focus(); 
       return false;
        }
     }
     if(document.edu.LnPeriod.value.indexOf(invalid) > -1){
      alert("Please Enter valid Loan Tenure!");
      document.edu.LnPeriod.focus(); 
      return false;
     }
        
     
     
      // more validations or checks 
      if ( (document.edu.LnAmt.value < 5000) && (document.edu.LnAmt.value >= 3000) && (document.edu.LnPeriod.value > 1) )
      {
       alert("Please note loan amount ranging S$3,000 - S$4,999 must be settled in a YEAR.");
       document.edu.LnPeriod.value = 1; 
       document.edu.LnPeriod.focus(); 
       return false;
      }
     if (document.edu.LnAmt.value < 3000)
     {
      alert("Please note that the minimum loan amount is S$3,000");
      document.edu.LnAmt.value = 3000; 
      document.edu.LnAmt.focus(); 
      return false;
     }
     if (document.edu.LnAmt.value > 100000)
     {
      alert("Please note that the maximum loan amount is S$100,000");
      document.edu.LnAmt.value = 100000; 
      document.edu.LnAmt.focus();
      return false;
     }
     if (document.edu.LnAmt.value > parseInt(document.edu.LnAmt.value))
     {
      alert("No Decimal Point for Loan Amount.");
      document.edu.LnAmt.focus();
      return false;
     }
     if (document.edu.LnPeriod.value < 1)
     {
      alert("Please note that the minimum loan tenure is 1 year.");
      document.edu.LnPeriod.value = 1; 
      document.edu.LnPeriod.focus(); 
      return false;
     }
     if (document.edu.LnPeriod.value > 10)
     {
      alert("Please note that the maximum loan tenure is 10 years.");
      document.edu.LnPeriod.value = 10; 
      document.edu.LnPeriod.focus(); 
      return false;
     }
     if (document.edu.LnPeriod.value > parseInt(document.edu.LnPeriod.value))
     {
      alert("No Decimal Point for Loan Tenure.");
      document.edu.LnPeriod.focus(); 
      return false;
     } 
     
     // more validations or checks : 26-Nov-2009 
      if ( (document.edu.LnAmt.value <= 15000) && (document.edu.LnAmt.value >= 5000) && (document.edu.LnPeriod.value > 5) )
      {
       alert("Please note loan amount ranging S$5,000 - S$15,000 must be settled within 5 Years.");
       document.edu.LnPeriod.value = 5; 
       document.edu.LnPeriod.focus(); 
       return false;
      }
     
     
     // check only if Graduated or Deferred Repayment Scheme selected
     if ( (document.edu.Repayment[1].checked) || (document.edu.Repayment[2].checked) )
     {
          
      if (isEmpty(document.edu.StudyPeriod.value))
      {
       alert("Please Enter Length of Studies!");
       document.edu.StudyPeriod.value = "";
       document.edu.StudyPeriod.focus(); 
       return false;
      }
      var StudyPeriod = document.edu.StudyPeriod.value;
      for (var i=0;i<StudyPeriod.length;i++)
      {
       if(StudyPeriod.charAt(i) == "."){
        alert("No Decimal Point for Length of Studies.");
        document.edu.StudyPeriod.focus(); 
        return false;
        }
      }
      if(document.edu.StudyPeriod.value.indexOf(invalid) > -1){
       alert("Please Enter valid Length of Studies!");
       document.edu.StudyPeriod.focus(); 
       return false;
      }      
      // more validations
      if (document.edu.StudyPeriod.value < 1)
      {
       alert("Please note that the minimum Length of Studies is 1 year.");
       document.edu.StudyPeriod.value = 1; 
       document.edu.StudyPeriod.focus(); 
       return false;
      }
      if (document.edu.StudyPeriod.value > 6)
      {
       alert("Please note that the maximum Length of Studies is 6 years.");
       document.edu.StudyPeriod.value = 6; 
       document.edu.StudyPeriod.focus(); 
       return false;
      }
      if (document.edu.StudyPeriod.value > parseInt(document.edu.StudyPeriod.value))
      {
       alert("No Decimal Point for Length of Studies.");
       document.edu.StudyPeriod.focus(); 
       return false;
      }      
      if ( (document.edu.LnAmt.value < 5000) && (document.edu.LnAmt.value >= 3000) && (document.edu.LnPeriod.value == 1) )
      {
       alert("Please note loan amount ranging S$3,000 - S$4,999 is applicable for Standard Repayment Scheme only.");
       document.edu.Repayment[0].checked = true; 
       document.edu.StudyPeriod.value = 0; 
       document.edu.LnPeriod.focus();
       return false;
      }
      
      // more validations or checks : 26-Nov-2009 
      //if ( (document.edu.StudyPeriod.value > document.edu.LnPeriod.value) )
      //{
      // alert("Please note Length of Studies must be within selected Loan Tenure.");
      // document.edu.StudyPeriod.value = 1; 
      // document.edu.StudyPeriod.value = document.edu.StudyPeriod.value; 
      // document.edu.StudyPeriod.focus(); 
      // return false;
      //}
      
      
     } // end of check only if Graduated or Deferred Repayment Scheme selected 
             
    return true;
   }; // end of checkValuesEdu  
  
  
  function CalcRenoRepayment()
  { 

    if (checkValuesReno()) // == true
    {      
     document.reno.MthRepayment.value = 0.00;
    
     var LnPeriod    = 0;       // no of years 
     var Tenure      = 0;       // repay period in mths 
     var FRPIntRate1 = 3.68;    // < 15k 3.68% pa (old: 3.88% pa) 
     var FRPIntRate2 = 2.98;    // > 15k 2.98% pa (old: 3.22% pa)  
     var VRPIntRate1 = 5.98;    // < 15k 5.70% pa (old: 5.70%, 6.25% pa) [Variable Rate Package] -- 24/11/2011 
     var VRPIntRate2 = 5.38;    // > 15k 5.70% pa (old: 5.70%, 6.25% pa) [Variable Rate Package] -- 24/11/2011
     var LnInt       = 0.00;    // interest applicable 
     var Principal   = 0;       // Loan amt 
     var Interest    = 0.00;    // Int amt 
     var TotAmt      = 0.00;    // Tot amt (principal + interest)   
     var MthRepayAmt = 0.00;    // Repay amt 
     
     // read the input values
     Principal = document.reno.LnAmt.value; 
     LnPeriod = document.reno.LnPeriod.value;         
     Tenure = ((LnPeriod)*12); // Ln period in months 
     
     // Calculation of Flat Rate Package (FRP) 
     if (document.reno.Package[0].checked) 
     {              
       if (document.reno.LnAmt.value >= 15000)
       {
        LnInt = FRPIntRate2; 
       }
       if (document.reno.LnAmt.value < 15000)
       {
        LnInt = FRPIntRate1; 
       } 
      
       //alert ("Flat Rate Package "+LnInt+'%'); 
       
       // Calculate repayment's 
       // Total Interest (loan amount X interest rate X no of years)
       Interest = ( parseFloat(((Principal)*(LnInt/100)))*(LnPeriod) );       
       // Total Ln amount (principal + interest)  //parseFloat()
       TotAmt = (parseFloat(Principal) + parseFloat(Interest));       
       // Monthly Repayment (principal+interest)/tenure 
       //MthRepayAmt = parseFloat((TotAmt/Tenure));
       MthRepayAmt = (Math.round(parseFloat((TotAmt/Tenure))*100)/100);
       
     } // end of FRP block 
    
     // Calculation of Variable Rate Package (VRP) 
     if (document.reno.Package[1].checked)
     {            
       if (document.reno.LnAmt.value >= 15000)
       {
        LnInt = VRPIntRate2; 
       }
       if (document.reno.LnAmt.value < 15000)
       {
        LnInt = VRPIntRate1; 
       } 
       
       //alert ("Variable Rate Package "+LnInt+'%'); 
       // Calculate repayment's 
       var r = 0.00; 
       var n = 0; 
       var p = 0; 
       var top = 0.00; 
       var bottom = 0.00; 
       var t1 = 0;   // 1+r value
       var t2 = 0;   // 1+r with n times value
       var b1 = 0;   // 
       var t = 0; 
  
       // new logic 10-Nov-2009          
       p = Principal; 
       n = Tenure; 
         //r = parseFloat((LnInt/100));            
       r = parseFloat((LnInt/100.0)); 
       r /= 12; 
       var pow = 1;       
       // 
       for (var j = 0; j < n; j++)
        pow = pow * (1 + r);  // power ==> (1+r)^n 
       
       money = "" + .01* Math.round(100*(p * pow * r) / (pow - 1)); 
       
       dec = money.indexOf("."); 
       // 
       if (dec < 0) {    // new logic 11-Nov-2009 
         dollars = money;
         cents = "00"; 
        } 
       if (dec >= 0) {
        dollars = money.substring(0,dec);
        cents = money.substring(dec+1,dec+3);
        cents = (cents.length < 2) ? cents + "0" : cents; 
        } 
       money = dollars + "." + cents; 
       
       
       //dollars = money.substring(0,dec);
       //cents = money.substring(dec+1,dec+3);
       //cents = (cents.length < 2) ? cents + "0" : cents; 
       //money = dollars + "." + cents; 
       
       MthRepayAmt =  money;    // new logic 10-Nov-2009  
       
     } // end of VRP block 
     
     // display to the user 
     document.reno.MthRepayment.value = (Math.round(parseFloat(MthRepayAmt)*100)/100);
     
    } // end of if loop (checkValuesReno) 
   
  }; // end of CalcRenoRepayment 
  
  
  function CalcEduRepayment() 
  { 

    if (checkValuesEdu()) // == true
    {      
     document.edu.MthRepayment.value = 0.00;
     document.edu.dMthRepayment.value = 0.00; 
     // 
     var LnPeriod    = 0;        // no of years 
     var Tenure      = 0;        // repay period in mths 
     var StudyPeriod = 0;        // no of years of study 
     var StudyTenure = 0;        // study period in mths
     var SRSIntRate1  = 8.00;    // Standard Repayment Scheme Int rate (1 yr, < 5k)
     var SRSIntRate2  = 4.88;    // Standard Repayment Scheme Int rate ( >=5k) 
     var GRSIntRate1  = 4.88;    // Graduated Repayment Scheme Int rate
     var GRSIntRate2  = 4.88;    // Graduated Repayment Scheme Int rate
     var DRSIntRate1  = 4.88;    // Deferred Repayment Scheme Int rate
     var DRSIntRate2  = 4.88;    // Deferred Repayment Scheme Int rate
     
     var LnInt       = 0.00;    // interest applicable 
     var Principal   = 0;       // Loan amt 
     var Interest    = 0.00;    // Int amt 
     var TotAmt      = 0.00;    // Tot amt (principal + interest)   
     var MthRepayAmt = 0.00;    // Repay amt 
     var dMthRepayAmt = 0.00;   // Interest Repayment for period of study 
             
     // read the input values
     Principal = document.edu.LnAmt.value; 
     LnPeriod = document.edu.LnPeriod.value;         
     Tenure = ((LnPeriod)*12); // Ln period in months 
     StudyPeriod = document.edu.StudyPeriod.value;         
     StudyTenure = ((StudyPeriod)*12); // study period in months 
   
     // 1)
     // Calculation of Standard Repayment scheme (SRS) 
     if (document.edu.Repayment[0].checked)
     { 
       //      
       if (document.edu.LnAmt.value >= 5000)
       {
        LnInt = SRSIntRate2; 
       }
       if (document.edu.LnAmt.value < 5000)
       {
        LnInt = SRSIntRate1; 
       } 
       //alert ("Standard Repayment Scheme "+LnInt+'%'); 
       // Calculate repayment's 
       var r = 0.00; 
       var n = 0; 
       var p = 0;  
              
    
       // new logic 10-Nov-2009          
       p = Principal; 
       n = Tenure; 
          //r = parseFloat((LnInt/100));            
       r = parseFloat((LnInt/100.0)); 
       r /= 12; 
       var pow = 1;       
       // 
       for (var j = 0; j < n; j++)
        pow = pow * (1 + r);  // power ==> (1+r)^n 
       
       money = "" + .01* Math.round(100*(p * pow * r) / (pow - 1)); 
       
       //alert ('money 1 = ' +money);
       
       dec = money.indexOf(".");
       //alert ('dec (dec)1 = ' +dec);
       if (dec < 0) {    // new logic 11-Nov-2009 
         dollars = money;
         cents = "00"; 
        } 
       if (dec >= 0) {
        dollars = money.substring(0,dec);
        cents = money.substring(dec+1,dec+3);
        cents = (cents.length < 2) ? cents + "0" : cents; 
        } 
       money = dollars + "." + cents; 
       //endif
       //alert ('dollars1 = ' +dollars);
       //cents = money.substring(dec+1,dec+3);
       //alert ('cents1 = ' +cents);
       //cents = (cents.length < 2) ? cents + "0" : cents; 
       //alert ('cents2 = ' +cents);
       //money = dollars + "." + cents; 

       MthRepayAmt =  money;    // new logic 10-Nov-2009  
     
        //alert ('MthRepayAmt amt =' +MthRepayAmt);      
      } // end of SRS loop   
              
     // 2)  
     // Calculation of Graduated Repayment scheme (GRS) 
     if (document.edu.Repayment[1].checked)
     { 
       //      
       if (document.edu.LnAmt.value >= 5000)
       {
        LnInt = GRSIntRate2; 
       }
       if (document.edu.LnAmt.value < 5000)
       {
        LnInt = GRSIntRate1; 
       } 
       //alert ("Graduated Repayment Scheme "+LnInt+'%'); 
       // Calculate repayment's 
       var r = 0.00; 
       var n = 0; 
       var dn = 0;   // study tenure (deferred period) 
       var p = 0; 
       var dpay = 0;  // deferred period re-payment  
              
       
      // new logic 10-Nov-2009          
       p = Principal; 
       n = Tenure; 
       dn = StudyTenure; 
         //r = parseFloat((LnInt/100));            
       r = parseFloat((LnInt/100.0)); 
       r /= 12; 
       
       // calculate for deferred period 
       dpay = Math.round(p * r * dn);  // deferred total interest amount 
       drepay = (Math.round(100*(dpay/dn)) / 100) ;  // deferred monthly repayment 
       //alert ('Total Interest Amount during deferred period = S$ ' +dpay ); 
       //alert ('Deferred Monthly Repayment = S$ ' +drepay ); 
       
       
       var pow = 1;       
       // 
       for (var j = 0; j < n; j++)
        pow = pow * (1 + r);  // power ==> (1+r)^n 
       
       money = "" + .01* Math.round(100*(p * pow * r) / (pow - 1)); 
       
       dec = money.indexOf(".");
       // 
       if (dec < 0) {    // new logic 11-Nov-2009 
         dollars = money;
         cents = "00"; 
        } 
       if (dec >= 0) {
        dollars = money.substring(0,dec);
        cents = money.substring(dec+1,dec+3);
        cents = (cents.length < 2) ? cents + "0" : cents; 
        } 
       money = dollars + "." + cents; 
       
       //dollars = money.substring(0,dec);
       //cents = money.substring(dec+1,dec+3);
       //cents = (cents.length < 2) ? cents + "0" : cents; 
       //money = dollars + "." + cents; 

       MthRepayAmt =  money;    // new logic 10-Nov-2009  
       dMthRepayAmt = drepay;
              
        //alert ('MthRepayAmt amt =' +MthRepayAmt);      
      } // end of GRS loop 
      
     // 3)
     // Calculation of Deferred Repayment scheme (DRS) 
     if (document.edu.Repayment[2].checked)
     { 
       //      
       if (document.edu.LnAmt.value >= 15000)
       {
        LnInt = DRSIntRate2; 
       }
       if (document.edu.LnAmt.value < 15000)
       {
        LnInt = DRSIntRate1; 
       } 
       //alert ("Deferred Repayment Scheme "+LnInt+'%'); 
       // Calculate repayment's 
       var r = 0.00; 
       var n = 0; 
       var p = 0; 
       
       
       // new logic 10-Nov-2009          
       p = Principal; 
       n = Tenure; 
       dn = StudyTenure; 
         //r = parseFloat((LnInt/100));            
       r = parseFloat((LnInt/100.0)); 
       r /= 12; 
       
       // calculate for deferred period 
       dpay = Math.round(p * r * dn);  // deferred total interest amount 
       drepay = (Math.round(100*(dpay/dn)) / 100) ;  // deferred monthly repayment 
       //alert ('Total Interest Amount during deferred period = S$ ' +dpay ); 
       //alert ('Deferred Monthly Repayment = S$ ' +drepay ); 
       
       
       var pow = 1;   
       // get new principal amount 
       for (var j = 0; j < dn; j++)
        pow = pow * (1 + r);  // power ==> (1+r)^dn 
       
       // new principal amount (including
       p =  "" + .01* (Math.round(100*(p * pow)) ) ; 
       //alert (' new value for p = ' +dp ); 
        
       pow = 1;     
       // 
       for (var j = 0; j < n; j++)
        pow = pow * (1 + r);  // power ==> (1+r)^n 
       
       money = "" + .01* Math.round(100*(p * pow * r) / (pow - 1)); 
       
       dec = money.indexOf(".");
       // 
       if (dec < 0) {    // new logic 11-Nov-2009 
         dollars = money;
         cents = "00"; 
        } 
       if (dec >= 0) {
        dollars = money.substring(0,dec);
        cents = money.substring(dec+1,dec+3);
        cents = (cents.length < 2) ? cents + "0" : cents; 
        } 
       money = dollars + "." + cents; 
       
       
       //dollars = money.substring(0,dec);
       //cents = money.substring(dec+1,dec+3);
       //cents = (cents.length < 2) ? cents + "0" : cents; 
       //money = dollars + "." + cents; 

       MthRepayAmt =  money;    // new logic 10-Nov-2009  
       
        //alert ('MthRepayAmt amt =' +MthRepayAmt);      
      } // end of DRS loop 
              
      // display to the user 
      document.edu.MthRepayment.value = (Math.round(parseFloat(MthRepayAmt)*100)/100); 
      document.edu.dMthRepayment.value = (Math.round(parseFloat(dMthRepayAmt)*100)/100); 
     
     } // end of if loop (checkValuesEdu)
  
  }; // end of CalcEduRepayment 
