

// JavaScript Document
function nuevoAjax(){
	var xmlhttp=false;
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");  		// Creación del objeto ajax para navegadores diferentes a Explorer
	} catch (e) {
		try {								// o bien
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");		// Creación del objet ajax para Explorer
		} catch (E) {
			xmlhttp = false;
		}
	}

	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}






// objecte ajax per fer servir
var ajax;



// quan es retorna el httprequest, es crida aquesta funció per canviar els valors del combo
function canvia_models_final ()
{
	
		if (ajax.readyState==4) 		
		{
			cursor_normal();
			node = document.getElementById("model");
			
			//src = ajax.responseText;		// resposta en text
						
			node.length = 0;
			node.options[node.length] = new Option("", "0", false, false);
			
			response = ajax.responseXML.documentElement;		// resposta en xml

			if(response != null)
			{
							
				models = response.getElementsByTagName('model');

				// recorrem tots els models del xml 			
				for (var i = 0; i < models.length; i++) 
				{ 
   					model_actual = models[i];
					codi = model_actual.getElementsByTagName('id')[0].firstChild.data;
					descripcio = model_actual.getElementsByTagName('descripcio')[0].firstChild.data;
					node.options[node.length] = new Option(descripcio, codi, false, false);		
					
				}
			}
		}	
}


function canvia_models_final_1 ()
{
	
		if (ajax1.readyState==4) 		
		{
			cursor_normal();
			node = document.getElementById("model");
			
			//src = ajax.responseText;		// resposta en text
						
			node.length = 0;
			node.options[node.length] = new Option("", "0", false, false);
			
			response = ajax1.responseXML.documentElement;		// resposta en xml

			if(response != null)
			{
							
				models = response.getElementsByTagName('model');

				// recorrem tots els models del xml 			
				for (var i = 0; i < models.length; i++) 
				{ 
   					model_actual = models[i];
					codi = model_actual.getElementsByTagName('id')[0].firstChild.data;
					descripcio = model_actual.getElementsByTagName('descripcio')[0].firstChild.data;
					node.options[node.length] = new Option(descripcio, codi, false, false);					
				}
			}
		}	
		
		document.forms['form_buscador_vehicles'].elements['marca'].selectedIndex.value="1";
		
		

}




// anem a buscar les dades a l'aplicació
function canvia_models ()
{
	marca = document.getElementById("marca");	
	codis = String(marca.options[marca.selectedIndex].value);
	codis = codis.split("-");
	codi_marca = codis[0];
	codi_entitat = codis[1];	
	
	//si estem a spw i seleccionem 1 marca que ens mostri la pagina amb aspecte marca seleccionada
	/*if (document.forms['form_buscador_vehicles'].mostra_marca.value == 1)
	{
		if (codi_marca == 22) document.forms['form_buscador_vehicles'].action = "audi.php";
		if (codi_marca == 11) document.forms['form_buscador_vehicles'].action = "volkswagen.php";
		if (codi_marca == 33) document.forms['form_buscador_vehicles'].action = "comercials.php";
	}
	*/
	

	url = pagina_principal + "?md=ajax&accio=models_de_marca&codi_marca=" + codi_marca + "&codi_entitat=" + codi_entitat;
	//url = "prova.xml"			
	ajax=nuevoAjax();
	ajax.open("GET", url ,true);

	ajax.onreadystatechange=canvia_models_final;

	ajax.send(null)
}



// anem a buscar les dades a l'aplicació
function carrega_models()
{
	
	
	//if (document.getElementById("num_portal").value != 1)	
	if (document.forms['form_buscador_vehicles'].elements['num_portal'].value != 1)
	{
		
		cursor_espera();
		/*marca = document.getElementById("codi_marca").value;				
		codis = marca.split("-");
		codi_marca = codis[0];
		codi_entitat = codis[1];*/		
		if (document.forms['form_buscador_vehicles'].elements['num_portal'].value == 2)
		{
			codi_marca = 22;
			codi_entitat = 12;			
			
		}
		
		if (document.forms['form_buscador_vehicles'].elements['num_portal'].value == 3)
		{
			codi_marca = 11;
			codi_entitat = 12;
			
		}	
		
		
		//if (document.getElementById("num_portal").value == 4)		
		if (document.forms['form_buscador_vehicles'].elements['num_portal'].value == 4)
		{
			codi_marca = 33;
			codi_entitat = 12;			
		}
	
		url = pagina_principal + "?md=ajax&accio=models_de_marca&codi_marca=" + codi_marca + "&codi_entitat=" + codi_entitat;
		
		ajax1=nuevoAjax();
		
		ajax1.open("GET", url ,true);
	
		ajax1.onreadystatechange=canvia_models_final_1;
	
		ajax1.send(null);
	}
	
	
	
	
	
	
}



