jQuery(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);
	}
	
	/* Vender Contact Information - Form Validation */
	jQuery('#about-us-contact-form').submit(function() {
  		var msg = '';
		var zipcodes = false;
		
		jQuery(this).find('input').each(function(){
			var classname = jQuery(this).attr('class');
			
			switch(classname){
				case 'name':
			  		if(jQuery(this).val().length == 0){
						msg += '<li>You must enter your name</li>';
					}
					break;
				case 'zip':
			  		if(jQuery(this).val().length == 0){
						msg += '<li>You must enter the zipcode</li>';
					}
					
					jQuery.ajax({
						url: '/modules/mod_checkZip.cfm?zip=' + jQuery(this).val(),
						success: function(data){
							if (data == 0){
								jQuery('div.errors ul').append('<li>You must enter a valid zipcode</li>');
							}
					  	}
					});
					break;
				case 'phone':
			  		if(jQuery(this).val().length == 0){
						msg += '<li>You must enter the phone number</li>';
					}
					break;
				case 'email':
					if (!validateEmail(jQuery(this).val())){
						msg += '<li>You must provide a valid email address.</li>';
					}
					break;
			}
		});
		
		if (msg != ''){
			jQuery('div.errors').html('<p>There appears to be an error with the information provided. Please correct these items and resubmit.</p><ul></ul>');
			jQuery('div.errors ul').html(msg);
			jQuery('div.errors').slideDown();
			return false;
		}
	});
	
	/* Vender Contact Information - Form Validation */
	jQuery('#vender-contact-form').submit(function() {
  		var msg = '';
		
		jQuery(this).find('input').each(function(){
			var classname = jQuery(this).attr('class');
			
			switch(classname){
				case 'first-name':
			  		if(jQuery(this).val().length == 0){
						msg += '<li>You must enter your first name</li>';
					}
					break;
				case 'last-name':
					if(jQuery(this).val().length == 0){
						msg += '<li>You must enter your last name</li>';	
					}
					break;
				case 'company-name':
			  		if(jQuery(this).val().length == 0){
						msg += '<li>You must enter the company name</li>';
					}
				case 'company-address1':
			  		if(jQuery(this).val().length == 0){
						msg += '<li>You must enter the company address</li>';
					}
					break;
				case 'city':
			  		if(jQuery(this).val().length == 0){
						msg += '<li>You must enter the city</li>';
					}
					break;
				case 'state':
			  		if(jQuery(this).val().length == 0){
						msg += '<li>You must enter the state</li>';
					}
					break;
				case 'zip':
			  		if(jQuery(this).val().length == 0){
						msg += '<li>You must enter the zipcode</li>';
					}
					
					jQuery.ajax({
						url: '/modules/mod_checkZip.cfm?zip=' + jQuery(this).val(),
						success: function(data){
							if (data == 0){
								jQuery('div.errors ul').append('<li>You must enter a valid zipcode</li>');
							}
					  	}
					});
					break;
				case 'phone1':
					if ((jQuery(this).val() == '' || jQuery('input.phone2').val() == '' || jQuery('input.phone3').val() == '') || (jQuery(this).val().length != 3 || jQuery(this).val() == 0) || (jQuery('input.phone2').val().length != 3 || jQuery('input.phone2').val() == 0) || (jQuery('input.phone3').val().length != 4 || jQuery('input.phone3').val() == 0)){
						msg += '<li>You must enter a valid phone number.</li>';
					}
					
					break;
				case 'email':
					if (jQuery(this).val() != jQuery('input.email-verify').val()){
						msg += '<li>Email addresses do not match.</li>';	
					}else if (!validateEmail(jQuery(this).val())){
						msg += '<li>You must provide a valid email address.</li>';
					}
					break;
			}
		});
		
		if (msg != ''){
			jQuery('div.errors').html('<p>There appears to be an error with the information provided. Please correct these items and resubmit.</p><ul></ul>');
			jQuery('div.errors ul').html(msg);
			jQuery('div.errors').slideDown();
			return false;
		}
	});
	
	/* Guest Register - Form Validation */
	jQuery('#guest-register-form').submit(function() {
  		var msg = '';
		
		jQuery(this).find('input').each(function(){
			var classname = jQuery(this).attr('class');
			
			switch(classname){
				case 'first-name':
			  		if(jQuery(this).val().length == 0){
						msg += '<li>You must enter your first name</li>';
					}
					break;
				case 'last-name':
					if(jQuery(this).val().length == 0){
						msg += '<li>You must enter your last name</li>';	
					}
					break;
				case 'email':
					if (jQuery(this).val() != jQuery('input.email_verify').val()){
						msg += '<li>Email addresses do not match.</li>';	
					}else if (!validateEmail(jQuery(this).val())){
						msg += '<li>You must provide a valid email address.</li>';
					}
					break;
				case 'zip':
			  		if(jQuery(this).val().length == 0){
						msg += '<li>You must enter the zipcode</li>';
					}
					
					jQuery.ajax({
						url: '/modules/mod_checkZip.cfm?zip=' + jQuery(this).val(),
						success: function(data){
							if (data == 0){
								jQuery('div.errors ul').append('<li>You must enter a valid zipcode</li>');
							}
					  	}
					});
					break;
			}
		});
		
		jQuery(this).find('select').each(function(){
			var classname = jQuery(this).attr('class');
			
			switch(classname){
				case 'datebirth1':
					if (jQuery(this).find('option:selected').val() == '' || jQuery('select.datebirth2 option:selected').val() == '' || jQuery('select.datebirth3 option:selected').val() == ''){
						msg += '<li>You must enter a valid birthday.</li>';	
					}
					
					/*if (jQuery(this).val().length != 2 || jQuery(this).val() > 12 || jQuery(this).val() == 0){
						msg += '<li>You must enter a valid month.</li>';
					}
					
					if (jQuery('input.date-birth2').val().length != 2 || jQuery('input.date-birth2').val() == 0){
						msg += '<li>You must enter a valid day.</li>';
					}
					
					if (jQuery('input.date-birth3').val().length != 4 || jQuery('input.date-birth3').val() == 0){
						msg += '<li>You must enter a valid year.</li>';
					}*/
					
					if (checkAge(jQuery('select.datebirth3 option:selected').val(), jQuery('select.datebirth1 option:selected').val(), jQuery('select.datebirth2 option:selected').val()) == false){
						msg += '<li>You must be 21 or over to register.</li>';
					}
					
					break;
			}
		});
		
		if (msg != ''){
			jQuery('div.errors').html('<p>There appears to be an error with the information provided. Please correct these items and resubmit.</p><ul></ul>');
			jQuery('div.errors ul').html(msg);
			jQuery('div.errors').slideDown();
			return false;
		}
	});
	
	jQuery("#multi").multiSelect({
        selectAll: false,
        noneSelected: 'Select all that apply.',
        oneOrMoreSelected: '% options checked'
    });
});
