var xmlHttpMakes;
var xmlHttpModels;
var xmlHttpModelTypes;
var xmlHttpBody;
var xmlHttpFuel;
var xmlHttpTransmission;
var xmlHttpLiveResults;

function GetXmlHttpObject() {
		var xmlHttp = null;
		try {
				// Firefox, Opera 8.0+, Safari
				xmlHttp = new XMLHttpRequest();
		} catch(e) {
				// Internet Explorer
				try {
						xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
				} catch(e) {
						xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
				}
		}
		return xmlHttp;
}

// takes a select menu id and nulls the options within it.
function removeOptions(menuId) {
		document.getElementById(menuId).options.length = 0;
		/*optionMenu=document.getElementById(menuId);
		alert(menuId+' has '+optionMenu.options.length+' options');
		for(var i=0; i < optionMenu.options.length; i++){
				alert('Removing: ' + menuId + ' = '+optionMenu.options[i].value);
				optionMenu.options[i]=null;
		}*/
}

// takes a select menu id, an array with elements as 'value|option'
// e.g.  1000|£1,000.00
// builds new option into the select menu matching menuId.
function addOptions(menuId, newOptions) {
		var optionMenu = document.getElementById(menuId);
		if (optionMenu == null) {
				return;
		}
		optionMenu.options.length = 0;
		optionList = newOptions.split(',');
		for (var i = 0; i < optionList.length; i++) {
				opt = optionList[i];
				arr = opt.split('|');
				if (menuId == 'make' && arr[0] == 'Citroen') {
						optionMenu.value = 'Citroen';
						optionMenu[i] = new Option('Citroen', 'Citroen', 1, 1);
				} else {
						optionMenu[i] = new Option(arr[1], arr[0]);
				}
				// alert('Adding: ' + menuId +' = '+arr[0]+' | '+arr[1]);
		}
}

function updateOptionsBySite() {
		getMakes();
		updateModels();
		liveResults();
}

function updateOptionsBySiteSimple() {
		getMakes();
		updateModels();
}

function getMakes() {
		var location = document.getElementById('location').value;
		xmlHttpMakes = GetXmlHttpObject();
		if (xmlHttpMakes == null) {
				alert("Your browser does not support AJAX!");
				return;
		}
		var url = "http://www.alistairfleming.com/usedcars/updateMenus.php";
		url = url + "?type=make&location=" + location;
		xmlHttpMakes.onreadystatechange = buildMakes;
		xmlHttpMakes.open("GET", url, true);
		xmlHttpMakes.send(null);
}

function buildMakes() {
		if (xmlHttpMakes.readyState == 4) {
				if (xmlHttpMakes.responseText != "false") {
						//alert(xmlHttpMakes.responseText);
						//removeOptions('make');
						addOptions('make', xmlHttpMakes.responseText);
						//document.getElementById("make").innerHTML=xmlHttpMakes.responseText;
				}
		}
		return true;
}

function updateModels() {
		getModels();
		updateBodyTypes();
		updateFuelTypes();
		updateTransmissionTypes();
		liveResults();
}

function updateModelsSimple() {
		getModels();
}

function getModels() {
		var location = document.getElementById('location').value;
		var make = document.getElementById('make').value;
		xmlHttpModels = GetXmlHttpObject();
		if (xmlHttpModels == null) {
				alert("Your browser does not support AJAX!");
				return;
		}
		var url = "http://www.alistairfleming.com/usedcars/updateMenus.php";
		url = url + "?type=model&make=" + make + "&location=" + location;
		xmlHttpModels.onreadystatechange = buildModels;
		xmlHttpModels.open("GET", url, true);
		xmlHttpModels.send(null);
}

function buildModels() {
		if (document.getElementById('make').value == 'any') {
				if (document.getElementById('model')) {
						addOptions('model', 'any|Any');
				}
				if (document.getElementById('modeltype')) {
						addOptions('modeltype', 'any|Any');
				}
				return true;
		}

		else if (xmlHttpModels.readyState == 4) {
				if (xmlHttpModels.responseText != "false") {
						if (document.getElementById('model')) {
								addOptions('model', xmlHttpModels.responseText);
						}
						if (document.getElementById('modeltype')) {
								addOptions('modeltype', 'any|Any');
						}
						return true;
				} else {
						if (document.getElementById('model')) {
								addOptions('model', 'any|Any');
						}
						if (document.getElementById('modeltype')) {
								addOptions('modeltype', 'any|Any');
						}
						return true;
				}
		} else {
						if (document.getElementById('model')) {
								addOptions('model', 'any|Any');
						}
						if (document.getElementById('modeltype')) {
								addOptions('modeltype', 'any|Any');
						}
				return true;
		}
}

