
function initC()
{
	document.all['disp_model2'].style.display = "none";
	document.all['disp_model3'].style.display = "none";	
	document.all['disp_model4'].style.display = "none";
	form1.dpValue.disabled=true;
}


function check() {
	var contents = document.form1.intRate.value
    if (((contents / contents) != 1) && (contents != 0)) 
	{
		alert('Please enter only a number into this field')
		document.form1.intRate.focus();
	}
}

function checkCh(objEvent) 
	{
	  var iKeyCode;  	
	  iKeyCode = objEvent.keyCode;			
	  if((iKeyCode>=48 && iKeyCode<=57) || iKeyCode==46) return true;
	  return false;
	}


function setRegType(m)
{
	if(m=="Vanette 1")
	{
	removeOptions(document.form1.regType)
	addOption(document.form1.regType,"COMMERCIAL", "COMMERCIAL"); 
	}
	else if(m=="All")
	{
		removeOptions(document.form1.regType)
		addOption(document.form1.regType,"PRIVATE", "PRIVATE"); 
		addOption(document.form1.regType,"COMPANY", "COMPANY"); 	
		addOption(document.form1.regType,"COMMERCIAL", "COMMERCIAL"); 
	}
	else if(m=="PrivateCompany")
	{
		removeOptions(document.form1.regType)
		addOption(document.form1.regType,"PRIVATE", "PRIVATE"); 
		addOption(document.form1.regType,"COMPANY", "COMPANY"); 	
	}	
	else if(m=="Private")
	{
		removeOptions(document.form1.regType)
		addOption(document.form1.regType,"PRIVATE", "PRIVATE"); 
	}	
	else if(m=="Commercial")
	{
		removeOptions(document.form1.regType)
		addOption(document.form1.regType,"COMMERCIAL", "COMMERCIAL"); 
	}		
	return true;
}

function setPaintType(m)
{
	if(m=="1")
	{
	removeOptions(document.form1.model3)
	addOption(document.form1.model3,"Metallic", "Metallic"); 
	}
	else if(m=="2")
	{
	removeOptions(document.form1.model3)
	addOption(document.form1.model3,"Non-Metallic", "Non-Metallic"); 
	}
	else if(m=="3")
	{
	removeOptions(document.form1.model3)
	addOption(document.form1.model3,"Metallic", "Metallic"); 
	addOption(document.form1.model3,"Non-Metallic", "Non-Metallic");
	}	
	return true;
}

