﻿var CONTROL_ERROR_COLOUR = '#ffaaaa';
var CONTROL_NONERROR_COLOUR = '#ffffff';

var m_googleTrackingID = "";
var m_googlePageTracker = null;
var m_basePath = "";
var m_absBasePath = "";
var m_prm = null;
var m_enterCheck_clicked = false;

var m_glossaryDefinitions = []; // new Array(); // to store the list of retrieved definitions.
var m_glossaryTitles = []; //new Array(); // to store the list of retrieved Titles.
var m_glossaryTerms = []; //new Array(); // to store the list of retrieved terms.
var m_glossaryMouseOutTimer = 0;
var m_glossaryMouseOverTimer = 0;
var m_glossaryTermLink = null;
var m_glossaryTermFadeTimeout = 500;

/////////////////////////////////////////////////////////////////////////////////////////////

function DisableAnchors()
{
	// Get anchors with a disabled class
	var disabledAnchors = $("a[class*=disabled]");

	if (disabledAnchors.length > 0)
	{
		disabledAnchors.attr("href", "javascript:alert('Please select the options.');");
	}
}

function ShowLoadingImage(sender, args)
{
	var img = $("img[id*='imgSmallLoading']:first");
	var control = $(args.get_postBackElement());

	if (args.get_postBackElement().id.endsWith("_btnFind"))
	{
		control = $(control.siblings("input")[0]);
	}

	if (control.attr("type") == "select-one" || control.attr("id").match("conAddressLookup"))
	{
		img.css("top", control.offset().top);
		img.css("left", control.offset().left - 25);
		img.show();
	}
}

/////////////////////////////////////////////////////////////////////////////////////////////
// Glossary
/////////////////////////////////////////////////////////////////////////////////////////////

function viewport()
{
	return {
		x: $(window).scrollLeft() + $(window).width(),
		y: $(window).scrollTop() + $(window).height()
	};
}

function GlossaryTerm_Hide()
{
	var tooltip = $("#glossary_tooltip");
	tooltip.fadeOut(250);
}
function GlossaryTerm_Position(link)
{
	var v = viewport(); //, link = $(obj);
	var left = link.position().left + link.width() + 10;
	var top = link.position().top + link.height() + 5;

	var tooltip = $("#glossary_tooltip");
	tooltip.css("top", top + "px");
	tooltip.css("left", left + "px");

	// check horizontal position
	if (v.x < tooltip.offset().left + tooltip.width())
	{
		left = left - ((tooltip.offset().left + tooltip.width()) - v.x + 20); //tooltip.width() + 20 + tooltip.position().left;

	}
	// check vertical position
	if (v.y < tooltip.offset().top + tooltip.height())
	{
		top = top - ((tooltip.offset().top + tooltip.height()) - v.y + 20);   //(v.y -( tooltip.position().top + tooltip.height()))//tooltip.height() + 20 + tooltip.position().top;
	}


	tooltip.css("top", top + "px");
	tooltip.css("left", left + "px");
}

function GlossaryTerm_Show(def, title, anchor)
{
	var tooltip = $("#glossary_tooltip");
	tooltip.css("display", "inline");
	tooltip.width(200);
	tooltip.height("auto");
	$("#glossary_term_close").show();
	$("#glossary_definition").html(def);
	$("img[id*=glossary_loading]").hide();
	$("#glossary_term").text(title);
	GlossaryTerm_Position(anchor);
}
function GlossaryTerm_Loaded(data, term, anchor)
{
	var def = "";
	var title = term;

	var termRegex = /<!--TERMBEGIN-->([\w\W\r\n]{1,})(?=<!--TERMEND-->)/;
	var synRegex = /<!--SYNONYMSBEGIN-->([\w\W\r\n]{1,})(?=<!--SYNONYMSEND-->)/;
	var match = termRegex.exec(data);
	if (match != null && match.length > 1)
	{
		def = match[1];
		if (def.indexOf("<!--TERMEND-->") > -1)
		{
			def = def.substring(0, def.indexOf("<!--TERMEND-->"));
		}
	}

	match = synRegex.exec(data);
	if (match != null && match.length > 1)
	{
		title = match[1];
		if (title.indexOf("<!--SYNONYMSEND-->") > -1)
		{
			title = title.substring(0, title.indexOf("<!--SYNONYMSEND-->"));
		}
	}

	if (def != "")
	{
		m_glossaryDefinitions.push(def);
		m_glossaryTitles.push(title);
		m_glossaryTerms.push(term);
		//alert(def);
		GlossaryTerm_Show(def, title, anchor);
	}
}

