var pmtDBO_present, pmtFMV_present, pmtMVPUT_present

function myDate() {
   var d=new Date()
   document.write(d.getDate() + "/")
   document.write(d.getMonth() + 1 + " ")
   var hours = d.getHours()
   var minutes = d.getMinutes()
   var seconds = d.getSeconds()
   var clock = hours
   var ap = "AM"
   if (hours > 12) {
      ap = "PM"
   }
   clock += ((minutes < 10) ? ":0" : ":") + minutes
   clock += ((seconds < 10) ? ":0" : " ") + ap
   document.write(clock);
}

function DollarDigitsOnly(s)
{
   var result = "";
   var i, c;
   var neg_found=false;
   var non_zero_found=false;

   for( i = 0 ; i < s.length ; i++ ) {
      c = s.charAt(i);
      if (( c >= '1' && c <= '9' )|| (c == '.')) {
         result = result + c;
         non_zero_found = true;
      } else {
         if (c == '-' && !neg_found ) {
            result = result + c;
            neg_found = true;
         } else {
            if (c == '0' && non_zero_found ) {
               result = result + c;
            }
         }
      }
   }
   if( result == "" || result == "-" ) result = "0";

   return result;
}

function FormatDollars(s)
{
   var k = 0;
   var w = 0;
   var result = "";

   s = DollarDigitsOnly(s);
   if (s.length == 0) return result;

   l=0;
   if (s.indexOf ('.') != -1) {
      k = s.length - s.indexOf ('.');
      w = s.indexOf ('.');
   }
   else {
      k = 0;
      w = 0;
   }
   for(i = s.length; i > 0 ; i--) {
      c = s.charAt(i - 1);
      if ((s.length - i < k) && (k != 0)) {
         result = c + result;
         continue;
      }
      if (l > 0 && c != '-' && (l % 3 == 0)) result = "," + result;
      result = c + result;
      l++;
   }
   //if (result = "0") return "";
   return "$" + result;
}

terms = new Array;
pmtDBOs = new Array;
pmtFMVs = new Array;
pmtMVPUTs = new Array;

var sp = '        ';
//deletes a select option
function delSel(whoa)
{
  var i;
  document.forms['f1'].elements[whoa].length = 0;
  document.forms['f1'].elements[whoa].options[0] = new Option(sp)
}

//selection of lease-amount (leftmost input)
// 7/14/2003 Jonathan F. 
// Overhauled to contain new data, loan amount ranges, new months and to add FMV PUT 

