// Name: INS.js
// Author: Adam Butcher
// Date: June 2005
// Description: INS Node Scripts (Currently for Finance Now pre-approval)
// Purpose: Performs on the fly calculations for the Finance Calculator

function Finance_jumpMenu(targ,selObj,restore) {
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function floor(number) {
return Math.floor(number*Math.pow(10,2))/Math.pow(10,2); 
}

function formatAsMoney(mnt) {
    mnt -= 0;
    mnt = (Math.round(mnt*100))/100;
    return (mnt == Math.floor(mnt)) ? mnt + '.00' 
              : ( (mnt*10 == Math.floor(mnt*10)) ? 
                       mnt + '0' : mnt);
}

function dosum()
{
  var minamt = (document.fnc.MinAmt.value / 1);
  var mi = (document.fnc.FinRate.value) / 1200;
  var base = 1;
  var mbase = 1 + mi;
  var bf = (document.fnc.BF.value);
  var term = (document.fnc.trm.value);

   if ((document.fnc.ServiceFeeFreq.value) == "") {
   	ServiceFee = 0;
   	var TotalServiceFee = 0;
   }
  
   if ((document.fnc.ServiceFee.value) == "") {
   	var ServiceFee = 0;
   }
   else {
   var ServiceFee = (document.fnc.ServiceFee.value);
   }
   
  if ((document.fnc.ServiceFeeFreq.value) == "Monthly") {
	var TotalServiceFee = (ServiceFee * term);
  }
  else
  if ((document.fnc.ServiceFeeFreq.value) == "Yearly") {
		if (term <= 12) {
			var TotalServiceFee = (ServiceFee * 1);
		}
		if (term > 12) {
			var TotalServiceFee = (ServiceFee * 2);
		}
		if (term > 24) {
			var TotalServiceFee = (ServiceFee * 3);
		}
		if (term > 36) {
			var TotalServiceFee = (ServiceFee * 4);
		}
  }
  else 
  {
  	var TotalServiceFee = 0;
  }
  
for (i=0; i<document.fnc.trm.value; i++)
  {
    base = base * mbase
  }

   document.fnc.CP.value = formatAsMoney(document.fnc.cp.value);
   document.fnc.DPD.value = formatAsMoney(document.fnc.dep.value);
   
   var bf = Math.abs(document.fnc.BF.value);
   document.fnc.BF.value = formatAsMoney(bf);


  if (isNaN(document.fnc.ServiceFee.value)) {
   document.fnc.ServiceFee.value = 0;
   document.fnc.TotalServiceFee.value = 0;
  } 
  else
  if (isNaN(document.fnc.cp.value)) {
   document.fnc.wp.value = "-";
   document.fnc.mp.value = "-";
   document.fnc.CP.value = "-";
   document.fnc.DPD.value = "-";
   document.fnc.AF.value = "-";
   document.fnc.IA.value = "-";
   document.fnc.TotalServiceFee.value = "-";
   document.fnc.TAP.value = "-";
   alert("Please enter a finance amount greater than $" + minamt);
  } 
  else
  if ((document.fnc.DPD.value / 1) > (document.fnc.CP.value / 1)) { 
   document.fnc.dep.value = 0; 
   document.fnc.wp.value = "-";
   document.fnc.mp.value = "-";
   document.fnc.DPD.value = "-";
   document.fnc.AF.value = "-";
   document.fnc.IA.value = "-";
   document.fnc.TotalServiceFee.value = "-";
   document.fnc.TAP.value = "-";
   alert("Please enter valid deposit amount"); 
  }
  else 
  if (document.fnc.trm.value == 1000) {
   document.fnc.trm.value = "1000";
   document.fnc.wp.value = "-";
   document.fnc.mp.value = "-";
   document.fnc.DPD.value = "-";
   document.fnc.IA.value = "-";
   document.fnc.TotalServiceFee.value = "-";
   document.fnc.TAP.value = "-";
//   alert("Please select a term");
  } 
  else 
  if (document.fnc.dep.value < 0) {
   document.fnc.dep.value = 0; 
   document.fnc.wp.value = "-";
   document.fnc.mp.value = "-";
   document.fnc.DPD.value = "-";
   document.fnc.AF.value = "-";
   document.fnc.IA.value = "-";
   document.fnc.TotalServiceFee.value = "-";
   document.fnc.TAP.value = "-";
   alert("Please enter valid deposit amount"); 
  } 
  else 
  if (isNaN(document.fnc.dep.value)) {
   document.fnc.wp.value = "-";
   document.fnc.mp.value = "-";
   document.fnc.DPD.value = "-";
   document.fnc.AF.value = "-";
   document.fnc.IA.value = "-";
   document.fnc.TotalServiceFee.value = "-";
   document.fnc.TAP.value = "-";
  }
  else
  if ((document.fnc.FinRate.value) == 0) { 
   document.fnc.AF.value = (document.fnc.cp.value-document.fnc.DPD.value);
   document.fnc.AF.value = formatAsMoney(document.fnc.AF.value);
   var ServiceFee = Math.abs(document.fnc.ServiceFee.value);
   var af = Math.abs(document.fnc.AF.value);
   document.fnc.TAP.value = floor(bf+af+TotalServiceFee);
   var mp = document.fnc.TAP.value/document.fnc.trm.value;
   document.fnc.TAP.value = formatAsMoney(document.fnc.TAP.value);
   document.fnc.mp.value = floor(mp);
   document.fnc.mp.value = formatAsMoney(document.fnc.mp.value);
   document.fnc.wp.value = floor((document.fnc.mp.value * 12) / 52 );
   document.fnc.wp.value = formatAsMoney(document.fnc.wp.value);
   document.fnc.DPD.value = formatAsMoney(document.fnc.dep.value);
   document.fnc.IA.value = "-";
   document.fnc.TotalServiceFee.value = formatAsMoney(TotalServiceFee);
  }
  else 
  {
   document.fnc.AF.value = (document.fnc.cp.value-document.fnc.DPD.value);
   document.fnc.AF.value = formatAsMoney(document.fnc.AF.value);
   var dasum = (document.fnc.AF.value * mi / ( 1 - (1/base)));
   var mp = (dasum + (bf/document.fnc.trm.value) + (TotalServiceFee/term));
   document.fnc.mp.value = floor(mp);
   document.fnc.mp.value = formatAsMoney(document.fnc.mp.value);
   document.fnc.wp.value = floor((document.fnc.mp.value * 12) / 52 );
   document.fnc.wp.value = formatAsMoney(document.fnc.wp.value);
   document.fnc.IA.value = floor((mp*document.fnc.trm.value)-document.fnc.AF.value-bf-TotalServiceFee);
   document.fnc.IA.value = formatAsMoney(document.fnc.IA.value);
   document.fnc.TotalServiceFee.value = formatAsMoney(TotalServiceFee);
   var TotalServiceFee = Math.abs(TotalServiceFee);
   var ia = Math.abs(document.fnc.IA.value);
   var af = Math.abs(document.fnc.AF.value);
   document.fnc.TAP.value = floor(TotalServiceFee+bf+ia+af);
   document.fnc.TAP.value = formatAsMoney(document.fnc.TAP.value);
  }
}

function validateForm() { 
 var minamt = (document.fnc.MinAmt.value / 1);
  if ((document.fnc.DPD.value / 1) > (document.fnc.CP.value / 1)) { 
   alert("Please enter valid deposit amount"); 
   return false;
  }
  if (document.fnc.AF.value == "-") { 
   alert("Please enter valid deposit amount"); 
   return false;
  }
 if (document.fnc.cp.value < minamt ) {
   alert("Please enter a finance amount greater than $" + minamt);
   fnc.cp.focus();
   return false;
   }

 if (isNaN(document.fnc.cp.value)) {
   alert("Please enter a finance amount greater than $" + minamt);
   fnc.cp.focus();
   return false;
   }
   
 if (isNaN(document.fnc.dep.value)) {
   alert("Please enter valid deposit amount");
   fnc.dep.focus();
   return false;
   }
   
 if (document.fnc.trm.value > 48) {
   alert("Please select a term");
   fnc.trm.focus();
   return false;
   }
}   

function checktrm(){
if (document.layers||document.getElementById||document.all)
return validateForm()
else
return true
}

function validateForm2() { 
if (fncCustDetails.fcfn.value == "") {
   alert("Please enter your first name");
   fncCustDetails.fcfn.focus();
   return false;
   }
   
if (fncCustDetails.fcln.value == "") {
   alert("Please enter your last name");
   fncCustDetails.fcln.focus();
   return false;
   }

   var testresults
   var str=document.fncCustDetails.fce.value
   var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
   if (filter.test(str))
   testresults=true
   else{
   alert("Please enter a valid email address")
   testresults=false
   }
   return (testresults)
}


function checkbae(){
if (document.layers||document.getElementById||document.all)
return validateForm2()
else
return true
}