var url = "location_controller.php";
var loadingStates = "SetLoadingStates()";
var loadingCities = "SetLoadingCities()";
var loadingNeighborhoods = "SetLoadingNeighborhoods()";

function SetLoadingStates()
{
	var loadingStates = document.getElementById("loadingStates");                                             
	loadingStates.innerHTML = "<img alt='Loading'  height='21' width='21' src='images/loading.gif' style='vertical-align:bottom' />";
}

function SetLoadingCities()
{
	var loadingCities = document.getElementById("loadingCities");                                             
	loadingCities.innerHTML = "<img alt='Loading'  height='21' width='21' src='images/loading.gif' style='vertical-align:bottom' />";
}

function SetLoadingNeighborhoods()
{
	var loadingNeighborhoods = document.getElementById("loadingNeighborhoods");                                             
	loadingNeighborhoods.innerHTML = "<img alt='Loading'  height='21' width='21' src='images/loading.gif' style='vertical-align:bottom' />";
}


var what = "SetStates(req.responseText)";

function GetStates(Country)
{
    DoCallbackStates("country="+Country);
}



function SetStates(States)
{
	 
    var stateBox = document.getElementById("state");
	var cityBox = document.getElementById("city");
	var neighborhoodBox = document.getElementById("neighborhood");

    cityBox.options.length = 0;
	cityBox.options[cityBox.options.length] = new Option("Seleccione la ciudad:", "");         
    neighborhoodBox.options.length = 0;

	neighborhoodBox.options[neighborhoodBox.options.length] = new Option("Seleccione un barrio:", "");         
	stateBox.options.length = 0;

    if(States != "")
    {
        var arrStates = States.split(";");
		stateBox.options[stateBox.options.length] = new Option("Seleccione la provincia o estado:", "");                    
        for(i = 0; i < arrStates.length; i++)
        {
            var state = arrStates[i].split(":");
            if(state[1] != "")
            {
            	var filterOption = document.createElement("OPTION");
				var selectText = document.createTextNode("");
                selectText = document.createTextNode(state[1]);
                filterOption.setAttribute("value", state[0]);
                filterOption.appendChild(selectText);
                
				if (state[0] == currentState)
				{
					filterOption.setAttribute("selected", "selected");    
				}
				
				stateBox.appendChild(filterOption);
            }
        }

    	if (currentState != '')
    	{
			GetCities(currentState, currentCountry);			
		}
	}



    var loadingStates = document.getElementById("loadingStates");                                             



    loadingStates.innerHTML = "";



    

}











//////////////////////////////////////////////////////////////////////////////////////////////////////



//////////////////////////////////////////////////////////////////////////////////////////////////////







var what2 = "SetCities(req.responseText)";







function GetCities(State, Country)

{                                

	          

	DoCallbackCities("state="+State+"&country_code="+Country);



}







function SetCities(Cities)



{



	var cityBox = document.getElementById("city");



	var neighborhoodBox = document.getElementById("neighborhood");



	



	neighborhoodBox.options.length = 0;



	neighborhoodBox.options[neighborhoodBox.options.length] = new Option("Seleccione un barrio:", "");         



	



	cityBox.options.length = 0;    



	if(Cities != "")

	{

		var arrCities = Cities.split(";");

		cityBox.options[cityBox.options.length] = new Option("Seleccione la ciudad:", "");



		for(i = 0; i < arrCities.length; i++)

		{

			var city = arrCities[i].split(":");

			if(city[1] != "")
			{
				var filterOption = document.createElement("OPTION");
				var selectText = document.createTextNode("");
                selectText = document.createTextNode(city[1]);
                filterOption.setAttribute("value", city[0]);
                filterOption.appendChild(selectText);
                
				if (city[0] == currentCity)
				{
					filterOption.setAttribute("selected", "selected");    
				}
				
				cityBox.appendChild(filterOption);
			}

		}

		

		if (currentCity != '')

		{

				GetNeighborhoods(currentCity);

		}

		



	}



	var loadingCities = document.getElementById("loadingCities");                                             



	loadingCities.innerHTML = "";







}







//////////////////////////////////////////////////////////////////////////////////////////////////////



//////////////////////////////////////////////////////////////////////////////////////////////////////







var what3 = "SetNeighborhoods(req.responseText)";