// quan es retorna el httprequest, es crida aquesta funció per canviar els valors del combo
function canvia_marques_final ()
{
		
		if (ajax.readyState==4) 		
		{
		
			cursor_normal();	
			//fiquem a cero el combo models per si tenia algun valor seleccionat
			node_model = document.getElementById("model");
			node_model.length = 0;
			node_model.options[node_model.length] = new Option("", "0", false, false);
			// fi fiquem a cero el combo models per si tenia algun valor seleccionat
			
			
			node = document.getElementById("marca");
			
					
			//src = ajax.responseText;		// resposta en text
					
			node.length = 0;
			node.options[node.length] = new Option("", "0", false, false);
			
			response = ajax.responseXML.documentElement;		// resposta en xml
				
			
			if(response != null)
			{
			
				marques = response.getElementsByTagName('marca');
				
			
						
				// recorrem totes les marques del xml 			
				for (var i = 0; i < marques.length; i++) 
				{ 
   					marca_actual = marques[i];
					codi = marca_actual.getElementsByTagName('id')[0].firstChild.data;
					descripcio = marca_actual.getElementsByTagName('descripcio')[0].firstChild.data;
					node.options[node.length] = new Option(descripcio, codi, false, false);										
									
				}
			}
		}
		
			
}


function canvia_marques_final_stock()
{
		
		if (ajax.readyState==4) 		
		{
		
			cursor_normal();	
			//fiquem a cero el combo models per si tenia algun valor seleccionat
			node_model = document.getElementById("model");
			node_model.length = 0;
			node_model.options[node_model.length] = new Option("", "0", false, false);
			// fi fiquem a cero el combo models per si tenia algun valor seleccionat
			
			
			node = document.getElementById("marca");
			
					
			//src = ajax.responseText;		// resposta en text
					
			node.length = 0;
			node.options[node.length] = new Option("", "0", false, false);
			
			response = ajax.responseXML.documentElement;		// resposta en xml
				
			
			if(response != null)
			{
			
				marques = response.getElementsByTagName('marca');
				
				//alert();
				
						
				// recorrem totes les marques del xml 			
				for (var i = 0; i < marques.length; i++) 
				{ 
   					marca_actual = marques[i];
					codi = marca_actual.getElementsByTagName('id')[0].firstChild.data;
					descripcio = marca_actual.getElementsByTagName('descripcio')[0].firstChild.data;
					
										
					//portal spw					
					if (document.forms['form_buscador_vehicles'].elements['num_portal'].value == 1)
					{
						node.options[node.length] = new Option(descripcio, codi, false, false);
					}
					//portal audi 
					if (document.forms['form_buscador_vehicles'].elements['num_portal'].value == 2)
					{
						if (codi == "22-12") 
						{
							node.options[node.length] = new Option(descripcio, codi, false, true);
							document.forms['form_buscador_vehicles'].action = "audi.php";
						}	
						else  node.options[node.length] = new Option(descripcio, codi, false, false);
					}
					//volkswagen
					if (document.forms['form_buscador_vehicles'].elements['num_portal'].value == 3)
					{
						if (codi == "11-12") 
						{
							node.options[node.length] = new Option(descripcio, codi, false, true);
							document.forms['form_buscador_vehicles'].action = "volkswagen.php";
						}
						else  node.options[node.length] = new Option(descripcio, codi, false, false);
					}
					//comerciales
					if (document.forms['form_buscador_vehicles'].elements['num_portal'].value == 4)
					{
						if (codi == "33-12")
						{
							node.options[node.length] = new Option(descripcio, codi, false, true);
							document.forms['form_buscador_vehicles'].action = "comercials.php";
						}	
						else  node.options[node.length] = new Option(descripcio, codi, false, false);
					}
					
															
									
				}
			}
		}
		
			
}





