/** KYB Function
 * ver:1
 * date:16/06/08
 * by: Nextia Studio´s (www.nextiastudios.com)
 **/
 // stores the reference to the XMLHttpRequest object
var xmlHttp = createXmlHttpRequestObject();

// load make list
var MakeUrl = "../../Util/Php/KYBMake.php";
// load model list
var ModelUrl = "../../Util/Php/KYBModel.php";
// load model list
var SModelUrl = "../../Util/Php/KYBSModel.php";
// load version list
var VersionUrl = "../../Util/Php/KYBVersion.php";
// load year list
var YearUrl = "../../Util/Php/KYBYear.php";
// loar parts number
var PartsUrl = "../../Util/Php/KYBPart.php";
// loar pic part
var picPartUrl = "../../Util/Php/KYBpicPart.php";

// creates an XMLHttpRequest instance
function createXmlHttpRequestObject(){
	// will store the reference to the XMLHttpRequest object
	var xmlHttp;
	// this should work for all browsers except IE6 and older
	try{
		// try to create XMLHttpRequest object
		xmlHttp = new XMLHttpRequest();
	}
	catch(e){
		// assume IE6 or older
		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
										"MSXML2.XMLHTTP.5.0",
										"MSXML2.XMLHTTP.4.0",
										"MSXML2.XMLHTTP.3.0",
										"MSXML2.XMLHTTP",
										"Microsoft.XMLHTTP");
		// try every prog id until one works
		for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++){
			try{
				// try to create XMLHttpRequest object
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
			}
			catch (e) {}
		}
	}
	// return the created object or display an error message
	if (!xmlHttp)
	alert("Error creating the XMLHttpRequest object.");
	else
	return xmlHttp;
}


function showhLogo(boxLetter,stepNo){
	document.getElementById(boxLetter).src = "/Images/Web/" + boxLetter + stepNo + ".gif";
}

function loadMake(){
// test if user has browser that supports native XSLT functionality
	if(window.XMLHttpRequest){
		// load the grid
		loadMakes();
		return;
	}
	// test if user has Internet Explorer with proper XSLT support
	if (window.ActiveXObject){
		// load the grid
		loadMakes();
		// exit the function
		return;
	}
	// if browser functionality testing failed, alert the user
	alert("Your browser doesn't support the necessary functionality.");
}

function loadMakes(){
	// disable edit mode when loading new page
	editableId = false;
	// continue only if the XMLHttpRequest object isn't busy
	if (xmlHttp && (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)){
		var query = MakeUrl + "?act=lmk" ;
		xmlHttp.open("GET", query, true);
		xmlHttp.onreadystatechange = handleMakeLoad;
		xmlHttp.send(null);
	}
}

function handleMakeLoad(){
	// when readyState is 4, we read the server response
	if (xmlHttp.readyState == 4 && xmlHttp.status == 200 ){
		// read the response
		document.getElementById("Loading").style.display = "none";
		response = xmlHttp.responseText;
		// server error?
		if (response.indexOf("ERRNO") >= 0 || response.indexOf("error") >= 0 || response.length == 0){
			// display error message
			alert(response.length == 0 ? "Server serror." : response);
			// exit function
			return;
		}
		// the server response in XML format
		xmlResponse = xmlHttp.responseText;
		// browser with native functionality?
		if (window.XMLHttpRequest || window.ActiveXObject){
			// display the page of products
			var gridDiv = document.getElementById("lstMake");
			gridDiv.innerHTML=xmlHttp.responseText;
		}
	}
	else{
	}
}


function loadModel(){
// test if user has browser that supports native XSLT functionality
	if(window.XMLHttpRequest){
		// load the grid
		loadModels();
		return;
	}
	// test if user has Internet Explorer with proper XSLT support
	if (window.ActiveXObject){
		// load the grid
		loadModels();
		// exit the function
		return;
	}
	// if browser functionality testing failed, alert the user
	alert("Your browser doesn't support the necessary functionality.");
}

