/* CSS Document */

if(typeof VehicleApplication=='undefined') {
    var VehicleApplication = {};
}

VehicleApplication.setYear = function(year_select){
	var reloadurl = '/navigation/application/index';  
	new Ajax.Request(
				reloadurl, 
				{ 
					method: 	"get", 
					parameters: "search_year=" + year_select.value, 
					onComplete: 
						function(transport)
						{
							//alert(transport.responseText);
							var theurl = location.href
							var path = location.pathname
							if (path != "/index.php" && path != "/")
							{	
								theurl = theurl.replace("p=","_p=");				// replace the page in the url
								theurl = theurl.replace("search_year=","lastsearchyear="); 	// replace the year in the url
								theurl = theurl.replace("#","&"); 				// stop the page form scrolling to an ahchor
								location.href = theurl;
							}
						}
				}
	);		
}

VehicleApplication.setModelID = function(model_id, refresh)
{
	//alert(model_select.value);
	var reloadurl = '/navigation/application/index';  
	new Ajax.Request(
				reloadurl, 
				{ 
					method: 	"get", 
					parameters: "search_model_id=" + model_id, 
					onComplete: 
						function(transport)
						{
							//alert(transport.responseText);
							var theurl = location.href
							var path = location.pathname
							if (path != "/index.php" && path != "/" && refresh == true)
							{	
								theurl = theurl.replace("p=","_p=");				// replace the page in the url
								theurl = theurl.replace("search_model_id=","lastsearchmodelid="); 	// replace the year in the url
								theurl = theurl.replace("#","&"); 				// stop the page form scrolling to an ahchor
								location.href = theurl;
							}
						}
				}
	);
	
}

VehicleApplication.setCarYear = function(year)
{
	// set the year cookie
	//document.cookie = "CarYear=" + year;
	
	// clear year and model
	// and refresh the results
	if(year == "ALL")
	{
		VehicleApplication.setModelID("0", true)
	}
}

VehicleApplication.callAHAH = function(url, pageElement, callMessage)
{
	var req; // we have multiple instances at the same time
	
	document.getElementById(pageElement).innerHTML = callMessage;
	
	try {
		req = new XMLHttpRequest();	/* fire fox */
	} catch(e) {
		try { 
			req = new ActiveXObject("Msxml2.XMLHTTP") /* some versions of IE */
		} catch(e){
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP") /* other versions of IE */
			} catch(E) {
				req = false;
			}
		}
	}
	// test to see it xmlHttp has been created
	if (req==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 
	req.onreadystatechange = function(){VehicleApplication.responseAHAH(pageElement, req);}
	req.open("GET", url, true);
	req.send(null);
}

VehicleApplication.responseAHAH = function(pageElement, req)
{
	var output = '';
	
	if(req.readyState == 4) {
		if(req.status == 200)
		{
			//alert(pageElement); debug
			output = req.responseText;
			document.getElementById(pageElement).innerHTML = output;
		}
		//if(req.status == 500)
		//{
		//	//alert(pageElement); debug
		//	output = req.responseText;
		//	document.getElementById(pageElement).innerHTML = output;
		//}
		if(req.status == 404)
		{
			//alert(pageElement); debug
			output = "404 not found";
			document.getElementById(pageElement).innerHTML = output;
		}
	}
}

