/*
 *  UPDATED: 06.11.09
 *	VERSION: 1.7
 *
 *  This class is used for unabotrusive form field validation
 *  every input to be validated must start with a class name of validate followed by "|" then whether it is required then "|" validation type
 *  VALIDATION TYPES: 
 *		date  									Matches: [1/1/2006], [1-1-2006], [1.1.2006]
 *		zipcode (5 or 9 digit)					Matches: [12345], [12345-6789], [123456789]
 *		numeric									Matches: [5,000], [-5,000], [100.044]
 *		email									Matches: any valid email
 *		phone (10 digit)						Matches: (800)-123-1234, 800.123.1234, 800-123-1234
 *		notfirst (for selects)					Matches: a select list with a selectedIndex > 1
 *		oneof (for groups of radio or checkbox)	Matches: a group of radio or checkbox with none checked
 *		checked									Matches: a checkbox or radio that is checked
 *		length|N (N=any number)					Matches: a lengthe less than the given number
 *
 * 	OPTIONS:
 *		errBox - id of the location for the errors, if not given then the errors are shown in an ALERT
 *		className - the CSS class that will be applied to the errBox
 *		overlay - the path to the image to display on the overlay **requires the dialog.js library**
 *		labelColor - the font color of the label for the field that has an error
 *		inputColor - the background color for the field that has an error
 *		custom - a custom function that is called when the form is submitted before it checks the required fields
 *  
 *  EXAMPLE: 
 *  var myForm = new WSC.Validator('form1', {labelColor : '#FF0000', inputColor : '#FEC7C7', errBox: 'msgBox', overlay : 'path/to/image'});
 *  
 *  <input type="text" name="var1" class='validate|yes|date' value="12/12/2006" size="12" maxlength="10" />
 * ------------------------------------------------------------------------------------------------ */
if(!window.WSC){var WSC={}}(function($){WSC.Validator=function(h){var j=$.extend({errBox:null,overlay:null,className:'error',labelColor:'#FF0000',inputColor:'#FEC7C7',custom:null},arguments[1]||{});var k=null;var l=[];var m=[];var n=this;var o=[];var p=[];var q=function(){var g=$('#'+j.errBox);r(g);if(typeof tinyMCE!='undefined'){tinyMCE.triggerSave()}if(j.custom){j.custom()}$('[class*="validate"]',k).each(function(){var a=$(this);a.css({backgroundColor:''});var b=a.attr('class').match(/validate[\S]*/)[0].split('|');var c=(b[1]=='yes')?true:false;var d=b[2];var e=b[3];var f=(c||this.value!=='')?v(d).test(a,e):false;if(f){w(a,a.attr('title'))}});if(n.errors.length>0){if(g){scroll(0,k.offset().top);g.addClass(j.className).html(n.errors.join('<br>'))}else{alert(n.errors.join('\n'))}return false}else{if(j.overlay&&typeof WSC.Dialog!="undefined"){WSC.Dialog(j.overlay,{type:'image'})}return true}};var r=function(a){if(a.length>0){a.removeClass(j.className).html()}n.errors=[];s();t()};var s=function(){for(var i=0;i<o.length;i++){$(o[i]).css({color:''})}};var t=function(){for(var i=0;i<p.length;i++){$(p[i]).css({backgroundColor:''})}};var u=function(a,b){l[a]=b};var v=function(b){return{test:(l[b]?l[b]:function(a){return a.val()===''})}};var w=function(a,b){if(a.find(':radio, :checkbox').length>0){a=a.find(':radio, :checkbox')}x(a);y(a);n.errors.push(b)};var x=function(a){var b=a.parent('label');if(b.length===0){b=$('label[for="'+a.attr('name')+'"]')}if(b.length>0){b.css({color:j.labelColor});o.push(b)}return};var y=function(a){a.css({backgroundColor:j.inputColor});p.push(a)};u('phone',function(a){var b=/^(\(?\d\d\d\)?)?( |-|\.)?\d\d\d( |-|\.)?\d{4,4}(( |-|\.)?[ext\.]+ ?\d+)?$/;return!b.test(a.val())});u('date',function(a){var b=/^([0]?[1-9]|[1][0-2])[.\/-]([0]?[1-9]|[1|2][0-9]|[3][0|1])[.\/-]([0-9]{4}|[0-9]{2})$/;return!b.test(a.val())});u('zipcode',function(a){var b=/^\d{5}((-|\s)?\d{4})?$/;return!b.test(a.val())});u('numeric',function(a){var b=/^(\d|-)?(\d|,)*\.?\d*$/;return!b.test(a.val())});u('email',function(a){var b=/^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/;return!b.test(a.val())});u('notfirst',function(a){return(a[0].selectedIndex===0)?true:false});u('checked',function(a){return(!a[0].checked)});u('minlength',function(a,b){return(a.val().length<b)});u('maxlength',function(a,b){return(a.val().length>b)});u('min',function(a,b){return(a.val()<parseFloat(b))});u('max',function(a,b){return(a.val()>parseFloat(b))});u('is',function(a,b){return(a.val()!==b)});u('isnot',function(a,b){return(a.val()==b)});u('alphanum',function(a){var b=/\W/;return b.test(a.val())});u('alpha',function(a){var b=/^[a-zA-Z]+$/;return!b.test(a.val())});u('url',function(a){var b=/^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\/?/i;return!b.test(a.val())});u('oneof',function(a){return(a.find(':checked').length===0)});$(function(){if(j.overlay){var a=new Image();a.src=j.overlay}k=$('#'+h).submit(q)});return{errors:m,addError:w,addRule:u}}})(jQuery);