/////////////////////////////////////////////////////////////////////////////
// Common.js
//		Common Java script functions used by the application.

/////////////////////////////////////////////////////////////////////////////
//	Attempt to open a popup window to determine if a popup blocker is enabled.
//	If popups do not appear to be blocked the idPopupMessage will be hidden.
//	Returns true if popups appear to be working.
function _TestPopups(idPopupMessage)
{
	var objChild;								// Window
	var reWork = new RegExp('object*','gi');		// Regular expression

	// See if we can find the divName object
	var	objMessage = document.getElementById(idPopupMessage);

	// Attempt to open a popup window
	try {
		objChild = window.open('','lpPopupTest','width=1,height=1,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,top=0,left='+screen.width);
		window.focus();
		objChild.close();
	}
	catch(e) { }

	// See if the popup appears to have been blocked
	if(!reWork.test(String(objChild)))
	{
		if (objMessage)
		{
			objMessage.style.visibility = 'visible';
			objMessage.style.display = 'inline';
		}
		return false;	
	}
	
	// It appears the popup was successful
	if (objMessage)
	{
		objMessage.style.visibility = 'hidden';
		objMessage.style.display = 'none';
	}
	return true
}

/////////////////////////////////////////////////////////////////////////////
//	Get the width and hieght of the client area for the browser's window
function _GetWindowRect()
{
	var		rect = Object();
	rect.top = rect.left = 0;
	
	if (self.innerHeight)
	{
		rect.right = self.innerWidth;
		rect.bottom = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
	{
		rect.right = document.documentElement.clientWidth;
		rect.bottom = document.documentElement.clientHeight;
	}
	else if (document.body)
	{
		rect.right = document.body.clientWidth;
		rect.bottom = document.body.clientHeight;
	}
	
	return rect;
}

/////////////////////////////////////////////////////////////////////////////
//	Get the width and hieght of the full page
function _GetPageRect()
{
	var		rect =  _GetWindowRect();
	
	if (document.body.scrollHeight && document.body.scrollHeight > rect.bottom)
		rect.bottom = document.body.scrollHeight;
	if (document.body.scrollWidth && document.body.scrollWidth > rect.right)
		rect.right = document.body.scrollWidth;
	
	return rect;
}

/////////////////////////////////////////////////////////////////////////////
//	Attempt to determine and return the rectangle for the specified
//	object in coordinates relative to the document.
function _GetObjectRect(obj)
{
	var		rect = Object();

	rect.left = obj.offsetLeft;
	rect.top = obj.offsetTop;

	objOffsetParent = obj.offsetParent;
	while (objOffsetParent) {
		if (objOffsetParent) {
			rect.left += objOffsetParent.offsetLeft;
			rect.top += objOffsetParent.offsetTop;
		}
		objOffsetParent = objOffsetParent.offsetParent;		
	}

	rect.right = rect.left + obj.offsetWidth;
	rect.bottom = rect.top + obj.offsetHeight;

	return rect;
}

/////////////////////////////////////////////////////////////////////////////
//	Used to preload an array of passed images.
function _PreloadImages()
{
	var d = document;
	if (d.images)
	{
		if (!d._lppi)
			d._lppi = new Array();
		var i, j = d._lppi.length, a = _PreloadImages.arguments;
		for (i=0; i < a.length; i++)
		{
			if (a[i].indexOf("#") != 0)
			{
				d._lppi[j] = new Image;
				d._lppi[j++].src = a[i];
			}
		}
	}
}

/////////////////////////////////////////////////////////////////////////////
//	Alert all properties of the specified object.
function _dumpObject(obj)
{
	for(var prop in obj)
	{
		alert(prop + '  ' + obj[prop]);
	}
}

/////////////////////////////////////////////////////////////////////////////
//	Return the current vertical scroll position for the specified window or frame.
function _getVerticalScrollPos(windowOrFrame)
{ 
//	var ie = (document.layers) ? false : true;
//	if (ie)
//		return (windowOrFrame.document.body.scrollTop);	// Internet Explorer
//	return (windowOrFrame.pageYOffset);					// Netscape, Firefox and Safari

	var ScrollTop = windowOrFrame.document.body.scrollTop;

	if (ScrollTop == 0)
	{
		if (window.pageYOffset)
			ScrollTop = window.pageYOffset;
		else
			ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
	}
	return ScrollTop;
}

/////////////////////////////////////////////////////////////////////////////
//	Modify the current vertical scroll position for the specified window or frame.
function _setVerticalScrollPos(windowOrFrame, pos)
{ 
	var ie = (document.layers) ? false : true;
	if (ie)
		windowOrFrame.document.body.scrollTop = pos;	// Internet Explorer
	windowOrFrame.pageYOffset = pos;					// Netscape, Firefox and Safari
}

var		_reportControlID = "";

/////////////////////////////////////////////////////////////////////////////
//	Event function that watches for the vertical scroll position to change.
function _onVerticalScroll(e)
{
	var	pos = _getVerticalScrollPos(window);
	var obj = document.getElementById(_reportControlID);
	if (obj)
		obj.value = pos;
}

/////////////////////////////////////////////////////////////////////////////
// Return the current vertical scroll pos
function _GetVerticalScroll()
{
	var obj = document.getElementById(_reportControlID);
	if (obj && obj.value)
		return parseInt(obj.value);
	return 0;
}

/////////////////////////////////////////////////////////////////////////////
//	Used to capture vertical scroll events and record the current position
//	in the specified text input control.
function _captureVerticalScroll(reportControlID)
{
	_reportControlID = reportControlID;

	// Test for addEventListener (Netscape, Firefox and Safari)
	if (document.addEventListener)
		document.addEventListener("scroll", _onVerticalScroll, false); 
	// Test for attachEvent (IE)
	else if (window.attachEvent)
		window.attachEvent("onscroll", _onVerticalScroll);
}

/////////////////////////////////////////////////////////////////////////////
//	Copy the supplied text to the system clipboard.
function _copyToClipboard(copyText)
{
	// IE
	if (window.clipboardData && window.clipboardData.setData)
	{
		window.clipboardData.clearData();
		window.clipboardData.setData("text", copyText);
	}
	else if (window.netscape)
	{
		netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect'); 
	  
		// make a copy of the Unicode
		var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
	                       
		if (!str) return false; // couldn't get string obj
		str.data = copyText; // unicode string?

		// add Unicode to the transferable widget  
		var trans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable);
		if (!trans) return false; //no transferable widget found
	  
		trans.addDataFlavor("text/unicode");
		trans.setTransferData("text/unicode", str, copyText.length * 2); // *2 because it's unicode
	  
		// copy the transferable widget!  
		var clipboard = Components.classes["@mozilla.org/widget/clipboard;1"].getService(Components.interfaces.nsIClipboard);
		if (!clipboard) return false; // couldn't get the clipboard
	  
		clipboard.setData(trans, null, Components.interfaces.nsIClipboard.kGlobalClipboard);
	}	
}

