// http://www.alphaadv.net/vatitle/varatecalc.js
// Virginia Title Insurance Rate Calculator
// Copyright 2008 by John Granger
// Thank you for finding this page interesting enough to view the source code.
// Please respect my copyrights.  Thankx
// Page written April 23, 2008, Last update May 5, 2008

<!-- Changing the lines above or below is an invitation to Adventures in Legal Land -->

function noticetwo(what){var usc17etseq = "True";}

function calcVarates(form)
{
    var amt = form.saleprice.value;
    if (form.mtgamount.value > form.saleprice.value){amt = form.mtgamount.value}

    if (amt > 2000000)
     {
     form.basic.value    = "Talk";
     form.enhanced.value = "to";
     form.si.value       = "your";
     form.reissue.value  = "title";
     form.lender.value   = "company";
     return;
     }

     // calculation goes here - February 2006 Rate Manual
     // Rates:     basic, enhanced, si, reissue, lender
     // Inputs:    saleprice, mtgamount, priorowner 

     var sale  = form.saleprice.value;
     var mtg   = form.mtgamount.value;
     var prior = form.priorowner.value;

     var amt = 0
     var ownerrate = 0
     var lenderrate = 0
		 
     // calculate minimums in functions - Owner's 125, Lender's 100, Enhanced 150

     // caclulate owner rate
     form.basic.value    = owner(sale)
     form.si.value       = (owner(sale) + 100);
     form.enhanced.value = (form.basic.value * 1.2);
     form.reissue.value  = (form.basic.value * .7);

     // calculate si rate
     // If the mortgage amount is greater than the sale price, calc rest at the loan rate
		 // First add in loan amount at new mortgage amount
		 // Then subtract the loan amount at the sale price
		 
     if ((mtg / sale) > 1)
		 {
		 form.si.value = (owner(sale) + 100 + lender(mtg) - lender(sale));
		 }

     // calculate reissue rate
		 // Calculate Reissue at 70% of owner rate and then the rest at owner's
		 // First add in owner amount at the sale price
		 // Then subtract the owner amount at the prior policy amount
		 
     if ((sale / prior) > 1 && prior > 0)
     {
     form.reissue.value = ((owner(prior) * .7) + owner(sale) -  owner(prior)); 
		 }

     // caclulate lender rate
     form.lender.value = lender(mtg)
	 
     // round
     form.basic.value    = roundit(form.basic.value);
     form.reissue.value  = roundit(form.reissue.value);
     form.si.value       = roundit(form.si.value);
     form.enhanced.value = roundit(form.enhanced.value);
     form.lender.value   = roundit(form.lender.value);
     return;
}

function owner(amt) { 
     // calculation - February 2006 Rate Manual

     // if amt <= 100,000
     if (amt <= 100000)
     {
     amt1=thousands(amt, 0);
     ownerrate = (amt1 * 3.90);
     // calculate minimums here - Owner's 125, Lender's 100, Enhanced 150
     if (ownerrate < 125){ownerrate = 125}
     }

     // if amt <= 500,000
     else if (amt <= 500000)
     {
     amt1=thousands(amt, 100000);
     ownerrate = (390 + (amt1 * 3.40));
     }

     // if amt <= 1,000,000
     else if (amt <= 1000000)
     {
     amt1=thousands(amt, 500000);
     ownerrate = (1750 + (amt1 * 3.00));
     }

     // if amt <= 2000,000
     else if (amt <= 2000000)
     {
     amt1=thousands(amt, 1000000);
     ownerrate = (3250 + (amt1 * 2.00));
     }
     return(ownerrate);
}


function lender(amt) {
     // calculation lender

     // if amt <= 100,000
     if (amt <= 100000)
     {
     amtl=thousands(amt, 0);
     lenderrate = (amtl * 2.90);
     // calculate minimums here - Owner's 125, Lender's 100, Enhanced 150
     if (lenderrate < 100 && amt > 0){lenderrate = 100}
     }

     // if amt <= 500,000
     else if (amt <= 500000)
     {
     amtl=thousands(amt, 100000);
     lenderrate = (290 + (amtl * 2.40));
     }

     // if amt <= 1,000,000
     else if (amt <= 1000000)
     {
     amtl=thousands(amt, 500000);
     lenderrate = (1250 + (amtl * 2.00));
     }

     // if amt <= 2000,000
     else if (amt <= 2000000)
     {
     amtl=thousands(amt, 1000000);
     lenderrate = (2250 + (amtl * 1.50));
     }
     return(lenderrate);
}


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 usca17etseq = "True";
     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.prior.value = "";
    form.basic.value = "";
    form.enhanced.value = "";
    form.si.value = "";
    form.reissue.value = "";
    form.lender.value = "";
}


