// JavaScript for the Calculator
// Copyright Christopher Chong Productions Inc. 2005
// info@christopherchong.com 


//Global Variables
legal_fee = 999.00;
gst_rate = 0.06;
govt_reg_cost = 141.40;
title_insurance = 251.00;

// This function checks a field for a valid number.
function checknumber(testnum)
{
	var x=testnum;
	var anum=/(^\d+$)|(^\d+\.\d+$)/;
	
	if (anum.test(x))
	testresult=true;
	else
	{
		testresult=false;
	}
	return (testresult);
}

// This function calculates the land tax and fees
function calculate()
{
	// Grab purchase price and remove any commas or $
	var x = document.calculator.purchase_price.value.replace(/\$|\,/g,'')
	x = x - 0; //fix to convert string to integer (works in old browsers)
	var y = x.toFixed(2);
	
	// Calculate the different tax rates	
	var tax1 = x * 0.005;
	var tax2 = x * 0.01 - 275;
	var tax3 = x * 0.015 - 1525;
	var tax4 = x * 0.02 - 3525;
	
	// Declare variables for calculations
	var land_transfer_tax = 0; //default
	var gst = 0; //default
	var total = 0; //default
	
	// Verify that the input is a valid number
	if (x < 0 || x > 500000 || !checknumber(x))
	{
		alert("Please enter a value between 0 and 500000");
		document.estimator.width.focus();
	}
	
	// Calculate the Land Transfer Tax
	if (x <= 55000)
	{
		land_transfer_tax = tax1;
	}
	else if (x > 55000 && x < 250000)
	{
		land_transfer_tax = tax2;
	}
	else if (x >= 250000 && x < 400000)
	{
		land_transfer_tax = tax3;
	}
	else
	{
		land_transfer_tax = tax4;
	}
	
	// Fix all the global variables to two decimal places
	var legal_fee2 = legal_fee.toFixed(2);
	var govt_reg_cost2 = govt_reg_cost.toFixed(2);
	var title_insurance2 = title_insurance.toFixed(2);
	var land_transfer_tax2 = land_transfer_tax.toFixed(2);
	
	// Calculate the GST
	gst = legal_fee * gst_rate;
	var gst2 = gst.toFixed(2);
	
	// Calculate the TOTAL fees
	total = legal_fee + gst + govt_reg_cost + title_insurance + land_transfer_tax;
	var total2 = total.toFixed(2)
	
	// Open a new window...
	win1 = window.open("","popup","height=400,width=500,scrollbars=yes,resizable=no");
	win1.document.write('<HTML>');
	win1.document.write('<HEAD>');
	win1.document.write('<TITLE>Summary Window</TITLE>');
	win1.document.write('</HEAD>');
	win1.document.write('<BODY background="images/background04.gif" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">');
	win1.document.write('<table width="460" border="0" cellspacing="0" cellpadding="0">');
	win1.document.write('<tr valign="top">');
	win1.document.write('<td width="160">');
	win1.document.write('<img src="images/jk_logo.gif">');
	win1.document.write('</td>');
	win1.document.write('<td>');
	
	// Print a summary of Costs...
	win1.document.write('<FONT FACE="arial" SIZE="2">');
	win1.document.write('<BR>');
	win1.document.write('<table width="300" border="0" cellspacing="0" cellpadding="2">');
	
	win1.document.write('<tr valign="top">');
	win1.document.write('<td width="200">');
	win1.document.write('Purchase Price:');
	win1.document.write('</td>');
	win1.document.write('<td width="100">');
	win1.document.write('$'+y);
	win1.document.write('</td>');
	win1.document.write('</tr>');
	
	win1.document.write('<tr valign="top">');
	win1.document.write('<td width="200">');
	win1.document.write('Legal Fee:');
	win1.document.write('</td>');
	win1.document.write('<td width="100">');
	win1.document.write('$'+legal_fee2);
	win1.document.write('</td>');
	win1.document.write('</tr>');
	
	win1.document.write('<tr valign="top">');
	win1.document.write('<td width="200">');
	win1.document.write('GST:');
	win1.document.write('</td>');
	win1.document.write('<td width="100">');
	win1.document.write('$'+gst2);
	win1.document.write('</td>');
	win1.document.write('</tr>');
	
	win1.document.write('<tr valign="top">');
	win1.document.write('<td width="200">');
	win1.document.write('Govt Registration Costs:');
	win1.document.write('</td>');
	win1.document.write('<td width="100">');
	win1.document.write('$'+govt_reg_cost2);
	win1.document.write('</td>');
	win1.document.write('</tr>');
	
	win1.document.write('<tr valign="top">');
	win1.document.write('<td width="200">');
	win1.document.write('Title Insurance:');
	win1.document.write('</td>');
	win1.document.write('<td width="100">');
	win1.document.write('$'+title_insurance2);
	win1.document.write('</td>');
	win1.document.write('</tr>');
	
	win1.document.write('<tr valign="top">');
	win1.document.write('<td width="200">');
	win1.document.write('Land Transfer Tax:');
	win1.document.write('</td>');
	win1.document.write('<td width="100">');
	win1.document.write('$'+land_transfer_tax2);
	win1.document.write('</td>');
	win1.document.write('</tr>');
	
	win1.document.write('<tr valign="top">');
	win1.document.write('<td width="200">');
	win1.document.write('TOTAL FEES:');
	win1.document.write('</td>');
	win1.document.write('<td width="100">');
	win1.document.write('$'+total2);
	win1.document.write('</td>');
	win1.document.write('</tr>');
	
	win1.document.write('</table>');
	win1.document.write('</FONT>');
	
	win1.document.write('<FONT FACE="arial" SIZE="2">');
	win1.document.write('<BR><BR>* Please note that this quote is for reference purposes only. I reserve the right to make any changes I see as necessary without notice. Please contact me for a written quote on your transaction');
	win1.document.write('<BR><BR>Thank you.<BR>Joel Kadish<BR>416-636-5565<BR>');
	win1.document.write('</FONT>');
	
	// Create close, print, and save buttons and close off the HTML tags...
	win1.document.write('<BR><form><INPUT TYPE="BUTTON" NAME="SUB" VALUE="Close" onClick="window.close();">');
	win1.document.write('<input type="button" name="Button" value="Save" onClick=document.execCommand("SaveAs")>');
	win1.document.write('<input type="button" name="Button" value="Print" onClick=document.execCommand("Print")></form>');
	win1.document.write('</td>');
	win1.document.write('</tr>');
	win1.document.write('</table>');
	win1.document.write('</BODY>');
	win1.document.write('</HTML>');
	
}


