var currentALink = null;
var bFirstSetAdCall = true;
var strAdSrc = 'blank.gif';
var strAdRef = '';
var RSTR_EXPAND_COLLAPSE = 'Click to expand/collapse';


function wfErrorHandler(errorMessage,url,line) 
{
//	window.status = 'Webforia: error running external scripts';
	return true;
}
onerror = wfErrorHandler; 

function isNS()
{
	return (document.layers!=null);
}

function getAppendage()
{
	var strApp = null;
	if(!isNS())
	{
		strApp = document.body.WFAPPENDAGE;
	}
	
	if(null == strApp)
	{
		var result = '';
		var tocFrame = getFrame('', 1);
		if(null!=tocFrame)
		{
			if(isNS())
			{
		    	var iImage = eval("tocFrame.document.images[0]");
		    	result = iImage.name;
			}
			else
			{
				//document.body.WFAPPENDAGE = tocFrame.document.body.WFAPPENDAGE;
				result = document.body.WFAPPENDAGE;
			}
		}
	}
	else
	{
		result = document.body.WFAPPENDAGE;
	}
	
	return result;
}



/**********************************************************************
	Function: wfSetDigestFrameName
	Ret: None
	
	Description: Resets the name of the digest frame to wfdigest
	  It assumes that the digest panel is the LAST frame in the collection
************************************************************************/
function wfSetDigestFrameName()
{
//  if(null == document.body.WFTYPE) return;
  
	var iParentWindow = window.parent;
	if(null == iParentWindow) return;
	
	var index = iParentWindow.frames.length-1;
	if(-1== index)return;
  
	var strA = getAppendage();
	iParentWindow.frames[index].name = 'wfdigest' + strA;
}
  

function wfNSMouseDown(e)
{
	wfSetDigestFrameName();
}

/**********************************************************************
	Function: getFrame
	Args: nIndex(integer); strName(string)
	Ret: window
	
	Description: This function will find the correctly indexed frame in
	  the parent frame collection of the current document.  It is used
	  for calls from any of the presentation mode documents (wftitle, toc, digest).
************************************************************************/
function getFrame(strName, nIndex)
{
  var iFrame = null;
  
  // If the index is specified then this call is from a Webforia frame and
  // we can therefore attempt to just get the indexed frame from the 
  // current window's parent's frameset
  if(null!=nIndex)
  {
	  var frameWindow = window.parent;
	  if(frameWindow.frames.length > nIndex)
	  {
	    iFrame = frameWindow.frames[nIndex];
	    if(null==document.layers)
	    {
		    if((null==iFrame.document.body)||(null==iFrame.document.body.WFTYPE))
		    {
	    	  iFrame = null;
		    }
	    }
	  }
  }
	return (iFrame);
}



/**********************************************************************
	Function: adLoad
	Ret: None
	
	Description: Dummy function to keep backward compatibility.
************************************************************************/
function adLoad(ref)
{
}


