// Archivo JScript
var detalleProductopf = {};
detalleProductopf.IdSKU = null;
detalleProductopf.Visible = false;
detalleProductopf.Posicion;            // array de la forma [0:X,1:Y]
detalleProductopf.Autenticado = false; // Indica si el usuario se encuentra autenticado
detalleProductopf.NOM_MAX_LENGTH = 40; // Constante que define el máximo número de caracteres del nombre.
detalleProductopf.ZoomVisible = false; // Indica si el zoom está visible.
detalleProductopf.CombosVisible = false;
detalleProductopf.KitsVisible = false;
detalleProductopf.TimerDescuento = null;
detalleProductopf.MostrarMotivo = false;
detalleProductopf.Acompanamiento = false;
detalleProductopf.Recargar = false;

function verDetalleProducto(codigoMundial)
{
    for(var id in Catalogo.Productos)
    {
        if(Catalogo.Productos[id].codigoMundial == codigoMundial)
        {
            CallMe.IdSKU = id;
            detalleProductopf.IdSKU = id;
            detalleProductopf.ObtenerDetalle(id);
            break;
        }
    }
}

detalleProductopf.mostrarScrollPanel = function()
{
    $('kitspf').style.display = detalleProductopf.KitsVisible ? 'block' : 'none';
    $('kitspf').style.visibility = detalleProductopf.KitsVisible ? 'visible' : 'hidden';
    $('combospf').style.display = detalleProductopf.CombosVisible ? 'block' : 'none';
    $('combospf').style.visibility = detalleProductopf.CombosVisible ? 'visible' : 'hidden';
}

