﻿$(function () {
    if ($('.checkBox').length > 0) {
        $.getScript('/Scripts/jquery.disable.text.select.pack.js', function () {
            $('.checkBox').disableTextSelect();
        });
    }

    $('input').attr('autocomplete', 'off');
    $('.checkBox').checkBoxOverride();
    if ($('#cs_config_country_field').length > 0)
        $('#cs_config_country_field').val($('.country').attr('id'));
    if ($('#cs_config_state_field').length > 0)
        $('#cs_config_state_field').val($('.state').attr('id'));
    if ($('#cs_config_country_field').length > 0 || $('#cs_config_state_field').length > 0)
        initCountry();

    $('.resendInvite').click(function () {
        $.ajax({
            url: '/Member/ReSendEmailConfirmation/',
            cache: false,
            type: "POST"
        });
        $(this).after('<span class="inviteSent">Sent!</span>');
        $(this).remove();
    });

    var f = $('.authenticationForm').ajaxForm(function (data) {
        if (data.result) {
            window.location.href = data.url
        }
        else {
            $('#loginErrors').html(data.error);
        }
    });

    //$(window).blur(function () { $('input').blur(); });
});

(function ($) {
    $.fn.extend({
        checkBoxOverride: function () {
            if ($(document).data('chk:' + $(this).selector))
                return;

            $(document).data('chk:' + $(this).selector, true);

            $(this).click(function () {
                var value = ($($(this).find('div')[0]).css('background-image').indexOf('off.jpg') >= 0);
                applyValue(this, value);
            });

            $.fn.checked = function (value) {
                applyValue(this, value);
            }

            $.fn.makeReadOnly = function (value) {
                $(this).data('readonly', value);
            }

            function applyValue(item, value) {
                if ($(item).data('readonly'))
                    return;
                var status = (value ? "on" : "off");
                $(item).find('div').css('background-image', 'url(/Content/images/checkbox_' + status + '.jpg)');
                $(item).find('input').val(value);
                $(item).data('value', value);
            }
        }
    });
})(jQuery);

(function ($) {
    $.fn.extend({
        numericOnly: function() {
            $(this).keypress(function(e) {
                if ($(this).data('selected')) {
                    $(this).val('');
                    $(this).removeData('selected');
                }
                var allow = false;
                var code = (e.keyCode ? e.keyCode : e.which);
                if ((code >= 48 && code <= 57)) allow = true;
                if (code == 8 || code == 9 || code == 13 || code == 46 || code == 37 || code == 39) allow = true;

                if (!allow) {
                    e.preventDefault();
                    e.stopPropagation();
                    return;
                }
            });
        }
    });
})(jQuery);

$("a.deleteRow").live("click", function () {
    var id = $(this).siblings("input:hidden").val();

    $.ajax({
        url: '/CreateLeague/DeleteOwner/' + id,
        cache: false,
        type: "POST"
    });
    $(this).parents("tr.editorRow").remove();
    return false;
});

function checkHeight(container, height) {
    if ($(container).height() > height) {
        $(container).css('height', height);
        $(container).css('overflow', 'auto');
    }
}

function showError(errorContainer, message, show) {
    if (show)
        $(errorContainer).html(message);
    else
        $(errorContainer).html('');
}

function formValid(form) {
    $(form).find('.red').each(function () {
        if ($(this).html().length > 0) {
            return false;
        }
    });
    return true;
}

function loginUser() {
    var f = $("#login-form");
    $.post(f.attr("action"), f.serialize(), function (data) {
        if (data.result) {
            window.location.href = data.url
        }
        else {
            $('#loginErrors').html(data.error);
        }
    });
}

function removeAlert(alertId) {
    
    //ajax call
    $.post('/Member/RemoveAlert/', {alertId:alertId}, function(data){
        $("#memberAlert_" + alertId).hide(200);
    });


}