function loadModels(){
	var make = document.getElementById('selMake').value;
	// disable edit mode when loading new page
	editableId = false;
	// continue only if the XMLHttpRequest object isn't busy
	if (xmlHttp && (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)){
		var query = ModelUrl + "?act=lmd&make=" + make;
		xmlHttp.open("GET", query, true);
		xmlHttp.onreadystatechange = handleModelLoad;
		xmlHttp.send(null);
	}
}

function handleModelLoad(){
	// when readyState is 4, we read the server response
	if (xmlHttp.readyState == 4 && xmlHttp.status == 200 ){
		// read the response
		document.getElementById("Loading").style.display = "none";
		response = xmlHttp.responseText;
		// server error?
		if (response.indexOf("ERRNO") >= 0 || response.indexOf("error") >= 0 || response.length == 0){
			// display error message
			alert(response.length == 0 ? "Server serror." : response);
			// exit function
			return;
		}
		// the server response in XML format
		xmlResponse = xmlHttp.responseText;
		// browser with native functionality?
		if (window.XMLHttpRequest || window.ActiveXObject){
			// display the page of products
			var gridDiv = document.getElementById("lstModel");
			gridDiv.innerHTML=xmlHttp.responseText;
		}
	}
	else{
	}
}

function loadSModel(){
// test if user has browser that supports native XSLT functionality
	if(window.XMLHttpRequest){
		// load the grid
		loadSModels();
		return;
	}
	// test if user has Internet Explorer with proper XSLT support
	if (window.ActiveXObject){
		// load the grid
		loadSModels();
		// exit the function
		return;
	}
	// if browser functionality testing failed, alert the user
	alert("Your browser doesn't support the necessary functionality.");
}

function loadSModels(){
	var make = document.getElementById('selMake').value;
	var model = document.getElementById('selModel').value;
	// disable edit mode when loading new page
	editableId = false;
	// continue only if the XMLHttpRequest object isn't busy
	if (xmlHttp && (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)){
		var query = SModelUrl + "?act=lsm&make=" + make + "&model=" + encodeURIComponent(model);
		xmlHttp.open("GET", query, true);
		xmlHttp.onreadystatechange = handleSModelLoad;
		xmlHttp.send(null);
	}
}

function handleSModelLoad(){
	// when readyState is 4, we read the server response
	if (xmlHttp.readyState == 4 && xmlHttp.status == 200 ){
		// read the response
		document.getElementById("Loading").style.display = "none";
		response = xmlHttp.responseText;
		// server error?
		if (response.indexOf("ERRNO") >= 0 || response.indexOf("error") >= 0 || response.length == 0){
			// display error message
			alert(response.length == 0 ? "Server serror." : response);
			// exit function
			return;
		}
		// the server response in XML format
		xmlResponse = xmlHttp.responseText;
		// browser with native functionality?
		if (window.XMLHttpRequest || window.ActiveXObject){
			// display the page of products
			var gridDiv = document.getElementById("lstSModel");
			gridDiv.innerHTML=xmlHttp.responseText;
		}
	}
	else{
	}
}

function loadVersion(){
// test if user has browser that supports native XSLT functionality
	if(window.XMLHttpRequest){
		// load the grid
		loadVersions();
		return;
	}
	// test if user has Internet Explorer with proper XSLT support
	if (window.ActiveXObject){
		// load the grid
		loadVersions();
		// exit the function
		return;
	}
	// if browser functionality testing failed, alert the user
	alert("Your browser doesn't support the necessary functionality.");
}

