/** XHConn - Simple XMLHTTP Interface - bfults@gmail.com - 2005-04-08        **
 ** Code licensed under Creative Commons Attribution-ShareAlike License      **
 ** http://xkr.us/code/javascript/XHConn/
 ** http://creativecommons.org/licenses/by-sa/2.0/                           **/
//var xmlhttp;
var isBusy = false; //indica si se está realizando una conexión

function XHConn(xmlhttp)
{
  var 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);
		isBusy = 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");
		isBusy = true;
      }
      xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4 && !bComplete)
        {
          bComplete = true;
		  isBusy = false;
          fnDone(xmlhttp);
        }};
      xmlhttp.send(sVars);
    }
    catch(z) { return false; }
    return true;
  };
  return this;
}

function cargar_contenido(fuente, divID, variables, divIDCargando, mensaje, imagenLoad) 
//fuente:		 página que se carga en el divisor principal
//divID:		 divisor principal
//variables:	 variables que se pasan a fuente
//divIDCargando: divisor en el que se muestra el mensaje de cargando...
//mensaje:		 mensaje que se muestra en el divisor de carga
{ 
	if (imagenLoad == "") {
		imagenLoad = "ajax-loader-rosa.gif";
	}
	document.getElementById(divIDCargando).innerHTML = "<table class='void' border=0 cellpadding=0 cellspacing=0><tr><td class='peque'><img src='css/img/" + imagenLoad + "'></td><td class='peque'>&nbsp;&nbsp;" + mensaje +"</td></tr></table>"; 
	var xmlhttp;
    var myConn = new XHConn(xmlhttp); 
    if (!myConn) alert("XMLHTTP no esta disponible. Inténtalo con un navegador más actual."); 
	var peticion = function (oXML) {
						document.getElementById(divID).innerHTML = oXML.responseText; 
						document.getElementById(divIDCargando).innerHTML = "";
					}; 
	myConn.connect(fuente, "GET", variables, peticion); 
} 



function cargar_contenidoUsuario(fuente, variables, divIDCargando) 
{ 
	imagenLoad = "ajax-loader-rosa.gif";
	document.getElementById(divIDCargando).innerHTML = "<img src='css/img/" + imagenLoad + "'>"; 
	var xmlhttp;
    var myConn = new XHConn(xmlhttp); 
    if (!myConn) alert("XMLHTTP no esta disponible. Inténtalo con un navegador más actual."); 
	var peticion = function (oXML) {
						document.getElementById("divErrorTXT").innerHTML = oXML.responseText;
						if (document.getElementById("divErrorTXT").innerHTML.length > 0 )
							document.getElementById("divError").style.visibility="visible";
						else
							document.getElementById("divError").style.visibility="hidden";
						document.getElementById(divIDCargando).innerHTML = "";
					}; 
	myConn.connect(fuente, "GET", variables, peticion); 
} 




function ocultarTiendas(){
	document.getElementById('divTiendas').style.display = 'none';
	document.getElementById('divTiendas').innerHTML = '';
}

function cargarTiendas(ciudad, dns) {
	document.getElementById("divTiendas").style.display='block';
	cargar_contenido('inc/incCargarTiendas.asp','divTiendas','ciudad='+ciudad+'&dns='+dns,'divCargandoTiendas','','');
} 

function cargarContacto(menu) {
	cargar_contenido('inc/incTextoContacto.asp','divTextoContacto','menu='+menu,'divCargandoContenido','','ajax-loader-contacto.gif');
} 

function eliminarCestaSesion(id) {
	cargar_contenido('inc/incEliminarCestaSesion.asp','divSesion','fila='+id,'divCargandoContenido','','ajax-loader-rosa.gif');
} 

function cambiarCestaSesion(id, qty) {
	cargar_contenido('inc/incCambiarCestaSesion.asp','divSesion','fila='+id+'&qty='+qty,'divCargandoContenido','','ajax-loader-rosa.gif');
} 

function buscarUsuarioAjax(usuario) {
	cargar_contenidoUsuario('inc/incBuscarUsuario.asp','usuario='+usuario,'divCargandoContenido');
} 

function abortarAjax() {
	if (isBusy)
	{
		xmlhttp.onreadystatechange = function () {}
		xmlhttp.abort();
	}
}
