/* -------------------------------------
/////////////////////////////////////////
// Simple Form Validation with JQuery. //
/////////////////////////////////////////
// C.Burnett :: 09.05.06 ////////////////
/////////////////////////////////////////
---------------------------------------*/
//var str = 'marcus@airtightdesign.com';str.isEmail();
/* -------------------------------------
/////////////////////////////////////////
// Simple Form Validation with JQuery. //
/////////////////////////////////////////
// C.Burnett :: 09.05.06 ////////////////
/////////////////////////////////////////
---------------------------------------*/
$.fn.validate = function(e) {
	$('.formError',this).remove();
	$('.radioFormError',this).remove();
	var er = 0;
	var region = 0;
	var sal = 0;
	var isRadio = 0;
	var effect = 'fadeIn'; // Pick default effect
    var speed = 'fast'; // Choose default speed
	// Simple empty field validation
	$('.validate-required',this).each(function (i) {
		var d = $(this).css('display');
		if(d != 'none'){
			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;
			} else {
				var v = this.value;
			}
			var msg = $(this).attr('title');
			if (v === '' || v === false) {
				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-radio',this).each(function (i) {
		var d = $(this).css('display');
		if(d != 'none'){
			var check = false;
			$("#"+this.id+" input").each(function () {
				if(this.checked){
					check = true;
				}
			});
			if(!check) {
				var msg = $(this).attr('title');
				html = '<div class="formError">'+msg+'</div>';
				$('#'+this.id).before(html);
				if (!er)
				   $('#'+this.id).ScrollTo(500);
				er++;
			}else{
				// Remove error class
			}
		}
	});
	$('.validate-year',this).each(function (i) {
		var d = $(this).css('display');
		if(d != 'none'){
			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 != 4) {
						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-zip',this).each(function (i) {
		var d = $(this).css('display');
		if(d != 'none'){
			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',this).each(function (i) {
		var d = $(this).css('display');
		if(d != 'none'){
			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.length != 11) {
						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',this).each(function (i) {
		var d = $(this).css('display');
		if(d != 'none'){
		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 < 12) {
						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',this).each(function (i) {
		var d = $(this).css('display');
		if(d != 'none'){
			var v = this.value;
			var msg = $(this).attr('title');
			var reg = new RegExp('^[^@()<>,;:\\\\/"[\\]]+@[^@()<>,;:\\\\/"[\\]]+\\.[a-zA-Z0-9]{2,6}$','');
			if(!v.match(reg)){
				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');
			}
		}
	});

	// Catch Submit Event on Error
	if (er) {
		//e.preventDefault();
		return false;
	}else{
		return true;
	}
}