function ajax_new() { 
    var xmlhttp = false; 

    try { 
// Creacion del objeto AJAX para navegadores no IE
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); 
    }
    catch(e) { 
        try { 
// Creacion del objet AJAX para IE 
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
        } 
        catch(E) { 
            xmlhttp = false; 
        }
    }
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') { 
        xmlhttp = new XMLHttpRequest(); 
    } 
    return xmlhttp; 
}

function ajax_load(idContainer, url, method, parameters, transparency) {
    note_close();
    var ajax = ajax_new();
    var container = document.getElementById(idContainer);

    if (ajax) {
        ajax.open (method.toUpperCase(), url, true);
        ajax.onreadystatechange = function() {
            var waitMess = '';

            waitMess = "<table width='100%' height='100%' border='0' cellpadding='0' cellspacing='0'>";
            waitMess = waitMess + "<tr><td style='height: 100%; width: 100%; text-align: center; vertical-align: middle;'>";
            waitMess = waitMess + "<img src='img/loader-s.gif' alt='loading' width='24' height='24'/><br/>Cargando...";
            waitMess = waitMess + "</td></tr></table>";
            if (ajax.readyState == 1) {
                if (!transparency) container.innerHTML = waitMess;
            }
            else if (ajax.readyState == 4) {
                if (ajax.status == 200 || window.location.href.indexOf ("http") == - 1) {
                    container.innerHTML = ajax.responseText;
                }
                else if (ajax.status == 404) {
                    container.innerHTML = "La página no existe";
                }
                else {
                    container.innerHTML = "Error: ".ajax.status;
                }
            }
        }
        ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        if (method.toUpperCase() == 'POST') {
            ajax.send (parameters);
            return;
        }
        else {
            ajax.send (null);
            return;
        }
    }
}

function reversal(s) {
    var len = s.length;
    var trans = "";

    for (i=0; i<len; i++) {
        trans = trans + s.substring(len-i-1, len-i);
    }
    s = trans;
    return s;
}

function decToHex(num, radix) {
    var hexVals = new Array("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F");
    var hexString = "";

    while (num >= radix) {
        temp = num % radix;
        num = Math.floor(num / radix);
        hexString += hexVals[temp];
    }
    hexString += hexVals[num];
    return reversal(hexString);
}

function URLEncode(val) {
    var len = val.length;
    var i = 0;
    var newStr = "";

    for (i=0;i<len;i++) {
        tval1 = val.substring(i, i+1);
        if (tval1 != '\n')
            newStr = newStr + "%" + decToHex(tval1.charCodeAt(0),16);
    }
    return newStr; 
}

function enable (id) {
    var element = document.getElementById(id);

    element.disabled = false;
}

function disable (id) {
    var element = document.getElementById(id);

    element.disabled = true;    
}

function refresh(action) {
    ajax_load('div_body', '?action='+action, 'GET', '', false);
}

function command(action, command) {
    ajax_load('div_body', 'action.php', 'POST', 'action='+action+'&command='+command, false);
}

function menu(id) {
    ajax_load('div_menu', '?action=menu&opt='+id, 'GET', '', true);
}

function showById(id) {
    currentStyle = document.getElementById(id).style;
    currentStyle.visibility = "visible";
}

function hideById(id) {
    currentStyle = document.getElementById(id).style;
    currentStyle.visibility = "hidden";
}

function swapBackgroundImage(id, url) {
    document.getElementById(id).style["backgroundImage"] = 'url(' + url + ')';
}

function go_secure (options) {
    var url = "https://"+document.domain;
    if (options == 'change_passwd')
        url = url + "/set_pass.php";
    else if (options != null)
        url = url + "/?refresh=1&" + options;
    window.location = url;
}

function go_insecure (options) {
    url = "http://"+document.domain;
    if (options != null) url = url + "/?refresh=1&" + options;
    window.location = url;
}


