function home() {
	window.location = "http://www.hdb.gov.sg";
}

function HousingLoanType() {
	url = "http://www.hdb.gov.sg/bb24/bb24002p.nsf/Welcome?OpenForm";
	window.open(url,"newwin2");
}

function recalculate() {
	document.forms[0].Back.value = "Back";
	document.forms[0].submit();
}

function PurchasePrice() {
	alert("Price of the resale flat that you have bought");
}

function disableGrnt() {
	grnt = document.forms[0].HouseGrant;
	addGrnt = document.forms[0].AddHouseGrant;
	disable = false;
	for (i = 0; i < 7; i++) {
		if (grnt[i].checked) {
			if ((grnt[i].value == "30,000") || (grnt[i].value == "40,000") || 
				(grnt[i].value == "20,000" && i == 4)) {
				for (i = 0; i < 8; i++) {
					addGrnt[i].disabled = false;
					}
			}
			disable = true;
		}
	}
	if (!disable) {
		for (i = 0; i < 8; i++) {
			addGrnt[i].checked = false;
			addGrnt[i].disabled = true;
		}
		document.forms[0].GrantZero.value = "true";
		document.forms[0].AddGrantZero.value = "true";
	}
	else {
		document.forms[0].GrantZero.value = "false";
	}
	for (i = 0; i < 7; i++) {
		if (!grnt[i].checked) grnt[i].disabled = disable;
	}
}

function disableAddGrnt() {
	addGrnt = document.forms[0].AddHouseGrant;
	zero = true;
	for (i = 0; i < 8; i++) {
		if (addGrnt[i].checked) {
			zero = false;
		}
	}
	
	if (zero) {
		document.forms[0].AddGrantZero.value = "true";
	}
	else {
		document.forms[0].AddGrantZero.value = "false";
	}
	
	allDisabled = true;
	for (i = 0; i < 8; i++) {
		if (!addGrnt[i].disabled) allDisabled = false;
	}
	if (!allDisabled) {
		disable = false;
		for (i = 0; i < 8; i++) {
			if (addGrnt[i].checked) disable = true;
		}
		for (i = 0; i < 8; i++) {
			if (!addGrnt[i].checked) addGrnt[i].disabled = disable;
		}
	}
}

function setSelectedInt(index) {
	document.forms[0].SelectedInterest.value = index - 1;
}

function setInterest() {
	if (document.forms[0].InterestLoan[0] == null && document.forms[0].SelectedInterest.value == "0") {
		document.forms[0].InterestLoan.checked = true;
	} else if (document.forms[0].SelectedInterest.value != "") {
		document.forms[0].InterestLoan[document.forms[0].SelectedInterest.value].checked = true;
	}
}

function init() {
	disableGrnt();
	disableAddGrnt();
	setInterest();
}

function isCharsInBag(n,bag) {
	var i;
	var k=(n.length);
	for(i=0;i<k;i++)
	{
	var c=n.charAt(i);
	if(bag.indexOf(c) == -1)
		return false;
	}
	return true;
}

function validateFIN() {
	if (validateInt(document.forms[0].MaxLoan, "Estimated Maximum Loan") &&
		validateInt(document.forms[0].RepayPeriod, "Repayment Period") &&
		validateInt(document.forms[0].PurPrice, "Purchase Price Of Resale Flat") &&
		validateInt(document.forms[0].MrktValue, "Market Valuation Of Resale Flat") &&
		validateInt(document.forms[0].Deposit, "Deposit") &&
		validateFlatType() &&
		validateInt(document.forms[0].MonthlyCPF, "Monthly contribution to CPF Ordinary Account") &&
		validateInt(document.forms[0].TotalCPF, "Total CPF Monies") &&
		validateInterest() &&
		validate2ndConLoan()) {
		document.forms[0].Calculate.value = "Continue";
		if(mobileUser()){
			document.forms[0].action = "/webapp/BB29FIN/BB29PMobileFinPlanEnq.jsp";
		}
		document.forms[0].submit();
	}
}

function isInt(num) {
	if(!isCharsInBag(num,"0123456789,")) {
		return false;
	}
	return true;
}

function validateInt(field, label) {
	if (field.value == "") {
		field.select();
		alert("Please provide the " + label);
		return false;
	}
	else {
		if (!isInt(field.value)) {
			field.select();
			alert("Please provide a valid " + label);
			return false;
		}
	}
	return true;
}

function validateFlatType() {
	for (i = 0; i < 3; i++) if (document.forms[0].FlatType[i].checked) return true;
	alert("Please select a Flat Type");
	return false;
}

function validateInterest() {
	if (document.forms[0].InterestLoan.length == null) {
		if (document.forms[0].InterestLoan.checked) return true;
		else {
			alert("Please select an interest Rate");
			return false;
		}
	}
	else {
		for (i = 0; i < document.forms[0].InterestLoan.length; i++) if (document.forms[0].InterestLoan[i].checked) return true;
		alert("Please select an interest rate");
		return false;
	}
}

