/****************************************************************************************
Copyright @ 2004-2006 Image Unlimited Inc. All rights reserved.
Authorship: Daniel J Burke

This License is exclusively issued to : 


This License is exclusively issued to : Worldcom Title Corp as of April 11, 2004 for the sole use of this product for the website worldcomtitlecorp.com.  


Except as otherwise expressly permitted by Image Unlimited Inc, no one is allowed to 
reproduce or copy any part of this NY Title Rate Calculator ; modify or create any 
derivative works of NY Title Rate Calculator, including translation or localization; 
redistribute, encumber, sell, rent, lease, or sublicense NY Title Rate Calculator; 
remove or alter any trademark, logo, copyright or other proprietary notices, legends,
symbols or labels in NY Title Rate Calculator; or copy the printed materials accompanying 
NY Title Rate Calculator. Any changes to, modifications to, or derivative works of this
NY Title Rate Calculator shall become the exclusive property of Image Unlimited Inc.
*****************************************************************************************/


/****************************
 COMPANY NAME					
****************************/

// company name is now defined in caller
if(!company_name){company_name='';}


/****************************
 GLOBALS					
****************************/
var Zone2 = ['Albany', 'Bronx', 'Columbia', 'Dutchess', 'Greene', 'Kings', 'MountVernon', 'Nassau', 'NewYork', 'Orange', 'Putnam', 'Queens', 'Rensselaer', 'Richmond', 'Rockland', 'Suffolk', 'Sullivan', 'Ulster','Westchester',"Yonkers"]
//var disclaimer = 'All&nbsp;information&nbsp;is&nbsp;believed&nbsp;to&nbsp;be&nbsp;accurate&nbsp;but&nbsp;not&nbsp;guaranteed.';
var disclaimer = '';
var MorTax_Reduction = '';
if(typeof(reduction_notice) != 'undefined'){MorTax_Reduction = reduction_notice;}
var borrower_tax_string = '';
var borrower_tax = " (borrower's portion)";
var no_discount_refi_rate_diff=0; // flat rate difference
var no_discount_refi_rate=0; // flat rate
if(typeof(header_width) == 'undefined'){header_width='500'} // default size of calculator results
if(typeof(use_year) == 'undefined'){use_year = 0; }// toggles year calculation in mortgage

reduce_owner=1;

