/************************************************************
* CALC.JS													*
* 02-May-2001												*
*															*
* Calculates the rental terms for a given equipment value.	*
* UPdated by Sumo 2007-12-20
************************************************************/

	document.write('<script src=\"/calc/rates.js\" type=\"text/javascript\"></script>');

    var do_calculation = "TRUE";

    function AddCents(arg)
    {
    	//Correct calculation
    	if (arg > 0)
		{
			arg = Math.round((parseFloat(arg) + 0.01 )* 100)/100;
    	}
    	return arg;
    }
    
	function RoundValue( pSource )
	{
		return (Math.round( pSource * 100 ) / 100);
	}
    
    function ValidateAndCalc()
    {
    	do_calculation = "TRUE";
    	temp_value = document.rsform.thardware.value;
    	if ((document.rsform.thardware.value <= 0) || isNaN(temp_value))
    	{
    		alert("Please enter a valid Amount.");
    		return false;
    	}
    	else if (!document.rsform.Name.value)
		{
	    	alert("Please enter your Name.");
	    	return false;
    	}
    	calcform();
    	
    	//Correct rounding
    	document.rsform.tweekly1.value = checkzeros(RoundValue(document.rsform.tweekly1.value));
    	document.rsform.tweekly2.value = checkzeros(RoundValue(document.rsform.tweekly2.value));
    	document.rsform.tweekly3.value = checkzeros(RoundValue(document.rsform.tweekly3.value));
    	document.rsform.tmonthly1.value =checkzeros(RoundValue(document.rsform.tmonthly1.value));
    	document.rsform.tmonthly2.value = checkzeros(RoundValue(document.rsform.tmonthly2.value));
    	document.rsform.tmonthly3.value = checkzeros(RoundValue(document.rsform.tmonthly3.value));
    	document.rsform.ttotal1.value = checkzeros(RoundValue(document.rsform.ttotal1.value));
    	document.rsform.ttotal2.value = checkzeros(RoundValue(document.rsform.ttotal2.value));
    	document.rsform.ttotal3.value = checkzeros(RoundValue(document.rsform.ttotal3.value));

		if (do_calculation == "TRUE")
    	{
    		document.rsform.submit();
    	}
    }

    function calcform()
    {
	   SetRates();

      var TaxRate = document.rsform.TaxRate.value;

       var thwkeyed = document.rsform.thardware.value;
       var nfldlength = thwkeyed.length;
              
       if ((nfldlength > 0)&&(thwkeyed >= 0))
       {
          var nhw = parseFloat(thwkeyed);
       }
       else
       {
          var nhw = 0;
       }
       
       var nequipval = nhw
       var tequipval = checkzeros(nequipval)
       document.rsform.tequipval.value = tequipval
       
       //check minimum Equipment Value
       if ((nequipval < 500) && (nequipval != ''))
       {
          alert('The Amount Required must be at least \$500.00')
          do_calculation = "FALSE";
       }

       var tmonthly24 = "";
       var nmonthly24 = 0;
       var tmonthly36 = "";
       var nmonthly36 = 0;       
       var tmonthly48 = "";
       var nmonthly48 = 0;
              
       var linsured = false;
       
       //24 month check
       for (index=0; index<upper_limit24.length; index++)
       {
       		if((nequipval/1.125) < upper_limit24[index])
       		{
       			nmonthly24 = rentcalc((nequipval/1.125),rates24[index],linsured,ninsurepercent);
       			break;
          	}
       }
       
       //36 month check
       for (index=0; index<upper_limit36.length; index++)
       {
		if((nequipval/1.125) < upper_limit36[index])
		{
			nmonthly36 = rentcalc((nequipval/1.125),rates36[index],linsured,ninsurepercent);
			break;
		}
       }
       
       //48 month check
       for (index=0; index<upper_limit48.length; index++)
       {
       		if((nequipval/1.125) < upper_limit48[index])
       		{
       			nmonthly48 = rentcalc((nequipval/1.125),rates48[index],linsured,ninsurepercent);
       			break;
       		}
       }
      
       if(nequipval > 30000)
       {
             document.rsform.tequipval.value = '0.00';
             document.rsform.tweekly1.value = '0.00';
             document.rsform.tweekly2.value = '0.00';
             document.rsform.tmonthly1.value = '0.00';
             document.rsform.tmonthly2.value = '0.00';
             document.rsform.ttotal1.value = '0.00';
             document.rsform.ttotal2.value = '0.00';
             nhw = 0;
             nmonthly24 = 0;
             nmonthly36 = 0;
             
             document.rsform.tweekly3.value = '0.00';
             document.rsform.tmonthly3.value = '0.00';
             document.rsform.ttotal3.value = '0.00';
             nmonthly48 = 0;
             
             alert('Calculations for Amounts above \$30,000 will not be performed.');
             do_calculation = "FALSE";
       } 
       tmonthly24 = checkzeros(nmonthly24);
       tmonthly36 = checkzeros(nmonthly36);
       tmonthly48 = checkzeros(nmonthly48);
       
       document.rsform.thardware.value = checkzeros(nhw);
       if(nhw > 0)
       {
          document.rsform.thardware.value = checkzeros(nhw);
       }
       else
       {
          document.rsform.thardware.value = '';
       }
       
              
       document.rsform.tweekly1.value = checkzeros(nmonthly24*1.125*12/52);
       document.rsform.tweekly2.value = checkzeros(nmonthly36*1.125*12/52);
       document.rsform.tmonthly1.value = checkzeros(nmonthly24*1.125);
       document.rsform.tmonthly2.value = checkzeros(nmonthly36*1.125);
       
       document.rsform.tweekly3.value = checkzeros(nmonthly48*1.125*12/52);
       document.rsform.tmonthly3.value = checkzeros(nmonthly48*1.125);
	
	   at24 = (nmonthly24*24*1.125)*0.67;
	   at36 = (nmonthly36*36*1.125)*0.67;
	   at48 = (nmonthly48*48*1.125)*0.67;
       
       document.rsform.ttotal1.value = at24;
       document.rsform.ttotal2.value = at36;
       document.rsform.ttotal3.value = at48;
       
}


function rentcalc(ninnuma,ninnumb,linsured,nins_pc)
{
     var naddinsurance = 0;
     var nreturnnum = RoundValue( ninnuma ) * ninnumb / 100;
     
     if(linsured)
     {
        nreturnnum += RoundValue( nreturnnum * (nins_pc / 100) );
     }
     
     //BEGIN 24-Jan-2001 PT
     //return (nreturnnum += nreturnnum*(TaxRate/100);
     //END 24-Jan-2001
     
     return nreturnnum;
}


function checkzeros(ninnum)
{
      ninnum = RoundValue( ninnum );
      var tinnum = "" + ninnum;
      var lpointfound = false;
      var ndecimalplaces = 0;
      var nfldlength = tinnum.length;
      
      for(j = 0; j < nfldlength; j++)
      {
          if(lpointfound)
          {
             ndecimalplaces++ ;
          }
          if(tinnum.substring(j,j+1) == ".")
          {
             lpointfound = true;
          }
      }
      if(ndecimalplaces == 0)
      {
        tinnum = tinnum+".00";
      }
      if(ndecimalplaces == 1)
      {
        tinnum = tinnum+"0";
      }
      return tinnum;
}
