// always-include-ns.js  Copyright 1998-2004 PaperThin, Inc. All rights reserved.
function setStatbar(statbar)
{
	var strStatbar=unescape(statbar);
	window.status=strStatbar;
}
function HandleLink(parentID,link,displaylink)
{
	// links are in one of the following formats:
	// 		cpe_60_0,CP___PAGEID=100
	// 		CPNEWWIN:WindowName^params@CP___
	// displaylink is the server relative URL or fully qualified URL
	windowname = "";
	windowparams = "";

	// "CPNEWWIN:" & NewWindowName & "^" & params & "@" & linkStruct.LinkURL;
	pos = link.indexOf ("CPNEWWIN:");
	if (pos != -1)
	{
		pos1 = link.indexOf ("^");
		windowname = link.substring (pos+9, pos1);
		pos2 = link.indexOf ("@");
		windowparams = link.substring (pos1 + 1, pos2);
		link = link.substring (pos2 + 1, link.length);
	}

	if( displaylink && displaylink != "" )
	{		
		if (windowname == "")
			window.location = displaylink;
		else
		{
			windowparams = FormatWindowParams(windowparams);
			window.open (displaylink, windowname, windowparams);
		}
	}
	else
	{
		if (link.indexOf ("CP___") != -1)
		{
			targetLink = link;

			if (link.indexOf ("CP___") != -1)
			{


				httpPos = -1;
				commaPos = link.indexOf(",");
				if (commaPos != -1)
				{
					targetUrl = link.substr(commaPos + 1);
					if (targetUrl.indexOf("://") != -1 || targetUrl.indexOf("/") == 0)
					{
							httpPos = commaPos + 1;
					}		
				}
				 
				if (httpPos != -1)
					targetLink = link.substr(httpPos);
				else
					targetLink = jsDlgLoader + "?url=/commonspot/utilities/handle-link.cfm&thelink=" + link;

				if (windowname == "")
					window.location = targetLink;
				else
				{
					windowparams = FormatWindowParams(windowparams);
					window.open (targetLink, windowname, windowparams);
				}
			}
		}
		else
		{
			if (windowname == "")
				window.location = link;
			else
			{
				windowparams = FormatWindowParams(windowparams);
				window.open (link, windowname, windowparams);
			}
		}
	}
}
function onLoadComplete()
{
}
function doWindowOpen(href,name,params)
{
	window.open (href, name, params);
}
function FormatWindowParams(windowparams)
{
	if (windowparams.indexOf(":loc=") != -1 || windowparams.indexOf(":ww=") != -1 || windowparams.indexOf(":hh=") != -1 ||
	    windowparams.indexOf(":left=") != -1 || windowparams.indexOf(":top=") != -1)
	{
		windowparams = substringReplace(windowparams,':left=',',left=');
		windowparams = substringReplace(windowparams,'left=','left=');
		windowparams = substringReplace(windowparams,':ww=',',width=');
		windowparams = substringReplace(windowparams,'ww=','width=');
		windowparams = substringReplace(windowparams,':hh=',',height=');
		windowparams = substringReplace(windowparams,'hh=','height=');
		windowparams = substringReplace(windowparams,':loc=',',location=');
		windowparams = substringReplace(windowparams,'loc=','location=');
		windowparams = substringReplace(windowparams,':dir=',',directories=');
		windowparams = substringReplace(windowparams,'dir=','directories=');
		windowparams = substringReplace(windowparams,':tb=',',toolbar=');
		windowparams = substringReplace(windowparams,'tb=','toolbar=');
		windowparams = substringReplace(windowparams,':stat=',',status=');
		windowparams = substringReplace(windowparams,':mb=',',menubar=');
		windowparams = substringReplace(windowparams,':sb=',',scrollbars=');
		windowparams = substringReplace(windowparams,':rs=',',resizeable=');
	}
	return windowparams;
}
function substringReplace(source,pattern,replacement)
{
	var pos = 0;
	var target="";
	while ((pos = source.indexOf(pattern)) != (-1))
	{
		target = target + source.substring(0,pos) + replacement;
		source = source.substring(pos+pattern.length);
		pos = source.indexOf(pattern);
	}
	return (target + source);
}

// handle scheduled element links
/* mod by Todd will not work
function show_sched_element(cid,id)
{
	target_el = "sched_elements_" + cid + "_" + id;
	evalCol = String(self.element_collection);
	dispFlag = false;
	tmp = "";
	if  (evalCol != 'undefined')
	{
		str = self.location.search;
		for(i=0;i<self.element_collection.length;i++)
		{
			if  (self.element_collection[i].id == target_el)
			{
				dispFlag = true;
				if  (str == '' || str.indexOf(target_el) == -1)
				{
					tmp = "?forceElement=true&" + target_el + '=1';
					self.location.search = tmp;
				}
			}
		}
	}

	if  (!dispFlag)
		alert("You do not have access to the requested element.")
	else
	{
		setTimeout('doReload(tmp)',500);
	}
}
*/
// handle scheduled element links
function show_sched_element(eid)
{
	evalCol = String(self.element_collection);
	dispFlag = false;
	tmp = "";
	if  (evalCol != 'undefined')
	{
		str = self.location.search;
		
		arrCtrls = new Array;
		for (i=0;i < self.element_collection.length;i++){
			sitem = self.element_collection[i];
			if (sitem.id == eid)
				arrCtrls[arrCtrls.length] = sitem.ctrl + "-" + eid;
		}

		dispFlag = arrCtrls.length != 0;
		// Look for the existing forceElement array		
		if (str != ""){
			qArray = str.split("&");
			forceElement = new Array();
			forceElementIndex = -1;
			for(i=0; i < qArray.length; i++)
			{
				el = qArray[i];
				qel = el.split('=');
				if  (qel.length == 1)
					qel[1] = '';
				if  (qel[0] == 'forceElement' || qel[0] == '?forceElement'){
					forceElement = qel[1].split(",");					
					forceElementIndex = i;
					break;
				}
			}	
			
			if (0 == forceElement.length)
				newLocation = str + "&forceElement=" + arrCtrls.join(",");
			else{	// There is a forceElement
				arrCtrlsCnt = arrCtrls.length;
				for (i=0;i < forceElement.length;i++){				// Loop over the existing elements
					iel = forceElement[i].split("-");
					for (j=0;j < arrCtrlsCnt;j++){
						jel = arrCtrls[j].split("-");
						if (jel[0] == iel[0])									// Is it the same control
							break;
					}
					if (j == arrCtrls.length)								// We reached the end  and did not find the control
						arrCtrls[arrCtrls.length] = forceElement[i];
				}
				qel = qArray[forceElementIndex].split("=");
				qArray[forceElementIndex] = qel[0] + "=" + arrCtrls.join(",");
				newLocation = qArray.join("&");
			}
		}
		else
			newLocation = "?forceElement=" + arrCtrls.join(",");
			
		self.location.search = newLocation;
	}

	if  (!dispFlag)
		alert("You do not have access to the requested element.")
}

var loopcnt = 0;
function doReload(param)
{
	if (self.location.search == param)
	{
		self.location.reload();
		loopcnt = 0;
	}
	else
	{
		if (loopcnt > 10)
			return 0;
		setTimeout('doReload(tmp)',500);
		loopcnt = loopcnt + 1;
	}
	return 1;
}
