/* =======================================================================
This JavaScript file is used for calculating Stamp Duty for the Department of Treasury  Finance - Tasmania, Australia

Version 9, Craig Vertigan, June 2006

========================================================================*/

var f;

//  to return:  25c per $100 or part thereof of $n,  call:  perChunk($n, 100, 0.25)
function perChunk(n, chunk, multiplier){
	n = Math.ceil( n / chunk) * chunk;  // round up n to the nearest chunk
	var qty = eval(n / chunk); // qty is the quantity of chunks in the amount (n)
	return eval(qty * multiplier);
}

// 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;
}

// reset theField
function doReset(theField){
	f = document.forms["stampDuty"];
	f[theField].value = "";
	f[theField + "Result"].value = "";
	if (theField=="Mortgage")
	{
		f[theField + "Date"].value = "";
	}
}

// validate; execute the handler for theField; set decimal result at 2
function doCalc(theField){
	//var f = document.forms[0];
	f = document.forms["stampDuty"];
	var n = f[theField].value;
	// check that there was something entered in the field
	if ( n == "" ) {
		alert ("Please type a number into the " + theField + " field.");
		f[theField].select(); return;
	}
	// check that it was a number entered
	if ( isNaN(n) ) {
		alert ("Please check your typing,\ryou have entered a non-numeric value.");
		f[theField].select(); return;
	}
	// pass the number to the appropriate function
	var theResult = eval( "do" + theField + "(" + n + ")");
	// fill the result field with the result to 2 decimal places
	//f[theField + "Result"].value = decimal2(theResult);
	f[theField + "Result"].value = "$" + theResult.toFixed(2);
}

function doBill(n){
	if(n<8001){ return 20; }
	if(n<10001){ return (20 + perChunk(n - 8000, 100, .25)); }
	return (25 + perChunk(n - 10000, 100, .35));
}

function doConveyance(n){
	if(n<=1300){ return 20;}
	if(n<=10000){ return perChunk(n, 100, 1.5);}
	if(n<=30000){ return eval(150 + perChunk(n-10000, 100, 2));}
	if(n<=75000){ return eval(550 + perChunk(n-30000, 100, 2.5));}
	if(n<=150000){ return eval(1675 + perChunk(n-75000, 100, 3));}
	if(n<=225000){ return eval(3925 + perChunk(n-150000, 100, 3.5));}
	return eval(6550 + perChunk(n-225000, 100, 4));
}

function doCreditArrangement(n){
	if(n<101){ return 0;}
	if(n<4001){ return eval(n * .02);}
	return eval(Math.min(n * .02, 4000));
}

function doCreditPurchase(n){
	if(n<101){ return 0;}
	if(n<4001){ return eval(n * .02);}
	return eval(Math.min(n * .02, 4000));
}

function doHirePurchase(n){
	if(n<101){ return 0;}
	if(n<4001){ return eval(n * .02);}
	return eval(Math.min(n * .02, 4000));
}

function doHireGoods(n) {
	var max = 10000;
	var duty = eval(n * 0.02);
	if ( duty>max ) {
   	return max;
	}
	else {
   	return eval(n * 0.02);
	}
}

function doLeaseInstruments(n) {
	if(n<=3000){return "nil";}
	if(n<=5714){ return 20;}
	return eval(n * 0.0035);
	//return perChunk(n, 100, 0.35);
}

function doLifeInsurance(n){
	if (n<=2000) { return ( perChunk(n, 200, 0.1)); }
	return (1 + perChunk(n-2000, 200, 0.2));
}

function doGeneralInsurance(n){
	return eval(n * 0.08);
}

function doTempInsurance(n) {
	return(n * 0.05);
}

function doMortgageInsurance(n) {
	return (n * 0.02);
}

