// version 4 of Land Tax calculator
// Department of Treasury & Finance, Tasmania, Australia
// Craig Vertigan, 02-06-2005

//round number to nearest 2 decimal places
function roundCents(n)
{
	return Math.round(n*100)/100;
}

//convert num into $ format
function fDollars(amount){
	//var tempstring = "$" + Math.round(amount*100)/100;
	var tempstring = "$" + roundCents(amount);
	if(tempstring.indexOf(".") == -1){
		tempstring += ".00";}
	else{
	if(tempstring.indexOf(".") == tempstring.length -2){
		tempstring += "0"}
	}
	return tempstring;
}

// return the given string with no more than two decimals
function decimal2(s){
	if (s.indexOf(".") == -1){ return s;};
	var decimals = (s.length) - (s.indexOf(".")) -1;  // -1 because of the decimal point
	// if more than two decimals delete the others
	if( decimals > 2) {
		s = s.substring(0, s.length - decimals +2)
	}
	// if only one decimal place add another zero to make it look better
	if (decimals == 1) {
   	s = s + "0"
   }
	return s;
}

// validate; execute the handler for theField; set decimal result at 2
function doLandCalc(theField, landTaxTotalOwed){
	var f = document.forms["landTax"];
	var n = f[theField].value;
	if ( n == "" ) {
		alert ("Please type a number into the " + theField.name + " field.");
		f[theField].select(); return;
	}
	if ( isNaN(n) ) {
		alert ("Please check your typing,\ryou have entered a non-numeric value.");
		f[theField].select(); return;
	}
	
	var theResult = roundCents(doLandTax(n));
	f[landTaxTotalOwed].value = theResult;
	f[theField + "Result"].value = fDollars(theResult);
}

function doLandTax(n){
	var r = 0;
	if(n<25000){ return "0";};
	if(n<349999){ return eval(50 + ( n - 25000) * .0055 );};
	if(n<749999){ return eval(1837.50 + ( n - 350000) * .02 );};
	return eval(9837.50 + ( n - 750000) * .025 );
}

// This is a new bit of code that is to calculate individual duty owed for a person that owns multiple properties
function doIndividualPropertyLandTax(landTaxTotalOwed, individualValue, combinedValue){
	var f = document.forms["landTax"];
	var LTTO = f[landTaxTotalOwed].value;
	var IV = f[individualValue].value;
	var CV = f[combinedValue].value;

	if ( IV == "") {
		alert ("Please type a number into the Individual Land Value field.");
		f[individualValue].select(); return;
	}
	if ( CV == "") {
		alert ("Please complete the above tax on total land value form \r before doing the multiple land holder form.");
		f.landTax.select(); return;
	}
	if ( Number(IV) > Number(CV)) {
		alert ("Error: individual land value can not be greater than the total land value.");
		f[individualValue].select(); return;
	}
	if ( isNaN(IV) ) {
		alert ("Please check your typing,\ryou have entered a non-numeric value.");
		f[individualValue].select(); return;
	}

	var theResult = eval(LTTO * IV / CV);
	f["individualValueResult"].value =  fDollars(theResult);
}

// reset Mutiple property fields only
function doResetMultipleLand(landTaxTotalOwed, individualValue, combinedValue, individualValueResult){
	var f = document.forms["landTax"];
	f[individualValue].value = "";
	f[individualValueResult].value = "";
}
