/****************************************************************************************
'
' JAVASCRIPT.JS
'
' Name:         rvaLinks.js
' Description:  Creates RVA hyperlinks using rva substring stored in user cookie and 
'               updates cookie upon page load
'           
*****************************************************************************************
'
' FUNCTIONS
'
' Name:         GetCookieVal
' Description:  Gets RVA substring from from cookie using index number of RVA
' Accepts:      offset - index number in string for the RVA cookie
' Returns:      RVA Cookie substring
'
' ...
'
' Name:         GetCookie
' Description:  Searches for index number of RVA String within the Cookie
' Accepts:      name - name of substring to search for
' Returns:      If substring is found returns substring else returns an empty string
'
' ...
'
' Name:         GetPageMatch
' Description:  Matches current page with matching expression string
' Accepts:      n/a
' Returns:      RVA entry (display name ~ URL) of matched page
'
' ...
'
' Name:         GetRVAList
' Description:  Uses GetCookie method to retieve RVA substring then splits the substring
'               into each link properties, creating an array of Links for the rva Control
' Accepts:      N/A
' Returns:      rvaArray - Array of links to be used for the creation of hyperlinks
'
' ...
'
' Name:         CreateLinks
' Description:  Using array created from rva substring, creates html hyperlinks with
'               rva cookie string. Gets URL and Display name of each link and generates
'               hyperlinks in html format.
' Accepts:      N/A
' Returns:      N/A
'
' ...
'
' 
' Name:         MatchPage
' Description:  Matches/Inserts current page into RVA cookie string, creates RVA string
'               to be passed to update function of cookie
' Accepts:      N/A
' Returns:      N/A
'
' ...
'
' Name:         UpdateCookie
' Description:  Retrieves Cookie string passed and updates cookie w/ string recieved
' Accepts:      cookieString - Updated RVA cookie string to be stored in cookie
' Returns:      N/A
'
*****************************************************************************************
'
' REVISION HISTORY
'
' Date Created: 01/05/2005
' Author:       NND
' 
' Revision Date      Author         Description
' -------------      ------         -----------
' 7/18/2005			 NND			Added checking for cms posting name to eliminate
'									duplicate url's in RVA.				 
' 9/16/2005		     NND			Updated Code ignore querystrings, anchor links and 
'									file extensions from the matching of URL's.
' 09/05/2006		 DAV			Updated for RMT 1557.
****************************************************************************************/
<!--
//Set cookie substring variable for creation of RVA links string
var _cookieSubstring;
//Set Current Page URL and Title variable seperated by hyphen
var _currentPage;
//Set matching expression to current page URL
var _matchingExpr = unescape(document.location.href.toLowerCase());
if(_postingDisplayName == "" || _postingDisplayName == null)
{
	_currentPage = document.title.replace("Accenture: ","") + "~" + _matchingExpr; 
}
else
{
	_currentPage = _postingDisplayName + "~" + _matchingExpr; 
}
//Set RVA key value to be used in accessing and saving RVA data
var _rvaCookieKey = "RVA";
var _rvaLinks = new Array();
var _rvaLinkDefinitions = new Array(); 
var _rvaArray = new Array();
//Set Rva Links max Length variable
var _rvaMaxLength = 4;
//Set checker variable to determine if condition is false
var	_checker = -1;

//Remove HTTP protocol from matching expression	
_matchingExpr = _matchingExpr.replace("http://","");
_matchingExpr = _matchingExpr.replace("https://","");

//Remove QueryString from matching expression		
if(_matchingExpr.lastIndexOf("?") != _checker)
{
	_matchingExpr = _matchingExpr.substring(0,_matchingExpr.lastIndexOf("?"));
}

//Remove Anchor from Matching Expression
if(_matchingExpr.lastIndexOf("#") != _checker)
{
	_matchingExpr = _matchingExpr.substring(0,_matchingExpr.lastIndexOf("#"));
}


//if cms page and url end with "/" then append CMS display name through variable
try
{
	//check if cms posting name variable is existing
	if(_postingName != "" || _postingName != null)
	{
		//change posting name string to lower case for comparison
		_postingName = _postingName.toLowerCase();
		//check if posting name is present in current url
		if(_currentPage.indexOf(_postingName) == _checker)
		{
		
			var _queryString = "";
		
			//Remove querystring from URL		
			if(_currentPage.lastIndexOf("?")!=_checker)
			{
				_queryString = _currentPage.substring(_currentPage.lastIndexOf("?"),_currentPage.length);
				_currentPage = _currentPage.substring(0,_currentPage.lastIndexOf("?"));
			}
						
			//Remove anchor from URL
			if(_currentPage.lastIndexOf("#")!=_checker)
			{
				_queryString = _currentPage.substring(_currentPage.lastIndexOf("#"),_currentPage.length);
				_currentPage = _currentPage.substring(0,_currentPage.lastIndexOf("#"));
			}
			
			//append posting name to url after forward slash
			if(_currentPage.lastIndexOf("/") + 1 == _currentPage.length)
			{
				_currentPage = _currentPage + _postingName + _queryString;
				_matchingExpr = _matchingExpr + _postingName;
			}
			//append forward slash and posting name to url
			else
			{
				_currentPage = _currentPage + "/" + _postingName + _queryString;
				_matchingExpr = _matchingExpr + "/" + _postingName;
			}
		}
	}
}
catch(err)
{
}

_rvaLinkDefinitions = GetRVAList();
CreateLinks();
MatchPage();

//get cookie rva substring in cookie and removes ASP.NET session id from cookie string
function GetCookieVal(offset) 
{
	var endstr = unescape(document.cookie.indexOf (";", offset));
	if (endstr == _checker)
	{
		endstr = document.cookie.length;
	}
	return unescape(document.cookie.substring(offset, endstr));
}