function doMortgage(n){
//do check for date first
	var mortDate;
	var newRateDate;
	var endDate;
	if (f["MortgageDate"].value=="")
	{
		alert("you need to enter a date for the mortgage");	
		return false;
	}
	else
	{
		if(! isValidDate(f["MortgageDate"].value))
		{
			alert("Please enter the mortgage date in this format dd/mm/yyyy");
			return "";
		}
		newRateDate = new Date(2006,6,1);
		endDate = new Date(2007,6,1);
		mortDate = new Date(year, month-1, day);
		//now do calc based on the date
		if (mortDate < newRateDate)
		{
			if(n<8001){ return 20;}
			if(n<10001){ return  (20 + ((n - 8000) * .0025));}
			return (25 + ((n - 10000) * .0035));
		}
		if (mortDate >= endDate)
		{
			alert("Mortgage duty will be completely abolished from 1 July 2007");
			return 0;
		}
		else
		{
			if(n <= 10000){ return 20;}
			{
			return (20 + ((n - 10000) * .00175));
			}
		}
	}
}


function doVehicle(n){
	f = document.forms["stampDuty"];
	if(n<=600 && f.VehicleType[2].checked == false)
		{ return 20;}
	else if (n<=2000 && f.VehicleType[2].checked == true)
		{ return 20;}
	else if (f.vehicleFleetDiscount[0].checked == true && f.VehicleType[2].checked == false) // then the Yes button is checked or unchecked
	{
	// Fleet Discount vehicle
		return perChunk(n, 100, 3.5);
		
	}
	
	else if (f.VehicleType[0].checked == true) // passenger vehicle
	{
	// Passenger vehicle
		if(n<=35000){ return perChunk(n, 100, 3);};
		if(n<=40000){return eval(1050 + perChunk(n-35000, 100, 11));};
		return perChunk(n, 100, 4);
	}
		
	else if (f.VehicleType[1].checked == true) // weight under 4.5 tonnes GVM
	{
	// Commercial Vehicle
		return perChunk(n, 100, 3);
	}
	
	else if (f.VehicleType[2].checked == true) // weight over 4.5 tonnes GVM
	{
	// Commercial Vehicle
		return perChunk(n, 100, 1);
	}
	
	else
	{
	
	}
}

function doLandTax(n){
	var r = 0;
	if(n<1001){ return 0;}
	if(n<15002){ return 25;}
	if(n<40001){ return eval(25 + ( n - 15000) * .0075 );}
	if(n<68751){ return eval(212.50 + ( n - 40000) * .01 );}
	if(n<100001){ return 500;}
	if(n<125001){ return eval(500 + ( n - 100000) * 125 / 10000 );}
	if(n<170001){ return eval(812.50 + ( n - 125000) * 15 / 1000 );}
	if(n<210001){ return eval(1487.50 + ( n - 170000) * 175 / 10000 );}
	if(n<250001){ return eval(2187.50 + ( n - 210000) * 2 / 100 );}
	if(n<500001){ return eval(2987.50 + ( n - 250000) * 225 / 10000 );}
	return eval(8612.50 + ( n - 500000) * 25 / 1000 );
}

function doMarkSecs(n){
	return perChunk(n, 100, 0.6);
}

var year,month,day; // these are global so that they can be used in isValidDate and isDateInPast
function isValidDate(dateStr)
{
// Checks for the following valid date formats:
// DD/MM/YYYY DD-MM-YYYY
// Also separates date into month, day, and year variables

	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

	var matchArray = dateStr.match(datePat); // is the format ok?
	if (matchArray == null) 
	{
		return false;
	}
	month = matchArray[3]; // parse date into variables
	day = matchArray[1];
	year = matchArray[4];
	if (month < 1 || month > 12) 
	{ // check month range
		return false;
	}
	if (day < 1 || day > 31) 
	{
		return false;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) 
	{
		return false
	}
	if (month == 2) 
	{ // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day>29 || (day==29 && !isleap)) 
		{
			return false;
		}
	}
	return true; // date is valid
}