﻿function AddUnidad(unidad, prod, tipo,amigable) {

    var aux = window.location.href.split("/");
    var loc = aux[0] + "//" + aux[2] + "/views/publica/juguetes/juguete_detail_publica.aspx";
    
    
    var indice = loc.indexOf("?");
    if (indice != -1)
        loc = loc.substr(0, indice);

    $.ajax({
        type: "POST",
        url: loc + "/AddUnidad",
        data: "{cantidad: '" + unidad + "', prod: '" + prod + "', tipo: '" + tipo + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        cache: false,
        success: function(msg) {
            $(".number").html(msg.d);

            var cantidad = parseInt(msg.d);

            if (cantidad < 20) {
                if ($(".sumar").attr("onclick") == null) {
                    $(".sumar").attr("onclick", "");
                    $(".sumar").bind("click", function(e) { e.preventDefault(); AddUnidad(1, prod, tipo, amigable); return false; });
                }
                $(".sumar").attr("href", "/" + amigable + "/sumar");
            }
            else {
                $(".sumar").unbind();
                $(".sumar").removeAttr("onclick");
                $(".sumar").removeAttr("href");
            }

            if (cantidad > 1) {
                if ($(".restar").attr("onclick") == null) {
                    $(".restar").attr("onclick", "");
                    $(".restar").bind("click", function(e) { e.preventDefault(); AddUnidad(-1, prod, tipo, amigable); return false; });
                }
                $(".restar").attr("href", "/" + amigable + "/restar");
            }
            else {
                $(".restar").unbind();
                $(".restar").removeAttr("onclick");
                $(".restar").removeAttr("href");
            }
        }
    });

}


function vota(id, tipo) {

    var aux = window.location.href.split("/");
    var loc = aux[0] + "//" + aux[2] + "/views/publica/juguetes/juguete_detail_publica.aspx";


    var indice = loc.indexOf("?");
    if (indice != -1)
        loc = loc.substr(0, indice);

    $.ajax({
        type: "POST",
        url: loc + "/Vota",
        data: "{id: '" + id + "', tipo: '" + tipo + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        cache: false,
        success: function(msg) {
            $(".success").html("Gracias por tu voto");
            $(".success").attr("class", "voto_ok");
            $(".voto_ok").removeAttr("href");
        }
    });

}


function Guardar(carro) {

    var aux = window.location.href.split("/");
    var loc = aux[0] + "//" + aux[2] + "/iocoo.aspx";

    var indice = loc.indexOf("?");
    if (indice != -1)
        loc = loc.substr(0, indice);

    var al = document.getElementById("uxtb_destino");
    var origen = al.value;
    al = document.getElementById("uxtb_enviado");
    var destino = al.value;
    al = document.getElementById("uxtb_comentario");
    var mensa = al.value;


    var img;
    var i;
    for(i = 1; i<=3;i++){
        if (document.getElementById("ctl00_ux_cph_uxrb_" + i).checked)
            img = i;
    }


    $.ajax({
        type: "POST",
        url: loc + "/GuardarFelicitacion",
        data: "{img: '" + img + "', carro: '" + carro + "', 'origen': '" + origen + "', 'destino': '" + destino + "', 'mensaje': '" + mensa + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        cache: false,
        success: function(msg) {
            $("#mensaje").html("<p class=\"ico_tarjeta\"><a href=\"/views/intranet/informes/visor_felicitacion.aspx?id=" + msg.d +"\" title=\"Ver Tarjeta\" rel=\"external\">Ver mi tarjeta de regalo</a></p>");
            $("#tarjeta_fecilitacion").html("");
            document.getElementById("uxbt_felicitaciones").value = "Editar Tarjeta de Regalo";
        }
    });

}

function CargarDesc(seccion, id, tipo,amigable) {

    var aux = window.location.href.split("/");
    var loc = aux[0] + "//" + aux[2] + "/views/publica/juguetes/juguete_detail_publica.aspx";

    var indice = loc.indexOf("?");
    if (indice != -1)
        loc = loc.substr(0, indice);

    $.ajax({
        type: "POST",
        url: loc + "/TextoDescripciones",
        data: "{secc: '" + seccion + "', id: '" + id + "', tipo: '" + tipo + "', 'amigable': '" + amigable + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        cache: false,
        success: function(msg) {
        $(".descripciones").html(msg.d);

        }
    });
}


function ReloadGallery(id) {

    var aux = window.location.href.split("/");
    var loc = aux[0] + "//" + aux[2] + "/views/publica/juguetes/juguete_detail_publica.aspx";

    var indice = loc.indexOf("?");
    if (indice != -1)
        loc = loc.substr(0, indice);

    $.ajax({
        type: "POST",
        url: loc + "/ReloadGallery",
        data: "{id: '" + id + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        cache: false,
        success: function(msg) {
            $(".galeria-big").html(msg.d);
        }
    });
}


function Poner(nombre) {
    document.getElementById("ctl00_uxtb_BuscNombre").value = nombre;
    $("#resultados-ajax").html("");
}

function MostrarFelicitaciones() {

    var aux = window.location.href.split("/");
    var loc = aux[0] + "//" + aux[2] + "/iocoo.aspx";

    var indice = loc.indexOf("?");
    if (indice != -1)
        loc = loc.substr(0, indice);
    
    var a = $("#tarjeta_fecilitacion").html();

    if ((a.toString() == null) || (a.toString() == "") || a.length == 29)
        oculto = 0;
    else
        oculto = 1;


    $.ajax({
        type: "POST",
        url: loc + "/MostrarFelicitaciones",
        data: "{oculto: '" + oculto + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        cache: false,
        success: function(msg) {
            $("#tarjeta_fecilitacion").html(msg.d);
        }
    });
}


function Buscar() {

    var aux = window.location.href.split("/");
    var loc = aux[0] + "//" + aux[2] + "/iocoo.aspx";

    var indice = loc.indexOf("?");
    if (indice != -1)
        loc = loc.substr(0, indice);

    var al = document.getElementById("ctl00_uxtb_BuscNombre");
    var mystring = al.value;

    $("#resultados-ajax").html("");

    $.ajax({
        type: "POST",
        url: loc + "/Buscar",
        data: "{'msg': '" + mystring + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        cache: false,
        success: function(msg) {
            $("#resultados-ajax").html(msg.d);
        }
    });

}