﻿//////////////////////////
//- Variables globales -//
//////////////////////////
var dobj;
var NumeroDeFichasAbiertas = 0;
var moz = document.getElementById && !document.all;
var estoyArrastrando = false;
var DIVMover = "B_Ficha_drag"
var DIVMovido = "";
var IDPNLNoteolvides = ""
var IDPNLComentarios = ""
var IDPNLAlternativas = ""

/////////////////////////////
//- Funciones automáticas -//
/////////////////////////////
document.onmousedown = presionarBoton; //Para mover la ficha
document.onmousemove = arrastrarRaton; //Para mover la ficha
document.onmouseup = soltarBoton; //Para mover la ficha

//////////////////////////////////
//***- FUNCIONES WEBSERVICE -***//
//////////////////////////////////

//-> Muestro la ficha
function VerFicha(CodProd) {
    if (Codcliente == '') { Codcliente = $get('ctl00_CodClienteJS').value; }
    ConexionSW.VerFicha(CodProd, Codcliente, Path_Imagenes, VerFicha_OK, VerFicha_FAIl); 
 }
function VerFicha_OK(resultado) {
    var NuevaFicha = document.createElement("div");
    NuevaFicha.innerHTML = resultado;
    document.body.appendChild(NuevaFicha);
    var Top = parseInt(-260);
    Top = Top + (NumeroDeFichasAbiertas + 15) * NumeroDeFichasAbiertas;
    var Left = parseInt(-350);
    Left = Left + (NumeroDeFichasAbiertas + 15) * NumeroDeFichasAbiertas;
    $get(NuevaFicha.childNodes[0].id).style.marginTop = Top + 'px';
    $get(NuevaFicha.childNodes[0].id).style.marginLeft = Left + 'px';
    $get(NuevaFicha.childNodes[0].id).style.display = 'block';
    NumeroDeFichasAbiertas = NumeroDeFichasAbiertas + 1;
}
function VerFicha_FAIl(Error) { alert('No se ha podido abrir la ficha de este producto.'); }

//-> Inserta un nuevo comentario en un producto
function INS_ComentariosProducto(Comentario, CodProducto) { ConexionSW.INS_Comentario_Producto(Codcliente, CodUsuario, Comentario, CodProducto, INS_ComentariosProducto_OK, INS_ComentariosProducto_FAIL); }
function INS_ComentariosProducto_OK(resultado) { if (resultado == 0) { alert('Ya has comentado este producto.'); } else { alert('Gracias por comentar.'); Ficha_PintarPNLComentarios(resultado); } }
function INS_ComentariosProducto_FAIL(Error) { alert('No se pueden guardar comentarios en este momento. Por favor, intentelo de nuevo mas tarde.'); }

//-> Guarda una nueva valoracion de un producto
function ValorarProducto(CodProducto, Valoracion) { ConexionSW.UPD_ValorarProducto(CodProducto, CodUsuario, Valoracion, ValorarProducto_OK, ValorarProducto_FAIL); }
function ValorarProducto_OK(resultado) { if (resultado == "") { alert('Ya has valorado este producto recientemente.'); } else { alert('Su valoracion ha sido enviada. Gracias por votar.'); Ficha_PintarPNLComentarios(resultado); } }
function ValorarProducto_FAIL(error) { alert('La valoracion esta temporalmente deshabilitada para este producto.'); }

//-> Muestro el panel de no te olvides de
function Ficha_PintarPNLNoteolvides(CodProd) { ConexionSW.Ficha_Pintar_PNLNoteolvides(Codcliente, CodProd, IDPNLNoteolvides, Ficha_PintarPNLNoteolvides_OK, Ficha_PintarPNLNoteolvides_FAIL); }
function Ficha_PintarPNLNoteolvides_OK(resultado) { $get(IDPNLNoteolvides).innerHTML = resultado; }
function Ficha_PintarPNLNoteolvides_FAIL(error) { alert('No se encuentran disponibles los complementos en este momento.'); }

