﻿// JScript File

(function(){
    //Prevent cut copy paste
    $.fn.PreCutCopyPaste = function(){
        $(this).bind('cut copy paste',function(e){
            e.preventDefault();
        });
    };
    
    //Only alpha numeric character
    $.fn.AlphaNum = function(){
        $(this).bind('keyup blur',function(){
            if(this.value.match(/[^a-zA-Z0-9 ]/g)){
                this.value = this.value.replace(/[^a-zA-Z0-9 ]/g, '');
            }
        });
    };
    
    //character limit
    $.fn.CharLimit = function(options){
        options = $.extend({
            limit: 50
        },options);
        
        $(this).after('<span class="charLeft"><strong>'+ options.limit +'</strong> characters left</span>');
        
        $(this).bind('keyup click blur focus change paste',function(){
            var len = $(this).val().length;
            if(len > options.limit){
                this.value = this.value.substring(0,options.limit);
            }
            $(this).siblings('.charLeft').children('strong').text(options.limit-len);
        });
    };
    
    //word limit
    $.fn.WordLimit = function(options){
        options = $.extend({
            minWord: 2,
            maxWord: 30
        },options);
        
        $(this).after('<span class="wordLeft"><strong>'+ options.maxWord +'</strong> words left</span>');
        $(this).bind('keyup click blur focus change paste',function(){
            var numWords = jQuery.trim($(this).val()).split(' ').length;
            if($(this).val() === '')
            {
                numWords = 0;
            }
            $(this).siblings('.wordLeft').children('strong').text(options.maxWord - numWords);
            if(numWords > options.maxWord){
                //alert(numWords + " ||| " + options.maxWords);
                var valArray = jQuery.trim(this.value).split(' ');
                var finalVal = '';
                for(var len=0; len < options.maxWord; len++)
                {
                    finalVal += valArray[len] + ' ';
                }
                $(this).val(finalVal);
                $(this).siblings('.wordLeft').children('strong').text(0);
            }
        });
    };
    
    //only numeric value
    $.fn.Numeric = function(){
        $(this).bind('keyup blur',function(){
            if(this.value.match(/[^0-9]/g)){
                this.value = this.value.replace(/[^0-9]/g, '');
            }
        });
    };
    
    //not null value
    $.fn.NotNull = function(options){
        options = $.extend({
            errorMessage: 'please enter value'
        },options);
        
        $(this).bind('blur',function(){
            if($(this).val() == '' || $(this).val() == null || $(this).val() == ' '){
                alert(options.errorMessage);
                $(this).focus();
                return false;
            }
        });
    };
    
    //not null value
    $.fn.NotNullVal = function(options){
        options = $.extend({
            errorMessage: 'please enter value'
        },options);
        
        if($(this).val() == '' || $(this).val() == null || $(this).val() == ' '){
            alert(options.errorMessage);
            $(this).focus();
            return false;
        }
        else {
            return true;
        }
    };
    
    //check email id
    $.fn.CheckEmail = function(){
        $(this).bind('blur',function(){
            regexp = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i;
            if(!(regexp.test(this.value))){
                alert("please enter correct email");
                return false;
            }
        });
    };
    
})(jQuery);
