
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=="O" || document.form1.model1.value=="O")
		{
			
			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 H/B 1.5 4A/T (Base)")
			{
				form1.fullprice.value = "32,678.00";
			}		
			else if(document.form1.model2.value=="Nissan Latio Sedan 1.5 4A/T (Base)")
			{
				form1.fullprice.value = "29,692.00";
			}
		}
		else if(document.form1.model1.value=="Murano")
		{
			if(document.form1.model2.value=="Nissan Murano V6 4WD A/T")
			{
				form1.fullprice.value = "71,853.00";
			}
		}
		else if(document.form1.model1.value=="Navara")
		{
			if(document.form1.model2.value=="Nissan Navara 4x4 6M/T")
			{
				form1.fullprice.value = "41,051.00";
			}
		}
		else if(document.form1.model1.value=="Sunny")
		{
			if(document.form1.model2.value=="Nissan Sunny EX-Sedan 1.6 A/T")
			{
				form1.fullprice.value = "28,715.00";
			}
		}
		else if(document.form1.model1.value=="Serena")
		{
			if(document.form1.model2.value=="Nissan Serena 2.0 A/T 5DRS 8 Seat")
			{
				form1.fullprice.value = "59,204.00";
			}
		}
		else if(document.form1.model1.value=="Cefiro")
		{
			if(document.form1.model2.value=="Nissan Cefiro 2.3DE A/T")
			{
				form1.fullprice.value = "47,733.00";
			}
			else if(document.form1.model2.value=="Nissan Cefiro 3.5DE 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 HWS 2.5 A/T")
			{
				form1.fullprice.value = "54,336.00";
			}	
		}
		else if(document.form1.model1.value=="370Z")
		{
			if(document.form1.model2.value=="Nissan 370Z V6 M/T 6 Speed")
			{
				form1.fullprice.value = "76,082.00";
			}
			else if(document.form1.model2.value=="Nissan 370Z V6 A/T 7 Speed")
			{
				form1.fullprice.value = "77,714.00";
			}
			else if(document.form1.model2.value=="Nissan 370Z Roadster V6 A/T 7 Speed")
			{
				form1.fullprice.value = "88,000.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=="Teana")
		{
			if(document.form1.model2.value=="Nissan Teana 2.5 V6 A/T")
			{
				form1.fullprice.value = "53,581.00";
			}
			else if(document.form1.model2.value=="Nissan Teana 3.5 V6 A/T")
			{
				form1.fullprice.value = "63,367.00";
			}		
		}
		else if(document.form1.model1.value=="Patrol")
		{
			if(document.form1.model2.value=="Nissan Patrol 3.0GRX 4A/T")
			{
				form1.fullprice.value = "72,641.00";
			}	
		}		
		else if(document.form1.model1.value=="X-Trail")
		{
			if(document.form1.model2.value=="Nissan X-Trail CVT 4A/T")
			{
				form1.fullprice.value = "44,523.00";
			}	
		}				
		else if(document.form1.model1.value=="Urvan")
		{
			if(document.form1.model2.value=="Nissan Urvan Microbus 9 Seater (L)")
			{
				form1.fullprice.value = "38,211.00";
			}
			else if(document.form1.model2.value=="Nissan Urvan Microbus 9 Seater (H)")
			{
				form1.fullprice.value = "42,655.00";
			}		
			else if(document.form1.model2.value=="Nissan Urvan Microbus 15 Seater")
			{
				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 A/T", "Nissan Cefiro 2.3DE A/T"); 
			addOption(document.form1.model2,"Nissan Cefiro 3.5DE A/T", "Nissan Cefiro 3.5DE 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 H/B 1.5 4A/T (Base)", "Nissan Latio H/B 1.5 4A/T (Base)"); 
			addOption(document.form1.model2,"Nissan Latio Sedan 1.5 4A/T (Base)", "Nissan Latio Sedan 1.5 4A/T (Base)"); 
    }		

    else if(choice == "Murano")
    {
			document.all['disp_model2'].style.display = "";
		  removeOptions(document.form1.model2)
	
			addOption(document.form1.model2,"", ""); 			
			addOption(document.form1.model2,"Nissan Murano V6 4WD A/T", "Nissan Murano V6 4WD A/T"); 
    }
	
    else if(choice == "Navara")
    {
			document.all['disp_model2'].style.display = "";
		  removeOptions(document.form1.model2)
	
			addOption(document.form1.model2,"", ""); 			
			addOption(document.form1.model2,"Nissan Navara 4x4 6M/T", "Nissan Navara 4x4 6M/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-Sedan 1.6 A/T", "Nissan Sunny EX-Sedan 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 HWS 2.5 A/T", "Nissan Presage HWS 2.5 A/T"); 		   
     }	

     else if(choice == "370Z")
     {
			document.all['disp_model2'].style.display = "";
	    removeOptions(document.form1.model2)

			addOption(document.form1.model2,"", ""); 						
			addOption(document.form1.model2,"Nissan 370Z V6 M/T 6 Speed", "Nissan 370Z V6 M/T 6 Speed"); 
			addOption(document.form1.model2,"Nissan 370Z V6 A/T 7 Speed", "Nissan 370Z V6 A/T 7 Speed"); 
			addOption(document.form1.model2,"Nissan 370Z Roadster V6 A/T 7 Speed", "Nissan 370Z Roadster V6 A/T 7 Speed");
     }		
     else if(choice == "Serena")
     {
			document.all['disp_model2'].style.display = "";
	    removeOptions(document.form1.model2)

			addOption(document.form1.model2,"", ""); 						
			addOption(document.form1.model2,"Nissan Serena 2.0 A/T 5DRS 8 Seat", "Nissan Serena 2.0 A/T 5DRS 8 Seat"); 
     }	

     else if(choice == "Teana")
     {
			document.all['disp_model2'].style.display = "";
	    removeOptions(document.form1.model2)

			addOption(document.form1.model2,"", ""); 						
			addOption(document.form1.model2,"Nissan Teana 2.5 V6 A/T", "Nissan Teana 2.5 V6 A/T"); 
			addOption(document.form1.model2,"Nissan Teana 3.5 V6 A/T", "Nissan Teana 3.5 V6 A/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 3.0GRX 4A/T", "Nissan Patrol 3.0GRX 4A/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 Seater (L)", "Nissan Urvan Microbus 9 Seater (L)"); 
			addOption(document.form1.model2,"Nissan Urvan Microbus 9 Seater (H)", "Nissan Urvan Microbus 9 Seater (H)"); 
			addOption(document.form1.model2,"Nissan Urvan Microbus 15 Seater", "Nissan Urvan Microbus 15 Seater"); 
     }

     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 CVT 4A/T", "Nissan X-Trail CVT 4A/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 A/T")
				{
					var carPrice = calc(47733.10);
				}
				else if(document.form1.model2.value=="Nissan Cefiro 3.5DE A/T")
				{
					var carPrice = calc(56496.50);
				}
			}
			
			else if (document.form1.model1.value=="Presage")
			{
				if(document.form1.model2.value=="Nissan Presage HWS 2.5 A/T")
				{
					var carPrice = calc(54336.00);
				}
			}
			else if (document.form1.model1.value=="Sunny")
			{
				if(document.form1.model2.value=="Nissan Sunny EX-Sedan 1.6 A/T")
				{
					var carPrice = calc(28715.00);
				}
			}

			else if (document.form1.model1.value=="Murano")
			{
				if(document.form1.model2.value=="Nissan Murano V6 4WD A/T")
				{
					var carPrice = calc(71853.00);
				}
			}
			else if (document.form1.model1.value=="Navara")
			{
				if(document.form1.model2.value=="Nissan Navara 4x4 6M/T")
				{
					var carPrice = calc(41051.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=="Serena")
			{
				if(document.form1.model2.value=="Nissan Serena 2.0 A/T 5DRS 8 Seat")
				{
					var carPrice = calc(59204.00);
				}
			}
			else if (document.form1.model1.value=="370Z")
			{
				if(document.form1.model2.value=="Nissan 370Z V6 M/T 6 Speed")
				{
					var carPrice = calc(76082.00);
				}
				else if(document.form1.model2.value=="Nissan 370Z V6 A/T 7 Speed")
				{
					var carPrice = calc(77714.00);

				}				
				else if(document.form1.model2.value=="Nissan 370Z Roadster V6 A/T 7 Speed")
				{
					var carPrice = calc(88000.00);

				}
			}
			
			else if (document.form1.model1.value=="Teana")
			{
				if(document.form1.model2.value=="Nissan Teana 2.5 V6 A/T")
				{
					var carPrice = calc(53581.00);
				}
				else if(document.form1.model2.value=="Nissan Teana 3.5 V6 A/T")
				{
					var carPrice = calc(63367.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 3.0GRX 4A/T")
				{
					var carPrice = calc(72641.00);
				}
			}
			else if (document.form1.model1.value=="Urvan")
			{
				if(document.form1.model2.value=="Nissan Urvan Microbus 9 Seater (L)")
				{
					var carPrice = calc(38211.00);
				}
				else if(document.form1.model2.value=="Nissan Urvan Microbus 9 Seater (H)")
				{
					var carPrice = calc(42655.00);
				}
				else if(document.form1.model2.value=="Nissan Urvan Microbus 15 Seater")
				{
					var carPrice = calc(36221.00);
				}			
			}
			else if (document.form1.model1.value=="X-Trail")
			{
				if(document.form1.model2.value=="Nissan X-Trail CVT 4A/T")
				{
					var carPrice = calc(44523.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 H/B 1.5 4A/T (Base)")
				{
					var carPrice = calc(32678.00);
				}
				else if(document.form1.model2.value=="Nissan Latio Sedan 1.5 4A/T (Base)")
				{
					var carPrice = calc(29692.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());
		}
		
		
		