//Search cookie for "rva" string to get rva cookie string
function GetCookie (cookie_name)
{
	var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );
	if (results)  
		return (unescape ( results[2] ) );
	else
		return null;
}


function GetRVAList()
{
	// Get RVA substring in cookie	
	_cookieSubstring = unescape(GetCookie("RVA"));
	
	// Split RVA substring into Link Definitions
	_rvaArray = _cookieSubstring.split("|");

	// return Links Definition Array to calling object
	return _rvaArray;
}
	
function CreateLinks()
{	
	
	document.write("<table cellpadding='0' cellspacing='0'>");
	
	// For each link definition in array, create html hyperlink	
	for(var _counter=0;_counter<_rvaLinkDefinitions.length-1;++_counter)
	{									
		var _link = _rvaLinkDefinitions[_counter].split("~");		
		
		_rvaLinks[_counter] = _link;
		
		
		document.write("<tr>");	
		document.write("<td><a href='"+_rvaLinks[_counter][1]+"'>" + _rvaLinks[_counter][0] + "</a>");				
		document.write("<tr>");
		document.write("<td height='16'><IMG height='16' alt='' src='../images/clear.gif' border='0'>");
	} 
	
	// return Links array to calling object	
	
	document.write("</table>");
	
	return _rvaLinks; 
}	

//function to match current page with match expression
function GetPageMatch()
{

	//URL to be returned for a matched page
	var replaceLink = "";	

	//Loop through each link entry of RVA Cookie array
	for(var _counter=0;_counter<_rvaLinkDefinitions.length-1;++_counter)
	{	
		//Get RVA link 								
		var _link = _rvaLinkDefinitions[_counter].split("~");
		
		//Set RVA link to array		
		_rvaLinks[_counter] = _link;
		
		//Set Current RVA Link
		var _linkDefinition = _rvaLinks[_counter][1];
		//Create URL extension variable to check if url contains file extension
		var extCheck;
		
		//get extension matching expression and set to variable
		extCheck = _matchingExpr.substring(_matchingExpr.indexOf("//")+2,_matchingExpr.length)
		extCheck = extCheck.substring(extCheck.indexOf("/")+1,extCheck.length)
		//if url contains a extension, remove the extension from the matching expression
		if(extCheck.lastIndexOf(".")!=_checker)
		{
			_matchingExpr = _matchingExpr.substring(0,_matchingExpr.lastIndexOf("."));
		}
		
		//get extension for RVA Link and set to variable
		extCheck = _linkDefinition.substring(_linkDefinition.indexOf("//")+2,_linkDefinition.length)
		extCheck = extCheck.substring(extCheck.indexOf("/")+1,extCheck.length)
		//if url contains a extension, remove the extension from the matching expression
		if(extCheck.lastIndexOf(".")!=_checker)
		{
			_linkDefinition = _linkDefinition.substring(0,_linkDefinition.lastIndexOf("."));
		}
		
		//remove HTTP protocol from URL
		_linkDefinition = _linkDefinition.replace("http://","");
		_linkDefinition = _linkDefinition.replace("https://","");

		//Remove querystring from URL		
		if(_linkDefinition.lastIndexOf("?")!=_checker)
		{
			_linkDefinition = _linkDefinition.substring(0,_linkDefinition.lastIndexOf("?"));
		}
		
		//Remove anchor from URL
		if(_linkDefinition.lastIndexOf("#")!=_checker)
		{
			_linkDefinition = _linkDefinition.substring(0,_linkDefinition.lastIndexOf("#"));
		}

		//match link from RVA list with matching expression, if match is found set this to 
		//return string to remove from RVA cookie
		if(_matchingExpr == _linkDefinition)
		{
			replaceLink =  _rvaLinks[_counter][0].toString() + "~" +  _rvaLinks[_counter][1].toString() + "|";
		}
				
	} 
	
	//return URL to remove in RVA Cookie	
	return replaceLink;

}

function MatchPage()
{

	var _cookie="";
	var _match;
	var _end = 0;
	
	_match = GetPageMatch();
	// If match is found rearrange RVA string else insert current page to list
	if(_match!="")
	{ 
		_cookie = _currentPage + "|" + _cookieSubstring.replace(_match,"");
	}
	else
	{
	
		// if number of links is not equal to five, insert current page to links
		// else remove last link and insert current page
		if(_rvaLinks.length!=_rvaMaxLength)
		{
			_cookie = _currentPage + "|" + _cookieSubstring; 
		}
		else
		{	
			//Get index of last link seperator (pipeline)
			_end = _cookieSubstring.lastIndexOf("|");
			//Remove last Pipeline from cookieSubstring variable
			_cookieSubstring = _cookieSubstring.substring(0,_end);
			//Get new last index of seperator (Pipeline)
			_end = _cookieSubstring.lastIndexOf("|");
			//Remove last link string from cookieString using seperator index
			_cookieSubstring = _cookieSubstring.substring(0,_end+1);
			
			_cookie = _currentPage + "|" + _cookieSubstring;
		}
		
	}
	
	//Update cookie w/ RVA string
	UpdateCookie(_cookie);

	//Clean Variables
	_cookie = "";
	_match=0;
	_end=0;
}
	
//Update Cookie w/ cookieString
function UpdateCookie(value)
{
	var _updatedCookie = _rvaCookieKey  + "=" + escape(value) + "; Path=/";
	document.cookie = _updatedCookie;
	
	//Clean Variables
	_updatedCookie = "";
	
}
//Clean Variables         
_rvaLinks = null;
_currentPage = "";
_rvaCookieKey="";//-->