// anem a buscar les dades a l'aplicació
function carrega_marques_buscador_spwcars()
{

	cursor_espera();
	codi_entitat = 9;
	document.forms['form_buscador_vehicles'].action = "spw.php";
	
		
	/*fem apareixer el combo del any i amagem el de la disponibilitat */
	//document.getElementById("cercador_any").style.display="block"; 	
	
		
	document.getElementById("cercador_marca").style.display="block";
	
		
	// ara sempre sera visible document.getElementById("cercador_disponibilitat").style.display="none"; 
	/* fi fem apareixer el combo del any i amagem el de la disponibilitat */
	
	/*fiquem tots els valors en blanc */	
	/* aixo amb el firefox no funciona
	document.getElementById("combustible").value="";
	document.getElementById("preu_min").value=""; 
	document.getElementById("preu_max").value="";*/	
	document.forms['form_buscador_vehicles'].elements['combustible'].value = "";
	document.forms['form_buscador_vehicles'].elements['preu_min'].value = "";
	document.forms['form_buscador_vehicles'].elements['preu_max'].value = "";	
	//document.getElementById("any").value="";
	/* fi fiquem tots els valors en blanc */
	
	
	
	document.forms['form_buscador_vehicles'].elements['filtrar_marca'].value ="9";
	
	
		
	
	url = pagina_principal + "?md=ajax&accio=carrega_marques&codi_entitat=" + codi_entitat;
		
		
	ajax=nuevoAjax();
	ajax.open("GET", url ,true);

	ajax.onreadystatechange=canvia_marques_final;

	ajax.send(null);
	
	
}


function carrega_marques_buscador_stock()
{
	cursor_espera();
	codi_entitat = 12;
	

		
	/*fem apareixer el combo de la disponibilitat i amagem el combo de l'any */
	//document.getElementById("cercador_any").style.display="none"; 
	// ara sempre sera visible document.getElementById("cercador_disponibilitat").style.display="block"; 	
	/* fi fem apareixer el combo de la disponibilitat i amagem el combo de l'any */
	
	
	//fem apareixer sempre combo marca al vehicle nou
	document.getElementById("cercador_marca").style.display="block";
	
	/*
	modifiquem que nomes aparegui combo marca al spw ara apareix tb al vehicle nou
	if (document.forms['form_buscador_vehicles'].elements['num_portal'].value == 1)
		document.getElementById("cercador_marca").style.display="block";
	else
	{
		document.getElementById("cercador_marca").style.display="none";			
	}
	fi modifiquem que nomes aparegui combo marca al spw ara apareix tb al vehicle nou
	*/	
	
	
	/*fiquem tots els valors en blanc */
	document.forms['form_buscador_vehicles'].elements['combustible'].value = "";
	document.forms['form_buscador_vehicles'].elements['preu_min'].value = "";
	document.forms['form_buscador_vehicles'].elements['preu_max'].value = "";
	/* fi fiquem tots els valors en blanc */
	
	document.forms['form_buscador_vehicles'].elements['filtrar_marca'].value ="";

	/* si em seleccionat marca al spwcars la fiquem a cero pq surti correcte la búsqueda */	
	marca1 = document.getElementById("marca");		
	codis1 = String(marca1.options[marca1.selectedIndex].value);
	codis1 = codis1.split("-");
	codi_marca1 = codis1[0];
	codi_entitat1 = codis1[1];	

	
	if (codi_entitat1 == 9)
		document.forms['form_buscador_vehicles'].elements['marca'].value = "0";		
	/* fi si em seleccionat marca al spwcars la fiquem a cero pq surti correcte la búsqueda */
	
		
	
		
			
	url = pagina_principal + "?md=ajax&accio=carrega_marques&codi_entitat=" + codi_entitat;
	
	ajax=nuevoAjax();
	ajax.open("GET", url ,true);

	ajax.onreadystatechange=canvia_marques_final_stock;
	ajax.send(null);	
	
	
	
	
}


function cursor_espera()
{	
	document.body.style.cursor='wait';	
	//desabilitem camps formulari fins que estiguin carregats
	/*document.forms['form_buscador_vehicles'].elements['marca'].disabled=true;
	document.forms['form_buscador_vehicles'].elements['model'].disabled=true;
	document.forms['form_buscador_vehicles'].elements['combustible'].disabled=true;	
	document.forms['form_buscador_vehicles'].elements['disponibilitat'].disabled=true;*/	
	//fi desabilitem camps formulari fins que estiguin carregats
}

function cursor_normal()
{
	document.body.style.cursor='auto';
	//habilitem camps formulari ja estan carregats
	/*document.forms['form_buscador_vehicles'].elements['marca'].disabled=false;
	document.forms['form_buscador_vehicles'].elements['model'].disabled=false;
	document.forms['form_buscador_vehicles'].elements['combustible'].disabled=false;
	document.forms['form_buscador_vehicles'].elements['disponibilitat'].disabled=false;*/
	//fi habilitem camps formulari ja estan carregats
}