// JavaScript Document

(function($){ //Using $ as alias can cause plugin to conflict with other libraries...Throwing in jquery at the bottom fixes this
	$.fn.extend({
		
		isValid: function(options) {
			return valid;
		},
		
		validate: function(options) {
			debug(this);
			
			var defaults = {};
			var o = $.extend({}, options); //Extend allows us to include default settings for the plugin
			var errors = new Array();
			valid = true;
			
			this.each(function() { //////////Validate each input and push error messages to array
				var obj = $(this);
				validate(obj);
			});
			
			if (!valid)
				$("#message").message({ /////////Alert user of errors
					messages: errors, 
					status: "wrong" 
				});
				
			return this;
	
			function wrong(obj) {
				obj.removeClass("wrong");
				obj.css("border","1px solid red");
				obj.addClass("wrong");
				valid = false;
			}
			
			function correct(obj) {
				obj.css("border", "1px inset #AAA");
				obj.removeClass("wrong");
			}
			
			function debug(obj) {
			  if (window.console && window.console.log)
				window.console.log('hilight selection count: ' + obj.size());
			}		
	
			function validate(o) { //Attemped to use label's name for message appending, but IE wouldn't recognize????
				var theInput = jQuery.trim(o.val());
				
				switch(o.attr("name")) {
					/*case "address":
							if (theInput.length < 5) {
								wrong(o);
								errors.push("Address 1 is required and must be greater than 5 characters");						
							}
							else {
								correct(o);
							}
						break;*/
					/*				
					case "city":
							if (theInput.length < 3 || !isNaN(theInput)) {
								wrong(o);
								errors.push("City is required and must be greater than 3 characters");						
							}
							else {
								correct(o);
							}					
						break;*/
					case "comment":
						if (theInput.length <= 5) {
							wrong(o);
							errors.push("Message must contain more than 5 characters");
						}
						else {
							correct(o);					
						}
						break;							
					/*case "country":
							if (theInput.length < 3 || !isNaN(theInput)) {
								wrong(o);
								errors.push("Country is required and must be greater than 3 characters");							
							}
							else {
								correct(o);
							}						
						break;*/
					case "email":
						if (!theInput.match(/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+$/)) {
							wrong(o);
							errors.push("Invalid email address");
						}
						else {
							correct(o);				
						}
						break;
					case "name":
							if (theInput.length < 3 || !isNaN(theInput)) {
								wrong(o);
								errors.push("Name must be greater than 3 characters");
							}
							else {
								correct(o);	
							}						
						break;						
					/*
					case "phone":
							if (theInput != "") {
								if (!theInput.match(/^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})$/)) {
									wrong(o);
									errors.push("Invalid phone number");											
								}
								else {
									correct(o);
								}
							}
							else {
								correct(o);
							}
						break;
					case "subject":
						if (theInput.length <= 3) {
							wrong(o);
							errors.push("Subject must contain more than 3 characters");						
						}
						else {
							correct(o);					
						}
						break;*/			
					default:
				}
			}
		}
	});
})(jQuery);