detalleProductopf.ObtenerDetalle = function(idSKU)
{
     var MostrarDetalleProducto_OnSuccess = function(args)
            {
                var respuesta = eval('(' + args + ')');
                                
                if(!respuesta.resultado)
                {
                    ModalDialog.Show("Error", respuesta.mensajeError, ModalDialog.OK);
                    return;
                }
                $('lblNombreProdpf').innerHTML = respuesta.nom;
                    
                $('lblCodigopf').innerHTML = respuesta.cod;
                $('lblRefpf').innerHTML = respuesta.ref;
                $('lblEANpf').innerHTML = respuesta.ean;
                $('lblPUMpf').innerHTML = respuesta.pum;
                $('lblMarcapf').innerHTML = respuesta.mar;
                var marca_OnSuccess = function(args2)
                {
                    $('hlkMarcapf').href = "Homemarca.aspx?mnd1=" + args2    
                }
                Mundial.ScriptServices.Encriptar(respuesta.idmar, marca_OnSuccess, function(args2) { return null; }, function(args2) { });
                
                
                $('lblPrecioUnitariopf').innerHTML = respuesta.pu;
                $('lblDescuentopf').innerHTML = respuesta.des;
                
                if(respuesta.des != "0%")
                {
                    $('lblPrecioUnitariopf').className = "Scratchout";
                }
                else
                {
                    $('lblPrecioUnitariopf').className = "detalle_producto_precio";                    
                }
                
                
                $('lblPrecioDescuentopf').className = "detalle_producto_precio";
                $('lblPrecioDescuentopf').innerHTML = respuesta.pdes;
                $('lblUnidadVentapf').innerHTML = respuesta.uv;
                $('lblUnidadEmpaquepf').innerHTML = respuesta.ue;
                $('imgProd96pf').src = respuesta.img;
                $('lblDescripcionpf').innerHTML = respuesta.desc;
                $('lblEspecTecpf').innerHTML = respuesta.espec;
                $('lblTablaDescuentospf').innerHTML = respuesta.tdes;
                
                $('lbPreciopf').innerHTML = respuesta.pdes;
                
                
                //$('idVerInformacion').href = respuesta.vi;
                
                
                if (respuesta.kv == "true")
                {
                    detalleProductopf.KitsVisible = true;
//                    $('kitspf').style.display = "block";
//                    $('kitspf').style.visibility = "visible";                    
                }
                else
                {
                    detalleProductopf.KitsVisible = false;
//                    $('kitspf').style.display = "none";
//                    $('kitspf').style.visibility = "hidden";
                }
                if (respuesta.cv == "true")
                {
                    detalleProductopf.CombosVisible = true;
//                    $('combospf').style.display = "block";
//                    $('combospf').style.visibility = "visible";                    
                }
                else
                {
                    detalleProductopf.CombosVisible = false;
//                    $('combospf').style.display = "none";
//                    $('combospf').style.visibility = "hidden";
                }
                
                for (var i = 0; i < carruseles.length ; i++)
                    carruseles[i].actualizarDataSource(detalleProductopf.IdSKU);
                
                actualizarScrollPanel(detalleProductopf.IdSKU);
                var tabViewpf = new YAHOO.widget.TabView('tabs_detalle_productopf');
                if(respuesta.tdes == "")
                {
                    $('li3pf').style.display = "none";
                    if($('li3pf').style.display == "none")
                    {
                        tabViewpf.set('activeIndex', 0);
                    }
                }
                else
                {
                    $('li3pf').style.display = '';
                    tabViewpf.set('activeIndex', 2);
                }
                
                
                
                var ampliarJsonObj = {id:idSKU, nombre:respuesta.nom};
                
                if(YAHOO.util.Event.getListeners("aHrefZoompf", "click") != null)
                {
                    YAHOO.util.Event.removeListener("aHrefZoompf", "click");
                    YAHOO.util.Event.addListener('aHrefZoompf', "click", detalleProductopf.Zoom, ampliarJsonObj);
                }
                else
                {
                    YAHOO.util.Event.addListener('aHrefZoompf', "click", detalleProductopf.Zoom, ampliarJsonObj);
                }
                $('imgProductZoompf').src = respuesta.zoom;

                

                if(!Catalogo.Autenticado)
                {
                    $('idQtyQuickLookpf').style.display = "none";
                    $('SpanQtypf').style.display = "none";
                    $('idPrecioUnitariopf').style.display = "none";
                    $('idDescuentopf').style.display = "none";
                    $('PrecioDescuentopf').style.display = "none";
                    $('lbPreciopf').style.display = "none";
                    $('lbPreciopf').style.visibility = "hidden";                    
                }
                else
                {
                    $('idQtyQuickLookpf').value = respuesta.uv;
                }
                // Descuento adicional
                if(detalleProductopf.Acompanamiento)
                {
                    $('idMotivoDescuentopf').style.display = 'none';
                    $('idPUMpf').style.display = '';
                    $('idDescuentopf').style.display = 'none';
                    $('idDescuentoAdicionalpf').style.display = '';
                    $('idDescuentoMaxpf').style.display = '';
                    $('lblDescuentoMaxpf').innerHTML = respuesta.descuentoMaximo + '%';
                    // Cargar descuento por volumen
                    if(respuesta.descuentoVolumen > 0)
                    {
                        $('txtDescuentoAdicionalpf').value = respuesta.descuentoVolumen;
                        if(respuesta.descuentoVolumen > respuesta.descuentoMaximo)
                        {
                            $('idMotivoDescuentopf').style.display = '';
                            $('motivoDescuentopf').value = '-1';                            
                        }
                    }
                    else
                    {
                        $('txtDescuentoAdicionalpf').value = '00.00';
                        $('idMotivoDescuentopf').style.display = 'none';
                    }
                }
                // Usuario inactivo
                if(!QuickLookPopUp.ClienteActivo)
                {
                    $('idPUMpf').style.display = 'none';
                    $('idDescuentopf').style.display = 'none';
                    $('idDescuentoAdicionalpf').style.display = 'none';
                    $('PrecioDescuentopf').style.display = 'none';
                    $('lblPrecioUnitariopf').className = "detalle_producto_precio";
                    $('idDescuentoMaxpf').style.display = 'none';
                }
                $('imgOfertaPublicacion').style.display = (respuesta.oferta ? 'inline' : 'none');
                $('imgNuevoPublicacion').style.display = (respuesta.nuevo ? 'inline' : 'none');
                $('imgComboPublicacion').style.display = (respuesta.combo ? 'inline' : 'none');

                detalleProductopf.Mostrar(respuesta.nom);
            }


            Mundial.ScriptServices.DetallePedidosRapidos(idSKU, Catalogo.GUID, MostrarDetalleProducto_OnSuccess, function(args) { }, function(args) { });
}


detalleProductopf.Mostrar = function(titulo)
{
    if(!detalleProductopf.Visible)
    {
        detalleProductopf.Visible = true;
        
        var panelDetProd = $('pnlDetalleProductopf');
        YAHOO.util.Dom.setStyle(panelDetProd, 'opacity', 0);
        panelDetProd.style.display='block';
        panelDetProd.style.visibility='visible';
        $('lblNombreProdpf').style.fontSize = detalleProductopf.GetTitleSize(463, 24, titulo);
        detalleProductopf.Posicion = [0, 0];
        detalleProductopf.Posicion[0] = (YAHOO.util.Dom.getViewportWidth() - panelDetProd.clientWidth)/2 - 50;
        detalleProductopf.Posicion[1] = YAHOO.util.Dom.getDocumentScrollTop() + ((YAHOO.util.Dom.getViewportHeight() - panelDetProd.clientHeight)/2);
        YAHOO.util.Dom.setXY(panelDetProd, detalleProductopf.Posicion);
        var animObj = new YAHOO.util.Anim('pnlDetalleProductopf', { opacity: { from: 0, to: 1 } }, 0.5, YAHOO.util.Easing.easeOut);
        animObj.animate();
    }
}

