// http://alphaadv.net/njtitle/njratecalc.js
<!-- New Jersey Title Insurance Rate Calculator -->
<!-- Thank you for finding this page interesting enough to view the source code. -->
<!-- Please respect my copyrights.  Thankx -->
<!-- Page written November 8, 1998, Last update December 18, 2006 -->

<!-- Changing the lines above or below is an invitation to Adventures in Legal Land -->

<!-- Activate Cloaking Device -->

function checkNumber(input, min, max, msg)
{
    msg = msg + " field has invalid data: " + 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 calcNJrates(form)
{
     form.basicsearch.value =105.00
     form.basicsearch.value=roundit(form.basicsearch.value)
     form.reissuesearch.value =90.00
     form.reissuesearch.value=roundit(form.reissuesearch.value)
     form.refinancesearch.value =90.00
     form.refinancesearch.value=roundit(form.refinancesearch.value)
     form.constructionsearch.value =90.00
     form.constructionsearch.value=roundit(form.constructionsearch.value)

// tiamt = greater of saleprice or mortgageamount
     var tiamt = 0
     var sale = form.saleprice.value / 1
     var mtg = form.mtgamount.value / 1
     var previoussale = form.previoussaleprice.value / 1
     var previousmtg = form.previousmtgamount.value / 1
     if (sale >= mtg) {
     tiamt = form.saleprice.value
     }
     else {
     tiamt = form.mtgamount.value
     }

// caclulate basic
     form.basic.value=basic(tiamt)


// calculate reissue
// - A - partial reissue (basic + reissue), if the sale price is greater than the previous sale price, else
// - B - full reissue
     if (tiamt > previoussale) {
     // A - partial reissue
          // 1  reissue(previoussaleprice)
               var reiscalc=0
               reiscalc=reissue(form.previoussaleprice.value)

          // 2 - basic(previoussaleprice)
               reiscalc=reiscalc - basic(form.previoussaleprice.value)

          // 3 + basic(saleprice)
               form.reissue.value = reiscalc + basic(tiamt)
     }

     // B - full reissue
     else {
          form.reissue.value= reissue(tiamt)
     }


// calculate refinance
// - A - full refinance, if the old mortgage amount is greater than the mortgage amount, else
// - B - refinance & reissue, if the previous sale price is greater than the mortgage amount
// - C - regular refiance - refi, reissue, basic
     if (previousmtg > mtg) {
     // A - full refinance
          form.refinance.value = refi(mtg)
     }

     // B - refinance up to the old mtg, then reissue to new mtg
     else if (previoussale > mtg) {
          // 1  refinance(previousmtgamount)
               var reficalc=0
               reficalc = refi(form.previousmtgamount.value)

          // 2  - reissue(previousmtgamount)
               reficalc = reficalc - reissue(form.previousmtgamount.value)

          // 3 + reissue(mtg)
               form.refinance.value = reficalc + reissue(mtg)
     }

     // C - refinance up to the old mtg, then reissue to the previous sale price, then basic to mtg
     else {
          // 1  refinance(previousmtgamount)
               var reficalc=0
               reficalc = refi(form.previousmtgamount.value)

          // 2  - reissue(previousmtgamount)
               reficalc = reficalc - reissue(form.previousmtgamount.value)

          // 3 + reissue(previoussaleprice)
               reficalc = reficalc + reissue(form.previoussaleprice.value)

          // 4 - basic(previoussaleprice)
               reficalc = reficalc - basic(form.previoussaleprice.value)

          // 5 + basic(saleprice)
               form.refinance.value = reficalc + basic(mtg)
     }

// calculate construction
     form.construction.value=construction(tiamt)


// test minimums for all 4 rates
// basic
     if (form.basic.value > 200) {
          form.basic.value=roundit(form.basic.value);
          }
     else {
          form.basic.value = "200.00"
          form.basic.value=roundit(form.basic.value)
     }
// reissue
     if (form.reissue.value > 200) {
          form.reissue.value=roundit(form.reissue.value);
          }
     else {
          form.reissue.value = "200.00"
          form.reissue.value=roundit(form.reissue.value)
     }
// refinance
     if (form.refinance.value > 200) {
     form.refinance.value=roundit(form.refinance.value);
          }
     else {
          form.refinance.value = "200.00"
          form.refinance.value=roundit(form.refinance.value)
     }
// construction
     if (form.construction.value > 200) {
     form.construction.value=roundit(form.construction.value);
          }
     else {
          form.construction.value = "200.00"
          form.construction.value=roundit(form.construction.value)
     }

// end of overall calculation

     // form.basic.value=roundit(form.basic.value);
     // form.reissue.value=roundit(form.reissue.value);
     // form.construction.value=roundit(form.construction.value);
     // form.refinance.value=roundit(form.refinance.value);

     return;
}

function basic(amt)
{
     // if amount <= 100,000 - basic = 5.25/1000
     if (amt <= 100000) {
          amt1=thousands(amt, 0);
          basicrate = (amt1 * 5.25);
     }

     // if amount <= 500,000 - basic = 525 + 4.00 /1000 over 100,000
     else if (amt <= 500000) {
          amt1=thousands(amt, 100000);
          basicrate = (525 + (amt1 * 4.00));
     }

     // if amount <= 2,000,000 - basic = 2,125 + 2.75 /1000 over 500,000
     else if (amt <= 2000000) {
          amt1=thousands(amt, 500000);
          basicrate = (2125 + (amt1 * 2.75));
     }

     // if amount > 2,000,000 - basic = 6,250 + 2.25 /1000 over 2,000,000
     else if (amt > 2000000) {
          amt1=thousands(amt, 2000000);
          basicrate = (6250 + (amt1 * 2.25));
     }

     // round - I need to remember rounding info
     return(basicrate);
}

function reissue(amt)
{
     // if amount <= 100,000 - reissue = 4.25/1000
     if (amt <= 100000) {
          amt1=thousands(amt, 0);
          reissuerate = (amt1 * 4.25);
     }

     // if amount <= 500,000 - reissue = 425 + 3.25 /1000 over 100,000
     else if (amt <= 500000) {
          amt1=thousands(amt, 100000);
          reissuerate = (425 + (amt1 * 3.25));
     }

     // if amount <= 2,000,000 - reissue = 1,725 + 2.25 /1000 over 500,000
     else if (amt <= 2000000) {
          amt1=thousands(amt, 500000);
          reissuerate = (1725 + (amt1 * 2.25));
     }

     // if amount > 2,000,000 - reissue = 5, 100 + 2.00 /1000 over 2,000,000
     else if (amt > 2000000) {
          amt1=thousands(amt, 2000000);
          reissuerate = (5100 + (amt1 * 2.00));
     }

     // round - I need to remember rounding info
     return(reissuerate);
}

function refi(amt)
{
     // if amount <= 100,000 - reissue = 2.50/1000
     if (amt <= 100000) {
          amt1=thousands(amt, 0);
          refirate = (amt1 * 2.50);
     }

     // if amount <= 500,000 - reissue = 250 + 2.25 /1000 over 100,000
     else if (amt <= 500000) {
          amt1=thousands(amt, 100000);
          refirate = (250 + (amt1 * 2.25));
     }

     // if amount <= 2,000,000 - reissue = 1,150 + 2.00 /1000 over 500,000
     else if (amt <= 2000000) {
          amt1=thousands(amt, 500000);
          refirate = (1150 + (amt1 * 2.00));
     }

     // if amount > 2,000,000 - reissue = 4,150 + 1.50 /1000 over 2,000,000
     else if (amt > 2000000) {
          amt1=thousands(amt, 2000000);
          refirate = (4150 + (amt1 * 1.50));
     }

     // round - I need to remember rounding info
     return(refirate);
}

function construction(amt)
{
     // if amount <= 100,000 - basic = 1.00/1000
     if (amt <= 100000) {
          amt1=thousands(amt, 0);
          basicrate = (95 + (amt1 * 1.00));
     }

     // if amount <= 500,000 - basic = 195 + 1.00 /1000 over 100,000
     else if (amt <= 500000) {
          amt1=thousands(amt, 100000);
          basicrate = (195 + (amt1 * 1.00));
     }

     // if amount <= 2,000,000 - basic = 595 + 1.00 /1000 over 500,000
     else if (amt <= 2000000) {
          amt1=thousands(amt, 500000);
          basicrate = (595 + (amt1 * 1.00));
     }

     // if amount > 2,000,000 - basic = 2, 095 + 1.00 /1000 over 2,000,000
     else if (amt > 2000000) {
          amt1=thousands(amt, 2000000);
          basicrate = (2095 + (amt1 * 1.00));
     }

     // round - I need to remember rounding info
     return(basicrate);
}

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.saleprice.value = "";
     form.mtgamount.value = "";
     form.previoussaleprice.value = "";
     form.previousmtgamount.value = "";

     form.basic.value = "";
     form.reissue.value = "";
     form.refinance.value = "";
     form.construction.value = "";

     form.basicsearch.value = "";
     form.reissuesearch.value = "";
     form.refinancesearch.value = "";
     form.constructionsearch.value = "";
}

<!-- Deactivate Cloaking Device -->