function clearResult()
{
	form1.installment.value = "";
	form1.dp.value = "";
	form1.carPrice.value = "";
	form1.totalLoan.value = "";
	form1.EndPay.value="";
}
function clearField()
{
		form1.installment.value = "";
		form1.dp.value = "";
		form1.carPrice.value = "";
		form1.totalLoan.value = "";
		form1.EndPay.value="";
		if(document.form1.model1.value=="C22 VANETTE")
		{ 
		var x = vanette();
			if(document.form1.model2.value=="Chasis Cab Wooden Body" || document.form1.model2.value=="Chasis Cab Steel Body" || document.form1.model2.value=="Panel" || document.form1.model2.value=="Semi-Panel")
			{
				var n = setPaintType("2");
				var x = setRegType("Commercial")	
				document.all['disp_model3'].style.display = "";
			}
			else if(document.form1.model2.value=="Elite")
			{
				removeOptions(document.form1.regType)
				addOption(document.form1.regType,"PRIVATE", "PRIVATE"); 
				addOption(document.form1.regType,"COMPANY", "COMPANY"); 
			}
			else if(document.form1.model2.value=="Window Van")
			{
				var n = setPaintType("3");
				var x = setRegType("All")	
			}
			
		}
		else if(document.form1.model1.value=="SENTRA" || document.form1.model1.value=="SERENA MPV")
		{
			
			var x = setRegType("PrivateCompany")
		}
		else if(document.form1.model1.value=="FRONTIER")
		{
			var x = setRegType("All")
		}
		
		else if(document.form1.model1.value=="LATIO")
		{
			if(document.form1.model2.value=="NISSAN LATIO CVT 1.5 A/T")
			{
				form1.fullprice.value = "30,721.00";
			}
		}
		else if(document.form1.model1.value=="SUNNY")
		{
			if(document.form1.model2.value=="NISSAN SUNNY EX-SALOON 1.6 A/T")
			{
				form1.fullprice.value = "26,380.00";
			}
		}
		else if(document.form1.model1.value=="CEFIRO")
		{
			if(document.form1.model2.value=="NISSAN CEFIRO 2.3DE 4 A/T")
			{
				form1.fullprice.value = "47,733.00";
			}
			else if(document.form1.model2.value=="NISSAN CEFIRO 3.5DE 4 A/T")
			{
				form1.fullprice.value = "56,496.00";
			}			
		}		
		else if(document.form1.model1.value=="SYLPHY")
		{
			if(document.form1.model2.value=="NISSAN SYLPHY BLUEBIRD CVT 2.0 A/T")
			{
				form1.fullprice.value = "34,446.00";
			}
		}
		else if(document.form1.model1.value=="PRESAGE")
		{
			if(document.form1.model2.value=="NISSAN PRESAGE HIGHWAY STAR 2.5 4 A/T")
			{
				form1.fullprice.value = "53,430.00";
			}	
		}
		else if(document.form1.model1.value=="350Z")
		{
			if(document.form1.model2.value=="NISSAN 350Z 5A/T")
			{
				form1.fullprice.value = "77,460.00";
			}
			else if(document.form1.model2.value=="NISSAN 350Z 6M/T")
			{
				form1.fullprice.value = "74,363.00";
			}		
		}
		else if(document.form1.model1.value=="ELGRAND")
		{
			if(document.form1.model2.value=="NISSAN ELGRAND 5A/T")
			{
				form1.fullprice.value = "73,804.00";
			}
		}
		else if(document.form1.model1.value=="PATROL")
		{
			if(document.form1.model2.value=="NISSAN PATROL SW 3.0GRX A/T")
			{
				form1.fullprice.value = "62,727.00";
			}	
		}		
		else if(document.form1.model1.value=="X-TRAIL")
		{
			if(document.form1.model2.value=="NISSAN X-TRAIL 2.5 A/T")
			{
				form1.fullprice.value = "40,438.00";
			}	
		}				
		else if(document.form1.model1.value=="URVAN")
		{
			if(document.form1.model2.value=="NISSAN URVAN MICROBUS 9 SEAT (L)")
			{
				form1.fullprice.value = "32,168.00";
			}
			else if(document.form1.model2.value=="NISSAN URVAN MICROBUS 9 SEAT (H)")
			{
				form1.fullprice.value = "38,941.00";
			}		
			else if(document.form1.model2.value=="NISSAN URVAN MICROBUS 15 SEAT")
			{
				form1.fullprice.value = "36,221.00";
			}					
		}			
}

	function vanette()
	{
		var v = document.form1.model2.value;
		if(v=="Elite" || v=="Panel" || v=="Semi-Panel")
		{
			document.all['disp_model3'].style.display = "none";	
			if(v=="Panel" || v=="Semi-Panel")
			{
				//document.all['disp_model4'].style.display = "none";	
			}
			if(v=="Elite")
			{
				var m = setPaintType("1");
				document.all['disp_model3'].style.display = "";
				document.all['disp_model4'].style.display = "";
			}
		}
		else if(v=="Chasis Cab Wooden Body" || v=="Chasis Cab Steel Body")
		{
			var m = setPaintType("2");
			document.all['disp_model3'].style.display = "";	
		}
		else if(v=="Window Van")
		{
			document.all['disp_model3'].style.display = "";	
			document.all['disp_model4'].style.display = "";	
		}		
		else
		{
			document.all['disp_model3'].style.display = "";	
		}
		return true;
	}