//*************************************************************************************************

detalleProductopf.GetTitleSize = function(maxContTitleSize, titleSize, title)
{
    var tag_nobr = $('hiddenNoBrpf');
    var size = titleSize;
    
    tag_nobr.innerHTML = title;
    tag_nobr.style.fontSize = titleSize + "px";
    
    while(tag_nobr.offsetWidth > maxContTitleSize)
    {
        size = size - 1;
        tag_nobr.style.fontSize = size + "px";        
    }
    
    return size + "px";
}

//*************************************************************************************************

detalleProductopf.Zoom = function(e, opciones)
{
    detalleProductopf.IdSKU = opciones.id;
    
    if(opciones.id != null && !detalleProductopf.ZoomVisible)
    {
        detalleProductopf.ZoomVisible = true;
        var panelDetProd = $('pnlZoomProductpf');        
        $('lblAmpNombreProdpf').innerHTML = opciones.nombre;
        $('lblAmpNombreProdpf').style.fontSize = detalleProductopf.GetTitleSize(280, 24, opciones.nombre);
        YAHOO.util.Dom.setStyle(panelDetProd, 'opacity', 0);
        panelDetProd.style.display='block';
        panelDetProd.style.visibility='visible';
        // Posición en X de la ventana de Zoom.
        detalleProductopf.Posicion[0] = (YAHOO.util.Dom.getXY('aHrefZoompf')[0] - ((panelDetProd.offsetWidth - $('aHrefZoompf').offsetWidth) / 2));
        // Posición en Y de la ventana de Zoom con respecto al link de ampliar de la ventana de detalle.
        detalleProductopf.Posicion[1] = YAHOO.util.Dom.getXY('aHrefZoompf')[1] - 340;
        YAHOO.util.Dom.setXY(panelDetProd, detalleProductopf.Posicion);
        var animObj = new YAHOO.util.Anim('pnlZoomProductpf', { opacity: { from: 0, to: 1 } }, 1, YAHOO.util.Easing.easeOut);
        animObj.animate();
    }
}

detalleProductopf.Restore = function()
{
    if($('pnlZoomProductpf') != null)
        var animObj = new YAHOO.util.Anim('pnlZoomProductpf', { opacity: { from: 1, to: 0 } }, 1, YAHOO.util.Easing.easeOut);
        animObj.onComplete.subscribe(function()
        {
            this.getEl().style.display='none';
            this.getEl().style.visibility='hidden';
            detalleProductopf.ZoomVisible=false;
        });
        animObj.animate();
}

detalleProductopf.AgregarCarropf = function()
{
    if(Catalogo.Autenticado == false)
    {
        $('idQtyQuickLookpf').value = "1";
        detalleProductopf.Recargar = true; 
        detalleProductopf.Login();
        return false;
    }
    else
    {
        var panelDetProd = $('pnlLoginZoom');        
        YAHOO.util.Dom.setStyle(panelDetProd, 'opacity', 0);
        panelDetProd.style.display='none';
        panelDetProd.style.visibility='hidden';                
    }
    var cantidad = $('idQtyQuickLookpf').value.replace(/^\\s+|\\s+$/g,'');
    var descuento = $('txtDescuentoAdicionalpf').value.replace(/^\\s+|\\s+$/g,'');
    var motivo = $('motivoDescuentopf').value;
    if(cantidad.length == 0 || !IsNumeric(cantidad) || !IsInt(cantidad) || parseInt(cantidad) <= 0)
    {
        ModalDialog.Show("Error", "La cantidad no es válida", ModalDialog.OK);
        return;
    }
    var AgregarCarro_OnSuccess = function(args)
    {
        panelLoading.hide();
        var respuesta = eval('(' + args + ')');
        if(!respuesta.resultado)
        {
            ModalDialog.Show("Error", respuesta.mensajeError, ModalDialog.OK);
            return;
        }
        actualizarEstadoLoginTop('true');                        
        mostrarSliderCarroCompra(respuesta.datosSlider);        
        actualizarContadorCarro(respuesta.totalCarro);
    }
    panelLoading.show();
    $('panelLoading_mask').style.zIndex=1001;
    $('panelLoading_c').style.zIndex=1002;
    
    if(!detalleProductopf.Acompanamiento)
        Mundial.ScriptServices.AgregarCarro(detalleProductopf.IdSKU, cantidad, AgregarCarro_OnSuccess, Catalogo.WS_OnError, Catalogo.WS_OnTimeout);
    else
        Mundial.ScriptServices.AgregarCarroAcompanamiento(detalleProductopf.IdSKU, cantidad, descuento, motivo, AgregarCarro_OnSuccess, Catalogo.WS_OnError, Catalogo.WS_OnTimeout);
}

