function NxUtils_GetParentClientID(clientID, recursions)
{
	var rExp = /__/gi;
	var returnValue = clientID.replace(rExp,"_#");
	if (recursions == null) recursions = 1;
	for (var i=0; i<recursions; i++)
	{
		returnValue = returnValue.substring(0, returnValue.lastIndexOf("_"))
		//alert("input:" + clientID + "\nrecursion:" + i + "\nvalue;" + returnValue)
	}
	var rExp2 = /_#/gi;
	returnValue = returnValue.replace(rExp2,"__")
	return returnValue;
}
/*
if(window.opener != null)
{
	if(window.onblur == null)
	{
		window.onblur = window.focus;
	}
}

*/
var asdf;

function openPopup(url, name, features, replace)
{
	var theWin = window.open(url, "PopupWindow", features, replace);
    s= features.split(",");
    var newHeight = theWin.height;
    var newWidth = theWin.width;
    for(i=0 ; i<s.length; i++)// we have an array of features
    {
		var feature = s[i].replace(' ','');
		if(feature.indexOf('height=') >= 0)
		{
			f_height = s[i].split("=");
			if(f_height.length = 2)
			{
				newHeight = f_height[1];
			}
		}
		if(feature.indexOf('width=') >= 0)
		{
			f_width = s[i].split("=");
			if(f_width.length = 2)
			{
				newWidth = f_width[1];
			}
		}		
	}
	theWin.resizeTo(newWidth,newHeight);
	return theWin;
}


function NxUtils_FindEventController(eventDomain)
{
	var RECURSION_LIMIT = 20;
	var objWindow = window;
	var currentRecursion = 0;
	
	_FindEventController:
	do
	{
		if (objWindow.NxEventController)
		{
			if (eventDomain != null)
			{
				var eventDomains = objWindow.NxEventDomains
				for (var i=0; i<eventDomains.length; i++)
				{
					if (eventDomains[i].toUpperCase() == eventDomain.toUpperCase())
						break _FindEventController;
				}
			}
			for (var domainIndex=0; domainIndex<objWindow.NxEventDomains.length; domainIndex++)
			{
				if (objWindow.NxEventDomains[domainIndex] == "*")
			{
				break _FindEventController;
			}
		}
		}
		if (objWindow != objWindow.top)
		{
			objWindow = objWindow.parent;
		}
		else
		{
			if (objWindow.opener)
				objWindow = objWindow.opener;
			else
				break _FindEventController;
		}
		currentRecursion++;
	}
	while (currentRecursion < RECURSION_LIMIT);
	return objWindow;
}



///TODO: make this an array to allow for several event targets
var NxUtils_EventTarget;

function NxUtils_FindEventTarget(objWindow, strLocation)
{
	if (objWindow == null) objWindow = NxUtils_FindEventController();
	if (strLocation == null) strLocation = document.location;
	var thisLoc = NxUtils_RemoveQueryString(strLocation);
	var compareLoc = NxUtils_RemoveQueryString(objWindow.document.location);
	if (thisLoc == compareLoc) NxUtils_EventTarget = objWindow;
	for (var i=0; i<objWindow.frames.length; i++)
	{
		try
		{
			if (objWindow.frames[i].name != "HiddenFrame")
				NxUtils_FindEventTarget(objWindow.frames[i], strLocation);
		}
		catch(e){}
	}
}

function NxUtils_PopulateEventArgDefinitions(eventArgs)
{
	var strItem = "NxEventArgDefinitions=";
	if (eventArgs[1].indexOf(strItem) == 0) var strDefinitions = eventArgs[1].substring(strItem.length, eventArgs[1].length);
	var arrItems = strDefinitions.split(",");
	for (var i=0; i<arrItems.length; i++)
	{
		var eventArgsIndex = i + 2;
		eval(arrItems[i] + " = eventArgs[" + eventArgsIndex + "]");
	}
}

function NxUtils_RemoveQueryString(url)
{
	url = url.toString();
	var i = url.indexOf("?");
	if (i >=0)
	{
		url = url.substring(0, i);
	}
	return url;
}

function NxUtils_CheckWindow(objWindow, strLocation)
{
	return (objWindow.document.location != strLocation);
}


function FireNxEvent(sender, eventArgs)
{
	var strDelimiter = "<,%>";
	var argsOut = "";
	var eventDomain = "*";
	switch (typeof(eventArgs))
	{
		case "object":
			for (var i=0; i<eventArgs.length; i++)
			{
				argsOut += escape(eventArgs[i]) + strDelimiter;
			}
			if (argsOut.length > 0)
			{
				argsOut = argsOut.substring(0, (argsOut.length - strDelimiter.length));
			}
			if (eventArgs.length > 0)
			{
				eventDomain = eventArgs[0].split(".")[0].toUpperCase();
			}
			break;
		case "string":
		default:
			argsOut = escape(eventArgs.toString());
			eventDomain = eventArgs.toString().split(".")[0].toUpperCase();
	}
	var qryOut = "&NxEventArgs=" + argsOut
		+ "&NxEventSender=" + sender;
	var objController = NxUtils_FindEventController(eventDomain);
	objController.NxPostAway(null, qryOut);
}

function FireNxClientEvent(sender, eventArgs)
{
	var eventDomain = "*";
	switch (typeof(eventArgs))
	{
		case "object":
			if (eventArgs.length > 0)
			{
				eventDomain = eventArgs[0].split(".")[0].toUpperCase();
			}
			break;
		case "string":
		default:
			eventDomain = eventArgs.toString().split(".")[0].toUpperCase();
	}
	if (NxUtils_FindEventController(eventDomain) == null) eventDomain = "*";
	NxUtils_FindEventController(eventDomain).NxEvent_FireClientEvent(sender, eventArgs);
}