/**********************************************************************
	Function: clikker
	Args: SubSectionID, ImageID
	Ret: None
	
	Description: Toggles the subsection show/hide and indicates the state
	by changing the graphic image,
	Subsection hidden - wfu.gif blue right arrow
	Subsection shown - wfd.gif red down arrow 
************************************************************************/
function clikker(a,b) 
{
	event.cancelBubble = true;
	if( a == null || b == null ) return;
		
	/* When copying and pasting in the news body, can get duplicate
		abstracts, this checks for it */
	var num = a.length;
	if(num > 1) return;
	
	if (a.style.display =='') 
	{
		a.style.display = 'none';
		b.src='wfu.gif';		
	}
	else 
	{
 		a.style.display='';
		b.src='wfd.gif';		
	}
}
		
	  
/**********************************************************************
	Function: setAd
	Args: Image, Width, Height, URL Ref
	Ret: None
	
	Description: Sets the Ad in the newsad.html frame and links the URL
	to the image - so you can navigate to the URL by clicking on the image
************************************************************************/
function setAd(ad,wd,ht,ref)
{
	var titleFrame = getFrame("wfTitle", 0);	
	if( titleFrame == null || ad == null || wd == null || ht == null) return;

    if(isNS())
    {
		var iImage = eval("titleFrame.document.images['0']");
		if(null==iImage) return;
		var iLink = eval("titleFrame.document.links['0']");

		if(bFirstSetAdCall)
		{
			// Store off the Default ad properties
			strAdSrc = iImage.src;
			bFirstSetAdCall = false;
			if(null!=iLink)
			{
				strAdRef = iLink.href;
			}
		}

		if(null!=iLink)
		{
			if(''==ref)
			{
				iLink.href = 'javascript:var wfi=0;';
			}
			else
			{
				iLink.href = ref;
			}
		}
      
		iImage.src = ad;
	}
	else
	{
		if(document.body.WFAPPENDAGE != titleFrame.document.body.WFAPPENDAGE)
		{
			return;
		}
		
    	
		var iImage = titleFrame.document.all("WFTITLEADICON");
		  
		if( null != iImage)
		{
			var iParent=iImage.parentElement;
			var bAnchorPresent = (iParent.tagName == 'A');
		    
			iImage.src="blank.gif";
			iImage.width = wd;
			iImage.height = ht;
			iImage.src = ad;
			iImage.removeAttribute("WFDEFAD", 0);
			iImage.style.display = '';
			iImage.WFADLINK = ref;

			if((''==ref) || (null==ref))
			{
				iImage.removeAttribute("WFADLINK");
				iImage.removeAttribute("title");
				iImage.removeAttribute("alt");
				if(bAnchorPresent)
				{
					iImage.style.cursor = 'auto';
					var strOuter = iImage.outerHTML;
					iParent.outerHTML = strOuter;
				}
			}
			else
			{
				iImage.WFADLINK = ref;
				iImage.title = ref;
				if(bAnchorPresent)
				{
					iParent.href = ref;
				}
				else
				{
					// Add the anchor element
					var strAnchor = '<A ID=WFTITLEANCHOR target="wfdigest' + getAppendage() + '" HREF="' + ref + '">' + iImage.outerHTML + '</A>';
					iImage.outerHTML = strAnchor;
				}
			}
		}
	}
}

/**********************************************************************
	Function: setDefAd
	Args: None
	Ret: None
	
	Description: Sets the Ad in the wfTitle frame
************************************************************************/
function setDefAd()
{
	var titleFrame = getFrame("wfTitle", 0);
	if(null==titleFrame) return;
    
	if(isNS())
	{
		var iImage = eval("titleFrame.document.images['0']");
		var iLink = eval("titleFrame.document.links['0']");
      
		if(null!=iImage)
		{
			if(bFirstSetAdCall)
			{
				// Store off the Default ad properties
				strAdSrc = iImage.src;
				if(null!=iLink)
				{
					strAdRef = iLink.href;
				}
				bFirstSetAdCall = false;
			}
			else
			{
				iImage.src = strAdSrc;
				if(null!=iLink)
				{
					iLink.href = strAdRef;
				}
			}
		}
	}
	else
	{
		var iImage = titleFrame.document.all("WFTITLEADICON");
		if(null==iImage)
		{
			return;
		}
  		
		if(null!=iImage.WFDEFAD)
		{
			return;
		}
		iImage.WFDEFAD = 1;
  		
		
		if(null==iImage.WFDEFADNAME)
		{
			// Hide the image as no default exists
			iImage.style.display = 'none';
		}
		else
		{
			var iParent=iImage.parentElement;
			var bAnchorPresent = (iParent.tagName == 'A');
        
			iImage.src = "blank.gif";
			iImage.width = iImage.WFDEFADW;
			iImage.height = iImage.WFDEFADH;
			iImage.src = iImage.WFDEFADNAME;
			iImage.style.display = '';
			  
			if(iImage.WFDEFADLINK==null)
			{
				iImage.removeAttribute('title');
				iImage.removeAttribute("alt");
				iImage.removeAttribute('WFADLINK');
				if(bAnchorPresent)
				{
					iImage.style.cursor = 'auto';
					var strOuter = iImage.outerHTML;
					iParent.outerHTML = strOuter;
				} 			
			}
			else
			{
				iImage.title = iImage.WFDEFADLINK;
				iImage.WFADLINK = iImage.WFDEFADLINK;

				if(bAnchorPresent)
				{
					iParent.href = iImage.WFDEFADLINK;
				}
				else
				{
					// Add the anchor element
					var strAnchor = '<A ID=WFTITLEANCHOR target="wfdigest' + getAppendage() + '" HREF="' + iImage.WFDEFADLINK + '">' + iImage.outerHTML + '</A>';
					iImage.outerHTML = strAnchor;
				}
			}
			  		  
			//iImage.style.display = '';
		}
	}
}