detalleProductopf.Login = function()
{
    var panelDetProd = $('pnlLoginZoom');        
    YAHOO.util.Dom.setStyle(panelDetProd, 'opacity', 0);
    panelDetProd.style.display='block';
    panelDetProd.style.visibility='visible';
    // Posición en X de la ventana de Zoom.
    detalleProductopf.Posicion[0] = (YAHOO.util.Dom.getViewportWidth() - panelDetProd.clientWidth)/2 + 130;
    // Posición en Y de la ventana de Zoom con respecto al link de ampliar de la ventana de detalle.
    detalleProductopf.Posicion[1] = YAHOO.util.Dom.getDocumentScrollTop() + ((YAHOO.util.Dom.getViewportHeight() - panelDetProd.clientHeight)/2) - 40;
    YAHOO.util.Dom.setXY(panelDetProd, detalleProductopf.Posicion);
    var animObj = new YAHOO.util.Anim('pnlLoginZoom', { opacity: { from: 0, to: 1 } }, 1, YAHOO.util.Easing.easeOut);
    animObj.animate();
}

//*************************************************************************************************

detalleProductopf.CerrarLogin = function()
{
    var animObj = new YAHOO.util.Anim('pnlLoginZoom', { opacity: { from: 1, to: 0 } }, 1, YAHOO.util.Easing.easeOut);
    animObj.onComplete.subscribe(function()
    {
        this.getEl().style.display='none';
        this.getEl().style.visibility='hidden';
    });
    animObj.animate();
}

//*************************************************************************************************

detalleProductopf.Cerrar = function()
{
    if(detalleProductopf.ZoomVisible)
        return;
    if($('pnlDetalleProductopf') != null)
        var animObj = new YAHOO.util.Anim('pnlDetalleProductopf', { opacity: { from: 1, to: 0 } }, 1, YAHOO.util.Easing.easeOut);
        animObj.onComplete.subscribe(function()
        {
            this.getEl().style.display='none';
            this.getEl().style.visibility='hidden';
            $('pnlDetalleProductopf').style.display='none';
            $('pnlDetalleProductopf').style.visibility='hidden';
            detalleProductopf.Visible=false;
        });
        animObj.animate();
     if (detalleProductopf.Recargar)
     {
        detalleProductopf.Recargar = false; 
        document.location.reload(true);        
     }
}

//*************************************************************************************************

detalleProductopf.AgregarCarro = function()
{
    var cantidad = $('idQtyQuickLookpf').value.replace(/^\\s+|\\s+$/g,'');
    if(cantidad.length == 0 || !IsNumeric(cantidad) || !IsInt(cantidad) || parseInt(cantidad) <= 0)
    {
        ModalDialog.Show("Error", "La cantidad no es válida", ModalDialog.OK);
        return;
    }
    var AgregarCarro_OnSuccess = function(args)
    {
        panelLoading.hide();
        var respuesta = eval('(' + args + ')');
        if(!respuesta.resultado)
        {
            ModalDialog.Show("Error", respuesta.mensajeError, ModalDialog.OK);
            
            return;
        }
        
        mostrarSliderCarroCompra(respuesta.datosSlider);
        actualizarContadorCarro(respuesta.totalCarro);
    }
    panelLoading.show();
    $('panelLoading_mask').style.zIndex=1001;
    $('panelLoading_c').style.zIndex=1002;
    Mundial.ScriptServices.AgregarCarro(detalleProductopf.IdSKU, cantidad, AgregarCarro_OnSuccess, Catalogo.WS_OnError, Catalogo.WS_OnTimeout);
    
}