function clean() {
	document.forms[0].MaxLoan.value = "";
	document.forms[0].RepayPeriod.value = "";
	document.forms[0].PurPrice.value = "";
	document.forms[0].MrktValue.value = "";
	document.forms[0].Deposit.value = "";
	document.forms[0].MonthlyCPF.value = "";
	document.forms[0].TotalCPF.value = "";
	
	document.forms[0].FlatType[0].checked = false;
	document.forms[0].FlatType[1].checked = false;
	document.forms[0].FlatType[2].checked = false;
	
	if (document.forms[0].InterestLoan.length == null) {
		document.forms[0].InterestLoan.checked = false;
	}
	else {
		for (i = 0; i < document.forms[0].InterestLoan.length; i++) {
			document.forms[0].InterestLoan[i].checked = false;
		}
	}
	
	document.forms[0].HouseGrant[0].checked = false;
	document.forms[0].HouseGrant[0].disabled = false;
	document.forms[0].HouseGrant[1].checked = false;
	document.forms[0].HouseGrant[1].disabled = false;
	document.forms[0].HouseGrant[2].checked = false;
	document.forms[0].HouseGrant[2].disabled = false;
	document.forms[0].HouseGrant[3].checked = false;
	document.forms[0].HouseGrant[3].disabled = false;
	// This function clear the input values of the AHG	
	document.forms[0].HouseGrant[4].checked = false;
	document.forms[0].HouseGrant[4].disabled = false;
	document.forms[0].HouseGrant[5].checked = false;
	document.forms[0].HouseGrant[5].disabled = false;
	document.forms[0].HouseGrant[6].checked = false;
	document.forms[0].HouseGrant[6].disabled = false;
	// This function clear the input values of the AHG	
	document.forms[0].AddHouseGrant[0].checked = false;
	document.forms[0].AddHouseGrant[0].disabled = true;
	document.forms[0].AddHouseGrant[1].checked = false;
	document.forms[0].AddHouseGrant[1].disabled = true;
	document.forms[0].AddHouseGrant[2].checked = false;
	document.forms[0].AddHouseGrant[2].disabled = true;
	document.forms[0].AddHouseGrant[3].checked = false;
	document.forms[0].AddHouseGrant[3].disabled = true;
	document.forms[0].AddHouseGrant[4].checked = false;
	document.forms[0].AddHouseGrant[4].disabled = true;
	document.forms[0].AddHouseGrant[5].checked = false;
	document.forms[0].AddHouseGrant[5].disabled = true;
	document.forms[0].AddHouseGrant[6].checked = false;
	document.forms[0].AddHouseGrant[6].disabled = true;
	document.forms[0].AddHouseGrant[7].checked = false;
	document.forms[0].AddHouseGrant[7].disabled = true;
	
	document.forms[0].SellTag[0].checked = false;
	document.forms[0].SellTag[1].checked = false;
	document.forms[0].SellingPrice.value = "";
	document.forms[0].OutstandingLoan.value = "";
	document.forms[0].CpfRefund.value = "";
	toggleSellTag('N');
}
function cpfMonies() {
	newwin = window.open('','','top=360,left=250,width=400,height=120');
	if (!newwin.opener) newwin.opener = self;
	with (newwin.document)
	{
		open();
		write('<html>');
		write('<title>Total CPF Monies of All Buyers in the Ordinary Account</title>');
		write('<body><form name=form><font Face=Arial size=2>If you or any of the co-purchasers have taken an HDB concessionary loan before, please exclude the CPF refund from the existing flat to be sold or previous flat sold</Font>');
		write('<p><center><input type=button value="Close" onClick=window.close()>');
		write('</center></form></body></html>');
		close();
	}
}
function maxLoan() {
	newwin = window.open('','','top=360,left=250,width=400,height=78');
	if (!newwin.opener) newwin.opener = self;
	with (newwin.document)
	{
		open();
		write('<html>');
		write('<title>Estimated Maximum Loan</title>');
		write('<body><form name=form><font Face=Arial size=2>It is the maximum loan that can be granted to the flat buyers.</Font>');
		write('<p><center><input type=button value="Close" onClick=window.close()>');
		write('</center></form></body></html>');
		close();
	}
}
function resaleFlat() {
	newwin = window.open('','','top=360,left=250,width=400,height=150');
	if (!newwin.opener) newwin.opener = self;
	with (newwin.document)
	{
		open();
		write('<html>');
		write('<title>Purchase Price Of Resale Flat</title>');
		write('<body><form name=form><font Face=Arial size=2>This is also known as the declared resale price in the Option to Purchase (OTP). <br><br>The price is agreed between the buyers and sellers of a flat.</Font>');
		write('<p><center><input type=button value="Close" onClick=window.close()>');
		write('</center></form></body></html>');
		close();
	}
}
function deposit() {
	newwin = window.open('','','top=360,left=250,width=400,height=240');
	if (!newwin.opener) newwin.opener = self;
	with (newwin.document)
	{
		open();
		write('<html>');
		write('<title>Deposit</title>');
		write('<body><form name=form><font Face=Arial size=2>The deposit is the amount paid by the buyer to confirm his or her intention to  buy the flat. <br><br>Deposit= Option Fee + Option Exercise Fee. <br><br>Option Fee Fee must not exceed $1,000, while Option Exercise Fee must not exceed $4,000 and their total must not exceed $5,000. <br><br>Neither the Option Fee nor the Option Exercise Fee can be $0.</Font>');
		write('<p><center><input type=button value="Close" onClick=window.close()>');
		write('</center></form></body></html>');
		close();
	}
}