function showField()
    {
    var choice = document.form1.model1.options[document.form1.model1.selectedIndex].value;
		form1.installment.value = "";
		form1.dp.value = "";
		form1.carPrice.value = "";
		form1.totalLoan.value = "";
		form1.EndPay.value="";
    if(choice == "CEFIRO")
    {	
			document.all['disp_model2'].style.display = "";
			removeOptions(document.form1.model2)

			addOption(document.form1.model2,"", ""); 			
			addOption(document.form1.model2,"NISSAN CEFIRO 2.3DE 4 A/T", "NISSAN CEFIRO 2.3DE 4 A/T"); 
			addOption(document.form1.model2,"NISSAN CEFIRO 3.5DE 4 A/T", "NISSAN CEFIRO 3.5DE 4 A/T"); 	

		}
		else if(choice == "")
		{
			document.all['disp_model2'].style.display = "none";
			document.all['disp_model3'].style.display = "none";	
			document.all['disp_model4'].style.display = "none";	
		}
    else if(choice == "SYLPHY")
    {
			document.all['disp_model2'].style.display = "";
	    removeOptions(document.form1.model2)

			addOption(document.form1.model2,"", ""); 						
			addOption(document.form1.model2,"NISSAN SYLPHY BLUEBIRD CVT 2.0 A/T", "NISSAN SYLPHY BLUEBIRD CVT 2.0 A/T"); 
			   
    }
		
    else if(choice == "LATIO")
    {
			document.all['disp_model2'].style.display = "";
			removeOptions(document.form1.model2)
	
			addOption(document.form1.model2,"", ""); 			
			addOption(document.form1.model2,"NISSAN LATIO CVT 1.5 A/T", "NISSAN LATIO CVT 1.5 A/T"); 
    }		

    else if(choice == "SUNNY")
    {
			document.all['disp_model2'].style.display = "";
		  removeOptions(document.form1.model2)
	
			addOption(document.form1.model2,"", ""); 			
			addOption(document.form1.model2,"NISSAN SUNNY EX-SALOON 1.6 A/T", "NISSAN SUNNY EX-SALOON 1.6 A/T"); 
    }		

     else if(choice == "PRESAGE")
     {
			document.all['disp_model2'].style.display = "";
	    removeOptions(document.form1.model2)

			addOption(document.form1.model2,"", ""); 						
			addOption(document.form1.model2,"NISSAN PRESAGE HIGHWAY STAR 2.5 4 A/T", "NISSAN PRESAGE HIGHWAY STAR 2.5 4 A/T"); 		   
     }	

     else if(choice == "350Z")
     {
			document.all['disp_model2'].style.display = "";
	    removeOptions(document.form1.model2)

			addOption(document.form1.model2,"", ""); 						
			addOption(document.form1.model2,"NISSAN 350Z 5A/T", "NISSAN 350Z 5A/T"); 
			addOption(document.form1.model2,"NISSAN 350Z 6M/T", "NISSAN 350Z 6M/T"); 
     }		

     else if(choice == "ELGRAND")
     {
			document.all['disp_model2'].style.display = "";
		    removeOptions(document.form1.model2)

			addOption(document.form1.model2,"", ""); 						
			addOption(document.form1.model2,"NISSAN ELGRAND 5A/T", "NISSAN ELGRAND 5A/T"); 
     }					

     else if(choice == "PATROL")
     {
			document.all['disp_model2'].style.display = "";
		    removeOptions(document.form1.model2)
		    
			addOption(document.form1.model2,"", "");
			addOption(document.form1.model2,"NISSAN PATROL SW 3.0GRX A/T", "NISSAN PATROL SW 3.0GRX A/T"); 
     }					

     else if(choice == "URVAN")
     {
	    document.all['disp_model2'].style.display = "";
	    removeOptions(document.form1.model2)

			addOption(document.form1.model2,"", "");			
			addOption(document.form1.model2,"NISSAN URVAN MICROBUS 9 SEAT (L)", "NISSAN URVAN MICROBUS 9 SEAT (L)"); 
			addOption(document.form1.model2,"NISSAN URVAN MICROBUS 9 SEAT (H)", "NISSAN URVAN MICROBUS 9 SEAT (H)"); 
			addOption(document.form1.model2,"NISSAN URVAN MICROBUS 15 SEAT", "NISSAN URVAN MICROBUS 15 SEAT"); 
     }

     else if(choice == "X-TRAIL")
     {
	    document.all['disp_model2'].style.display = "";
	    removeOptions(document.form1.model2)

			addOption(document.form1.model2,"", "");			
			addOption(document.form1.model2,"NISSAN X-TRAIL 2.5 A/T", "NISSAN X-TRAIL 2.5 A/T"); 
     }
     		
		else if(choice == "MURANO" || choice == "")
        {
		   var m = setRegType("Private");
		   var n = setPaintType("1");
           document.all['disp_model2'].style.display = "none";
		   document.all['disp_model3'].style.display = "";	  
		   document.all['disp_model4'].style.display = "";	
		    
        }

        else
        {
   
		    removeOptions(document.form1.model2)
			document.all['disp_model2'].style.display = "none";
			document.all['disp_model3'].style.display = "none";	
			document.all['disp_model4'].style.display = "none";	
        }
    }

	function checkValue()             
	{
		//form1.installment.value = "RM 100"
		var intR = document.form1.intRate.value;
		If (intR > 10)
		{
			alert("Please enter an interest rate between 0 - 10%")
		}
	}	
	
	function valueExist(boxName)
	{
		if(boxName.value != "")
		{
		return true;
		}
		else
		{
		return false;
		}
	}

	function addOption(selectbox,text,value )
	{
	var optn = document.createElement("OPTION");
	optn.text = text;
	optn.value = value;
	selectbox.options.add(optn);
	}	
	
	function removeOptions(selectbox)
	{
	var i;
	for(i=selectbox.options.length-1;i>=0;i--)
	{
	selectbox.remove(i);
	}
	}

	
	
	
