// JavaScript Document

function ShowElementByID(id) {
	if (document.getElementById) { // DOM3 = IE5, NS6
		if(document.getElementById(id).style.display == "none"){
			document.getElementById(id).style.display = 'block';
		}
	}
	else{
		if(document.layers){
			if(document.id.display == "none"){ 
				document.id.display = 'block'; 
			}
		}
		else
		{
			if(document.all.id.style.visibility == "none"){
				document.all.id.style.visibility = 'block';
			}
		}
	}
};;

function HideElementByID(id) {
	if (document.getElementById) { // DOM3 = IE5, NS6
		if(document.getElementById(id).style.display == "block"){
			document.getElementById(id).style.display = 'none';
		}
	}
	else{
		if(document.layers){
			if(document.id.display == "block"){ 
				document.id.display = 'none'; 
			}
		}
		else
		{
			if(document.all.id.style.visibility == "block"){
				document.all.id.style.visibility = 'none';
			}
		}
	}
};;


function calcCarEC(){
	var ec = 0;
	var ecValue = 0;
	var road_tax = 0;
	var mc_ecValue=0;
	var mc_ec = 0;

	for (var i=0; i < document.Calculator.ecRB.length; i++){
		if (document.Calculator.ecRB[i].checked){
			ecValue = document.Calculator.ecRB[i].value;
		}
	}
	
	ShowElementByID ('resultTable');
	//document.all('resultTable').style.visibility = "visible";
	
	if (document.Calculator.amtTB.value == "") {
		document.Calculator.amtTB.focus();
		document.Calculator.result.value = "Error: Please fill in Engine Capacity (cc).";
		document.Calculator.result2.value = "";
		return false;
  	}
	else{
		ec = parseInt(document.Calculator.amtTB.value);
		
		// 600 < EC <= 1000
		if (ecValue == 2){
			if (ec > 600 && ec <= 1000){
				road_tax = (200 + parseFloat(0.125 * (ec - 600))) * 0.782;
			}
			else{
				document.Calculator.amtTB.value = "";
				document.Calculator.amtTB.focus();
				document.Calculator.result.value = "Error: Please select the correct engine capacity.";
				document.Calculator.result2.value = "";
				return false;
			}
		}
		// 1000 < EC <= 1600
		else if (ecValue == 3){
			if (ec > 1000 && ec <= 1600){
				road_tax = (250 + parseFloat(0.375 * (ec - 1000))) * 0.782;
			}
			else{
				document.Calculator.amtTB.value = "";
				document.Calculator.amtTB.focus();
				document.Calculator.result.value = "Error: Please select the correct engine capacity.";
				document.Calculator.result2.value = "";
				return false;
			}
		}
		// 1600 < EC <= 3000
		else if (ecValue == 4){
			if (ec > 1600 && ec <= 3000){
				road_tax = (475 + parseFloat(0.75 * (ec - 1600))) * 0.782;
			}
			else{
				document.Calculator.amtTB.value = "";
				document.Calculator.amtTB.focus();
				document.Calculator.result.value = "Error: Please select the correct engine capacity.";
				document.Calculator.result2.value = "";
				return false;
			}
		}
		// EC > 3000
		else if (ecValue == 5){
			if (ec > 3000){
				road_tax = (1525 + parseFloat((ec - 3000))) * 0.782;
			}
			else{
				document.Calculator.amtTB.value = "";
				document.Calculator.amtTB.focus();
				document.Calculator.result.value = "Error: Please select the correct engine capacity.";
				document.Calculator.result2.value = "";
				return false;
			}
		}
		
		document.Calculator.result.value = "Road Tax for Cars (" + ec + " cc) is S$ " + 	(Math.ceil(road_tax)).toFixed(2) + " (6 months).";
		document.Calculator.result2.value = "Road Tax for Cars (" + ec + " cc) is S$ " + 	(2 * Math.ceil(road_tax)).toFixed(2) + " (12 months).";
	}
}

