
var contenidos
var secciones
var ajax = false

var procesoActivo = false
var http = getHTTPObject();
var tmrAnular = setTimeout("anularPeticio()", 10000); //20 segundos
var elementoCargar
var elementosCargar

function getHTTPObject() {
    var xmlhttp;
    /*@cc_on
    @if (@_jscript_version >= 5)
       try {
          xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
       } catch (e) {
          try {
             xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
          } catch (E) { xmlhttp = false; }
       }
    @else
    xmlhttp = false;
    @end @*/
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
       try {
          xmlhttp = new XMLHttpRequest();
       } catch (e) { xmlhttp = false; }
    }
    return xmlhttp;
}



function anularPeticio()
{
	if (procesoActivo){
		alert("anulado");
   http.abort();
   procesoActivo = false;
   espere.style.visibility="hidden"
  }
}

function cargarContenido(pIdioma, pSeccion, pElemento) {
	if (ajax){
		//espere.style.visibility="visible"
	  if (!procesoActivo && http) {
	  	 clearTimeout(tmrAnular);
	     var url = urlContenidos +"?seccion="+ pSeccion + "&idioma=" + pIdioma;
	     http.open("GET", url, true);
	     elementoCargar = pElemento;
	     http.onreadystatechange = handleHttpResponse;
	     procesoActivo = true;
	     http.send(null);
	  }
	  cargarImagenes(pIdioma);
	}else{
		document.forms[0].submit();
	}	
}

function cargarContenidosSegunSecciones(pIdioma, pSecciones, pElementos) {
	if (ajax){
		//espere.style.visibility="visible"
	  if (!procesoActivo && http) {
	  	 clearTimeout(tmrAnular);
	     var url = urlContenidos +"?secciones="+ pSecciones + "&idioma=" + pIdioma;
	     http.open("GET", url, true);
	     elementosCargar = pElementos;
	     http.onreadystatechange = handleHttpResponseSegunSecciones;
	     procesoActivo = true;
	     http.send(null);
	  }
	  cargarImagenes(pIdioma);
	}else{
		document.forms[0].submit();
	}
  
}

function handleHttpResponseSegunSecciones() { 
		var valores
		var elementos
    if (http.readyState == 4) { 
       if (http.status == 200) {
          if (http.responseText.indexOf('invalid') == -1) {
        		valores = http.responseText.split(SEPARADOR); 
        		elementos = elementosCargar.split(SEPARADOR);
        		for (var i=0; i<valores.length;i++){
        			//document.getElementById(elementos[i]).innerHTML = valores[i];
        			alert(valores[i]);
        			tratarRespuesta(elementos[i], valores[i]);
        		}
        		respuestaObtenida();
          }
       }
    }
}

function handleHttpResponse() { 
    if (http.readyState == 4) { 
       if (http.status == 200) { 
          if (http.responseText.indexOf('invalid') == -1) {
            var resultado ;
            tratarRespuesta(elementoCargar, http.responseText);
						respuestaObtenida();
          }
       }
    }
}


function respuestaObtenida(){
	espere.style.visibility="hidden";
	procesoActivo = false;            
}

function determinarUsoAjax(){
	if (document.forms.seleccion.ajax.value=="0") {
		ajax = true;
	}else{
		ajax = false;
	}
}

function tratarRespuesta (pElemento, pRespuesta) {
var dest = "";
var len = pRespuesta.length;
var index = 0;
var code = null;
var i = 0;
document.getElementById(pElemento).innerHTML="";
	while (i < len) {
		i = pRespuesta.indexOf ("<script>");
		if (i == -1){
			document.getElementById(pElemento).innerHTML = document.getElementById(pElemento).innerHTML + pRespuesta;
			break;
		}else{
			document.getElementById(pElemento).innerHTML = document.getElementById(pElemento).innerHTML + pRespuesta.substring(0,i);
			pRespuesta = pRespuesta.substring(i+8,len);
			i = pRespuesta.indexOf ("</script>");
			code = pRespuesta.substring(0,i);
			resultado = eval(code);
			len = pRespuesta.length;
			pRespuesta = pRespuesta.substring(i+9,len);
		}
		i=0;
		len = pRespuesta.length;
	}
	return true;
}


