
/********** AA common Functionality **********/

	/********** email sign up form **********/
		var FormDivID = '';

		function isValidEmailAddress(formFieldID) {
			return $(formFieldID).val().match(/[\w.-]+@[\w-]+(\.[\w-]+){1,}/)
		}

		function registerEmailCallback(result) {
			if (result.RegistrationMessage !== undefined && result.RegistrationMessage !== null) {
				$(FormDivID).html(result.RegistrationMessage);
			}
		}

		function registerEmail(formFieldID, formDivID) {
			FormDivID = formDivID;
			var text = $(formFieldID).val();
			if (!isValidEmailAddress(formFieldID)) {
				alert("Please enter a valid email address.");
			}
			else {
				$(formDivID).html("<strong>Loading...</strong>");
				SendServiceCall(emailRegistrationServiceAddress, {
					email: text
				}, registerEmailCallback, null, {
					email: text
				});
			}
		}

		function stopEvent(e) {
			if (!e) {
				e = window.event;
			}
			e.cancelBubble = true;
			if (e.stopPropagation) {
				e.stopPropagation();
			}
		}

		function emailAddressOnKeyDown(e, formFieldID, formDivID) {

			var key;
			if (window.event) {
				key = window.event.keyCode; //IE
			}
			else {
				key = e.which; //firefox
			}

			if (key == 13) {
				if (!isValidEmailAddress(formFieldID)) {
					alert("Please enter a valid email address.");
					return false;
				}
				else {
					registerEmail(formFieldID, formDivID);
					return true;
				}
			}
			else {
				return true;
			}
		}

	/********** END email sign up form **********/


	/********** view all departments *********/

		// Expand and hide the view all departments menu on the new category page
		function ToggleViewAll() {
			// load view all if not already loaded
			if ($("div#leftNavPopoutMenu div#popoutMenu").html().length == 0) {
				var leftNavContent = CategoryNavigationWebService.GetViewAllMenuLinks(GetViewAllMenuLinksCallback, GetViewAllMenuLinksOnError);
			}
			else {
				TogglePopupForm("leftNavPopoutMenu");
			}
		}

		function GetViewAllMenuLinksCallback(result) {
			var cols = 4;
			var rows;

			var html = "";
			if (result !== null) {
				for (var i in result) {
					rows = Math.ceil(result[i].Children.length / cols);

					html = html + "<div class=\"department\"><p class=\"imageHeading\"><img src=\"" + ImagePath + result[i].DepartmentMediaFilename + "\" alt=\"" + ImagePath + result[i].DepartmentMediaAltTag + "\"</p>";

					var k = 0;
					for (var j in result[i].Children) {
						if (k === 0) {
							html = html + "<ul>";
						}

						html = html + "<li><a rel=\"nofollow\" href=\"" + result[i].Children[j].Url + "\" title=\"" + result[i].Children[j].DisplayName + "\">" + result[i].Children[j].DisplayName + "</a></li>";

						//					if (rows === 0)
						//					{
						//						k = 0;
						//					}
						//					else
						//					{
						//						k = (k + 1) % rows;
						//					}

						k = (k + 1) % rows;

						if (k === 0) {
							html = html + "</ul>";
						}
					}

					html = html + "</div>";
				}
				$("div#leftNavPopoutMenu #popoutMenu").html(html);
			}
			TogglePopupForm("leftNavPopoutMenu");
		}

		function GetViewAllMenuLinksOnError() { }

	/********** view all departments *********/
	
	

	/********** Global Helper Functions **********/
	
		function SendServiceCall(url,data,successDelegate,failedDelegate,userContext) {

			$.ajax({
				type: "POST",
				contentType: "application/json; charset=utf-8",
				url: url,
				context: userContext,
				data: JSON.stringify(data),
				dataType: "json",
				error: function (response)
				{
					ToggleNotificationPopup('Error', 'There was a problem processing your request, please refresh the browser and try again');
				},
				success: function (response)
				{
					if (response !== undefined && response !== null)
					{
						if (response.d !== undefined && response.d !== null)
						{
							if (response.d.IsSuccessful === true)
							{
								if (successDelegate !== undefined && successDelegate !== null)
								{
									successDelegate(response.d, this);
								}
								else
								{
									ToggleNotificationPopup(response.d.Messaging.Title, response.d.Messaging.Content);
								}
							}
							else
							{
								if (failedDelegate !== undefined && failedDelegate !== null)
								{
									failedDelegate(response.d, this);
								}
								else
								{
									ToggleNotificationPopup(response.d.Messaging.Title, response.d.Messaging.Content);
								}
							}
						}
					}
				}
			});
		}

		function goToPage(obj) {
			if (obj.value != "") {
				location.href = obj.value;
			}
		}
		
		function actionOnEnterKeyDown(e, enterFunction)
		{
			//handle the enter key on the email registration textbox
			var key;

			if(window.event)
			{
				 key = window.event.keyCode;    //IE
			}
			else
			{
				 key = e.which;                 //firefox
			}
	
			if (key == 13)
			{
				enterFunction();
				return false;
			}
			return true;
		}

		function actionOnEscKeyDown(e, enterFunction)
		{
			//handle the enter key on the email registration textbox
			var key;

			if(window.event)
			{
				 key = window.event.keyCode;    //IE
			}
			else
			{
				 key = e.which;                 //firefox
			}
	
			if (key == 27)
			{
				enterFunction();
				return false;
			}
			return true;
		}

		// Flags fields with yellow background and red border
		function flagFormField(formField)
		{
			formField.style["border"] = "1px solid red";
			formField.style["background"] = "#FFFCCB";
		}

		// Clears alert style from form fields
		function resetFormField(formField)
		{
			if( formField.type != "checkbox" )
			{
				formField.style["border"] = "1px solid #ccc";
				formField.style["background"] = "#fff";
			}
		}

		// trims spaces of a form field
		function trimSpaces(formField)
		{
			formField.value = formField.value.replace(/\s/g, "");
		}



	/********** END Global Helper Functions **********/

/********** End AA Common Funcitonality *********/
































