//<![CDATA[
//
$(function(){
	$("<span>").addClass('warning').appendTo('fieldset p');
	
	var phonePattern = /[\s\(]*[0-9]{3}[\s\)-\.]*[0-9]{3}[\s-\.]*[0-9]{4}/ ;
	var emailPattern = /\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i ;
	$('#myForm').submit(function(){
		var valid = true;
		var message = "Please correct the following:\r\n-------------------------\r\n";
		$('input').removeClass('error');
				
		if(!validate($('#phone').val(), phonePattern) ){
			valid=false;
			message += "Use valid phone number format.\r\n";
			$('#phone').addClass('error');
		}
		if(!validate($('#email').val(), emailPattern) ){
			valid=false;
			message += "Check email address.\r\n";
			$('#email').addClass('error');
		}
		$('input.required', this).each(function(){
			if(this.value == ''){
				$(this).addClass('error');
				message += $(this).attr('alt')+"\r\n";
				valid=false;
			}
		});
		$('input.requiredCheck', this).each(function(){
			if(this.checked == false){
				$(this).addClass('error');
				message += $(this).attr('alt')+"\r\n";
				valid=false;
			}
		});
		if(!document.myForm.contactPermission.checked){
			$(this).addClass('error');
			message += "You must check the permission box.\r\n";
			valid=false;
		}
		if(!document.myForm.infoAccurate.checked){
			$(this).addClass('error');
			message += "You must check the accuracy box.\r\n";
			valid=false;
		}

		if(valid){
			// continue to publish
		} else{
			alert(message);
			return false;
		}
	});
	//first name validation
	$('#firstName').blur(function(){
		if(this.value == ''){
			$(this).addClass('error');
			$(this).next('span').text(' First name required.');
		}else{
			$(this).removeClass('error');
			$(this).next('span').text('');
		}
	});
	//last name validation
	$('#lastName').blur(function(){
		if(this.value == ''){
			$(this).addClass('error');
			$(this).next('span').text(' Last name required.');
		}else{
			$(this).removeClass('error');
			$(this).next('span').text('');
		}
	});
	//email validation
	$('#email').blur(function(){
		if(!validate(this.value, emailPattern) || this.value == ''){
			$(this).addClass('error');
			$(this).next('span').text(' Invalid e-mail. (name@gmail.com)');
		}else{
			$(this).removeClass('error');
			$(this).next('span').text('');
		}
		
	});
	//address validation
	$('#address').blur(function(){
		if(this.value == ''){
			$(this).addClass('error');
			$(this).next('span').text(' Address required.');
		}else{
			$(this).removeClass('error');
			$(this).next('span').text('');
		}
	});
	//city validation
	$('#city').blur(function(){
		if(this.value == ''){
			$(this).addClass('error');
			$(this).next('span').text(' City required.');
		}else{
			$(this).removeClass('error');
			$(this).next('span').text('');
		}
	});
	//state validation
	$('#state').blur(function(){
		if(this.value == ''){
			$(this).addClass('error');
			$(this).next('span').text(' State required.');
		}else{
			$(this).removeClass('error');
			$(this).next('span').text('');
		}
	});
	//zip validation
	$('#zipCode').blur(function(){
		if(this.value == ''){
			$(this).addClass('error');
			$(this).next('span').text(' Zip code required.');
		}else{
			$(this).removeClass('error');
			$(this).next('span').text('');
		}
	});
	//phone validation
	$('#phone').blur(function(){
		if(!validate(this.value, phonePattern) || this.value == ''){
			$(this).addClass('error');
			$(this).next('span').text(' Invalid phone number (555-555-5555)');
		}else{
			$(this).removeClass('error');
			$(this).next('span').text('');
		}
	});
	//location validation
	$('#location').blur(function(){
		if(this.value == ''){
			$(this).addClass('error');
			$(this).next('span').text(' Location is required.');
		}else{
			$(this).removeClass('error');
			$(this).next('span').text('');
		}
	});

	function validate(fieldVal, regexPattern){
		return fieldVal.match(regexPattern);
	}
});
//]]>