// Documento JavaScript

// Esta función cargará las paginas
//-------------------------------------------------------------------------------------------------------------------------------
function llamarasincrono ( url, id_contenedor )
{
    var pagina_requerida = false;
    if ( window.XMLHttpRequest ) 
    {
        pagina_requerida = new XMLHttpRequest (); // Si es Mozilla, Safari etc
    } else if (window.ActiveXObject) // IE
			{
					// pero si es IE
					try 
					{
							pagina_requerida = new ActiveXObject ("Msxml2.XMLHTTP");
					}
					catch (e)
					{
							// en caso que sea una versión antigua
							try
							{
									pagina_requerida = new ActiveXObject ("Microsoft.XMLHTTP");
							}
							catch (e)
							{
							}
					}
			} 
    else { return false; } // Si no se puede usar el XMLHTTP cortamos la ejecucion

		// Creamos un metodo
		pagina_requerida.onreadystatechange = function ()
    {
        cargarpagina (pagina_requerida, id_contenedor); // función de respuesta
    }
    pagina_requerida.open ('GET', url, true); // asignamos los métodos open y send
    pagina_requerida.send (null); 
}


// nuevoAjax()
// ----------------------------------------------------------------------------------------------------------------------------------
function nuevoAjax() {
  var xmlhttp=false;
  try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
  	try {
    	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    	} catch (E) {
    		xmlhttp = false;
    		}
    	}

   	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    	xmlhttp = new XMLHttpRequest();
   		}
   return xmlhttp;
} 

// todo es correcto y ha llegado el momento de poner la información requerida
// en su sitio en la pagina xhtml
// ----------------------------------------------------------------------------------------------------------------------------------
function cargarpagina (pagina_requerida, id_contenedor)
{
    if (pagina_requerida.readyState == 4 && (pagina_requerida.status == 200 || window.location.href.indexOf ("http") == - 1))
    document.getElementById (id_contenedor).innerHTML = pagina_requerida.responseText;
}

// cargarPaginaPOST( pagina_requerida, parametros, id_contenedor )
// ----------------------------------------------------------------------------------------------------------------------------------
// Esta funcion es similar a llamarasincrono, mezclandose con nuevoAjax, pero por post, se podria parametrizar en una
// ----------------------------------------------------------------------------------------------------------------------------------
function cargarPaginaPOST( pagina_requerida, parametros, id_contenedor ) // Parametros: t1=''&t2=''
{
	var t1, t2, contenedor;
	contenedor = document.getElementById(id_contenedor);
	t1 = document.getElementById('texto1').value;
	t2 = document.getElementById('texto2').value;
	ajax = nuevoAjax();
	ajax.open("POST", pagina_requerida,true);
	ajax.onreadystatechange=function() {
		if ( ajax.readyState == 4 ) {
			contenedor.innerHTML = ajax.responseText
		}
	}
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send( parametros );
}

function objetus(file) {
    xmlhttp=false;
    this.AjaxFailedAlert = "Su navegador no soporta las funciónalidades de este sitio y podria experimentarlo de forma diferente a la que fue pensada. Por favor habilite javascript en su navegador para verlo normalmente.\n";
    this.requestFile = file;
    this.encodeURIString = true;
    this.execute = false;
    if (window.XMLHttpRequest) { 
        this.xmlhttp = new XMLHttpRequest();
        if (this.xmlhttp.overrideMimeType) {
            this.xmlhttp.overrideMimeType('text/xml');
        }
    } 
    else if (window.ActiveXObject) { // IE
        try {
            this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        }catch (e) {
            try {
                this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                this.xmlhttp = null;
            }
        }
        if (!this.xmlhttp && typeof XMLHttpRequest!='undefined') {
            this.xmlhttp = new XMLHttpRequest();
            if (!this.xmlhttp){
                this.failed = true; 
            }
        } 
    }
    return this.xmlhttp ;
}
function recibeid(_pagina,valorget,valorpost,capa){ 
    ajax=objetus(_pagina);
    if(valorpost!=""){
        ajax.open("POST", _pagina+"?"+valorget+"&tiempo="+new Date().getTime(),true);
    } else {
        ajax.open("GET", _pagina+"?"+valorget+"&tiempo="+new Date().getTime(),true);
    }
    ajax.onreadystatechange=function() {
        if (ajax.readyState==1){
            document.getElementById(capa).innerHTML = 
				"<img src='loadingcircle.gif' align='center'> Aguarde por favor...";
        }
        if (ajax.readyState==4) {
            if(ajax.status==200)
            {document.getElementById(capa).innerHTML = ajax.responseText;}
            else if(ajax.status==404)
            {
                capa.innerHTML = "La direccion no existe";
            }
            else
            {
                capa.innerHTML = "Error: ".ajax.status;
            }
        }
    }
    if(valorpost!=""){
        ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        ajax.send(valorpost);
    } else {
        ajax.send(null);
    }
} 
