/*
----------------------------------------------------------------------------
 Author:       Marc-Antoine Latour
 Copyright (c) - 2002, Strategia

 Description
 ----------------------------------------------------------------------------
   - Use to return the user to the proper page (base on cookie)
 ----------------------------------------------------------------------------
 History
 ----------------------------------------------------------------------------
  Date         User                Description
 ----------------------------------------------------------------------------
  2002/12/08   Marc-Antoine        Creation
 ----------------------------------------------------------------------------
*/ 
	function hideReturnButton(){
        if (getSubCookie("PageReferer", Ed_Navigator.Locator()) == "")
			if (document.getElementById("RETURN"))
				document.getElementById("RETURN").style.display = "none";
	}
	
	function Ed_NavigatorAPI(){
		this.onTop = "";
		this.cookieLocator = new Array()
		this.page = getCurrentPage();
        
        if (typeof(Browser) == "object")
            Browser.addListener(window, "onload", hideReturnButton)
	}
	
	Ed_NavigatorAPI.prototype.Locator = function() {
		for(i=0; i < this.cookieLocator.length; i++ ){
			if (this.cookieLocator[i][1] == this.page)
				return this.cookieLocator[i][0]
		}
		return ""
	}
	
	function getSpecialDelimiter1() { return escape('!'); }
	function getSpecialDelimiter2() { return escape('*'); }
        
        
	Ed_NavigatorAPI.prototype.goTo = function(name, blank) {
		var backUrl = document.location.pathname.toLowerCase() + document.location.search

		var Url = (this.onTop == "")? backUrl : this.onTop + escape(backUrl)
		
		setSubCookie("PageReferer",name, (blank == "")? "" : Url)
		
	}
    
    //this function is used when we have to specify special query string
    //the addresss of the current location will be stored with this query string
    //for the future goBack call
	Ed_NavigatorAPI.prototype.customGoTo = function(name, queryString) {
		var backUrl = document.location.pathname.toLowerCase() + queryString

		var Url = (this.onTop == "")? backUrl : this.onTop + escape(backUrl)
		
		setSubCookie("PageReferer",name, Url)
		
	}
    
	Ed_NavigatorAPI.prototype.customGoToPage = function(name, page, queryString) {
		var backUrl = page + queryString
		var Url = (this.onTop == "")? backUrl : this.onTop + escape(backUrl)
		setSubCookie("PageReferer",name, Url)
	}
	
	function pushArray(arr, val){ arr[arr.length] = val }
	
	Ed_NavigatorAPI.prototype.pushNew = function(name, page) {
		pushArray(this.cookieLocator, new Array(name.toLowerCase(), page.toLowerCase()))
		//this.cookieLocator.push( new Array(name.toLowerCase(), page.toLowerCase()))
	}
	
	// Roger Note: Editing go back function to be able to handle "returns" from discussion forum pages
	// If bookmark is undefined (no param is passed) goBack functions as it always has
	// If bookmark is defined (a bookmark is passed) then that will be used to find to appropriate cookie
	// This was necessary because the discussion pages referenced each other and all used "portal" as 
	// the bookmark (which resulted in unwanted behaviour)
	Ed_NavigatorAPI.prototype.goBack = function(bookmark) {
	
		var pageLocator = (typeof(bookmark) == "undefined")? this.Locator() : bookmark;
        

		var goToHref = getSubCookie("PageReferer", pageLocator)
        
		if (goToHref.toLowerCase().indexOf("elearning.asp") > 0)
			top.document.location = goToHref
		else
			document.location = goToHref
	}
	
	function getCurrentPage(){
		
		return document.location.pathname.toLowerCase()
	}
	
	function setCookie( name, value ){
		document.cookie = name + '=' + value +
						  ";path=/"
	}
	
	function getCookie( name, value ){
  
		var flag = document.cookie.indexOf(";"+name+'=' )
        if (flag == -1) {
            flag = document.cookie.indexOf(name+'=' )
        }
        
		if( flag != -1 ) {
			flag += name.length + 1
			end = document.cookie.indexOf( "; ", flag )
			if( end == -1 ) end = document.cookie.length
			return document.cookie.substring( flag, end )
		}
	}
	
	function setSubCookie( name, subKey, value ){
		
		uValue = getCookie( name )
		if( uValue ) {
			// getSpecialDelimiter2 '¶' is breaker for sub cookie each
			
			uCookies = uValue.split( getSpecialDelimiter2() )
			var found = false // whether sub-cookie for the name is present
			for( c = 0; c < uCookies.length; c++ ){
				//  getSpecialDelimiter1 '§' is identifier between name and value for sub cookie.
				t = uCookies[c].split( getSpecialDelimiter1() )
				tName = t[0]
				tValue = t[1]
				if( tName == subKey ) {
					found = true
					t[1] = value
					uCookies[c] = t.join( getSpecialDelimiter1() )
					break ;
				}
			}
			if(!found) 
			{
				uCookies[uCookies.length] = subKey + getSpecialDelimiter1() + value
			}
			uValue = uCookies.join( getSpecialDelimiter2() )
		} else {
			uValue = subKey + getSpecialDelimiter1() + value
		}
		// update real cookie
		setCookie( name, uValue )
	}
	
	function getSubCookie( name, subKey ){
		var uValue = getCookie( name )
		if( !uValue ) { return ""}
		var uValues = uValue.split(getSpecialDelimiter2())
        for (i = 0; i < uValues.length; i++)
        {
            var keyVal = uValues[i].split(getSpecialDelimiter1())
            if(keyVal[0] == subKey)
                return keyVal[1]
        }
		return ""
		
	}

    function Nav(sUrl, Locator) {
        Ed_Navigator.goTo(Locator);
        document.location = sUrl;
    }
	
