//*******************************************
//DO NOT REMOVE THIS COPYWRITE INFO!
//Credit Card Comparison Calculator
//2002 Daniel C. Peterson ALL RIGHTS RESERVED
//Created: 01/29/2002
//Last Modified: 01/29/2002
//This script may not be copied, edited, distributed or reproduced
//without express written permission from
//Daniel C. Peterson of Web Winder Website Services
//For commercial use rates, contact:
//Dan Peterson:
//Web Winder Website Services
//P.O. Box 11
//Bemidji, MN  56619
//dan@webwinder.com
//http://www.webwinder.com
//Commercial User Licence #:5782-1237-10-1198
//Commercial Licence Date:2008-09-11
//*******************************************



function stripNum(num) {

   num=num.toString();


   var len = num.length;
   var rnum = "";
   var test = "";
   var j = 0;

   var b = num.substring(0,1);
   if(b == "-") {
      rnum = "-";
   }

   for(i = 0; i <= len; i++) {

      b = num.substring(i,i+1);

      if(b == "0" || b == "1" || b == "2" || b == "3" || b == "4" || b == "5" || b == "6" || b == "7" || b == "8" || b == "9" || b == ".") {
         rnum = rnum + "" + b;

      }

   }

   if(rnum == "" || rnum == "-") {
      rnum = 0;
   }

   rnum = Number(rnum);

   return rnum;

}