/**********************************************************************
	Function: makeACurrent
	Args: None
	Ret: None
	
	Description: Makes current link in TOC be underlined
************************************************************************/
function makeACurrent(change)
{
	// Have decided to use an unknown attribute of body to store the currentALink as
	// it has become necessary to be able to manipulate this from other frames
  
	if(!isNS())
	{
		if(document.body.currentALink != null)
		{
			document.body.currentALink.className = "WFLINK";
		}
		if(null==change)
		{
			event.srcElement.className = "WFCURRENTLINK";
			document.body.currentALink = event.srcElement;
		} 
	}
}



function hideFocus(inEl)
{
	if(isNS())
	{
		return;
	}
	else
	{
		if(inEl==null)
		{
			document.body.focus();
		}
		else
		{
			inEl.blur();
		}
	}
}


/**********************************************************************
	Function: wfCreatedStoredLoad
	Args: None
	Ret: None
	
	Description: Called by a created content inner frame to allow for 
	  the redirection of the outer frame in export.
	  It will only call stored load when the innerframe is directly
	  off the WFDIGEST panel.
************************************************************************/
function wfCreatedStoredLoad()
{
	if (-1==window.name.toUpperCase().indexOf('WFDIGEST')) 
	{
		storedLoad();
	}
	else
	{
		return;
	}
}


/**********************************************************************
	Function: NSStoredLoad
	Args: None
	Ret: None
	
	Description: This will reset the section graphics which are wrong
	  in Netscape as all sections are expanded.
************************************************************************/
function NSStoredLoad(strInHREF)
{
	// Check whether this is the digest page
	var strLocation = document.location.href.toUpperCase();
  
	if(-1==strLocation.indexOf('WFDIGEST'))
	{
		return;
	}
  
	var images = document.images;
  
	for(var i=0; i<images.length; i++)
	{ 
		if(-1!=images[i].src.indexOf('wfu.gif'))
		{
			images[i].src="wfd.gif";
		}
	}  
}


