
function IntegerKeyPressHandler()
{
	if (event.keyCode < 48 || event.keyCode > 57) return false;
	else return true
}

function DecimalKeyPressHandler()
{
	if (event.keyCode != 46 && !IntegerKeyPressHandler()) return false;
	else return true
}

function OnFocusHandler(obj)
{
	obj.select();
}

function Round2SD(v)
{
	return Math.round(v * 100) / 100;
}

function PreserveZeros(v)
{
	if (typeof(v) != "string") v = v.toString();
	
	var decimalPos = v.indexOf(".");
	if (decimalPos == -1) 
	{
		v += ".";
		for (i = 0; i < 2; i++) 
		{
			v += "0";
		}
	} 
	else 
	{
		var actualDecimals = (v.length - 1) - decimalPos;
		var difference = 2 - actualDecimals;
		for (i = 0; i < difference; i++) 
		{
			v += "0";
		}
	}
	
	return v;
}

function StampDutyCalc()
{
	var cAmount = document.frmStampDutyCalc.txtAmount;
	var amount = cAmount.value;
	
	if (amount == "")
	{
		amount = 0;
		cAmount.value = PreserveZeros(amount);
	}
	else
	{ 
		amount = parseFloat(amount);
		if (isNaN(amount) || amount < 0)
		{
			cAmount.focus();
			alert("Invalid Amount");
			return;
		}
		else
			cAmount.value = PreserveZeros(amount);
	}
	
	var txType;
	
	for (var i = 0; i < document.frmStampDutyCalc.txType.length; i++) 
	{
		if (document.frmStampDutyCalc.txType[i].checked) 
		{
			txType = document.frmStampDutyCalc.txType[i].value;
		}
	}
	
	var stampDutyAmount = 0;

    if (txType == "2") //Residential Land
    {
		if (amount < 150001) 
		{
			stampDutyAmount = 0;
		} 
		else if (amount < 250001) 
		{
			stampDutyAmount = amount * 0.01;
		} 
		else if (amount < 500001) 
		{
			stampDutyAmount = amount * 0.03;
		} 
		else 
		{
			stampDutyAmount = amount * 0.04;
		}
    } 
    else if (txType == "3") //Non-Residential Land
    {
		if (amount < 150001) 
		{
			duty = 0;
		} 
		else if (amount < 250001) 
		{
			stampDutyAmount = amount * 0.01;
		}
		else if (amount < 500001) 
		{
			stampDutyAmount = amount * 0.03;
		} 
		else 
		{
			stampDutyAmount = amount * 0.04;
		}                      
    } 
    else //shares
		stampDutyAmount = amount * 0.005;
    	
	stampDutyAmount = Round2SD(stampDutyAmount);
	var cStampDutyAmount = document.frmStampDutyCalc.txtStampDutyAmount;
	cStampDutyAmount.value = PreserveZeros(stampDutyAmount);               
}
