// JavaScript Document

$(function() {
	//Menu Box last child separator removal
	$("#menu_box>ul li:last-child").css("background", "none");
		
	// Age Gate
	var popupH = $(window).height();
	var popupW = $(window).width();
	
	$(window).resize(function() {
		var popupH = $(window).height();
		var popupW = $(window).width();
		$("#age-gate").css("height",popupH);
		$("#age-gate").css("width",popupW);
	});
	
	if($.cookie("age_verified") == null)				 
	{
		$("#age-gate").css("height",popupH);
		$("#age-gate").css("width",popupW);
		$("#age-gate").show();
	}
					 
	$("#lda-form").submit(function(){

		var strReqMsg = "";
		var strValidationMsg = "";
		
		if($("#month").val().replace(/\s+/g, "") == "")
		{ strReqMsg += "    - Month\n"; }
		
		if(isNaN(parseFloat($("#month").val())))
		{ strValidationMsg += "    - Month must be numeric\n"; }
		
		if($("#day").val().replace(/\s+/g, "") == "")
		{ strReqMsg += "    - Day\n"; }
		
		if(isNaN(parseFloat($("#day").val())))
		{ strValidationMsg += "    - Day must be numeric\n"; }
		
		if($("#year").val().replace(/\s+/g, "") == "")
		{ strReqMsg += "    - Year\n"; }
		
		if(isNaN(parseFloat($("#year").val())))
		{ strValidationMsg += "    - Year must be numeric\n"; }
		
		var validateDate = $("#month").val() + "/" + $("#day").val() + "/" + $("#year").val();
		
		if(!isDate(validateDate))
		{ strValidationMsg += validateDate + " is not a valid date.\n"; }
		
		// Assemble all of the error messates together to display to the user
		if(strReqMsg.length || strValidationMsg.length)
		{
			var strDisplay = "";
			if(strReqMsg.length)
			{ strDisplay += "The following fields are required to be completed:\n\n" + strReqMsg + "\n"; }
	
			if(strValidationMsg.length)
			{ strDisplay += "The following fields are not filled in correctly:\n\n" + strValidationMsg; }
	
			alert(strDisplay);
			return false;
		}
		else
		{
			var day = $("#day").val();
			var month = $("#month").val();
			var year = $("#year").val();
			var age = 21;
			var mydate = new Date();
			mydate.setFullYear(year, month-1, day);
	 		
			var currdate = new Date();
			currdate.setFullYear(currdate.getFullYear() - age);
			if ((currdate - mydate) < 0){
					alert("We're sorry. Our products are for people " + age + " and older.");
					return false;
			}
			$("#age-gate").hide();
			var options = { path: '/' }
			$.cookie("age_verified", "Yes", options);
			return false;
		}
	});
	
	// Feeds	
	$(".column .feed_item .feed_preview_date").each(function() {
		var arrDate = $(this).text().replace("Posted ", "").split("-");
		var thisDay = parseInt(arrDate[2], 10);
		var thisMonth = parseInt(arrDate[1], 10) - 1;
		var thisYear = parseInt(arrDate[0], 10);
		$(this).text($.datepicker.formatDate('MM dd, yy', new Date(thisYear,thisMonth,thisDay)));
	});
	
	//Events
	$(".column .event .event_date").each(function() {
		var arrDate = $(this).text().split("-", 3);
		var arrTime = $(this).text().split(" ");
		var thisDay = parseInt(arrDate[2], 10);
		var thisMonth = parseInt(arrDate[1], 10) - 1;
		var thisYear = parseInt(arrDate[0], 10);
		$(this).text($.datepicker.formatDate('M dd, yy', new Date(thisYear,thisMonth,thisDay)) + " " + arrTime[1] + " " + arrTime[2]);
	});
});

function isPhone(strValue) { return /^\d{3}-\d{3}-\d{4}$/.test(strValue); }
function isEmail(strValue) { return /^[-!#\$%\*\+\/\?\|\^&{}`~\w]+(\.[-!#\$%\*\+\/\?\|\^&{}`~\w]+)*@[-\w]+(\.[-\w]+)+$/.test(strValue); } 
function isZIP(strValue) { return /(^\d{5}$)|(^\d{5}-\d{4}$)/.test(strValue); } 

function validateContact(objForm)
{
	var strReqMsg = "";
	var strValidationMsg = "";

	//if(objForm.salutation.value.replace(/\s+/g, "") == "")
	//{ strReqMsg += "    - Salutation\n"; }

	if(objForm.name.value.replace(/\s+/g, "") == "")
	{ strReqMsg += "    - Name\n"; }
	
	if(objForm.email.value.replace(/\s+/g, "") == "")
	{ strReqMsg += "    - Email\n"; }
	
	if(objForm.email.value.length && !isEmail(objForm.email.value))
	{ strValidationMsg += "    - Email must be in the format username@domain.com\n"; }
	
	if(objForm.comments.value.replace(/\s+/g, "") == "")
	{ strReqMsg += "    - Comments\n"; }
	
	// Assemble all of the error messates together to display to the user
	if(strReqMsg.length || strValidationMsg.length)
	{
		var strDisplay = "";
		if(strReqMsg.length)
		{ strDisplay += "The following fields are required to be completed:\n\n" + strReqMsg; }

		if(strValidationMsg.length)
		{ strDisplay += "The following fields are not filled in correctly:\n\n" + strValidationMsg; }

		alert(strDisplay);
		return false;
	}
	else
	{
		return true;
	}

}
