﻿function ContextPopup()
{
	var THIS = this;
	if (BrowserDetect.browser == "Explorer")
		document.onclick = Window_Click;
	else
		window.onclick = Window_Click;

	var objPopup = null;
	this.OpenPopup = null;
	this.CurrentElement = null;

	var intCloseHandle = 0;
	var intCloseWaitHandle = 0;
	var divPopup = null;

	this.OpenOpacity = 100;
	this.Opacity = this.OpenOpacity;

	this.AutoFadeOut = true;
	this.AutoFadeOutDelay = 1000;
	this.AutoFadeOutSpeed = 10;

	var strLastContext = null;
	var strLastKey = null;

	//====================================================
	// An element on the window has been clicked
	//====================================================
	function Window_Click(evt)
	{
		try
		{
			evt = window.event ? window.event : evt;
			var target = evt.srcElement ? evt.srcElement : evt.target;
			var strTagName = target.tagName;
			var elmParent = target.parentElement ? target.parentElement : target.parentNode;
			if (evt.ctrlKey)
			{
				strLastContext = null;
				strLastKey = null;
			}
			
			if (elmParent == THIS.OpenPopup)
				return;
			
			if (strTagName != "ABBR")
			{
				hidePopup(true);
				return;
			}
			
			hidePopup();

			var strContext = target.getAttribute("context");

			if (strContext == "" || strContext == null)
				return;
				
			if(!LeavingPage(null,0))
			return;

			THIS.CurrentElement = target;

			evt.cancelBubble = true;
			evt.returnValue = false;
			if (evt.stopPropagation)
				evt.stopPropagation();

			strContext = strContext.toLowerCase();
			var strKey = target.innerHTML;

			try
			{
				var strEval = strContext.toLowerCase() + "_Prepare(strKey, target, evt, strContext)";
				var strNewKey = eval(strEval);

				if (strNewKey)
					strKey = strNewKey;
				else
					return;
			}
			catch (exc) { }

			getPopup(strContext, evt, strKey);
		}
		catch (exc)
		{
			HtmlPopup.Error(exc);
		}
		return false;
	}

	//====================================================
	// Prepare the key for a company
	//====================================================
	function company_Prepare(strKey, elmAbbr, evt, strContext)
	{
		try
		{
			strKey = strKey.replace(/&amp;/g, "&");

			var strCheck = strKey.toLowerCase();
			var strOnContractFrom = " (on contract from ";
			var intContractFrom = strCheck.indexOf(strOnContractFrom);

			var blnShowContractFromOption = true;

			if (intContractFrom != -1)
			{
				var strOperator = strKey.substring(0, intContractFrom);
				strOperator = strOperator.trim();

				if (blnShowContractFromOption)
				{
					var strContractCompany = strKey.substring(intContractFrom + strOnContractFrom.length, strKey.length - 1);
					strContractCompany = strContractCompany.trim();

					var objButtons = new Object();
					objButtons["Close"] = "CLOSE";

					var objPopup = HtmlPopup.Confirm("This vehicle is operated by <b><abbr context=\"" + strContext + "\">" + strOperator + "</abbr></b>.\nBut is on contract from <b><abbr context=\"" + strContext + "\">" + strContractCompany + "</abbr></b>.\n\nClick on the name of the company for the options", objButtons);
					return null;
				}
				else
					return strOperator;
			}

			return strKey;
		}
		catch (exc)
		{
			HtmlPopup.Error(exc);
			return strKey;
		}
	}

	//====================================================
	// Get/Create a popup for the given context
	//====================================================
	function hidePopup(blnForce)
	{
		try
		{
			if (intCloseHandle != 0)
			{
				window.clearInterval(intCloseHandle);
				intCloseHandle = 0;
			}

			if (intCloseWaitHandle != 0)
			{
				window.clearTimeout(intCloseWaitHandle);
				intCloseWaitHandle = 0;
			}

			THIS.Opacity = THIS.OpenOpacity;

			if (objPopup != null)
				SetOpacity();

			if ((blnForce || THIS.OpenPopup != null) && objPopup != null)
				objPopup.style.display = "";

			if (THIS.OpenPopup == null)
				return;

			THIS.OpenPopup = null;
			THIS.CurrentElement = null;
		}
		catch (exc)
		{
			HtmlPopup.Error(exc);
		}
	}

	//====================================================
	// Get/Create a popup for the given context
	//====================================================
	function getPopup(strContext, evt, strKey)
	{
		try
		{
			var intLeft = evt.clientX;
			var intTop = evt.clientY;

			THIS.Open(strContext, strKey, intLeft, intTop);
		}
		catch (exc)
		{
			HtmlPopup.Error(exc);
		}
	}

	//====================================================
	// Manually open the popup
	//====================================================
	this.Open = function Open(strContext, strKey, intLeft, intTop, blnForce)
	{
		try
		{
			if (arguments.length <= 4 || !blnForce)
			{
				if (!strContext || strContext == "")
					throw new Error("Context must be supplied");

				if (!strKey || strKey == "")
					throw new Error("Key must be supplied");
			}

			if (!objPopup)
			{
				objPopup = document.createElement("DIV");
				objPopup.className = "contextPopup";

				divPopup = document.createElement("DIV");
				objPopup.appendChild(divPopup);

				if (objPopup.attachEvent)
				{
					objPopup.attachEvent("onclick", objPopup_Click);
					if (this.AutoFadeOut)
					{
						objPopup.attachEvent("onmouseout", objPopup_MouseOut);
						objPopup.attachEvent("onmouseover", objPopup_MouseOver);
					}
				}
				else
				{
					objPopup.addEventListener("click", objPopup_Click, false);
					if (this.AutoFadeOut)
					{
						objPopup.addEventListener("mouseout", objPopup_MouseOut, false);
						objPopup.addEventListener("mouseover", objPopup_MouseOver, false);
					}
				}

				document.forms[0].appendChild(objPopup);
			}
			else
			{
				if (strContext == strLastContext && strKey == strLastKey)
				{
					objPopup.style.left = intLeft + "px";
					objPopup.style.top = intTop + "px";

					if (divPopup != null)
						divPopup.className = "";

					objPopup.style.display = "block";

					return;
				}
			}

			strLastContext = strContext;
			strLastKey = strKey;

			hidePopup();

			divPopup.innerHTML = "Please wait...";
			divPopup.className = "loading";

			if (strKey != "" && strContext != "" && strKey != null && strContext != null)
			{
				var params = new SOAPClientParameters();
				params.add("strContext", strContext);
				params.add("strKey", strKey);

				SOAPClient.invoke("/webservices/common.asmx", "ContextPopup_Select", params, true, DisplayPopupOptions, strKey);
			}

			objPopup.style.left = intLeft + "px";
			objPopup.style.top = intTop + "px";
			objPopup.style.display = "block";
		}
		catch (exc)
		{
			HtmlPopup.Error(exc);
		}
	}

	//====================================================
	// The mouse has left the popup
	//====================================================
	function objPopup_MouseOut(evt)
	{
		try
		{
			evt = window.event ? window.event : evt;

			intCloseWaitHandle = window.setTimeout(StartFade, THIS.AutoFadeOutDelay);
		}
		catch (exc)
		{
			HtmlPopup.Error(exc);
		}
	}

	//====================================================
	// Start the fade of the popup
	//====================================================
	function StartFade()
	{
		try
		{
			THIS.Opacity -= THIS.AutoFadeOutSpeed;

			if (intCloseHandle == 0)
			{
				intCloseHandle = window.setInterval(StartFade, 30);
				intCloseWaitHandle = 0;
			}

			if (THIS.Opacity <= 0)
			{
				hidePopup(true); //and clear the timers
				return;
			}

			SetOpacity();
		}
		catch (exc)
		{
			HtmlPopup.Error(exc);
		}
	}

	//====================================================
	// Set the opacity
	//====================================================
	function SetOpacity()
	{
		if (objPopup != null)
		{
			objPopup.style.opacity = (THIS.Opacity / 100);
			if (THIS.Opacity >= 100)
				objPopup.style.filter = "";
			else
				objPopup.style.filter = "alpha(opacity=" + THIS.Opacity + ")";
		}
	}

	//====================================================
	// The mouse has entered the popup
	//====================================================
	function objPopup_MouseOver(evt)
	{
		try
		{
			evt = window.event ? window.event : evt;

			if (intCloseHandle != 0)
			{
				window.clearInterval(intCloseHandle);
				intCloseHandle = 0;
			}

			if (intCloseWaitHandle != 0)
			{
				window.clearTimeout(intCloseWaitHandle);
				intCloseWaitHandle = 0;
			}

			THIS.Opacity = THIS.OpenOpacity;

			if (objPopup != null)
				SetOpacity();		
		}
		catch (exc)
		{
			HtmlPopup.Error(exc);
		}
	}

	//====================================================
	// The popup has been clicked
	//====================================================
	function objPopup_Click(evt)
	{
		//try
		//{
			evt = window.event ? window.event : evt;
			var target = evt.srcElement ? evt.srcElement : evt.target;
			
			if (!target || target.tagName != "SPAN")
			{
				if (target.tagName == "DIV")
					hidePopup();
				
				return;
			}

			var strTarget = target.getAttribute("target");

			if (strTarget == null || strTarget == "")
				return;

			hidePopup();

			// Check if an execute command exists.
			if (strTarget.indexOf("exec:") > -1)
			{
				eval(strTarget.replace('exec:', ''));
				return;
			}

			if (strTarget.indexOf("smart:") == 0)
			{
				var strSearch = strTarget.substring(6, strTarget.length);
				SmartSearch.Create().Find(strSearch, false);
				return;
			}

			if (evt.ctrlKey && (strTarget.indexOf("P:") == -1 && strTarget.indexOf("W:") == -1))
			{
				var objWindow = window.open(strTarget);
				if (objWindow || (!objWindow && !confirm("The page could not be opened.\nA popup blocker is stopping the page from opening.\nWould you like to navigate to this page?")))
					return;
			}

			var strPopupName = target.innerHTML;
			strPopupName = strPopupName.replace(/:/g, "");
			strPopupName = strPopupName.replace(/\\/g, "");
			strPopupName = strPopupName.replace(/\//g, "");
			strPopupName = strPopupName.replace(/ /g, "");
			strPopupName = strPopupName.replace(/;/g, "");
			strPopupName = strPopupName.replace(/&/g, "");
			strPopupName = strPopupName.replace(/\?/g, "");

			if (strTarget.indexOf("P:") == 0)
			{
				var strURL = strTarget.substring(2, strTarget.length);
				try
				{
					OpenPopup(strURL, strPopupName, "width=900,height=800,resizable=1,scrollbar=1", "iframe=1");
				}
				catch (exc)
				{
					throw new Error("Could not open popup '" + strPopupName + "'\nURL = " + strURL + "\n" + exc.message);
				}
			}
			else if (strTarget.indexOf("W:") == 0)
			{
				var strURL = strTarget.substring(2, strTarget.length);
				try
				{
					OpenPopup(strURL, strPopupName, "width=900,height=800,resizable=1,scrollbar=1", "iframe=0");
				}
				catch (exc)
				{
					throw new Error("Could not open popup '" + strPopupName + "'\nURL = " + strURL + "\n" + exc.message);
				}
			}
			else if (strTarget.indexOf("O:") == 0)
			{
				var strURL = strTarget.substring(2, strTarget.length);
				
				try
				{
					OpenWindow(strURL, "", "width=900,height=800,resizable=1,scrollbar=1");
				}
				catch (exc)
				{
					throw new Error("Could not open window '" + strPopupName + "'\nURL = " + strURL + "\n" + exc.message);
				}
			}
			else
			{
				try
				{
					Navigate(strTarget, false, null);
				}
				catch (exc)
					{
					throw new Error("Could not navigate to '" + strTarget + "'");
				}
			}
		/*}
		catch (exc)
		{
			HtmlPopup.Error(exc);
		}*/
	}

	//====================================================
	// Show the options in this popup
	//====================================================
	this.ShowOptions = function ShowOptions(strXML, strKey, intLeft, intTop)
	{
		this.Open(null, null, intLeft, intTop, true);
		DisplayPopupOptions(strXML, null, strKey, true);
	}

	//====================================================
	// Create the options in the popup for the given context
	//====================================================
	function DisplayPopupOptions(strXML, docXML, strKey, blnForce)
	{
		try
		{	
			if (typeof (strXML) == "object" && strXML != null)
				throw new Error(docXML);

			if (divPopup)
			{
				divPopup.innerHTML = "";
				
				if (strKey != "" && strKey != null)
				{
					if (THIS.OverrideTitle)
					{
						divPopup.innerHTML += "<label>" + THIS.OverrideTitle + "</label>";
					}
					else
					{
						divPopup.innerHTML += "<label>" + strKey + "</label>";
					}
				}
			}
			else
				return;

			THIS.OpenPopup = objPopup;

			if (strXML == "" || strXML == null)
				return;

			var recordset = new Recordset();
			recordset.LoadXML(strXML);

			if (recordset.RecordCount > 0)
			{
				while (!recordset.EOF())
				{
					addPopupOption(recordset["Name"], recordset["Icon"], recordset["URL"], blnForce);
					recordset.MoveNext();
				}
			}
			else
			{
				addPopupOption("No information found", "/images/truckfile16.png", "", blnForce);
			}

			if (!THIS.AutoFadeOut)
			{
				var btnClose = document.createElement("DIV");
				btnClose.innerHTML = "Close";
				btnClose.className = "closeContextPopup";
				divPopup.appendChild(btnClose);
			}
		}
		catch (exc)
		{
			HtmlPopup.Error(exc);
		}
		finally
		{
			if (divPopup != null)
				divPopup.className = "";
		}
	}

	//====================================================
	// Add an option to the given popup
	//====================================================
	function addPopupOption(strText, strIcon, strURL, blnForce)
	{
		var divOption = document.createElement("SPAN");
		divOption.innerHTML = strText;
		divOption.style.backgroundImage = "url('" + strIcon + "')";

		if (strURL)
		{
			var strURLPath = strURL;
			if (strURL.indexOf("?") != -1)
				strURLPath = strURL.substring(0, strURL.indexOf("?"));

			strURLPath = strURLPath.toLowerCase();

			var strDocumentURL = document.location.toString().toLowerCase();
			if (strDocumentURL.indexOf(strURLPath) != -1 && !blnForce)
				return;

			divOption.setAttribute("target", strURL);
		}

		var intOptions = divPopup.getAttribute("options") == null ? 0 : Number(divPopup.getAttribute("options"));
		intOptions++;
		divPopup.setAttribute("options", intOptions);

		divPopup.appendChild(divOption);
	}

	//====================================================
	// Show the popup
	//====================================================
	this.Show = function Show(strContext, strKey, evt, OverrideTitle)
	{
		try
		{
			if (OverrideTitle)
				THIS.OverrideTitle = OverrideTitle;

			getPopup(strContext, evt, strKey, OverrideTitle);
		}
		catch (exc)
		{
			HtmlPopup.Error(exc);
		}
	}

	//====================================================
	// Hide the popup
	//====================================================
	this.Hide = function Hide(blnImmediate)
	{
		try
		{
			if (blnImmediate)
				hidePopup();
			else
				StartFade();
		}
		catch (exc)
		{
			HtmlPopup.Error(exc);
		}
	}
}

var m_objContextPopup = null;
ContextPopup.Create = function Create()
{
	try
	{
		if (m_objContextPopup == null)
			m_objContextPopup = new ContextPopup();

		return m_objContextPopup;
	}
	catch (exc)
	{
		HtmlPopup.Error(exc);
	}
}

ContextPopup.Show = function Show(strContext, strKey, evt, OverrideTitle)
{
	try
	{
		evt.cancelBubble = true;
		evt.returnValue = false;
		if (evt.stopPropagation)
			evt.stopPropagation();
	
		ContextPopup.Create().Show(strContext, strKey, evt, OverrideTitle);
	}
	catch (exc)
	{
		HtmlPopup.Error(exc);
	}
}

addInitMethod(ContextPopup.Create);
