function PreventDefault(e)
{
	try
	{
		e.preventDefault();
	}
	catch (Error)
	{
		e.returnValue = false;
	}
}

function NormaliseEvent(e)
{
	return e || window.event;
}

function RemoveChildren(Element)
{
	while (Element.firstChild)
	{
		Element.removeChild(Element.firstChild);
	}
}

function GetByClass(Parent, Tag, ClassValue)
{
	var ReturnValue = [];
	var Elements = Parent.getElementsByTagName(Tag);

	for (var Element in Elements)
	{
		if (Elements[Element].className == ClassValue)
		{
			ReturnValue.push(Elements[Element]);
		}
	}

	return ReturnValue;
}

function GetByType(Parent, Type)
{
	var ReturnValue = [];
	var Elements = Parent.getElementsByTagName("*");

	for (var Element in Elements)
	{
		if (Elements[Element].type == Type)
		{
			ReturnValue.push(Elements[Element]);
		}
	}

	return ReturnValue;
}

function php_urlencode(str)
{
	str = escape(str);
	return str.replace(/[*+\/@~¦]|%20&'/g, function (s)
	{
		switch (s)
		{
			case "*": s = "%2A"; break;
			case "+": s = "%2B"; break;
			case "/": s = "%2F"; break;
			case "@": s = "%40"; break;
			case "~": s = "%7E"; break;
			case "¦": s = "%A6"; break;
			case "%20": s = "+"; break;
			case "&": s = "%26"; break;
			case "'": s = "%27"; break;
		}
		
		return s;
	});
}

