﻿var DefaultScript = (function () {
    var def = {};
    def.ShowIndicator = function (text, id) {
        var pageElement = arguments.length == 1 ? $('body') : (typeof id == "object") ? id : $('#' + id);
        pageElement.block({ message: "<h2>" + text + "</h2>" });

    };

    def.HideIndicator = function (id) {
        var pageElement = arguments.length == 0 ? $('body') : (typeof id == "object") ? id : $('#' + id);
        pageElement.unblock();
    };

    def.PNotify = function (title, text, types) {
        //$.jGrowl(text, { life: 2000 });
        if (parseInt($('#divAlert').length) > 0) {
            $('#divAlert').html(text);
        }
        else {
            $("body").append('<div  style="display:none;"><div id="divAlert">' + text + '</div></div>');
        }
        $.colorbox({ inline: true, href: '#divAlert' });
    };

    def.checkEmail = function (txt) {

        var email = jQuery('#' + txt).val();
        if (email == "") return true;
        var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
        if (!filter.test(email)) {
            jQuery('#' + txt).focus();
            return false;
        }
        return true;
    }

    def.reloadWidget = function (divId, url, data) {
        if (jQuery('#' + divId).length == 0) return;
        def.ShowIndicator('yükleniyor', divId);
        jQuery.ajax({
            type: 'post',
            data: data,
            url: url,
            success: function (data) {

                if (jQuery('#' + divId).length > 0)
                    jQuery('#' + divId).html(data);
                def.HideIndicator(divId);
            },
            error: function () { }
        });
    }

    def.htmlEncode = function (value) {
        return $('<div/>').text(value).html();
    };

    def.htmlDecode = function (value) {
        return $('<div/>').html(value).text();
    };

    def.IsSpace = function (evt) {
        var charCode = (evt.which) ? evt.which : event.keyCode
      
        if (charCode == 32)
            return false;

        return true;
    };

    def.IsNumeric = function (evt) {
        var charCode = (evt.which) ? evt.which : event.keyCode
        if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;

        return true;
    };
    def.IsDecimal = function (evt) {
        var charCode = (evt.which) ? evt.which : event.keyCode
        if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 44)
            return false;

        if ((evt.target.value.indexOf(',') > 0) && (charCode == 44))
            return false;

        return true;
    };

    def.IsEmpty = function (StrToCheck) {
        StrToCheck = StrToCheck.replace(/^\s+|\s+$/, '');
        if (StrToCheck.length == 0)
            return true;
        else
            return false;
    };

    return {
        ShowIndicator: def.ShowIndicator,
        HideIndicator: def.HideIndicator,
        PNotify: def.PNotify,
        CheckEmail: def.checkEmail,
        ReloadWidget: def.reloadWidget,
        htmlEncode: def.htmlEncode,
        htmlDecode: def.htmlDecode,
        IsNumeric: def.IsNumeric,
        IsDecimal: def.IsDecimal,
        IsEmpty: def.IsEmpty,
        IsSpace : def.IsSpace
    }
}
());