function setloanApplicationAmount(amt)
{
var loanApplicationAmount=DollarDigitsOnly(amt);

var termCount = 5   //5 terms:  12, 24, 36, 48 and 60 months

var F12DBO, F24DBO,F36DBO, F48DBO, F60DBO
var F12FMV, F24FMV, F36FMV, F48FMV, F60FMV
var F12FMVPUT, F24FMVPUT, F36FMVPUT, F48FMVPUT, F60FMVPUT

// JF: Figure out the loan amount, then give them the appropriate rates based on amount ranges
maxTermForAmount = termCount;

	F12DBO = NaN
	F24DBO = NaN
	F36DBO = NaN
	F48DBO = NaN
	F60DBO = NaN
	F12FMV = NaN
	F24FMV = NaN
	F36FMV = NaN
	F48FMV = NaN
	F60FMV = NaN
	F12MVPUT = NaN
	F24MVPUT = NaN
	F36MVPUT = NaN
	F48MVPUT = NaN
	F60MVPUT = NaN

if (loanApplicationAmount >= 1000 & loanApplicationAmount < 25000) {
    maxTermForAmount = 3;  // 36 months is the longest term available for this amount
	F12DBO = 0.09772
	F24DBO = 0.05051
	F36DBO = 0.03482
	F48DBO = 0.0
	F60DBO = 0.0
	F12FMV = 0.08805
	F24FMV = 0.04583
	F36FMV = 0.03179
	F48FMV = 0.0
	F60FMV = 0.0
	F12MVPUT = 0.07958
	F24MVPUT = 0.04274
	F36MVPUT = 0.03085
	F48MVPUT = 0.0
	F60MVPUT = 0.0
}

if (loanApplicationAmount >= 25000 & loanApplicationAmount < 50000) {
	F12DBO = 0.09641
	F24DBO = 0.04968
	F36DBO = 0.03414
	F48DBO = 0.02771
	F60DBO = 0.02322
	F12FMV = 0.08672
	F24FMV = 0.04498
	F36FMV = 0.03110
	F48FMV = 0.0
	F60FMV = 0.0
	F12MVPUT = 0.07825
	F24MVPUT = 0.04189
	F36MVPUT = 0.03017
	F48MVPUT = 0.02589
	F60MVPUT = 0.02183
}

if (loanApplicationAmount >= 50000 & loanApplicationAmount < 75000) {
	F12DBO = 0.09614
	F24DBO = 0.04951
	F36DBO = 0.03400
	F48DBO = 0.02750
	F60DBO = 0.02311
	F12FMV = 0.08646
	F24FMV = 0.04481
	F36FMV = 0.03097
	F48FMV = 0.0
	F60FMV = 0.0
	F12MVPUT = 0.07798
	F24MVPUT = 0.04173
	F36MVPUT = 0.03003
	F48MVPUT = 0.02568
	F60MVPUT = 0.02161
}

if (loanApplicationAmount >= 75000 & loanApplicationAmount < 100000) {
	F12DBO = 0.09602
	F24DBO = 0.04944
	F36DBO = 0.03395
	F48DBO = 0.02750
	F60DBO = 0.02311
	F12FMV = 0.08635
	F24FMV = 0.04475
	F36FMV = 0.03090
	F48FMV = 0.0
	F60FMV = 0.0
	F12MVPUT = 0.07786
	F24MVPUT = 0.04166
	F36MVPUT = 0.02998
	F48MVPUT = 0.02568
	F60MVPUT = 0.02161
}

// DBR:  Now set up arrays with monthly payments for the various products
terms[0]=12
pmtDBOs[0]=Math.round(loanApplicationAmount * F12DBO)
pmtFMVs[0]=Math.round(loanApplicationAmount * F12FMV)
pmtMVPUTs[0]=Math.round(loanApplicationAmount * F12MVPUT)

terms[1]=24
pmtDBOs[1]=Math.round(loanApplicationAmount * F24DBO)
pmtFMVs[1]=Math.round(loanApplicationAmount * F24FMV)
pmtMVPUTs[1]=Math.round(loanApplicationAmount * F24MVPUT)

terms[2]=36
pmtDBOs[2]=Math.round(loanApplicationAmount * F36DBO)
pmtFMVs[2]=Math.round(loanApplicationAmount * F36FMV)
pmtMVPUTs[2]=Math.round(loanApplicationAmount * F36MVPUT)

terms[3]=48
pmtDBOs[3]=Math.round(loanApplicationAmount * F48DBO)
pmtFMVs[3]=Math.round(loanApplicationAmount * F48FMV)
pmtMVPUTs[3]=Math.round(loanApplicationAmount * F48MVPUT)

terms[4]=60
pmtDBOs[4]=Math.round(loanApplicationAmount * F60DBO)
pmtFMVs[4]=Math.round(loanApplicationAmount * F60FMV)
pmtMVPUTs[4]=Math.round(loanApplicationAmount * F60MVPUT)

  //DBR:  determine which payment fields we have to calc (as user may not pass us all of them...)
  pmtDBO_present = typeof document.forms['f1'].pmtDBO != 'undefined';
  pmtFMV_present = typeof document.forms['f1'].pmtFMV != 'undefined';
  pmtMVPUT_present = typeof document.forms['f1'].pmtMVPUT != 'undefined';

  //DBR:  clear payment values--we'll re-calc when user selects the term
  if (pmtDBO_present) {
    document.forms['f1'].pmtDBO.value = "";}
  if (pmtFMV_present) {
    document.forms['f1'].pmtFMV.value = "";}
  if (pmtMVPUT_present) {
    document.forms['f1'].pmtMVPUT.value = "";}

  //DBR:  populate term option box
  //First, remember  orig. selection, if possible
  origTermIndex = document.forms['f1'].elements['optionTerm'].selectedIndex;
  
  for (i = 0; i < termCount; i++)
  {
    if (i <= maxTermForAmount ){
      document.forms['f1'].elements['optionTerm'].options[i] = new Option(terms[i], i);}
	else  {
      document.forms['f1'].elements['optionTerm'].options[i] = new Option(sp, i);
	}
  }
  document.forms['f1'].elements['optionTerm'].length=maxTermForAmount;
  
  if ((origTermIndex  >= 0) & (origTermIndex <= maxTermForAmount)) {
    document.forms['f1'].elements['optionTerm'].options[origTermIndex].selected = true;}
  else {
    document.forms['f1'].elements['optionTerm'].options[maxTermForAmount-1].selected = true;}
  setTerm( document.forms['f1'].elements['optionTerm'].selectedIndex);	
}