//-> Muestro el panel de las alternativas en la ficha
function Ficha_PintarPNLAlternativas(CodProd) { ConexionSW.Ficha_Pintar_PNLAlternativas(Codcliente, CodProd, IDPNLAlternativas, Ficha_PintarPNLAlternativas_OK, Ficha_PintarPNLAlternativas_FAIL); }
function Ficha_PintarPNLAlternativas_OK(resultado) { $get(IDPNLAlternativas).innerHTML = resultado; }
function Ficha_PintarPNLAlternativas_FAIL(error) { alert('No se encuentran disponibles las alternativas en este momento.'); }

//-> Muestro el panel de los comentarios en la ficha
function Ficha_PintarPNLComentarios(CodProd) { ConexionSW.Ficha_Pintar_PNLComentarios(Codcliente, CodProd, IDPNLComentarios, Path_Imagenes, Ficha_PintarPNLComentarios_OK, Ficha_PintarPNLComentarios_FAIL) }
function Ficha_PintarPNLComentarios_OK(resultado) { $get(IDPNLComentarios).innerHTML = resultado; }
function Ficha_PintarPNLComentarios_FAIL(error) { alert('No se pueden mostrar los comentarios en este momento.'); }

//-> Muestro la ficha extendida
function PrintProduct(Link, Precio, LogoTienda) { ConexionSW.ImprimirFicha(Link, Precio, LogoTienda, ImprimirOk); }
function ImprimirOk(resultado) { window.open(resultado); }

//-> Envio la ficha por e-mail
function EnviarFichaPorEmail(TuNombre, TuEmail, EmailDestinatario, Asunto, Body, MiImg, Porcen, Producto) {
    try {
        var TuNombreC = $get(TuNombre).value;
        var TuEmailC = $get(TuEmail).value;
        var EmailDestC = $get(EmailDestinatario).value;
        var AsuntoC = $get(Asunto).value;
        var BosyC = $get(Body).value;
        var MiImgC; try { MiImgC = $get(MiImg).checked; }

        catch (Error)
        { MiImgC = false; }
        var PorcenC;
        try {
            PorcenC = $get(Porcen).value;
        }
        catch (Error) { PorcenC = '0'; }
        //alert('Ficha de Producto enviada correctamente');
        document.forms[0].hidTDClickID.value = 'Envio_Send#' + TuNombreC + '#' + TuEmailC + '#' + EmailDestC + '#' + AsuntoC + '#' + BosyC + '#' + MiImgC + '#' + PorcenC + '#' + Producto; document.forms[0].btnTDClicked.click(); alert(TuNombreC + ', Su ficha se ha enviado correctamente a ' + EmailDestC);
    }
    catch (Error) { }
}

function makeAJAXPostback(Cnt, TDClickID) {
    if (TDClickID == 'Dinamico') {
        var cnt = $get(Cnt).value;
        var CodProd = $get('ctl00_ContentPlaceHolder1_CampoOculto').value;
        document.forms[0].hidTDClickID.value = CodProd + '#' + cnt; document.forms[0].btnTDClicked.click();
        alert("El Producto se agrego correctamente a su cesta de la compra");
    }
    else {
        var cnt = $get(Cnt).value;
        document.forms[0].hidTDClickID.value = TDClickID + '#' + cnt;
        document.forms[0].btnTDClicked.click();
    }
}

/////////////////////////
//- Funciones comunes -//
/////////////////////////

//-> Al cerrar la ficha, corrige la var
function ReiniciarFichasAbiertas() { NumeroDeFichasAbiertas = NumeroDeFichasAbiertas - 1; }

//-> Muestra/Oculta los botones (Imprimir, Mail y Comentarios) dentro de la ficha
function MostrarOcultarBotonFicha(div) {
    if ($get(div).style.display == 'inline') {
        $get(div).style.display = 'none';
    } else {
        $get(div).style.display = 'inline';
    }
}

