

jQuery.iFormValidate = {
	build : function(user_options)
	{
		var defaults = {
			ajax: true,
			validCheck: false,
			phpFile:"/js/formvalidator/send.php"
		};
		return $(this).each(
			function() {
			var options = $.extend(defaults, user_options); 
			if(options.validCheck){
				$inputs = $(this).find(":input").filter(":not(:submit)").filter(":not(:checkbox)").filter(":not(.novalid)");
			}else{
				$inputs = $(this).find(":input").filter(":not(:submit)").filter(":not(:checkbox)");
			}
			//catch the submit
			$(this).submit(function(){
				//we need to do a seperate analysis for checboxes
				$checkboxes = $(this).find(":checkbox");
				//we test all our inputs
				var isValid = jQuery.iFormValidate.validateForm($inputs);
                var notChecked = false;
				//if any of them come back false we quit
                if(this.id == "claimForm"){
                    if(!this.elements.agree.checked){
                        isValid = false;
                        notChecked = true;
                    }
                }
				if(!isValid){
                    myMessage = "Fields outlined in red are empty or invalid.\n";
                    if(notChecked){
                        myMessage += "Pease certify this form by checking the box.";
                    }
                    alert(myMessage);
					return false;
				}
				if(options.ajax){
					var data = {};
					$inputs.each(function(){
						data[this.name] = this.value;
					});
					$checkboxes.each(function(){
						if($(this).is(':checked')){
							data[this.name] = this.value;
						}else{
							data[this.name] = "";
						}
					});	
					$(this).parent('div').fadeOut("slow", function(){
						$(this).load(options.phpFile, data, function(){
							$(this).fadeIn("slow");
						});
					});
					return false;
				}else{
					return true;
				}
			});
			
			$inputs.bind("keyup", jQuery.iFormValidate.validate);
            $("#date").bind("change", jQuery.iFormValidate.validate);
            $("#claimdate").bind("change", jQuery.iFormValidate.validate);
            $("#towdate").bind("change", jQuery.iFormValidate.validate);
            $("#email").bind("change", jQuery.iFormValidate.validate);
            $("#confirmEmail").bind("change", jQuery.iFormValidate.validate);
			$inputs.filter("select").bind("change", jQuery.iFormValidate.validate);
		});
	},
	validateForm : function($inputs)
	{
		var isValid = true; //benifit of the doubt?
		$inputs.filter(".is_required").each(jQuery.iFormValidate.validate);
		if($inputs.filter(".is_required").hasClass("invalid")){isValid=false;}
		return isValid;
	},
		
	validate : function(){
		var $val = $(this).val();
		var isValid = true;
        //do matching
        var matches = new Array();        
        if($(this).hasClass('vdate')){
			var Regex = /^([\d]|1[0,1,2]|0[1-9])(\-|\/|\.)([0-9]|[0,1,2][0-9]|3[0,1])(\-|\/|\.)\d{2}$/;
			isValid = Regex.test($val);
            if(!Regex.test($val)){isValid = false;} 
		//Regex for Email
		}else if($(this).hasClass('vemail')){
			var Regex =/^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
			if(!Regex.test($val)){isValid = false;}		
		//Regex for Phone
		}else if($(this).hasClass('vphone')){
			var Regex = /^(?:\([2-9]\d{2}\)\ ?|[2-9]\d{2}(?:\-?|\ ?))[2-9]\d{2}[- ]?\d{4}$/;
			if(!Regex.test($val)){isValid = false;}        
        
        /*
		}else if($(this).hasClass('vphone')){
			var Regex = /^\(?[2-9]\d{2}[ \-\)] ?\d{3}[\- ]?\d{4}$/;
			if(!Regex.test($val)){isValid = false;}
        */	
		//Check for U.S. 5 digit zip code
		}else if($(this).hasClass('vzip')){
			var Regex = /^\d{5}/;
			if(!Regex.test($val)){isValid = false;}
		//Check for state
		}else if($(this).hasClass('vstate')){
			var Regex = /^[a-zA-Z]{2}$/;
			if(!Regex.test($val)){isValid = false;}
		//Check for name	
		}else if($(this).hasClass('vname')){
			var Regex = /[a-zA-Z']/;
			if(!Regex.test($val)|| $val.indexOf("Customer")>-1){isValid = false;}
		//Check for not empty empty
		}else if($(this).hasClass('required')){
			var Regex = /[0-9a-zA-Z]+/;
			if(!Regex.test($val)){isValid = false;}          
		}else {
			//return false;
		}
		
		if(isValid){
			$(this).removeClass("invalid");
			$(this).addClass("valid");
		}else{
			$(this).removeClass("valid");
			$(this).addClass("invalid");
		}
	
        return isValid;
	}	
}
jQuery.fn.FormValidate = jQuery.iFormValidate.build;
