﻿//======================================================
// Popup window class
//======================================================
function PopupWindowClass(XML)
{
	var THIS = this;
	
	this.AllowMove = true;
	this.Name = 'TitlePrompt';
	this.Title = 'Title';
	this.XML = '<Data/>';
	this.XSLT = '/Xslt/Prompt.xslt';
	this.Buttons = null;
	this.CallBack = null;
	this.Message = false;
	this.Loaded = false;
	
	this.ShadowOffsetTop = 5;
	this.ShadowOffsetLeft = 5;
	
	//==================================================
	// Open the popup window..
	//==================================================
	this.Open = function Open()
	{
		try
		{
			this.WaitWindow = Functions.WaitWindow('Loading, please wait...');

			// Create the lock layer.
			this.LockLayer = document.createElement('DIV');
			this.LockLayer.className = 'LockLayer';
			this.LockLayer.innerHTML = '&nbsp;';
			
			if ((BrowserDetect.browser == 'Explorer') && (BrowserDetect.version == 6))
				this.LockLayer.style.height = document.documentElement.clientHeight + 'px';
			
			document.body.appendChild(this.LockLayer);
			
			// Check the browser.
			if ((BrowserDetect.browser == 'Explorer') && (BrowserDetect.version == 8))
			{
				// Create the shadow.
				this.PopupWindowContainerShadow = document.createElement('DIV');
				this.PopupWindowContainerShadow.innerHTML = '&nbsp;';
				this.PopupWindowContainerShadow.className = 'PopupWindowShadow';
				document.body.appendChild(this.PopupWindowContainerShadow);
			}
			
			// Create and load the popup window main container.
			this.PopupWindowContainer = document.createElement('DIV');
			this.PopupWindowContainer.className = 'PopupWindow';
			
			this.PopupWindowContainer.style.display = 'none';
			
			var objXSLTransform = new XSLTransform();
			objXSLTransform.Transform(this.PopupWindowContainer, '/XSLT/PopupWindowFrame.xslt', '<Data><PopupID>' + this.Name + '</PopupID><Title>' + this.Title + '</Title></Data>', this.PopupWindowFrame_Completed);
			delete objXSLTransform;
		}
		catch(exc)
		{
			Functions.DisplayError(this.Name + '.Open()\n\n' + exc);
		}
		return;
	}
	
	//==================================================
	// Popup frame XSLT completed handler.
	//==================================================
	this.PopupWindowFrame_Completed = function PopupWindowFrame_Completed()
	{
		try
		{
			// Append the popup window to the document.
			document.body.appendChild(THIS.PopupWindowContainer);
			
			// Get the popup window elements.
			THIS.PopupWindowTitle = document.getElementById('PopupWindowTitle' + THIS.Name);
			THIS.PopupContentContainer = document.getElementById('PopupWindowContent' + THIS.Name);
			THIS.PopupWindowActions = document.getElementById('PopupWindowActions' + THIS.Name);
			
			// Attach the mouse down and up handlers.
			THIS.PopupWindowTitle.onmousedown = THIS.OnMouseDown;
			THIS.PopupWindowTitle.onmouseup = THIS.OnMouseUp;

			var objXSLTransform = new XSLTransform();
			objXSLTransform.Transform(THIS.PopupContentContainer, THIS.XSLT, THIS.XML, THIS.PopupContent_Completed);
			delete objXSLTransform;
		}
		catch(exc)
		{
			Functions.DisplayError(this.Name + '.PopupWindowFrame_Completed()\n\n' + exc);
		}
		return;
	}
	
	//==================================================
	// Popup content XSLT completed handler.
	//==================================================
	this.PopupContent_Completed = function PopupContent_Completed()
	{
		try
		{
			// Check if there are any buttons to add.
			if (THIS.Buttons == null)
			{
				var btnOKButton = document.createElement('BUTTON');
				btnOKButton.innerHTML = 'OK';
				btnOKButton.id = 'OK';
				btnOKButton.onclick = THIS.ButtonClickedHandler;
				THIS.PopupWindowActions.appendChild(btnOKButton);
			}
			else
			{
				// Add the buttons.
				for(Button in THIS.Buttons)
				{
					var btnOKButton = document.createElement('BUTTON');
					btnOKButton.innerHTML = Button;
					btnOKButton.id = Button;
					btnOKButton.onclick = THIS.ButtonClickedHandler;
					THIS.PopupWindowActions.appendChild(btnOKButton);
					THIS.Buttons[Button].parentNode = btnOKButton;
				}
			}
			
			// Check if there are any buttons.
			if (THIS.Message)
			{
				if (THIS.PopupWindowContainer.offsetWidth > 350)
					THIS.PopupWindowContainer.style.width = 350 + 'px';
			}

			// Reposition the popup window depending upon the content.
			THIS.PopupWindowContainer.style.display = 'block';
			THIS.PopupWindowContainer.style.left = THIS.PopupWindowContainer.offsetLeft - (THIS.PopupWindowContainer.offsetWidth / 2) + 'px';
			THIS.PopupWindowContainer.style.top = THIS.PopupWindowContainer.offsetTop - (THIS.PopupWindowContainer.offsetHeight / 2) + 'px';
			
			// Check the browser.
			if ((BrowserDetect.browser == 'Explorer') && (BrowserDetect.version == 8))
			{
				// Position the shadow.
				THIS.PopupWindowContainerShadow.style.width = THIS.PopupWindowContainer.offsetWidth;
				THIS.PopupWindowContainerShadow.style.height = THIS.PopupWindowContainer.offsetHeight;
				THIS.PopupWindowContainerShadow.style.top = THIS.PopupWindowContainer.offsetTop + THIS.ShadowOffsetTop;
				THIS.PopupWindowContainerShadow.style.left = THIS.PopupWindowContainer.offsetLeft + THIS.ShadowOffsetLeft;
			}
			
			// Invoke the callback.
			if ((THIS.CallBack != null) && (THIS.Buttons != null))
				THIS.CallBack(THIS);

			// Set focus to the right most button.
			if (THIS.Message)
			{
				var intChildNodes = THIS.PopupWindowActions.childNodes.length;
				var objActionButton = THIS.PopupWindowActions.childNodes[(intChildNodes - 1)];
				objActionButton.focus();
			}

			THIS.tmrLoaded = window.setTimeout(THIS.PopupLoaded, 100);
			THIS.WaitWindow.Close();
		}
		catch(exc)
		{
			Functions.DisplayError(this.Name + '.PopupContent_Completed()\n\n' + exc);
		}
		return;
	}
	
	//==================================================
	// Popup loaded event handler.
	//==================================================
	this.PopupLoaded = function PopupLoaded()
	{
		try
		{
			window.clearTimeout(THIS.tmrLoaded);
			THIS.Loaded = true;
		}
		catch(exc)
		{
			Functions.DisplayError(this.Name + '.PopupLoaded()\n\n' + exc);
		}
		return;
	}
	
	//==================================================
	// Button click event handler.
	//==================================================
	this.ButtonClickedHandler = function ButtonClickedHandler(evt)
	{
		try
		{
			// Check if the popup has loaded.
			if (!THIS.Loaded)
				return;

			var evt = ((evt == null) ? window.event : evt);
			var objSourceElement = evt.srcElement ? evt.srcElement : evt.target;
			
			// Check if the button array has been defined.
			if (THIS.Buttons == null)
			{
				if (THIS.CallBack)
					THIS.CallBack(THIS);

				THIS.Close();
			}
			else
			{
				THIS.Buttons[objSourceElement.id](THIS);
			}
		}
		catch(exc)
		{
			Functions.DisplayError(this.Name + '.ButtonClickedHandler()\n\n' + exc);
		}
		return;
	}
	
	//==================================================
	// On mouse down event handler.
	//==================================================
	this.OnMouseDown = function OnMouseDown(evt)
	{
		try
		{
			// Check if the popup is allowed to move, if so, attach to the window mouse move event.
			if (THIS.AllowMove)
			{
				var evt = ((evt == null) ? window.event : evt);

				THIS.OffsetX = ((BrowserDetect.browser == 'Firefox') ? evt.layerX : evt.offsetX) + 1;
				THIS.OffsetY = ((BrowserDetect.browser == 'Firefox') ? evt.layerY : evt.offsetY) + 1;
				
				document.onselectstart = function(){ return false};
				document.ondragstart  = function(){ return false};
				document.onmousemove = THIS.Move;
			}
		}
		catch(exc)
		{
			Functions.DisplayError(this.Name + '.OnMouseDown()\n\n' + exc);
		}
		return;
	}

	//==================================================
	// Move event handler.
	//==================================================
	this.Move = function Move(evt)
	{
		try
		{
			var evt = ((evt == null) ? window.event : evt);

			THIS.PopupWindowContainer.style.top = evt.clientY - THIS.OffsetY + 'px';
			THIS.PopupWindowContainer.style.left = evt.clientX - THIS.OffsetX + 'px';
			
			THIS.PopupWindowContainerShadow.style.top = THIS.PopupWindowContainer.offsetTop + THIS.ShadowOffsetTop;
			THIS.PopupWindowContainerShadow.style.left = THIS.PopupWindowContainer.offsetLeft + THIS.ShadowOffsetLeft;
		}
		catch(exc)
		{
			//Functions.DisplayError(THIS.Name + '.Move()\n\n' + exc);
		}
		return;
	}
	
	//==================================================
	// On mouse up event handler.
	//==================================================
	this.OnMouseUp = function OnMouseUp(evt)
	{
		try
		{
			document.onmousemove = null;
			document.onselectstart = function(){ return true};
			document.ondragstart  = function(){ return true};
		}
		catch(exc)
		{
			Functions.DisplayError(this.Name + '.OnMouseUp()\n\n' + exc);
		}
		return;
	}

	//==================================================
	// Popup content XSLT completed handler.
	//==================================================
	this.Close = function Close()
	{
		try
		{
			if ((BrowserDetect.browser == 'Explorer') && (BrowserDetect.version == 8))
				document.body.removeChild(THIS.PopupWindowContainerShadow);

			document.body.removeChild(THIS.PopupWindowContainer);
			document.body.removeChild(THIS.LockLayer);
			THIS.Dispose();
		}
		catch(exc)
		{
			//Functions.DisplayError(this.Name + '.Close()\n\n' + exc);
		}
		return;
	}
	
	//==================================================
	// Dispose.
	//==================================================
	this.Dispose = function Dispose()
	{
		try
		{
			for (Element in THIS)
			{
				if (typeof (THIS[Element]) != 'function')
				{
					if ((THIS[Element] != undefined) || (THIS[Element] != null))
					{
						if (Element != 'undefined')
						{
							THIS[Element] = null;
							delete THIS[Element];
						}
					}
				}
			}

			delete THIS;
		}
		catch(exc)
		{
			Functions.DisplayError(this.Name + '.Dispose()\n\n' + exc);
		}
		return;
	}	
	return;
}
