// 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:'bottom',decay: 1000000});
									$(this).focus();
									var offset 	= $(this).offset();
									var t		= offset.top>100 ? offset.top-100 : 0;
									$(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:'bottom',decay: 1000000});
									$(this).focus();
									var offset 	= $(this).offset();
									var t		= offset.top>100 ? offset.top-100 : 0;
									$(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:'bottom',decay: 10000});	
								$(this).focus();
								var offset 	= $(this).offset();
								var t		= offset.top>100 ? offset.top-100 : 0;
								$(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:'bottom',decay: 10000});
								$(this).focus();
								var offset 	= $(this).offset();
								var t		= offset.top>100 ? offset.top-100 : 0;
								$(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:'bottom',decay: 10000});
								$(this).focus();
								var offset 	= $(this).offset();
								var t		= offset.top>100 ? offset.top-100 : 0;
								$(window).scrollTop(t);
								errors = true;
							}	
						}
					});
					$(this).find('[class*=requiredIf]').each(function(){
						inp = $(this);
						if(!errors){
							deps = $(this).attr('class').split(' ');
							var dep = '';
							$.each(deps,function(idx,val){
								if(val.indexOf('requiredIf')==0){
									dep = val.replace('requiredIf',''); 
								}
							});
							if($('#'+dep).is(':checked')){
								if(inp.is(':checkbox')){
									if(!inp.is(':checked')){
										var attribVal = inp.attr('validateMessage'); 
										if(attribVal !== undefined && (attribVal !== false) && $.trim(attribVal)!=''){
											var msg = attribVal;
										}else{
											var msg = inp.attr('name')+' is a required field';
										}
										inp.jpopup({title: 'Missing Field',message:msg,align:'bottom',decay: 1000000});
										inp.focus();
										var offset 	= inp.offset();
										var t		= offset.top>100 ? offset.top-100 : 0;
										$(window).scrollTop(t);
										errors = true;	
									}
								}else{
									var val = $.trim(inp.val());
									if(val==''){
										var attribVal = inp.attr('validateMessage'); 
										if(attribVal !== undefined && (attribVal !== false) && $.trim(attribVal)!=''){
											var msg = attribVal;
										}else{
											var msg = inp.attr('name')+' is a required field';
										}
										inp.jpopup({title: 'Missing Field',message:msg,align:'bottom',decay: 1000000});
										inp.focus();
										var offset 	= inp.offset();
										var t		= offset.top>100 ? offset.top-100 : 0;
										$(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:'bottom',decay: 10000});
						$(this).focus();
						var offset 	= $(this).find('.requiredSame:last').offset();
						var t		= offset.top>100 ? offset.top-100 : 0;
						$(window).scrollTop(t);
						return false;
					}
					if(typeof(opts.func)!='undefined'){
						opts.func();
					}
					return false;
				});
			}
		});
	}
	// plugin defaults
	$.fn.jvalidate.defaults	= {}
})(jQuery);
