//<![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 = {'company': "Company required", 'contactname': "Contact Name required", 'telephone': "Phone Number required", 'email': "Email Required"};

	$('#arinc600_form').submit(function(){
		var valid = true;
		var message = "Please correct the following:\r\n-------------------------\r\n";
		var phone = $('#telephone').val();
		$('input').removeClass('error');
		
		//if(!validate($('#telephone').val(), phonePattern) ){
		//	valid=false;
		//	message += "Use valid phone number format.\r\n";
		//	$('#telephone').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 += messagelist[$(this).attr('id')] + "\r\n";
				valid=false;
			}
		});
		if(valid){
			// do nothing
		} else{
			alert(message);
			return false;
		}
	});
	//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('');
		}
	});
	//contactname validation
	$('#contactname').blur(function(){
		if(this.value == ''){
			$(this).addClass('error');
			$(this).next('span').text('Contact Name required.');
		}else{
			$(this).removeClass('error');
			$(this).next('span').text('');
		}
	});
	//phone validation
	$('#telephone').blur(function(){
		//if(!validate(this.value, phonePattern) || this.value == ''){
		if(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('');
		}
		
	});
	function validate(fieldVal, regexPattern){
		return fieldVal.match(regexPattern);
	}
});
//]]>
