$(document).ready(function(){
	
	function checkAge(y, m, d){
		var min_age = 21;
		
		var year = parseInt(y);
		var month = parseInt(m) - 1;
		var day = parseInt(d);
		
		var theirDate = new Date((year + min_age), month, day);
		var today = new Date;

		if ( (today.getTime() - theirDate.getTime()) < 0) {
			return false;
		}else {
			return true;
		}
	}
	
	function validateEmail(email){
   		var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
   		return emailPattern.test(email);
	}
	
	function isNumber(n) {
		return !isNaN(parseFloat(n)) && isFinite(n);
	}
	
	/* Vender Contact Information - Form Validation */
	$('#clinicalResearch').submit(function() {
  		var msg = '';
		
		$(this).find('input').each(function(){
			var classname = $(this).attr('class');
			
			switch(classname){
				case 'firstname':
			  		if($(this).val().length == 0){
						msg += '<li>You must enter a first name</li>';
					}
					break;
				case 'lastname':
			  		if($(this).val().length == 0){
						msg += '<li>You must enter a last name</li>';
					}
					break;
				case 'address1':
			  		if($(this).val().length == 0){
						msg += '<li>You must enter the address street</li>';
					}
					break;
				case 'city':
			  		if($(this).val().length == 0){
						msg += '<li>You must enter the address city</li>';
					}
					break;
				case 'state':
			  		if($(this).val().length == 0){
						msg += '<li>You must enter the address state</li>';
					}
					break;
				case 'zip':
			  		if($(this).val().length == 0){
						msg += '<li>You must enter the zipcode</li>';
					}
					break
				case 'email':
					if ($(this).val() != $('input.emailConfirm').val()){
						msg += '<li>Email addresses do not match.</li>';	
					}else if (!validateEmail($(this).val())){
						msg += '<li>You must provide a valid email address.</li>';
					}
					break;
				case 'dobMonth':
			  		if($(this).val().length == 0 || $(this).val().length != 2 || $(this).val() > 12 || !isNumber($(this).val())){
						msg += '<li>You must enter a valid birth month</li>';
					}
					break;
				case 'dobDay':
			  		if($(this).val().length == 0 || $(this).val().length != 2 | $(this).val() > 31 || !isNumber($(this).val())){
						msg += '<li>You must enter a valid birth day</li>';
					}
					break;
				case 'dobYear':
			  		if($(this).val().length == 0 || $(this).val().length != 4 || !isNumber($(this).val())){
						msg += '<li>You must enter a valid birth year</li>';
					}
					break;
				case 'gender':
			  		if($(this).val() != 'Select Gender'){
						msg += '<li>You must specify a gender</li>';
					}
					break;
				case 'agreement':
			  		if($(this).val() != 'agree'){
						msg += '<li>You must agree to the terms and conditions below.</li>';
					}
					break;
			}
		});
		
		if (msg != ''){
			$('div#errors').html('<p>There appears to be an error with the information provided. Please correct these items and resubmit.</p><ul></ul>');
			$('div#errors ul').html(msg);
			$('div#errors').slideDown();
			return false;
		}
	});
	
	$('input.dobMonth, input.dobDay').change(function(){
		if ($(this).length < 1){
			$(this).attr('value', '0' + $(this).val());
		}
	});
});
