$(document).ready(function(){
// start of DOM ready

	// validate required fields filled out on contact form
	$('form#contact').submit(function(){
		if ($('#contact_to_opt').val()=='none' || 
			$('#contact_subject').val()=='' || 
			$('#email').val()=='' ||
			$('#answer').val()==''){
			msg = 'To, subject, email and anti-spam question are required.'
			printErr(msg, '#contact_btn');
			return false;
		};
	});
	
	// validate required fields filled out on mail list form
	$('form#mailing').submit(function(){
		if ($('#fname').val()=='' || 
			$('#lname').val()=='' || 
			$('#email').val()=='' ||
			$('#answer').val()==''){
			msg = 'First name, Last name, email and anti-spam question are required.'
			printErr(msg, '#maillist_btn');
			return false;
		};
	});

	
	// Firstname validation
	$('.fname').blur(function(){
		locat = "#" + $(this).attr('id');
		if (this.value != ''){
			if (!/^[a-zA-Z\s]{2,15}$/.test(this.value)){
				msg = 'Letters only. 2-15 chars';
				printErr(msg, locat);
				$(locat).val('');
			};
		};
	});
	
	// Lastname validation
	$('.lname').blur(function(){
		locat = "#" + $(this).attr('id');
		if (this.value != ''){
			if (!/^[a-zA-Z\s]{2,25}$/.test(this.value)){
				msg = 'Letters only. 2-25 chars';
				printErr(msg, locat);
				$(locat).val('');
			};
		};
	});
	
	// Street validation
	$('.street').blur(function(){
		locat = "#" + $(this).attr('id');
		if (this.value != ''){
			if (!/\d{1,3}.?\d{0,3}\s[a-zA-Z]{2,30}\s[a-zA-Z]{2,15}/.test(this.value)){
				msg = 'Enter a valid street';
				printErr(msg, locat);
				$(locat).val('');
			};
		};
	});
	
	// PO validation
	$('.pobox').blur(function(){
		locat = "#" + $(this).attr('id');
		if (this.value != ''){
			if (!/^((p[\s|\.|,]*| ^post[\s|\.]*)(o[\s|\.|,]*| office[\s|\.]*))| (^box[.|\s]*\d+)/.test(this.value)){
				msg = 'Enter a valid PO box';
				printErr(msg, locat);
				$(locat).val('');
				$(this).addClass('placeholder').val(poboxHint);
			};
		};
	});
	
	// City validation
	$('.city').blur(function(){
		locat = "#" + $(this).attr('id');
		if (this.value != ''){
			if (!/^\s*[a-zA-Z.\s]+\s*$/.test(this.value)){
				msg = 'Enter a valid city';
				printErr(msg, locat);
				$(locat).val('');
			};
		};
	});
	
	// Zip code validation
	$('.zip').blur(function(){
		locat = "#" + $(this).attr('id');
		if (this.value != ''){
			if (!/^\d{5}(-\d{4})?$/.test(this.value)){
				msg = 'Enter a valid zip code';
				printErr(msg, locat);
				$(locat).val('');
			};
		};
	});
	
	// Phone validation
	$('.phone').blur(function(){
		locat = "#" + $(this).attr('id');
		if (this.value != ''){
			if (!/^\d{3}-\d{3}-\d{4}$/.test(this.value)){
				msg = 'Use format ###-###-####';
				printErr(msg, locat);
				$(locat).val('');
				$(this).addClass('placeholder').val(phoneHint);
			};
		};
	});
	
	// email validation
	$('.email').blur(function(){
		locat = "#" + $(this).attr('id');
		if (this.value != ''){
			if (!/^[\w\.=-]+@[\w\.-]+\.[\w]{2,3}$/.test(this.value)){
				msg = 'Enter a valid email address';
				printErr(msg, locat);
				$(locat).val('');
			};
		};
	});
	
	// question validation
 	$('.question').blur(function(){
 		locat = "#" + $(this).attr('id');
 		if (this.value != ''){
 			if (!/shotgun/i.test(this.value)){
	 			msg = 'Answer is incorrect';
	 			printErr(msg, locat);
	 			$(locat).val('');
 			};
 		};
 	});
	
// end DOM ready	
});

// other functions that do not need to run on DOM load

function printErr(msg, locat){
	$('#error_msg').remove();
	$(locat).next('span.required').hide();
	$('<p><span></span></p>')
		.hide()
		.attr({'id':'error_msg', 'class':'error'})
		.append(msg)
		.insertAfter(locat)
		.fadeIn('slow');
	setTimeout( "$('#error_msg').fadeOut('slow')", 5000);
	setTimeout( "$('span.required').show()", 6000);
};
