
		function initPage ()
		{
			f = document.forms["monthly"];
			calcAmount (f.dep_percent, f.prop_price, f.dep_amt);
		}

		function switchDeposit (on, off)
		{
			on.style.background = "#F2F4F9";
			off.style.background = "#cccccc";
			return;
		}

		function recalculate_Amount()
		{
			f = document.forms["monthly"];
			if (isNaN(f.prop_price.value))
			{
				alert("Please enter a valid property price");
				return false;
			}
			else
			{
				if ((!isNaN(f.dep_amt.value))&&(f.dep_amt.value!=""))
				{
					return calcPercent (f.dep_amt, f.prop_price, f.dep_percent);
				}
			}
		}
		
		function calcPercent (amt, prp, per)
		{
			if ((amt.value == "")&&(per.value == ""))
			{
				alert("Please enter a valid deposit amount/percentage");
				return false;
			}
			else
			{
				if (amt.value != "")
				{
					if (isNaN(amt.value))
					{
						alert("Please enter a valid deposit amount");
						return false;
					}
					else
					{
						var x = (amt.value / prp.value) * 100;
						per.value = (Math.round (x * 100) / 100);
						subtractDeposit (amt.value, prp.value, per.value);
						return;
					}
				}
			}
		}

		function calcAmount (per, prp, amt)
		{
			if ((amt.value == "")&&(per.value == ""))
			{
				alert("Please enter a valid deposit amount/percentage");
				return false;
			}
			else
			{
				if (per.value != "")
				{
					if (isNaN(per.value))
					{
						alert("Please enter a valid percentage");
						return false;
					}
					else
					{
						var number = parseFloat(per.value);
						per.value=(Math.round(number*100)/100); 
						var x = (per.value / 100) * prp.value;
						amt.value = (Math.round (x * 100) / 100);
						subtractDeposit (amt.value, prp.value, per.value);
						return;
					}
				}
			}
		}

function addCommas(argNum, argThouSeparator, argDecimalPoint)
{
	// default separator values (should resolve to local standard)
	var sThou = (argThouSeparator) ? argThouSeparator : ","
	var sDec = (argDecimalPoint) ? argDecimalPoint : "."
	
	// split the number into integer & fraction
	var aParts = argNum.split(sDec)
	
	// isolate the integer & add enforced decimal point
	var sInt = aParts[0] + sDec
	
	// tests for four consecutive digits followed by a thousands- or  decimal-separator
	var rTest = new RegExp("(\\d)(\\d{3}(\\" + sThou + "|\\" + sDec + "))")
	
	while (sInt.match(rTest))
	{
	        // insert thousands-separator before the three digits
	        sInt = sInt.replace(rTest, "$1" + sThou + "$2")
	}
	
	// plug the modified integer back in, removing the temporary  decimal point
	aParts[0] = sInt.replace(sDec, "")
	
	// debug display
//	document.write ("<tr>" + "<td>" + rTest.source + "</td>" + "<td>" + argNum + "</td>" + "<td>" + aParts.join(sDec) + "</td>" +"</tr>")
	
	// return the modified integer + decimal portion(s)
	return aParts.join(sDec)
 }		
		
		
