function ThreeDecimal(Num1) {
	var NewNum = Math.round(Num1 * 1000)/1000;
	return NewNum;

}

function BrowserStuffs() {
	var b = navigator.appName
	if (b=="Netscape") this.b = "ns"
	else if (b=="Microsoft Internet Explorer") this.b = "ie"
	else this.b = b
	this.version = navigator.appVersion
	this.v = parseInt(this.version)
	this.ns = (this.b=="ns" && this.v>=4)
	this.ns4 = (this.b=="ns" && this.v==4)
	this.ns5 = (this.b=="ns" && this.v==5)
	this.ie = (this.b=="ie" && this.v>=4)
	this.ie4 = (this.version.indexOf('MSIE 4')>0)
	this.ie5 = (this.version.indexOf('MSIE 5')>0)
	this.min = (this.ns||this.ie)
}

	is = new BrowserStuffs()

function carAllowance() {
	var Flag = 0;
	
	//Read in all the variables for manipulation
	var icost		 	= document.CarAllowance.cost.value;
	var iallowance 		= document.CarAllowance.allowance.value;
	var ilog 			= eval("document.CarAllowance.log.options[document.CarAllowance.log.selectedIndex].value");
	var iperiod 		= eval("document.CarAllowance.period.options[document.CarAllowance.period.selectedIndex].value");
	var margrate		= document.CarAllowance.MargTaxRate.value;
	var iKperY			= document.CarAllowance.KperY.value;
	var iBusK			= document.CarAllowance.BusK.value;
	var iPrivK			= document.CarAllowance.PrivK.value;
		
	if (icost == "") {
		alert("Please Enter the Cost of the Motor Car");
		document.CarAllowance.cost.focus();
		Flag = 1;
	} else {
		if (parseInt(icost) !=  icost || parseInt(icost) < 0) {
		alert("Please Enter an Positive Integer Cost of the Motor Car");
		document.CarAllowance.cost.focus();
		Flag = 1;
		} else {
			//Look up table
			if (icost >= 0 && icost <= 30000) {
				var fixed = 16916;
				var fuel = 0.231;
				var maintenance = 0.171;
			}
			if (icost >= 30001 && icost <= 35000) {
				var fixed = 18984;
				var fuel = 0.235;
				var maintenance = 0.173;
			}
			if (icost >= 35001 && icost <= 40000) {
				var fixed = 21051;
				var fuel = 0.238;
				var maintenance = 0.178;
			}
			if (icost >= 40001 && icost <= 45000) {
				var fixed = 23116;
				var fuel = 0.243;
				var maintenance = 0.185;
			}
			if (icost >= 45001 && icost <= 50000) {
				var fixed = 25197;
				var fuel = 0.248;
				var maintenance = 0.192;
			}
			if (icost >= 50001 && icost <= 55000) {
				var fixed = 27670;
				var fuel = 0.253;
				var maintenance = 0.199;
			}
			if (icost >= 55001 && icost <= 60000) {
				var fixed = 29778;
				var fuel = 0.255;
				var maintenance = 0.206;
			}
			if (icost >= 60001 && icost <= 70000) {
				var fixed = 33873;
				var fuel = 0.259;
				var maintenance = 0.213;
			}
			if (icost >= 70001 && icost <= 80000) {
				var fixed = 39102;
				var fuel = 0.261;
				var maintenance = 0.222;
			}
			if (icost >= 80001 && icost <= 90000) {
				var fixed = 40538;
				var fuel = 0.263;
				var maintenance = 0.227;
			}
			if (icost >= 90001 && icost <= 100000) {
				var fixed = 44535;
				var fuel = 0.265;
				var maintenance = 0.234;
			}
			if (icost >= 100001 && icost <= 110000) {
				var fixed = 48533;
				var fuel = 0.268;
				var maintenance = 0.241;
			}
			if (icost >= 110001 && icost <= 120000) {
				var fixed = 51110;
				var fuel = 0.275;
				var maintenance = 0.248;
			}
			if (icost >= 120001 && icost <= 130000) {
				var fixed = 54990;
				var fuel = 0.281;
				var maintenance = 0.255;
			}
			if (icost >= 130001 && icost <= 140000) {
				var fixed = 58803;
				var fuel = 0.289;
				var maintenance = 0.262;
			}
			if (icost >= 140001 && icost <= 150000) {
				var fixed = 62677;
				var fuel = 0.294;
				var maintenance = 0.269;
			}
			if (icost >= 150001) {
				//INCREMENT FIXED VALUE COST TO REFLECT CHANGES
				var COUNT = 0
				var LargeNumber = icost - 150000; //This Represents the large Cost of the Vehicle
				while (LargeNumber > 0) {
					COUNT = COUNT + 1;
					LargeNumber = LargeNumber - 10000;
				}				
				
				var fixed = 62677 + 3874 * COUNT;
				var fuel = 0.294;
				var maintenance = 0.269;
				
			}
			
			//alert(fixed);
			//alert(fuel);
			//alert(maintenance);
		}
	}
	
	if (iallowance == "") {
		//alert("Please Enter the Cost of the Motor Car");
		//document.CarAllowance.cost.focus();
		//var Flag = 1;
	} else {
		if (parseInt(iallowance) !=  iallowance || parseInt(iallowance) < 0) {
		alert("Please Enter an Positive Integer Motor Allowance");
		document.CarAllowance.allowance.focus();
		Flag = 1;
		} else {
			//alert("The Number is OK");
		}
	}
	
	if (ilog == "0") {
		alert("Please Select if you keep a log book");
		document.CarAllowance.log.focus();
		Flag = 1;
	}
	
	if (iperiod == "0") {
		alert("Please Select the period till the end of the Tax Year");
		document.CarAllowance.period.focus();
		Flag = 1;
	}
	
	if (margrate == "") {
		alert("Please Enter the Marginal tax rate");
		document.CarAllowance.MargTaxRate.focus();
		Flag = 1;
	} else {
		if (margrate > 100 || margrate < 0 || parseFloat(margrate) != margrate) {
		alert("Please Enter an Positive Marginal tax rate Between 0% and 100%");
		document.CarAllowance.MargTaxRate.focus();
		Flag = 1;
		} else {
			//alert("The Number is OK");
		}
	}
	
	if (ilog == "y") {
		if (iBusK == "") {
			alert("Please Enter the Number of Business Kilometers");
			document.CarAllowance.BusK.focus();
			Flag = 1;
		} else {
			if (parseInt(iBusK) !=  iBusK || parseInt(iBusK) < 0) {
			alert("Please Enter an Positive Integer for Business Kilometers");
			document.CarAllowance.BusK.focus();
			Flag = 1;
			} else {
			//alert("The Number is OK");
			}
		}
		if (iPrivK == "") {
			alert("Please Enter the Number of Private Kilometers");
			document.CarAllowance.PrivK.focus();
			var Flag = 1;
		} else {
			if (parseInt(iPrivK) !=  iPrivK || parseInt(iPrivK) < 0) {
			alert("Please Enter an Positive Integer for Private Kilometers");
			document.CarAllowance.PrivK.focus();
			Flag = 1;
			} else {
				//alert("The Number is OK");
			}
		}
		if (Flag != 1) {
			document.CarAllowance.KperY.value = iKperY = parseInt((parseInt(iPrivK) + parseInt(iBusK))*12/iperiod); 
		}
	}else if (ilog == "n") {
		if (iKperY == "") {
			alert("Please Enter the Total kms Travelled per Year");
			document.CarAllowance.KperY.focus();
			Flag = 1;
		} else {
			if (parseInt(iKperY) !=  iKperY || parseInt(iKperY) < 0) {
			alert("Please Enter an Positive Integer Number of Kilometers per year");
			document.CarAllowance.KperY.focus();
			Flag = 1;
			} else {
				//alert("The Number is OK");
			}
		}
		if (Flag != 1) {
			var totKmPeriod = iKperY/12*iperiod;
			var minPrivateMonth = 1166.667;
			var maxBussMonth = 1500.00;
			if (totKmPeriod > (minPrivateMonth*iperiod)) {
				document.CarAllowance.BusK.value = iBusK = totKmPeriod - (minPrivateMonth*iperiod);
				document.CarAllowance.PrivK.value = iPrivK = totKmPeriod - iBusK;
			}
			
			if (totKmPeriod < (minPrivateMonth*iperiod)) {
				document.CarAllowance.BusK.value = iBusK = 0;
				document.CarAllowance.PrivK.value = iPrivK = totKmPeriod;
			}
						
			if ( iBusK > (maxBussMonth*iperiod)) {
				document.CarAllowance.BusK.value = iBusK = Math.round((maxBussMonth*iperiod));
				document.CarAllowance.PrivK.value = iPrivK = Math.round(totKmPeriod - iBusK);
			}
		}
	}
		
	if (Flag != 1) {
		if (icost == "")
			icost=0;
		if (iallowance == "")
			iallowance=0;
		if (iperiod == "")
			iperiod=0;
		if (iKperY == "")
			iKperY=0;
		if (iBusK == "")
			iBusK=0;	
		if (iPrivK == "") {
			iPrivK=parseInt(iKperY)-parseInt(iBusK);
		}
		if (margrate == "")
			margrate=0;
		
		icost 			= parseInt(icost);
		iallowance 	= parseInt(iallowance);
		iperiod 		= parseInt(iperiod);
		iKperY 			= parseInt(iKperY);
		iBusK 			= document.CarAllowance.BusK.value = Math.round(iBusK);
		iPrivK 			= document.CarAllowance.PrivK.value = Math.round(iPrivK);
		fixed				= parseFloat(fixed);
		fuel				= parseFloat(fuel);
		maintenance	= parseFloat(maintenance);
		margrate		= parseFloat(margrate)/100;
		
		//Monthly Tax Deduction
		var MONTHLYDEDUCTION = iallowance * (0.5) * margrate;
		//alert('MONTHLYDEDUCTION:'+MONTHLYDEDUCTION);
		
		//Yearly Tax Deduction
		var PERIODDEDUCTION = MONTHLYDEDUCTION*iperiod;
		//alert('PERIODDEDUCTION:'+PERIODDEDUCTION);
		
		//THAT CONCLUDES THE DEDUCTIONS FOR THE PERIOD
		
		//Calculate Fixed Cost for Period
		var FCPeriod 	= (fixed/12)*(iperiod);
		//alert('FCPeriod:'+FCPeriod);
		var totKmPeriod = iKperY/12*iperiod;
		//Calculate the Fixed Cost Rate per KM
		var FCRate		=	(FCPeriod)/(totKmPeriod);
		FCRate 				= ThreeDecimal(FCRate);
		
		//Fuel and Maintenance values are stored in fuel & maintenance variables
		
		//Cost per Kilometer
		var CperK			= FCRate + fuel + maintenance;
		//alert('CperK:'+CperK);
		
		//Cost of Business KM's
		var BCtot			=	CperK*iBusK;
		BCtot = ThreeDecimal(BCtot);
		//alert('BCtot:'+BCtot);
				
		//Car Allowance Received
		var ALLRECEIVED = iallowance * iperiod;
		//alert('ALLOWANCERECEIVED:'+ALLRECEIVED);
		
		//Taxable Portion
		var TAXPORTION = ALLRECEIVED - BCtot;
		//alert('TAXPORTION:'+TAXPORTION);
		
		//Tax Payable on Taxible portion
		var TAXPAYABLE = TAXPORTION * margrate;
		if (TAXPAYABLE < 0) TAXPAYABLE = parseInt("0");
		//alert('TAXPAYABLE:'+TAXPAYABLE);
		
		//Tax Balance 
		var TAXBALANCE = PERIODDEDUCTION - TAXPAYABLE;
		//alert('TAXBALANCE:'+TAXBALANCE);
		
		//Optimum Car Allowence
		var TAXOPTIMUM = BCtot / iperiod;
		//alert('TAXOPTIMUM:'+TAXOPTIMUM);
		
		//Company Car tax for Period
		
		//Vehicle Price
		var VEHICLEPRICE = (icost/114)*100;
		
		//Monthly Benefit
		var MONTHLYBENEFIT = VEHICLEPRICE * (0.018);
		
		//Perks Tax Payable monthly by employee
		var PERKSTAXMONTH = MONTHLYBENEFIT * margrate;
		
		//Total Co. Car Tax Payable
		var TAXCOMPANY = PERKSTAXMONTH * iperiod
		//alert('TAXCOMPANY:'+ TAXCOMPANY);
		
		//Need to Correctly format the output numbers
		var iMONTHLYDEDUCTION 	= parseInt(MONTHLYDEDUCTION);
		var remMONTHLYDEDUCTION = MONTHLYDEDUCTION - iMONTHLYDEDUCTION;
		remMONTHLYDEDUCTION		= Math.round(remMONTHLYDEDUCTION*100);
		
		var iPERIODDEDUCTION 	= parseInt(PERIODDEDUCTION);
		var remPERIODDEDUCTION = PERIODDEDUCTION - iPERIODDEDUCTION;
		remPERIODDEDUCTION		= Math.round(remPERIODDEDUCTION*100);
		
		var iTAXPAYABLE 	= parseInt(TAXPAYABLE);
		var remTAXPAYABLE = TAXPAYABLE - iTAXPAYABLE;
		remTAXPAYABLE		= Math.round(remTAXPAYABLE*100);
		
		var iTAXBALANCE 	= parseInt(TAXBALANCE);
		var remTAXBALANCE = TAXBALANCE - iTAXBALANCE;
		remTAXBALANCE		= Math.round(remTAXBALANCE*100);
		
		if (parseFloat(BCtot) < parseFloat(ALLRECEIVED)) {
			var shortFall = (ALLRECEIVED-BCtot)/iperiod;
			//alert(shortFall);
			TAXOPTIMUM = parseFloat(shortFall) + parseFloat(iallowance); 	
			//alert("TAXOPTIMUM: " + TAXOPTIMUM);
		}
		var iTAXOPTIMUM 	= parseInt(TAXOPTIMUM);
		//alert("iTAXOPTIMUM: " + iTAXOPTIMUM);		
var remTAXOPTIMUM = TAXOPTIMUM - iTAXOPTIMUM;
		remTAXOPTIMUM		= Math.round(remTAXOPTIMUM*100);
		
		var iTAXCOMPANY 	= parseInt(TAXCOMPANY);
		var remTAXCOMPANY = TAXCOMPANY - iTAXCOMPANY;
		remTAXCOMPANY		= Math.round(remTAXCOMPANY*100);
		
		
		if (isNaN(iMONTHLYDEDUCTION) ||	isNaN(iPERIODDEDUCTION)	|| isNaN(iTAXPAYABLE)	|| isNaN(iTAXBALANCE)	|| isNaN(iTAXOPTIMUM)	|| isNaN(iTAXCOMPANY)	|| isNaN(remMONTHLYDEDUCTION)	|| isNaN(remPERIODDEDUCTION) || isNaN(remTAXPAYABLE) || isNaN(remTAXBALANCE) || isNaN(remTAXOPTIMUM)|| isNaN(remTAXCOMPANY)) {
			alert("The numbers you have selected equate to a value larger than the calculator can handle. ");
		} else {
		
			if (remMONTHLYDEDUCTION < 10) {
				remMONTHLYDEDUCTION = new String(remMONTHLYDEDUCTION);
				remMONTHLYDEDUCTION = "0" + remMONTHLYDEDUCTION;
			}
			if (remPERIODDEDUCTION < 10) {
				remPERIODDEDUCTION = new String(remPERIODDEDUCTION);
				remPERIODDEDUCTION = "0" + remPERIODDEDUCTION;
			}
			if (remTAXPAYABLE < 10 && remTAXPAYABLE > 0) {
				remTAXPAYABLE = new String(remTAXPAYABLE);
				remTAXPAYABLE = "0" + remTAXPAYABLE;
			}
			if (remTAXBALANCE < 10 && remTAXBALANCE > 0) {
				remTAXBALANCE = new String(remTAXBALANCE);
				remTAXBALANCE = "0" + remTAXBALANCE;
			}
			if (remTAXOPTIMUM < 10) {
				remTAXOPTIMUM = new String(remTAXOPTIMUM);
				remTAXOPTIMUM = "0" + remTAXOPTIMUM;
			}
			if (remTAXCOMPANY < 10) {
				remTAXCOMPANY  = new String(remTAXCOMPANY );
				remTAXCOMPANY  = "0" + remTAXCOMPANY ;
			}
		
			document.CarAllowance.TaxDeductM.value=iMONTHLYDEDUCTION + "." + remMONTHLYDEDUCTION;
			document.CarAllowance.TaxDeductY.value=iPERIODDEDUCTION + "." + remPERIODDEDUCTION;
			
			if (iTAXPAYABLE > 0) {
				document.CarAllowance.TaxDeductP.value=iTAXPAYABLE + "." + remTAXPAYABLE;
			}
			else { 
				iTAXPAYABLE = parseInt("0");
				document.CarAllowance.TaxDeductP.value=iTAXPAYABLE;
			}
						
			if (iTAXBALANCE > 0)
				document.CarAllowance.TaxBalance.value=iTAXBALANCE + "." + remTAXBALANCE;
			else
				document.CarAllowance.TaxBalance.value="("+(-1)*iTAXBALANCE + "." + (-1)*remTAXBALANCE +")";
			
			if (parseFloat(BCtot) < parseFloat(ALLRECEIVED)) { 
				iTAXOPTIMUM = "******";
				document.CarAllowance.TaxOptimum.value=iTAXOPTIMUM;
			}
			else {
				document.CarAllowance.TaxOptimum.value=iTAXOPTIMUM + "." + remTAXOPTIMUM;		
			}
			document.CarAllowance.TaxCompany.value=iTAXCOMPANY + "." + remTAXCOMPANY ;
			
		}
	}	
}

function Alternate() {
//Some of these objects are not supported by Netscape Navigator v 3 
/*if (is.ns) {
	KMEbrowser = document.layers.KME;
	KMAbrowser = document.layers.KMA;
} else {
	KMEbrowser = this.KME.style;
	KMAbrowser = this.KMA.style;
}

var tmp = eval("document.CarAllowance.log.options[document.CarAllowance.log.selectedIndex].value");

	if (tmp=="y") {
		KMEbrowser.left = (is.ns)? "428" : "425"
		KMEbrowser.top = (is.ns)? "220" : "242"
		KMEbrowser.visibility = (is.ns)? "show" : "visible"
		KMAbrowser.visibility = (is.ns)? "hide" : "hidden"
	} else {
		KMAbrowser.left = (is.ns)? "428" : "425"
		KMAbrowser.top = (is.ns)? "220" : "242"
		KMEbrowser.visibility = (is.ns)? "hide" : "hidden"
 		KMAbrowser.visibility = (is.ns)? "show" : "visible"
 	}*/
}
