// Copyright 2008: Thomson Reuters Global Resources. All Rights Reserved. Proprietary and Confidential information of TRGR. Disclosure, Use or Reproduction without the written authorization of TRGR is prohibited.
//The arrownav file contains the arrownav function as well as other code used for 
//enhanced part,rank,term and best navigation.

var gsarrowdisable = 'False';//global variable used to turn off arrownavigation for greyed arrows.
var usesRegions = false;  //  variable used to indicate whether product uses frames or regions. Default as frames for web2.


//Performs an action depending on which arrow is clicked
//This function takes the type of arrow clicked (forward or back)
// and the document information (rank and part)and determines the URL
// for all possible navigations. Functions in an included javascript file
// (Backclick) and (ForwardClick) are called to either scroll the page or
// return a value that signals arrownav to navigate to result.aspx for new
// documents and a screen refresh.

function arrownav(strNavMode,arrowtype,strnavnewdoc,Numrslts,Rank,TrmNvPath,Urlnext,Urlprev,Urlsame,lPreviousbeginsdu,lBeginSDU,lNextbeginsdu, lCurrentPart, lNumParts, lSDUs, locatedFirstRank, subRank, bOnLoad, stayInDoc, baseURL, locatedLastRank, atFirstBest, atLastBest) 
{
    atFirstBest = atFirstBest == "True";
    atLastBest = atLastBest == "True";
	if (gsarrowdisable == 'False')//check to see if arrow has been disabled 
	{		
		var strURL;
		if(baseURL)
		{
			strURL = baseURL;
		} 
		else
		{
			strURL = location.href;
		}
		
		var strType;
		var strSelectVal = "";
		var objfrm;
		var strDirection = "";
		
		if (window.document.form1)
		{
			objfrm = window.document.form1;
		}
		else
		{
			objfrm = window.document.formControl;
		}

		if (strNavMode)
		{
			 strSelectVal=strNavMode;
		}else{
			 strSelectVal='Term';
		}
		
		// check for page js variable, registered through documenttext control and decide whether product uses regions/frames
		 if(typeof textregion != "undefined" && textregion != null)	
		 { 
			usesRegions = true;	
		 }
		 else
		 { 
			usesRegions = false; 
		 }
		
		
		
		
		//---------------------------------------PARTS---------------------------------
		if (strnavnewdoc=='no' && (strSelectVal=='parts'))
		{
			objfrm.arwtype.name=arrowtype+'.x';
			objfrm.arwtype.value='5';//bogus .x val to satisfy document.wl
			
			objfrm.selectval.value=strSelectVal; //sets hidden form box to docaction value;
			objfrm.docaction.value=strNavMode;
				
			objfrm.submit();
			
		} 
		//---------------------------------------RANK---------------------------------
		else if(strnavnewdoc=='no' && strSelectVal == 'rank') 
		{
			//Need to clear a lot of parameters so that navigating by rank works correctly, if certain
			//parameters are not removed, then it is very likely the navigation will skip documents.
			//This removes docaction=rank and the adds it again, however this code may be moved up so lets keep it there
			strURL = strURL.replace("strparmarrowtype=asnext","");
			strURL = strURL.replace("strparmarrowtype=asprev","");
			strURL = strURL.replace("docaction=term","");
			strURL = strURL.replace("docaction=best","");
			strURL = strURL.replace("docaction=rank","");
			strURL = strURL.replace("direction=Next", "");
			strURL = strURL.replace("direction=Prev", "");
			strURL = strURL.replace("direction=BNext", ""); //TODO: Why does best get its own direction?
			strURL = strURL.replace("direction=BPrev", "");
			strURL = strURL.replace("dmm=T", "");
			strURL = strURL.replace("dmm=D", "");
			strURL = strURL.replace("dmm=E", "");
			while(strURL.indexOf("&&") > -1)
			{
				strURL = strURL.replace("&&", "&");
				strURL = strURL.replace("?&", "?");
			}
			
			//Add url parameters
			if(arrowtype == "asnext")
			{ 
				strURL = strURL + "&asnext.x=y"
			} else
			{
				strURL = strURL + "&asprev.x=y"
			}
			if(strURL.indexOf("docaction=rank") < 0)
			{
				strURL = strURL + "&docaction=rank";
			}
			
			//-------------------------------------- full doc or shell navigation --------------------------------
			//if the nav path has sasd (shell document) in it, then be sure this is passed through in the querystring
			if(TrmNvPath)
			{
				if(TrmNvPath.indexOf("cnt=sasd") > -1)
				{
					//found, go ahead and set 
					if(strURL.indexOf("cnt=DOC") > -1){
						strURL = strURL.replace("cnt=DOC","cnt=sasd");
					}else{
						strURL = strURL.replace("cnt=DOC","cnt=sasd");
					}	
				}
			}
			
			urlParms = strURL.split("?")[1];
			
			if(baseURL)
			{
				location.href = strURL;
			} else if (parent.bLinkViewer) {
				//Make sure we are targeting result/previewmain.aspx !
				urlPath = location.href.split("?")[0].toLowerCase();	
				urlPath = urlPath.substring(0, 8 + urlPath.substring(8, urlPath.length).indexOf("/")); // https:// is 8
				urlPath = urlPath + "/result/previewmain.aspx?";						
				parent.m_parentframe.changeURL(urlPath + urlParms,"forward");
			} else {
				if (parent.name == 'content')
				{	
					//Make sure we are targeting result/result.aspx !
					urlPath = parent.parent.document.location.href.split("?")[0].toLowerCase();	
					urlPath = urlPath.substring(0, 8 + urlPath.substring(8, urlPath.length).indexOf("/")); // https:// is 8
					urlPath = urlPath + "/result/result.aspx?";
					parent.parent.document.location.href = urlPath + urlParms;	
				} else {
					if (usesRegions)
					{
						//Make sure we are targeting result/result.aspx !
						urlPath = document.location.href.split("?")[0].toLowerCase();
						urlPath = urlPath.substring(0, 8 + urlPath.substring(8, urlPath.length).indexOf("/")); // https:// is 8
						urlPath = urlPath + "/result/result.aspx?";
						document.location.href = urlPath + urlParms;
					}
					else
					{
						//Make sure we are targeting result/result.aspx !
						urlPath = parent.document.location.href.split("?")[0].toLowerCase();
						urlPath = urlPath.substring(0, 8 + urlPath.substring(8, urlPath.length).indexOf("/")); // http:// is 8
						urlPath = urlPath + "/result/result.aspx?";
						parent.document.location.href = urlPath + urlParms;
					}
				}
			}
		}
		//---------------------------------------BEST OR TERM---------------------------------
		else if (strSelectVal=='best' || strSelectVal=='term')
		{	
			if (strSelectVal=='best'){
				type='BestSection' //translation of varname from select box to navdoc.js parms;
			}else if(strSelectVal=='term'){
				type = 'SearchTerm';
			}				

			//The previous SDU for SearchTerms is the last SDU of the previous part, not the begining sdu of the previous part!
			lPreviousbeginsdu = lPreviousbeginsdu == "0" ? lPreviousbeginsdu : parseInt(lBeginSDU);
				
			//alert('arrownav  lSDUs=' + lSDUs);
			//----------------------------------------------------ASPREV ARROW----------------
			if (arrowtype == "asprev")//Check arrow direction
			{
				//SET PREV DIRECTION
				if (strSelectVal=='best'){
				    if(atFirstBest) { lPreviousbeginsdu = lBeginSDU; }
					strDirection = 'BPrev';
				}else if(strSelectVal=='term'){
					strDirection = 'Prev';
				}

				if (lPreviousbeginsdu >= 1) 
				{
					//For new media streaming features. swfobject_v8.07.js has the methods below.
					if(strURL.indexOf("&dmm") != -1)
					{
						var ts = GetVideoTime();
						if(ts > 0)
						{
							var hasTS = GetUrlParameter(TrmNvPath,"ts");
							if(hasTS)
							{
								TrmNvPath = SetUrlParameter(TrmNvPath,"ts",hasTS,ts)
							}
							else
							{
								TrmNvPath = TrmNvPath + "&ts="+ts;
							}
						}
					}
					
					//-------------------------------------- full doc or shell navigation --------------------------------
					//if the nav path has sasd (shell document) in it, then be to change it to DOC for proper term navigation
					if(TrmNvPath)
					{
						if(TrmNvPath.indexOf("cnt=sasd") > -1)
						{
							TrmNvPath = TrmNvPath.replace("cnt=sasd","cnt=DOC");
						}
					}
					
					strURL = TrmNvPath+Urlsame+'&selctval='+strSelectVal+'&strparmarrowtype='+arrowtype + '&BeginSDU=' + lPreviousbeginsdu + '&CurrentPart=' + lCurrentPart + '&NumParts=' + lNumParts + '&NumSDUs=' + lSDUs + '&Direction=' + strDirection + '&docaction=' + strNavMode;
				} else 
				{
					//Going to a new document so clear out all the dmm params.
					strURL = strURL.replace("dmm=T", "");
					strURL = strURL.replace("dmm=D", "");
					strURL = strURL.replace("dmm=E", "");
					TrmNvPath = TrmNvPath.replace("dmm=T", "");
					TrmNvPath = TrmNvPath.replace("dmm=D", "");
					TrmNvPath = TrmNvPath.replace("dmm=E", "");
					if (Rank == 1 && lPreviousbeginsdu == 0 || subRank > 0 || locatedFirstRank == Rank && lPreviousbeginsdu == 0) {
						strURL = '' //If we can't move further back we set parm to empty string. Function sends //alert. 
					} else {
						     strURL = TrmNvPath+Urlsame+'&selctval='+strSelectVal+'&strparmarrowtype='+arrowtype+ '&CurrentPart=' + lCurrentPart + '&NumParts=' + lNumParts + '&NumSDUs=' + lSDUs + '&Direction=' + strDirection + '&docaction=' + strNavMode; 
							}
				}
				//We still want BackClick to execute, however after it is done check stayInDoc
				if (BackClick(type,strnavnewdoc,strURL, bOnLoad, stayInDoc, usesRegions)=='yes' && stayInDoc != "true")//runs func in navdoc.js - determines we are at last search term and need to move
				{
					strnavnewdoc = 'yes' //sets var for request string;
					strURL = strURL+'&strparmnavnewdoc='+strnavnewdoc;
					//alert('parent.document.location.href=' + strURL);
					if (parent.bLinkViewer) {
						strURL = strURL.replace("result.aspx?","previewmain.aspx?");
						parent.m_parentframe.changeURL(strURL,"forward");
					} else {
						if (parent.name == 'content')
						{
							parent.parent.document.location.href=strURL;
						} else {						  
							    parent.document.location.href=strURL;
						}
					}
				}else{
					strnavnewdoc = 'no';
				}
			}
			//----------------------------------------------------ASNEXT ARROW----------------
			else if (arrowtype == "asnext")//Check arrow direction
			{

				//SET NEXT DIRECTION
				if (strSelectVal=='best'){	
				if(atLastBest) { lNextbeginsdu = lBeginSDU; }
					strDirection = 'BNext';
				}else if(strSelectVal=='term'){
					strDirection = 'Next';
				}

				if (lNextbeginsdu > 1) 
				{
					//For new media streaming features. swfobject_v8.07.js has the methods below.
					if(strURL.indexOf("&dmm") != -1)
					{
						var ts = GetVideoTime();
						if(ts > 0)
						{
							var hasTS = GetUrlParameter(TrmNvPath,"ts");
							if(hasTS)
							{
								TrmNvPath = SetUrlParameter(TrmNvPath,"ts",hasTS,ts)
							}
							else
							{
								TrmNvPath = TrmNvPath + "&ts="+ts;
							}
						}
					}
					
					//-------------------------------------- full doc or shell navigation --------------------------------
					//if the nav path has sasd (shell document) in it, then be to change it to DOC for proper term navigation
					if(TrmNvPath)
					{
						if(TrmNvPath.indexOf("cnt=sasd") > -1)
						{
							TrmNvPath = TrmNvPath.replace("cnt=sasd","cnt=DOC");
						}
					}
					
					strURL = TrmNvPath+Urlsame+'&selctval='+strSelectVal+'&strparmarrowtype='+arrowtype + '&BeginSDU=' + lNextbeginsdu + '&CurrentPart=' + lCurrentPart + '&NumParts=' + lNumParts + '&NumSDUs=' + lSDUs + '&Direction=' + strDirection + '&docaction=' + strNavMode;
				}
				else if (lNextbeginsdu==1 || lNextbeginsdu==0) 
				{
					//Going to a new document so clear out all the dmm params.
					strURL = strURL.replace("dmm=T", "");
					strURL = strURL.replace("dmm=D", "");
					strURL = strURL.replace("dmm=E", "");
					TrmNvPath = TrmNvPath.replace("dmm=T", "");
					TrmNvPath = TrmNvPath.replace("dmm=D", "");
					TrmNvPath = TrmNvPath.replace("dmm=E", "");
					//If we can't move further forward set parm to empty string so ForwardClick function will send alert.
					if (Numrslts == 1 || Numrslts == Rank || subRank > 0 || locatedLastRank == Rank) {
						strURL = '';
					} else {
						strURL = TrmNvPath+Urlnext+'&selctval='+strSelectVal+'&strparmarrowtype='+arrowtype+ '&CurrentPart=' + lCurrentPart + '&NumParts=' + lNumParts + '&NumSDUs=' + lSDUs + '&Direction=' + strDirection + '&docaction=' + strNavMode;
					}
				}
				//alert('strURL='+strURL);
	
				//We still want ForwardClick to execute, however after it is done check stayInDoc
				if (ForwardClick(type,strnavnewdoc,strURL, bOnLoad, stayInDoc, usesRegions)=='yes' && stayInDoc != "true") //runs func in navdoc.js - determines we are at last search term and need to move
				{
					strnavnewdoc = 'yes' //sets var for request string;
					strURL = strURL+'&strparmnavnewdoc='+strnavnewdoc;
					if (parent.bLinkViewer) {
						strURL = strURL.replace("result.aspx?","previewmain.aspx?");
						parent.m_parentframe.changeURL(strURL,"forward");
					} else {
						if (parent.name == 'content')
						{
							parent.parent.document.location.href=strURL;
						} else {						
							  parent.document.location.href=strURL;
						}
					}
				}else{
					strnavnewdoc = 'no';	
				}
			}//alert('strURL=' + strURL);
		}//end termnav arrows
	}//end global arrowdisable check
 }//end function