/////////////////////////////////////////////////////////////////////////////
//	Return the value for the specified get parameter.
function _getGetParameter(name)
{
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if (results == null)
		return false;
	else
		return results[1];
}

/////////////////////////////////////////////////////////////////////////////
//	Enable or disable the specified object using it's id.
function _DisableObject(control, bEnabled)
{
	var	obj = document.getElementById(control);
	if (obj)
		obj.disabled = !bEnabled;
}

/////////////////////////////////////////////////////////////////////////////
//	Hide or show the specified object using it's id.
function _ShowObject(control, bHide, strDisplayStyle)
{
	var	obj = document.getElementById(control);
	if (obj)
	{
		obj.style.visibility = (!bHide) ? "visible" : "hidden";
		obj.style.display = (!bHide) ? strDisplayStyle : "none";
	}
}

/////////////////////////////////////////////////////////////////////////////
// Display a confirmation messgae before performing a postback event
function _ConfirmPostback(message, clientid, argument)
{
	if (!confirm(message)) return;
		__doPostBack(clientid, argument);
}

/////////////////////////////////////////////////////////////////////////////
//	_IsNumeric
//		Determine if the string consists of all numeric characters.
//		value = String to validate.
//		Returns true if the specified string appears to contain only numeric characters.
function _IsNumeric(value)
{
	var	digits = "0123456789";
	for (var i=0; i < value.length; i++)
	{
		if (digits.indexOf(value.charAt(i)) == -1)
			return false;
	}
	return true;
}

/////////////////////////////////////////////////////////////////////////////
//	_IsAlphaNumeric
//		Determine if the string consists of all alpha-numeric characters.
//		value = String to validate.
//		Returns true if the specified string appears to contain only alpha-numeric characters.
function _IsAlphaNumeric(value)
{
	var	alphas = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var	digits = "0123456789";
	for (var i=0; i < value.length; i++)
	{
		if (digits.indexOf(value.charAt(i)) == -1 && alphas.indexOf(value.charAt(i)) == -1)
			return false;
	}
	return true;
}

/////////////////////////////////////////////////////////////////////////////
// Add the specified function call to our window.onload event
function _AddWindowOnLoadEvent(func)
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function')
	{
		window.onload = func;
	}
	else
	{
		window.onload = function()
		{
		oldonload();
		func();
		}
	}
}


