﻿$(document).ready(function() {
    if ($(".rss").html() == null) {
        $(".pag").attr("colspan", "3");
    }
});
$(".bar").live("click", function() {
    $(this).next(".box").toggle();
    if ($(this).find("img").hasClass("facettoggle")) {
        $(this).find("img").removeClass("facettoggle");
        $(this).find("img").addClass("facettoggle2");
    } else {
        $(this).find("img").removeClass("facettoggle2");
        $(this).find("img").addClass("facettoggle");
    }
});
    
$(".atit, .alrt").live("click", function() { $(".atit").hide(); $(".alertfrm").show(); $.scrollTo($("#frmAlert"), 200); $("#cat").focus(); });
$(".close, .cls").live("click", function() { $(".alertfrm").hide(); $(".atit").show(); $(".alertmsg .atxt").html(''); $(".alertmsg").addClass('nodis'); });

$(".more").live("click", function() {
    $(this).parent().children().show();
    $(this).hide();
    return false;
});

function sendalert() {
    var data = "cat=" + $("#cat").val() + "&scat=" + $("#scat").val() + "&name=" + $("#name").val() + "&surname=" + $("#surname").val() + "&email=" + $("#email").val() + "&ctry=" + $("#ctry").val() + "&prov=" + $("#prov").val();
    $.ajax({
        type: "POST",
        url: "/_sysutils/alert.aspx",
        data: data,
        success: function(j, s) {
        if (j.length > 0) {
            $(".alertfrm").hide();
            $(".alertmsg .msg").html(j);
            $(".alertmsg").removeClass('nodis');
        }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            $(".alertfrm").hide();
            $(".alertmsg .msg").html('<div class="t">No ha sido posible crear tu alerta</div><div class="st">Por favor, inténtalo de nuevo más tarde.</div>');
            $(".alertmsg").removeClass('nodis');
        }
    });
}

function checkcond() {
    if ($("#cond").attr("checked"))
        return true;
    else {
        alert('Si deseas crear una alerta, debes aceptar las condiciones de servicio y la política de privacidad de datos');
        return false;
    }
}


$("#sbm").live("click", function() {
    htmlForSummary = '';
    $(".err").hide();
    $(".txReq").removeClass("txReq");
    $(".ismarked").removeClass("ismarked");
    $(".ValidateForm :input").trigger("blur");
    if ($(".ValidateForm").hasClass("SummaryError")) {
        if (htmlForSummary != '') {
            SetSummary(htmlForSummary)
            htmlForSummary = "";
            return false;
        } else {
            if (checkcond())
                sendalert();
            else
                return false;
        }
    } else {
        if ($(".ismarked").length != 0) {
            $.scrollTo($("#frmAlert"), 200);
            return false;
        } else {
            if (checkcond())
                sendalert();
            else
                return false;
        }
    }
});

$("#cat").change(function() {
$.getJSON("/_sysutils/GetSelect.aspx", { id: $(this).val(), dic: '<%= (byte)SF_Core.Enumerators.Dictionary.SubCategories %>', firstVal: '',  firstText: '<%=GetGlobalResourceObject("cursos", "Todas").ToString() %>' }, function(j) {
        var options = '';
        for (var i = 0; i < j.length; i++) {
            options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
        }
        $("#scat").html(options);
    });
});

$("#ctry").change(function() {
    $.getJSON("/_sysutils/GetSelect.aspx", { id: $(this).val(), dic: '<%= (byte)SF_Core.Enumerators.Dictionary.Provinces %>' }, function(j) {
        var options = '';
        for (var i = 0; i < j.length; i++) {
            options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
        }
        $("#prov").html(options);
        $("#prov").addClass("err_cbx_obligatorio");
    });
});