	calendar= new Array;
	calendar[0]= "January";
	calendar[1]= "February";
	calendar[2]= "March";
	calendar[3]= "April";
	calendar[4]= "May";
	calendar[5]= "June";
	calendar[6]= "July";
	calendar[7]= "August";
	calendar[8]= "September";
	calendar[9]= "October";
	calendar[10]= "November";
	calendar[11]= "December";
	calendar[12]= "";

	function Loan()
		{
		var interest= 0;
		var totalInt= 0;

		var month;

		// Grab the form vars
		var basePrice= parseFloat(document.calcform.price.value);
		var dpayment = parseFloat(document.calcform.dpayment.value);
		if(isNaN(dpayment))
			dpayment= 0;
		var yearlyIR = parseFloat(document.calcform.interest.value);
		var numYears = parseFloat(document.calcform.time.value);
		var startmonth = parseInt(document.calcform.startm.value);
		var year= parseInt(document.calcform.startyear.value);

		// Now some prep calcs
		var mortAmount= basePrice- dpayment;
		// If payments are monthly, num periods= 12; if bi-monthly= 12*2 (i.e.24); if bi-weekly= 26
		var numPeriods= parseInt(document.calcform.periods.value);
		var numPayments= numYears* numPeriods;
		var effIR= yearlyIR/ numPeriods;
		var negmonth= -1* numPayments;
		var deno= 1- (Math.pow(effIR+ 1,negmonth));
		var numo= effIR;
		var result= numo/ deno;

		var monthPay= mortAmount* result;

		win02= window.open("", "", "menubar=no,location=no,toolbar=no,resizeable=yes,scrollbars=yes,directories=no");
		win02.window.resizeTo(600, 560);
		
		open1='<html> \
					<head><title>theHomeslink.com -- Mortage Payment Schedule</title> \
						<style type="text/css"> \
							.tableMort {padding: 3pt; font: 90% tahoma; } \
							.tableMort th {background-color: #00ccff; padding: 3pt; font: 100% tahoma; font-weight: strong; } \
							.tableMort td {text-align: right; padding: 3pt; font: 80% tahoma; } \
							p { font: 80% tahoma } \
							fieldset { background-color:#eeeeee; } \
						</style>	\
					</head> \
				<body bgcolor="#e7f1ff"> \
					<h3>Your Mortgage Payment Calculations</h3>'

		win02.document.write(open1);

		table1= '	<table class="tableMort" border=1> \
						<tr><th colspan="3" style="text-align: left; valign:bottom; background-color:#cccccc">Table 1 - Your Mortgage Details</tr> \
						<tr> \
							<th colspan="2" style="text-align: left; valign:bottom">Your Mortgage Details</th> \
							<td rowspan="7"><img align="right" src="imgs/thehomeslinklogo_blueback_huge.gif" height="100" width="200" /></td> \
						</tr> \
						<tr><td>Purchase price</td><td>' +formatCurrency(basePrice)+ '</td></tr> \
						<tr style="background-color: #ccccff"><td>Downpayment</td><td>' +formatCurrency(dpayment)+ '</td></tr>	\
						<tr><td>Mortgaged Amount (the Principal)</td><td>' +formatCurrency(mortAmount)+ '</td></tr> \
						<tr style="background-color: #ccccff"><td style="background-color: #ccccff">Interest rate applied</td><td> '+yearlyIR*100+ '%</td></tr> \
						<tr><td>Monthly payment &sup1;</td><td>' +formatCurrency(monthPay)+ '</td></tr> \
						<tr style="background-color: #ccccff"><td>Amortization Period (years)</td><td> ' +numYears+ '</td></tr> \
					</table>';
					
		win02.document.write(table1);

/*
		win02.document.write('Purchase price: $'+ basePrice);
		win02.document.write('<br />Downpayment: $'+ dpayment);
		win02.document.write('<br />Mortgaged Amount (the Principal): $'+mortAmount);
		win02.document.write('<br />Interest rate applied: ' +yearlyIR*100+ '%');
		win02.document.write('<br />Amount of each payment &sup1;: $'+Currency(monthPay));
		win02.document.write('<br />Amortization Period (years): '+ numYears);
*/

		table2= '<table class="tableMort"> \
						<tr><th colspan="5" style="text-align: left; valign:bottom; background-color:#cccccc">Table 2 - Your Mortgage Payment Table</tr> \
						<tr> \
							<th width="70">Payment</th> \
							<th width="125">Payment Date</th> \
							<th width="90">Interest Paid</th> \
							<th width="90">Principle Paid</th> \
							<th width="90">Balance</th> \
						</tr>';
		win02.document.write(table2);

		for(y= 1; y<= numPayments; y++)
			{
			//var interest= Houseprice* IR;
			interest= mortAmount* effIR;
			var principal= monthPay- interest;

			mortAmount= Currency(mortAmount- principal);

			interest= Currency(interest);
			principal= Currency(principal);

			totalInt+= parseInt(interest);

			monthofpayment= getMonthName(startmonth);

			// Alternate colours for every second row in the table
			if( (y% 2)== 0)
				win02.document.write('<tr style="background-color: #ccccff">');
			else
				win02.document.write('<tr style="background-color: #e9eeff">');

			win02.document.write('<td>' +y+ '</td><td>' + monthofpayment+ ', ' +year+ '</td><td>'+ formatCurrency(interest)+ '</td><td>'+ formatCurrency(principal) + '</td><td>'+formatCurrency(mortAmount)+ '</td></tr>');

			// startmonth must account for the various payments frequencies
			if(numPeriods== 26)
				{
				// Then the bi-weekly payments are a pain in the arse!
				// We don't display a month, and only increment the year after every 26th payment
				startmonth= 12;

				if(y%26== 0)
					{
					// We will only print the startmonth at the start of a new year's payment cycle
					startmonth= parseInt(document.calcform.startm.value);
					year++;
					}
				}

			else
				{
				// For monthly, we can ad on every loop
				if(numPeriods== 12)
					startmonth++;

				// If bi-monthly, then increment every second time through
				if(numPeriods== 24)
					{
					if(y%2== 0)
						startmonth++;
					}

				// If weekly, then every fourth time
				if(numPeriods== 52)
					{
					if(y%4== 0)
						startmonth++;
					}

				if(startmonth> 11)
					{
					startmonth= 0;
					year++;
					}

				} // end else

			} // end payment loop

		win02.document.write("</table>");

		tail1= '<p>&sup1; Monthly payment amount is based on a constant interest rate throughout \
					the amortization period.</p> <fieldset>Based on these calculations, you will pay $';
		tail2= ' in total interest.</fieldset></p>';
		win02.document.write(tail1 +totalInt+ tail2);


		footer1=   '<img src="imgs/borderbar.gif" height="2" width="760" /> \
						<div style="font: 60% tahoma"> \
						<img align="right" src="imgs/thehomeslinklogo_blueback_huge.gif" height="50" width="100" /> \
						Copyright &#169 2005, 2006 Homes and Castles On-Line.com. All rights reserved.<br /> \
						To find out more about our products and services,<br /> \
						see our web page at www.theHomesLink.com, \
						or contact us at <a href="mailto:support@theHomesLink.com>support@theHomesLink.com</a>. \
						<br/><br /> \
						</div>';
		win02.document.write(footer1);

		close1= '</body></html>';
		win02.document.write(close1);

		} // end loan()



function getMonthName(index)
	{
	return calendar[index];
	}



function check()
	{
	var dPayment = document.calcform.dpayment.value;
	var price = document.calcform.price.value;

	// First two checks on on the string values represented by the price and downpayment
	if(price.indexOf(",") >= 0)
		{
		alert("Please do not put commas in the Purchase Price.");
		return false;
		}


	if(dPayment.indexOf(",") >= 0)
		{
		alert("Please do not put commas in the Down Payment number");
		return false;
		}


	// Now pull the integer values- and check if they are set.
	price= parseInt(price);
	dPayment= parseInt(dPayment);

	// Price cannot be empty
	if( isNaN(price) )
		{
		alert("Please enter a purchase price for your home.");
		return false;
		}

	// Downpayment can be empty, but set it to zero if it is.
	if(isNaN(dPayment))
		dpayment= 0;

	// Downpayment cannot be greater than purchase price
	if(price< dPayment)
		{
		alert("Your downpayment is greater than the purchase price.\nYou can buy the home with cash!");
		return false;
		}

	return true;
	}


