/*

Selector de provincias y localidades Ajax - non intrusive - Cross browser 

Copyright (C)Ińaki Gorostiza Esquerdeiro

http://www.hellogoogle.com

webmaster@hellogoogle.com 

This program is free software; you can redistribute it and/or

modify it under the terms of the GNU General Public License

as published by the Free Software Foundation; either version 2

of the License, or any later version.



This program is distributed in the hope that it will be useful,

but WITHOUT ANY WARRANTY; without even the implied warranty of

MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

GNU General Public License for more details.



You should have received a copy of the GNU General Public License

along with this program; if not, write to the Free Software

Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

*/

var busqueda="";

var XmlHttpObj;

var Utf8 = {



    //Convierte de UTF-8 a ISO

    decode : function (utftext) {

        var string = "";

        var i = 0;

        var c = c1 = c2 = 0;



        while ( i < utftext.length ) {



            c = utftext.charCodeAt(i);



            if (c < 128) {

                string += String.fromCharCode(c);

                i++;

            }

            else if((c > 191) && (c < 224)) {

                c2 = utftext.charCodeAt(i+1);

                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));

                i += 2;

            }

            else {

                c2 = utftext.charCodeAt(i+1);

                c3 = utftext.charCodeAt(i+2);

                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));

                i += 3;

            }



        }



        return string;

    }



}



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 provinciaOnChange(busquedaprov) {		

	var tolocload=document.getElementById("spanlocalidadesload");			

    var provincia = document.getElementById("provincia"); 

	var localidad = document.getElementById("localidad"); 

    var selectedprovincia = provincia.options[provincia.selectedIndex].value;

    var requestUrl;

	var pais = document.getElementById("paisid"); 
	
	busqueda=busquedaprov;

	tolocload.innerHTML="<img src='img/mini_loading.gif' align='absmiddle'>";
	
	if (provincia.value!='-1')
	{
		requestUrl = "recuperar_xml_ localidades.php" + "?filter=" + encodeURIComponent(selectedprovincia) + "&pais=" + pais.value + "&tipo=localidades&busqueda=" + busqueda;
	}else
	{
		requestUrl = "recuperar_xml_ localidades.php" + "?filter=none&tipo=localidades&busqueda=" + busqueda + "&pais=" + pais.value;
	}
	
	CreateXmlHttpObj();
	

	if(XmlHttpObj)

	{

	

		XmlHttpObj.onreadystatechange = StateChangeHandlerLoc;

		XmlHttpObj.open( "GET", requestUrl, true );

		XmlHttpObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

		XmlHttpObj.send('');		

	}		
		

}



function paisOnChange(busquedapais) {	

	

	var toproload=document.getElementById("spanprovinciasload");		

    var provincia = document.getElementById("provincia"); 

	var localidad = document.getElementById("localidad"); 

	var pais = document.getElementById("paisid"); 

    var selectedprovincia = provincia.options[provincia.selectedIndex].value;

    var requestUrl;

	busqueda=busquedapais;

	toproload.innerHTML="<img src='img/mini_loading.gif' align='absmiddle'>";



	requestUrl = "recuperar_xml_ localidades.php" + "?tipo=provincias&pais=" + pais.value + "&busqueda=" + busqueda;

	

	CreateXmlHttpObj();

	

	if(XmlHttpObj)

	{

	

		XmlHttpObj.onreadystatechange = StateChangeHandlerPro;

		XmlHttpObj.open( "GET", requestUrl, true );

		XmlHttpObj.setRequestHeader("Content-Type","text/html; charset=iso-8859-1");

		XmlHttpObj.send('');		

		

	}		



}



function StateChangeHandlerPro()

{

	// Si se ha recibido la información correctamente

	if (XmlHttpObj.readyState==4) {

		// si la información es válida 		

		if (XmlHttpObj.responseText.indexOf('invalid') == -1) 

		{ 		

			// obtener la respuesta 

			var msgs = XmlHttpObj.responseText.split('|');		

			// Buscamos la div

			document.getElementById("spanprovincias").innerHTML = msgs[0];

			document.getElementById("spanprovinciasload").innerHTML = "";

			provinciaOnChange(busqueda);

		}

		else {

			// Por si hay algun error 

			document.getElementById("spanprovincias").innerHTML = "Error llamando";

		}

	}

}



function StateChangeHandlerLoc()

{

	// Si se ha recibido la información correctamente

	if (XmlHttpObj.readyState==4) {

		// si la información es válida 		

		if (XmlHttpObj.responseText.indexOf('invalid') == -1) 

		{ 		

			// obtener la respuesta 

			var msgs = XmlHttpObj.responseText.split('|');		

			// Buscamos la div

			document.getElementById("spanlocalidades").innerHTML = msgs[0];

			document.getElementById("spanlocalidadesload").innerHTML = "";

		}

		else {

			// Por si hay algun error 

			document.getElementById("spanlocalidades").innerHTML = "Error llamando";

		}

	}

}



