//For Cars Engine Capacity <= 600
function car_ec600(){
	var ecValue =0;
	var road_tax =0;
	
	for (var i=0; i < document.Calculator.ecRB.length; i++){
		if (document.Calculator.ecRB[i].checked){
			ecValue = document.Calculator.ecRB[i].value;
		}
	}
	
	if (ecValue == 1){
		road_tax = parseFloat(200 * 0.782);
		ShowElementByID ('resultTable');
		document.Calculator.result.value = "Road Tax for Cars (<= 600 cc) is S$ " + (Math.ceil(road_tax)).toFixed(2) + " (6 months).";
		document.Calculator.result2.value = "Road Tax for Cars (<= 600 cc) is S$ " + 	(2 * Math.ceil(road_tax)).toFixed(2) + " (12 months).";
		
	}
}




function calcHybridCarEC(){
	var pr = 0;
	var prValue = 0;
	var road_tax = 0;
	var mc_prValue=0;
	var mc_pr = 0;

	for (var i=0; i < document.Calculator.pwRB.length; i++){
		if (document.Calculator.pwRB[i].checked){
			prValue = document.Calculator.pwRB[i].value;
		}
	}
	
	ShowElementByID ('resultTable');
	
	if (document.Calculator.amtTB3.value == "") {
		document.Calculator.amtTB3.focus();
		document.Calculator.result.value = "Error: Please fill in Power Rating, PR (kw).";
		document.Calculator.result2.value = "";
		return false;
  	}
	else{
		pr = parseInt(document.Calculator.amtTB3.value);
		
		// 7.5 < pr <= 32.5
		if (prValue == 2){
			if (pr > 7.5 && pr <= 32.5){
				road_tax = (200 + parseFloat(2 * (pr - 7.5))) * 0.782;
			}
			else{
				document.Calculator.amtTB3.value = "";
				document.Calculator.amtTB3.focus();
				document.Calculator.result.value = "Error: Please select the correct power rating.";
				document.Calculator.result2.value = "";
				return false;
			}
		}
		// 32.5 < pr <= 70
		else if (prValue == 3){
			if (pr > 32.5 && pr <= 70){
				road_tax = (250 + parseFloat(6 * (pr - 32.5))) * 0.782;
			}
			else{
				document.Calculator.amtTB3.value = "";
				document.Calculator.amtTB3.focus();
				document.Calculator.result.value = "Error: Please select the correct power rating.";
				document.Calculator.result2.value = "";
				return false;
			}
		}
		// 70 < pr <= 157.5
		else if (prValue == 4){
			if (pr > 70 && pr <= 157.5){
				road_tax = (475 + parseFloat(12 * (pr - 70))) * 0.782;
			}
			else{
				document.Calculator.amtTB3.value = "";
				document.Calculator.amtTB3.focus();
				document.Calculator.result.value = "Error: Please select the correct power rating.";
				document.Calculator.result2.value = "";
				return false;
			}
		}
		// pr > 157.5
		else if (prValue == 5){
			if (pr > 157.5){
				road_tax = (1525 + parseFloat(16 * (pr - 157.5))) * 0.782;
			}
			else{
				document.Calculator.amtTB3.value = "";
				document.Calculator.amtTB3.focus();
				document.Calculator.result.value = "Error: Please select the correct power rating.";
				document.Calculator.result2.value = "";
				return false;
			}
		}
		
		document.Calculator.result.value = "Road Tax for Electric/Hybrid Cars (" + pr + " kW) is S$ " + 	(Math.ceil(road_tax)).toFixed(2) + " (6 months).";
		document.Calculator.result2.value = "Road Tax for Electric/Hybrid Cars (" + pr + " kW) is S$ " + 	(2 * Math.ceil(road_tax)).toFixed(2) + " (12 months).";
	}
}


//For Hybrid cars Power rating <= 7.5
function hybrid_pr75(){
	var ecValue =0;
	var road_tax =0;
	
	for (var i=0; i < document.Calculator.pwRB.length; i++){
		if (document.Calculator.pwRB[i].checked){
			ecValue = document.Calculator.pwRB[i].value;
		}
	}
	
	if (ecValue == 1){
		road_tax = parseFloat(200 * 0.782);
		ShowElementByID ('resultTable');
		document.Calculator.result.value = "Road Tax for Electric/Hybrid Cars (PR <= 7.5 kW) is S$ " + (Math.ceil(road_tax)).toFixed(2) + " (6 months).";
		document.Calculator.result.value = "Road Tax for Electric/Hybrid Cars (PR <= 7.5 kW) is S$ " + (2 * Math.ceil(road_tax)).toFixed(2) + " (12 months).";
		
	}
}



