var PhotoHeight = 150;
var PhotoWidth = 300;
var Trims = "";

function ArrMake()
{
	var a = Makes.split(";");
	var b = document.getElementById("make").value;
	for (var i = 0; i < a.length; i++)
	{
		var c = a[i].split(":");
		if (b == c[0])
			return a[i].split(":");
	}
}

function ArrTrim()
{
	var a = Trims.split(";");
	var b = document.getElementById("trim").value;
	for (var i = 0; i < a.length; i++)
	{
		var c = a[i].split(":");
		if (b == c[0])
			return a[i].split(":");
	}
}

function FormFocus()
{
	var o = document.getElementById("zip");
	if (o.value == "")
		o.focus();
}

function FormSubmit(o)
{
	o.zip.value = OnlyNumbers(o.zip.value);
	if (o.zip.value.length != 5) {alert("Enter your 5-digit zip code."); o.zip.select(); return false;}
	OpenVeilLoading("400px", "Searching Dealer Prices", "Searching local dealers for pricing and availability.", "This may take up to 15-seconds.", "none");
	return true;
}

function FormSubmitCities()
{
	document.getElementById("veilCities").style.display = "none";
	document.getElementById("veilHead").style.display = "none";
	document.getElementById("veilCitiesLoading").style.display = "block";
	
	// Reset the animated GIF to work with problematic browsers.
	var p = document.getElementById("spinner");
	setTimeout(function() {p.src = p.src + "?rnd" + new Date().getTime()}, 200);
	
	return true;
}

function FormSubmitSimilar()
{
	document.getElementById("veilSimilar").style.display = "none";
	document.getElementById("veilHead").style.display = "none";
	document.getElementById("veilSimilarLoading").style.display = "block";
	
	// Reset the animated GIF to work with problematic browsers.
	var p = document.getElementById("spinner");
	setTimeout(function() {p.src = p.src + "?rnd" + new Date().getTime()}, 200);
	
	return true;
}

function LoadMakes(Id)
{
	var a = Makes.split(";");
	var o = document.getElementById("make"); o.length = 0;
	
	for (var i = 0; i < a.length; i++)
	{
		var b = a[i].split(":");
		o.options[i] = new Option(b[1]);
		o.options[i].value = b[0];
		if (Id == b[0])
			o.options[i].selected = true;
	}
}

function LoadModels(Yy,Id)
{
	var a = Models.split(";");
	var f = false;
	var j = 0;
	var o = document.getElementById("model"); o.length = 0;
	var x = document.getElementById("make").value;
	
	for (var i = 0; i < a.length; i++)
	{
		var b = a[i].split(":");
		if (x == b[0])
		{
			o.options[o.options.length] = new Option(b[4] + " " + b[2], b[4] + "," + b[1]);
			
			if (f == false && ((Id == "0" && Yy == b[4]) || (Id == b[1] && (Yy == "0" || Yy == b[4]))))
			{
				f = true;
				o.options[j].selected = true;
			}
			j++;
		}
	}
}

function LoadPhoto()
{
	var a = ArrMake();
	var b = ArrTrim();
	var p = document.getElementById("photo");
	var Year = document.getElementById("model").value.split(",")[0];
	p.className = "photo" + PhotoWidth + "x" + PhotoHeight;
	p.style.height = PhotoHeight + "px";
	p.style.width = PhotoWidth + "px";
	
	if (b[1] != "" && Year != "")
		p.src = "/photos/" + a[2] + "/" + Year + "/" + b[1] + "-" + PhotoWidth + "x" + PhotoHeight + ".jpg";
	else
		p.src = "/images/missing/" + PhotoWidth + "x" + PhotoHeight + ".jpg";
	
	// Update the photo alt and title.
	var x = document.getElementById("model")[document.getElementById("model").selectedIndex].text;
	x = x.substring(0,4) + " " + document.getElementById("make")[document.getElementById("make").selectedIndex].text + " " + x.substring(5,x.length) + " " + document.getElementById("trim")[document.getElementById("trim").selectedIndex].text;
	document.getElementById("photo").alt = x;
	document.getElementById("photo").title = x;
	document.title = x;
}

function LoadTrims(Id)
{
	var Year = document.getElementById("model").value.split(",")[0];
	var ModelId = document.getElementById("model").value.split(",")[1];
	
	if (window.ActiveXObject) {try {r = new ActiveXObject("Msxml2.XMLHTTP");} catch (e) {try {r = new ActiveXObject("Microsoft.XMLHTTP");} catch (e) {}}}
	else if (window.XMLHttpRequest) {r = new XMLHttpRequest();}//Mozilla, Safari, etc.
	else {return false;}
	r.onreadystatechange = function() {ReturnTrims(Id,r)};
	r.open("GET", "/quote/trims/?model="+ModelId+"&year="+Year+"&cache="+new Date().getTime(), true);// open(Method, Url, Async)
	r.send(null);
}

function ReturnTrims(Id,r)
{
	var p = document.getElementById("photo");
	p.style.height = "150px";
	p.style.width = "300px";
	p.src = Spin300x150.src
	
	if (r.readyState == 4 && (r.status==200 || window.location.href.indexOf("http")==-1))
	{
		Trims = r.responseText;
		var a = Trims.split(";");
		var j = 0;
		var o = document.getElementById("trim"); o.length = 0;
		
		for (var i = 0; i < a.length; i++)
		{
			var b = a[i].split(":");
			o.options[j] = new Option(b[2]);
			o.options[j].value = b[0];
			if (Id == b[0])
				o.options[j].selected = true;
			j++;
		}
		LoadPhoto();
	}
	else if (r.readyState == 4)
		alert("There was a connection timeout. Please try your selection again. If you continue to experience problems, please try a different browser such as Internet Explorer 7.0 (or above) or FireFox 2.0 (or above).");
}

function UpdateTitles(x)
{
	document.title = x;
	document.getElementById("h1").innerHTML = x;
}