//-> Muestra los paneles de la ficha
function Ficha_mostrarpanel(obj, PNLEspec, PNLEnvio, PNLNoteolvides, PNLAlternativas, PNLComentarios) {
    $get(PNLEspec).style.display = 'none';
    $get(PNLEnvio).style.display = 'none';
    $get(PNLNoteolvides).style.display = 'none';
    $get(PNLAlternativas).style.display = 'none';
    $get(PNLComentarios).style.display = 'none';
    $get(obj).style.display = 'block';
}

//-> Carga la valoracion de un producto
function PintarValoraciones(num, idstar1, idstar2, idstar3, idstar4, idstar5) {
    var startnum = parseFloat(num);
    ValoracionesEmpty(idstar1, idstar2, idstar3, idstar4, idstar5);
    switch (startnum) {
        case 1: { $get(idstar1).style.backgroundPosition = '0 0'; return true; }
        case 1.5: { $get(idstar1).style.backgroundPosition = '0 0'; $get(idstar2).style.backgroundPosition = '0 -16px'; return true; }
        case 2: { $get(idstar1).style.backgroundPosition = '0 0'; $get(idstar2).style.backgroundPosition = '0 0'; return true; }
        case 2.5: { $get(idstar1).style.backgroundPosition = '0 0'; $get(idstar2).style.backgroundPosition = '0 0'; $get(idstar3).style.backgroundPosition = '0 -16px'; return true; }
        case 3: { $get(idstar1).style.backgroundPosition = '0 0'; $get(idstar2).style.backgroundPosition = '0 0'; $get(idstar3).style.backgroundPosition = '0 0'; return true; }
        case 3.5: { $get(idstar1).style.backgroundPosition = '0 0'; $get(idstar2).style.backgroundPosition = '0 0'; $get(idstar3).style.backgroundPosition = '0 0'; $get(idstar4).style.backgroundPosition = '0 -16px'; return true; }
        case 4: { $get(idstar1).style.backgroundPosition = '0 0'; $get(idstar2).style.backgroundPosition = '0 0'; $get(idstar3).style.backgroundPosition = '0 0'; $get(idstar4).style.backgroundPosition = '0 0'; return true; }
        case 4.5: { $get(idstar1).style.backgroundPosition = '0 0'; $get(idstar2).style.backgroundPosition = '0 0'; $get(idstar3).style.backgroundPosition = '0 0'; $get(idstar4).style.backgroundPosition = '0 0'; $get(idstar5).style.backgroundPosition = '0 -16px'; return true; }
        case 5: { $get(idstar1).style.backgroundPosition = '0 0'; $get(idstar2).style.backgroundPosition = '0 0'; $get(idstar3).style.backgroundPosition = '0 0'; $get(idstar4).style.backgroundPosition = '0 0'; $get(idstar5).style.backgroundPosition = '0 0'; return true; }
    }
}

//-> Restablece la imagen de las estrellas de valoracion a vacio
function ValoracionesEmpty(id1, id2, id3, id4, id5) {
    $get(id1).style.backgroundPosition = '0 -32px';
    $get(id2).style.backgroundPosition = '0 -32px';
    $get(id3).style.backgroundPosition = '0 -32px';
    $get(id4).style.backgroundPosition = '0 -32px';
    $get(id5).style.backgroundPosition = '0 -32px';
}

//-> Funciones de captura para mover la ficha de producto
function presionarBoton(e) {
    if (DIVMovido == '') { } else {
        var fobj = moz ? e.target : event.srcElement;
        while (fobj.tagName.toLowerCase() != "html" && fobj.id != DIVMover) { fobj = moz ? fobj.parentNode : fobj.parentElement; }
        if (fobj.id == DIVMover) { estoyArrastrando = true; dobj = fobj; return false; }
    }
}
function arrastrarRaton(e) {
    if (estoyArrastrando) {
        newLeft = moz ? e.clientX : event.clientX;
        newTop = moz ? e.clientY : event.clientY;
        document.getElementById(DIVMovido).style.left = newLeft + "px";
        document.getElementById(DIVMovido).style.top = newTop + 200 + "px";
        return false;
    }
}
function soltarBoton() { estoyArrastrando = false; }
