﻿ /* JAVASCRIPT GLOBAL FUNCTIONS */
var UltimaSubcategoriaSeleccionada = 0;
function $get(obj) { return document.getElementById(obj); }

/* SCRIPTS PARA MODIFICAR LOS ASPECTOS */

function SET_Css(obj, nuevocss) { $get(obj).className = nuevocss; }
function SET_Display(obj, status) {try { $get(obj).style.display = status; } catch (Error) { }  }
function SET_Visibility(obj, status) {try { $get(obj).style.visibility = status; } catch (Error) { }  }
function SET_Border(obj, border) { $get(obj).style.border = border; }
function SET_BGPosition(obj, nuevapos) { $get(obj).style.backgroundPosition = nuevapos; }
function SET_BGColor(obj, color) { $get(obj).style.backgroundColor = color; }
function SET_ImagenSRC(obj, ruta) { $get(obj).src = ruta; }
function SET_Scroll(horiz, vert) { window.scrollTo(horiz, vert); }

function MostrarOcultarCapa(obj) {
    try {
        if ($get(obj).style.display != 'block') { $get(obj).style.display = 'block'; }
        else { $get(obj).style.display = 'none'; }
    } catch (Error) { }
}

/* UTILIDADES */
function CogerAlto(obj) {
    var intElemOffsetHeight = $get(obj).offsetHeight;
    alert(intElemOffsetHeight);
}

function EnQueURLEstoy(localhost) {
    if (localhost == 'true') {
        var urlactual = window.location.href.substring(22, window.location.href.length);
        var urlactualpartida = urlactual.split('.');
        return urlactualpartida[0];
    } else {
        var urlactual = window.location.href.substring(24, window.location.href.length);
        var urlactualpartida = urlactual.split('.');
        return urlactualpartida[0];
    }
}

function LimitCar(obj) {
    var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
    if (obj.getAttribute && obj.value.length>mlength){
        obj.value=obj.value.substring(0,mlength)
    }
}

function BuscarPorTexto(texto) {
    try {
        document.location.href = 'Buscador.aspx?q=' + texto;
        return false;
    } catch (Error) {alert('Error al buscar por el texto introducido.'); }
}

function BuscarPorFabricante(fab) {
    if (fab != 'vacio') {
        try {
            document.location.href = 'Buscador.aspx?fab=' + fab;
            return false;
        } catch (Error) { alert('Error al buscar por el fabricante introducido.'); }
    }
}

function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent)
        while (1) {
            curtop += obj.offsetTop;
            if (!obj.offsetParent)
                break;
            obj = obj.offsetParent;
        }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}

function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent)
        while (1) {
            curleft += obj.offsetLeft;
            if (!obj.offsetParent)
                break;
            obj = obj.offsetParent;
        }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}

function Loading(b) { if (b == true) { $get('MPLoad_w').style.display = 'block'; } else { $get('MPLoad_w').style.display = 'none'; } }

/*************************************/
/********       PÁGINAS      *********/
/*************************************/

/*** MASTER PAGE ***/
function MP_RebootCats() {
    for (i = 0; i < 30; i++) {
        try { $get('MPCat_' + i).className = 'normal'; } catch (err) { }
        try { $get('MPSubcat_' + i).style.display = 'none'; } catch (err) { }
    }
}

function MP_SoporteOnline(codigo) {
    window.open('http://settings.messenger.live.com/Conversation/IMMe.aspx?invitee=' + codigo + '@apps.messenger.live.com&mkt=es-ES', '_blank', 'width=980,height=800,resizable=yes,status=yes,toolbar=yes, scrollbars=yes');
}

/************************************/
/********       FICHAS      *********/
/************************************/
var URLIMGGrande = '';

function FICHA_PrecargaLowPic() {
    try {
        $get('MP_Ficha_lowpic').src = 'img/anim/loader_horizontal_180x22.gif';
        $get('MP_Ficha_lowpic').width = 180;
        $get('MP_Ficha_lowpic').height = 120;
    } catch (Error) { }
}

function FICHA_CambiarThumb(thumb, ruta, ancho, alto) {
    try { SET_Border('MP_Ficha_thumb1', 'solid 1px #fff'); } catch (Error) { }
    try { SET_Border('MP_Ficha_thumb2', 'solid 1px #fff'); } catch (Error) { }
    try { SET_Border('MP_Ficha_thumb3', 'solid 1px #fff'); } catch (Error) { }
    try {
        URLIMGGrande = ruta;
        setTimeout("FICHA_CambiarSRC('" + ruta + "'," + ancho + "," + alto + ")", 1000);
    } catch (Error) { }
    switch (thumb) {
        case 'thumb1': { SET_Border('MP_Ficha_thumb1', 'solid 1px #4575c1'); return true; }
        case 'thumb2': { SET_Border('MP_Ficha_thumb2', 'solid 1px #4575c1'); return true; }
        case 'thumb3': { SET_Border('MP_Ficha_thumb3', 'solid 1px #4575c1'); return true; }
    }
}

function FICHA_CambiarSRC(ruta, ancho, alto) {
    $get('MP_Ficha_lowpic').src = ruta;
    $get('MP_Ficha_lowpic').width = ancho;
    $get('MP_Ficha_lowpic').height = alto;
}

function FICHA_VerGrande(original) {
    if (URLIMGGrande == '') {
        window.open(original, '_blank', 'width=1024,height=768'); return false;
    } else {
        window.open(URLIMGGrande, '_blank', 'width=1024,height=768'); return false; 
    }
}

/*********************************************/
/********       CAPTURAR TECLAS      *********/
/*********************************************/

/* CAPTURO EL ENTER */
document.onkeypress = function (e) {
    var esIE = (document.all);
    var esNS = (document.layers);
    tecla = (esIE) ? event.keyCode : e.which;
    if (tecla == 13) {

        // BUSCAR UN PRODUCTO PARA EDITAR
        try {
            if ($get('CPH_Body_AD_codart').value != '') {
                AD_Editar_Producto();
                return false;
            } else {
                alert('Introduce el código del producto');
                return false;
            }
        } catch (Error) { }

        //BUSCADOR SUPERIOR
        try {
            if ($get('MP_Searchbox_input').value != '' && $get('MP_Searchbox_input').value != 'Búsqueda de productos...') {
                document.location.href = 'Buscador.aspx?q=' + $get('MP_Searchbox_input').value;
                return false;
            }
        }
        catch (Error) { }

        //LOGIN DE USUARIO
        try {
            if (($get('MP_userinput').value != '') && ($get('MP_passinput').value != '')) {
                Validar();
            } else {
                alert('Debes introducir el usuario y la contraseña');
                return false;
            }
        } catch (Error) { }

        return false;
    }
}

