/*
 * Stampduty.js - holds function called by stampduty.html to calculate stamp duty
 *
 *
 */


/*
 * Clean the input from the user
 */ 
function cleanInput(inputString)
{
	var cleanedString = "";
	var Chars = "0123456789."; // Only allow digits and `.`
	for (var i = 0; i < inputString.length; i++)
	{
		if (Chars.indexOf(inputString.charAt(i)) != -1) 
		{
			cleanedString = cleanedString + inputString.charAt(i);
		}
	}
	return cleanedString;
}

/*
 * Calculate the stamp duty.
 */
function calcStampDuty() 
{
	// declare variables
	var strAmount;
	var strLoanAmount;
	var intDuty;
	var intLoanDuty;
	var intTransfer;
	var intMortgage;
	var strState;
	var intMod;
	var intMod2;

	strAmount = cleanInput(stampDutyForm.txtAmount.value);
	strLoanAmount = cleanInput(stampDutyForm.txtLoanAmount.value);
	
	if ((strAmount == "") && (strLoanAmount == ""))
	{
		window.alert("Need to enter the value of the property and the loan amount.");
		return;
	}
	
	intAmount = eval(strAmount);
	intLoanAmount = eval(strLoanAmount);
	
	strState = stampDutyForm.cboState.options[stampDutyForm.cboState.selectedIndex].value;
	
	t_amount = intAmount;
	
	// round up to the 100
	if ((intAmount % 100) != 0 )
	{
		intMod = 100-(intAmount % 100);
		intAmount = intAmount + intMod;
	}
	// round up to the 100
	if ((intLoanAmount % 100) != 0 )
	{
		intMod2 = 100-(intLoanAmount % 100);
		intLoanAmount = intLoanAmount + intMod2;
	}
	//
	// Victoria
	//
	if (strState == "VIC")
	{
		intMortgage = 59;

		// round up to the 1000
		if ((t_amount % 1000) != 0 )
		{
			intMod = 1000-(t_amount % 1000);
			t_amount = t_amount + intMod;
		}
		
		if ((t_amount > 0) && (t_amount <= 500000))
		{
			intTransfer = (t_amount / 1000) * 2.46 + 90;
		}
		else
		{
			intTransfer = 1320;
		}
		
		if (intAmount <= 20000)
		{
			intDuty = ((intAmount)/100) * 0;
		}
		else if (intAmount <= 115000)
		{
			intDuty = (((intAmount - 20000)/100) * 0) + 0;
		}
		else if (intAmount <= 870000)
		{
			intDuty = (((intAmount - 115000)/100) * 0) + 0;
		}
		else {
			intDuty = ((intAmount)/100) * 0;
		}
		
		if ((intLoanAmount % 200) != 0 )
		{
			intMod = 200-(intLoanAmount % 200);
			intLoanAmount = intLoanAmount + intMod;
		}
		if (intLoanAmount <= 10000)
		{
			intLoanDuty = 0;
		}
		else
		{
			intLoanDuty= (((intLoanAmount - 10000)/200) * 0) + 0;
		}
	}
	//
	// New South Wales
	//
	else if (strState == "NSW")
	{
		intMortgage = 62;
		intTransfer = 62;
	
		if (intAmount <= 14000)
		{
			intDuty = (intAmount/100) * 1.25;
		}
		else if (intAmount <= 30000)
		{
			intDuty = (((intAmount - 14000)/100) * 1.5) + 175;
		}
		else if (intAmount <= 80000)
		{
			intDuty = (((intAmount - 30000)/100) * 1.75) + 415;
		}
		else if (intAmount <= 300000)
		{
			intDuty = (((intAmount - 80000)/100) * 3.5) + 1290;
		}
		else if (intAmount <= 1000000)
		{
			intDuty = (((intAmount - 300000)/100) * 4.5) + 8990;
		}
		else
		{
			intDuty = (((intAmount - 1000000)/100) * 5.5) + 40490;
		}
		
		if ((intLoanAmount % 1000) != 0 )
		{
			intMod = 1000 - (intLoanAmount % 1000);
			intLoanAmount = intLoanAmount + intMod;
		}
		if (intLoanAmount <= 16000)
		{
			intLoanDuty = 5;
		}
		else
		{
			intLoanDuty = (((intLoanAmount - 16000)/1000) * 4) + 5;
		}
	}
	//
	// ACT
	//
	else if (strState == "ACT")
	{
		intMortgage = 78;
		intTransfer = 155;
		intLoanDuty = 0;
	
		if (intAmount <= 100000)
		{
			intDuty = (intAmount/100) * 2;
			if (intDuty < 20) {
				intDuty = 20;
			}
		}
		else if (intAmount <= 200000)
		{
			intDuty = (((intAmount - 100000)/100) * 3.5) + 2000;
		}
		else if (intAmount <= 300000)
		{
			intDuty = (((intAmount - 200000)/100) * 4) + 5500;
		}
		else if (intAmount <= 500000)
		{
			intDuty = (((intAmount - 300000)/100) * 5.5) + 9500;
		}
		else if (intAmount <= 1000000)
		{
			intDuty = (((intAmount - 500000)/100) * 5.75) + 20500;
		}
		else
		{
			intDuty = (((intAmount - 1000000)/100) * 6.75) + 49250;
		}
	}
	//
	// Queensland
	//
	else if (strState == "QLD")
	{
		intMortgage = 100;
	
		// round up to the 10000
		if ((t_amount % 10000) != 0 )
		{
			intMod = 10000 - (t_amount % 10000);
			t_amount = t_amount + intMod;
		}
		
		if (t_amount <=180000)
		{
			intTransfer = 100;
		}
		else {
			intTransfer = (((t_amount - 180000)/10000) * 21.5)+100;
		}
		
		if(intLoanAmount > 70000)
		{
			intLoanDuty = (intLoanAmount - 70000) * 0.004;
		}
		else{
			intLoanDuty = 0;
		}
		if (intAmount <= 250000)
		{
			intDuty = (intAmount/100)*1;
		}
		else if (intAmount <= 500000)
		{
			intDuty = (((intAmount - 250000)/100) * 3.5) + 2500;
		}
		else
		{
			intDuty = (((intAmount - 500000)/100) * 3.75) + 11250;
		}	
	}
	//
	// South Australia
	//
	else if (strState == "SA")
	{
		intMortgage = 90.5;
	
		if (intAmount <= 5000)
	        intTransfer = 90.5;
	    else if (intAmount <= 20000)
	        intTransfer = 101;
	    else if (intAmount <= 40000)
	        intTransfer = 113;
	    else if (intAmount <= 50000)
	        intTransfer = 161;
	    else
	    {
			// round up to the 10000
			if ((t_amount % 10000) != 0 )
			{
				intMod = 10000-(t_amount % 10000);
				t_amount = t_amount + intMod;
			}
	        intTransfer = (((t_amount - 50000)/10000) * 50) + 161;
		}
		if (intAmount <= 12000) {
			intDuty = (intAmount/100)*1;
		}
	
		else if (intAmount <= 30000)
		{
			intDuty = (((intAmount - 12000)/100) * 2) + 120;
		}
	
		else if (intAmount <= 50000)
		{
			intDuty = (((intAmount - 30000)/100) * 3) + 480;
		}
	
		else if (intAmount <= 100000)
		{
			intDuty = (((intAmount - 50000)/100) * 3.5) + 1080;
		}
	
		else if (intAmount <= 200000)
		{
			intDuty = (((intAmount - 100000)/100) * 4) + 2830;
		}
		else if (intAmount <= 250000)
		{
			intDuty = (((intAmount - 200000)/100) * 4.25) + 6830;
		}
		else if (intAmount <= 300000)
		{
			intDuty = (((intAmount - 250000)/100) * 4.75) + 8955;
		}
		else if (intAmount <= 500000)
		{
			intDuty = (((intAmount - 300000)/100) * 5) + 11330;
		}
		else {
			intDuty = (((intAmount - 500000)/100) * 5.5) + 21330;
		}
		
		if (intLoanAmount <= 400)
		{
			intLoanDuty = 0;
		}
		else if (intLoanAmount <= 4000)
		{
			intLoanDuty = 10;
		}
		else if (intLoanAmount <= 10000)
		{
			intLoanDuty = (((intLoanAmount - 4000)/100) * 0.25) + 10;
		}
		else {
			intLoanDuty = (((intLoanAmount - 10000)/100) * 0.35) + 25;
		}
	}
	//
	// Tasmania
	//
	else if (strState == "TAS")
	{
		intMortgage = 85.50;
		intTransfer = 130;
		
		if (intAmount <= 1300) {
			intDuty = 20;
		}
		else if (intAmount <= 10000)
		{
			intDuty = (intAmount/100)*1.5;
		}
	
		else if (intAmount <= 30000)
		{
			intDuty = (((intAmount - 10000)/100) * 2) + 150;
		}
	
		else if (intAmount <= 75000)
		{
			intDuty = (((intAmount - 30000)/100) * 2.5) + 550;
		}
	
		else if (intAmount <= 150000)
		{
			intDuty = (((intAmount - 75000)/100) * 3) + 1675;
		}
	
		else if (intAmount <= 225000)
		{
			intDuty = (((intAmount - 150000)/100) * 3.5) + 3925;
		}
		
		else {
			intDuty = (((intAmount - 225000)/100) * 4) + 6550;
		}
		
		if (intLoanAmount <= 8000)
		{
			intLoanDuty = 20;
		}
		else if (intLoanAmount <= 10000)
		{
			intLoanDuty = (((intLoanAmount - 8000)/100) * 0.25) + 20;
		}
		else {
			intLoanDuty = (((intLoanAmount - 10000)/100) * 0.35) + 25;
		}
	}
	//
	// Western Australia 
	//
	else if (strState == "WA")
	{
		intMortgage = 75;
	
		if (intAmount <= 85000)
		{
			intTransfer = 77;
		}
		else if (intAmount <= 120000)
		{
			intTransfer = 87;
		}
		else if (intAmount <= 200000)
		{
			intTransfer = 107;
		}
		else
		{
			tempProperty = (intAmount - 200000) / 100000;
				
			k = tempProperty;
			
			intTransfer = (k * 107) + 107;
		}

		
		if (intAmount <= 80000)
		{	
			intDuty = (intAmount/100)*2.3;
		}
		else if (intAmount <= 100000)
		{
			intDuty = (((intAmount - 100000)/100) * 3.45) + 1840;
		}
		else if (intAmount <= 250000)
		{
			intDuty = (((intAmount - 100000)/100)*4.75) + 2530;
		}
		else if (intAmount <= 500000)
		{
			intDuty = (((intAmount - 250000)/100)*5.9) + 9655;
		}
		else 
		{
			intDuty = (((intAmount - 500000)/100)*6.3) + 24405;
		}
		
		if (intLoanAmount <= 35000)
		{
			intLoanDuty = intLoanAmount * 0.0025;
		}
		else
		{
			intLoanDuty = (((intLoanAmount - 35000)/100) * 0.4) + 87.5;
		}
		
	}
	//
	// Northern Territory 
	//
	else if (strState == "NT")
	{
		intMortgage = 90;
		intTransfer = 90;
		intLoanDuty = 0;
	
		if (intAmount <=500000)
		{
			newamount = intAmount / 1000;
			intDuty = (0.065 * Math.pow(newamount,2)) + (21 * newamount);
		}
		else
		{
			intDuty = 5.4 * (intAmount/100);
		}
	}
	//
	// Final output
	//
	stampDutyForm.txtDuty.value = Math.round(intDuty);
	stampDutyForm.txtLoanDuty.value = Math.round(intLoanDuty);
	stampDutyForm.txtMortgage.value = Math.round(intMortgage);
	stampDutyForm.txtTransfer.value = Math.round(intTransfer);
	stampDutyForm.txtTotal.value = Math.round(intDuty + intLoanDuty + intMortgage + intTransfer);
}