/*
 *  UPDATED: 02.07.08
 *	VERSION: 1.5
 *
 *  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
 *		checked					Matches: a checkbox or radio that is checked
 *		lengthN (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(A){WSC.Validator=function(D){var C=A.extend({errBox:null,overlay:null,className:"error",labelColor:"#FF0000",inputColor:"#FEC7C7",custom:null},arguments[1]||{});this.errors=[];var B=this;var I=[];var G=function(){var J=A("#"+C.errBox);if(J.length>0){J.removeClass(C.className).html()}B.errors=[];A(I).css({color:""});if(typeof tinyMCE!="undefined"){tinyMCE.triggerSave()}if(C.custom){C.custom()}A('[class*="validate"]').each(function(){var M=A(this);M.css({backgroundColor:""});var O=M.attr("class").match(/validate[\S]*/)[0].split("|");var N=(O[1]=="yes")?true:false;var K=O[2];var L=(N||this.value!=="")?H(M,K):false;if(L){F(M);E(M);B.errors.push(M.attr("title"))}});if(B.errors.length>0){if(J){scroll(0,0);J.addClass(C.className).html(B.errors.join("<br>"))}else{alert(B.errors.join("\n"))}return false}else{if(C.overlay&&typeof WSC.Dialog!="undefined"){WSC.Dialog(C.overlay,{type:"image"})}return true}};var H=function(N,J){var L;var M=N[0].value;if(!J||J===""){return(M==="")}J=J.toLowerCase();if(/^length/.test(J)){return(M.length<parseInt(J.replace("length",""),10))}var K;switch(J){case"date":K=/^([0]?[1-9]|[1][0-2])[.\/-]([0]?[1-9]|[1|2][0-9]|[3][0|1])[.\/-]([0-9]{4}|[0-9]{2})$/;L=!K.test(M);break;case"zipcode":K=/^\d{5}((-|\s)?\d{4})?$/;L=(!K.test(M));break;case"numeric":K=/^(\d|-)?(\d|,)*\.?\d*$/;L=(!K.test(M));break;case"email":K=/^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/;L=(!K.test(M));break;case"phone":K=/^(\(?\d\d\d\)?)?( |-|\.)?\d\d\d( |-|\.)?\d{4,4}(( |-|\.)?[ext\.]+ ?\d+)?$/;L=(!K.test(M));break;case"notfirst":L=(N[0].selectedIndex===0)?true:false;break;case"checked":L=(!N[0].checked);break;default:L=(M==="");break}return L};var F=function(K){var J=K.parent("label");if(J.length===0){J=A('label[for="'+K.attr("name")+'"]')}if(J.lenght>0){J.css({color:C.labelColor});I.push(J)}return};var E=function(J){J.css({backgroundColor:C.inputColor})};A(function(){if(C.overlay){var J=new Image();J.src=C.overlay}A("#"+D).submit(G)})}})(jQuery);