function getXMLHTTPObject()
{
	var xmlHTTP;
	
	if(window.XMLHttpRequest)
		xmlHTTP = new XMLHttpRequest();
	else if(window.ActiveXObject)
		xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP");
		
	return xmlHTTP;
}

var currentSelectedProduct = -1;
var models = null;
var products = null;

function updateModels(brand)
{	
	var modelSelect = document.getElementById("modelSelector");
	
	modelSelect.disabled = true;
	modelSelect.style.backgroundColor = "#AAAAAA";
	modelSelect.options.length = 0;
	
	var chassisSelect = document.getElementById("chassisSelector");
	
	chassisSelect.disabled = true;
	chassisSelect.style.backgroundColor = "#AAAAAA";
	chassisSelect.options.length = 0;	
	
	var productionYearSelect = document.getElementById("productionYearSelector");
	
	productionYearSelect.disabled = true;
	productionYearSelect.style.backgroundColor = "#AAAAAA";
	productionYearSelect.options.length = 0;	
	
	var engineSelect = document.getElementById("engineSelector");
	
	engineSelect.disabled = true;
	engineSelect.style.backgroundColor = "#AAAAAA";
	engineSelect.options.length = 0;		
	
	var url = "/ajaxGetModelsForBrand.php?brand=" + brand;
	url = url.replace("+", "_plus_");
	var xmlHTTP = getXMLHTTPObject();
	
	modelSelect.options[0] = new Option("", "-1");
	
	if(xmlHTTP)
	{		
		xmlHTTP.onreadystatechange  = function()
		{
			if(xmlHTTP.readyState == 4)
			{				
				var resultStr = xmlHTTP.responseText;				
				if(resultStr != "")
				{
					models = resultStr.split(";");
					for(i = 0; i < models.length; i++)
					{
						var pair = models[i].split(",");
						modelSelect.options[i + 1] = new Option(pair[1], pair[0]);
					}	
					
					modelSelect.disabled = false;
					modelSelect.style.backgroundColor = "#FFFFFF";
				}
			}
		}		
		xmlHTTP.open("GET", url, true);
		xmlHTTP.send(null);
	}
}

function updateChassisses(model)
{
	var chassisSelect = document.getElementById("chassisSelector");
	
	chassisSelect.disabled = true;
	chassisSelect.style.backgroundColor = "#AAAAAA";
	chassisSelect.options.length = 0;
	
	var productionYearSelect = document.getElementById("productionYearSelector");
	
	productionYearSelect.disabled = true;
	productionYearSelect.style.backgroundColor = "#AAAAAA";
	productionYearSelect.options.length = 0;	
	
	var engineSelect = document.getElementById("engineSelector");
	
	engineSelect.disabled = true;
	engineSelect.style.backgroundColor = "#AAAAAA";
	engineSelect.options.length = 0;	
	
	chassisSelect.options[0] = new Option("", "-1");
	
	var url = "/ajaxGetChassissesForModel.php?model=" + model + "&brand=" + document.getElementById("brandSelector").value;
	url = url.replace("+", "_plus_");
	var xmlHTTP = getXMLHTTPObject();
	
	if(xmlHTTP)
	{		
		xmlHTTP.onreadystatechange  = function()
		{
			if(xmlHTTP.readyState == 4)
			{				
				var resultStr = xmlHTTP.responseText;
				if(resultStr != "")
				{
					chassisses = resultStr.split(";");
					for(i = 0; i < chassisses.length; i++)
					{
						var pair = chassisses[i].split(",");
						chassisSelect.options[i + 1] = new Option(pair[1], pair[0]);
					}	
					
					chassisSelect.disabled = false;
					chassisSelect.style.backgroundColor = "#FFFFFF";
				}
			}
		}		
		xmlHTTP.open("GET", url, true);
		xmlHTTP.send(null);
	}
}

function updateProductionYears(chassis)
{
	var productionYearSelect = document.getElementById("productionYearSelector");
	
	productionYearSelect.disabled = true;
	productionYearSelect.style.backgroundColor = "#AAAAAA";
	productionYearSelect.options.length = 0;
	
	productionYearSelect.options[0] = new Option("", "-1");
	
	var engineSelect = document.getElementById("engineSelector");
	
	engineSelect.disabled = true;
	engineSelect.style.backgroundColor = "#AAAAAA";
	engineSelect.options.length = 0;	
	
	var url = "/ajaxGetProductionYearsForChassis.php?chassis=" + chassis + "&model=" + document.getElementById("modelSelector").value + "&brand=" + document.getElementById("brandSelector").value;
	url = url.replace("+", "_plus_");
	var xmlHTTP = getXMLHTTPObject();
	
	if(xmlHTTP)
	{		
		xmlHTTP.onreadystatechange  = function()
		{
			if(xmlHTTP.readyState == 4)
			{				
				var resultStr = xmlHTTP.responseText;
				if(resultStr != "")
				{
					productionYears = resultStr.split(";");
					for(i = 0; i < productionYears.length; i++)
					{
						var pair = productionYears[i].split(",");
						productionYearSelect.options[i + 1] = new Option(pair[1], pair[0]);
					}	
					
					productionYearSelect.disabled = false;
					productionYearSelect.style.backgroundColor = "#FFFFFF";
				}
			}
		}		
		xmlHTTP.open("GET", url, true);
		xmlHTTP.send(null);
	}
}

