function validator() {
	var x = this;
	x.errorFields = [];
	
	validator.prototype.validateEmail = function(sel){
		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
		return pattern.test($(sel).val());
	}
	
	validator.prototype.appendErrorMsg = function(sel){
		$(sel).append($('<span></span>').attr('class','errormsg').text('This field is required.  Please complete before continuing.'));
	}
	
	validator.prototype.addError = function(field){
		x.errorFields.push(field);
	}
	
	validator.prototype.clearForm = function() {
		$('#errorContainer').fadeOut();
		$('#errorlist').remove();
		$('.error').removeClass('error');
		x.errorFields = [];
	}
	
	validator.prototype.markErrors = function(){
		if (x.errorFields.length > 0) {
			var l = $('<ul></ul>').attr('id','errorlist');
		
			$.each(x.errorFields,function(){
				var s = this;
				label = $('#'+s).siblings('label').text();
				$('#'+s).parent().addClass('error');
			
				if ($('#'+s).parent().hasClass('checkboxfield')) {
					console.log('Adding error class to '+s);
					label = $('#'+s).parent().siblings('p').text();
					$('#'+s).parent().addClass('error');
				}
				
				if (label == '') {
					label = $('#'+s).siblings('p').text();
				}
			
				if (s == 'project_masonrydescription') {
					label = 'Description of Masonry Use';
				}
			
				label = label.replace(/:/,'');
				label = label.replace(/\*/,'');
				h = $('<a></a>').attr('href','#'+s).text(label);
				i = $('<li></li>').html(h)
				//l.append(i);
			});
			//$('#errorContainer').fadeIn().append(l);
			
			//$.scrollTo('#'+x.errorFields[0]);//all divs w/class pane
			
			// We return false so that the form doesn't submit
			return false;
		} else {
			// We return true so that the form itself submits
			return true;
		}
	}
	
	validator.prototype.validate = function() {
		x.clearForm();
		
		$('.required:not(.phonenumber):not(.conditionalrequired) input[type=text],.required select').each(function(){
			if ($(this).val() == '') {
				x.addError($(this).attr('id'));
			}
		});		
	}	
}

var v = new validator();

$(document).ready(function(){
	$('form:not(.admin) .field,.checkboxfield').append($('<div></div>').attr('class','clear'));
	$('.required>label,.required:not(.description)>p').append($('<span>*</span>').attr('class','required'));
	v.appendErrorMsg('.required:not(span)');
});