function GetNeighborhoods(City)

{                                



	DoCallbackNeighborhoods("city_id="+City);



}







function SetNeighborhoods(Neighborhoods)



{



	var neighborhoodBox = document.getElementById("neighborhood");



	neighborhoodBox.options.length = 0;







	if(Neighborhoods != "")



	{



		



		var arrNeighborhoods = Neighborhoods.split(";");



		



		neighborhoodBox.options[neighborhoodBox.options.length] = new Option("Seleccione el barrio:", "");



		for(i = 0; i < arrNeighborhoods.length; i++)



		{



			var neighborhood = arrNeighborhoods[i].split(":");



			if(neighborhood[1] != "")
			{

				var filterOption = document.createElement("OPTION");
				var selectText = document.createTextNode("");
                selectText = document.createTextNode(neighborhood[1]);
                filterOption.setAttribute("value", neighborhood[0]);
                filterOption.appendChild(selectText);
                
				if (neighborhood[0] == currentNeighborhood)
				{
					filterOption.setAttribute("selected", "selected");    
				}
				
				neighborhoodBox.appendChild(filterOption);
			}                                             
		}                                                                                                           
	}

	var loadingNeighborhoods = document.getElementById("loadingNeighborhoods");                                             
	loadingNeighborhoods.innerHTML = "";
}



	







function DoCallbackStates(data)



{



    // branch for native XMLHttpRequest object



    if (window.XMLHttpRequest) {



        req = new XMLHttpRequest();



        req.onreadystatechange = processReqChange;



        req.open('POST', url, true);



        req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');



        req.send(data);



    // branch for IE/Windows ActiveX version



    } else if (window.ActiveXObject) {



        req = new ActiveXObject('Microsoft.XMLHTTP')



        if (req) {



            req.onreadystatechange = processReqChange;



            req.open('POST', url, true);



            req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');



            req.send(data);



        }



    }



}







function processReqChange() {



    // only if req shows 'loaded'

     

    if (req.readyState == 4) 

    {



        // only if 'OK'



        if (req.status == 200) 

        {

            eval(what);

        } else 

        {

            alert('There was a problem retrieving the XML data: ' + req.responseText);

        }



    } else 

    {

		if (req.readyState == 1) 

		{ 

			eval(loadingStates) 

		}

	}



		



}







// JavaScript Document



function DoCallbackCities(data)



{



    // branch for native XMLHttpRequest object



    if (window.XMLHttpRequest) {



        req = new XMLHttpRequest();



        req.onreadystatechange = processReqChange2;



        req.open('POST', url, true);



        req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');



        req.send(data);



    // branch for IE/Windows ActiveX version



    } else if (window.ActiveXObject) {



        req = new ActiveXObject('Microsoft.XMLHTTP')



        if (req) {



            req.onreadystatechange = processReqChange2;



            req.open('POST', url, true);



            req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');



            req.send(data);



        }



    }



}







function processReqChange2() {



    // only if req shows 'loaded'



    if (req.readyState == 4) {



        // only if 'OK'



        if (req.status == 200) {



            eval(what2);



        } else {



            alert('There was a problem retrieving the XML data: ' +



                req.responseText);



        }



    }else {



				if (req.readyState == 1) { 



					eval(loadingCities) 



				}



			}



		



}







// JavaScript Document



function DoCallbackNeighborhoods(data)



{



    // branch for native XMLHttpRequest object



    if (window.XMLHttpRequest) {



        req = new XMLHttpRequest();



        req.onreadystatechange = processReqChange3;



        req.open('POST', url, true);



        req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');



        req.send(data);



    // branch for IE/Windows ActiveX version



    } else if (window.ActiveXObject) {



        req = new ActiveXObject('Microsoft.XMLHTTP')



        if (req) {



            req.onreadystatechange = processReqChange3;



            req.open('POST', url, true);



            req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');



            req.send(data);



        }



    }



}







function processReqChange3() {



    // only if req shows 'loaded'



    if (req.readyState == 4) {



        // only if 'OK'



        if (req.status == 200) {



            eval(what3);



        } else {



            alert('There was a problem retrieving the XML data: ' +



                req.responseText);



        }



    }else {



				if (req.readyState == 1) { 



					eval(loadingNeighborhoods) 



				}



			}



		



}