function updateEngines(productionYear)
{
	var engineSelect = document.getElementById("engineSelector");
	
	engineSelect.disabled = true;
	engineSelect.style.backgroundColor = "#AAAAAA";
	engineSelect.options.length = 0;
	
	engineSelect.options[0] = new Option("", "-1");
	
	var url = "/ajaxGetEnginesForProductionYear.php?productionYear=" + productionYear + "&chassis=" + document.getElementById("chassisSelector").value + "&model=" + document.getElementById("modelSelector").value + "&brand=" + document.getElementById("brandSelector").value;
	url = url.replace("+", "_plus_");
	var xmlHTTP = getXMLHTTPObject();
	
	if(xmlHTTP)
	{		
		xmlHTTP.onreadystatechange  = function()
		{
			if(xmlHTTP.readyState == 4)
			{				
				var resultStr = xmlHTTP.responseText;
				if(resultStr != "")
				{
					engines = resultStr.split(";");
					for(i = 0; i < engines.length; i++)
					{
						var pair = engines[i].split("|");
						engineSelect.options[i + 1] = new Option(pair[1], pair[0]);
					}	
					
					engineSelect.disabled = false;
					engineSelect.style.backgroundColor = "#FFFFFF";
				}
			}
		}		
		xmlHTTP.open("GET", url, true);
		xmlHTTP.send(null);
	}
}

function findProducts(lang)
{
	var brandSelect = document.getElementById("brandSelector");
	var modelSelect = document.getElementById("modelSelector");
	var chassisSelect = document.getElementById("chassisSelector");
	var productionYear = document.getElementById("productionYearSelector");
	var engine = document.getElementById("engineSelector");

	if(brandSelect.value != -1)
	{
		currentSelectedProduct = -1;
		var url = "/ajaxFindProducts.php?brand=" + brandSelect.value + "&model=" + modelSelect.value + "&chassis=" + chassisSelect.value + "&lang=" + lang + "&productionYear=" + productionYear.value + "&engine=" + engine.value;
		url = url.replace(/\+/g, "_plus_");
		var xmlHTTP = getXMLHTTPObject();

		if(xmlHTTP)
		{		
			xmlHTTP.onreadystatechange  = function()
			{
				if(xmlHTTP.readyState == 4)
				{		
					document.getElementById("instructions").style.display = "none";		
					document.getElementById("noProductsFound").style.display = "none";
					document.getElementById("productsFound").style.display = "none";
					
					var resultStr = xmlHTTP.responseText;
					if(resultStr == "NONE")
						document.getElementById("noProductsFound").style.display = "none";
					else
					{						
						document.getElementById("productOverview").innerHTML = resultStr;
						document.getElementById("productsFound").style.display = "block";
						
						if((engine.value == "") || (engine.value == "-1"))
						{
							document.getElementById("noProductSelected").style.display = "block";
							document.getElementById("productSelected").style.display = "none";
						}
						else
						{
							document.getElementById("noProductSelected").style.display = "none";
							document.getElementById("productSelected").style.display = "block";							
						}
					}
				}
			}		
			xmlHTTP.open("GET", url, true);
			xmlHTTP.send(null);
		}		
	}
}

function markProduct(id)
{
	document.getElementById("productCell1" + id).style.backgroundColor = "#EEEEEE";
	document.getElementById("productCell2" + id).style.backgroundColor = "#EEEEEE";
	document.getElementById("productCell3" + id).style.backgroundColor = "#EEEEEE";
	document.getElementById("productCell4" + id).style.backgroundColor = "#EEEEEE";
	
	document.getElementById("productCell1" + id).style.color = "#000000";
	document.getElementById("productCell2" + id).style.color = "#000000";
	document.getElementById("productCell3" + id).style.color = "#000000";
	document.getElementById("productCell4" + id).style.color = "#000000";	
}

function unMarkProduct(id)
{
	document.getElementById("productCell1" + id).style.backgroundColor = "#888888";
	document.getElementById("productCell2" + id).style.backgroundColor = "#888888";
	document.getElementById("productCell3" + id).style.backgroundColor = "#888888";
	document.getElementById("productCell4" + id).style.backgroundColor = "#888888";
	
	document.getElementById("productCell1" + id).style.color = "#FFFFFF";
	document.getElementById("productCell2" + id).style.color = "#FFFFFF";
	document.getElementById("productCell3" + id).style.color = "#FFFFFF";
	document.getElementById("productCell4" + id).style.color = "#FFFFFF";	
}

function selectProduct(id, lang)
{
	if(currentSelectedProduct != -1)
		unMarkProduct(currentSelectedProduct);
	markProduct(id);
	currentSelectedProduct = id;
	
	var url = "/ajaxGetProductDetails.php?id=" + id + "&lang=" + lang;
	var xmlHTTP = getXMLHTTPObject();
	
	if(xmlHTTP)
	{		
		xmlHTTP.onreadystatechange  = function()
		{
			if(xmlHTTP.readyState == 4)
			{						
				var resultStr = xmlHTTP.responseText;
				document.getElementById("productDetails").innerHTML = resultStr;
			}
		}		
		xmlHTTP.open("GET", url, true);
		xmlHTTP.send(null);
	}		

	document.getElementById("productDetails").style.display = "block";
}