function html2text(str)
{ 
	str = str.replace(/\&amp;/g,"&"); 
	str = str.replace(/\&gt;/g,">"); 
	str = str.replace(/\&lt;/g,"<"); 
	str = str.replace(/\&quot;/g,'"'); 
	str = str.replace(/\&#039;/g,"'"); 
	return str; 
}

function ShowPopup(PopupWidth, PopupHeight, ElementID, PopupToHide, TopAdjustment)
{
	var Popup = document.getElementById(ElementID);

	var TransparencyContainer = document.getElementById("TransparencyContainer");
	var SiteWrapper = document.getElementById("SiteWrapper");

	var WindowWidth = GetWidth();
	var WindowHeight = SiteWrapper.offsetHeight;
	
	/* Yet another problem courtesy of IE...
	 * IE7 has a bug in respect of z-index such that, despite giving the
	 * transparency container a lower z-index than the popup, it shows
	 * the transparency container in front of the popup as it is earlier
	 * in the HTML order.  At least I think that's the problem!  A
	 * workaround is to enclose the transparency container in a div that
	 * has position:relative.  Tried this outside of the SiteWrapper div
	 * and it still didn't work.  I therefore placed the transparency
	 * container inside the SiteWrapper div and this solved the problem.
	 * However, as the transparency container's position has to be absolute,
	 * this meant that it's left position starts at the left of the SiteWrapper
	 * rather than at the left border of the browser.  The following therefore
	 * gets the left position of the SiteWrapper and sets the transparency
	 * container's left to the negative of this!
	 */
	var LeftAdjustment = SiteWrapper.offsetLeft;

	var TopMargin = 125;

	if (PopupToHide === undefined)
	{
		HideFlashObjects(true);
	
		TransparencyContainer.className = "Transparent";
		TransparencyContainer.style.width = WindowWidth + "px";
		TransparencyContainer.style.height = WindowHeight + "px";
		TransparencyContainer.style.left = -LeftAdjustment + "px";

		TransparencyContainer.style.display = "block";
	}
	else
	{
		document.getElementById(PopupToHide).style.display = "none";
	}

	if (Popup !== null)
	{
		if (TopAdjustment === undefined)
		{
			window.scrollTo(0,0);
		}
		else
		{
			window.scrollTo(0, TopAdjustment);

			TopMargin += TopAdjustment;
		}

		Popup.style.top = TopMargin + "px";
		Popup.style.width = PopupWidth + "px";
		Popup.style.height = PopupHeight + "px";
		Popup.style.left = Math.floor((SiteWrapper.offsetWidth - PopupWidth - 15) / 2) + "px";

		Popup.style.display = "block";
	}
}

function HidePopup(ElementID, PopupToShow, CleanUpFunction)
{
	var Popup = document.getElementById(ElementID);
	var TransparencyContainer = document.getElementById("TransparencyContainer");

	if (Popup !== null)
	{
		Popup.style.display = "none";
	}

	if (CleanUpFunction !== undefined)
	{
		if (typeof CleanUpFunction == "function")
		{
			CleanUpFunction();
		}
	}

	if (PopupToShow === undefined)
	{
		HideFlashObjects(false);

		TransparencyContainer.style.display = "none";
	}
	else
	{
		document.getElementById(PopupToShow).style.display = "block";
	}
}

function HideFlashObjects(Hide)
{
	var FlashObjects = GetByClass(document, "div", "ChooseListingVideo");

	var MultiLevelMap = document.getElementById("statelocator_map");

	if (MultiLevelMap)
	{
		FlashObjects.push(MultiLevelMap);
	}
	else
	{
		MultiLevelMap = document.getElementById("Map_NetworkGroups");

		if (MultiLevelMap)
		{
			FlashObjects.push(MultiLevelMap);
		}
	}

	for (var Flash in FlashObjects)
	{
		if (Hide)
		{
			FlashObjects[Flash].style.display = "none";
		}
		else
		{
			FlashObjects[Flash].style.display = "block";
		}
	}
	
}

function GetWidth()
{
	var x = 0;

	if (self.innerHeight)
	{
		// Allow for the scroll bar in Chrome, Safari, Firefox and Opera
		x = self.innerWidth - 17;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
	{
		x = document.documentElement.clientWidth;
	}
	else if (document.body)
	{
		x = document.body.clientWidth;
	}

	return x;
}

function GetHeight()
{ 
	var y = 0;

	if (self.innerHeight)
	{
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
	{
		y = document.documentElement.clientHeight;
	}
	else if (document.body)
	{
		y = document.body.clientHeight;
	}

	return y;
}

function IsValidEmail(EmailAddress)
{
	apos = EmailAddress.indexOf("@");
	dotpos = EmailAddress.lastIndexOf(".");

	if (apos < 1 || dotpos-apos < 2)
	{
		return false;
	}
	else
	{
		return true;
	}
}

function ShowWait(Element)
{
	var WaitImage = document.getElementById("Wait");
	var CentreIn = document.getElementById(Element);

	WaitImage.style.left = (CentreIn.offsetLeft +
		Math.floor((CentreIn.offsetWidth - WaitImage.offsetWidth - 30) / 2)) + "px";
	WaitImage.style.top = (CentreIn.offsetTop + 10) + "px";
	WaitImage.style.display = "block";
}

function HideWait()
{
	var WaitImage = document.getElementById("Wait");

	WaitImage.style.display = "none";
}

/*function MLM_AreaClick(PostcodeArea)
{
	var Parameters = "PostcodeArea=" + PostcodeArea;
	var TransparencyContainer = document.getElementById("TransparencyContainer");

	if (TransparencyContainer.style.display != "block")
	{
		var Element = document.getElementById("Map_ListingExtras");

		if (Element !== null)
		{
			PostResults("../utilities/GetPostcodeTownInfo.php", Parameters, ShowPostcodeTownInfo);
		}
		else
		{
			Element = document.getElementById("Map_NetworkGroups");

			if (Element !== null)
			{
				PostResults("../utilities/AddPostcodeAreaToSession.php", Parameters, ShowNetworkListings);
			}
		}
	}
	else
	{
		// Popup with map is being displayed so treat the mouse-click as selecting
		// or unselecting the specified Postcode Area
		PostResults("../utilities/AddPostcodeAreaToSession.php", Parameters, ShowPostcodeAreas);
	}
}

function ShowPostcodeAreas(Response)
{
	var Content = document.getElementById("SelectedPostcodeAreas");
	var ContentHTML = "";
	var Counter = 0;

	if (Content !== null)
	{
		if (Response != "")
		{
			var SelectedPostcodeAreas = Response.split(",");

			if (SelectedPostcodeAreas.length > 0)
			{
				ContentHTML = "<div class='SelectedPostcodeTable'>";

				for (var SelectedArea in SelectedPostcodeAreas)
				{
					if ((Counter % 2) == 0)
					{
						ContentHTML += "<div class='SelectedPostcodeRow_Odd'>";
					}
					else
					{
						ContentHTML += "<div class='SelectedPostcodeRow_Even'>";
					}

					ContentHTML += "<div class='SelectedPostcodeCell'>" + SelectedPostcodeAreas[SelectedArea] + "</div>" +
						"<div class='RemovePostcodeCell'><a href='javascript:MLM_AreaClick(\"" + SelectedPostcodeAreas[SelectedArea] + "\")'>Remove</a></div></div>";

					Counter++;
				}

				ContentHTML += "</div>";
			}
		}

		Content.innerHTML = ContentHTML;

		var OKButton = document.getElementById("PopupOKButton");

		if (OKButton !== null)
		{
			OKButton.disabled = false;
		}
	}
}*/

function ShowPostcodeTownInfo(Response)
{
	if (Response != "")
	{
		var Content = document.getElementById("PostcodeTownInfoContent");

		Content.innerHTML = Response;

		ShowPopup(800, 500, "PostcodeTownInfo");
	}
}

function GetPostcodeTownInfo_Overlay(PostcodeArea)
{
	var Content = document.getElementById("PostcodeTownInfoContent_Overlay");

	if (Content !== null)
	{
		var Parameters = "PostcodeArea=" + PostcodeArea;

		PostResults("../utilities/GetPostcodeTownInfo.php", Parameters, ShowPostcodeTownInfo_Overlay);
	}
}

function ShowPostcodeTownInfo_Overlay(Response)
{
	if (Response != "")
	{
		var Content = document.getElementById("PostcodeTownInfoContent_Overlay");

		Content.innerHTML = Response;

		ShowPopup(800, 500, "PostcodeTownInfo_Overlay", "MultiLevelMap");
	}
}

/*function ConfirmPostcodeAreas(ElementToHide)
{
	// Can't use SelectedPostcodeAreas as a global array to hold these details
	// as it is intialised each time the page is refreshed
	//
	// Instead, re-build the innerHTML of each LI within SelectedPostcodeAreas div
	// This is in the format "B - Towns and Cities" so also need to extract just
	// the postcode area from the HTML
	var SelectedAreas = "";
	var PostcodeArea="";

	var SelectedPostcodes = document.getElementById("SelectedPostcodeAreas");
	var ListAreas = GetByClass(SelectedPostcodes, "div", "SelectedPostcodeCell");

	for (var SelectedArea in ListAreas)
	{
		if (SelectedAreas != "")
		{
			SelectedAreas += ",";
		}

		PostcodeArea = ListAreas[SelectedArea].innerHTML;
		SelectedAreas += PostcodeArea;
	}

	HidePopup(ElementToHide);

	var Parameters = "PostcodeArea=" + SelectedAreas + "&ResetAreas=1";

	var Element = document.getElementById("network_search_controls");

	if (Element !== null)
	{
		PostResults("../utilities/AddPostcodeAreaToSession.php", Parameters, ShowNetworkListings);
	}
}*/

function SetMinimumHeight(FirstElement, SecondElement)
{
	var FirstSection = document.getElementById(FirstElement);
	var SecondSection = document.getElementById(SecondElement);

	if ((FirstSection !== null) && (SecondSection !== null))
	{
		var FirstHeight = FirstSection.offsetHeight;
		var SecondHeight = SecondSection.offsetHeight;

		//alert(FirstHeight);
		//alert(SecondHeight);
		if (FirstHeight > SecondHeight)
		{
			SecondSection.style.height = FirstHeight;
		}
		else if (SecondHeight > FirstHeight)
		{
			FirstSection.style.height = SecondHeight;
		}
		FirstHeight = FirstSection.offsetHeight;
		SecondHeight = SecondSection.offsetHeight;

		//alert(FirstHeight);
		//alert(SecondHeight);
	}
}

function ShowFileName(FullFileName, ShowIn)
{
	FileName = FullFileName.match(/[^\/\\]+$/);

	var FileLabel = document.getElementById(ShowIn);

	FileLabel.innerHTML = FileName;
	FileLabel.title = FileName;
}

function Expand(AnswerNumber, RequiredHeight)
{
	var Animator = null;

	var Mode = null;

	var Div = document.getElementById("Answer_" + AnswerNumber);
	var CurrentHeight = null;

	if (Div.style.display == "none")
	{
		Mode = "Expand";

		Div.style.display = "block";
		Div.style.height = "0px";
	}
	else
	{
		Mode = "Shrink";
	}

	var Animate = function()
	{
		CurrentHeight = parseInt(Div.style.height);

		if (Mode == "Expand")
		{
			if (CurrentHeight <= RequiredHeight)
			{
				Div.style.height = CurrentHeight + 10 + "px";
			}
			else
			{
				clearInterval(Animator);
			}
		}
		else
		{
			if (CurrentHeight > 0)
			{
				Div.style.height = CurrentHeight - 10 + "px";
			}
			else
			{
				clearInterval(Animator);

				Div.style.display = "none";
			}
		}
	}

	Animator = setInterval(Animate, 20);
}

String.prototype.trim = function()
{
	return this.replace(/^\s+|\s+$/g,"");
}

String.prototype.ltrim = function()
{
	return this.replace(/^\s+/,"");
}

String.prototype.rtrim = function()
{
	return this.replace(/\s+$/,"");
}