function loadVersions(){
	var make = document.getElementById('selMake').value;
	var model = document.getElementById('selModel').value;
	var smodel = document.getElementById('selSModel').value;
	// disable edit mode when loading new page
	editableId = false;
	// continue only if the XMLHttpRequest object isn't busy
	if (xmlHttp && (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)){
		var query = VersionUrl + "?act=lve&make=" + make + "&model=" + encodeURIComponent(model) + "&smodel=" + encodeURIComponent(smodel);
		xmlHttp.open("GET", query, true);
		xmlHttp.onreadystatechange = handleVersionsLoad;
		xmlHttp.send(null);
	}
}

function handleVersionsLoad(){
	// when readyState is 4, we read the server response
	if (xmlHttp.readyState == 4 && xmlHttp.status == 200 ){
		// read the response
		document.getElementById("Loading").style.display = "none";
		response = xmlHttp.responseText;
		// server error?
		if (response.indexOf("ERRNO") >= 0 || response.indexOf("error") >= 0 || response.length == 0){
			// display error message
			alert(response.length == 0 ? "Server serror." : response);
			// exit function
			return;
		}
		// the server response in XML format
		xmlResponse = xmlHttp.responseText;
		// browser with native functionality?
		if (window.XMLHttpRequest || window.ActiveXObject){
			// display the page of products
			var gridDiv = document.getElementById("lstVersion");
			gridDiv.innerHTML=xmlHttp.responseText;
		}
	}
	else{
	}
}

function loadYear(){
// test if user has browser that supports native XSLT functionality
	if(window.XMLHttpRequest){
		// load the grid
		loadYears();
		return;
	}
	// test if user has Internet Explorer with proper XSLT support
	if (window.ActiveXObject){
		// load the grid
		loadYears();
		// exit the function
		return;
	}
	// if browser functionality testing failed, alert the user
	alert("Your browser doesn't support the necessary functionality.");
}

function loadYears(){
	var make = document.getElementById('selMake').value;
	var model = document.getElementById('selModel').value;
	var smodel = document.getElementById('selSModel').value;
	var version = document.getElementById('selVersion').value;
	// disable edit mode when loading new page
	editableId = false;
	// continue only if the XMLHttpRequest object isn't busy
	if (xmlHttp && (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)){
		var query = YearUrl + "?act=lyr&make=" + make + "&model=" + encodeURIComponent(model) + "&smodel=" + encodeURIComponent(smodel) + "&version=" + encodeURIComponent(version);
		xmlHttp.open("GET", query, true);
		xmlHttp.onreadystatechange = handleYearsLoad;
		xmlHttp.send(null);
	}
}

function handleYearsLoad(){
	// when readyState is 4, we read the server response
	if (xmlHttp.readyState == 4 && xmlHttp.status == 200 ){
		// read the response
		document.getElementById("Loading").style.display = "none";
		response = xmlHttp.responseText;
		// server error?
		if (response.indexOf("ERRNO") >= 0 || response.indexOf("error") >= 0 || response.length == 0){
			// display error message
			alert(response.length == 0 ? "Server serror." : response);
			// exit function
			return;
		}
		// the server response in XML format
		xmlResponse = xmlHttp.responseText;
		// browser with native functionality?
		if (window.XMLHttpRequest || window.ActiveXObject){
			// display the page of products
			var gridDiv = document.getElementById("lstYear");
			gridDiv.innerHTML=xmlHttp.responseText;
		}
	}
	else{
	}
}


// eveything starts here
function loadPart(){
// test if user has browser that supports native XSLT functionality
	if(window.XMLHttpRequest){
		// load the grid
		loadParts();
		return;
	}
	// test if user has Internet Explorer with proper XSLT support
	if (window.ActiveXObject){
		// load the grid
		loadParts();
		// exit the function
		return;
	}
	// if browser functionality testing failed, alert the user
	alert("Your browser doesn't support the necessary functionality.");
	document.getElementById('SelYear').innerHTML="<option>Aņos</option>";
}

