function restore() {
    ajax_load('div_body', '?action=restore', 'GET', '', false);
}

function note_show (title, texto, coords, dimension) {
    if (coords != null) {
        if (coords[0] != null) xPos = coords[0];
        if (coords[1] != null) yPos = coords[1];
    }
    if (dimension != null) {
        if (dimension[0] != null) {
            document.getElementById('div_notebox').style["width"]  = dimension[0]+'px';
            document.getElementById('div_notebox_body').style["width"] = (dimension[0]-10)+'px';
        }
        if (dimension[1] != null) {
            document.getElementById('div_notebox').style["height"] = dimension[1]+'px';
            document.getElementById('div_notebox_body').style["height"] = (dimension[1] - 30)+'px';
        }
    }    
    if (xPos == null) {
        xPos = xMousePos;
        if ((xPos + 300) > xMousePosMax) {
            xPos = xMousePosMax - 300;
        }
        xPos = xPos - 50;
    }
    if (yPos == null) {
        yPos = yMousePos;
        if ((yPos + 150) > yMousePosMax) {
            yPos = yMousePosMax - 150;
        }
    }
    if (title != null) document.getElementById('span_notebox_title').innerHTML = title;
    if (texto != null) document.getElementById('div_notebox_body').innerHTML = texto;
    document.getElementById('div_notebox').style["left"] = xPos+'px';
    document.getElementById('div_notebox').style["top"]  = yPos+'px';
    showById('div_notebox');
}

function note_close () {
    hideById('div_notebox');
    document.getElementById('div_notebox').style["width"]       = '300px';
    document.getElementById('div_notebox').style["height"]      = '150px';
    document.getElementById('div_notebox_body').style["width"]  = '100%';
    document.getElementById('div_notebox_body').style["height"] = '100%';
    document.getElementById('div_notebox_body').innerHTML = "";
    document.getElementById('span_notebox_title').innerHTML = "";
}