function computeForm(form) {

if(form.principal.value == "" || form.principal.value ==0) {
alert("Please enter a principal amount.");
form.principal.focus();
} else
if(form.payment.value == "" || form.payment.value ==0) {
alert("Please enter a payment amount.");
form.payment.focus();
} else
if(form.regIntRate1.value == "" || form.regIntRate1.value ==0) {
alert("Please enter a regular interest rate for Card #1.");
form.regIntRate1.focus();
} else
if(form.regIntRate2.value == "" || form.regIntRate2.value ==0) {
alert("Please enter a regular interest rate for Card #2.");
form.regIntRate1.focus();
} else
if((form.introIntRate1.value.length == 0) && form.introMonths1.value.length > 0) {
alert("Please enter an introductory interest rate for Card #1.");
form.introIntRate1.focus();
} else
if((form.introIntRate2.value.length == 0) && form.introMonths2.value.length > 0) {
alert("Please enter an introductory interest rate for Card #2.");
form.introIntRate2.focus();
} else
if((form.introMonths1.value == "" || form.introMonths1.value ==0) && form.introIntRate1.value.length > 0) {
alert("Please enter the number of months the introductory interest rate will last for Card #1.");
form.introMonths1.focus();
} else
if((form.introMonths2.value == "" || form.introMonths2.value ==0) && form.introIntRate2.value.length > 0) {
alert("Please enter the number of months the introductory interest rate will last for Card #2.");
form.introMonths2.focus();
} else {

var Vprincipal = stripNum(form.principal.value);
var Vpayment = stripNum(form.payment.value);

//CALC CARD #1

var VannFee1 = stripNum(form.annFee1.value);
if(VannFee1 == 0 || VannFee1 =="") {
   VannFee1 = 0;
} else {
   VannFee1 = VannFee1 / 12;
}

var prin1 = Vprincipal;
var pmt1 = Vpayment;

var prin1 = Vprincipal;
var pmt1 = Vpayment;
var prinPort1 = 0;
var intPort1 = 0;
var count1 = 0;
var accumInt1 = 0;
var dailyCount1 = 0;
var tempInt1 = 0;

//IF INTRO RATE, DO FOLLOWING:
var VintroMonths1 = stripNum(form.introMonths1.value);
if(VintroMonths1 == 0 || VintroMonths1 =="") {
VintroMonths1 = 0;
}

if(VintroMonths1 > 0 && form.introIntRate1.value.length > 0) {

   var VintroIntRate1 = stripNum(form.introIntRate1.value);
   if (VintroIntRate1 > 1.0) {
      VintroIntRate1 = VintroIntRate1 / 100.0;
   }

   if(form.compound1.selectedIndex == 0) {
   VintroIntRate1 /= 12;
   } else {
   VintroIntRate1 /= 365;
   }

   if(form.compound1.selectedIndex == 1) {
      while(count1 < VintroMonths1) {
         dailyCount1 = 0;
         tempInt1 = 0;
         while(dailyCount1 < 31) {
            tempInt1 = eval(VintroIntRate1 * prin1);
            accumInt1 = eval(accumInt1) +  + eval(tempInt1);
            prin1 = eval(prin1) + eval(tempInt1);
            dailyCount1 = dailyCount1 + 1;
         }
         prin1 = eval(prin1) - eval(pmt1);
         count1 = count1 + 1
         if(count1 > 1000) {
            alert("Given the entered balance, payment and interest rate, card #1 will never be paid off. Please increase the payment amount.");
            form.payment.focus();
            break;
         } else {
            continue;
         }
      }

   } else {
      while(count1 < VintroMonths1) {
         intPort1 = eval(VintroIntRate1 * prin1);
         prinPort1 = eval(pmt1 - intPort1);
         prin1 = eval(prin1 - prinPort1);
         accumInt1 = eval(accumInt1 + intPort1);
         count1 = count1 + 1
         if(count1 > 1000) {
            alert("Given the entered balance, payment and interest rate, card #1 will never be paid off. Please increase the payment amount.");
            form.payment.focus();
            break;
         } else {
            continue;
         }
      }
   }
}
// END INTRO CALC

//form.nPer2.value = prin1;
//form.totalCosts2.value = accumInt1;

//BEGIN REGULAR INTEREST CALC

var VregIntRate1 = stripNum(form.regIntRate1.value);
if (VregIntRate1 > 1.0) {
   VregIntRate1 = VregIntRate1 / 100.0;
}

if(form.compound1.selectedIndex == 0) {
VregIntRate1 /= 12;
} else {
VregIntRate1 /= 365;
}

if(form.compound1.selectedIndex == 1) {
while((prin1 * ( 1 + VregIntRate1)) > pmt1) {
//while(count1 < 2) {
      dailyCount1 = 0;
      tempInt1 = 0;
      while(dailyCount1 < 31) {
         tempInt1 = eval(VregIntRate1 * prin1);
         accumInt1 = eval(accumInt1) +  + eval(tempInt1);
         prin1 = eval(prin1) + eval(tempInt1);
         dailyCount1 = dailyCount1 + 1;
      }
      prin1 = eval(prin1) - eval(pmt1);
      count1 = count1 + 1
      if(count1 > 1000) {
         alert("Given the entered balance, payment and interest rate, card #1 will never be paid off. Please increase the payment amount.");
         form.payment.focus();
         break;
      } else {
         continue;
      }
   }
//FINAL INTEREST PAYMENT
      dailyCount1 = 0;
      tempInt1 = 0;
      while(dailyCount1 < 31) {
         tempInt1 = eval(VregIntRate1 * prin1);
         accumInt1 = eval(accumInt1) +  + eval(tempInt1);
         prin1 = eval(prin1) + eval(tempInt1);
         dailyCount1 = dailyCount1 + 1;
      }
   count1 = count1 + 1

} else {
   while((prin1 * ( 1 + VregIntRate1)) > pmt1) {
      intPort1 = eval(VregIntRate1 * prin1);
      prinPort1 = eval(pmt1 - intPort1);
      prin1 = eval(prin1 - prinPort1);
      accumInt1 = eval(accumInt1 + intPort1);
      count1 = count1 + 1
      if(count1 > 1000) {
         alert("Given the entered balance, payment and interest rate, card #1 will never be paid off. Please increase the payment amount.");
         form.payment.focus();
         break;
      } else {
         continue;
      }
   }
//FINAL INTEREST PAYMENT
   intPort1 = eval(VregIntRate1 * prin1);
   accumInt1 = eval(accumInt1 + intPort1);
   count1 = count1 + 1
}

VannFee1 = VannFee1 * count1;

var VtotalCosts1 = eval(VannFee1) + eval(accumInt1);

form.totalCosts1.value = "$" +parseInt(VtotalCosts1,10);
form.nPer1.value = count1; 

//CALC CARD #2

var VannFee2 = stripNum(form.annFee2.value);
if(VannFee2 == 0 || VannFee2 =="") {
   VannFee2 = 0;
} else {
   VannFee2 = VannFee2 / 12;
}

var prin2 = Vprincipal;
var pmt2 = Vpayment;

var prin2 = Vprincipal;
var pmt2 = Vpayment;
var prinPort2 = 0;
var intPort2 = 0;
var count2 = 0;
var accumInt2 = 0;
var dailyCount2 = 0;
var tempInt2 = 0;

//IF INTRO RATE, DO FOLLOWING:
var VintroMonths2 = stripNum(form.introMonths2.value);
if(VintroMonths2 == 0 || VintroMonths2 =="") {
VintroMonths2 = 0;
}

if(VintroMonths2 > 0 && form.introIntRate1.value.length > 0) {

   var VintroIntRate2 = stripNum(form.introIntRate2.value);
   if (VintroIntRate2 > 1.0) {
      VintroIntRate2 = VintroIntRate2 / 100.0;
   }

   if(form.compound2.selectedIndex == 0) {
   VintroIntRate2 /= 12;
   } else {
   VintroIntRate2 /= 365;
   }

   if(form.compound2.selectedIndex == 1) {
      while(count2 < VintroMonths2) {
         dailyCount2 = 0;
         tempInt2 = 0;
         while(dailyCount2 < 31) {
            tempInt2 = eval(VintroIntRate2 * prin2);
            accumInt2 = eval(accumInt2) +  + eval(tempInt2);
            prin2 = eval(prin2) + eval(tempInt2);
            dailyCount2 = dailyCount2 + 1;
         }
         prin2 = eval(prin2) - eval(pmt2);
         count2 = count2 + 1
         if(count2 > 1000) {
            alert("Given the entered balance, payment and interest rate, card #2 will never be paid off. Please increase the payment amount.");
            form.payment.focus();
            break;
         } else {
            continue;
         }
      }

   } else {
      while(count2 < VintroMonths2) {
         intPort2 = eval(VintroIntRate2 * prin2);
         prinPort2 = eval(pmt2 - intPort2);
         prin2 = eval(prin2 - prinPort2);
         accumInt2 = eval(accumInt2 + intPort2);
         count2 = count2 + 1
         if(count2 > 1000) {
            alert("Given the entered balance, payment and interest rate, card #2 will never be paid off. Please increase the payment amount.");
            form.payment.focus();
            break;
         } else {
            continue;
         }
      }
   }
}
// END INTRO CALC

//form.nPer2.value = prin2;
//form.totalCosts2.value = accumInt2;

//BEGIN REGULAR INTEREST CALC

var VregIntRate2 = stripNum(form.regIntRate2.value);
if (VregIntRate2 > 1.0) {
   VregIntRate2 = VregIntRate2 / 100.0;
}

if(form.compound2.selectedIndex == 0) {
VregIntRate2 /= 12;
} else {
VregIntRate2 /= 365;
}

if(form.compound2.selectedIndex == 1) {
while((prin2 * ( 1 + VregIntRate2)) > pmt2) {
//while(count2 < 2) {
      dailyCount2 = 0;
      tempInt2 = 0;
      while(dailyCount2 < 31) {
         tempInt2 = eval(VregIntRate2 * prin2);
         accumInt2 = eval(accumInt2) +  + eval(tempInt2);
         prin2 = eval(prin2) + eval(tempInt2);
         dailyCount2 = dailyCount2 + 1;
      }
      prin2 = eval(prin2) - eval(pmt2);
      count2 = count2 + 1
      if(count2 > 1000) {
         alert("Given the entered balance, payment and interest rate, card #2 will never be paid off. Please increase the payment amount.");
         form.payment.focus();
         break;
      } else {
         continue;
      }
   }
//FINAL INTEREST PAYMENT
      dailyCount2 = 0;
      tempInt2 = 0;
      while(dailyCount2 < 31) {
         tempInt2 = eval(VregIntRate2 * prin2);
         accumInt2 = eval(accumInt2) +  + eval(tempInt2);
         prin2 = eval(prin2) + eval(tempInt2);
         dailyCount2 = dailyCount2 + 1;
      }
   count2 = count2 + 1;

} else {
   while((prin2 * ( 1 + VregIntRate2)) > pmt2) {
      intPort2 = eval(VregIntRate2 * prin2);
      prinPort2 = eval(pmt2 - intPort2);
      prin2 = eval(prin2 - prinPort2);
      accumInt2 = eval(accumInt2 + intPort2);
      count2 = count2 + 1;
      if(count2 > 2000) {
         alert("Given the entered balance, payment and interest rate, card #2 will never be paid off. Please increase the payment amount.");
         form.payment.focus();
         break;
      } else {
         continue;
      }
   }
//FINAL INTEREST PAYMENT
   intPort2 = eval(VregIntRate2 * prin2);
   accumInt2 = eval(accumInt2 + intPort2);
   count2 = count2 + 1;
}

VannFee2 = VannFee2 * count2;

var VtotalCosts2 = eval(VannFee2) + eval(accumInt2);

form.totalCosts2.value = "$" +parseInt(VtotalCosts2,10);
form.nPer2.value = count2; 

}


}