function imprimirDetalle()
{
    var ventana = window.open("print.htm", "imprimirDetalle", "toolbar=0,status=0,location=0,menubar=0,scrollbars=1,width=800,height=600");
    if(ventana == null)
    {
        alert("Para imprimir, debe permitir el uso de ventanas emergentes para www.mundial.com.co");
        return;
    }
    ventana.document.write('<html><head><style>#vistaprevia,#tituloContent table, #detalle_producto_grid_especificacionespf table,#kitspf table,#combospf table, #tabs_detalle_productopf table{font-size:11px;}</style>');
    ventana.document.write('<link rel="shortcut icon" type="image/ico" href="images/encabezado/favicon.ico" />');
    ventana.document.write('<link href="css/print.css" rel="stylesheet" type="text/css" />');
    ventana.document.write('<link rel="stylesheet" type="text/css" href="js/yui/container/assets/skins/sam/container.css" />');
    ventana.document.write('<link rel="stylesheet" type="text/css" href="js/yui/tabview/assets/skins/sam/tabview.css">');
    ventana.document.write('</head><body class="yui-skin-sam" onload="window.print();window.close();"><div id="vistaprevia">');
    ventana.document.write($('pnlDetalleProductopf').innerHTML.replace(/<script/gi,'<!--<script').replace(/<\/script>/gi, '</script>-->').replace(/UnTip\(/gi, '//UnTip(').replace(/Tip\(/gi, '//Tip('));
    ventana.document.write('</div><script type="text/javascript">');
    ventana.document.write('(function(){ document.getElementById(\'toolboxContainer\').style.display=\'none\'; })();');
    ventana.document.write('</script></body></html>');
    ventana.document.close();
}
//*************************************************************************************************



detalleProductopf.descuentoOnKeyUp = function(ev, el)
{
    var teclaValida = detalleProductopf.descuentoOnKeyDown(ev);
    if(!teclaValida)
        return false;
    if(detalleProductopf.TimerDescuento != null)
        clearTimeout(detalleProductopf.TimerDescuento);
    detalleProductopf.TimerDescuento = setTimeout(function() { detalleProductopf.ActualizarDescuento(el.value, detalleProductopf.IdSKU); }, 1000);
}

//*************************************************************************************************

detalleProductopf.descuentoOnFocus = function(el)
{
    if(el.value=="00.00")
        el.value="";
}

//*************************************************************************************************

detalleProductopf.descuentoOnBlur = function(el)
{
    if(el.value=="")
    {
        el.value="00.00";
        return true;
    }
    return false;
}

//*************************************************************************************************

detalleProductopf.descuentoOnKeyDown = function(ev)
{
    if(ev.which || ev.keyCode)
    {
        if ((ev.which == 13) || (ev.keyCode == 13)) {return false;}
    } else {return true}
    var codes = new Array(9,48,49,50,51,52,53,54,55,56,57,8,37,39,46,13,96,97,98,99,100,101,102,103,104,105,109,110,190);
    if(ev.which != undefined) 
        aux = !(codes.indexOf(ev.which) < 0);
    if(ev.keyCode != undefined) 
        aux = !(codes.indexOf(ev.keyCode) < 0);
    if (ev.shiftKey || ev.altKey || ev.ctrlKey)
        return false;
    return aux;
}

//*************************************************************************************************

detalleProductopf.ActualizarDescuento = function(descuento, idsku)
{
    descuento = descuento.replace(/^\\s+|\\s+$/g,'');
    $('motivoDescuentopf').value = '-1';
    motivo = $('motivoDescuentopf').value;
    var i;
    if(isNaN(parseFloat(descuento)))
        return;
    var actualizarDescuento_OnSuccess = function(result, eventargs)
    {
        panelLoading.hide();
        respuesta =  eval("(" + result + ")");
        if(!respuesta.resultado)
        {
            ModalDialog.Show("Error", respuesta.mensajeError , ModalDialog.OK);
            $('motivoDescuentopf').style.display = "none";
            $('txtDescuentoAdicionalpf').value = "00.00";
            return;
        }
        detalleProductopf.MostrarMotivo = (detalleProductopf.Acompanamiento && respuesta.mostrarMotivo);
        $('idMotivoDescuentopf').style.display = (detalleProductopf.MostrarMotivo ? "" : "none");
        $('lblPrecioDescuentopf').innerHTML = respuesta.valorUnitario;
    }
    var actualizarDescuento_OnError = function(error)
    {
        panelLoading.hide();
        ModalDialog.Show("Error", "Verifique que el descuento sea un valor numérico", ModalDialog.OK);
    }
    panelLoading.show();
    $('panelLoading_mask').style.zIndex=1001;
    $('panelLoading_c').style.zIndex=1002;
    Mundial.ScriptServices.ActualizarDescuentoQuickLook(idsku, descuento, actualizarDescuento_OnSuccess, actualizarDescuento_OnError, function(args) { });
}

//*************************************************************************************************

