var OtherSelect = 0;

function MM_validateForm() { //v4.0
  if (document.getElementById){
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
      if (val) { nm=val.name; if ((val=val.value)!="") {
        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
          if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
        } else if (test!='R') { num = parseFloat(val);
          if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
      } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
    } if (errors) alert('The following error(s) occurred:\n\n - Make sure all fields marked with * are filled in');
    document.MM_returnValue = (errors == '');
} }

function ValidateForm(){
	var validatemodel = $("#model").val();
	var validatezip = $("#zip").val();
	if(validatemodel != " " && validatezip !=""){
		document.forms["order"].submit();
	}
	else{
		alert('The following error(s) occurred:\n\n - Make sure you entered valid postal code\n - Select Year/Model/Make\n\nNOTE: If your model is unavailable,check OTHER \nand enter vehicle make/model manually');
	}
}

function OtherMake()
{

	if(OtherSelect == 0){
        $('#makeselect').html('<input type="text" name="year" id="year" class="inputorder" />');
        $('#modelselect').html('<input type="text" name="model" id="model" class="inputorder" />');	
        var yearCase = $('#year').parent();
        yearCase.html('Vehicle Year<br /><input type="text" name="year" id="year" class="inputorder" />');
        OtherSelect = 1;
	}
    else if(OtherSelect == 1){
    	$('#makeselect').html('<select name="make" id="make" class="inputorder" disabled="disabled" onchange="MakeSelect()"></select>');
        $('#modelselect').html('<select name="model" id="model" disabled="disabled" class="inputorder"></select>');	
        var yearCase = $('#year').parent();
        yearCase.html('Vehicle Year<br /><select name="year" id="year" class="inputorder" onchange="YearSelect()"><option value="" selected>-- Select Vehicle Year --</option><option value="2012">2012</option><option value="2011">2011</option><option value="2010">2010</option><option value="2009">2009</option><option value="2008">2008</option><option value="2007">2007</option><option value="2006">2006</option><option value="2005">2005</option><option value="2004">2004</option><option value="2003">2003</option><option value="2002">2002</option><option value="2001">2001</option><option value="2000">2000</option><option value="1999">1999</option><option value="1998">1998</option><option value="1997">1997</option><option value="1996">1996</option><option value="1995">1995</option><option value="1994">1994</option><option value="1993">1993</option><option value="1992">1992</option><option value="1991">1991</option><option value="1990">1990</option></select>');
        OtherSelect = 0;
    }
}

    function YearSelect(){
    var vehyear = $("#year").val();
	$("#make").removeClass("input-disabled");
        //$("#model").html('<option value="">-- All Models --</option>');
        $.ajax({
               url: "ajax_year.php",
               global: false,
               type: "POST",
               async: false,
               dataType: "html",
               data: "year="+vehyear, //the name of the $_POST variable and its value
               success: function (response) //'response' is the output provided
                           {
                        //counts the number of dynamically generated options
                        var dynamic_options = $("*").index( $('.dynamic')[0] );
                        //removes previously dynamically generated options if they exists (not equal to 0)
                        if (dynamic_options != (-1)) $(".dynamic").remove();
                        $("#make").html(response);
                        $(".first").attr({selected: ' selected'});
                        
                       }
              });
    }
    
    function MakeSelect(){
        var vehyear = $("#year").val();
        var vehmake = $("#make").val();
		$("#model").removeClass("input-disabled");
        $.ajax({
               url: "ajax_make.php",
               global: false,
               type: "POST",
               async: false,
               dataType: "html",
               data: "year="+ vehyear+"&make="+vehmake, //the name of the $_POST variable and its value
               success: function (response) //'response' is the output provided
                           {
                         //counts the number of dynamically generated options
                        $("#model").html(response);
                        $(".first").attr({selected: ' selected'});                        
                       }
              });
              return false
    }      
