//<![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;
			}
		});
		$('select.required', this).each(function(){
			if(this.selectedIndex == 0){
				$(this).addClass('error');
				message += $(this).attr('alt')+"\r\n";
				valid=false;
			}
		});
		if(valid){
			// continue to publish
		} else{
			alert(message);
			return false;
		}
	});
	//campusOfInterest validation
	$('#campusOfInterest').blur(function(){
		if(this.selectedIndex == 0){
			$(this).addClass('error');
			$(this).next('span').text(' Campus Of Interest required.');
		}else{
			$(this).removeClass('error');
			$(this).next('span').text('');
		}
	});
	//programOfInterest validation
	$('#programOfInterest').blur(function(){
		if(this.selectedIndex == 0){
			$(this).addClass('error');
			$(this).next('span').text(' Program Of Interest required.');
		}else{
			$(this).removeClass('error');
			$(this).next('span').text('');
		}
	});
	//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('');
		}
	});
	//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('');
		}
	});
	//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('');
		}
	});
	//graduation validation
	$('#graduation').blur(function(){
		if(this.value == ''){
			$(this).addClass('error');
			$(this).next('span').text(' Graduation required.');
		}else{
			$(this).removeClass('error');
			$(this).next('span').text('');
		}
	});
	//hearAbout validation
	$('#hearAbout').blur(function(){
		if(this.value == ''){
			$(this).addClass('error');
			$(this).next('span').text(' Hear About required.');
		}else{
			$(this).removeClass('error');
			$(this).next('span').text('');
		}
	});
	function validate(fieldVal, regexPattern){
		return fieldVal.match(regexPattern);
	}
});
//]]>