function GlossaryTerm_ShowDiv()
{
	m_glossaryMouseOverTimer = 0;
	if (m_glossaryTermLink == null)
	{
		return;
	}

	var anchor = $(m_glossaryTermLink);
	var tooltip = $("#glossary_tooltip");
	$("#glossary_term_close").hide();
	tooltip.css("visibility", "hidden");
	tooltip.css("display", "block");

	GlossaryTerm_Position(anchor);

	tooltip.css("visibility", "visible");
	tooltip.css("display", "none");

	var term = m_glossaryTermLink.href;

	if (term.indexOf("javascript") > -1)
	{
		term = term.substring(term.indexOf("='") + 2);
		term = term.substring(0, term.indexOf(";void") - 1);
	}
	var href = term;

	term = term.substring(term.lastIndexOf("=") + 1);

	$("#glossary_term").text(term.replace("+", " "));

	tooltip.fadeIn(250);

	var index = $.inArray(term, m_glossaryTerms);
	if (index == -1)
	{
		$("img[id*=glossary_loading]").show();
		$.get(href, null,
						  function (data)
						  {
							  GlossaryTerm_Loaded(data, term, anchor);
						  }, "html");
	}
	else
	{
		var def = m_glossaryDefinitions[index];
		var title = m_glossaryTitles[index];

		GlossaryTerm_Show(def, title, anchor);
	}
}

function InitAjaxRequest(sender, args)
{
	if (Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack() && !m_enterCheck_clicked)
	{
		alert('Please wait for the current request to complete.');
		args.set_cancel(true);
	}
}

function EndAjaxRequest(sender, args)
{
	DisableAnchors();
	m_enterCheck_clicked = false;
}
function HideLoadingImage(sender, args)
{
	var img = $("img[id*='imgSmallLoading']:first");
	img.hide();
}
function InitialiseLoadingImage()
{
	var prm = Sys.WebForms.PageRequestManager.getInstance();

	prm.add_initializeRequest(ShowLoadingImage);
	prm.add_endRequest(HideLoadingImage);
}





function GlossaryTerm_MouseOver(e)
{
	if (m_glossaryMouseOutTimer > 0)
	{
		window.clearTimeout(m_glossaryMouseOutTimer);
	}
	if (m_glossaryMouseOverTimer > 0)
	{
		window.clearTimeout(m_glossaryMouseOverTimer);
		m_glossaryTermLink = null;
	}
	m_glossaryTermLink = e.target;
	m_glossaryMouseOverTimer = window.setTimeout(GlossaryTerm_ShowDiv, 500);
}

function GlossaryTerm_MouseOut(e)
{
	if (m_glossaryMouseOverTimer > 0)
	{
		window.clearTimeout(m_glossaryMouseOverTimer);
	}
	else
	{
		m_glossaryMouseOutTimer = window.setTimeout("GlossaryTerm_Hide()", m_glossaryTermFadeTimeout);
	}
}

function GlossaryTermInit()
{
	var anchors = $("a[class*=glossary_term]");

	anchors.mouseover(GlossaryTerm_MouseOver);
	anchors.mouseout(GlossaryTerm_MouseOut);
}

function PageInitiate()
{
	m_prm = Sys.WebForms.PageRequestManager.getInstance();

	m_prm.add_initializeRequest(InitAjaxRequest);
	m_prm.add_endRequest(EndAjaxRequest);

	DisableAnchors();

	GlossaryTermInit();
}




function SearchBoxFadeIn()
{
	$("#divSearchAgain").fadeIn(1000);
}

