//<![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 ;
	
	var messagelist = {'firstname': "First name required", 'lastname': "Last name required", 'title': "Title required", 'company': "Company required", 'phone': "Phone Number required", 'email': "Email Required"};
	
	$('#lit_form').submit(function(){
		var valid = true;
		var message = "Please correct the following:\r\n-------------------------\r\n";
		var phone = $('#phone').val();
		$('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');
		}
		//var myOption = -1;
		//for (i=this.contactme.length-1; i > -1; i--) {
		//	if (this.contactme[i].checked) {
		//		myOption = i;
		//	}
		//}
		//if (myOption == -1) {
		//	message += "Contact choice required.\r\n";
		//	valid=false;
		//}
		$('input.required', this).each(function(){
			if(this.value == ''){
				$(this).addClass('error');
				message += messagelist[$(this).attr('id')] + "\r\n";
				valid=false;
			}
		});
		if(valid){
			// do nothing
		} else{
			alert(message);
			return false;
		}
	});
	//firstname 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('');
		}
	});
	//lastname 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('');
		}
	});
	//title validation
	$('#title').blur(function(){
		if(this.value == ''){
			$(this).addClass('error');
			$(this).next('span').text(' Title required.');
		}else{
			$(this).removeClass('error');
			$(this).next('span').text('');
		}
	});
	//company validation
	$('#company').blur(function(){
		if(this.value == ''){
			$(this).addClass('error');
			$(this).next('span').text(' Company required.');
		}else{
			$(this).removeClass('error');
			$(this).next('span').text('');
		}
	});
	//phone validation
	$('#phone').blur(function(){
		//if(!validate(this.value, phonePattern) || this.value == ''){
		if(this.value == ''){
			$(this).addClass('error');
			$(this).next('span').text(' Phone number 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('');
		}
		
	});
	function validate(fieldVal, regexPattern){
		return fieldVal.match(regexPattern);
	}
});
//]]>