/**********************************************************************
	Function: storedLoad
	Args: None
		Ret: None
	
	Description: Called by a content page when it loads up.  This will
  cause the corresponding TOC item to be underlined as well as setting
  the tooltip to the correct detail level.
************************************************************************/
function storedLoad(strInHREF)
{
	if(isNS())
	{
		NSStoredLoad();
		return;
	}
  

	var tocFrame = getFrame("wftoc", 1);	
	
	if(null!=strInHREF)
	{
		// This means we have been called from a WFTOC or WFDIGEST page
		// Before doing anything we need to check that the function is being
		// called from a page which belongs to this report
	    if(null!=tocFrame)
	    {
		    if(tocFrame.document.body.WFAPPENDAGE != document.body.WFAPPENDAGE)
		    {
			    return;
		    }
	    }
	}
	else
	{
	    // Only place the trim icon if the document is one of ours.  In this case the
	    // storedLoad will be called with a null parameter.
		ensurePlacementOfTrimIcon(document);
	}
	

	if(null==tocFrame)
	{
		return;
	}

	wfSetDigestFrameName();


	var iLink = null;
	var strHREF;
	var bFound=false;
	var isDigest=false;
	var myLinks = tocFrame.document.links;
	var tmpStr = '';
	var bStored;
	var strTmpHREF;
	
	if(null==strInHREF)
	{
		strHREF = unescape(document.location.href);
	  
		// Check for a trimmed document
		if(strHREF.indexOf('_WFO.htm',0)!=-1)
		{
			var nLen = strHREF.length - 8;
			strHREF = (strHREF.substring(0,  nLen) + '.htm');
		}	  
	  
		bStored = true;
	}
	else
	{
		iLink = tocFrame.document.all(strInHREF);
		bFound = (iLink != null);
		if(!bFound)
		{ 
			strHREF = unescape(strInHREF);
		}
		bStored = false;
	}


	for(var i=0; i< myLinks.length; i++)
	{
		myLinks[i].className = 'WFLINK';
		if(!bFound)
		{
			strTmpHREF=unescape(myLinks[i].href);
			if(-1!=strTmpHREF.indexOf(strHREF))
			{
				if(0==i)
				{
					isDigest=true;
				}
				iLink = myLinks[i];
				bFound = true;
			}
		}
		if(isDigest)
		{
			if(null!=myLinks[i].shortToolTip)
			{
				myLinks[i].title = myLinks[i].shortToolTip;
			}
		}
    	else
    	{
			if(null!=myLinks[i].longToolTip)
			{
				myLinks[i].title = myLinks[i].longToolTip;
			}
		}
	}
  
	if(null!=iLink)
	{
		iLink.className = 'WFCURRENTLINK';
		tmpStr = iLink.parentElement.section;
		clikkerEXT(tmpStr, tocFrame.document);
		iLink.scrollIntoView();
		document.body.focus();
    
		if(bStored)
		{
			var iTrimIcon = findNamedElementInDocument("WFTRIMIMG", window);
			if(null!=iTrimIcon)
			{
				// Check if this is the original page
				iTrimIcon.alt = 'Click to see original page.\n' + iLink.WFCOPYRIGHT;
				iTrimIcon.outerHTML = iTrimIcon.outerHTML;
			}
		}    
	}
  
	// ATS916 - Add the expand/collapse on the sections
	if(isDigest)
	{
		iImages = document.images;
		for(i=0;i<iImages.length-1;i++)
		{
			var strId = iImages[i].id.toUpperCase();
			if(strId.length>12)
			{
				var strStart = strId.substring(0,8);
				var strEnd = strId.substring(strId.length-4);
				if(strStart=='WFDIGSEC' &&  strEnd=='_PIC')
				{
					var iParent = iImages[i].parentElement;
					if(iParent.tagName=='SPAN')
					{
						iParent.title = RSTR_EXPAND_COLLAPSE;
					} 
				}
			}
		}
	}

	// Dynamic Images solution.
	var numImages = document.images.length;
	var imageNameArray = new Array();
	for (var i=0; i<numImages; i++)
	{
		var imageNameSrc = document.images[i].src;
		var imageNameLoc = imageNameSrc.lastIndexOf("/");
		var imageName = imageNameSrc.substr(imageNameLoc + 1);
		imageNameArray[i] = imageName;
	}

	var allMetaTags = document.all.tags("META");
	var numMetaTags = allMetaTags.length;
	for (var j=0; j<numMetaTags; j++)
	{
		var metaName = allMetaTags[j].name;
		if (metaName == "WFELEMENT")
		{
			var metaContent = allMetaTags[j].content;
			var separatorLoc = metaContent.indexOf(",");
			var contentPart1 = metaContent.substr(0,separatorLoc);
			if (contentPart1.substr(contentPart1.length - 3) != ".js")
			{
				var contentPart2 = metaContent.substr(separatorLoc+1);
				var contentNameLoc = contentPart1.lastIndexOf("/");
				var contentName = contentPart1.substr(contentNameLoc + 1);
				for (var k=0; k<numImages; k++)
				{
					if (contentName == imageNameArray[k])
					{
            			document.images[k].src = contentPart2;
					}
				}
			}
		}
	}
}

