﻿/*     
	2/4/09
	Form Validator
	Jquery plugin for form validation and quick contact forms
	Copyright (C) 2009 Jeremy Fry. www.jeremy-fry.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

jQuery.iFormValidate = {
	build : function(user_options)
	{
		var defaults = 
		{
			ajax: true
		};
		return jQuery(this).each(function() 
		{
			var options = jQuery.extend(defaults, user_options); 
			var inputs = jQuery(this).find(":input").filter(":not(:submit)").filter(":not(:checkbox)").filter(":not([validate=false])");
			//catch the submit
			jQuery(this).submit(function(){
				//we need to do a seperate analysis for checboxes
				var checkboxes = jQuery(this).find(":checkbox");
				var submitFile = jQuery(this).attr('action');
				//we test all our inputs
				var isValid = jQuery.iFormValidate.validateForm(inputs,options);
				//if any of them come back false we quit
				if(!isValid){
					return false;
				}
				if(options.ajax){
					var data = {};
					inputs.each(function(){
						data[this.name] = this.value;
					});
					checkboxes.each(function(){
						if(jQuery(this).is(':checked')){
							data[this.name] = this.value;
						}else{
							data[this.name] = "";
						}
					});
						
					jQuery(this).parent('div').fadeOut("slow", function(){
						jQuery(this).load(submitFile, data, function(){
							if(typeof(options.onSuccess) == 'function')
							{
								options.onSuccess();
							}else{
								jQuery(this).fadeIn("slow");
							}
						});
					});
					return false;
				}else{
					return true;
				}
			});
			
			inputs.bind("keyup",{options: options}, jQuery.iFormValidate.validate);
			inputs.filter("select").bind("change", {options: options}, jQuery.iFormValidate.validate);
		});
	},
	validateForm : function(inputs,options)
	{
		var isValid = true; //benifit of the doubt?
		inputs.filter("[required=true]").trigger("keyup",{options: options}, jQuery.iFormValidate.validate);
		inputs.filter("[required=true]").filter("select").trigger("change", {options: options}, jQuery.iFormValidate.validate);
		if(inputs.filter("[required=true]").hasClass("invalid")){isValid=false;}
		return isValid;
	},
		
	validate : function(e,how){
		options = e.data.options;
		var val = jQuery(this).val();
		var isValid = true;
		var type = jQuery(this).attr('validate');
		var Regex;
		//Regex for Street
		if(type == 'street'){
			Regex = /^[a-zA-Zäöüß\s\-\']+\s[0-9]+$/;
			isValid = Regex.test(val);
		//Regex for DATE
		}else if(type == 'date'){
			Regex = /^([\d]|1[0,1,2]|0[1-9])(\-|\/|\.)([0-9]|[0,1,2][0-9]|3[0,1])(\-|\/|\.)\d{4}$/;
			isValid = Regex.test(val);
		//Regex for Email
		}else if(type == 'email'){
			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(type == 'telefon'){
			//Regex = /(^\(?[2-9]\d{2}[ \-\)] ?\d{3}[\- ]?\d{4}$|^[2-9]\d{9}$)/;
			Regex = /^((\+[0-9]{2,4}( [0-9]+? | ?\([0-9]+?\) ?))|(\(0[0-9 ]+?\) ?)|(0[0-9]+? ?( |-|\/)?))[0-9]+?[0-9 \/-]*[0-9]$/;
			if(!Regex.test(val)){isValid = false;}
		//Check for U.S. 5 digit zip code
		}else if(type == 'zip'){
			Regex = /^\d{5}$/;
			if(!Regex.test(val)){isValid = false;}
		//Check for state
		}else if(type == 'state'){
			Regex = /^[a-zA-Z]{2}$/;
			if(!Regex.test(val)){isValid = false;}
		//Check for name	
		}else if(type == 'name'){
			Regex = /^[a-zA-Zäöüß\']+$/;
			if(!Regex.test(val)){isValid = false;}
		//Check for not empty empty
		}else if(type == 'confirmpassword'){
			Regex = /(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$/;
			if(typeof(options.password)!= "undefined")
			{
				Regex = options.password;
			}
			if(!Regex.test(val)){isValid = false;}			
			var el = jQuery(this).parents('form:first').find('[validate=password]');
			var pass = el.val();
			if(jQuery(this).val() != pass){isValid = false;}
			if(typeof(how) == "undefined")
			{
				el.trigger('keyup',["auto"]);
			}
		}else if(type == 'password'){
			Regex = /(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$/;
			if(typeof(options.password)!= "undefined")
			{
				Regex = options.password;
			}
			if(!Regex.test(val)){isValid = false;}			
			var el = jQuery(this).parents('form:first').find('[validate=confirmpassword]');
			var pass = el.val();
			if(jQuery(this).val() != pass){ isValid = false;}
			if(typeof(how) == "undefined")
			{
				el.trigger('keyup',["auto"]);
			}
		}else if(val.length === 0){
			isValid = false;
		}
		
		if(isValid){
			jQuery(this).removeClass("invalid").addClass("valid");
			jQuery('#formerror').css("display","none");
		}else{
			jQuery(this).removeClass("valid").addClass("invalid");
			jQuery('#formerror').css("display","block");
		}
	}	
}
jQuery.fn.FormValidate = jQuery.iFormValidate.build;

