/*
 * Copyright (c) 2010 Ed Rodriguez
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 * 
 * Version 1.0
 */
 
/*
 * Allows only valid characters to be entered into input boxes.
 * Based off of jquery.numeric.js by Sam Collett
 *
 * @name     valid
 * @param    valid      A string of valid characters
 * @param    callback   A function that runs if the input is not valid (fires onblur)
 * @author   Ed Rodriguez
 * @example  $(".numeric").valid();
 * @example  $(".numeric").numeric(callback);
 */
$(function(){
	if($.fn.valid){
		$('[data-valid]').valid();
	}
});
jQuery.fn.valid = function(valid, callback){
    valid = typeof valid === 'string' ? valid : '';
	callback = typeof callback === 'function' ? callback : function(){};
	this.keypress(
        function(e){
            valid = (typeof $(this).attr('data-valid') !== 'undefined') ? $.base64Decode($(this).attr('data-valid')) : valid;
            if(valid !== ''){
                var allow = false,
                key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0,
                c = String.fromCharCode(key);
                
                // allow enter/return key (only when in an input box)
                if(key == 13 && this.nodeName.toLowerCase() == 'input'){
                    return true;
                }else if(key == 13){
                    return false;
                }
                
                // allow Ctrl+A
                if((e.ctrlKey && key == 97 /* firefox */) || (e.ctrlKey && key == 65) /* opera */) return true;
                // allow Ctrl+X (cut)
                if((e.ctrlKey && key == 120 /* firefox */) || (e.ctrlKey && key == 88) /* opera */) return true;
                // allow Ctrl+C (copy)
                if((e.ctrlKey && key == 99 /* firefox */) || (e.ctrlKey && key == 67) /* opera */) return true;
                // allow Ctrl+Z (undo)
                if((e.ctrlKey && key == 122 /* firefox */) || (e.ctrlKey && key == 90) /* opera */) return true;
                // allow or deny Ctrl+V (paste), Shift+Ins
                if((e.ctrlKey && key == 118 /* firefox */) || (e.ctrlKey && key == 86) /* opera */
                || (e.shiftKey && key == 45)) return true;
                
                if(valid.indexOf(c) >= 0){
                    allow = true;
                }else{
                    if(
                        key != 8 /* backspace */ &&
                        key != 9 /* tab */ &&
                        key != 13 /* enter */ &&
                        key != 35 /* end */ &&
                        key != 36 /* home */ &&
                        key != 37 /* left */ &&
                        key != 39 /* right */ &&
                        key != 46 /* del */
                    ){
                       allow = false; 
                    }else{
                        // for detecting special keys (listed above)
                        // IE does not support 'charCode' and ignores them in keypress anyway
                        if(typeof e.charCode != 'undefined'){
                            // special keys have 'keyCode' and 'which' the same (e.g. backspace)
                            if(e.keyCode == e.which && e.which != 0){
                                allow = true;
                            }
                            // or keyCode != 0 and 'charCode'/'which' = 0
                            else if(e.keyCode != 0 && e.charCode == 0 && e.which == 0){
                                allow = true;
                            }
                        }
                    }
                }
                return allow;
            }else{
                return true;
            }
    }).blur(
		function(){
		}
	);
	return this;
}
