<!--
		
function formatNumber( expr, decimals )
{
	var str = "" + Math.round( eval(expr) * Math.pow(10,decimals));
	while (str.length <= decimals) { str = "0" + str }
	var decpoint = str.length - decimals;
	var result = str.substring(0,decpoint);
	if (decimals) result += "." + str.substring(decpoint,str.length);
	return result;
}

function roundThousand( num, acc )
{
	var fact = Math.pow(10,acc);
	return Math.ceil( num * fact )/fact;
}

function abs( num )
{
	return((num<0)?num*-1:num);
}

function isNumber( num )
{
	if( !num.match(/^[0-9-'.'-',']*$/) )
	{
		return true;
	}
}

function calc( block )
{
	if( block.txtEstimatedClosingDate.value == '' )
	{
		alert( 'You must enter the estimated closing date first.' );
	}
	else if( block.selCounty.selectedIndex == 0 )
	{
		block.txtNetSellerProceeds.value = 0;
		alert( 'Please select a county.' );
	}
	else
	{
	var closingDate;
	var today;
	var endYear;
	var msPerDay;
	var daysLeft;
	var daysPast;
	
    closingDate = new Date();
    closingDate.setTime(Date.parse(block.txtEstimatedClosingDate.value));
        
    today = new Date();
    endYear = new Date("December 31, 2010"); // Set day and month
    endYear.setYear( closingDate.getYear()); // Set year to this year
    msPerDay = 24 * 60 * 60 * 1000 // Number of milliseconds per day
    daysLeft = (endYear.getTime() - closingDate.getTime()) / msPerDay;
    daysLeft = Math.round(daysLeft);
    daysPast = 365 - daysLeft;
    
    if ( daysPast > 182 )
	{
		// second half closing date
       	block.txtTotalDays.value = daysPast;
    }
	else
	{
		// first half closing date
       	block.txtTotalDays.value =  daysPast + 183;
    }
    
    block.txtAmountPerDay.value = Math.round(100*(eval(block.txtFirstHalf.value) + eval(block.txtSecondHalf.value)) / 365)/100;
      
    block.txtDailyTax.value = eval(eval(block.txtTotalDays.value) * eval(block.txtAmountPerDay.value));
    block.txtDailyTax.value = Math.round(100 * eval(block.txtDailyTax.value)) / 100;
    block.txtLessTax.value = formatNumber(eval(block.txtDailyTax.value),2);

	//Owners Policy
      if (block.txtSellingPrice.value <= 150000)
	  {
         var div = 5.75;
         var base_policy_amount = 0;
         var variable_policy_amount = eval(block.txtSellingPrice.value);
      }
	  else if (block.txtSellingPrice.value <= 250000 && block.txtSellingPrice.value > 150000)
	  {
         var div = 4.5;
         var base_policy_amount = 187.50;
         var variable_policy_amount = eval(block.txtSellingPrice.value);
      }
	  else if (block.txtSellingPrice.value <= 500000 && block.txtSellingPrice.value > 250000)
	  {
         var div = 3.5;
         var base_policy_amount = 437.50;
         var variable_policy_amount = eval(block.txtSellingPrice.value);
      }
	  else if (block.txtSellingPrice.value <= 10000000 && block.txtSellingPrice.value > 500000)
	  {
         var div = 2.75;
         var base_policy_amount = 812.50;
         var variable_policy_amount = eval(block.txtSellingPrice.value);
      }
	  else
	  {
         var div = 2.75;
         var base_policy_amount = 5812.50;
         var variable_policy_amount = eval(block.txtSellingPrice.value);
      }                             

      variable_policy_amount = eval(Math.ceil(eval(variable_policy_amount) /1000));
      block.txtOwnersPolicy.value = Math.round(100*eval(div * variable_policy_amount))/100 + base_policy_amount;
      block.txtOwnersPolicy.value = Math.round(100 * eval(block.txtOwnersPolicy.value)) / 100;
	  
	if (block.txtOwnersPolicy.value < 175)
	{
		block.txtOwnersPolicy.value = 175;
	}
	else
	{
		block.txtOwnersPolicy.value = formatNumber( eval(block.txtOwnersPolicy.value), 2 ); 
	}
	
	
	
	
	
	
	
	
	
	
	
	
	//Loan Policy
	var difference = (eval(block.txtLessNewMortgageAmount.value) - eval(block.txtSellingPrice.value));
	var total_amount = formatNumber((eval(block.txtSellingPrice.value) + eval(difference)), 2 );
	
	//Calculate Starting Level
	if( block.txtSellingPrice.value <= 150000 )
	{
		var starting_level = 1;
	}
	else if( block.txtSellingPrice.value <= 250000 && total_amount > 150000 )
	{
		var starting_level = 2;
	}
	else if( block.txtSellingPrice.value <= 500000 && total_amount > 250000 )
	{
		var starting_level = 3;
	}
	else if( block.txtSellingPrice.value <= 10000000 && total_amount > 500000 )
	{
		var starting_level = 4;
	}
	else
	{
		var starting_level = 5;
	}
	
	//Calculate Ending Level
	if( total_amount <= 150000 )
	{
		var ending_level = 1;
	}
	else if( total_amount <= 250000 && total_amount > 150000 )
	{
		var ending_level = 2;
	}
	else if( total_amount <= 500000 && total_amount > 250000 )
	{
		var ending_level = 3;
	}
	else if( total_amount <= 10000000 && total_amount > 500000 )
	{
		var ending_level = 4;
	}
	else
	{
		var ending_level = 5;
	}
	
	var variable_Loan_Policy = 0;
	var variable_Loan_Policy1 = 0;
	var variable_Loan_Policy2 = 0;
	var variable_Loan_Policy3 = 0;
	var variable_Loan_Policy4 = 0;
	var variable_Loan_Policy5 = 0;
	
	var amount_left = 0;
	var selling_Price = eval(block.txtSellingPrice.value);
	var mortgage_Amount = eval(block.txtLessNewMortgageAmount.value);
	var difference = (eval(block.txtLessNewMortgageAmount.value) - eval(block.txtSellingPrice.value));
	difference = roundThousand( difference, -3 );
	
	
	for( i=starting_level; i<ending_level+1; i++ )
	{
		if( i == 1 )
		{
			//next level 150,000
			if( starting_level == ending_level )
			{
				amount_left = formatNumber( eval( difference ), 2 );
			}
			else
			{
				if( ending_level == 1 )
				{
					amount_left = formatNumber( eval( difference ), 2 );
				}
				else
				{
					amount_left = formatNumber( 150000 - eval( selling_Price ), 2 );
				}
			}
			difference = formatNumber( eval( difference ) - eval( amount_left ), 2 );
			variable_Loan_Policy1 = formatNumber( eval( ( amount_left / 1000.00 ) * 4.00  ), 2 );
		}
		
		if( i == 2 )
		{
			//next level 250,000
			if( starting_level == ending_level )
			{
				amount_left = formatNumber( eval( difference ), 2 );
			}
			else
			{
				if( ending_level == 2 )
				{	
					amount_left = formatNumber( eval( difference ), 2 );
				}
				else if ( ( starting_level == 1 ) && ( ending_level > 2 ) )
				{
					amount_left = 100000;
				}
				else
				{
					amount_left = formatNumber( 250000 - eval( selling_Price ), 2 );
				}
			}
			difference = formatNumber( eval( difference ) - eval( amount_left ), 2 );
			variable_Loan_Policy2 = formatNumber( eval(  ( amount_left / 1000.00 ) * 3.25  ), 2 );
		}
		
		if( i == 3 )
		{
			//next level 500,000
			if( starting_level == ending_level )
			{
				amount_left = formatNumber( eval( difference ), 2 );
			}
			else
			{
				if( ending_level == 3 )
				{
					amount_left = formatNumber( eval( difference ), 2 );
				}
				else if ( ( starting_level < 3 ) && ( ending_level > 3 ) )
				{
					amount_left = 250000;
				}
				else
				{
					amount_left = formatNumber( 500000 - eval( selling_Price ), 2 );
				}
			}
			difference = formatNumber( eval( difference ) - eval( amount_left ), 2 );
			variable_Loan_Policy3 = formatNumber( eval(  ( amount_left / 1000.00 ) * 2.50  ), 2 );
		}
		
		if( i == 4 )
		{
			//next level 10,000,000
			if( starting_level == ending_level )
			{
				amount_left = formatNumber( eval( difference ), 2 );
			}
			else
			{
				if( ending_level == 4 )
				{
					amount_left = formatNumber( eval( difference ), 2 );
				}
				else if ( ( starting_level < 4 ) && ( ending_level > 4 ) )
				{
					amount_left = 9500000;
				}
				else
				{
					amount_left = formatNumber( 10000000 - eval( selling_Price ), 2 );
				}
			}
			difference = formatNumber( eval( difference ) - eval( amount_left ), 2 );
			variable_Loan_Policy4 = formatNumber( eval(  ( amount_left / 1000.00 ) * 2.25  ), 2 );
		}
		
		if( i == 5 )
		{
			//next level 10,000,000 and above
			if( starting_level == ending_level )
			{
				amount_left = formatNumber( eval( difference ), 2 );
			}
			else
			{
				if( ending_level == 5 )
				{
					amount_left = formatNumber( eval( difference ), 2 );
				}
				else
				{
					amount_left = formatNumber( 500000 - eval( selling_Price ), 2 );
				}
			}
			difference = formatNumber( eval( difference ) - eval( amount_left ), 2 );
			variable_Loan_Policy5 = formatNumber( eval(  ( amount_left / 1000.00 ) * 2.00  ), 2 );
		}
		
		variable_Loan_Policy = formatNumber( eval( variable_Loan_Policy1 ) + eval( variable_Loan_Policy2 ) + eval( variable_Loan_Policy3 ) + eval( variable_Loan_Policy4 ) + eval( variable_Loan_Policy5 ), 2 );
	}
	
	
	
	
	if( eval(block.txtLessNewMortgageAmount.value) <= eval(block.txtSellingPrice.value) )
	{
		block.txtLoanPolicy.value = "100.00";
	}
	else
	{
		block.txtLoanPolicy.value = formatNumber( eval( variable_Loan_Policy ) + 100, 2 );
	}
	
	//LENDER FEES
	block.txtOriginationCharge.value = formatNumber(eval(block.txtOriginationCharge.value),2);
	block.txtChargePoints.value = formatNumber(eval(block.txtChargePoints.value),2);
	block.txtAdjustedCharges.value = formatNumber(eval(block.txtOriginationCharge.value) + eval(block.txtChargePoints.value),2);
	block.txtAppraisal.value = formatNumber(eval(block.txtAppraisal.value),2);
	block.txtCredit.value = formatNumber(eval(block.txtCredit.value),2);
	block.txtTaxService.value = formatNumber(eval(block.txtTaxService.value),2);
	block.txtFloodCertification.value = formatNumber(eval(block.txtFloodCertification.value),2);
	block.txtOther1.value = formatNumber(eval(block.txtOther1.value),2);
	block.txtOther2.value = formatNumber(eval(block.txtOther2.value),2);
	block.txtOther3.value = formatNumber(eval(block.txtOther3.value),2);
	  
   	  // calculate escrow closing fee
      var coindex = block.selCounty.selectedIndex
   	  sp = eval(block.txtSellingPrice.value)
    
      // New Close calculations as of 10/19/08
      if (block.txtSellingPrice.value <= 100000)
	  {
         block.txtEscrowClosingFee.value = 355;
      }
	  else if (block.txtSellingPrice.value <= 200000 && block.txtSellingPrice.value > 100000)
	  {
         block.txtEscrowClosingFee.value = 385;
      }
	  else
	  {
         block.txtEscrowClosingFee.value = 395;
      }  
	
	
		// Set escrow closing for certain counties as of 10/05/2009
	if (coindex == 52 || coindex == 67 || coindex == 76 || coindex == 77)     
	{
		block.txtEscrowClosingFee.value = 250;
	}
	
	//FEES
	block.txtEscrowClosingFee.value = formatNumber(eval(block.txtEscrowClosingFee.value),2);
	block.txtTitleCommitment.value = formatNumber(eval(block.txtTitleCommitment.value),2);
	block.txtHandlingProcessing.value = formatNumber(eval(block.txtHandlingProcessing.value),2);
	block.txtTaxSearch.value = formatNumber(eval(block.txtTaxSearch.value),2);
	block.txtWireFee.value = formatNumber(eval(block.txtWireFee.value),2);
	block.txtTitleUpdate.value = formatNumber(eval(block.txtTitleUpdate.value),2);
	block.txtSurvey.value = formatNumber(eval(block.txtSurvey.value),2);
	block.txtTitleBinder.value = formatNumber(eval(block.txtTitleBinder.value),2);
	    
	//ENDORSEMENTS
	block.chkEPA.value = 0;
	block.chkARM.value = 0;
	block.chkPUD.value = 0;
	block.chkCondo.value = 0;
	block.chkComprehensive.value = 0;
	block.chkSurveyCoverage.value = 0;
	block.chkSurveyCoverageOwners.value = 0;
	block.chkClosingProtectionLender.value = 0;
	block.chkClosingProtectionBuyer.value = 0;
	block.txtOther4.value = formatNumber(eval(block.txtOther4.value),2);
	block.txtOther5.value = formatNumber(eval(block.txtOther5.value),2);
	
	if ( block.chkEPA.checked )
	{
			block.chkEPA.value = 75;
	}
	if ( block.chkARM.checked )
	{
			block.chkARM.value = 75;
	}
	if ( block.chkPUD.checked )
	{
			block.chkPUD.value = 50;
	}
	if ( block.chkCondo.checked )
	{
			block.chkCondo.value = 50;
	}
	if ( block.chkComprehensive.checked )
	{
			block.chkComprehensive.value = 150;
	}
	if ( block.chkSurveyCoverage.checked )
	{
			block.chkSurveyCoverage.value = 50;
	}
	if ( block.chkSurveyCoverageOwners.checked )
	{
			block.chkSurveyCoverageOwners.value = 100;
	}
	if ( block.chkClosingProtectionLender.checked )
	{
			block.chkClosingProtectionLender.value = 35;
	}
	if ( block.chkClosingProtectionBuyer.checked )
	{
			block.chkClosingProtectionBuyer.value = 35;
	}
	
	//GOVERNMENT RECORDING CHARGES
	block.chkDeed.value = 0;
	block.chkMortgage.value = 0;
	
	if ( block.chkDeed.checked )
	{
		block.chkDeed.value = 28.50;
	}
	if ( block.chkMortgage.checked )
	{
		block.chkMortgage.value = 220;
	}
	
    block.txtFirstHalf.value = formatNumber(eval(block.txtFirstHalf.value),2);
    block.txtSecondHalf.value = formatNumber(eval(block.txtSecondHalf.value),2);
	
	//TOTALS
	block.txtTotalItemsPayable.value = formatNumber(eval(eval(block.txtAdjustedCharges.value) + eval(block.txtAppraisal.value) + eval(block.txtCredit.value) + eval(block.txtTaxService.value) + eval(block.txtFloodCertification.value) + eval(block.txtOther1.value) + eval(block.txtOther2.value) + eval(block.txtOther3.value)),2);
	block.txtLessTitleCharges.value = formatNumber(eval( eval(block.txtEscrowClosingFee.value) + eval(block.txtTitleCommitment.value) + eval(block.txtHandlingProcessing.value) + eval(block.txtTaxSearch.value) + eval(block.txtWireFee.value) + eval(block.txtTitleUpdate.value) + eval(block.txtSurvey.value) + eval(block.txtTitleBinder.value) + eval(block.txtLoanPolicy.value) +eval(block.chkEPA.value) + eval(block.chkARM.value) + eval(block.chkPUD.value) + eval(block.chkCondo.value) + eval(block.chkComprehensive.value) + eval(block.chkSurveyCoverage.value) + eval(block.chkSurveyCoverageOwners.value) + eval(block.chkClosingProtectionLender.value) + eval(block.chkClosingProtectionBuyer.value) + eval(block.txtOther4.value) + eval(block.txtOther5.value)),2);
	block.txtLessTitleCharges2.value =  formatNumber( eval( eval(block.txtOwnersPolicy.value)),2);
	block.txtLessGovernmentCharges.value = formatNumber(eval(eval(block.chkDeed.value) + eval( block.chkMortgage.value )),2);
	
	//TOTAL REFINANCE PROCEED VALUE
	block.txtNetSellerProceeds.value = formatNumber(eval( eval( block.txtSellingPrice.value ) - eval( block.txtLessNewMortgageAmount.value ) - eval( block.txtLessDownPayment.value ) + eval( block.txtTotalItemsPayable.value ) + eval( block.txtLessTitleCharges.value ) + eval(block.txtLessTitleCharges2.value) + eval(block.txtLessGovernmentCharges.value) - eval(block.txtLessTax.value)),2 );
	}
}
//-->