function checkNumber(input, min, max, msg)
{
    msg = 'Error: Invalid value in ' + msg + " field: " + input.value;

    var str = input.value;
    for (var i = 0; i < str.length; i++) {
        var ch = str.substring(i, i + 1)
        if ((ch < "0" || "9" < ch) && ch != '.') {
            alert(msg);
            return false;
        }
    }
    var num = parseFloat(str)
    if (num < min || max < num) {
        // alert(msg + " not in range [" + min + ".." + max + "]");
        return false;
    }
    input.value = str;
    return true;
}

function computeField(input)
{
	if (input.value != null && input.value.length != 0)
		input.value = "" + eval(input.value);
		computeForm(input.form);
}

function computeForm(form)
{
    if (form.amount.value == null || form.amount.value.length == 0)
{
        return;
}

    if (!checkNumber(form.amount, 0, 32000000, "Insurance Amount") )
     {
     document.getElementById('basic').innerHTML = "Error: Amount is out of range";
     document.getElementById('reissue').innerHTML = "";
     document.getElementById('new_constr').innerHTML= "";
     document.getElementById('refi0-2yr').innerHTML= "";
     document.getElementById('refi2-4yr').innerHTML= "";
     return;
     }

     // calculation goes here - new rates effective January 1, 2002
     var amt = form.amount.value;
     amt = amt.toString().replace(/\$|\,/g,'');

     // if amount <= 30,000 - basic = 420
     if (amt <= 30000)
     {
     	document.getElementById('basic').innerHTML = "420.00";
     }

     // if amount <= 45,000 - basic = 420 + 7.25 /1000 over 30,000
     else if (amt <= 45000)
     {
		amt1=thousands(amt, 30000);
     	document.getElementById('basic').innerHTML = (420 + (amt1* 7.25));
     }

     // if amount <= 100,000 - basic = 528.75 + 6.00 /1000 over 45,000
     else if (amt <= 100000)
     {
     	amt1=thousands(amt, 45000);
     	document.getElementById('basic').innerHTML = (528.75 + (amt1* 6.00));
     }

     // if amount <= 500,000 - basic = 858.75 + 5.00 /1000 over 100,000
     else if (amt <= 500000)
     {
     	amt1=thousands(amt, 100000);
     	document.getElementById('basic').innerHTML = (858.75 + (amt1* 5.00));
     }

     // if amount <= 1,000,000 - basic = 2,858.75 + 3.75 /1000 over 500,000
     else if (amt <= 1000000)
     {
     	amt1=thousands(amt, 500000);
     	document.getElementById('basic').innerHTML = (2858.75 + (amt1* 3.75));
     }

     // if amount <= 2,000,000 - basic = 4,733.75 + 2.75 /1000 over 1,000,000
     else if (amt <= 2000000)
     {
     	amt1=thousands(amt, 1000000);
     	document.getElementById('basic').innerHTML = (4733.75 + (amt1* 2.75));
     }

     // over 2 million is a special area with everything at $2 per thousand
     // if amount <= 7,000,000 - basic = 7,483.75 + 2.00 /1000 over 2,000,000
     // if amount <= 7,000,000 - reissue = 6,735.375 + 2.00 /1000 over 2,000,000
     else if (amt <= 7000000)
     {
     amt1=thousands(amt, 2000000);
     document.getElementById('basic').innerHTML = (7483.75 + (amt1* 2.00));

     // now calculate reissue, construction and refinance - special for this range
     document.getElementById('reissue').innerHTML = (6735.375 + (amt1* 2.00) );
     document.getElementById('new_constr').innerHTML = (document.getElementById('reissue').innerHTML * .9);
     document.getElementById('refi0-2yr').innerHTML = (document.getElementById('reissue').innerHTML * .7);
     document.getElementById('refi2-4yr').innerHTML = (document.getElementById('reissue').innerHTML * .8);

     // round
     document.getElementById('basic').innerHTML = dollarFmt(roundit(document.getElementById('basic').innerHTML));
     document.getElementById('reissue').innerHTML = dollarFmt(roundit(document.getElementById('reissue').innerHTML));
     document.getElementById('new_constr').innerHTML = dollarFmt(roundit(document.getElementById('new_constr').innerHTML));
     document.getElementById('refi0-2yr').innerHTML = dollarFmt(roundit(document.getElementById('refi0-2yr').innerHTML));
	 document.getElementById('refi2-4yr').innerHTML = dollarFmt(roundit(document.getElementById('refi2-4yr').innerHTML));
     return;                                 // end of 2 - 7 million section
     }

     // over 7 million is a special area with everything at $1.50 per thousand?
     // if amount <= 30,000,000 - basic = 17,483.75 + 2.00 /1000 over 2,000,000
     // if amount <= 30,000,000 - reissue = 16,735.375 + 2.00 /1000 over 2,000,000
     else if (amt <= 30000000)
     {
     amt1=thousands(amt, 7000000);
     document.getElementById('basic').innerHTML = (17483.75 + (amt1* 1.50));

     // now calculate reissue, construction and refinance - special for this range
     document.getElementById('reissue').innerHTML = (16735.375 + (amt1* 1.50) );
     document.getElementById('new_constr').innerHTML = (document.getElementById('reissue').innerHTML * .9);
	 document.getElementById('refi0-2yr').innerHTML = (document.getElementById('reissue').innerHTML * .7);
     document.getElementById('refi2-4yr').innerHTML = (document.getElementById('reissue').innerHTML * .8);

     // round
     document.getElementById('basic').innerHTML = dollarFmt(roundit(document.getElementById('basic').innerHTML));
     document.getElementById('reissue').innerHTML = dollarFmt(roundit(document.getElementById('reissue').innerHTML));
     document.getElementById('new_constr').innerHTML = dollarFmt(roundit(document.getElementById('new_constr').innerHTML));
     document.getElementById('refi0-2yr').innerHTML = dollarFmt(roundit(document.getElementById('refi0-2yr').innerHTML));
	 document.getElementById('refi2-4yr').innerHTML = dollarFmt(roundit(document.getElementById('refi2-4yr').innerHTML));
     return;                                 // end of 7 - 30 million section
     }

     // now calculate reissue, construction and refinance
     document.getElementById('reissue').innerHTML = (document.getElementById('basic').innerHTML * .9);
     document.getElementById('new_constr').innerHTML = (document.getElementById('reissue').innerHTML * .9);
     document.getElementById('refi0-2yr').innerHTML = (document.getElementById('reissue').innerHTML * .7);
     document.getElementById('refi2-4yr').innerHTML = (document.getElementById('reissue').innerHTML * .8);

     // round
     document.getElementById('basic').innerHTML = dollarFmt(roundit(document.getElementById('basic').innerHTML));
     document.getElementById('reissue').innerHTML = dollarFmt(roundit(document.getElementById('reissue').innerHTML));
     document.getElementById('new_constr').innerHTML = dollarFmt(roundit(document.getElementById('new_constr').innerHTML));
     document.getElementById('refi0-2yr').innerHTML = dollarFmt(roundit(document.getElementById('refi0-2yr').innerHTML));
	 document.getElementById('refi2-4yr').innerHTML = dollarFmt(roundit(document.getElementById('refi2-4yr').innerHTML));

     return;
}