// *************************************************************************************
// ************** INITIALIZATION           *********************************************
// *** Must be edited to add new navigating page	Ed_Navigator.goTo("resourcemodify")
	var Ed_Navigator = new Ed_NavigatorAPI()
    
	Ed_Navigator.pushNew("portal",			"/ed/portal/default.asp")
	Ed_Navigator.pushNew("manifestdescription",	"/ed/formation/description.asp")
    Ed_Navigator.pushNew("activity_portal","/ed/formation/Activity.asp")
	Ed_Navigator.pushNew("plug_in", 		"/ed/formation/lcms/plug-in.asp")
	Ed_Navigator.pushNew("resourcemodify", 		"/ed/formation/lcms/m_ressources-n.asp")
	Ed_Navigator.pushNew("itemmodify", 		"/ed/formation/lcms/item-n.asp")
	Ed_Navigator.pushNew("glossarymodify", 		"/ed/formation/lcms/glossary-n.asp")
    Ed_Navigator.pushNew("bibliomodify", 		"/ed/documentation/biblio-n.asp")
	Ed_Navigator.pushNew("faqmodify", 		"/ed/documentation/faq-n.asp")
	Ed_Navigator.pushNew("webmodify", 		"/ed/documentation/web-n.asp")
	Ed_Navigator.pushNew("usergroupmodify",		"/ed/admin/group-users.asp")
	Ed_Navigator.pushNew("thatmodify",		"/ed/admin/group-roles.asp")
	Ed_Navigator.pushNew("thatlist",		"/ed/admin/user-droit.asp")
	Ed_Navigator.pushNew("usergrouplist",		"/ed/admin/user-droit.asp")
	Ed_Navigator.pushNew("assessment",		"/ed/manager/manifest/manifest-assessmentquest.asp")
	Ed_Navigator.pushNew("edit_addforum",				"/ed/support/forum-new.asp")
	Ed_Navigator.pushNew("edit_addforum_portal",		"/ed/portal/default.asp")
	Ed_Navigator.pushNew("viewallthreads",				"/ed/support/forum-view.asp")
	Ed_Navigator.pushNew("viewallthreads_portal",		"/ed/portal/default.asp")
	Ed_Navigator.pushNew("viewsinglethread",			"/ed/support/forum-msg.asp")
	Ed_Navigator.pushNew("viewsinglethread_portal",		"/ed/portal/default.asp")
	Ed_Navigator.pushNew("edit_addthread",			"/ed/support/forum-new.asp")
	Ed_Navigator.pushNew("edit_addthread_portal",	"/ed/portal/default.asp")
	Ed_Navigator.pushNew("threadreply",				"/ed/support/forum-new.asp")
	Ed_Navigator.pushNew("threadreply_portal",		"/ed/portal/default.asp")
	Ed_Navigator.pushNew("associateforum",      	"/ed/support/forum-view.asp")
	Ed_Navigator.pushNew("viewfromcourse",		"/ed/support/forum-view.asp")
	Ed_Navigator.pushNew("template_assessment",	"/ed/manager/manifest/manifesttemplate-quest.asp")
	Ed_Navigator.pushNew("course_tracking",		"/ed/formation/TrackingInfo.asp")
	Ed_Navigator.pushNew("programview",		"/ed/formation/programview.asp")
	Ed_Navigator.pushNew("mydevplanhistory",	"/ed/portal/default.asp")
	Ed_Navigator.pushNew("employeeevaluation",	"/ed/portal/default.asp")
	Ed_Navigator.pushNew("effectiverights",		"/ed/admin/user-role-list.asp")
	Ed_Navigator.pushNew("portalbiblioview",		"/ed/portal/default.asp")
	Ed_Navigator.pushNew("portalfaqview",			"/ed/portal/default.asp")
	Ed_Navigator.pushNew("portalwebview",			"/ed/portal/default.asp")
	Ed_Navigator.pushNew("portalglossaryview",		"/ed/portal/default.asp")
	Ed_Navigator.pushNew("classroom_detail",		"/ed/Formation/LMS/ListEquipments.asp")
	Ed_Navigator.pushNew("notifmodify",				"/ed/manager/notification/notificationlist-n.asp")
	Ed_Navigator.pushNew("notifmsgview",			"/ed/manager/notification/mailviewmsg.asp")
    Ed_Navigator.pushNew("editinstructor",			"/ed/formation/lms/instructeur.asp")
	Ed_Navigator.pushNew("editprovider",			"/ed/formation/lcms/provider-n.asp")
    Ed_Navigator.pushNew("treewrapper", "/ed/manager/tree/treewrapper.asp")
	Ed_Navigator.pushNew("myactivitiesprint", "/ed/portal/default.asp")
	Ed_Navigator.pushNew("portalinstructor", "/ed/portal/default.asp")
	Ed_Navigator.pushNew("available_activities", 			"/ed/formation/abstractactivities.asp")
	Ed_Navigator.pushNew("available_activities_portal", 	"/ed/formation/abstractactivities.asp")
	Ed_Navigator.pushNew("classroom",		"/ed/formation/lms/manifestclass.asp")
    Ed_Navigator.pushNew("uservisibility", "/ed/Admin/UserVisibility.asp")
    Ed_Navigator.pushNew("objectaccess", "/ed/Admin/ObjectAccess.asp")

