// JavaScript Document

/**
 * Disabales or enables the differen search fields, depending on the selected car category. Also loads the 
 * correct carseries dropdown, if the user has already selected a brand.
 * 
 * @param none
 * @author Eycke Langhans
*/
function changeForm() {
	// fields to switch
	var fields = ["SearchFromYear","SearchToYear","SearchState","SearchRegion","SearchPostcode","SearchPostcoderadius"];
	// switch them off if no category or new vehicles is selected
	if ($("SearchCarcategorieId").value == "4" || $("SearchCarcategorieId").value == "") { 
		fields.each(function(field) {
			$(field).disabled = true;
		});
		if($("SearchCarcategorieId").value == "4") {
			// load the new vehicle carseries
			if($("SearchBrandId").value != "") {
				new Ajax.Updater('carmodel','/carseries/update_search_dropdown', {asynchronous:true, evalScripts:true, onLoading:function(request) {document.getElementById("SearchCarseriesId").options.length=0; document.getElementById("SearchCarseriesId").options[0]=new Option("Loading...", "0", true, false);}, parameters:Form.Element.serialize('SearchBrandId'), requestHeaders:['X-Update', 'carmodel']});
			}
		}
	} else {
		var status = $("SearchCarseriesId").disabled;
		fields.each(function(field) {
			$(field).disabled = false;
		});
		// load the pre-owned carseries
		if($("SearchBrandId").value != "") {
			new Ajax.Updater('carmodel','/vehicles/update_model', {asynchronous:true, evalScripts:true, onLoading:function(request) {document.getElementById("SearchCarseriesId").options.length=0; document.getElementById("SearchCarseriesId").options[0]=new Option("Loading...", "0", true, false);}, parameters:Form.Element.serialize('SearchBrandId'), requestHeaders:['X-Update', 'carmodel']});
		}
	}
}