function thousands(amt, base) {
     var amt1 = ((amt - base) / 1000)
     var amt2 = parseInt(((amt - base) / 1000))
     if (amt1 > amt2) {
     amt1 = amt2 + 1
     }
     return (amt1);
}

function roundit(what){
     var places = 2
     var iplaces = 6
     var pad = ' '
     var xx = what.indexOf('.')
     var l = what.length
     var zstr = '0000000000000000000000'
     var theInt = ''
     var theFrac = ''
     var theNo = ''
     rfac = ''
     rfacx = 0
     whatx = 0
     var xt = parseInt(places) + 1
     var rstr = '' + zstr.substring(1,xt)
     var rfac = '.' + rstr + '5'
     var rfacx = parseFloat(rfac)
     if (xx == -1 ) {
          // even dollar amount - no decimals
          theFrac = zstr
          theInt = what
     }
     else if (xx == 0) {
          theInt = '0'
          whatx = 0 + parseFloat(what) + parseFloat(rfacx)
          what = whatx + zstr
          theFrac = '' + what.substring(1, what.length)
     }
     else {
          theInt = what.substring(0,xx)
          whatx = parseFloat(what) + rfacx
          what = '' + whatx + zstr
          theFrac = '' + what.substring(xx+1,xx + 1 + parseInt(places))
          var astr = 'places = ' + places
     }

     // theInt is the integer, theFrac is the decimals
     // round up to 0, 3, 5 or 8
     first = theFrac.substring(0,1)
     round = theFrac.substring(2,1)
     //special cases for 09 and 99 due to string consideration
     if (theFrac == 09){                          // 09 special
     theFrac = '10'
     }
     else if (theFrac == 99){                          // 99 special
     theFrac = '00'
     theInt  = parseInt(theInt) + 1
     theInt = '' + theInt
     }
     else if (round == 2 || round == 4 || round == 7 || round == 9 ) {          // add 1 cent
          //alert('the first = ' + first + '\nround = ' + round)
          if (first == 0){
               theFrac = parseInt(theFrac) + 1
               theFrac = '0' + parseInt(theFrac)
               }
               else {
               //alert('round 1 cent w/o a 0')
               theFrac = parseInt(theFrac) + 1
               }
          theFrac = '' + theFrac
          //alert('add 1 cent\nthe first = ' + first + '\nround = ' + round + '\ntheFrac = ' + theFrac)
     }
     else if (round == 1 || round == 6) {                        // add 2 cents
          if (first == 0){
               theFrac = parseInt(theFrac) + 2
               theFrac = '0' + parseInt(theFrac)
               }
               else {
               theFrac = parseInt(theFrac) + 2
               }
          theFrac = '' + theFrac
          //alert('add 2 cents\nthe first = ' + first + '\nround = ' + round + '\ntheFrac = ' + theFrac)
     }
     else {                                       // no rounding required
     }
     //alert('theInt= ' + theInt + '\ntheFrac= ' + theFrac + '\nwhat= ' + what + '\nwhatx= ' + whatx)
     theFrac = theFrac.substring(0,parseInt(places))
     var dif = iplaces - theInt.length
     var ii = 0
     var padstr = ''
     for (ii = 0 ; ii < dif ; ii++) {
          padstr += pad
     }
     theNo = padstr + theInt + '.' + theFrac
     return theNo;
}

function clearForm(form)
{
    form.amount.value = "";
    document.getElementById('basic').innerHTML = "";
    document.getElementById('reissue').innerHTML = "";
    document.getElementById('new_constr').innerHTML = "";
    document.getElementById('refi0-2yr').innerHTML = "";
    document.getElementById('refi2-4yr').innerHTML = "";
}


