/*
 * momVal - dynamic jquery form field validation for Mind Over Machines
 * version 0.1
 * Author: Shea Frederick
 * Released under MIT license.
 * Based on Code: jVal by Jim Palmer
 */
(function($) {
	function showWarning (selectElement, message, autoHide, styleType, focusFld, msgLoc) {
		$(selectElement).each(function(ind) {
				var curFld = $(this);
				var fldName = this.attributes.name.nodeValue;
				var errorBoxName = fldName+'_errorMsg';
				if (msgLoc){
					errorBoxName = msgLoc+'_errorMsg';
				}
				var curMsg = $('#'+errorBoxName);
				var curWidth = curFld.width();
				var curHeight = curFld.height();
				var errMsgLeft = curWidth+140;
				curFld.css({'borderColor':'red', 'background-color':'#ffe6e6'});
				if (curMsg.length == 0) {
					if (msgLoc) {
						$('#'+msgLoc).after('<div class="errormsg" id="' + errorBoxName + '" style="display:none;">' + message + '</div><div style="float: left;"><img src="images/bubble-cap.png"></div>');
					}else{
						curFld.after('<div class="errormsg" id="' + errorBoxName + '" style="display:none;">' + message + '</div><div style="float: left;"><img src="images/bubble-cap.png"></div>');
					}
					curMsg = $('#'+errorBoxName);
					$(curMsg).css({
						'display': ''
					}).animate({
						'opacity': 1
					}, 400);
				}else{
					$('#'+errorBoxName).text(message);
				}
				if (focusFld) {
					$(this).css('opacity', 0).animate({
						'opacity': 1
					}, 400);
					curMsg = $('#'+errorBoxName);
					curMsgCap = $('#'+errorBoxName).next();
					$(curMsg).css({
						'opacity': 0
					}).animate({
						'opacity': 1
					}, 400);
					$(curMsgCap).css({
						'opacity': 0
					}).animate({
						'opacity': 1
					}, 400);
				}
			});
	};
	function valKey (keyRE, e, cF, cA) {
		if ( !(keyRE instanceof RegExp) ) return false;
		if ( /^13$/.test(String(e.keyCode || e.charCode)) ) {
			try { (this[cF]) ? this[cF](cA) : eval(cF); } catch(e) { return true; }
			return -1;
		} else if (	( typeof(e.keyCode) != 'undefined' && e.keyCode > 0 && keyRE.test(String.fromCharCode(e.keyCode)) ) || 
					( typeof(e.charCode) != 'undefined' && e.charCode > 0 && String.fromCharCode(e.charCode).search(keyRE) != (-1) ) ||
					( typeof(e.charCode) != 'undefined' && e.charCode != e.keyCode && typeof(e.keyCode) != 'undefined' && e.keyCode.toString().search(/^(8|9|45|46|35|36|37|39)$/) != (-1) ) ||
					( typeof(e.charCode) != 'undefined' && e.charCode == e.keyCode && typeof(e.keyCode) != 'undefined' && e.keyCode.toString().search(/^(8|9)$/) != (-1) ) ) {
			return 1;
		} else {
			return 0;
		}
	};
	$.fn.momVal = function (focusFld) {
		var passVal = true;
		$(this).css({
					'borderColor': '',
					'background-color':'#fff'
				}).each( function () {
				try {
					eval( 'var cmd = ' + $(this).attr('momVal') + ';' );
					var fldName = this.attributes.name.nodeValue;
					var errorBoxName = fldName+'_errorMsg';
					if (cmd.msgLoc){
						errorBoxName = cmd.msgLoc+'_errorMsg';
					}
					var curMsg = $('#'+errorBoxName);
					curMsg.next().remove();
					curMsg.remove();
					if ( cmd instanceof Object && ( cmd.valid instanceof RegExp && !cmd.valid.test($(this).val()) ) || ( cmd.valid instanceof Function && !cmd.valid($(this).val()) ) ) {
						showWarning(this, cmd.message || $.fn.momVal.defaultMessage, cmd.autoHide || false, cmd.styleType || 'pod', focusFld || false, cmd.msgLoc || false);
						passVal = false;
					} else if ( ( cmd instanceof RegExp && !cmd.test($(this).val()) ) || ( cmd instanceof Function && !cmd($(this).val()) ) ) {
						showWarning(this, $.fn.momVal.defaultMessage, false, 'pod', focusFld, cmd.msgLoc || false);
						passVal = false;
					}
				} catch(e) { return true; }
			});
		return passVal;
	};
	$(document).ready( function () {
			$('input[momVal]').bind("keyup", function (e) {
					$(this).momVal(false);
				});
			$('input[momVal]').bind("blur", function (e) {
					$(this).momVal(true);
				});
			$('input[momVal]').bind("focus", function (e) {
					$(this).momVal(false);
				});
			$('input[momValKey]').bind("keypress", function (e) {
					try {
						eval( 'var cmd = ' + $(this).attr('momValKey') + ';' );
						var keyTest = valKey( ( (cmd instanceof Object) ? cmd.valid : cmd ), e, (cmd instanceof Object) ? cmd.cFunc : null, (cmd instanceof Object) ? cmd.cArgs : null );
						if ( keyTest == 0 ) {
							showWarning(this, (( cmd instanceof Object && cmd.message) || $.fn.jVal.defaultKeyMessage).replace('%c', String.fromCharCode(e.keyCode || e.charCode)), true, cmd.styleType || 'pod');
							return false;
						} else if ( keyTest == -1 ) return false;
						else $(this).css({
							'borderColor': '',
							'background-color':'#fff'
						});
						return true;
					} catch(e) { return true; }
				});
		});
	$.fn.momVal.defaultMessage = 'Invalid entry';
	$.fn.momVal.defaultKeyMessage = '"%c" Invalid character';
	$.fn.momVal.defaultPadding = 3;
	$.fn.momVal.defaultBorderWidth = 1;
	$.fn.momVal.IETopNudge = $.browser.msie ? -1 : 0;
})(jQuery);
