

var XmlHttpObj;


function CreateXmlHttpObj()
{
	
	try
	{
		XmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttpObj = null;
		}
	}
	
	if(!XmlHttpObj && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttpObj = new XMLHttpRequest();
	}
}


function MakeListOnChange(parametro) 
{
	var modeloption=document.getElementById("mod");
	modeloption.disabled=false;
    var makeList = document.getElementById("fab");
    
    
    var selectedMake = makeList.options[makeList.selectedIndex].value;

    var requestUrl;
    
	if (parametro==1)
	{
		requestUrl = "/scripts/axdp.php" + "?fab_id=" + encodeURIComponent(selectedMake)+"&qtm=1";
	}
	else
	{
		requestUrl = "/scripts/axdp.php" + "?fab_id=" + encodeURIComponent(selectedMake);
	}
    
	CreateXmlHttpObj();
	
	
	if(XmlHttpObj)
	{
        
		XmlHttpObj.onreadystatechange = MakeChangeHandler;
		
		
		XmlHttpObj.open("GET", requestUrl,  true);
		
		
		XmlHttpObj.send(null);		
	}
   
}



function MakeChangeHandler()
{
	
	if(XmlHttpObj.readyState == 4)
	{
		
		if(XmlHttpObj.status == 200)
		{			
			PopulateModelList(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}
}


function PopulateModelList(modelNode)
{
    var modelsList = document.getElementById("mod");
	
	for (var count = modelsList.options.length-1; count >-1; count--)
	{
		modelsList.options[count] = null;
	}

	var modelNodes = modelNode.getElementsByTagName('model');
	var idValue;
	var textValue; 
	var optionItem;
	
	for (var count = 0; count < modelNodes.length; count++)
	{
   		textValue = GetInnerText(modelNodes[count]);
		idValue = modelNodes[count].getAttribute("id");
		optionItem = new Option( textValue, idValue,  false, false);
		modelsList.options[modelsList.length] = optionItem;
	}
}


function GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}








///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// SEGUIMOS --------






function ProvinciaOnChange() 
{
	var poblacionoption=document.getElementById("pobid");
	poblacionoption.enabled=false;
    var ListadoProvincia = document.getElementById("provincia");
    
    // get selected make from dropdown list
    var selectedState = ListadoProvincia.options[ListadoProvincia.selectedIndex].value;
  	
   
	 toma2(encodeURIComponent(selectedState));
	
	poblacionoption.disabled=false;

	

}


function toma2(parametro)
{
		var requestUrl2;
		requestUrl2 = "/scripts/b-axdp.php" + "?prov_id=" + parametro;
		CreateXmlHttpObj();
		
		if(XmlHttpObj)
		{
			XmlHttpObj.onreadystatechange = StateChangeHandler2;
			XmlHttpObj.open("GET", requestUrl2,  true);
			XmlHttpObj.send(null);		
		}
}



function StateChangeHandler2()
{
	// state ==4 indicates receiving response data from server is completed
	if(XmlHttpObj.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpObj.status == 200)
		{			
			PoblarListaPoblaciones(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}
}




function PoblarListaPoblaciones(townNode)
{

	
    var townList = document.getElementById("pobid");
	// clear the model list 
	for (var count = townList.options.length-1; count >-1; count--)
	{
		townList.options[count] = null;
	}

	var townNodes = townNode.getElementsByTagName('poblacion');
	var idValue;
	var textValue; 
	var optionItem;
	// populate the dropdown list with data from the xml doc
	for (var count = 0; count < townNodes.length; count++)
	{
   		textValue = GetInnerText(townNodes[count]);
		idValue = townNodes[count].getAttribute("id");
		// alert(textValue+ "--"+ idValue);
		
		townList.options[townList.length] = new Option( textValue, idValue);
	}

}




/////////////////////////////////////////////////////////// POBLAMOS POBLACION Y CODIGO POSTA






function Provincia2OnChange()
{


    // Tratamiento para el código postal
    var cpoption=document.getElementById("cpid");
    cpoption.enabled=false;


    var ListadoProvincia = document.getElementById("provincia");

    // get selected make from dropdown list
    var selectedState = ListadoProvincia.options[ListadoProvincia.selectedIndex].value;

     toma4(encodeURIComponent(selectedState));
	 
     


	
    cpoption.disabled=false;


}




function toma4(parametro)
{


        // Tratamiento para el código postal
        var requestUrl3;
		requestUrl3 = "/scripts/c-axdp.php" + "?prov_id=" + parametro;
		CreateXmlHttpObj();

		if(XmlHttpObj)
		{
			XmlHttpObj.onreadystatechange = StateChangeHandler3;
			XmlHttpObj.open("GET", requestUrl3,  true);
			XmlHttpObj.send(null);
		}





}






function StateChangeHandler3()
{
	// state ==4 indicates receiving response data from server is completed
	if(XmlHttpObj.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpObj.status == 200)
		{
			PoblarListaCodigosPostales(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}
}







function PoblarListaCodigosPostales(townNode)
{


    var townList = document.getElementById("cpid");
	// clear the model list
	for (var count = townList.options.length-1; count >-1; count--)
	{
		townList.options[count] = null;
	}

	var townNodes = townNode.getElementsByTagName('codigo_postal');
	var idValue;
	var textValue;
	var optionItem;
	// populate the dropdown list with data from the xml doc
	for (var count = 0; count < townNodes.length; count++)
	{
   		textValue = GetInnerText(townNodes[count]);
		idValue = townNodes[count].getAttribute("id");
		// alert(textValue+ "--"+ idValue);

		townList.options[townList.length] = new Option( textValue, idValue);
	}

}
