
// declare a global  XMLHTTP Request object
var XmlHttpObj;

// create an instance of XMLHTTPRequest Object, varies with browser type, try for IE first then Mozilla
function CreateXmlHttpObj()
{
	// try creating for IE (note: we don't know the user's browser type here, just attempting IE first.)
	try
	{
		XmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttpObj = null;
		}
	}
	// if unable to create using IE specific code then try creating for Mozilla (FireFox) 
	if(!XmlHttpObj && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttpObj = new XMLHttpRequest();
	}
}

// called from onChange or onClick event of the make dropdown list
function MakeListOnChange() 
{
	var modeloption=document.getElementById("modelsList");
	modeloption.disabled=false;
    var makeList = document.getElementById("makeList");
    
    // get selected make from dropdown list
    var selectedMake = makeList.options[makeList.selectedIndex].value;
//    if (selectedMake!=0)
//    {
    	
    // url of page that will send xml data back to client browser
    var requestUrl;
    requestUrl = "/scripts/axdp.php" + "?make_id=" + encodeURIComponent(selectedMake);
    
	CreateXmlHttpObj();
	
	// verify XmlHttpObj variable was successfully initialized
	if(XmlHttpObj)
	{
        // assign the MakeChangeHandler function ( defined below in this file)
        // to be called when the state of the XmlHttpObj changes
        // receiving data back from the server is one such change
		XmlHttpObj.onreadystatechange = MakeChangeHandler;
		
		// define the iteraction with the server -- true for as asynchronous.
		XmlHttpObj.open("GET", requestUrl,  true);
		
		// send request to server, null arg  when using "GET"
		XmlHttpObj.send(null);		
	}
    //}
}


// this function called when state of  XmlHttpObj changes
// we're interested in the state that indicates data has been
// received from the server
function MakeChangeHandler()
{
	// 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)
		{			
			PopulateModelList(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}
}

// populate the contents of the model dropdown list
function PopulateModelList(modelNode)
{
    var modelsList = document.getElementById("modelsList");
	// clear the model list 
	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;
	// populate the dropdown list with data from the xml doc
	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;
	}
}

// returns the node text value 
function GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}


///////////////////////////////////////////////////////////////REPETIMOS

function ZIPListOnChange() 
{
	var modeloption=document.getElementById("zipList");
	modeloption.disabled=true;
    var stateList = document.getElementById("stateList");
    
    // get selected make from dropdown list
    var selectedState = stateList.options[stateList.selectedIndex].value;

    	//alert(selectedState);
    // url of page that will send xml data back to client browser
   var requestUrl;
    requestUrl = "/scripts/axdp.php" + "?state_id=" + encodeURIComponent(selectedState);
    
	CreateXmlHttpObj();
	
	// verify XmlHttpObj variable was successfully initialized
	if(XmlHttpObj)
	{
        // assign the StateChangeHandler function ( defined below in this file)
        // to be called when the state of the XmlHttpObj changes
        // receiving data back from the server is one such change
		XmlHttpObj.onreadystatechange = StateChangeHandler;
		
		// define the iteraction with the server -- true for as asynchronous.
		XmlHttpObj.open("GET", requestUrl,  true);
		
		// send request to server, null arg  when using "GET"
		XmlHttpObj.send(null);		
	}
   modeloption.disabled=false;


	
}

function ZIPListOnChange2() 
{
	var modeloption=document.getElementById("zipList");
	modeloption.disabled=true;
    var stateList = document.getElementById("townCityList");
    
    // get selected make from dropdown list
    var selectedState = stateList.options[stateList.selectedIndex].value;

    //	alert(selectedState);
    // url of page that will send xml data back to client browser
   var requestUrl;
    requestUrl = "/scripts/axdp.php" + "?state_id2=" + encodeURIComponent(selectedState);
    
	CreateXmlHttpObj();
	
	// verify XmlHttpObj variable was successfully initialized
	if(XmlHttpObj)
	{
        // assign the StateChangeHandler function ( defined below in this file)
        // to be called when the state of the XmlHttpObj changes
        // receiving data back from the server is one such change
		XmlHttpObj.onreadystatechange = StateChangeHandler;
		
		// define the iteraction with the server -- true for as asynchronous.
		XmlHttpObj.open("GET", requestUrl,  true);
		
		// send request to server, null arg  when using "GET"
		XmlHttpObj.send(null);		
	}
   modeloption.disabled=false;


	
}



function CountryListOnChange() 
{
	var modeloption=document.getElementById("townCityList");
	modeloption.enabled=false;
    var stateList = document.getElementById("stateList");
    
    // get selected make from dropdown list
    var selectedState = stateList.options[stateList.selectedIndex].value;
  	
   
	 toma2(encodeURIComponent(selectedState));
	
	modeloption.disabled=false;

	

}


function toma2(parametro)
	{
		
		// url of page that will send xml data back to client browser
			var requestUrl2;
			requestUrl2 = "/scripts/axdp.php" + "?state_id_to_town=" + parametro;
			
			CreateXmlHttpObj();
			
			// verify XmlHttpObj variable was successfully initialized
			if(XmlHttpObj)
			{
				// assign the StateChangeHandler function ( defined below in this file)
				// to be called when the state of the XmlHttpObj changes
				// receiving data back from the server is one such change
				XmlHttpObj.onreadystatechange = StateChangeHandler2;
				
				// define the iteraction with the server -- true for as asynchronous.
				XmlHttpObj.open("GET", requestUrl2,  true);
				
				// send request to server, null arg  when using "GET"
				XmlHttpObj.send(null);		
			}
			//}

	}

// this function called when state of  XmlHttpObj changes
// we're interested in the state that indicates data has been
// received from the server
function StateChangeHandler()
{
	// 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)
		{			
			PopulateZIPList(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}
}


// this function called when state of  XmlHttpObj changes
// we're interested in the state that indicates data has been
// received from the server
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)
		{			
			PopulateTownList(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}
}



// populate the contents of the model dropdown list
function PopulateZIPList(zipNode)
{
    var zipList = document.getElementById("zipList");
	// clear the model list 
	for (var count = zipList.options.length-1; count >-1; count--)
	{
		zipList.options[count] = null;
	}

	var zipNodes = zipNode.getElementsByTagName('zip');
	var idValue;
	var textValue; 
	var optionItem;
	// populate the dropdown list with data from the xml doc
	for (var count = 0; count < zipNodes.length; count++)
	{
   		textValue = GetInnerText(zipNodes[count]);
		idValue = zipNodes[count].getAttribute("id");
		optionItem = new Option( textValue, idValue,  false, false);
		zipList.options[zipList.length] = optionItem;
	}
}
// populate the contents of the model dropdown list
function PopulateTownList(townNode)
{

	
    var townList = document.getElementById("townCityList");
	// clear the model list 
	for (var count = townList.options.length-1; count >-1; count--)
	{
		townList.options[count] = null;
	}

	var townNodes = townNode.getElementsByTagName('town');
	var idValue;
	var textValue; 
	var optionItem;
	// populate the dropdown list with data from the xml doc
    textValue='Select a town/city';
    idValue= -1;
    townList.options[townList.length] = new Option( textValue, idValue);
	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);
	}
	

	


}









