/*     
Changed
	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/>.
*/
function trim(s) {
	return s.replace( /^\s*/, "" ).replace( /\s*$/, "" );
}

jQuery.iFormValidate = {
	build : function(user_options)
	{
		var defaults = {
			validCheck: false,
		};
		return $(this).each(
			function() {
			var options = $.extend(defaults, user_options); 
			var $divs = $(this).find(":div");
			
			if(options.validCheck){
				var $inputs = $(this).find(":input").filter(":not(:submit)").filter(":not(:checkbox)").filter(":not(.novalid)");
			}else{
				var $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
			
			if($('#terms').is(':not(:checked)')){
				$('#terms_error').addClass("invalid");
				$('#terms_error').html('You must agree to the Terms of Service.');
				//return false;
			} else
				if($(err_id)) {
					$('#terms_error').removeClass("invalid");
					$('#terms_error').empty();
				}
				
				//we test all our inputs
				var isValid = jQuery.iFormValidate.validateForm($inputs, $divs);
				//if any of them come back false we quit
				if(!isValid){
					return false;
				}
			});
			
			$inputs.bind("blur", jQuery.iFormValidate.validate);
			$inputs.filter("select").bind("change", jQuery.iFormValidate.validate);
		});
	},
	validateForm : function($inputs, $divs)
	{
		var isValid = true; //benifit of the doubt?
		$inputs.each(jQuery.iFormValidate.validate);
		if($divs.hasClass('invalid')){isValid=false;}
		return isValid;
	},
		
	validate : function(){
		//password check for wether vpassword == vpasswordconfirm
		//var $password = $("#"+formId).find(".vpassword");
		//var $passwordconfirm = $("#"+formId).find(".vpasswordconfirm");
		var $val = $(this).val();
		var $cur_id = this.id;
		//alert($cur_id);
		var isValid = true;
		var error_txt = 'Field is required';
		//Regex for DATE
		if($(this).hasClass('textarea_class')){
			var Regex = /^.+/;
			isValid = Regex.test($val);
		//Regex for Email
		}else 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{4}$/;
			isValid = Regex.test($val);
		//Regex for Email
		}else if($(this).hasClass('vemail')){
			var Regex = /^[\w.%+-]+@[\w.-]+\.(?:[a-z]{2}|com|edu|gov|org|net|biz|info|name|aero|biz|info|jobs|museum|name)$/i;
			if(!Regex.test($val)){isValid = false; error_txt = 'Email is not correct';} else
			{
			  $.post(
					"/member/valid", 
					{email : $val},
					function(data){
						data=trim(data);
						if(data) {
							$('#'+$cur_id+'_error').addClass("invalid");
							$('#'+$cur_id+'_error').html(data);
						}
					}
					);
			}
		//Regex for Phone
		}else if($(this).hasClass('vphone')){
			var Regex = /^\(?[2-9]\d{2}[ \-\)] ?\d{3}[\- ]?\d{4}$/;
			var Regex2 = /^[2-9]\d{9}$/;
			if((!Regex.test($val))&&(!Regex2.text($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-Z0-9_]{4,16}$/;
			if(!Regex.test($val)){isValid = false; error_txt = 'Username must be at least 4 characters long';} else
			{
			  $.post(
					"/member/valid", 
					{username : $val},
					function(data){
						data=trim(data);
						if(data) {
							$('#'+$cur_id+'_error').addClass("invalid");
							$('#'+$cur_id+'_error').html(data);
						}
						else
						{
							$('#'+$cur_id+'_error').removeClass("invalid");
							$('#'+$cur_id+'_error').empty();
						}
					}
					);
		 		if($('#'+$cur_id+'_error').hasClass('invalid')){isValid=false;}
				else { 
						if($('#'+$cur_id+'_error')) {
							$('#'+$cur_id+'_error').removeClass("invalid");
							$('#'+$cur_id+'_error').empty();
						} 
					} 
			}
		//Check for not empty empty
		}else if($(this).hasClass('vpasswordconfirm')){
			//we need to find the other password field and check it
			$el = $(this);
			//locate the form so we can search for the other field
			while($el.attr("tagName").toLowerCase() != "form"){$el = $el.parent();}
			$el = $el.find(".vpassword");
			//store text of other password field
			var checkValue = $el.val();
			//comapre and set the other to red if appropriate, or green
			if($val != checkValue)
			{
				isValid= false;
				$('#'+$cur_id+'_error').addClass("invalid");
				error_txt = 'Please verify your password again';
			}
			else
			{
				if($('#'+$cur_id+'_error')) {
					$('#'+$cur_id+'_error').removeClass("invalid");
					$('#'+$cur_id+'_error').empty();
				}
			}
			
			var checkValue = $('#password').val();
			if(!checkValue) {
				$('#password_error').addClass("invalid");
				$('#password_error').html('Password is not correct!');
			}
			else 
				if(checkValue && $('#password_error')) {
					$('#password_error').removeClass("invalid");
					$('#password_error').empty();
				}
			
		}else if($(this).hasClass('vpassword')){
			if(!$val)
			{
				isValid= false;
				error_txt = 'Password is not correct!';
			}
			else
			{
				if($('#'+$cur_id+'_error'))
					$('#'+$cur_id+'_error').empty();
			}
			
			var checkValue = $('#confirm_password').val();
			if($val != checkValue) {
				$('#confirm_password_error').html('Please verify your password again');
			}
			else if($('#confirm_password_error')) $('#confirm_password_error').empty();
				
		}else if($val.length === 0){
			isValid = false;
			
		}
		
		err_id='#'+$cur_id+'_error';
		lbl_id=$('#'+$cur_id+'_label').html();
		
		if(isValid)
		{
			if($(err_id)) {
				$(err_id).removeClass("invalid");
				$(err_id).empty();
			}
		}
		else
		{
			//alert($val);
			$(err_id).addClass("invalid");
			$(err_id).html(error_txt);
		}
	}	
}
jQuery.fn.FormValidate = jQuery.iFormValidate.build;

$(document).ready(function ()
{
	$('#reg_form').FormValidate({
		validCheck: false
	});
});
