﻿var JS = {};
JS.Common = {
    //
    GetCharKeyCode: function (event, obj) {
        var characterCode;
        // NN4 specific code
        if (event && event.which) { characterCode = event.which; }
        // IE specific code
        else { characterCode = event.keyCode; }
        return characterCode;
    },

    Trim: function (str) {
        return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
    },

    IsEmailValid: function (email) {
        var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
        return reg.test(email);
    },

    AllowInputChars: function (obj, event, regex) {
        var input = this.GetCharKeyCode(event, obj);

        // allow these
        if(input < 47) { return; }

        var char = String.fromCharCode(input);
        var re = new RegExp(regex);
        if (!regex.test(char)) { event.preventDefault(); }
    },

    BlockUI: function(id, message, durationMilliseconds) {
        $('#'+ id).click(function() {         
		    $.blockUI({ 
			    css: 
			    { 
				    border: 'none', 
				    padding: '15px', 
				    backgroundColor: '#000',
				    '-webkit-border-radius': '10px', 
				    '-moz-border-radius': '10px', 
				    opacity: .9, 
				    color: '#fff',
				    font: '14px arial',
				    fontWeight: 'bold'				
			    },
			    message: '<div style="align:center;"><img src="/static/img/status-bar.gif" /><br /> '+ message +'</div>',
		    });
		    setTimeout($.unblockUI, durationMilliseconds);
	    }); 
    },

    RestrictInput: {
        CardNumber: function (elementId) {
            $('#' + elementId).keypress(function (event) {
                var regex = /[0-9]/;
                JS.Common.AllowInputChars(this, event, regex);
            });
        },
        CardVerification: function (elemntId) {
            $('#' + elemntId).keypress(function (event) {
                var regex = /[0-9]/;
                JS.Common.AllowInputChars(this, event, regex);
            });
        },
        DollarAmount: function (elemntId) {
            var $elem = $('#' + elemntId);
            $elem.keypress(function (event) {
                var regex = /[0-9]|\./;
                // only one "."
                var key = JS.Common.GetCharKeyCode(event, $elem[0]);
                var index = $elem.val().indexOf('.');
                if (key == 46 && index > -1) {
                    regex = /[0-9]/;
                }
                JS.Common.AllowInputChars(this, event, regex);
            });
        },
        Generic: function (elementId, regex) {
            $('#' + elemntId).keypress(function (event) {
                JS.Common.AllowInputChars(this, event, regex);
            });
        }
    }



};


