$(document).ready(function() {
	$('#ContactEventTypeOther, #ContactEventLocationOther, #ContactFoodServiceTypeOther').hide();
	$('#ContactEventType').change(function() {
		if($(this).val() == "Other") {
			$('#ContactEventTypeOther').fadeIn();
		} else {
			$('#ContactEventTypeOther').fadeOut();
		}
	});
	
	$('#ContactEventLocation').change(function() {
		if($(this).val() == "Other") {
			$('#ContactEventLocationOther').fadeIn();
		} else {
			$('#ContactEventLocationOther').fadeOut();
		}
	});
	
	$('#ContactFoodServiceType').change(function() {
		if($(this).val() == "Other") {
			$('#ContactFoodServiceTypeOther').fadeIn();
		} else {
			$('#ContactFoodServiceTypeOther').fadeOut();
		}
	});
	var date = new Date();
	var year = date.getFullYear();
	var month = date.getMonth();
	var day = date.getDate() + 1;
	$('#ContactEventDate').attachDatepicker({
		yearRange: year+':'+(year+2),
		minDate: new Date(year, month, day),
		maxDate: new Date(year+2,month,day)
	});
	
	$('input[type=text], textarea').each(function() {
		if($(this).val() == "") {
			$(this).val($(this).attr('placeholder'));
		}
	});
	
	$('input[type=text], textarea').focus(function() {
		var value = $(this).val();
		var placeholder = $(this).attr('placeholder');
		
		if(value == placeholder) {
			$(this).val("");
		}
	});
	
	$('input[type=text], textarea').blur(function() {
		var value = $(this).val();
		var placeholder = $(this).attr('placeholder');
		
		if(value == "") {
			$(this).val(placeholder);
		}
	});
	
	$('#ContactForm').submit(function() {
		var success = true;
		
		$('.required').each(function() {
			var value = $(this).val();
			var placeholder = $(this).attr('placeholder');
			
			if(value == "" || value == placeholder) {
				success = false
				$(this).css('color', 'rgb(158,28,54)');
				$(this).css('border-color', 'rgb(158,28,54)');
			} else {
				$(this).css('color', 'rgb(65,75,86)');
				$(this).css('border-color', 'rgb(177,175,175)');
			}
		});
		
		if(!success) {
			alert('The fields outlined in red are required.');
			return success;
		}
		
		$('.phone').each(function() {
			var value = $(this).val();
			var placeholder = $(this).attr('placeholder');
			
			if((value != "" && value != placeholder) && !value.match(/^[\(]?(\d{0,3})[\)]?[\s]?[\-\.]?(\d{3})[\s]?[\-\.]?(\d{4})[\s]?[x]?(\d*)$/)) {
				success = false;
				$(this).css('color', 'rgb(158,28,54)');
				$(this).css('border-color', 'rgb(158,28,54)');
			} else {
				$(this).css('color', 'rgb(65,75,86)');
				$(this).css('border-color', 'rgb(177,175,175)');
			}
		});
		
		$('.email').each(function() {
			var value = $(this).val();
			var placeholder = $(this).attr('placeholder');
			
			if((value != "" && value != placeholder) && !value.match(/^([a-z0-9][a-z0-9_\-\.\+]*)@([a-z0-9][a-z0-9\.\-]{0,63}\.(com|org|net|biz|info|name|net|pro|aero|coop|museum|[a-z]{2,4}))$/i)) {
				success = false;
				$(this).css('color', 'rgb(158,28,54)');
				$(this).css('border-color', 'rgb(158,28,54)');
			} else {
				$(this).css('color', 'rgb(65,75,86)');
				$(this).css('border-color', 'rgb(177,175,175)');
			}
		});
		
		$('.zip').each(function() {
			var value = $(this).val();
			var placeholder = $(this).attr('placeholder');
			
			if((value != "" && value != placeholder) && !value.match(/^[0-9]{5}$/)) {
				success = false;
				$(this).css('color', 'rgb(158,28,54)');
				$(this).css('border-color', 'rgb(158,28,54)');
			} else {
				$(this).css('color', 'rgb(65,75,86)');
				$(this).css('border-color', 'rgb(177,175,175)');
			}
		});
		
		$('.time').each(function() {
			var value = $(this).val();
			var placeholder = $(this).attr('placeholder');
			
			if((value != "" && value != placeholder) && !value.match(/^([0-9]{1,2}):([0-9]{2})(:[0-9]{2})?[\s]?(am|pm)$/i)) {
				success = false;
				$(this).css('color', 'rgb(158,28,54)');
				$(this).css('border-color', 'rgb(158,28,54)');
			} else {
				$(this).css('color', 'rgb(65,75,86)');
				$(this).css('border-color', 'rgb(177,175,175)');
			}
		});
		
		$('.date').each(function() {
			var value = $(this).val();
			var placeholder = $(this).attr('placeholder');
			
			if((value != "" && value != placeholder) && !value.match(/^([0-9]{1,2})\/([0-9]{1,2})\/(20[0-9]{2})$/)) {
				success = false;
				$(this).css('color', 'rgb(158,28,54)');
				$(this).css('border-color', 'rgb(158,28,54)');
			} else {
				$(this).css('color', 'rgb(65,75,86)');
				$(this).css('border-color', 'rgb(177,175,175)');
			}
		});
		
		if(!success) {
			alert('Please check the fields outlined in red and verify that they are formatted correctly.');
		}
		
		$('#ContactForm input, #ContactForm textarea').each(function() {
			var value = $(this).val();
			var placeholder = $(this).attr('placeholder');
			
			if(value == placeholder) {
				$(this).val("");
			}
		});
		
		return success;
	});
});