function NoSource(){
	document.onmousedown=IE;
	document.onmouseup=NS;
	//document.oncontextmenu=new Function("return false");
}
function IE(e){
	if(navigator.appName == "Microsoft Internet Explorer" && (event.button == "2" || event.button == "3")){
		return false;
	}
}
function NS(e){
	if(document.layers || (document.getElementById && !document.all)){
		if(e.which == "2" || e.which == "3"){
			return false;
		}
	}
}
NoSource();
function CheckValue(el){
	var form = el.form;
	if(el.selectedIndex > -1) {
		var prop_type = el.options[el.selectedIndex].value;
		//if(prop_type != "1"){form.PMortgage.value = '';}
	}
}
function CalcTax(form){
	var type = 'owner';
	var sale = RevertAmount(form.Pamount.value);
	var mtg = RevertAmount(form.Mamount.value);
	var prop_type = 'NULL';
	var county = '';
	var previousmtg = '';
	var consolidation = '';
	var year = '';
	var previousmtg = '';
	var zone=1;
	InitCalc();   
	if(form.calculate[0].checked){ 
		if(sale == 0 && mtg == 0 ){
			alert("You must enter either a Purchase or a Mortgage amount!\n");
			return(false);         
		}
		if(form.county1.selectedIndex > -1) {
			county = form.county1.options[form.county1.selectedIndex].value;
         if(county == 'Westchester') {
            if(form.Westchester1.selectedIndex > -1) {
               county=form.Westchester1.options[form.Westchester1.selectedIndex].value;
            }
         }
			for(i=0;i<Zone2.length;i++){
				if(county == Zone2[i]){
					zone = 2;
					break;
				}
			}
		}else{
			alert("You must enter a County");
			return(false);
		}   
		if(form.PropertyType1.selectedIndex > -1) {
			prop_type = form.PropertyType1.options[form.PropertyType1.selectedIndex].value;
		}
	}else if(form.calculate[1].checked){
		if(form.PropertyType2.selectedIndex > -1) {
			prop_type = form.PropertyType2.options[form.PropertyType2.selectedIndex].value;
			//if(prop_type != "1"){form.PMortgage.value = '';}
		}
		mtg = RevertAmount(form.NMortgage.value);
		previousmtg = RevertAmount(form.PMortgage.value);                       
		if(mtg == "" || mtg == 0 )  {
			alert("You must enter New Mortgage ");
			return(false);
		}
      if(use_year){
   		if(previousmtg != "" && previousmtg != 0){
   			if(form.year.value == "")  {
   				alert("You must enter Year of your Current Mortgage");
   				return(false);
   			}
   		}
      }
		if(form.county2.selectedIndex > -1) {
			county = form.county2.options[form.county2.selectedIndex].value;
         if(county == 'Westchester') {
            if(form.Westchester2.selectedIndex > -1) {
               county=form.Westchester2.options[form.Westchester2.selectedIndex].value;
            }
         }
			for(i=0;i<Zone2.length;i++){
				if(county == Zone2[i]){
					zone = 2;
					break;
				}
			}
		} else{
			alert("You must enter a County");
			return(false);
		}

		type = 'loan';
		sale = '';
      if(use_year){
         year = form.year.value; 
      }
	}
	CalcNYRates(form,type,zone,sale,mtg,previousmtg,year,consolidation,county,prop_type); // Perform calculations and write results
	return(false);
}
function CalcNYRates(form,type,zone,sale,mtg,previousmtg,year,consolidation,county,prop_type){
	var owner = '';
	var mortgage = '';
	var si = '';
	var refinance = '';
	var ManTax = '';
	var NYSTtax = '';
	var NYCTtax = ''; 
	var WCHTtax = '';
	var MorTax = ''; 
	var BankMorTax = '';
	var total = 0;
	var b_total = 0;
	var b_si_total = 0;
	var s_total = 0;
   var sim_savings=0;

	if(type == 'loan'){

		mortgage = NYRates(mtg,zone,'loan');     
		refinance = NYRefinance(mtg,previousmtg,mortgage,zone,year,consolidation,prop_type);

		MorTax = MortageTax(mtg,county,prop_type);
		if(prop_type && (prop_type == '1' || prop_type == 'NULL')){
			borrower_tax_string = borrower_tax;
			BankMorTax = Convert(Bankers_Rounding(mtg * 0.0025),1);
		} else{
			borrower_tax_string = '';
		}
		if(consolidation && consolidation ==1){
			refinance = Consolidation(mtg, mortgage);
		}
		refinance = Bankers_Rounding(refinance);
		if(MorTax && MorTax != ''){
			MorTax = Bankers_Rounding(MorTax);
		}
		var tMorTax = MorTax;
		if(!tMorTax || tMorTax == ''){
			tMorTax = 0;
		}

		if(prop_type && prop_type == 'bldgloan'){
			refinance = refinance * 1; 
		}

		total = refinance/1 + tMorTax/1;
		total = Convert(total,1);
		if(mtg && mtg != ''){
			mtg = Convert(mtg,1);
		}
		if(previousmtg && previousmtg != ''){
			previousmtg = Convert(previousmtg,1);
		}

		if(refinance && refinance != ''){
         // min premiums
         if(zone == 1){
            if(refinance < 299){refinance = 299;}
         }else{
            if(refinance < 344){refinance = 344;}
         }
         refinance = Convert(refinance,1);
      }

		if(MorTax && MorTax != ''){
			MorTax = Convert(MorTax,1);
		}
      if(no_discount_refi_rate && no_discount_refi_rate != 0){
         no_discount_refi_rate = Convert(Bankers_Rounding(no_discount_refi_rate),1);
      }
      if(no_discount_refi_rate_diff && no_discount_refi_rate_diff != 0){
         no_discount_refi_rate_diff = Convert(Bankers_Rounding(no_discount_refi_rate_diff),1);
      }

		WriteRefinance(county,mtg,previousmtg,refinance,MorTax,BankMorTax,total);
	} else{
      if(sale >= 1000000){
         // why this isn't done in the rate percentages?  dunno
         reduce_owner=false;

      }
		owner=NYRates(sale,zone,'owner');
      bulk=false;
      if(form.bulkfee && form.bulkfee.checked){
         bulk=true;
      }
      if(bulk){
        // bulk rate
        owner=BulkRateDiscount(1,sale,mtg,owner,mortgage,zone);
      }        
		if(mtg != 0){
         mortgage = NYRates(mtg,zone,'loan');
         if(bulk && sale == 0){
            // bulk rate
            mortgage =BulkRateDiscount(2,sale,mtg,owner,mortgage,zone);
         }			
         if(sale != 0){
            // simultaneous issue            
            if(bulk){
               // bulk rate
               mortgage =BulkRateDiscount(4,sale,mtg,owner,mortgage,zone);
            }else{
               sim_savings=NYRates(mtg,zone,'loan');;
               mortgage=NYSimultaneousIssue(sale,mtg,owner,mortgage,zone);
            }				
			}
			MorTax = MortageTax(mtg,county,prop_type); 
			if(prop_type && (prop_type == '1' || prop_type == 'NULL')){
				borrower_tax_string = borrower_tax;
				BankMorTax = Convert(Bankers_Rounding(mtg * 0.0025),1);
			} else{
				borrower_tax_string = '';
			}       
		}

		if(prop_type && prop_type != 'comm'){
         // mansion tax
			ManTax = NYMansionTax(sale);
		}

      // transfer taxes
		NYSTtax = NYSTransferTax(sale);
		var Buroughs = ["Bronx", "Kings","NewYork","Queens","Richmond"]; 
		var tCounty = null;
		for(i=0;i<Buroughs.length;i++){
			if(county.toLowerCase() == Buroughs[i].toLowerCase()){
				tCounty = 1;
				break;
			}
		}           
		if(tCounty){
			NYCTtax = NYCTransferTax(sale,prop_type);
		}
		var Westchester = ["MountVernon","Yonkers"];
		var wCounty = null;
		for(i=0;i<Westchester.length;i++){
			if(county.toLowerCase() == Westchester[i].toLowerCase()){
				wCounty = 1;
				break;
			}
		}
		if(wCounty){
			WCHTtax = WCHTransferTax(sale,county);
		}

		if(owner && owner != ''){
         // min premiums
         if(zone == 1){
            if(reduce_owner){
               //6/15/2006
               if(owner < 303){owner = 303;}

            }else{
               if(owner < 356){owner = 356;}
            }
         }else{
            if(reduce_owner){
               //6/15/2006
               if(owner < 342){owner = 342;}
            }else{
               if(owner < 402){owner = 402;}
            }
         }         
       }
      if(mortgage && mortgage != ''){
         // min premiums (but not simultaneous issue)
         if(!sale || sale == 0){
            if(zone == 1){
               if(mortgage < 299){mortgage = 299;}
            }else{
               if(mortgage < 344){mortgage = 344;}
            }
         }
      }
		owner = Bankers_Rounding(owner);
		mortgage = Bankers_Rounding(mortgage);
      if(owner && owner != ''){owner = Convert(owner,1);}
      if(mortgage && mortgage != ''){mortgage = Convert(mortgage,1);}

      if(sim_savings > 0){
        sim_savings = Bankers_Rounding(sim_savings);
        sim_savings = sim_savings - mortgage;
      }
		si = Bankers_Rounding(si);

		if(MorTax && MorTax != ''){
			MorTax = Bankers_Rounding(MorTax);
		}
		if(ManTax && ManTax != ''){
			ManTax = Bankers_Rounding(ManTax);
		}
		if(NYSTtax && NYSTtax != ''){
			NYSTtax = Bankers_Rounding(NYSTtax);
		}
		if(NYCTtax && NYCTtax != ''){
			NYCTtax = Bankers_Rounding(NYCTtax);
		}
		if(WCHTtax && WCHTtax != ''){
			WCHTtax = Bankers_Rounding(WCHTtax);
		}

		var tMorTax = MorTax;
		if(!tMorTax || tMorTax == ''){
			tMorTax = 0;
		}
		var tManTax = ManTax;
		if(!tManTax || tManTax == ''){
			tManTax = 0;
		}
		var tNYSTtax = NYSTtax;
		if(!tNYSTtax || tNYSTtax == ''){
			tNYSTtax = 0;
		}
		var tNYCTtax = NYCTtax;
		if(!tNYCTtax || tNYCTtax == ''){
			tNYCTtax = 0;
		}
		var tWCHTtax = WCHTtax;
		if(!tWCHTtax || tWCHTtax == ''){
			tWCHTtax = 0;
		}

		var b_sub_total = tMorTax/1 + tManTax/1;    
		s_total = Convert(tNYSTtax/1 +  tNYCTtax/1 + tWCHTtax, 1);  

		b_si_total = si/1 + b_sub_total;
		b_si_total = Convert(b_si_total,1);
		b_total = owner/1 + mortgage/1 + b_sub_total;
		b_total = Convert(b_total,1);   

		if(sale && sale != ''){
			sale = Convert(sale,1);
		}
		if(mtg && mtg != ''){
			mtg = Convert(mtg,1);
		}
		if(sim_savings && sim_savings != ''){
			sim_savings = Convert(sim_savings,1);
         // add negative prefix
         sim_savings = ' - ' + sim_savings;
		}
		if(si && si != ''){
			si = Convert(si,1);
		}
		if(MorTax && MorTax != ''){
			MorTax = Convert(MorTax,1);
		}
		if(ManTax && ManTax != ''){
			ManTax = Convert(ManTax,1);
		}
		if(NYSTtax && NYSTtax != ''){
			NYSTtax = Convert(NYSTtax,1);
		}
		if(NYCTtax && NYCTtax != ''){
			NYCTtax = Convert(NYCTtax,1);
		}
		if(WCHTtax && WCHTtax != ''){
			WCHTtax = Convert(WCHTtax,1);
		}

		WritePurchase(county,sale,mtg,owner,mortgage,si,ManTax,NYSTtax,NYCTtax,WCHTtax,MorTax,BankMorTax,b_total,b_si_total,s_total,sim_savings);
	}
	return;
}
function NYSimultaneousIssue(sale,mtg,owner,mortgage,zone){

   /*   
       From TIRSA manual
       
       When owners' and leasehold owners' policies covering identical property are issued simultaneously:
       
       the rate for the owner's policy shall be at the applicable owner's rate.
        
       The rate for the leasehold owner's policy shall be thirty percent (30%) of the owner's rate 
       up to the amount of the owner's policy. 
       
       The rate on the amount of the leasehold owner's policy exceeding the amount of the owner's policy shall 
       be calculated at the full owner's rate.
       
       sale = sale amount
       mtg  = mortgage amount
       owner = purchase rate
       mortgage = mortgage rate  
       
   */

	var si=0;
	if(sale >= mtg) {
		si = (0.3 * mortgage);
	} else{
		mtgpolicy = NYRates(sale,zone,'loan');
		mtgpolicyover = mortgage - mtgpolicy; 
		si=(0.3 * mtgpolicy) + mtgpolicyover;
	}
	return si;
}
function BulkRateDiscount(section,sale,mtg,owner,mortgage,zone){
   /*
      sale = purchase amount
      mtg = mortgage amount
      owner = purchase rate
      mortgage = mortgage rate
      
   */
   /*
      section 20 of the TIRSA Manual
      
      The rates set forth herein shall apply only to the initial sale, loan, or lease 
      of any such condominium unit or subdivision lot following the creation of the condominium 
      or residential subdivision.
            
      1.
      Whenever application is made for an individual owner's, 
      leasehold and/or loan policy on condominium units or residential subdivision lots, 
      either of which is shown on a condominium plan or residential subdivision map of at least 10 units, 
      the following rate may be applied if the base fee title or a blanket mortgage covering the 
      condominium or residential subdivision was previously insured.
      
      1. Owner's or leasehold owner insurance: Charge seventy percent (70%) of the applicable owner's rate.
      
      2. Loan insurance: Charge seventy percent (70%) of the applicable loan rate.

      3. Simultaneous issue of fee owner's (or leasehold owner's) and loan policies (Covering Identical Premises):
      Charge the rate set forth in paragraph (1) above for the owner's (or leasehold owner's) policy.
      For the amount of the loan policy up to the amount of the owner's policy, 
         - charge thirty percent (30%) of the rate set forth in (2) above. 
      For the amount of the loan policy in excess of the amount of the owner's policy, 
         - charge the rate set forth in (2) above.
         
      4. Simultaneous issue of owner's and leasehold owner's policies (Covering Identical Premises): 
      Charge the rate set forth in paragraph (1) above for the owner's or leasehold owner's policy. 
      For the amount of the leasehold owner's policy up to the amount of the owner's policy, 
         - charge twenty-one percent (21%) of the owner's rate.
      For the amount of the leasehold policy in excess of the amount of the owner's policy, 
         - charge seventy percent (70%) of the owner's rate.
            
   */

   if(section <= 2){
      // rate for owner rate & mortrgage rate
      var rate=owner; // section 1
      if(section == 2){
         rate = mortgage;// section 2      
      }
      var amt = 0;
      amt = Bankers_Rounding(rate) * 0.7;
      return amt;
   }else{
      var discount = 0.3; // section 3
      if(section == 4){
         discount = 0.21; // section 4
      }
      // simultaneous issue section 3 & 4
      var si=0;
      if(sale >= mtg) { 
      //if(owner >= mtgpolicy) {
         si = (discount * mortgage);
      } else{
         mtgpolicy = NYRates(sale,zone,'loan');
         mtgpolicyover = mortgage - mtgpolicy;
         mtgpolicyover = mtgpolicyover * 0.7; 
         si=(discount * mtgpolicy) + mtgpolicyover;
      }
      return si;
   }
}
function NYRefinance(mtg,previousmtg,mortgage,zone,year,consolidation,prop_type){
  /*
   - If the loan is within the last ten years only
   - A - < $475,000 (& within ten years of a previously insured Mortgage ) 
       = 50% of Mortgage rate up to previous mtg + 100% of Mortgage rate over previous mtg
   - B - > $250,000 
      = 70% of Mortgage rate up to previous mtg + 100% of Mortgage rate over previous mtg
  */   
   var refinance=0;
   var discount=0;
   var cutoff = 475000.00; // old cutoff = 250000.00;
   
    discount=1;
   // if within the last 10 years
   if(use_year){  
      if(year != ''){
         var current = new Date();
         var current_year = current.getFullYear()/1;
         year = new String (year);
         year = year.replace(/[^0-9.]/g,"");
         if(year != '' && (year/1 >= 1990)){
            year = year/1; 
            if(current_year - year <= 10){
               discount=1;
            }
         }
      }
   }
         
   // get flat rate
   no_discount_refi_rate = NYRates(mtg,zone,'loan',prop_type);

   if(discount == 1){
   	if(mtg <= cutoff){
         // if less than or equal to cut off
   		if(previousmtg >= mtg) {
   			refinance =  (0.5 * mortgage);
   		} else{
   			if(previousmtg > 0){
   				mtgpolicy = NYRates(previousmtg,zone,'loan',prop_type);
   				mtgpolicyover = mortgage - mtgpolicy; 
   				refinance = (0.5 * mtgpolicy) + mtgpolicyover;
   			} else{
   				refinance = no_discount_refi_rate;
   			}
   		}
   	}else{
         // if greater than cut off
   		if(previousmtg >= mtg) {
   			refinance =  (0.7 * mortgage);
   		} else{
   			if(previousmtg > 0){
   				mtgpolicy = NYRates(previousmtg,zone,'loan',prop_type);
   				mtgpolicyover = mortgage - mtgpolicy; // delta
   				refinance = (0.7 * mtgpolicy) + mtgpolicyover;
   			} else{
   				refinance = no_discount_refi_rate;
   			}
   		}
   	}

      if(no_discount_refi_rate && refinance){
         // get diff for later viewing 
         no_discount_refi_rate_diff = no_discount_refi_rate - refinance;
   
      }else{
         no_discount_refi_rate=0;
      }
   
   }else{
      refinance = no_discount_refi_rate;
   }

	return refinance;
}
function NYRates(amt,zone,type,prop_type){
	var cap = [35000,50000,100000,500000,1000000,5000000,10000000,15000000];

   var z1_owner = [0,7.92,4.94,3.98,3.56,3.25,2.96,2.76,2.48];
   if(reduce_owner){
      //6/15/2006
      // 6.732, 4.199, 3.383, 3.026
      z1_owner = [0,6.73,4.20,3.38,3.03,3.25,2.96,2.76,2.48];
   }

	var z1_loan = [0,6.61,4.10,3.31,2.96,2.71,2.47,2.31,2.07];

	var z2_owner = [0,6.67,5.43,4.36,3.98,3.66,3.25,3.07,2.76];
   if(reduce_owner){
      //6/15/2006
      // 5.6695, 4.6155, 3.706, 3.383
      z2_owner = [0,5.67,4.62,3.71,3.38,3.66,3.25,3.07,2.76];

   }
	var z2_loan = [0,5.55,4.54,3.64,3.31,3.05,2.71,2.55,2.31];


	var indice = 8;
	var rate_amt = 0; 
	var base = 356;
   if(reduce_owner){
      base = 303;
      // base = 302.60;
   }
	var rates = z1_owner;


    if(type == 'loan' && prop_type == 'bldgloan'){
       type = 'owner';
    }

	if(type == 'owner'){
		if(zone == 2){
			base = 402;         
         if(reduce_owner){
            base = 342;
            // base = 341.70;

         }
			rates = z2_owner;
		}
	} else{
		if(zone == 1){
			base = 299;
			rates = z1_loan;
		} else{
			base = 344;
			rates = z2_loan;
		}   
	}
	for(i=0; i < 8; i++){
		if(amt <= cap[i]) {
			indice = i;
			break
		}
	}
	if(indice != 0){
		base_rate = base; 
		for(j=0; j<cap.length; j++){
			if(amt <= cap[j]){
				break;
			}
			if(j == 0){
				continue;
			}
			var temp_amt = Adjusted(cap[j], cap[j-1]);
			base_rate = (base_rate + (temp_amt * rates[j]));
		}
		var index = 7;
		if(indice < 8){
			index = indice - 1;
		}
		var adj_amt=Adjusted(amt, cap[index]);
		rate_amt = (base_rate + (adj_amt * rates[indice]));
	} else{
		rate_amt = base;
	}
	return rate_amt;
}
function NYMansionTax(amt){
	var tax = null;
	if(amt && amt > 999999){
		tax = amt * .01;
	}
	return tax;
}
function NYSTransferTax(amt){
	var tax = null;
	if(amt){
		tax = amt * .004;
	}
	return tax;
}
function WCHTransferTax(amt,county){
	var tax = null;
	if(county == 'MountVernon'){
	  //Mount Vernon is 1% after a $100,000 exclusion
		if(amt > 100000){
			tax = amt * 0.01;
		}
	} else{
	  // Yonkers is 1.5%
		if(amt > 25000){
			tax = amt * 0.015;
		}
	}
	return tax;
}
function NYCTransferTax(amt,prop_type){
	var tax = null;
	if(!prop_type || prop_type == '' || prop_type == 'NULL'){
		prop_type = '1';
	}
	if(amt){
		if(amt >= 500000){
			if(prop_type == 'comm' || prop_type == 'vacant'){
				tax = amt * 0.02625;
			} else if(prop_type == '1'){
				tax = amt * 0.01425;
			} else{
				tax = amt * 0.01;
			}
		} else{
			if(prop_type == 'vacant'){
				tax = amt * 0.01425;
			} else{
				tax = amt * 0.01;
			}
		}
		return tax;
	}
	return tax;
}
function MortageTax(mtg,county,prop_type){
	var Buroughs = ["Bronx", "Kings","NewYork","Queens","Richmond"]; 
	var rate = 0;
	var tax = 0;
	if(!prop_type || prop_type == '' || prop_type == 'NULL'){
		prop_type = '1';
	}

	for(i=0;i<Buroughs.length;i++){
		if(county.toLowerCase() == Buroughs[i].toLowerCase()){
			county = 'NYC';
			break;
		}
	}        
	switch(county.toLowerCase()){
	case 'nyc':
		if(mtg >= 500000){
			if(prop_type == '1'){
				rate = 0.02175;
			} else{
					// commercial/vacant
				rate = 0.028;
			}
		} else{
			rate = 0.0205;
		}
		break;  
	case 'suffolk': 
	case 'nassau':
		rate = 0.0105;  
		break;
	case 'westchester':
	case 'rockland':
		rate = 0.013;
		break;              
	case 'yonkers':
		rate = 0.018;
		break;                                  
	default:
		 // only if Zone 2 
		for(i=0;i<Zone2.length;i++){
			if(county == Zone2[i].toLowerCase()){
				alert("Please call " + company_name + " for a Mortgage Tax qoute  for " + county)
				break;
			}
		} 
		break;
	}
	if(prop_type == '1'){
		rate -= 0.0025;
	}
	tax = mtg * rate;
	if(prop_type == '1'){
		tax = tax - 30;
	}
	if(tax && tax < 1){
		tax = null;
	}
	return tax;
}
function Consolidation(mtg, mortgage){
	var rate = 0;
	if(mtg >= 250000){
		rate = mortgage * 0.7;
	} else{
		rate = mortgage * 0.5;
	}return rate;
}
function Adjusted(amount, cap) {
   var diff = (amount - cap)/1000;
   if(diff > parseInt(diff)){
      diff = parseInt(diff) + 1;
   }
   return diff;
}
function Bankers_Rounding(num){ 
   // as per TIRSA "Forty-nine cents or less shall be rounded down and fifty cents and above shall be rounded up" and this seems to do the job
   if(num){
      num = Math.round(num);
      return num;
   } 
   // not sure how it handles NaN  so return 0
   return 0;
}
function ConvertAmount(el){
	amount = Convert(el.value,null);
	el.value = amount;
}
function Convert(val,add_decimal){
	amount = new String (val);
	amount = amount.replace(/[^0-9.]/g,"");
	if(add_decimal){
		if(amount.search(/\./) == -1){
			amount=amount+'.00';
		}
	}
	if(add_decimal){
		if(amount.search(/\.$/) != -1){
			amount=amount+'00';
		}
	} else{
		if(amount.search(/\.$/) != -1){
			amount = amount.replace(/\.$/,"");
		}
	}
	if(amount.search(/(\.\d)$/) != -1){
		amount=amount+'0';
	}
	decimal='';
	result = amount.match(/(\.\d\d+)$/);
	if(result  != null){
		decimal=result[0];
		decimal = decimal.slice(0,3);
		amount = amount.replace(/(\.\d+)$/,"");
	}
	amount = amount.replace(/\./g,"");
	if(amount == ''){
		amount = '0';
	}
	amount = amount + decimal;  
	amount = amount.replace(/([0-9])([0-9]{3}$)/,"$1,$2"); 
	amount = amount.replace(/([0-9])([0-9]{3}\.)/,"$1,$2");
	amount = amount.replace(/([0-9])([0-9]{3},)/g,"$1,$2");
	amount = amount.replace(/([0-9])([0-9]{3},)/g,"$1,$2");
	amount = amount.replace(/([0-9])([0-9]{3},)/g,"$1,$2");
	amount = amount.replace(/^0/,"");
	if(amount.search(/^\.\d\d$/) != -1){
		amount='0'+amount;
	}
	if(amount == ''){
		return amount;
	}
	return '$'+amount;
}
function RevertAmount(amount){
	amount = new String (amount);
	amount = amount.replace(/[^0-9.]/g,"")
			 amount = amount/1;
	return amount;
}
function checkcalc(){
	if(document.Form1.calculate[1].checked ==true){
		EnableRefinance();
	}
}
function EnableRefinance(){
	document.Form1.NMortgage.disabled=false;
	document.Form1.PMortgage.disabled =false;
	document.Form1.county2.disabled=false;
	document.Form1.PropertyType2.disabled = false;
   document.Form1.Westchester2.disabled = false;
   if(use_year){document.Form1.year.disabled = false;}
	document.Form1.Pamount.disabled =true;
	document.Form1.Mamount.disabled = true;
	document.Form1.county1.disabled = true;
	document.Form1.PropertyType1.disabled = true;
   document.Form1.Westchester1.disabled = true;
   document.getElementById("PurchaseBlock").style.display = "none";
   document.getElementById("RefinanceBlock").style.display = "block";
}
function EnablePurchase(){
	document.Form1.NMortgage.disabled=true;
	document.Form1.PMortgage.disabled =true;
	document.Form1.county2.disabled=true;
	document.Form1.PropertyType2.disabled = true;
   document.Form1.Westchester2.disabled = true;
	if(use_year){document.Form1.year.disabled = true;}
	document.Form1.Pamount.disabled =false;
	document.Form1.Mamount.disabled = false;
	document.Form1.county1.disabled = false;
	document.Form1.PropertyType1.disabled =false;
   document.Form1.Westchester1.disabled = false;
   document.getElementById("PurchaseBlock").style.display = "block";
   document.getElementById("RefinanceBlock").style.display = "none";
}
function block_on_off(el,state) {
	if(state == 'on') {
		document.getElementById(el).style.display = "block";
	} else{
		document.getElementById(el).style.display = "none";
	}
}
function select_open_close(el,target,type){
   var block = target+type;
   document.getElementById(block).style.display = "none";
   if(el.selectedIndex && el.options[el.selectedIndex].value == target){
         document.getElementById(block).style.display = "block";
      }
}
function initWestchester(){
  var el = document.Form1.county1;
  select_open_close(el,'Westchester',1);
  el = document.Form1.county2;
  select_open_close(el,'Westchester',2);
}
function InitCalc(){
	document.getElementById('pur').innerHTML = '';
	document.getElementById('ref').innerHTML = '';
	block_on_off('Purchase','off');
	block_on_off('Refinance','off');
	block_on_off('CalcBlock','on');
}
function  WritePurchase(county,amt,mortgage,owner,mtg,si,ManTax,NYSTtax,NYCTtax,WCHTtax,MorTax,BankMorTax,b_total,b_si_total,s_total,sim_savings){
	document.getElementById('pur').innerHTML = '';
	document.getElementById('ref').innerHTML = '';
	block_on_off('Refinance','off');
	block_on_off('Purchase','on');
	block_on_off('CalcBlock','off');
	var MorTax_ReductionFlag = null;
	var string = ResultPurchaseBuyerHeader(county,amt,mortgage);
	if(amt != 0){
		string += ResultLabel("Owner's Policy Premium");
		string += ResultValue(owner);
	}
	if(mortgage && mortgage != 0){
		string += ResultLabel('Loan Policy Premium');
		string += ResultValue(mtg);
	}
   /*
	if(sim_savings && sim_savings != 0){
		string += ResultLabel('<B>Amount saved on the Loan Policy Premium</B>');
		string += ResultValue('<B><FONT COLOR="RED">' + sim_savings + '</FONT></B>');
	}
   */
	if(MorTax && MorTax != ''){
		string += ResultLabel('Mortgage Tax' + borrower_tax_string);
		string += ResultValue(MorTax);
		MorTax_ReductionFlag=1; 
	}
	if(BankMorTax && BankMorTax != '' && BankMorTax != '$0.00'){
		string += ResultLabel("Bank's Portion of Mortgage Tax (1/4)");
		string += ResultValue(BankMorTax);  
	}
	if(ManTax && ManTax != ''){
		string += ResultLabel('Mansion Tax');
		string += ResultValue(ManTax);  
	}
	if((NYSTtax && NYSTtax != '') || (NYCTtax && NYCTtax != '')){       
		string += ResultPurchaseSellerHeader();
	}
	if(NYSTtax && NYSTtax != ''){
		string += ResultLabel('NYS Transfer Tax');
		string += ResultValue(NYSTtax); 
	}
	if(NYCTtax && NYCTtax != ''){
		string += ResultLabel('NYC Transfer Tax');
		string += ResultValue(NYCTtax); 
	}
	if(WCHTtax && WCHTtax != ''){
      town='Yonkers';
      if(county.toLowerCase() == 'mountvernon'){
         town='Mount Vernon';
      }
		string += ResultLabel(town + ' Transfer Tax');
		string += ResultValue(WCHTtax); 
	}

	string += ResultFooter(MorTax_ReductionFlag);
	document.getElementById('pur').innerHTML = string;  
}
function WriteRefinance(county,mortgage,pmortgage,refinance,MorTax,BankMorTax,total){
	document.getElementById('pur').innerHTML = '';
	document.getElementById('ref').innerHTML = '';
	block_on_off('Purchase','off');
	block_on_off('Refinance','on');
	block_on_off('CalcBlock','off');
	var MorTax_ReductionFlag = null;
	var string = ResultRefinanceHeader(county,pmortgage,mortgage);
	string += ResultLabel("Loan Policy Premium");
	string += ResultValue(refinance);
   if(no_discount_refi_rate_diff && no_discount_refi_rate){
		string += ResultLabel('<B>Amount saved (rate before discount = ' + no_discount_refi_rate + ')</B>');
		string += ResultValue('<B><FONT COLOR="RED">' + no_discount_refi_rate_diff + '</FONT></B>')
   }
	if(MorTax && MorTax != ''){
		string += ResultLabel('Mortgage Tax' + borrower_tax_string);
		string += ResultValue(MorTax);
		MorTax_ReductionFlag=1; 
	}
	if(BankMorTax && BankMorTax != '' && BankMorTax != '$0.00'){
		string += ResultLabel("Bank's Portion of Mortgage Tax (1/4)");
		string += ResultValue(BankMorTax);  
	}
	string += ResultFooter(MorTax_ReductionFlag);   
	document.getElementById('ref').innerHTML = string;  
}
function BackToCalcButton(){
   var $string = '<input type="button" name="BackToCacluate" value="Back to NY Title Fees Calculator" onClick="InitCalc()">';
   return $string;
}
function BackToCalcLink(){
   return '<a href="#" onClick="InitCalc()";><span class="calctextbold">[Back to NY Title Fees Calculator]</span></a>';
}
function ResultPurchaseBuyerHeader(county,sale,mtg){
	if(sale == 0){
		sale = '$0.00';
	}
	if(mtg == 0){
		mtg = '$0.00';
	}
	var string = BackToCalcButton();
   string += '<BR>';
	string += '<table width='+header_width+' border=0>';
	string += '<tr><td colspan="2" class="results_header" height=25><i>'+disclaimer+'</i><br></td></tr>';
	string += '<tr><td colspan="2" class="results_header">Basic&nbsp;purchase&nbsp;fees&nbsp;for: ' + county + '</td></tr>';
	string += '<tr><td class="results_header">Purchase&nbsp;Amount:</td><td class="results_header">'+sale+'</td></tr>';
	string += '<tr><td class="results_header">Mortgage&nbsp;Amount:</td><td class="results_header">'+mtg+'</td></tr> ';
	string += '<tr colspan="2" height=20><td></td></tr>';
	string += "<tr height=20><td class=results_header>Buyer's Fee:</td></tr>";
	return string;
}
function ResultPurchaseSellerHeader(){
	var string ='<tr><td colspan="2">&nbsp;</td></tr>';
	string += "<tr height=20><td class=results_header>Seller's Fee:</td></tr>";
	return string;
}
function ResultRefinanceHeader(county,pmtg,nmtg){
	if(pmtg == 0){
		pmtg = '$0.00';
	}
	if(nmtg == 0){
		nmtg = '$0.00';
	}
	var string = BackToCalcButton();
   string += '<BR>';
	string +='<table width='+header_width+' border=0>';
	string += '<tr><td colspan="2" class="results_header" height=25><i>'+disclaimer+'</i><br></td></tr>';
	string += '<tr><td colspan="2" class="results_header">Basic&nbsp;refinance&nbsp;fees&nbsp;for: ' + county + '</td></tr>';
	string += '<tr><td class="results_header">New&nbsp;Mortgage:</td><td class="results_header">'+nmtg+'</td></tr>';
	string += '<tr><td class="results_header">Prior&nbsp;Mortgage:</td><td class="results_header">'+pmtg+'</td></tr> ';
	string += '<tr colspan="2" height=20><td>&nbsp;</td></tr>';
	string += "<tr height=20><td class=results_header>Owner's Fee</td></tr>";
	return string;
}
function ResultLabel(label){
	label = new String (label);
	label = label.replace(/\s+/g,"&nbsp;");
	var string = '<tr><td class="results_label" width=85% height=25>'+label+':</td>';
	return string;
}
function ResultValue(val){
	var string = '<td class="results_value">'+val+'</td></tr>';
	return string;
}
function TotalLabel(label){
	label = new String (label);
	label = label.replace(/\s+/g,"&nbsp;");
	var string = '<tr><td class=results_header height=25>'+label+':</td>';
	return string;
}
function TotalValue(val){
	var string = '<td class="results_header">'+val+'</td></tr>';
	return string;
}
function ResultFooter(MorTax_ReductionFlag){
	var string = '';
	string += '</table>';
	if(MorTax_ReductionFlag){
		string += '<BR><BR>'+MorTax_Reduction+'';
	}
	return string;
}


