(function(){
	jQuery.Wci = (function(){
		return{}		
	})();	//define the Wci namespace.
	
	jQuery.Wci.Validation = (function(){ //define the Wci.Valiation object.
		var controlsToValidate = new Array();
		var highlightColor = '#FF3328';
		
		function highlightError(control){
		    jQuery(control).css('border-color', highlightColor);
		    jQuery(control).children("option:selected").each(function(){
			jQuery(this).css('background-color', highlightColor);
		    });
		};
		
		function removeErrorHighlight(control){
		    jQuery(control).css('border-color', '');
		    jQuery(control).children().css('background-color', '');
		};
		
		return{
			//a regex to validate Candian and US zip codes.
			zipCodeRegex: /(^\d{5}(-\d{4})?$)|(^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$)/,
			emailRegex: /.+@.+\..+/,
			requiredFieldRegex: /.+/,
			//validates a US currency with optional dollar sign (ex. $4.98)
			moneyFieldRegex: /^\$?\d{1,3}(,\d{3})*(\.\d{2})?$/,
			
			getHighlightColor: function(){
				return highlightColor;
			},
			
			setHighlightColor: function(value){
				highlightColor = value;
			},
			
			addControlToValidationListWithCallback: function(control, validationCallback){
				controlsToValidate[control] = validationCallback;
				
				if(jQuery(control).attr('type') == "text" || jQuery(control).attr('type') == "password"){
					jQuery(control).change(function(){
						if(validationCallback()){
							removeErrorHighlight(control);
						}
					});
				}else{
					jQuery(control).blur(function(){
						if(validationCallback()){
							removeErrorHighlight(control);
						}
					});
				}
			},
			
			addControlToValidationList: function(control, validationRegex){
				this.addControlToValidationListWithCallback(control, function(){
					return jQuery(control).val().match(validationRegex);
				});
			},
			
			removeControlFromValidationList: function(control){
				controlsToValidate.splice(control, 1);	//remove the control from the validation list.
				
				if(jQuery(control).attr('type') == "text" || jQuery(control).attr('type') == "password"){
					jQuery(control).change(function(){});
				}else{
					jQuery(control).blur(function(){});
				}
			},
			
			validateControls: function(highlightErrors){
				var result = true;
				for(var control in controlsToValidate){
					//call the validation callback to determine control validation.
					var valid = controlsToValidate[control]();
					if(!valid){
						result = false;
						if(highlightErrors){
							highlightError(control);
						}	
					}
				}
				
				return result;
			}
		}		
	})();//end Wci.Validation
})();
