
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 VATCalc()
{
	var cAmount = document.frmVATCalc.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);
	}
	
	for (var i = 0; i < document.frmVATCalc.IncVAT.length; i++) 
	{
		if (document.frmVATCalc.IncVAT[i].checked) 
		{
			incVAT = document.frmVATCalc.IncVAT[i].value;
		}
	}
	
	var netAmount = 0;
	var vatAmount = 0;
	var totalAmount = 0;
	
	if (incVAT == "yes") 
    {		
		vatAmount = (amount * (7/47));
		netAmount = amount - vatAmount;
		totalAmount = amount;
	}
	else
	{
		vatAmount = amount * 0.175;
		netAmount = amount;
		totalAmount = amount * 1.175;
	}

	netAmount = Round2SD(netAmount);
	var cNetAmount = document.frmVATCalc.txtNetAmount;
	cNetAmount.value = PreserveZeros(netAmount);
	
	vatAmount = Round2SD(vatAmount);
	var cVATAmount = document.frmVATCalc.txtVATAmount;
	cVATAmount.value = PreserveZeros(vatAmount);
	
	totalAmount = Round2SD(totalAmount);
	var cTotalAmount = document.frmVATCalc.txtTotalAmount;
	cTotalAmount.value = PreserveZeros(totalAmount);                      
}