function validateField (amt)
		{
			//alert ("validate field");
			if ((amt.value == ""))
			{
				//alert("Please enter a valid deposit amount/percentage");
				alert ("empty field");
				return false;
			}
			else
			{
				//var tmp = parseFloat((amt.value));
				//alert ("validate field info" + tmp);
				if (isNaN(amt.value))
				{
					alert("Please enter a valid number");					
					amt.focus();
					amt.value = "";
					return false;
				}
			}
		}

		
		
		
		function subtractDeposit (dep, prp, per)
		{
			if(isNaN(prp))
			{
				alert("Please enter a valid property price");
				return false;
			}
			if(isNaN(dep))
			{
				alert("Please enter a valid deposit amount");
				return false;
			}
			
			myLoanAmount = prp - dep;

			if (myLoanAmount > 0)
			{
				//prp.form.loan_amt.value = prp.value - dep.value;
				document.monthly.loan_amt.value = (prp - dep);
				return;
			}
			else
			{
				alert("The deposit is greater than the purchase price.");
				//prp.form.loan_amt.value = "";
				//prp.form.dep_amt.value = "";
				//prp.form.dep_percent.value = "";
				document.monthly.loan_amt.value = "";
				document.monthly.dep_amt.value = "";
				document.monthly.dep_percent.value = "";
				return true;
			}
		}

		function LTrim(str)
		{
			var whitespace = new String(" \t\n\r");
			var s = new String(str);
			if (whitespace.indexOf(s.charAt(0)) != -1) {
				var j=0, i = s.length;
				while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
						j++;
				s = s.substring(j, i);
			}
			return s;
		}

		function RTrim(str)
		{
			var whitespace = new String(" \t\n\r");
			var s = new String(str);
			if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
				var i = s.length - 1;       // Get length of string
				while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
						i--;
				s = s.substring(0, i+1);
			}
			return s;
		}

		function Trim(str)
		{
			return RTrim(LTrim(str));
		}

		function getMonthlyPayment (f)
		{
		
		
			var prop_price = Trim(f.prop_price.value);
			if(isNaN(prop_price))
			{
				alert("Please enter the price of the property");
				return false;
			}			
				
			if (f.dep_amt.value == "");
			
			var loan_amt = Trim(f.loan_amt.value);
			if(isNaN(loan_amt))
			{
				alert("Please enter a valid loan amount");
				return false;
			}
			var repay_term = Trim(f.repay_term.value);
			if(isNaN(repay_term))
			{
				alert("Please enter a valid repayment term");
				return false;
			}
			var dep = Trim(f.dep_amt.value);
			if(isNaN(dep))
			{
				alert("Please enter a valid deposit amount");
				return false;
			}
			var int_rate = Trim(f.int_rate.value);
			if(isNaN(int_rate))
			{
				alert("Please enter a valid interest rate");
				return false;
			}

			f.loan_amt.value=loan_amt;
			f.repay_term.value=repay_term;
			f.dep_amt.value=dep;
			f.int_rate.value=int_rate;

			if((!f.dep_amt.value)&&(f.dep_percent.value))
			{
				var new_dep = calcAmount (f.dep_percent.value, f.prop_price.value, f.dep_amt.value);
				return getMonthlyPayment(f);
			}
			var out = "";
			
			out += (prop_price == "")? "- Property price.\n":"";			
			out += (dep == "")? "- Deposit amount/percentage.\n":"";
			out += (loan_amt == "")? "- Loan amount.\n":"";
			out += (repay_term == "")? "- Repayment term.\n":"";
			out += (int_rate < 0.001)? "- An interest rate higher than 0.001%.\n":"";
			out += (int_rate > 25)? "- An interest rate lower than 25%.\n":"";
			out += (repay_term < 1)? "- Repayment term to one year or more.\n":"";
			out += (repay_term > 50)? "- Repayment term to less than 51 years.\n":"";
			if (out != "")
			{
				sendMessage ("Please complete the following fields:", out, 0);
				return;
			}
			else
			{
				var temp_rate = (int_rate.indexOf ("%") == -1)?int_rate:stripPercent(int_rate);
				f.mon_cost.value = addCommas(doMonthlyFormula (loan_amt, temp_rate, repay_term, 0) + '');
				f.mon_cost_int.value = addCommas(doMonthlyFormula (loan_amt, temp_rate, repay_term, 1) + '');
				return;
			}
		}
		
		function stripPercent (str)
		{
			var out = str.substring (0, str.length - 1);
			return (out);
		}

		function doMonthlyFormula (loan, int_rate, rep_term, mort_type)
		{

			var numerator = loan * int_rate / 1200;
			var result = 0;
			
			if (mort_type == 0)
			{
				with (Math)
				{
					var denominator = 1 - 1 / (pow (1 + int_rate / 1200, rep_term * 12));
				}
				result = numerator / denominator;
			}
			else
			{
				result = numerator;
			}

			if (result == 0) result = "";
			return (Math.round (result * 100) / 100);
		}



		function getMaxBorrow (f)
		{
			var main_sal = f.main_sal.value;
			var main_freq = f.main_freq.options[f.main_freq.selectedIndex].value;
			var part_sal = f.part_sal.value;
			var part_freq = f.part_freq.options[f.part_freq.selectedIndex].value;
			var joint = false;
			if (main_sal == "")
			{
				f.amount.value = "0.00";
				sendMessage ("In order to calculate this figure...", "\t- Please enter your salary.\n", 50);
				return;
			}
			else
			{
				if (part_sal != "")
				{
					joint = true;
				}
				f.amount.value = doMaxFormula (main_sal, main_freq, part_sal, part_freq, joint);
				return;
			}
		}

		function doMaxFormula (ms, mf, ps, pf, j)
		{
			var s_mult = 4.0;
			var j_multA = 3.0;
			var j_multB = 3.5;
			var result = 0;
			if (ps == "")
			{
				// Single salary
				result = (s_mult * (ms * mf));
			}
			else
			{
				var resA = 0;
				var resB = 0;
				resA = (j_multA * ((ms * mf) + (ps * pf)));
				resB = ((j_multB * (ms * mf)) + (ps * pf));
				result = (resA > resB)?resA:resB;
			}
			return (Math.round (result * 100) / 100);
		}

		function sendMessage (title, out, width)
		{
			var divide = "";
			for (i = 0; i < width; i++)
			{
				divide += "_";
			}

			var top = divide + "\n\n" + title + "\n\n";
			alert (top + out);
			return;
		}