function updateModelTypes() {
		getModelTypes();
		updateBodyTypes();
		updateFuelTypes();
		updateTransmissionTypes();
		liveResults();
}

function getModelTypes() {
		var make = document.getElementById('make').value;
		var model = document.getElementById('model').value;
		var location = document.getElementById('location').value;
		xmlHttpModelTypes = GetXmlHttpObject();
		if (xmlHttpModelTypes == null) {
				alert("Your browser does not support AJAX!");
				return;
		}
		var url = "http://www.alistairfleming.com/usedcars/updateMenus.php";
		url = url + "?type=variant&make=" + make + "&model=" + model + "&location=" + location;
		xmlHttpModelTypes.onreadystatechange = buildModelVariants;
		xmlHttpModelTypes.open("GET", url, true);
		xmlHttpModelTypes.send(null);
}

function buildModelVariants() {
		if (xmlHttpModelTypes.readyState == 4) {
				if (xmlHttpModelTypes.responseText != "false") {
						//   removeOptions('modeltype');
						addOptions('modeltype', xmlHttpModelTypes.responseText);
						//document.getElementById("modeltype").innerHTML=xmlHttpModelTypes.responseText;
						document.getElementById("modeltype").disabled = "";
						return true;
				} else {
						//  removeOptions('modeltype');
						addOptions('modeltype', 'any|Any');
						//document.getElementById("modeltype").innerHTML="<option value='any'>Any</option>";
						document.getElementById("modeltype").disabled = "";
						return true;
				}
		} else {
				//   removeOptions('modeltype');
				addOptions('modeltype', 'any|Any');
				//document.getElementById("modeltype").innerHTML="<option value='any'>Any</option>";
				document.getElementById("modeltype").disabled = "";
				//document.getElementById("modeltype").innerHTML="<option value='any'>Any</option>";
				//document.getElementById("modeltype").disabled="";
				return true;
		}
}

function updateBodyTypes() {
		var location = document.getElementById('location').value;
		var make = document.getElementById('make').value;
		var model = document.getElementById('model').value;
		var modeltype = document.getElementById('modeltype').value;
		xmlHttpBody = GetXmlHttpObject();
		if (xmlHttpBody == null) {
				alert("Your browser does not support AJAX!");
				return;
		}
		var url = "http://www.alistairfleming.com/usedcars/updateMenus.php";
		url = url + "?type=body&make=" + make + "&model=" + model + "&modeltype=" + modeltype + "&location=" + location;
		xmlHttpBody.onreadystatechange = buildBodyTypes;
		xmlHttpBody.open("GET", url, true);
		xmlHttpBody.send(null);
}

function buildBodyTypes() {
		if (xmlHttpBody.readyState == 4) {
				if (xmlHttpBody.responseText != "false") {
						//  removeOptions('bodytype');
						addOptions('bodytype', xmlHttpBody.responseText);
						//document.getElementById("bodytype").innerHTML=xmlHttpBody.responseText;
				}
		}
		return true;
}

function updateFuelTypes() {
		var location = document.getElementById('location').value;
		var make = document.getElementById('make').value;
		var model = document.getElementById('model').value;
		var modeltype = document.getElementById('modeltype').value;
		xmlHttpFuel = GetXmlHttpObject();
		if (xmlHttpFuel == null) {
				alert("Your browser does not support AJAX!");
				return;
		}
		var url = "http://www.alistairfleming.com/usedcars/updateMenus.php";
		url = url + "?type=fuel&make=" + make + "&model=" + model + "&modeltype=" + modeltype + "&location=" + location;
		xmlHttpFuel.onreadystatechange = buildFuelTypes;
		xmlHttpFuel.open("GET", url, true);
		xmlHttpFuel.send(null);
}

function buildFuelTypes() {
		if (xmlHttpFuel.readyState == 4) {
				if (xmlHttpFuel.responseText != "false") {
						//  removeOptions('fueltype');
						addOptions('fueltype', xmlHttpFuel.responseText);
						//document.getElementById("fueltype").innerHTML=xmlHttpFuel.responseText;

				}
		}
		return true;
}

function updateTransmissionTypes() {
		var location = document.getElementById('location').value;
		var make = document.getElementById('make').value;
		var model = document.getElementById('model').value;
		var modeltype = document.getElementById('modeltype').value;
		xmlHttpTransmission = GetXmlHttpObject();
		if (xmlHttpTransmission == null) {
				alert("Your browser does not support AJAX!");
				return;
		}
		var url = "http://www.alistairfleming.com/usedcars/updateMenus.php";
		url = url + "?type=transmission&make=" + make + "&model=" + model + "&modeltype=" + modeltype + "&location=" + location;
		xmlHttpTransmission.onreadystatechange = buildTransmissionTypes;
		xmlHttpTransmission.open("GET", url, true);
		xmlHttpTransmission.send(null);
}

