// jvalidator
// requires jpopup for messaging
// Author: Sharif Corbic
// (c) Hyperactivedesigns.com.au
// Add these classes to field for validation
// required:		Check to make sure the field is not blank
// requiredInt:		Check to make sure the field is an integer
// requiredNum:		Check to make sure the field is an number
// requiredEmail:	Check to make sure the field is a valid email address
//

// Add the following attribute to the field to have a custom error message
// validateMessage="Add your validation message here"
(function($){
	$.fn.jvalidate 	= function(options){
		var opts	= $.extend({},$.fn.jvalidate.defaults,options);	
		return this.each(function(){
			if($(this).is('form')){
				$(this).submit(function(){
					var errors = false;
					$(this).find('.required').each(function(){
						if(!errors){
							if($(this).is(':checkbox')){
								if(!$(this).is(':checked')){
									var attribVal = $(this).attr('validateMessage'); 
									if(attribVal !== undefined && (attribVal !== false) && $.trim(attribVal)!=''){
										var msg = attribVal;
									}else{
										var msg = $(this).attr('name')+' is a required field';
									}
									$(this).jpopup({title: 'Missing Field',message:msg,align:opts.dir,decay: 10000});
									$(this).focus();
									var offset 	= $(this).offset();
									var t		= offset.top>100 ? offset.top-100 : 0;
									if(opts.scrollerror){
										$(window).scrollTop(t);
									}
									errors = true;	
								}
							}else{
								var val = $.trim($(this).val());
								if(val==''){
									var attribVal = $(this).attr('validateMessage'); 
									if(attribVal !== undefined && (attribVal !== false) && $.trim(attribVal)!=''){
										var msg = attribVal;
									}else{
										var msg = $(this).attr('name')+' is a required field';
									}
									$(this).jpopup({title: 'Missing Field',message:msg,align:opts.dir,decay: 10000});
									$(this).focus();
									var offset 	= $(this).offset();
									var t		= offset.top>100 ? offset.top-100 : 0;
									if(opts.scrollerror){
										$(window).scrollTop(t);
									}
									errors = true;
								}	
							}
						}
					});
					if(errors)return false;
					$(this).find('.requiredEmail').each(function(){
						if(!errors){
							var regex	= /^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/i;
							if(!regex.test($(this).val())){
								var attribVal = $(this).attr('validateMessage'); 
								if(attribVal !== undefined && (attribVal !== false) && $.trim(attribVal)!=''){
									var msg = attribVal;	
								}else{
									var msg = 'Invalid Email Address';	
								}
								$(this).jpopup({title: 'Invalid Email Address',message:'This is an invalid email address, please try again.',align:opts.dir,decay: 10000});	
								$(this).focus();
								var offset 	= $(this).offset();
								var t		= offset.top>100 ? offset.top-100 : 0;
								if(opts.scrollerror){
									$(window).scrollTop(t);
								}
								errors = true;
							}	
						}
					});
					if(errors)return false;
					$(this).find('.requiredNum').each(function(){
						if(!errors){
							if($.trim($(this).val())=='' || isNaN($(this).val())){
								var attribVal = $(this).attr('validateMessage'); 
								if(attribVal !== undefined && (attribVal !== false) && $.trim(attribVal)!=''){
									var msg = attribVal;
								}else{
									var msg = 'This field must be a number';	
								}
								$(this).jpopup({title: 'Invalid Format',message:msg,align:opts.dir,decay: 10000});
								$(this).focus();
								var offset 	= $(this).offset();
								var t		= offset.top>100 ? offset.top-100 : 0;
								if(opts.scrollerror){
									$(window).scrollTop(t);
								}
								errors = true;
							}	
						}
					});
					if(errors)return false;
					$(this).find('.requiredInt').each(function(){
						if(!errors){
							var num = $.trim($(this).val());
							if(num=='' || isNaN(num) || num%1!=0 ){
								var attribVal = $(this).attr('validateMessage'); 
								if(attribVal !== undefined && (attribVal !== false) && $.trim(attribVal)!=''){
									var msg = attribVal;	
								}else{
									var msg = 'This field must be an integer';	
								}
								$(this).jpopup({title: 'Invalid Format',message:msg,align:opts.dir,decay: 10000});
								$(this).focus();
								var offset 	= $(this).offset();
								var t		= offset.top>100 ? offset.top-100 : 0;
								if(opts.scrollerror){
									$(window).scrollTop(t);
								}
								errors = true;
							}	
						}
					});
					if(errors)return false;
					var sames = new Array();
					var compare = '';
					$(this).find('.requiredSame').each(function(idx,val){
						if(idx==0)compare = $(this).val();
						else if($(this).val()!=compare){
							errors = true;	
						}
					});
					if(errors){
						var msg = 'This field and '+$(this).find('.requiredSame:last').attr('name').replace('_',' ')+' must be the same';
						$(this).find('.requiredSame:last').jpopup({title:'Fields must match',message:msg,align:opts.dir,decay: 10000});
						$(this).focus();
						var offset 	= $(this).find('.requiredSame:last').offset();
						var t		= offset.top>100 ? offset.top-100 : 0;
						if(opts.scrollerror){
							$(window).scrollTop(t);
						}
						return false;
					}
					$(this).ajaxSubmit({
						type: 'post',
						dataType: 'json',
						beforeSubmit:	function(){
							waitElement('Send');
						},
						success: function(data){
							restoreElement('Send');
							$('.success').show();
							$('input').each(function(){
								if($(this).attr('type')!='hidden')$(this).val('');						 
							});
							$('textarea').val('');
						},
						error: function(){
							restoreElement('Send');
							jpop('Send','Error',standarderror,opts.dir,10000);
						}
					});
					return false;
				});
			}
		});
	}
	// plugin defaults
	$.fn.jvalidate.defaults	= {dir: 'bottom',scrollerror: true}
})(jQuery);