function roundNo(n) {
  var s = "" + Math.round(n * 100) / 100
  var i = s.indexOf('.')
  if (i < 0) return s + ".00"
  var t = s.substring(0, i + 1) + s.substring(i + 1, i + 3)
  if (i + 2 == s.length) t += "0"
  return t
}


function replaceChars(entry) {
out = "a"; // replace this
add = "z"; // with this
temp = "" + entry; // temporary holder

while (temp.indexOf(out)>-1) {
pos= temp.indexOf(out);
temp = "" + (temp.substring(0, pos) + add + 
temp.substring((pos + out.length), temp.length));
}
document.subform.text.value = temp;
}

	function setPrice(carPrice)
	{
		if(document.form1.dpPercent.disabled==false)
		{
			var DP1 = (document.form1.dpPercent.value * carPrice) / 100;
			return DP1;
		}
		else if(document.form1.dpValue.disabled==false)
		{
			return document.form1.dpValue.value;
		}		
	}		
	
	function calc(carPrice)
	{
			var DP = setPrice(carPrice);
			var LoanAmount = carPrice - DP;
			
			var interest = form1.intRate.value / 100;
			var LoanPeriodYear = form1.loanPeriod.value / 12;
			var LoanPeriodMonth = form1.loanPeriod.value ;					
			var endPay = monthsahead(LoanPeriodMonth-1);
			var TotalLoan = roundNo(((LoanAmount * interest) * LoanPeriodYear) + LoanAmount);
			var Installment = roundNo(TotalLoan / LoanPeriodMonth);
		form1.dp.value = "$" + " " + roundNo(DP);	
		form1.carPrice.value = "$" + " " + carPrice;	
		form1.installment.value = "$" + " " + Installment;
		form1.totalLoan.value = "$" + " " + TotalLoan;
		form1.EndPay.value = endPay;
		
	}
	