//selection of terms (center-left select)
function setTerm(termIndex)
{
  if (pmtDBO_present) { 
    if (isNaN(pmtDBOs[termIndex])) {document.forms['f1'].pmtDBO.value='Call for pricing'}
    else{document.forms['f1'].pmtDBO.value = FormatDollars(pmtDBOs[termIndex].toString())}
    document.forms['f1'].pmtDBO.disabled  = (document.forms['f1'].pmtDBO.value <= 0);
    document.forms['f1'].pmtDBO.size = document.forms['f1'].pmtDBO.value.length;
  }
	
  if (pmtFMV_present) {
    if (isNaN(pmtFMVs[termIndex])) {document.forms['f1'].pmtFMV.value='Call for pricing'}
    else{document.forms['f1'].pmtFMV.value = FormatDollars(pmtFMVs[termIndex].toString())}
    document.forms['f1'].pmtFMV.disabled  = (document.forms['f1'].pmtFMV.value <= 0);
    document.forms['f1'].pmtFMV.size = document.forms['f1'].pmtFMV.value.length;
  }
  
  if (pmtMVPUT_present) {
    if (isNaN(pmtMVPUTs[termIndex])) {document.forms['f1'].pmtMVPUT.value='Call for pricing'}
    else{document.forms['f1'].pmtMVPUT.value = FormatDollars(pmtMVPUTs[termIndex].toString())}
    document.forms['f1'].pmtMVPUT.disabled  = (document.forms['f1'].pmtMVPUT.value <= 0);
    document.forms['f1'].pmtMVPUT.size = document.forms['f1'].pmtMVPUT.value.length;  
  }
}

function val1(form) {
   var input_str=document.forms['f1'].loanApplicationAmount.value;
   var input_len4=input_str.length;
   if(input_len4 == 0 || Number(input_str) < 1000) {
     alert("Amount of Lease should not be blank or less than one thousand dollars,  Please try again");
     return false;
   }
   return true;
}
function val2(form) {
   var input_str=document.forms['f1'].optionTerm.value;
   var input_len4=input_str.length;
   if(input_len4 == 0) {
     alert("Terms of Lease should not be blank,  Please try again");
     return false;
   }
   return true;
}
function submitme(form) {
  if(val1(form) && val2(form)) {

    if(Number(document.forms['f1'].loanApplicationAmount.value) > 75000) {
      document.forms['f1'].loanID.value=326;
    }
    else {
	  document.forms['f1'].loanID.value=325;  
    }
    
    document.forms['f1'].leaseTerm.value = terms[document.forms['f1'].optionTerm.selectedIndex];
    // we could set leaseEndPurchaseOption, but currently the form doesn't ask the user
    // to select which option they want...
    document.forms['f1'].leaseEndPurchaseOption.value = '';
    
    document.forms['f1'].enctype="application/x-www-form-urlencoded";
	// we 're just submitting to FinanceApp.asp
    document.forms['f1'].submit();
    return true;
   } 
  else {
    return false;
   }
}