/* -------------------------------------
/////////////////////////////////////////
// Simple Form Validation with JQuery. //
/////////////////////////////////////////
// C.Burnett :: 09.05.06 ////////////////
/////////////////////////////////////////
---------------------------------------*/
$.fn.validate = function() {
    var effect = 'fadeIn'; // Pick default effect
    var speed = 'fast'; // Choose default speed
 	 this.submit(function(e) {
			$('.formError').remove();
			var er = 0;
			var region = 0;
			var sal = 0;
			var isRadio = 0;
			// Simple empty field validation
			$('.validate-required').each(function (i) {
				var type = $(this).attr('type');
				if (type == 'text') {
					var v = this.value;
				}else if (type == 'checkbox') {
					var v = this.checked;
				}else if (type == 'radio'){
					var v = this.checked;
					isRadio = 1;
				}
				var msg = $(this).attr('title');
				if (v == '' || v == false) {
					html = '<span class="formError">'+msg+'</span>';
					$('#'+this.id).after(html);
					$('#'+this.id).addClass('errorField');
					// ScrollTo Requires Interface extensions
					if (!er)
					   // $('#'+this.id).ScrollTo(500);
					er++;
				}else{
					$('#'+this.id).removeClass('errorField');
				}        
			});
			
			$('.validate-zip').each(function (i) {
				var type = $(this).attr('type');
				if (type == 'text') {
					var v = this.value;
				}else if (type == 'checkbox') {
					var v = this.checked;
				}else if (type == 'radio'){
					var v = this.checked;
					isRadio = 1;
				}
				var msg = $(this).attr('title');
							   
				if (v == '' || v == false || isNaN(v) == true || v.length != 5) {
							html = '<div class="formError">'+msg+'</div>';
							$('#'+this.id).after(html);
							$('#'+this.id).addClass('errorField');
							// ScrollTo Requires Interface extensions
							if (!er)
								//$('#'+this.id).ScrollTo(500);
							er++;
				}else{
					$('#'+this.id).removeClass('errorField');
				}        
			});
			$('.validate-ssn').each(function (i) {
				var type = $(this).attr('type');
				if (type == 'text') {
					var v = this.value;
				}else if (type == 'checkbox') {
					var v = this.checked;
				}else if (type == 'radio'){
					var v = this.checked;
					isRadio = 1;
				}
				var msg = $(this).attr('title');
							   
				if (v == '' || v == false || isNaN(v) == true || v.length != 9) {
							html = '<div class="formError">'+msg+'</div>';
							$('#'+this.id).after(html);
							$('#'+this.id).addClass('errorField');
							// ScrollTo Requires Interface extensions
							if (!er)
								//$('#'+this.id).ScrollTo(500);
							er++;
				}else{
					$('#'+this.id).removeClass('errorField');
				}        
			});
			$('.validate-phone').each(function (i) {
				var type = $(this).attr('type');
				if (type == 'text') {
					var v = this.value;
				}else if (type == 'checkbox') {
					var v = this.checked;
				}else if (type == 'radio'){
					var v = this.checked;
					isRadio = 1;
				}
				var msg = $(this).attr('title');
							   
				if (v == '' || v == false || v.length < 10) {
							html = '<div class="formError">'+msg+'</div>';
							$('#'+this.id).after(html);
							$('#'+this.id).addClass('errorField');
							// ScrollTo Requires Interface extensions
							if (!er)
								//$('#'+this.id).ScrollTo(500);
							er++;
				}else{
					$('#'+this.id).removeClass('errorField');
				}        
			});
			// Email validation
			$('.validate-email').each(function (i) {
				var v = this.value;
				var msg = $(this).attr('title');
				var emailFilter=/(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{3})$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/i;
				if (!(emailFilter.test(v))){
					html = '<span class="formError">'+msg+'</span>';
					$('#'+this.id).after(html);
					$('#'+this.id).addClass('errorField');
					// ScrollTo Requires Interface extensions
					if (!er)
						//$('#'+this.id).ScrollTo(500);
					er++;
				}else{
					$('#'+this.id).removeClass('errorField');
				}        
			});
			
			// Call Error effect
			$('div.formError')[effect](speed);
			// Catch Submit Event on Error
			if (er) {
				e.preventDefault();
				$('body').ScrollTo(500);
			}

			
		});
};