﻿//======================================================
// Popup window class
//======================================================
function PopupWindowClass(XML)
{
	var THIS = this;
	
	THIS.Name = 'PopupWindowClass';
	THIS.XML = ((XML == undefined) ? '<Data/>' : XML);
	THIS.DynamicXslt = (XML != undefined);
	THIS.Padder = 6;
	THIS.Cancelled = false;
	THIS.ButtonHandlerArray = new Object();
	THIS.ServerSideTransform = false;
	
	//==================================================
	// Dispose.
	//==================================================
	this.Dispose = function Dispose()
	{
		for(Element in THIS)
		{
			if (typeof(THIS[Element]) != 'function')
			{
				THIS[Element] = null;
				delete THIS[Element];
			}
		}
		
		delete THIS;
		return;
	}
	
	//==================================================
	// Open popup window.
	//==================================================
	this.Open = function Open(evt, Parent, Name, Load, OKButtonHandler, CancelButtonHandler, Title, Width, Height, Xslt, CallBack, ButtonText, ButtonArray, Object, MessageOnly, WebService, WebMethod, WebParameters)
	{
		if (document.body == null)
			return;

		Functions.AllowMove = true;
		THIS.IE6_7 = false;
		
		// Check if this is IE6 or IE7.
		if ((window.navigator.userAgent.indexOf('MSIE 6') != -1) || (window.navigator.userAgent.indexOf('MSIE 7') != -1))
			THIS.IE6_7 = true;
		
		THIS.Height = ((Height < 0) ? Height : 0);

		Xslt = (Xslt == null ? undefined : Xslt);
		Xslt = (Xslt == undefined ? '' : Xslt);
		var blnMessagePrompt = (Xslt.indexOf('Prompt') == -1 ? false : true);
		
		if (ButtonText == undefined)
			ButtonText = 'OK|Cancel';

		var aButtonText = ButtonText.split('|');
		THIS.Parent = Parent;
		THIS.PopupWindowID = 'PopWindow' + Name;
		THIS.PopupContentName = 'PopupContent' + Name;
		THIS.CallBack = CallBack;
		THIS.Object = Object;
		
		// Display the lock layer.
		THIS.LockLayer = document.createElement('DIV');
		THIS.LockLayer.id = 'winLockLayer';
		THIS.LockLayer.innerHTML = '&nbsp;';
		THIS.LockLayer.className = 'Show';
		THIS.LockLayer.style.zIndex = Functions.ZIndex();
		document.body.appendChild(THIS.LockLayer);
		
		THIS.PopupWindow = document.createElement('DIV');
		THIS.MainContentTopLeftCornerShadow = document.createElement('DIV');
		THIS.MainContentTopShadow = document.createElement('DIV');
		THIS.MainContentTopRightCornerShadow = document.createElement('DIV');
		THIS.MainContentLeftShadow = document.createElement('DIV');
		THIS.MainContentRightShadow = document.createElement('DIV');
		THIS.MainContentBottomShadow = document.createElement('DIV');
		THIS.MainContentBottomLeftShadow = document.createElement('DIV');
		THIS.MainContentBottomRightShadow = document.createElement('DIV');
		THIS.PopupMainContent = document.createElement('DIV');
		THIS.Title = document.createElement('DIV');
		THIS.PopupTitle = document.createElement('DIV');
		THIS.btnClosePopupWindow = THIS.CreateButton();
		THIS.PopupContent = document.createElement('DIV');
		THIS.ActionContainer = document.createElement('DIV');
		THIS.btnOK = THIS.CreateButton();
		THIS.btnCancel = THIS.CreateButton();
		
		THIS.PopupWindow.id = THIS.PopupWindowID;
		THIS.MainContentTopLeftCornerShadow.id = 'MainContentTopLeftCornerShadow';
		THIS.MainContentTopShadow.id = 'MainContentTopShadow';
		THIS.MainContentTopRightCornerShadow.id = 'MainContentTopRightCornerShadow';
		THIS.MainContentLeftShadow.id = 'MainContentLeftShadow';
		THIS.MainContentRightShadow.id = 'MainContentRightShadow';
		THIS.MainContentBottomShadow.id = 'MainContentBottomShadow';
		THIS.MainContentBottomLeftShadow.id = 'MainContentBottomLeftShadow';
		THIS.MainContentBottomRightShadow.id = 'MainContentBottomRightShadow';
		
		THIS.PopupMainContent.id = 'PopupMainContent';
		THIS.PopupMainContent.className = 'PopupMainContent';
		THIS.Title.id = 'Title' + Name;
		THIS.Title.className = 'Title';
		THIS.PopupTitle.id = 'PopupTitle' + Name;
		THIS.PopupTitle.className = 'PopupTitle';
		
		if (THIS.IE6_7)
		{
			THIS.PopupTitle.onmousedown = function(){Functions.SetMoveObject(document.getElementById(THIS.PopupWindowID), event)};
			THIS.PopupTitle.onmouseup = function(){Functions.ResetMoveObject(event)};
		}
		else
		{
			THIS.PopupTitle.setAttribute('onmousedown', 'Functions.SetMoveObject(document.getElementById(\'' + THIS.PopupWindowID + '\'), event);');
			THIS.PopupTitle.setAttribute('onmouseup', 'Functions.ResetMoveObject(event);');
		}
		
		THIS.btnClosePopupWindow.id = 'btnClosePopupWindow' + Name;
		THIS.btnClosePopupWindow.onclick = THIS.btnClosePopupWindow_Click;
		THIS.btnClosePopupWindow.disabled = (MessageOnly == undefined ? false : MessageOnly);
		THIS.PopupContent.id = 'PopupContent' + Name;
		THIS.PopupContent.className = (THIS.PopupContentClass == undefined ? 'PopupContent' : THIS.PopupContentClass);
		THIS.ActionContainer.id = 'ActionContainer' + Name;
		THIS.ActionContainer.className = 'ActionContainer';
		
		// Check if a button array has been passed.
		if (ButtonArray != undefined)
		{
			// Check the button array.
			if (typeof(ButtonArray) == 'object')
			{
				for (var strButtonName in ButtonArray)
				{
					// Check the button name.
					if (strButtonName == undefined)
						continue;

					var Button = THIS.CreateButton();
					Button.id = 'btn' + strButtonName.replace(/ /gi, '');
					Button.onclick = THIS.ClickCallBack;
					Button.innerHTML = strButtonName;
					
					THIS.ActionContainer.appendChild(Button);
					THIS.ButtonHandlerArray[Button.id] = ButtonArray[strButtonName];
				}
			}
			else
			{
				var aButtonArray = ButtonArray.split('|');
				
				for(var intCounter = 0; intCounter < aButtonArray.length; intCounter++)
				{
					var aButtonDetail = aButtonArray[intCounter].split(',');
					var Button = THIS.CreateButton();
					Button.id = 'btn' + aButtonDetail[0].toString().replace(/ /gi, '');
					
					if(THIS.IE6_7)
						Button.setAttribute('href','javascript:' + aButtonDetail[1] + '()');
					else
						Button.setAttribute('onclick', aButtonDetail[1] + '()');
					
					// Check the length of the button detail.
					if (aButtonDetail.length == 3)
						Button.className = aButtonDetail[2];

					Button.innerHTML = aButtonDetail[0];
					THIS.ActionContainer.appendChild(Button);
				}
			}
		}
		
		THIS.btnOK.disabled = true;
		THIS.btnCancel.disabled = true;
		
		if (OKButtonHandler != null)
		{
			THIS.btnOK.id = 'btnOK' + Name;
			THIS.btnOK.innerHTML = aButtonText[0];
			THIS.btnOK.onclick = THIS.btnOK_Click;
			THIS.btnOK.disabled = (MessageOnly == undefined ? false : MessageOnly);
			THIS.OKButtonHandler = OKButtonHandler;
			
			THIS.ActionContainer.appendChild(THIS.btnOK);
		}
		
		if (CancelButtonHandler != null)
		{
			THIS.btnCancel.id = 'btnCancel' + Name;
			THIS.btnCancel.innerHTML = aButtonText[1];
			THIS.btnCancel.onclick = THIS.btnCancel_Click;
			THIS.btnCancel.disabled = (MessageOnly == undefined ? false : MessageOnly);
			THIS.CancelButtonHandler = CancelButtonHandler;
			
			THIS.ActionContainer.appendChild(THIS.btnCancel);
		}
		
		THIS.PopupWindow.appendChild(THIS.MainContentTopLeftCornerShadow);
		THIS.PopupWindow.appendChild(THIS.MainContentTopShadow);
		THIS.PopupWindow.appendChild(THIS.MainContentTopRightCornerShadow);
		THIS.PopupWindow.appendChild(THIS.MainContentLeftShadow);
		THIS.PopupWindow.appendChild(THIS.MainContentRightShadow);
		THIS.PopupWindow.appendChild(THIS.MainContentBottomShadow);
		THIS.PopupWindow.appendChild(THIS.MainContentBottomLeftShadow);
		THIS.PopupWindow.appendChild(THIS.MainContentBottomRightShadow);
		THIS.PopupWindow.appendChild(THIS.PopupMainContent);
		THIS.PopupMainContent.appendChild(THIS.Title);
		THIS.Title.appendChild(THIS.PopupTitle);
		THIS.Title.appendChild(THIS.btnClosePopupWindow);
		THIS.PopupMainContent.appendChild(THIS.PopupContent);
		THIS.PopupMainContent.appendChild(THIS.ActionContainer);

		if (evt != null)
			THIS.SourceElement = evt.srcElement ? evt.srcElement : evt.target;

		THIS.Load = Load;
		THIS.PopupTitle.innerHTML = Title;
		THIS.PopupContent.innerHTML = 'Loading content, please wait...';

		THIS.PopupWindow.style.top = '-5000px';
		THIS.PopupWindow.style.width = Width + 'px';
		THIS.PopupWindow.style.height = '400px';
		THIS.PopupWindow.style.zIndex = Functions.ZIndex();
		THIS.PopupWindow.className = 'PopupWindow';
		document.body.appendChild(THIS.PopupWindow);

		// Try and transform the document on the client.
		if (THIS.ServerSideTransform)
		{
			WebService = (WebService == null ? undefined : WebService);
			WebMethod = (WebMethod == null ? undefined : WebMethod);
			
			// Load the XSLT.
			var soapXSLTContent = new SoapClient();
			soapXSLTContent.AddParameter('Decode', true);
			
			// Check if the Xslt value has been added.
			if ((Xslt != undefined) || (Xslt != null))
				soapXSLTContent.AddParameter('XSLT', escape(Xslt));
				
			// Check if there are any additional parameters to pass.
			if ((WebParameters != undefined) || (WebParameters != null))
			{
				var aWebParameters = WebParameters.split(',');
				
				for(var intWebParameter = 0; intWebParameter < aWebParameters.length; intWebParameter++)
				{
					var aWebParameter = aWebParameters[intWebParameter].split('=');
					soapXSLTContent.AddParameter(aWebParameter[0], aWebParameter[1]);
				}
			}

			soapXSLTContent.SendRequest((WebService == undefined ? '/WebServices/Proxy.asmx' : WebService), 'http://www.truckfile.co.uk/', (WebMethod == undefined ? 'XSLTContent' : WebMethod), THIS.soapXSLTContent_Completed);
		}
		else
		{
			var objXSLTransform = new XSLTransform();
			objXSLTransform.Transform(THIS.PopupContent, Xslt, THIS.XML, THIS.XSLTContent_Completed);
			delete objXSLTransform;
		}
		return;
	}
	
	//==================================================
	// Button click call back event handler.
	//==================================================
	this.ClickCallBack = function ClickCallBack(evt)
	{
		try
		{
			var evt = window.event ? window.event : evt;
			var target = evt.srcElement ? evt.srcElement : evt.target;
			var fncCallback = THIS.ButtonHandlerArray[target.id];
			var fncOnClick = null;
			
			// Check if a callback handler has been defined.
			if (fncCallback == undefined)
			{
				THIS.Close();
				return;
			}
			
			if (typeof (fncCallback) == "function")
			{
				THIS.Close();
				fncOnClick = fncCallback;
			}
			else if (typeof (fncCallback) == "string")
			{
				if (fncCallback == "CLOSE" || fncCallback == "")
					fncCallback = "fncClose";

				fncOnClick = eval(fncCallback);
			}
			
			fncOnClick(evt, THIS, target);
		}
		catch (exc)
		{
			HtmlPopup.Error(exc);
		}
		return;
	}
	
	//==================================================
	// Create a button.
	//==================================================
	this.CreateButton = function CreateButton()
	{
		// Check if this is IE6 or IE 7.
		if (THIS.IE6_7)
		{
			var objButton = document.createElement('A');
		}
		else
		{
			var objButton = document.createElement('BUTTON');
			objButton.setAttribute('type', 'button');
		}
		return objButton;
	}
	
	//==================================================
	// XSLT content soap completed event handler.
	//==================================================
	this.soapXSLTContent_Completed = function soapXSLTContent_Completed(Response)
	{
		if (THIS.PopupContent == undefined)
			return;

		var objReturnValue = null;
		
		// Load the content.
		THIS.PopupContent.innerHTML = Response;
		THIS.XSLTContent_Completed();
		return;
	}

	//==================================================
	// Resize the height of the popup.
	//==================================================
	this.ResizeHeight = function ResizeHeight()
	{
		THIS.intResizeHeight = window.setTimeout(THIS.ResizeHeightHandler, 100);
		return;
	}
	
	//==================================================
	// Resize the height timer event handler.
	//==================================================
	this.ResizeHeightHandler = function ResizeHeightHandler()
	{
		window.clearTimeout(THIS.intResizeHeight);
		delete THIS.intResizeHeight;
		
		// Check if the popup window needs to be resized due to the action buttons.
		var intButtonWidth = 0;
		for(var intCounter = 0; intCounter < THIS.ActionContainer.childNodes.length; intCounter++)
			intButtonWidth += THIS.ActionContainer.childNodes[intCounter].offsetWidth;

		if (intButtonWidth > THIS.PopupWindow.offsetWidth)
			THIS.PopupWindow.style.width = intButtonWidth + 23 + 'px';
		
		var intOverallHeight = Number(THIS.PopupTitle.offsetHeight) + Number(THIS.PopupContent.offsetHeight) + Number(THIS.ActionContainer.offsetHeight);
		var intPadding = ((THIS.ActionContainer.childNodes.length == 0) ? 4 : 17) + THIS.Height;
		
		if (THIS.ActionContainer.childNodes.length == 0)
		{
			THIS.ActionContainer.style.display = 'none';
			THIS.PopupContent.style.borderBottom = 'none';
		}

		if (!window.event && !window.ActiveXObject || (window.navigator.userAgent.indexOf('Safari') > -1))
			THIS.ActionContainer.style.height = (Number(THIS.ActionContainer.offsetHeight) - THIS.Padder) + 'px';
		
		THIS.PopupWindow.style.height = (intOverallHeight + intPadding) + 'px';
		
		// Check if the height of the popup is greater than the main window.
		if (THIS.PopupWindow.style.height > window.top.document.body.offsetHeight)
			THIS.PopupWindow.style.height = (window.top.document.body.offsetHeight - 20);

		// Fix for the calendar popup.
		if (THIS.PopupContentName == 'PopupContentCalendar')
			THIS.PopupWindow.style.height = ((intOverallHeight + intPadding) + 7) + 'px';

		// Center the popup.
		THIS.Center();
		return;
	}

	//==================================================
	// Center the popup.
	//==================================================
	this.Center = function Center()
	{
		var intWidth = THIS.PopupWindow.style.width.replace('px', '');
		var intHeight = THIS.PopupWindow.style.height.replace('px', '');
		var intHeightOffset = 0;
		
		if (document.body.clientHeight == 0)
			intHeightOffset = 80;
		
		// Center the popup in the middle of the window.
		THIS.PopupWindow.style.top = (((window.top.document.body.clientHeight / 2) - (intHeight / 2)) - intHeightOffset) + 'px';
		THIS.PopupWindow.style.left = ((window.top.document.body.clientWidth / 2) - (intWidth / 2)) + 'px';
		
		// Focus the element.
		if (THIS.FocusElement)
		{
			THIS.FocusElement.focus();
			
			// Check if the element is an input.
			if (THIS.FocusElement.tagName == 'INPUT')
				THIS.FocusElement.value = THIS.FocusElement.value;
		}
		else
		{
			// Set focus to the first button in the action controls container.
			if (THIS.ActionContainer.childNodes.length > 0)
			{
				for(var intCounter = 0; intCounter < THIS.ActionContainer.childNodes.length; intCounter++)
				{
					try
					{
						THIS.ActionContainer.childNodes[intCounter].focus();
					}
					catch(e){}
				}
			}
		}
		return;
	}
	
	//==================================================
	// XSLT content soap completed event handler.
	//==================================================
	this.XSLTContent_Completed = function XSLTContent_Completed()
	{
		var objReturnValue = null;
		var blnFocused = false;
		
		// Resize the height of the popup.
		THIS.ResizeHeight();

		// Check if a load event handler has been defined.
		if (THIS.Load != null)
		{
			// Execute the load function.
			if (THIS.CallBack == undefined)
			{
				THIS.FocusElement = THIS.Load(THIS, THIS.Parent);
			}
			else
			{
				THIS.FocusElement = THIS.Load(THIS, THIS.Parent, THIS.CallBack);
			}
		}

		if ((objReturnValue == undefined) || (objReturnValue == null))
		{
			if (!THIS.btnOK.disabled)
			{
				THIS.btnOK.focus();
				blnFocused = true;
			}
				
			if (THIS.btnOK.disabled && !THIS.btnCancel.disabled)
			{
				THIS.btnCancel.focus();
				blnFocused = true;
			}
		}
		else
		{
			// Focus the element.
			try
			{
				objReturnValue.focus();
				
				// Check if the element is an input.
				if (objReturnValue.tagName == 'INPUT')
					objReturnValue.value = objReturnValue.value;
			}
			catch(e)
			{}
		}
		
		// Check if a either the OK or cancel button has been given focus.
		if (!blnFocused)
		{
			// Set focus to the first button in the action controls container.
			if (THIS.ActionContainer.childNodes.length > 0)
			{
				for(var intCounter = 0; intCounter < THIS.ActionContainer.childNodes.length; intCounter++)
				{
					try
					{
						THIS.ActionContainer.childNodes[intCounter].focus();
					}
					catch(e){}
				}
			}
		}
		return;
	}
	
	//==================================================
	// Close popup window.
	//==================================================
	this.Close = function Close()
	{
		if (THIS.PopupWindow != undefined)
			document.body.removeChild(THIS.PopupWindow);
			
		if (THIS.LockLayer != undefined)
			document.body.removeChild(THIS.LockLayer);
			
		THIS.Dispose();
		return;
	}
	
	//==================================================
	// Close popup window button click event handler.
	//==================================================
	this.btnClosePopupWindow_Click = function btnClosePopupWindow_Click(evt)
	{
		THIS.Cancelled = true;

		if (THIS.CancelButtonHandler != null)
		{
			THIS.btnCancel_Click(THIS.SourceElement);
		}
		else
		{
			document.body.removeChild(THIS.PopupWindow);
			document.body.removeChild(THIS.LockLayer);
		}
		THIS.Dispose();
		return;
	}
	
	//==================================================
	// OK button click event handler.
	//==================================================
	this.btnOK_Click = function btnOK_Click(evt)
	{ 
		if (!THIS.OKButtonHandler(THIS.SourceElement, THIS.Object))
			return;

		if (THIS.PopupWindow != undefined)
			document.body.removeChild(THIS.PopupWindow);
			
		if (THIS.LockLayer != undefined)
			document.body.removeChild(THIS.LockLayer);

		THIS.Dispose();
		return;
	}
	
	//==================================================
	// Cancel button click event handler.
	//==================================================
	this.btnCancel_Click = function btnCancel_Click(evt)
	{
		if (!THIS.CancelButtonHandler(THIS.SourceElement, THIS.Object))
			return;

		document.body.removeChild(THIS.PopupWindow);
		document.body.removeChild(THIS.LockLayer);
		THIS.Dispose();
		return;
	}
	
	return;
}