function stampFee(){
	newwin = window.open('','','top=360,left=250,width=500,height=120');
	if (!newwin.opener) newwin.opener = self;
	with (newwin.document)
	{
		open();
		write('<html>');
		write('<title>Stamp Fee</title>');
		write('<body><form name=form><font Face=Arial size=2><p align="justify">Stamp fee is a tax on legal documents which record and give effect to certain transactions.  You can use the saving in your CPF Ordinary Account excluding CPF Housing Grant to pay the fee.</p></Font>');
		write('<p><center><input type=button value="Close" onClick=window.close()>');
		write('</center></form></body></html>');
		close();
	}
}
function convFee(){
	newwin = window.open('','','top=360,left=250,width=500,height=220');
	if (!newwin.opener) newwin.opener = self;
	with (newwin.document)
	{
		open();
		write('<html>');
		write('<title>Conveyancing Fees</title>');
		write('<body><form name=form><font Face=Arial size=2><p align="justify">Conveyancing is a process by which the ownership of a property is legally transferred from one owner to another. <br><br>Example of conveyancing services are drafting, preparation and checking of legal documents, title searches, lodging of caveat and legal advise relating to purchase/mortgage process. <br><br>You can use the saving in your CPF Ordinary Account excluding CPF Housing Grant to pay the fee.</p></Font>');
		write('<p><center><input type=button value="Close" onClick=window.close()>');
		write('</center></form></body></html>');
		close();
	}
}
function cavFee(){
	newwin = window.open('','','top=360,left=250,width=500,height=150');
	if (!newwin.opener) newwin.opener = self;
	with (newwin.document)
	{
		open();
		write('<html>');
		write('<title>Caveat Fee</title>');
		write('<body><form name=form><font Face=Arial size=2><p align="justify">A caveat is a legal document lodged with the Singapore Land Authority by a buyer to protect his/her interest after an option to purchase is exercised or a Sales & Purchase Agreement is signed. <br><br>You can pay the fee using cash only, on the first appointment.</p></Font>');
		write('<p><center><input type=button value="Close" onClick=window.close()>');
		write('</center></form></body></html>');
		close();
	}
}
function validateDeposit(obj)
{
	if(obj.value!="")
	{
		var depositAmt=parseFloat(obj.value);
		if(depositAmt<1 || depositAmt>5000)
		{
			alert("The deposit amount indicated should be between $1- $5000");
			obj.focus();

		}	
	}	
}
function redirect(){
	document.forms[0].method="post";
	document.forms[0].action="BB29SRedirect";
	document.forms[0].submit();
}

function setHouseGrantType(thisObj, val) {
	if (thisObj.checked)
		document.forms[0].HouseGrantType.value = val;
	else
		document.forms[0].HouseGrantType.value = "";
}
function toggleSellTag(val){
	var thisForm = document.forms[0];
	if (val == "N") {
		document.getElementById("2ndConLoanRow2").style.display = "none";
		document.getElementById("2ndConLoanRow3").style.display = "none";
		document.getElementById("2ndConLoanRow4").style.display = "none";
		document.getElementById("2ndConLoanRow5").style.display = "none";
		thisForm.SellingPrice.value = "";
		thisForm.OutstandingLoan.value = "";
		thisForm.CpfRefund.value = "";
	}
	else {
		document.getElementById("2ndConLoanRow2").style.display = "block";
		document.getElementById("2ndConLoanRow3").style.display = "block";
		document.getElementById("2ndConLoanRow4").style.display = "block";
		document.getElementById("2ndConLoanRow5").style.display = "block";
	}
}
function validate2ndConLoan() {
	thisForm = document.forms[0];
	
	if (!thisForm.SellTag[0].checked && !thisForm.SellTag[1].checked) {
		alert("Please indicate if you are taking your 2nd HDB Concessionary Loan.");
		return false;
	}
	else if (thisForm.SellTag[0].checked) {
		if (!(validateInt(thisForm.SellingPrice, "Estimated Selling Price") && 
			validateInt(thisForm.OutstandingLoan, "Outstanding Loan") &&
			validateInt(thisForm.CpfRefund, "CPF Refund"))) {
			
			return false;	
		}
		else if (toInt(thisForm.SellingPrice) < 50000) {
			alert("Estimated Selling Price is too low. Please check.")
			thisForm.SellingPrice.focus();
			return false;
		}
			
	}
	
	return true;
}
function toInt(input) {
	num = input.value;
	l = num.length;
	numeric = "";
	for (i = l - 1; i >= 0; i--) {
		ch = num.charAt(i);
		if (!(ch == ',')) {
			numeric = ch + numeric;
		}
	}
	return parseInt(numeric)
}