function result()
{
		var interest = form1.intRate.value / 100;
		var loanPeriod = form1.loanPeriod.value;
		var regType = form1.regType.value;
		if(document.form1.intRate.value != "")
		{
			if(document.form1.model1.value=="CEFIRO")
			{
				if(document.form1.model2.value=="NISSAN CEFIRO 2.3DE 4 A/T")
				{
					var carPrice = calc(47733.10);
				}
				else if(document.form1.model2.value=="NISSAN CEFIRO 3.5DE 4 A/T")
				{
					var carPrice = calc(56496.50);
				}
			}
			
			else if (document.form1.model1.value=="PRESAGE")
			{
				if(document.form1.model2.value=="NISSAN PRESAGE HIGHWAY STAR 2.5 4 A/T")
				{
					var carPrice = calc(53430.00);
				}
			}
			
			else if (document.form1.model1.value=="SYLPHY")
			{
				if(document.form1.model2.value=="NISSAN SYLPHY BLUEBIRD CVT 2.0 A/T")
				{
					var carPrice = calc(34446.00);
				}
			}

			else if (document.form1.model1.value=="350Z")
			{
				if(document.form1.model2.value=="NISSAN 350Z 5A/T")
				{
					var carPrice = calc(77460.00);
				}
				else if(document.form1.model2.value=="NISSAN 350Z 6M/T")
				{
					var carPrice = calc(74363.00);
				}				
			}
			
			else if (document.form1.model1.value=="ELGRAND")
			{
				if(document.form1.model2.value=="NISSAN ELGRAND 5A/T")
				{
					var carPrice = calc(73804.00);
				}
			}			

			else if (document.form1.model1.value=="PATROL")
			{
				if(document.form1.model2.value=="NISSAN PATROL SW 3.0GRX A/T")
				{
					var carPrice = calc(62727.00);
				}
			}
			else if (document.form1.model1.value=="URVAN")
			{
				if(document.form1.model2.value=="NISSAN URVAN MICROBUS 9 SEAT (L)")
				{
					var carPrice = calc(32168.00);
				}
				else if(document.form1.model2.value=="NISSAN URVAN MICROBUS 9 SEAT (H)")
				{
					var carPrice = calc(38941.00);
				}
				else if(document.form1.model2.value=="NISSAN URVAN MICROBUS 15 SEAT")
				{
					var carPrice = calc(36221.00);
				}			
			}
			else if (document.form1.model1.value=="X-TRAIL")
			{
				if(document.form1.model2.value=="NISSAN X-TRAIL 2.5 A/T")
				{
					var carPrice = calc(40438.00);
				}
			}
			else if (document.form1.model1.value=="MURANO")
			{
				
				var carPrice = calc(369975.00);
			}			
  		else if (document.form1.model1.value=="LATIO")
			{
				
				if(document.form1.model2.value=="NISSAN LATIO CVT 1.5 A/T")
				{
					var carPrice = calc(30721.00);
				}
				else if(document.form1.model2.value=="")
				{
					var carPrice = calc(0.00);
				}
			}			

		}
		else
		{
		alert("Please enter a valid interest rate");
		}
}	


		function makeArray() {
			for (i = 0; i<makeArray.arguments.length; i++)
				this[i + 1] = makeArray.arguments[i];
		}
		
		var months = new makeArray('January','February','March','April',
								   'May','June','July','August','September',
								   'October','November','December');
		
		function nths(day) {
			if (day == 1 || day == 21 || day == 31) return 'st';
			if (day == 2 || day == 22) return 'nd';
			if (day == 3 || day == 23) return 'rd';
			return 'th';
		}
		
		function y2k(number) { return (number < 1000) ? number + 1900 : number; }
		
		function monthsahead(noofmonths) {
			var today = new Date();
			var yearStart = document.form1.yearStart.value;
			var monthStart = document.form1.monthStart.value -1;			
			//var date = new Date(today.getYear(),today.getMonth() + noofmonths,today.getDate(),today.getHours(),today.getMinutes(),today.getSeconds());
			var date = new Date(yearStart,monthStart + noofmonths,today.getDate(),today.getHours(),today.getMinutes(),today.getSeconds());
			return months[date.getMonth() + 1] + ' ' + y2k(date.getYear());
			//return months[date.getMonth() + 1] + ' ' + y2k(date.getYear());
		}
		
		
		
