$(document).ready(function () {

		//e.preventDefault();
		
		var userSurvey;
		var userSubmissionURL = "_handlers/submitSurvey.aspx";
		
		/********************************
		Display User Survey?
		=================================
		Only display if cookies enabled and user has not already completed survey
		*********************************/
		if (userSurveyEnabled && cookiesEnabled() && getVisitorUserSegment() == null)
		{
			userSurvey = $("#basicModalContent").modal();
		}
		
		/********************************
			Process User Survey
		********************************/
		
		function submitSurveyResponse(birthYear,visitorType,visitorTypeName, visitorTypeOther)
		{
			$.getJSON(userSubmissionURL,
				{"utype":visitorType,"utypeother":visitorTypeOther,"byr":birthYear,"cache" : Math.random()},
				function(r)
				{
					if (r.submitted == 1)
					{
						setVisitorUserSegmentCookie(visitorTypeName);
						userSurvey.close();
					}
					else
					{
						alert(r.message);
					}
					
				}
			);
		}
		
		/**********************************
			Opt-out of Survey
		***********************************/
		$("#btCancelSurvey").click(
		
			function()
			{
				
				setVisitorUserSegmentCookie("Unknown");
			
				$.getJSON(userSubmissionURL,
					{"optout":1,"cache" : Math.random()}
				);
				
				userSurvey.close();
			}
		
		);
		
		/**********************************
			Submit Survey
		***********************************/
		$("#btSubmitUserSurvey").click(
		
			function()
			{
				
				var arrValidation = new Array();
				var visitorBirthYear = "";
				var visitorTypeName = "";
				var visitorTypeId = 0;
				var visitorTypeOther = "";
				var currentYear = parseInt((new Date()).getFullYear());
				
				// VALIDATE VISITOR SURVEY INFORMATION
				
				//*********************
				// VISITOR BIRTH YEAR
				//*********************
				visitorBirthYear = "";
				/*
				visitorBirthYear = jQuery.trim($("#visitorBirthYear").val());
				
				if ( !(/^\d+$/).test(visitorBirthYear) )
				{
					arrValidation[arrValidation.length] = "Invalid birth year provided.";
				}
				else if (parseInt(visitorBirthYear) > currentYear || parseInt(visitorBirthYear) < currentYear - 150)
				{
					arrValidation[arrValidation.length] = "Invalid birth year provided.";
				}
				*/
					
				//**********************
				// VISITOR CATEGORY
				//**********************
				if ($("input[name='visitorType']:checked").length > 0)
				{
					visitorTypeId = $("input[name='visitorType']:checked").val();
					visitorTypeName = $("input[name='visitorType']:checked").next().text();
					
					// Check whether other field provided for selection - if so verify
					// input provided by user
					if ( $("#" + $("input[name='visitorType']:checked").attr("id") + "_other").length > 0 )
					{

						visitorTypeOther = jQuery.trim($("#" + $("input[name='visitorType']:checked").attr("id") + "_other").val())
						
						if (visitorTypeOther == "")
						{
							arrValidation[arrValidation.length] = "Please specify 'Other' category that best describes you."
						}

					}

				}
				else
				{
					arrValidation[arrValidation.length] = "Please indicate the category that best describes you."
				}
				
				// Survey Submission Invalid?
				if (arrValidation.length > 0 )
				{
					DisplayErrorMessage("The following must be addressed before your survey can be submitted:",arrValidation);
					return;
				}
				
				// Submit Survey
				submitSurveyResponse(visitorBirthYear,visitorTypeId,visitorTypeName,visitorTypeOther);
				
			}
			
		);
		
});