// JavaScript File

// ************************************************ //
// ***** Definición del objeto XMLHttpRequest ***** //
// ************************************************ //
//var ajax = false;

function creaXHR () {
    var objXHR = false;
    
    try { // creamos el objeto XMLHttpRequest compatible con distintos entornos
        // Para navegadores distintos a Internet Explorer
        objXHR = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (el) { //navegadores web
        try {
            // Para Internet Explorer
            objXHR = new ActiveXObject ("Microsoft.XMLHTTP");
        }
        catch(e2) {
            objXHR = false;
        }
    }
    
    if (window.XMLHttpRequest)
        objXHR =new XMLHttpRequest();
    
    if (!objXHR & typeof XMLHttpRequest != 'undefined')
        objXHR =new XMLHttpRequest();
    
    return objXHR;
}

function AjaxUploadtoServer(destino, URL, capa) { //capa
    var ajax = creaXHR();
    
    if (ajax) { // si tenemos el objeto
        // enviamos solicitud al servidor
        // true para que se envie de forma asíncrona al servidor
        // si se envia de forma sincrona la ejecucion del código se dentendrá hasta
        // forma de la cadena
        
        var elementLoad = document.getElementById(capa);
        
        ajax.open("POST", destino, true); // .open (metodo, destino, modo, login, pwd)
        //Send the proper header information along with the request
        ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
        // Obliga al objeto XMLHttpRequest a que recupere el contenido en cualquier caso
        ajax.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
        ajax.onreadystatechange = function() {
            if (ajax.readyState==1) {
                if (elementLoad != null) {
                    elementLoad.innerHTML = '<img src="img/loading.gif" alt="Cargando" /><br /><span style="font-size: 0.9em; color: #FF0000;">Espere, conectando con el servidor...</span><br />';
                }
                            }
            else if(ajax.readyState == 4) {
                if(ajax.status==200) { // No hubo ningún error
                    //obtenemos la respuesta
                    var respuesta = ajax.responseText;
                    //lo cargamos a la pagina
                    //destino.innerHTML = respuesta;
                    if (elementLoad != null) {
                        elementLoad.innerHTML = respuesta;
                    }
                        
                    enabled();
                    
                }
               /* else if(ajax.status==404) {
                    destino.innerHTML = "La direccion no existe";
                }
                else {
                    destino.innerHTML = "Error: ".ajax.status;
                }*/
            }
        }
        
        ajax.send(URL);
    } 
    
}

function Petition(URL, capa) { //capa
    var ajax = creaXHR();
    
    if (ajax) { // si tenemos el objeto
        // enviamos solicitud al servidor
        // true para que se envie de forma asíncrona al servidor
        // si se envia de forma sincrona la ejecucion del código se dentendrá hasta
        // forma de la cadena
        ajax.open("GET", URL, true); // .open (metodo, destino, modo, login, pwd)
        //Send the proper header information along with the request
        ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
        // Obliga al objeto XMLHttpRequest a que recupere el contenido en cualquier caso
        ajax.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
        ajax.onreadystatechange = function() {
            if(ajax.readyState == 4) {
                //if(ajax.status==200) { // No hubo ningún error
                    //obtenemos la respuesta
                    var respuesta = ajax.responseText;
                    //lo cargamos a la pagina
                    //destino.innerHTML = respuesta;
                    document.getElementById(capa).innerHTML = respuesta;
               /* }
                else if(ajax.status==404) {
                    destino.innerHTML = "La direccion no existe";
                }
                else {
                    destino.innerHTML = "Error: ".ajax.status;
                }*/
            }
        }
        
        ajax.send(null);
    }
}


function AjaxUploadtoServer2(destino, URL, capa, ident, ServiceId, ZoneId, zonausuario) { //capa
    var ajax = creaXHR();
    
    if (ajax) { // si tenemos el objeto
        // enviamos solicitud al servidor
        // true para que se envie de forma asíncrona al servidor
        // si se envia de forma sincrona la ejecucion del código se dentendrá hasta
        // forma de la cadena
        
        var elementLoad = document.getElementById(capa);
        
        ajax.open("POST", destino, true); // .open (metodo, destino, modo, login, pwd)
        //Send the proper header information along with the request
        ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
        // Obliga al objeto XMLHttpRequest a que recupere el contenido en cualquier caso
        ajax.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
        ajax.onreadystatechange = function() {
            if (ajax.readyState==1) {
                if (elementLoad != null) {
                    elementLoad.innerHTML = '<img src="img/loading.gif" alt="Cargando" /><br /><span style="font-size: 0.9em; color: #FF0000;">Espere, conectando con el servidor...</span><br />';
                }
                            }
            else if(ajax.readyState == 4) {
                if(ajax.status==200) { // No hubo ningún error
                    //obtenemos la respuesta
                    var respuesta = ajax.responseText;
                    //lo cargamos a la pagina
                    //destino.innerHTML = respuesta;
                    if (elementLoad != null) {
                        elementLoad.innerHTML = respuesta;
                    }
                        
                    enabled();
                    
                    if (zonausuario != "undefined" && zonausuario != "" ) {
                        aportacionUsuario();
                    } else {
                        getTitlesOfService(ServiceId,ZoneId);
                    }
    
                    loadService(ident, 1, true, ZoneId, ServiceId);
                    
                }
               /* else if(ajax.status==404) {
                    destino.innerHTML = "La direccion no existe";
                }
                else {
                    destino.innerHTML = "Error: ".ajax.status;
                }*/
            }
        }
        
        ajax.send(URL);
    } 
    
}

/*
function getXMLHttpRequest(){
        var aVersions = [ "MSXML2.XMLHttp.5.0",
                "MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0",
                "MSXML2.XMLHttp","Microsoft.XMLHttp"
        ];

        if (window.XMLHttpRequest){
                // para IE7, Mozilla, Safari, etc: que usen el objeto nativo
                return new XMLHttpRequest();
        }else if (window.ActiveXObject){
                // de lo contrario utilizar el control ActiveX para IE5.x y IE6.x
                for (var i = 0; i < aVersions.length; i++) {
                        try {
                            var oXmlHttp = new ActiveXObject(aVersions[i]);
                            return oXmlHttp;
                        }catch (error) {
                        //no necesitamos hacer nada especial
                        }
            }
        }
}


*/