/**********************************************************************
	Function: findNamedElementInDocument
	Args: strElement, topper
	Ret: element
	
	Description: This will find a named element in a specified document.
	  If it is a BODY document it will find the element in the ALL collection
	  else it will recursively call itself on its frame documents.
************************************************************************/
function findNamedElementInDocument(strElement, topper)
{
	var iElement = topper.document.all(strElement);
	if(null==iElement)
	{
		
	    if(null!=topper.document.body)
	    {
			if('BODY'==topper.document.body.tagName)
			{
				return;
			}
	    }
		// Look in any frame documents
		var frames = topper.frames;
		if(frames.length==0)return null;
		for(var i=0; i<frames.length; i++)
		{
			tmpFrame = frames[i];
	  	
			iElement = findNamedElementInDocument(strElement, tmpFrame);
			if(null!=iElement)
			{
				return(iElement);
			}
		}
	}
	else
	{
		return(iElement);
	}
	return(null);
}


/**********************************************************************
	Function: clikkerEXT
	Args: section: section ID, tocDoc: TOC Document
	Ret: None
	
	Description: This will cause the section containing the current document
	to be expanded up to the root section.
************************************************************************/
function clikkerEXT(section, tocDoc)
{
	if(null==section || null==tocDoc)return;
  
	var myTable = tocDoc.all(section + '_TABLE');
	var myPIC = tocDoc.all(section + '_PIC');
	var mySection = tocDoc.all(section);

	if(myTable==null || null==myPIC)return;

	myTable.style.display = '';
	myPIC.src = 'wfd.gif';

	if(mySection.parentSection != 'NONE')
	{
		clikkerEXT(mySection.parentSection, tocDoc);
	}
}


/**********************************************************************
	Function: ensurePlacementOfTrimIcon
	
	Description: This function places the Trim Icon
				An Iframe will overwrite the icon so we have 
				to move it away from them
************************************************************************/
function ensurePlacementOfTrimIcon(iDoc)
{


	if (null==iDoc) return;
	var iTrimIcon = findNamedElementInDocument('WFTRIMICON', iDoc.parentWindow);

	if (null==iTrimIcon) return;
	iTrimIcon.style.posTop = 10;

	var nIconPos = iTrimIcon.style.posTop;
	var nBottomestFrameOffset = 0;
	
	// Get all the IFRAMES
	var iCol = iTrimIcon.document.all.tags('IFRAME');

	for(var i=0; i < iCol.length ;i++)
	{ 	
   		iEl = iCol.item(i);

		var tmpEl = iEl;
		var bFrameNotDisplayed = false;
		var nIFrameOffsetTop = 0;
		/**************************************************************
		** Recurse up the element Hierarchy.
		** Check whether this element is displayed 
		** and calulate the correct offset
		**************************************************************/
		while ((null!=tmpEl.parentElement) && ('BODY' != tmpEl.tagName))
		{
			if (null == tmpEl.offsetTop || null == iEl.offsetHeight) return;

			if (tmpEl.style.display == 'none')
			{
				bFrameNotDisplayed = true;
			}
			nIFrameOffsetTop += tmpEl.offsetTop
			tmpEl = tmpEl.parentElement;
		}
		if(bFrameNotDisplayed )
		{
		    continue;
		}
		


		/**************************************************************
		** Check that this is the lowest Frame
		** If not then the page is doing something funny (but legal)
		** so just go to the bottom of all the IFrames
		**************************************************************/
		var nIFrameOffsetBottom =  (nIFrameOffsetTop * 1) + (iEl.offsetHeight * 1);
		if(nBottomestFrameOffset < nIFrameOffsetBottom )
		{	
			nBottomestFrameOffset = nIFrameOffsetBottom
		}

		/* ****************************************************************
		** Determine if there is an overlap between the bottom of the icon 
		** and the top of this frame.
		** ***************************************************************/
		if(nIconPos + 44 > nIFrameOffsetTop)
		{	
			nIconPos = nBottomestFrameOffset +20;
		}

	}//for

	iTrimIcon.style.posTop = nIconPos;
	return;
}


