/*
	Recibe la dirección 
*/
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 diseñada. 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 cargarPrincipal(pagina1,valorGet1,valorPost1,contenedor1,pagina2,valorGet2,valorPost2,contenedor2){
	cargar(pagina1,valorGet1,valorPost1,contenedor1);
	cargar(pagina2,valorGet2,valorPost2,contenedor2);	
}

/*
	Permite pasar variables GET y POST entre dos páginas WEB usando AJAX
	_pagina: URL que se desea cargar
	valorget: Recibe las variables GET que se desean pasar
	valorpost: Recibe las variables POST que se desean pasar
	contenedor: Recibe el div que mostrará la página que se solicite mediante la función
*/
function cargar(_pagina,valorget,valorpost,contenedor){ 
    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(contenedor).innerHTML = 
				"<CENTER><img src='/media/imagenes/general/minervaGiratoria.gif' align='center' alt='Espere' height='50' width='40' /></CENTER>";
				
        }
        if (ajax.readyState==4) {
            if(ajax.status==200)
		{document.getElementById(contenedor).innerHTML = ajax.responseText;}
            else if(ajax.status==404)
            {
		document.getElementById(contenedor).innerHTML = "Error: La direcci&oacute;n solicitada no existe o no se encuentra disponible por el momento";
            }
            else
            {
		document.getElementById(contenedor).innerHTML = "Error: El servidor ha encontrado un error y no puede completar su solicitud. "+ajax.status;
            }
        }
    }
    if(valorpost!=""){
        ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        ajax.send(valorpost);
    } else {
        ajax.send(null);
    }
}

function vaciarFormulario(formid){
	var Formulario = document.getElementById(formid);
  	var longitudFormulario = Formulario.elements.length;
  	var cadenaFormulario = "";
  	sepCampos = "";
  	for (var i=0; i <= Formulario.elements.length-1;i++) {
	    if (Formulario.elements[i].type=='checkbox'){
	    	if (Formulario.elements[i].checked){
	        	cadenaFormulario += sepCampos+Formulario.elements[i].name+'='+encodeURI(Formulario.elements[i].value);
	        	sepCampos="&";      
	      	} 
	    }else{
	      		cadenaFormulario += sepCampos+Formulario.elements[i].name+'='+encodeURI(Formulario.elements[i].value);
	      		sepCampos="&";
	    }
  	}
  	return cadenaFormulario;
}

function cargarFormulario(_pagina,contenedor,idformulario){
	return cargar(_pagina,'',vaciarFormulario(idformulario),contenedor);
}
