function ShowHide(ElementID)
{
	if (document.getElementById(ElementID).style.display == 'block')
		document.getElementById(ElementID).style.display = 'none'
	else
		document.getElementById(ElementID).style.display = 'block'
}


function TryToSubmitComments()
{
	//1.	Ensure entered comments.
	//2.	If entered email, ensure valid format
	//3.	Send via ajax

	//1.
	if (document.CommentForm.CommentsText.value == "")
	{
		ShowPopup("ERROR", "Please enter your comments.");
		FocusOnField(document.CommentForm.CommentsText);
		return false;
	}
	
	//2.
	if (document.CommentForm.CommentsEmail.value != "" && !IsValidEmail(document.CommentForm.CommentsEmail.value))
	{
		ShowPopup("ERROR", "Please enter a valid email address, or leave this field blank.");
		FocusOnField(document.CommentForm.CommentsEmail);
		return false;
	}
	
	//3.
	var Parameters = "SubmitComments=1" +
			 "&Comments=" + escape(document.CommentForm.CommentsText.value) +
			 "&Email=" + escape(document.CommentForm.CommentsEmail.value);
	
	var MyAjax = new AjaxConnection("http://" + location.host + "/ajax/ajax_submit_comments.php", Parameters, "AfterTryToSubmitComments"); 
}


function AfterTryToSubmitComments(xml, text)
{
	ShowHide('CommentsWrapper');
	document.CommentForm.CommentsText.value = "";
	
	ShowPopup("Comments Submitted", "Thank you, your feedback is appreciated.");
}


function SubmitMailingListForm()
{
	//1.	Ensure entered valid e-mail
	//2.	Disable screen
	//3.	Submit form via AJAX
	//    After
	//4.	Show success message.
	
	//1.
	if (!IsValidEmail(document.MailingListForm.email.value))
	{
		ShowPopup("ERROR", "Please enter a valid e-mail address.");
		FocusOnField(document.MailingListForm.email);
		return false;
	}

	//2.
	ShowLoadingPopup();
	
	//3.
	var Parameters = "JoinMailingList=1&EmailAddress=" + document.MailingListForm.email.value;
	var MyAjax = new AjaxConnection("/~hustler/ajax/ajax_join_our_mailing_list.php", Parameters, "AfterSubmitMailingListForm"); 
}


function ShowLoadingPopup()
{
	DisableScreen();
	
	var Title	= "Loading...";
	var Msg		= "<p style='text-align: center;'><img src='/~hustler/images/Hustler/Icon-Loading.gif' alt='Loading' /></p>";

	var PageDimensionsArray = getPageSize();
	var PageWidth = PageDimensionsArray[0];
	var PopupWidth = 432;

	var PageScrollArray = getScrollXY();
	var ScrollY = PageScrollArray[1];

	var ErrorWrapper = document.createElement("div");
	ErrorWrapper.id = "ErrorWrapper";
	ErrorWrapper.style.top = ScrollY + 200 + "px";			//get height automatically
	ErrorWrapper.style.left = (PageWidth-PopupWidth)/2 + "px";

	var ErrorHeader = document.createElement("div");
	ErrorHeader.id = "ErrorHeader";

	var ErrorTitle = document.createElement("div");
	ErrorTitle.id = "ErrorTitle";
	ErrorTitle.innerHTML = Title;

	var ErrorCloseLink = document.createElement("div");
	ErrorCloseLink.id = "ErrorCloseLink";

	var MyLink = document.createElement("a");
	MyLink.id = "Links";
	MyLink.innerHTML = "close";
	MyLink.href = "javascript: HidePopup();";

	var ErrorBodyWrapper = document.createElement("div");
	ErrorBodyWrapper.id = "ErrorBodyWrapper";

	var ErrorBodyText = document.createElement("div");
	ErrorBodyText.id = "ErrorBodyText";
	ErrorBodyText.innerHTML = Msg;

	var ErrorFooter = document.createElement("div");
	ErrorFooter.id = "ErrorFooter";

	ErrorWrapper.appendChild(ErrorHeader);
	ErrorHeader.appendChild(ErrorTitle);
	ErrorHeader.appendChild(ErrorCloseLink);
	ErrorCloseLink.appendChild(MyLink);
	ErrorWrapper.appendChild(ErrorBodyWrapper);
	ErrorBodyWrapper.appendChild(ErrorBodyText);
	ErrorWrapper.appendChild(ErrorFooter);

	document.getElementsByTagName('body')[0].appendChild(ErrorWrapper);		//append to page
}


function AfterSubmitMailingListForm(xml, text)
{
	var ErrorMessage = "";
	
	if (xml.getElementsByTagName('ErrorMessage')[0].childNodes[0])
		ErrorMessage = xml.getElementsByTagName('ErrorMessage')[0].childNodes[0].nodeValue;

	if (ErrorMessage.length <= 0)
	{
		document.getElementById('ErrorTitle').innerHTML		= "Success!";
		document.getElementById('ErrorBodyText').innerHTML	= "Thank you!  You will receive a confirmation e-mail to finalize the sign-up process.";
	}
	else
	{
		document.getElementById('ErrorTitle').innerHTML		= "ERROR";
		document.getElementById('ErrorBodyText').innerHTML	= ErrorMessage;
	}
}


function SetBackgrounds(elem)
{
	//1.	Remove background-color from all elements
	//2.	Add background-color to this element

	//1.
	//ClearBackgrounds();

	//2.
	elem.style.backgroundColor = "#FFFF99";
}


function ClearBackgrounds(MyForm)
{
	for(i = 0; i < MyForm.elements.length; i++)
		MyForm.elements[i].style.backgroundColor = "";
}


function CentreImages()
{
	//1.	Go over the children of "CategoryContentWrapper"
	//2.	If the class is "ProductPreview", align it

	var IMAGE_WRAPPER_WIDTH = 200;
	var MyImage;

	//1.
	for (var i = 0; i < document.getElementById('CategoryContentWrapper').getElementsByTagName('img').length; i++)
	{
		if (document.getElementById('CategoryContentWrapper').getElementsByTagName('img')[i].className == "ProductPreview")
		{
			MyImage = document.getElementById('CategoryContentWrapper').getElementsByTagName('img')[i];

			//2.
			if (MyImage.width > IMAGE_WRAPPER_WIDTH)
			{
				MyImage.style.position = "relative";
				MyImage.style.left = "-" + ((MyImage.width - IMAGE_WRAPPER_WIDTH) / 2) + "px";
			}
		}
	}
}

