/** XHConn - Simple XMLHTTP Interface - bfults@gmail.com - 2005-04-08        **
 ** Code licensed under Creative Commons Attribution-ShareAlike License      **
 ** http://creativecommons.org/licenses/by-sa/2.0/                           **/
 
 // Loading preload
 
 var img = new Image();
 img.src = '/img/common/loader.gif';
 
function XHConn()
{
  var xmlhttp, bComplete = false;
  try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch (e) { try { xmlhttp = new XMLHttpRequest(); }
  catch (e) { xmlhttp = false; }}}
  if (!xmlhttp) return null;
  this.connect = function(sURL, sMethod, sVars, fnDone)
  {
    if (!xmlhttp) return false;
    bComplete = false;
    sMethod = sMethod.toUpperCase();

    try {
      if (sMethod == "GET")
      {
        xmlhttp.open(sMethod, sURL+"?"+sVars, true);
        sVars = "";
      }
      else
      {
        xmlhttp.open(sMethod, sURL, true);
        xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type",
          "application/x-www-form-urlencoded");
      }
      xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4 && !bComplete)
        {
          bComplete = true;
		  if(typeof(fnDone) == 'object') {
			  fnDone.run(xmlhttp);
		  } else {
	          fnDone(xmlhttp);
		  }
        }};
      xmlhttp.send(sVars);
    }
    catch(z) { return false; }
    return true;
  };
  return this;
}

function page(targetId,url,params,handler,method) {
	if(method == null) { method = 'GET'; }
	if(typeof(targetId) == 'object') {
		img = document.createElement('img');
		img.src = '/img/common/loader.gif';
		img.alt = 'Cargando...';	
		targetId.parentNode.replaceChild(img,targetId);
	} else {
		target= document.getElementById(targetId);
		if(target) {
			target.innerHTML = '<img src="/img/common/loader.gif" alt="Cargando..." class="loading" />';
		}
	}
var myConn = new XHConn();
    if (!myConn) alert("XMLHTTP not available. Try a newer/better browser.");
	if(!handler) {
	    var query = function (oXML) {  target.innerHTML = oXML.responseText; };
	} else {
		var query = handler;
	}
    myConn.connect(url, method, params, query);
}

var oldform = '';

function getProvincias(destination,id,cualquiera) {
	
	function requestHandler() {
		this.destination = null;
		this.restore = null;
		
		this.run = function(oXML) {
			if(oXML.responseText.substr(0,4) == 'true') {
				eval(oXML.responseText.substr(4));
				arrayToSelect(provincias,'provincia');
				getLocalidades('localidad',provincias[0][0]);
				this.destination.innerHTML = '';
			} else {
				//this.destination.innerHTML = this.restore;
			}
		}
		
		this.destination = document.getElementById('provincia_loading');
	}	
	
	var hndl = new requestHandler();
	
	var params = 'pais='+id.toString();
	if(cualquiera) {
		params += '&cualquiera=1';
	}
	page('provincia_loading','/ajax/listado-provincias.php',params,hndl);	
}

function getLocalidades(destination,id,cualquiera) {
	function requestHandler() {
		this.destination = null;
		this.restore = null;
		
		this.run = function(oXML) {
			if(oXML.responseText.substr(0,4) == 'true') {
				eval(oXML.responseText.substr(4));
				if(document.getElementById('poblacion')) {
					arrayToSelect(localidades,'poblacion');
			   } else if(document.getElementById('ciudad')) {
				   arrayToSelect(localidades,'ciudad');
			   } else {
				   arrayToSelect(localidades,'localidad');
			   }
				this.destination.innerHTML = '';
			} else {
				//this.destination.innerHTML = this.restore;
			}
		}
		
		this.destination = document.getElementById('localidad_loading');
	}	
	
	var hndl = new requestHandler();
	
	var params = 'provincia='+id.toString();
	if(cualquiera) {
		params += '&cualquiera=1';
	}	
	page('localidad_loading','/ajax/listado-localidades.php',params,hndl);		
}

function arrayToSelect(data,dest) {
	dest = document.getElementById(dest);
	dest.options.length = 0;	
	for(var i=0;i<data.length;i++) {
		if(data[i][1] == '') { continue; }
		dest.options[dest.options.length] = new Option(data[i][1],data[i][0]);
	}
}