function calcMotorEC(){
	var mc_ecValue = 0;
	var mc_ec = 0;
	var road_tax = 0;
	
	for (var i=0; i < document.Calculator.mc_ecRB.length; i++){
		if (document.Calculator.mc_ecRB[i].checked){
			mc_ecValue = document.Calculator.mc_ecRB[i].value;
		}
	}
	
	ShowElementByID ('resultTable');
	
	if (document.Calculator.amtTB2.value == "") {
		document.Calculator.amtTB2.focus();
		document.Calculator.result.value = "Error: Please fill in Engine Capacity (cc).";
		document.Calculator.result2.value = "";
		return false;
  	}
	else{
		mc_ec = parseInt(document.Calculator.amtTB2.value);
		if (mc_ecValue == 2){
			if (mc_ec > 200 && mc_ec <= 1000){
				road_tax = (40 + parseFloat(0.15 * (mc_ec - 200))) * 0.782;
			}
			else{
				document.Calculator.amtTB2.value = "";
				document.Calculator.amtTB2.focus();
				document.Calculator.result.value = "Error: Please select the correct engine capacity.";
				document.Calculator.result2.value = "";
				return false;
			}
		}
		else if (mc_ecValue == 3){
			if (mc_ec > 1000){
				road_tax = (160 + parseFloat(0.3 * (mc_ec - 1000))) * 0.782;
			}
			else{
				document.Calculator.amtTB2.value = "";
				document.Calculator.amtTB2.focus();
				document.Calculator.result.value = "Error: Please select the correct engine capacity.";
				document.Calculator.result2.value = "";
				return false;
			}
		}
		document.Calculator.result.value = "Road Tax for Motorcycles (" + mc_ec + " cc) is S$ " + (Math.ceil(road_tax)).toFixed(2) + " (6 months).";
		document.Calculator.result2.value = "Road Tax for Motorcycles (" + mc_ec + " cc) is S$ " + (2 * Math.ceil(road_tax)).toFixed(2) + " (12 months).";
	}
}

//For Motorcycles Capacity <= 200
function motor_ec200(){
	var mc_ecValue = 0;
	var road_tax = 0;
	
	for (var i=0; i < document.Calculator.mc_ecRB.length; i++){
	   if (document.Calculator.mc_ecRB[i].checked)
		  {
		  mc_ecValue = document.Calculator.mc_ecRB[i].value;
		  }
	}
	
	if (mc_ecValue == 1){
		road_tax = parseFloat(40 * 0.782);
		
		ShowElementByID ('resultTable');
		
		document.Calculator.result.value = "Road Tax for Motorcycles (<= 200 cc) is S$ " + (Math.ceil(road_tax)).toFixed(2) + " (6 months).";
		document.Calculator.result2.value = "Road Tax for Motorcycles (<= 200 cc) is S$ " + (2 * Math.ceil(road_tax)).toFixed(2) + " (12 months).";
	}
}

function gpv_price1(){
	ShowElementByID ('resultTable');
	document.Calculator.result.value = "Road Tax for GPVs (mlw <= 3.5 mt) is S$372.00 (Diesel) & S$298.00 (Petrol) (6 months).";
	document.Calculator.result2.value = "Road Tax for GPVs (mlw <= 3.5 mt) is S$744.00 (Diesel) & S$596.00 (Petrol) (12 months)";
}

function gpv_price2(){
	ShowElementByID ('resultTable');
	document.Calculator.result.value = "Road Tax for GPVs (mlw > 3.5 mt) is S$487.00 (Diesel) & S$390.00 (Petrol) (6 months).";
	document.Calculator.result2.value = "Road Tax for GPVs (mlw > 3.5 mt) is S$974.00 (Diesel) & S$780.00 (Petrol) (12 months)";
}