// makes asynchronous request to load a new page of the grid
function loadParts(){
	var make = document.getElementById('selMake').value;
	var model = document.getElementById('selModel').value;
	var smodel = document.getElementById('selSModel').value;
	var version = document.getElementById('selVersion').value;
	var year = document.getElementById('selYear').value;
	// disable edit mode when loading new page
	editableId = false;
	// continue only if the XMLHttpRequest object isn't busy
	if (xmlHttp && (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)){
		var query = PartsUrl + "?act=lpt&make=" + make + "&model=" + encodeURIComponent(model) + "&smodel=" + encodeURIComponent(smodel) + "&version=" + encodeURIComponent(version) + "&year=" + year;
		xmlHttp.open("GET", query, true);
		xmlHttp.onreadystatechange = handlePartLoad;
		xmlHttp.send(null);
	}
}

// handle receiving the server response with a new page of products
function handlePartLoad(){
	// when readyState is 4, we read the server response
	if (xmlHttp.readyState == 4 && xmlHttp.status == 200 ){
		// read the response
		document.getElementById("Loading").style.display = "none";
		response = xmlHttp.responseText;
		// server error?
		if (response.indexOf("ERRNO") >= 0 || response.indexOf("error") >= 0 || response.length == 0){
			// display error message
			alert(response.length == 0 ? "Server serror." : response);
			// exit function
			return;
		}
		// the server response in XML format
		xmlResponse = xmlHttp.responseText;
		// browser with native functionality?
		if (window.XMLHttpRequest || window.ActiveXObject){
			// display the page of products
			var gridDiv = document.getElementById("lstPart");
			gridDiv.innerHTML=xmlHttp.responseText;
		}
	}
	else{
	}
}

function goBack(){
	window.history.back()
}
 
function showSubMenu(nombre,sn,seccion){
	obj = document.getElementById(nombre);
	menu = document.getElementById(seccion);
	if (sn>0){
		obj.style.visibility = "visible";
		menu.style.background = "orangered";
	} else {
		obj.style.visibility = "hidden";
		menu.style.background = "red";
	}
}

function showPicAmortiguador(picAmortiguador){
// test if user has browser that supports native XSLT functionality
	if(window.XMLHttpRequest){
		// load the grid
		loadPicAmortiguador(picAmortiguador);
		return;
	}
	// test if user has Internet Explorer with proper XSLT support
	if (window.ActiveXObject){
		// load the grid
		loadPicAmortiguador(picAmortiguador);
		// exit the function
		return;
	}
	// if browser functionality testing failed, alert the user
	alert("Your browser doesn't support the necessary functionality.");
}

function loadPicAmortiguador(picAmortiguador){

	var year = document.getElementById('selYear').value;
	// disable edit mode when loading new page
	editableId = false;
	// continue only if the XMLHttpRequest object isn't busy
	if (xmlHttp && (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)){
		var query = picPartUrl + "?act=lpp&picPart=" + picAmortiguador;
		xmlHttp.open("GET", query, true);
		xmlHttp.onreadystatechange = handlePicAmortiguador;
		xmlHttp.send(null);
	}
}

// handle receiving the server response with a new page of products
function handlePicAmortiguador(){
	// when readyState is 4, we read the server response
	if (xmlHttp.readyState == 4 && xmlHttp.status == 200 ){
		// read the response
		//document.getElementById("LoadingPt").style.display = "none";
		response = xmlHttp.responseText;
		// server error?
		if (response.indexOf("ERRNO") >= 0 || response.indexOf("error") >= 0 || response.length == 0){
			// display error message
			alert(response.length == 0 ? "Server serror." : response);
			// exit function
			return;
		}
		// the server response in XML format
		xmlResponse = xmlHttp.responseText;
		// browser with native functionality?
		if (window.XMLHttpRequest || window.ActiveXObject){
			// display the page of products
			var gridDiv = document.getElementById("tblPicAmortiguador");
			gridDiv.innerHTML=xmlHttp.responseText;
		}
	}
	else{
	}
}