function AutoSubmit_Handler(control, postbackcontrol)
{
	if (event.keyCode == 13)
	{
		__doPostBack(postbackcontrol, '');
	}
}

function AdvertClick_Handler(advert_id_string)
{
	window.open(m_basePath + "res/AdvertHandler.ashx?" + advert_id_string);
}



/////////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////////

var _flashids = 0;

function SquirtFlash(moviepath, width, height, flashVars, id, onmouseover, onmouseout)
{
	var objectId = "";

	var windowobject = window;

	if (id == null || "" == id)
	{
		_flashids++;
		objectId = "FlashObject_" + _flashids;
	}
	else
	{
		objectId = id;
	}

	if (onmouseover == null) { onmouseover = ""; }
	if (onmouseout == null) { onmouseout = ""; }
	if (flashVars == null) { flashVars = ""; }

	windowobject.document.write("<object name=\"" + id + "\" onmouseout=\"" + onmouseout + "\" onmouseover=\"" + onmouseover + "\" id=\"" + objectId + "\" classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"" + (document.location.protocol) + "//download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0\" width=\"" + width + "\" height=\"" + height + "\"> \n");
	windowobject.document.write("	<param name=\"movie\" value=\"" + moviepath + "\"> \n");
	windowobject.document.write("	<param name=\"quality\" value=\"high\"> \n");
	windowobject.document.write("	<param name=\"FlashVars\" value=\"" + flashVars + "\"> \n");

	//windowobject.document.write("	<param name=\"wmode\" value=\"transparent\" > \n");
	windowobject.document.write("	<embed name=\"" + id + "\" FlashVars=\"" + flashVars + "\" onmouseout=\"" + onmouseout + "\" onmouseover=\"" + onmouseover + "\" src=\"" + moviepath + "\" quality=\"high\" pluginspage=\"" + (document.location.protocol) + "//www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" width=\"" + width + "\" height=\"" + height + "\" />\n");
	windowobject.document.write("</object> \n");
}

function getFlashMovieObject(movieName)
{
	if (window.document[movieName])
	{
		return window.document[movieName];
	}
	if (navigator.appName.indexOf("Microsoft Internet") == -1)
	{
		if (document.embeds && document.embeds[movieName])
		{
			return document.embeds[movieName];
		}
	}
	else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
	{
		return document.getElementById(movieName);
	}
}
$().ready(PageInitiate);


///
///   GENERIC METHODS
///
var windowsOpen = []; // new Array();
var newWindowHtml = '#OPENBRACE#html#CLOSEBRACE##OPENBRACE#head#CLOSEBRACE##OPENBRACE#title#CLOSEBRACE#Loading...#OPENBRACE#/title#CLOSEBRACE##OPENBRACE#/head#CLOSEBRACE##OPENBRACE#body style="overflow:hidden;width:100%;height:100%;text-align:center;line-height:##LINEHEIGHT##;"#CLOSEBRACE##OPENBRACE#h1 style="background:url(##ABSPATH##res/images/default/loading_300x300_t.gif) no-repeat center center;margin-top:auto;margin-bottom:auto;font-family:Arial;"#CLOSEBRACE#Loading...#OPENBRACE#/h1#CLOSEBRACE##OPENBRACE#/body#CLOSEBRACE##OPENBRACE#/html#CLOSEBRACE#';

function CorrectCentering(wind, height, width)
{

	if (height < 0) // AutoHeight - height becomes margin from top of the screen.
	{
		height = window.screen.height - (Math.abs(height) * 2);
	}
	if (width < 0) // AutoWidth - width becomes margin from left of the screen.
	{
		width = window.screen.width - (Math.abs(width) * 2);
	}
	wind.resizeTo(width, height);

	var windowHeight = wind.outerHeight;
	var windowWidth = wind.outerWidth;

	if (window.outerHeight === undefined)
	{
		windowHeight = height;
		windowWidth = width;
	}

	var left = (window.screen.width / 2) - (windowWidth / 2);
	var top = (window.screen.height / 2) - (windowHeight / 2);

	wind.moveTo(left, top);

	return [width, height];
}

function PopupOpen(URL, popName, width, height, centred)
{
	if (width == "" || width == null) { width = 920; }
	if (height == "" || height == null) { height = 500; }

	var top = 15, left = 15;

	if (centred)
	{
		if (width > 0)
		{
			left = (window.screen.width / 2) - (width / 2);
		}
		if (height > 0)
		{
			top = (window.screen.height / 2) - (height / 2);
		}
	}

	var features =
	'directories=0' +
	',location=0' +
	',menubar=0' +
	',scrollbars=1' +
	',status=0' +
	',toolbar=0' +
	',resizable=1' +
	',width=' + (width < 0 ? 1024 : width) +
	',height=' + (height < 0 ? 768 : height) +
	',screenX=' + left +
	',screenY=' + top +
	',top=' + top +
	',left=' + left;

	URL = URL.replace(/\s/, '%20');


	for (var x = 0; x < windowsOpen.length; x++)
	{
		try
		{
			if (windowsOpen[x].name == popName)
			{
				if (!windowsOpen[x].closed)
				{
					windowsOpen[x].blur();
					windowsOpen[x].focus();
					if (centred)
					{
						CorrectCentering(windowsOpen[x], height, width);
					}
					return;
				}
				else
				{
					windowsOpen.pop(windowsOpen[x]);
				}
				break;
			}
		} catch (ex)
		{ windowsOpen.pop(windowsOpen[x]); }

	}

	wind = window.open('', popName, features);
	if (centred)
	{
		var returned = CorrectCentering(wind, height, width);
		width = returned[0];
		height = returned[1];
	}
	wind.document.write(newWindowHtml.replace(/##LINEHEIGHT##/g, (height - 50) + "px").replace(/##ABSPATH##/g, m_absBasePath).replace(/#OPENBRACE#/g, "<").replace(/#CLOSEBRACE#/g, ">"));

	wind.setTimeout(function () { wind.document.location.href = URL; }, 250);

	wind.name = popName;

	windowsOpen.push(wind);
	wind.focus();

	return wind;
}


function GoTo(url)
{
	top.document.location.href = url;
}


var m_openDialog = null;
var m_modalDiv = null;

function DialogClose()
{
	if (m_openDialog == null)
	{
		return;
	}
	else
	{
		m_openDialog.fadeOut(250);
		m_modalDiv.fadeOut(250);
		window.setTimeout(function () { m_openDialog = null; m_modalDiv = null; }, 250);
	}
}

function _dialogReposition(obj, e)
{
	var ie6 = false;
	if ($.browser.msie && $.browser.version < 7)
	{
		ie6 = true;
	}

	if (m_openDialog == null)
	{
		return;
	}
	else
	{
		try
		{
			//Get the window height and width
			var winH = $(window).height();
			var winW = $(window).width();

			var top = (winH / 2 - m_openDialog.height() / 2); // - $(window).scrollTop();
			var left = (winW / 2 - m_openDialog.width() / 2); // - $(window).scrollLeft()

			if (ie6)
			{
				top = top + $(window).scrollTop();
				left = left + $(window).scrollLeft();
			}

			m_openDialog.css('top', top);
			m_openDialog.css('left', left);

			m_modalDiv.css("width", $(window).width());
			m_modalDiv.css("height", $(window).height());

			if (ie6)
			{
				m_modalDiv.css('top', $(window).scrollTop());
				m_modalDiv.css('left', $(window).scrollLeft());
			}
		}
		catch (ex) { }
	}
}

function DialogOpen(selector, dialogClass, titleText, closeText, closeMethod, modalOpacity)
{
	if (m_openDialog != null)
	{
		DialogClose();
		window.setTimeout(function () { DialogOpen(selector, dialogClass, titleText, closeText, closeMethod, modalOpacity); }, 251);
		return;
	}

	var ie6 = false;
	var pos = "fixed";

	if ($.browser.msie && $.browser.version < 7)
	{
		ie6 = true;
	}

	if (ie6)
	{
		pos = "absolute";
	}

	m_openDialog = $(selector);

	if (m_openDialog.length === 0)
	{
		m_openDialog = null;
		return;
	}

	m_modalDiv = $(".dialog-modal");
	if (m_modalDiv.length === 0)
	{
		m_modalDiv = $("<div/>");
		m_modalDiv.addClass("dialog-modal");
		m_modalDiv.hide();
		$(document.body).append(m_modalDiv);
	}

	m_openDialog.css("position", pos);
	m_openDialog.addClass(dialogClass == null ? "dialog-div" : dialogClass);
	m_openDialog.css("z-index", "1002");

	m_modalDiv.css("position", pos);
	m_modalDiv.css("z-index", "1001");
	m_modalDiv.css("top", "0px");
	m_modalDiv.css("left", "0px");

	m_modalDiv.css("width", $(window).width());
	m_modalDiv.css("height", $(window).height());

	var titleBarDiv = m_openDialog.children(".dialog-titlebar");
	if (titleBarDiv.length === 0)
	{
		titleBarDiv = $("<div/>");
		titleBarDiv.addClass("dialog-titlebar");
		m_openDialog.prepend(titleBarDiv);

		$("<span/>").addClass("dialog-title").appendTo(titleBarDiv);

		if (closeText != null)
		{
			$("<a/>").addClass("dialog-closebutton").appendTo(titleBarDiv);
		}
	}

	var titleSpan = titleBarDiv.children("span.dialog-title");
	var closeButton = titleBarDiv.children("a.dialog-closebutton");

	titleSpan.text(titleText == null ? "" : titleText);

	if (closeText != null)
	{
		if (closeMethod == null)
		{
			closeButton.click(DialogClose);
		}
		else
		{
			closeButton.click(closeMethod);
		}

		closeButton.text(closeText);
	}

	m_openDialog.css("visibility", "hidden");
	m_openDialog.show();

	_dialogReposition();

	m_openDialog.hide();
	m_openDialog.css("visibility", "visible");

	m_modalDiv.show();
	m_modalDiv.css("opacity", 0);
	m_modalDiv.fadeTo(0, modalOpacity == null ? 0.80 : modalOpacity);

	m_openDialog.fadeIn(250);

	$(window).resize(function (e) { _dialogReposition(this, e); });

	if (ie6)
	{
		$(window).scroll(function (e) { _dialogReposition(this, e); });
	}
}







function EnterCheck_KeyDownHandler(e, id)
{
	var code;
	if (!e)
	{
		e = window.event;
	}

	if (e.keyCode) { code = e.keyCode; }
	else if (e.which) { code = e.which; }

	var character = String.fromCharCode(code);

	if ((code == 13 || code == 10) && !m_enterCheck_clicked)
	{
		m_enterCheck_clicked = true;
		__doPostBack(id, '');
		return false;
	}
}


function CommentsValidate(id, badClass)
{
	var useStyle = false;
	if (badClass == typeof (undefined) || badClass == '')
	{
		useStyle = true;
	}

	if (useStyle)
	{
		$("#" + id + " textarea,:text:not([class*=optional])").filter("[value!='']").css("background-color", CONTROL_NONERROR_COLOUR);
	}
	else
	{
		$("#" + id + " textarea,:text:not([class*=optional])").removeClass(badClass);
	}

	var controls = $("#" + id + " textarea,:text:not([class*=optional])").filter("[value='']");

	if (controls.length > 0)
	{
		if (useStyle)
		{
			controls.css("background-color", CONTROL_ERROR_COLOUR);
		}
		else
		{
			controls.addClass(badClass);
		}

		controls[0].focus();

		alert("Please fill in the required fields.");

		return false;
	}
	else
	{
		return true;
	}

}

function CommentSubmitted(id)
{
	alert("Thanks for your comment! It will appear on the site once it's been approved by a moderator.");
}

newWindowHtml = newWindowHtml.replace("#OPENBRACE#", "<").replace("#CLOSEBRACE#", ">");