function getSelectedCarEC(){
	var ecValue=0;
	for (var i=0; i < document.Calculator.ecRB.length; i++){
		if (document.Calculator.ecRB[i].checked){
			ecValue = document.Calculator.ecRB[i].value;
			
			if (ecValue == 2)
				document.Calculator.carsECTB.value = "600 < EC <= 1000";
			else if (ecValue == 3)
				document.Calculator.carsECTB.value = "1,000 < EC <= 1,600";
			else if (ecValue == 4)
				document.Calculator.carsECTB.value = "1,600 < EC <= 3,000";
			else if (ecValue == 5)
				document.Calculator.carsECTB.value = "EC > 3,000";
		}
	}
}


function getSelectedHybridPR(){
	var prValue=0;
	for (var i=0; i < document.Calculator.pwRB.length; i++){
		if (document.Calculator.pwRB[i].checked){
			prValue = document.Calculator.pwRB[i].value;
			
			if (prValue == 2)
				document.Calculator.hybridPRTB.value = "7.5 < PR <= 32.5";
			else if (prValue == 3)
				document.Calculator.hybridPRTB.value = "32.5 < PR <= 70.5";
			else if (prValue == 4)
				document.Calculator.hybridPRTB.value = "70 < PR <= 157.5";
			else if (prValue == 5)
				document.Calculator.hybridPRTB.value = "PR > 157.5";
		}
	}
}

function getSelectedMotorEC(){
	var mc_ecValue=0;
	for (var i=0; i < document.Calculator.mc_ecRB.length; i++){
		if (document.Calculator.mc_ecRB[i].checked){
			mc_ecValue = document.Calculator.mc_ecRB[i].value;
			
			if (mc_ecValue == 2)
				document.Calculator.motorECTB.value = "200 < EC <= 1000";
			else if (mc_ecValue == 3)
				document.Calculator.motorECTB.value = "EC > 1000";
		}
	}
}

function resetTextTable(){
	document.Calculator.amtTB.value = "";
	document.Calculator.result.value = "";
	document.Calculator.result2.value = "";
}

function resetTextTable2(){
	document.Calculator.amtTB2.value = "";
	document.Calculator.result.value = "";
	document.Calculator.result2.value = "";
}

function resetTextTable3(){
	document.Calculator.amtTB3.value = "";
	document.Calculator.result.value = "";
	document.Calculator.result2.value = "";
}

function resetCarsStep1(){
	HideElementByID ('hybridcarsEC');
	HideElementByID ('motorEC');
	HideElementByID ('gpvMLW');
	HideElementByID ('textTable');
	HideElementByID ('textTable2');
	HideElementByID ('textTable3');
}

function resetHybridStep1(){
	HideElementByID ('carsEC');
	HideElementByID ('motorEC');
	HideElementByID ('gpvMLW');
	HideElementByID ('textTable');
	HideElementByID ('textTable2');
	HideElementByID ('textTable3');
}

function resetMotorStep1(){
	HideElementByID ('carsEC');
	HideElementByID ('hybridcarsEC');
	HideElementByID ('gpvMLW');
	HideElementByID ('textTable');
	HideElementByID ('textTable2');
	HideElementByID ('textTable3');
}

function resetGPVStep1(){
	HideElementByID ('carsEC');
	HideElementByID ('hybridcarsEC');
	HideElementByID ('motorEC');
	HideElementByID ('textTable');
	HideElementByID ('textTable2');
	HideElementByID ('textTable3');
	resetTextTable();
}

function resetAllFields(){
	//cars
	for (var i=0; i < document.Calculator.ecRB.length; i++){
	   document.Calculator.ecRB[i].checked = false;
	}
	
	//hybrid
	for (var i=0; i < document.Calculator.pwRB.length; i++){
	   document.Calculator.pwRB[i].checked = false;
	}
	
	//motorcycle
	for (var i=0; i < document.Calculator.mc_ecRB.length; i++){
	   document.Calculator.mc_ecRB[i].checked = false;
	}
	
	//GPVs
	for (var i=0; i < document.Calculator.mlwRB.length; i++){
	   document.Calculator.mlwRB[i].checked = false;
	}
	resetTextTable();
	resetTextTable2();
	resetTextTable3();
}