function map_show (idParent, idContenido, idDirections) {
    var ajax = ajax_new();
    var container = document.getElementById(idParent);

    if (ajax) {
        ajax.open ('GET', '?action=where', 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) {
                container.innerHTML = waitMess;
            }
            else if (ajax.readyState == 4) {
                if (ajax.status == 200 || window.location.href.indexOf ("http") == - 1) {
                    container.innerHTML = ajax.responseText;
                    map_load(idContenido, idDirections);
                }
                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');
        ajax.send (null);
        return;
    }
}

function user_edit_submit (name, email) {
    var command = 'action=user-info&command=update&user_name='+URLEncode(name)+'&user_email='+URLEncode(email);
    ajax_load('div_detalle', 'action.php', 'POST', command, false);
    alert('La información ha sido actualizada con éxito.');
    refresh('user-info');
}

function option_add (idParent) {
    var command = 'action=admin-menu&command=show&id=0&opt_parent=';
    if (idParent == null)
        command = command + '0&opt_type=1';
    else
        command = command + idParent + '&opt_type=4';
    ajax_load('div_detalle', 'action.php', 'POST', command, false);
    hideById('div_lista');
    showById('div_detalle');
}

function option_show (id, name, href, target, idParent, order, type, ispublic) {
    var command = 'action=admin-menu&command=show&id=' + id;
    if (name != null)
        command = command + '&opt_name='+URLEncode(name);
    if (href != null)
        command = command + '&opt_href='+URLEncode(href);
    if (target != null)
        command = command + '&opt_target='+target;
    if (idParent != null)
        command = command + '&opt_parent='+idParent;
    if (order != null)
        command = command + '&opt_order='+order;
    if (type != null)
        command = command + '&opt_type='+type;
    if (ispublic != null) 
        command = command + '&opt_public='+ispublic;
    ajax_load('div_detalle', 'action.php', 'POST', command, false);
    hideById('div_lista');
    showById('div_detalle');
}

function option_delete (id) {
    var confirmed = confirm("Si prosigue con la operación, se eliminará la opción seleccionada. ¿Desea continuar?");
    if (confirmed) {
        var command = 'action=admin-menu&command=delete&id=' + id;
        ajax_load('div_detalle', 'action.php', 'POST', command, false);
        alert('Opción del Menú eliminada con éxito.');
        refresh('admin-menu');
    }
}

function option_add_submit (id, name, href, target, idParent, order, type, isPublic, lAccess) {
    var command = 'action=admin-menu&command=update&id='+id+'&opt_name='+URLEncode(name)+'&opt_href='+URLEncode(href)+'&opt_target='+target+'&opt_parent='+idParent+'&opt_order='+order+'&opt_type='+type+'&opt_public='+isPublic;
    if (isPublic == 0) {
        for (key in lAccess) {
            command = command + '&opt_acces-'+key+'='+lAccess[key];
        }
    }
    ajax_load('div_detalle', 'action.php', 'POST', command, false);
    alert('Opción del Menú actualizada con éxito.');
    refresh('admin-menu');
    hideById('div_detalle');
}

function user_add () {
    var command = 'action=admin-users&command=show&id=0';
    ajax_load('div_detalle', 'action.php', 'POST', command, false);
    hideById('div_lista');
    showById('div_detalle');
}

function user_show (id, login, name, email) {
    var command = 'action=admin-users&command=show&id=' + id;
    if (login != null)
        command = command + '&user_login='+URLEncode(login);
    if (name != null)
        command = command + '&user_name='+URLEncode(name);
    if (email != null)
        command = command + '&user_email='+URLEncode(email);
    ajax_load('div_detalle', 'action.php', 'POST', command, false);
    hideById('div_lista');
    showById('div_detalle');
}

function user_delete (id) {
    var confirmed = confirm("Si prosigue con la operación, se eliminará el usuario. ¿Desea continuar?");
    if (confirmed) {
        var command = 'action=admin-users&command=delete&id=' + id;
        ajax_load('div_detalle', 'action.php', 'POST', command, false);
        alert('Usuario eliminado con éxito.');
        refresh('admin-users');
    }
}

function user_add_submit (id, login, password, name, email, lAccess) {
    var command = 'action=admin-users&command=update&id='+id+'&user_login='+URLEncode(login)+'&user_name='+URLEncode(name)+'&user_email='+email;
    if ((id == 0) || (password != '')) {
        command = command + '&user_password=' + URLEncode(password)
    }
    for (key in lAccess) {
        if (lAccess[key] > 1) command = command + '&user_acces-'+key+'='+lAccess[key];
    }
    ajax_load('div_detalle', 'action.php', 'POST', command, false);
    alert('Usuario actualizado con éxito.');
    refresh('admin-users');
    hideById('div_detalle');
}

function rol_add () {
    var command = 'action=admin-roles&command=show&id=0';
    ajax_load('div_detalle', 'action.php', 'POST', command, false);
    hideById('div_lista');
    showById('div_detalle');
}

function rol_show (id, name, description) {
    var command = 'action=admin-roles&command=show&id=' + id;
    if (name != null)
        command = command + '&rol_name='+URLEncode(name);
    if (description != null)
        command = command + '&rol_description='+URLEncode(description);
    ajax_load('div_detalle', 'action.php', 'POST', command, false);
    hideById('div_lista');
    showById('div_detalle');
}

function rol_delete (id) {
    var confirmed = confirm("Si prosigue con la operación, se eliminará el perfil. ¿Desea continuar?");
    if (confirmed) {
        var command = 'action=admin-roles&command=delete&id=' + id;
        ajax_load('div_detalle', 'action.php', 'POST', command, false);
        alert('Perfil eliminado con éxito.');
        refresh('admin-roles');
    }
}

function rol_add_submit (id, name, description) {
    var command = 'action=admin-roles&command=update&id='+id+'&rol_name='+URLEncode(name)+'&rol_description='+URLEncode(description);

    ajax_load('div_detalle', 'action.php', 'POST', command, false);
    alert('Perfil actualizado con éxito.');
    refresh('admin-roles');
    hideById('div_detalle');
}

function new_add () {
    var command = 'action=admin-news&command=show&id=0';
    var ajax = ajax_new();
    var container = document.getElementById('div_detalle');

    if (ajax) {
        ajax.open ('POST', 'action.php', true);
        ajax.onreadystatechange = function() {
            var waitMess = '';
            if (ajax.readyState == 1) {
                container.innerHTML = waitMess;
            }
            else if (ajax.readyState == 4) {
                if (ajax.status == 200 || window.location.href.indexOf ("http") == - 1) {
                    container.innerHTML = ajax.responseText;
                    initTextArea('new_body');
                }
                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');
        ajax.send (command);
    }
    hideById('div_lista');
    showById('div_detalle');
}


function new_show (id, name, description) {
    var command = 'action=admin-news&command=show&id=' + id;
    var ajax = ajax_new();
    var container = document.getElementById('div_detalle');

    if (ajax) {
        ajax.open ('POST', 'action.php', true);
        ajax.onreadystatechange = function() {
            var waitMess = '';
            if (ajax.readyState == 1) {
                container.innerHTML = waitMess;
            }
            else if (ajax.readyState == 4) {
                if (ajax.status == 200 || window.location.href.indexOf ("http") == - 1) {
                    container.innerHTML = ajax.responseText;
                    initTextArea('new_body');
                }
                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');
        ajax.send (command);
    }
    hideById('div_lista');
    showById('div_detalle');
}

function new_delete (id) {
    var confirmed = confirm("Si prosigue con la operación, se eliminará la noticia. ¿Desea continuar?");
    if (confirmed) {
        var command = 'action=admin-news&command=delete&id=' + id;
        ajax_load('div_detalle', 'action.php', 'POST', command, false);
        alert('Noticia eliminada con éxito.');
        refresh('admin-news');
    }
}

function new_add_submit (id, title, date, body, order) {
    var command = 'action=admin-news&command=update&id='+id+'&new_title='+URLEncode(title)+'&new_date='+date+'&new_body='+URLEncode(body)+'&new_order='+order;

    ajax_load('div_detalle', 'action.php', 'POST', command, false);
    refresh('admin-news');
    hideById('div_detalle');
    alert('Noticia actualizada con éxito.');
}

function prodh_add () {
    var command = 'action=admin-prodh&command=show&id=0';
    var ajax = ajax_new();
    var container = document.getElementById('div_detalle');

    if (ajax) {
        ajax.open ('POST', 'action.php', true);
        ajax.onreadystatechange = function() {
            var waitMess = '';
            if (ajax.readyState == 1) {
                container.innerHTML = waitMess;
            }
            else if (ajax.readyState == 4) {
                if (ajax.status == 200 || window.location.href.indexOf ("http") == - 1) {
                    container.innerHTML = ajax.responseText;
                    initTextArea('prodh_general');
                    initTextArea('prodh_technical');
                }
                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');
        ajax.send (command);
    }
    hideById('div_lista');
    showById('div_detalle');
}

function prodh_show (id, name, description) {
    var command = 'action=admin-prodh&command=show&id=' + id;
    var ajax = ajax_new();
    var container = document.getElementById('div_detalle');

    if (ajax) {
        ajax.open ('POST', 'action.php', true);
        ajax.onreadystatechange = function() {
            var waitMess = '';
            if (ajax.readyState == 1) {
                container.innerHTML = waitMess;
            }
            else if (ajax.readyState == 4) {
                if (ajax.status == 200 || window.location.href.indexOf ("http") == - 1) {
                    container.innerHTML = ajax.responseText;
                    initTextArea('prodh_general');
                    initTextArea('prodh_technical');
                }
                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');
        ajax.send (command);
    }
    hideById('div_lista');
    showById('div_detalle');
}

function prodh_delete (id) {
    var confirmed = confirm("Si prosigue con la operación, se eliminará el elemento. ¿Desea continuar?");
    if (confirmed) {
        var command = 'action=admin-prodh&command=delete&id=' + id;
        ajax_load('div_detalle', 'action.php', 'POST', command, false);
        alert('Elemento eliminado con éxito.');
        refresh('admin-prodh');
    }
}

function prodh_add_submit (id, code, seller, family, general, technical, price, dateValid) {
    var command = 'action=admin-prodh&command=update&id='+id+'&prod_code='+URLEncode(code)+'&prod_seller='+seller+'&prod_family='+family+'&prod_general='+URLEncode(general)+'&prod_technical='+URLEncode(technical)+'&prod_price='+price+'&prod_date='+dateValid;

    ajax_load('div_detalle', 'action.php', 'POST', command, false);
    hideById('div_detalle');
    alert('Elemento actualizado con éxito.');
    refresh('admin-prodh');
}