function NxEvent_Refresh(location, overrideStatefulRefresh)
{
	var objController = NxUtils_FindEventController();
	if (location == null)
	{
		var strLocation = document.location;
	}
	NxUtils_EventTarget = null;
	NxUtils_FindEventTarget(objController, strLocation);
	if (NxUtils_EventTarget == null)
	{
		NxUtils_EventTarget = objController.NxUtils_FindEventTargetWindow(strLocation);
	}
	if (NxUtils_EventTarget != null)
	{
		if (overrideStatefulRefresh != null)
		{
			strLoc = NxUtils_EventTarget.document.location.toString();
			strLoc = strLoc.replace("NxStatefulRefresh=1","");
			NxUtils_EventTarget.document.location = strLoc;
		}
		else
		{
			NxUtils_EventTarget.document.location = NxUtils_EventTarget.document.location;
		}
	}
}

function NxEvent_DoPostBack(location)
{
	var objController = NxUtils_FindEventController();
	if (location == null)
	{
		var strLocation = document.location;
	}
	NxUtils_EventTarget = null;
	NxUtils_FindEventTarget(objController, strLocation);
	if (NxUtils_EventTarget == null)
	{
		NxUtils_EventTarget = objController.NxUtils_FindEventTargetWindow(strLocation);
	}
	if (NxUtils_EventTarget != null)
	{
		NxUtils_EventTarget.document.forms[0].submit();
	}
}

function NxEvent_PostAway(qry, objForm)
{
	var objController = NxUtils_FindEventController();
	if (objForm == null) var objForm = document.forms[0];
	var qryOut = "&NxEventArgs=FulFill.PostAway"
		+ "&NxEventSender=null"
		+ "&" + qry;
	objController.NxPostAway(objForm, qryOut);
}

function NxUtils_FindParentContainer(obj)
{
	if (obj == null)
	{
		obj = NxUtils_FindParentIFrame();
	}
	do
	{
		if (obj.tagName == "BODY") return;
		obj = obj.parentElement;
	}
	while (obj.NxContentContainer != "1")
	return obj;
}

function NxUtils_FindParentIFrame()
{
	var strLocation = NxUtils_RemoveQueryString(document.location);
	for (var i=0; i<parent.document.body.getElementsByTagName("IFRAME").length; i++)
	{
		var objFrame = parent.document.body.getElementsByTagName("IFRAME")[i];
		var compareTo = NxUtils_RemoveQueryString(objFrame.src);
		if (strLocation == compareTo) return objFrame;
	}
}

function NxUtils_FindIFrame(url)
{
	var baseUrl = NxUtils_RemoveQueryString(url);
	var objIFrames = document.getElementsByTagName("IFRAME");
	for (var i=0; i<objIFrames.length; i++)
	{
		if (NxUtils_RemoveQueryString(objIFrames[i].src.toString()).indexOf("/" + baseUrl) > 0)
			return objIFrames[i];
	}
}

function NxUtils_IsHidden(obj)
{
	var isHidden = false;
	do
	{
		if (obj.style.display == "none") isHidden = true;
		obj = obj.parentElement;
	}
	while (obj.tagName != "BODY")
	return isHidden;
}

function NxUtils_GetPageOffsetLeft(obj)
{
	var rtnValue = 0;
	do
	{
		if (obj.offsetParent == obj.document.body) break;
		rtnValue += obj.offsetLeft;
		obj = obj.offsetParent;
		
	}
	while (true)
	return rtnValue;
}

function NxUtils_GetPageOffsetTop(obj)
{
	var rtnValue = 0;
	do
	{
		if (obj.offsetParent == obj.document.body) break;
		rtnValue += obj.offsetTop;
		obj = obj.offsetParent;
		
	}
	while (true)
	return rtnValue;
}

//TODO: ========== Resizing functions - need to be broken off into separate .js file ===========
var NxUtils_LastResize;
var NxUtils_ResizeEventFired = false;
var NxUtils_ResizeFunctionsToExecute = new Array(0);
var NxUtils_ResizeDelay = 300;

function NxUtils_DoResize(resizeFunctionToExecute)
{
	if (resizeFunctionToExecute != null)
	{
		var exists = false;
		try
		{
		for (var i=0; i<NxUtils_ResizeFunctionsToExecute.length; i++)
		{
			if (NxUtils_ResizeFunctionsToExecute[i] == resizeFunctionToExecute) exists = true;
		}
		if (!exists)
			NxUtils_ResizeFunctionsToExecute[NxUtils_ResizeFunctionsToExecute.length] = resizeFunctionToExecute;
	}
		catch(e)
		{
			NxUtils_ResizeCheckin();
		}
	}
	NxUtils_LastResize = (new Date()).valueOf();
	self.setTimeout(NxUtils_ResizeCheckin,NxUtils_ResizeDelay);
}

function NxUtils_ResizeCheckin()
{
	var newDate = ((new Date()).valueOf() - (NxUtils_ResizeDelay + 10));
	if (NxUtils_LastResize > newDate)
	{
		try
		{
		for (var i=0; i<NxUtils_ResizeFunctionsToExecute.length; i++)
		{
			eval(NxUtils_ResizeFunctionsToExecute[i]);
			if (NxUtils_ResizeFunctionsToExecute[i] == "NxPageBody_SetSize()")
					self.setTimeout("try{NxUtils_ResizeFunctionsToExecute[i]}catch(e){}",1000);
			}
		}
		catch(e)
		{
		
		}
	}
	else
	{
	}
}
// ========== End Resizing functions ===========