//calls scriptinit
function processdocaction(stractionvalue, strarrowtype, stayInDoc)
{	
	scriptinit(stractionvalue, strarrowtype,'no', 'FALSE', stayInDoc);
}

	
// Sets a global variable (gsarrowdisable) to enable or disable usage of nav. arrows.
// Uses type of arrow and type of navigation as well as rank and SDU parameters	
function arrowshow(strNavMode,initparmarrowtype,Numrslts,lRank,lPreviousbeginsdu,lNextbeginsdu,locatedFirstRank, locatedLastRank) 
{ 
	gsarrowdisable = 'False';
	if (strNavMode=='rank')
	{
		if (initparmarrowtype=='asprev')
		{	//alert('entered rank for asprev')
			if (lRank==1 || lRank==locatedFirstRank) 
			{//alert('entered lRank==1')
				gsarrowdisable = 'True';				 
			}
		}
		else if (initparmarrowtype=='asnext')
		{
			if (lRank==Numrslts || lRank==locatedLastRank)
			{//alert('entered rank for asprev')
				gsarrowdisable = 'True';
			}//alert('entered rank for asprev but didnt change arrowdisable')
		}
	}
	else if(strNavMode=='parts')
	{
		if (initparmarrowtype=='asprev')
		{
			if (lPreviousbeginsdu == 0 ) 
				{
					gsarrowdisable = 'True';
				}
		}
		else if(initparmarrowtype=='asnext')
		{
			if (lNextbeginsdu == 0) 
			{
				gsarrowdisable = 'True';	
			}//end nextsdu check
		}//end is 'asnext'
	}//end is 'parts'
}//end arrowshow
scriptrun = true; //sets global var to show script has loaded - checked in result/result.aspx

// Looks for the last <a> with 'looking' in the name.
// Inputs
// a = link being clicked.
// looking = name of anchors.  Anchor must start with 'looking'.
function FindLastAnchorName(a, looking, isMigrated) {
    var aList = document.getElementsByTagName("a");
    var controlCount = aList.length;
    var bestNumber = 0;
    var bestName = "";
    var previousBestNumber = 0;
    var previousBestName = "";

    for (var i = 0; i < controlCount; i++) {
        var element = aList[i];
        var name = element.name;
        if (name.length == 0) {
            continue;
        }
        name = name.toUpperCase();
        if (name.indexOf(looking) != -1) {
            var stringNumber = name.substring(looking.length);
            if (stringNumber.length > 0) {
                var number = parseInt(stringNumber);
                if (!isNaN(number) && (number > bestNumber)) {
                    previousBestNumber = bestNumber;
                    previousBestName = bestName;
                    bestNumber = number;
                    bestName = element.name;
                }
            }
        }
    }
    if (bestNumber > 0) {
        if (isMigrated) {
            a.href = "#" + bestName;
        }
        else {
            a.href = "#" + previousBestName;
        }
    }
}

	
if(typeof(Sys) !== "undefined") {
	Sys.Application.notifyScriptLoaded();
}