function buildTransmissionTypes() {
		if (xmlHttpTransmission.readyState == 4) {
				if (xmlHttpTransmission.responseText != "false") {
						//  removeOptions('transmission');
						addOptions('transmission', xmlHttpTransmission.responseText);
						//document.getElementById("transmission").innerHTML=xmlHttpTransmission.responseText;
				}
		}
		return true;
}

function liveResults() {
		if (document.getElementById('liveresults').checked) {
				var action = "liveResults.php?liveresults=1";
				var location = document.getElementById('location');
				var make = document.getElementById('make');
				var model = document.getElementById('model');
				var modeltype = document.getElementById('modeltype');
				var bodytype = document.getElementById('bodytype');
				var fueltype = document.getElementById('fueltype');
				var colour = document.getElementById('colour');
				var transmission = document.getElementById('transmission');
				var minprice = document.getElementById('minprice');
				var maxprice = document.getElementById('maxprice');
				var maxage = document.getElementById('maxage');
				var maxmileage = document.getElementById('maxmileage');
				var numresults = document.getElementById('numresults');
				// check box below
				var AirConditioning = document.getElementById('AirConditioning');
				var ClimateControl = document.getElementById('ClimateControl');
				var LeatherTrim = document.getElementById('LeatherTrim');
				var NavigationSystem = document.getElementById('NavigationSystem');
				var approved = document.getElementById('approved');

				var url = action;
				var params = "&location=" + location.value;
				params += "&make=" + make.value;
				params += "&model=" + model.value;
				params += "&modeltype=" + modeltype.value;
				params += "&bodytype=" + bodytype.value;
				params += "&fueltype=" + fueltype.value;
				params += "&colour=" + colour.value;
				params += "&transmission=" + transmission.value;
				params += "&minprice=" + minprice.value;
				params += "&maxprice=" + maxprice.value;
				params += "&maxage=" + maxage.value;
				params += "&maxmileage=" + maxmileage.value;
				params += "&numresults=" + numresults.value;
				if (AirConditioning.checked) {
						params += "&options[]=Air Conditioning";
				}
				if (ClimateControl.checked) {
						params += "&options[]=Climate Control";
				}
				if (LeatherTrim.checked) {
						params += "&options[]=Leather Trim";
				}
				if (NavigationSystem.checked) {
						params += "&options[]=Navigation System";
				}
				if (approved.checked) {
						params += "&approved=Approved Only";
				}

				//alert(url+params);

				xmlHttpLiveResults = GetXmlHttpObject();
				if (xmlHttpLiveResults == null) {
						alert("Your browser does not support AJAX!");
						return;
				}

				xmlHttpLiveResults.onreadystatechange = writeResultsTable;
				xmlHttpLiveResults.open("POST", url, true);
				//Send the proper header information along with the request
				xmlHttpLiveResults.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				xmlHttpLiveResults.setRequestHeader("Content-length", params.length);
				xmlHttpLiveResults.setRequestHeader("Connection", "close");
				xmlHttpLiveResults.onreadystatechange = writeResultsTable;
				xmlHttpLiveResults.send(params);
				xmlHttpLiveResults == null;
		}
}

function writeResultsTable() {
		if (xmlHttpLiveResults.readyState == 4) {
				if (xmlHttpLiveResults.responseText != "false") {
						document.getElementById("main").innerHTML = xmlHttpLiveResults.responseText;
				}
		}
}
//// display the chosen template with the email content
//function previewEmail(link, template, heading, product, image, article){
//    var lk=getElementUsingId(link);
//    var tp=getElementUsingId(template);
//    var hd=getElementUsingId(heading);
//    var pd=getElementUsingId(product);
//    var im=getElementUsingId(image);
//    var ar=getElementUsingId(article);
//
//    //alert(ar.value);
//    xmlHttp=GetXmlHttpObject();
//    if (xmlHttp==null){
//        alert ("Your browser does not support AJAX!");
//        return;
//    }
//
//    var url=lk.href;
//    var params="&id="+tp.value+"&hd="+hd.value+"&pd="+pd.value+"&im="+im.value+"&ar="+ar.value;
//
//    xmlHttp.onreadystatechange=stateChanged;
//    xmlHttp.open("POST",url,true);
//    //Send the proper header information along with the request
//    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
//    xmlHttp.setRequestHeader("Content-length", params.length);
//    xmlHttp.setRequestHeader("Connection", "close");
//
//    xmlHttp.onreadystatechange=stateChanged;
//    xmlHttp.send(params);
//    xmlHttp==null;
//    ar.value="";
//    return false;
//}

