
function select_item(name, value) {
	this.name = name;
	this.value = value;
}

function get_selection(select_object) {
	contents = new select_item();
	for(var i=0;i<select_object.options.length;i++) {
		if(select_object.options[i].selected == true) {
			contents.name = select_object.options[i].text;
			contents.value = select_object.options[i].value;
		}
	}
	return contents;
}

function StripChars(strIn) {
	var newstr = "";
	var Chars = "0123456789.";
    for (var i = 0; i < strIn.length; i++) {
       if (Chars.indexOf(strIn.charAt(i)) == -1) {}
	   else {
	   	newstr = newstr + strIn.charAt(i);
	   }
    }
	return newstr;
}

function stampduty(formfield) {
	var amount = StripChars(formfield.amount.value);
	var loanamount = StripChars(formfield.loanamount.value);
	var state = get_selection(formfield.state);
	var type = "OO"
	for (i=0; i< formfield.elements.length; i++) {
		if (formfield.elements[i].name == 'Type') {
			for (s=0;s < 2; s++) {
				if (formfield.Type[s].checked == true) {
					type = formfield.Type[s].value;
				}
			}
		}
	}
	if ((amount == "") && (loanamount == "")) {
		window.alert("You have not entered any values!");
		return;
	}
	t_amount = amount;
	if ((amount % 100) != 0 ) {
			var mod = eval(100-(amount % 100));
			amount = -(-amount - mod);
	}
	
	if ((loanamount % 100) != 0 ) {
			var mod2 = eval(100-(loanamount % 100));
			loanamount = -(-loanamount- mod2);
	}
	
//********************** Victoria *************************
	if (state.value == "VIC") {

		if ((t_amount % 1000) != 0 ) {
			var mod = eval(1000-(t_amount % 1000));
			t_amount = -(-t_amount - mod);
		}
		if ((t_amount > 0) && (t_amount <= 500000)) {
			var transfer = (t_amount / 1000) * 2.46 + 90;
		}
		else {
			var transfer = 1320;
		}
		mortgage = 59;
		var exnotes = "First Home Bonus\n- First home buyers are eligible for a $5,000 bonus if consideration of the property does not exceed $500,000 and if transaction takes place between 1st May 2004 and 30th June 2005.\n\nFirst Home Buyers with Families\n- Must have at least 1 dependant child (under 18 and under control of applicant).\n- Full stamp duty exemption for properties valued up to $150,000.\n- Stamp duty concessions on a reducing scale for properties between $150,000 to $200,000.\n- All applicants must have not previously owned a home.\n\nHome Buyers with Concession Cards\n- Full stamp duty exemption for properties valued up to $250,000\n- Stamp duty concessions on a reducing scale for properties between $250,000 to $350,000\n- Have a Pensioner Concession Card (Department of Social Security or Department of Veterans' Affairs), Veterans' Affairs Gold Card, Health Benefits Card, or Health Care Card.";

		if (amount <= 20000) {
			var duty = eval(((amount)/100)*1.4);
	
		}
		else if ((amount > 20000) && (amount <= 115000)) {
			var duty = eval((((amount - 20000)/100)*2.4) + 280);
		}
	
		else if ((amount > 115000) && (amount <= 870000)) {
			var duty = eval((((amount - 115000)/100)*6) + 2560);
		}
		
		else {
			var duty = eval(((amount)/100)*5.5) ;
		}
		if ((loanamount % 200) != 0 ) {
			var mod = eval(200-(loanamount % 200));
			loanamount = -(-loanamount- mod);
		}
/*
		No more loan duty as of 1/7/2004
		
		if (loanamount <= 10000) {
			var loanduty = 4;
		}
		else {
			var loanduty = eval((((loanamount - 10000)/200)*0.8) + 4);
		}
*/
		var loanduty = 0;
		
	}

//********************** New South Wales *************************
	else if (state.value == "NSW") {
		var mortgage = 75;
		var transfer = 75;
		var exnotes = "First Home Plus Scheme\n- Full stamp and mortgage duty exemption for homes valued up to $500,000 in NSW\n- Stamp and mortgage duty concessions on a reducing scale for homes between $500,000 and $600,000 in NSW.\n- First home buyers purchasing vacant block of residential land will pay no stamp duty on land valued up to $300,000.\n- Discounts are available on stamp duty between $300,000 and $450,000.";	
	
		if (amount <= 14000) {
			var duty = eval((amount/100)*1.25);
		}
	
		else if ((amount > 14000) && (amount <= 30000)) {
			var duty = eval((((amount - 14000)/100)*1.5) + 175);
		}
	
		else if ((amount > 30000) && (amount <= 80000)) {
			var duty = eval((((amount - 30000)/100)*1.75) + 415);
		}
	
		else if ((amount > 80000) && (amount <= 300000)) {
			var duty = eval((((amount - 80000)/100)*3.5) + 1290);
		}
	
		else if ((amount > 300000) && (amount <= 1000000)) {
			var duty = eval((((amount - 300000)/100)*4.5) + 8990);
		}
		
		else {
			var duty = eval((((amount - 1000000)/100)*5.5) + 40490);
		}
		if ((loanamount % 1000) != 0 ) {
			var mod = eval(1000-(loanamount % 1000));
			loanamount = -(-loanamount- mod);
		}
		if (loanamount <= 16000) {
			var loanduty = 5;
		}
		else {
			var loanduty = eval((((loanamount - 16000)/1000)*4) + 5);
		}
		
	}

//********************** ACT *************************
	else if (state.value == "ACT") {
		var mortgage = 82;
		var transfer = 160;
		var loanduty = 0;
		var exnotes = "Home Buyer Concession\n- Eligibility is determined by income and the number of dependent children.\n- Minimum duty ($20) payable on properties valued less than $273,000. Concessional duty payable for properties valued between $273,000 and $375,000.\n- Minimum duty ($20) payable on vacant block valued less than $107,000. Concessional duty payable for vacant block valued between $107,000 and $158,000.";	
	
		if (amount <= 100000) {
			var duty = Math.max(Math.ceil(eval(amount/100))*2, 20);
		}
		else if (amount <= 200000) {
			var duty = Math.ceil(eval((amount-100000)/100))*3.5 + 2000;
		}
		else if (amount <= 300000) {
			var duty = Math.ceil(eval((amount-200000)/100))*4 + 5500;
		}
		else if (amount <= 500000) {
			var duty = Math.ceil(eval((amount-300000)/100))*5.5 + 9500;
		}
		else if (amount <= 1000000) {
			var duty = Math.ceil(eval((amount-500000)/100))*5.75 + 20500;
		}
		else {
			var duty = Math.ceil(eval((amount-1000000)/100))*6.75 + 49250;
		}
	}

//********************** Queensland *************************
	else if (state.value == "QLD") {
		if ((t_amount % 10000) != 0 ) {
			var mod = eval(10000-(t_amount % 10000));
			t_amount = -(-t_amount - mod);
		}
		var transfer = 105.6;
		if (t_amount > 180000) transfer += Math.ceil(eval((t_amount-180000)/10000))*22.6;

		var mortgage = 105.6;
		
		if (type == 'OO') {
			var exnotes = "First Home Buyers' Rebate\n- Full stamp and mortgage duty exemption for properties valued up to $250,000.\n- Stamp duty rebate applies on a reducing scale for properties valued between $250,000 and $500,000.";
		
			if(loanamount > 70000) loanduty = Math.ceil(eval((loanamount - 70000)/100)) * 0.40;
			else loanduty = 0;
/*						
			if (amount <= 250000) {
				var duty = eval((amount/100)*1);
			}
			else if ((amount > 250000) && (amount <= 500000)) {
				var duty = eval((((amount - 250000)/100)*3.5) + 2500);
			}
			else {
				var duty = eval((((amount - 500000)/100)*3.75) + 11250);
			}
*/

			if (amount <= 80000) var duty = 800;
			else if (amount <= 300000) var duty = amount*0.01;
			else if (amount <= 500000) var duty = 3000 + Math.ceil(eval((amount - 300000)/100)) * 3.5;
			else var duty = 11250 + Math.ceil(eval((amount - 500000)/100)) * 3.75;
				
		}
		else {
			var exnotes = "";
			
			loanduty = Math.ceil(eval(loanamount/100)) * 0.40

			if (amount <= 20000) var duty = Math.ceil(eval(amount/100)) * 1.5;
			else if (amount <= 50000) var duty = 300 + Math.ceil(eval((amount - 20000)/100)) * 2.25;
			else if (amount <= 100000) var duty = 975 + Math.ceil(eval((amount - 50000)/100)) * 2.75;
			else if (amount <= 250000) var duty = 2350 + Math.ceil(eval((amount - 100000)/100)) * 3.25;
			else if (amount <= 500000) var duty = 7225 + Math.ceil(eval((amount - 250000)/100)) * 3.5;
			else var duty = 15975 + Math.ceil(eval((amount - 500000)/100)) * 3.75;
/*
			
			if (amount <= 20000) {
				var duty = Math.ceil(eval(amount/100))*1.5;
			}
			else if ((amount > 20000) && (amount <= 50000)) {
				var duty = Math.ceil(eval((((amount - 20000)/100)*2.25) + 300);
			}
			else if ((amount > 50000) && (amount <= 100000)) {
				var duty = eval((((amount - 50000)/100)*2.75) + 975);
			}
			else if ((amount > 100000) && (amount <= 250000)) {
				var duty = eval((((amount - 100000)/100)*3.25) +2350);
			}
			else if ((amount > 250000) && (amount <= 500000)) {
				var duty = eval((((amount - 250000)/100)*3.5) + 7225);
			}
			else {
				var duty = eval((((amount - 500000)/100)*3.75) + 15975);
			}
*/
		}
		
	}

//********************** South Australia *************************
	else if (state.value == "SAN") {
		if (amount <= 5000)
	        transfer = 98;
	    if (amount > 5000 && amount <= 20000)
	        transfer = 109;
	    if (amount > 20000 && amount <= 40000)
	        transfer = 121;
	    if (amount > 40000)
	    { 
			transfer = Math.ceil(eval((amount - 50000)/10000)*54) + 174;
		}
		mortgage = 98;

		var exnotes = "First Home Buyers' Concession\n- Full stamp duty exemption for properties valued up to $80,000.\n- Stamp duty concessions on a reducing scale for properties between $80,000 and $250,000.\n- Property must be first home and intended as principal residence.\n\nMortgage Duty Exemption for first home buyers\n- An exemption from stamp duty on mortgages will be provided to an eligible first home owner entered into on or after 27th May 2004.\n- No limit on the value of first home loans eligible for the exemption from stamp duty on mortgages.";
		
		// Round up to next $100
		amount = Math.ceil(amount/100.0)*100;
		
		if (amount <= 12000) {
			var duty = eval((amount/100.0)*1);
		}
	
		else if ((amount > 12000) && (amount <= 30000)) {
			var duty = eval((((amount - 12000)/100)*2) + 120);
		}
	
		else if ((amount > 30000) && (amount <= 50000)) {
			var duty = eval((((amount - 30000)/100)*3) + 480);
		}
	
		else if ((amount > 50000) && (amount <= 100000)) {
			var duty = eval((((amount - 50000)/100)*3.5) + 1080);
		}
	
		else if ((amount > 100000) && (amount <= 200000)) {
			var duty = eval((((amount - 100000)/100)*4) + 2830);
		}
		else if ((amount > 200000) && (amount <= 250000)) {
			var duty = eval((((amount - 200000)/100)*4.25) + 6830);
		}
		else if ((amount > 250000) && (amount <= 300000)) {
			var duty = eval((((amount - 250000)/100)*4.75) + 8955);
		}
		else if ((amount > 300000) && (amount <= 500000)) {
			var duty = eval((((amount - 300000)/100)*5) + 11330);
		}
		else {
			var duty = eval((((amount - 500000)/100)*5.5) + 21330);
		}
		
		loanamount = Math.ceil(loanamount/100)*100;
		
		if (loanamount <= 400) {
			var loanduty = 0;
		}
		else if ((loanamount > 400) && (loanamount <= 6000)) {
			var loanduty = 10;
		}
		else {
			if (type == "OO") var loanduty = eval((((loanamount - 6000)/100)*0.35) + 10);
			else var loanduty = eval((((loanamount - 6000)/100)*0.45) + 10);
		}
	}

//********************** Tasmania *************************
	else if (state.value == "TAS") {
		var mortgage = 85.50;
		var transfer = 131;
		var exnotes = "First Home Owner's Rebate\n- Homes purchased between 20th May 2004 and 30th June 2005 will receive up to $4,000 stamp duty relief for homes purchased for less than $350,000.\n- Concessional rate of duty does not apply to the purchase of land unless it is part of a house and land package.\n- Property must be first home and intended as principal residence.";
		if (amount <= 1300) {
			var duty = 20;
		}
		else if ((amount > 1300) && (amount <= 10000)) {
			var duty = eval((amount/100)*1.5);
		}
	
		else if ((amount > 10000) && (amount <= 30000)) {
			var duty = eval((((amount - 10000)/100)*2) + 150);
		}
	
		else if ((amount > 30000) && (amount <= 75000)) {
			var duty = eval((((amount - 30000)/100)*2.5) + 550);
		}
	
		else if ((amount > 75000) && (amount <= 150000)) {
			var duty = eval((((amount - 75000)/100)*3) + 1675);
		}
	
		else if ((amount > 150000) && (amount <= 225000)) {
			var duty = eval((((amount - 150000)/100)*3.5) + 3925);
		}
		
		else {
			var duty = eval((((amount - 225000)/100)*4) + 6550);
		}
		
		if (loanamount <= 8000) {
			var loanduty = 20;
		}
		else if ((loanamount > 8000) && (loanamount <= 10000)) {
			var loanduty = eval((((loanamount - 8000)/100)*0.25) + 20);
		}
		else {
			var loanduty = eval((((loanamount - 10000)/100)*0.35) + 25);
		}
	}

//********************** Western Australia *************************
	else if (state.value == "WAN") {
		if (amount <= 85000) {
			var transfer = 77;
		}
		else if ((amount > 85000) && (amount <= 120000)) {
			var transfer = 87;
		}
		else if ((amount > 120000) && (amount <= 200000)) {
			var transfer = 107;
		}
		else {
			// Huh?! All it needs is the ceiling operator... - MC
			//tempProperty = (amount - 200000) / 100000;
			//for (var i=0, k=1; tempProperty >i && tempProperty > k ; i++, k++);
			//var transfer = (k * 20) + 107;
			
			var transfer = Math.ceil(eval((amount - 200000)/100000))*20 + 107;
			
		}
		
		mortgage = 77;
		var exnotes = "Concessional Rate\n-Applies to purchase of principal place of residence or business undertaking with a dutiable value not exceeding $200,000.\nThe concessional rate is $1.50 per $100 (or part thereof) up to $100,000 and $5.42 per $100 (or part thereof) for that amount between $100,001 and $200,000.\n\nFirst Home Owner's Rebate\n- Property must be first home and intended as principal residence.\n- First home buyers will get a full exemption from conveyance duty on homes up to $220,000\n- Homes valued between $220,000 and $300,000 will receive a partial exemption.\n- First home purchasers buying vacant land and building a home will receive full rebate on land values to $100,000 and a partial rebate on land values to $150,000.";
		
		if (type == 'OO' && amount <= 200000) {
			if (amount <= 100000) {	
				var duty = Math.ceil(eval(amount/100))*1.5;
			}
			else {
				var duty = Math.ceil(eval((amount - 100000) / 100)) *5.42 + 1500;
			}
		}
		else {
			if (amount <= 80000) {	
				var duty = Math.ceil(eval(amount/100))*2.2;
			}
		
			else if ((amount > 80000) && (amount <= 100000)) {
				var duty = Math.ceil(eval((amount - 80000)/100))*3.30 + 1760;
			}
		
			else if ((amount > 100000) && (amount <= 250000)) {
				var duty = Math.ceil(eval((amount - 100000)/100))*4.5 + 2420;
			}
		
			else if ((amount > 250000) && (amount <= 500000)) {
				var duty = Math.ceil(eval((amount - 250000)/100))*5.6 + 9170;
			}
		
			else {
				var duty = Math.ceil(eval((amount - 500000)/100))*6 + 23170;
			}
		}

        var loanduty = 20;

		if (type =="OO") {
		    if (loanamount > 8000) loanduty += Math.ceil(eval((loanamount-8000)/100)) * 0.25;
		}
		else {
			if (loanamount > 5000) loanduty += Math.ceil(eval((loanamount-5000)/100)) * 0.40;
		}
	}

//********************** Northern Territory  *************************
	else if (state.value == "NT") {
		var mortgage = 90;
		var transfer = 90;
		var loanduty = 0;

		var exnotes = "First Home Owner's Concession\n- If property is your first home anywhere in Australia, then you may be eligible for a concession of up to $3,640.60 on the total stamp duty payable.\n- Property may be an existing home or land to build a new home.\n\nPrincipal Place of Residence Rebate\n- If property is your principal place of residence you may be eligible for a stamp duty rebate of up to $1,500."
		
		// A rebate of up to $3,640.60 off the total stamp duty payable\n- Property must be first home and intended as principal residence"
	
		if (amount <=500000) {
			newamount = eval(amount / 1000);
			var duty = eval((0.065 * Math.pow(newamount,2)) + (21 * newamount));
		}
	
		else {
			var duty = eval(0.054 * amount);
		}
		if (type =="OO") {
			duty = duty - 1500;
			if (duty < 0) duty = 0;
		}
	}
	total = eval(duty + loanduty + mortgage + transfer);
	total = rounding(total);
	duty = rounding(duty);
	loanduty = rounding(loanduty);
	mortgage = rounding(mortgage);
	transfer = rounding(transfer);
	
	formfield.duty.value = duty;
	formfield.loanduty.value = loanduty;
	formfield.mortgage.value = mortgage;
	formfield.transfer.value = transfer;
	formfield.total.value = total;
	formfield.notes.value = exnotes;
}
function rounding(n) {
	pennies = n * 100;
	pennies = Math.round(pennies);
	strPennies = "" + pennies;
	len = strPennies.length;
	return strPennies.substring(0, len - 2) + "." + strPennies.substring((len - 2), len);
}
