// Scripts de AJAX

var http_request = false;
var Formulario = false;

function Ajax(pag) {
  //ENVIA DATOS POR GET
  //LLAMADA A LA FUNCION
  //Ajax("PAGINA");
        http_request = false;
        if ( window.XMLHttpRequest) { // Mozilla, Safari,...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
                // Ver nota sobre esta linea al final 
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try { 
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!http_request) {
            alert('Falla :( No es posible crear una instancia XMLHTTP');
            return false;
        }
  
    
	document.getElementById("DivCargando").style.top = document.body.scrollTop+5;
	document.getElementById("DivCargando").style.display = 'block';
	//ENVIO LA PETICION POR GET
    http_request.onreadystatechange = alertContents;
    http_request.open('GET', pag, true);
    http_request.send(null);

}
 
function AjaxPost(pag, formid, metodo) {
	//ENVIA DATOS POR POST
	//LLAMADA A LA FUNCION
	//Ajax("PAGINA", "IDFORMULARIO");
		http_request = false;
		if ( window.XMLHttpRequest) { // Mozilla, Safari,...
			http_request = new XMLHttpRequest();
			if (http_request.overrideMimeType) {
				http_request.overrideMimeType('text/xml');
				// Ver nota sobre esta linea al final 
			}
		} else if (window.ActiveXObject) { // IE
			try {
				http_request = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try { 
					http_request = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			}
		}
		
		if (!http_request) {
			alert('Falla :( No es posible crear una instancia XMLHTTP');
			return false;
		}
		
		var cadenaFormulario = ""; 
		//TOMO LA CADENA DE CAMPOS Y VALORES DEL FORMULARIO
		cadenaFormulario = FormularioPost (formid);
		document.getElementById("DivCargando").style.top = document.body.scrollTop+5;
		document.getElementById("DivCargando").style.display = 'block';
		http_request.open("POST", pag, true); 
		http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=ISO-8859-1'); 
		http_request.onreadystatechange = alertContents;
		http_request.send(cadenaFormulario); 
		

}

    function alertContents() {

        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
			var texto = "";
			texto = unescape(http_request.responseText).replace(/\+/g,' ');
				document.getElementById("DivCargando").style.display = 'none';
				Proceso (texto);
				if (Formulario) DesbloqueoForm ();
				
            } else {
				document.getElementById("DivCargando").style.display = 'none';
                alert('Hubo problemas con la petición. Error Nº '+http_request.status+'  '+VerError (http_request.status));
				if (Formulario) DesbloqueoForm ();
            }
        }
    }


function FormularioPost (formid)
{   //GENERA UNA CADENA CON LOS ELEMENTOS DEL FORMULARIO
	  Formulario = document.getElementById(formid); 
	  var longitudFormulario = Formulario.elements.length; 
	  var cadenaFormulario = "" 
	  var sepCampos 
	  sepCampos = "" 
	  for (var i=0; i <= Formulario.elements.length-1;i++) 
	  { 
	   cadenaFormulario += sepCampos+Formulario.elements[i].name+'='+encodeURI( Formulario.elements[i].value); 
	   sepCampos="&";
	   Formulario.elements[i].disabled = true; //BLOQUEA LOS ELEMENTOS DEL FORMULARIO
	  } 
	  return cadenaFormulario;
 }

function DesbloqueoForm ()
 {   //DESBLOQUEA LOS ELEMENTOS DEL FORMULARIO
	  for (var i=0; i <= Formulario.elements.length-1;i++) 
	  { 
	   Formulario.elements[i].disabled = false;
	  } 
	  Formulario = false;
 }


 //FUNCIONES QUE SE USAN EN LAS PAGINAS PARA LLAMAR AL AJAX
 //ENVIO DEL FORMULARIO POR POST-----------
 function postAJAX (pag,formid)
 {
  pag = pag + "?r="+Math.random();
  AjaxPost(pag, formid, "POST");
 }
 //---------------------------------------- 
 //ENVIO DEL FORMULARIO POR GET-----------
 function getAJAX (pag)
 {
	  pag = pag + "&r="+Math.random();
	  Ajax(pag);
 }
 //----------------------------------------

//Funcion para ver el detalle del error
function VerError (estado)
{
	if (estado == 400 )
	return "Error en la lectura del archivo";
	
	if (estado == 401  )
	return "Acceso no autorizado";
	
	if (estado == 403 )
	return "ForbiddenAccess Denied";
	
	if (estado == 404 )
	return "Archivo no encontrado";
	
	if (estado == 408 )
	return "Terminó el tiempo de espera";
	
	if (estado == 500 )
	return "Error interno";
	
	if (estado == 501 )
	return "No implementado";
	
	if (estado == 502 )
	return "Service Temporarily Overloaded";
	
	if (estado == 503 )
	return "Service Unavailable";
}

