// DirectURLs.js
//
// The functions in this file are to be used in lieu of WSS 4 Direct URLs.
// At there core, these functions build the WSS 4 URLs, but they also provide
// additional functionality that one cannot get with a simple "http URL".
//
// The key added functionality with the DirectURL JavaScript functions
// is the addition of a uniquely generated parameter for each URL.  By generating
// a unique parameter for the URL, each URL becomes unique, and hence is never
// pulled from a web browser cache or a proxy server cache.
//
// There are two distinct cases in which these DirectURLs will be used:
//
// 1 - From an external "launching" page which takes a user into Learning Center.
// 2 - From a frame (configured via the Layout Process) which contains a series
//     of links into different portions of Learning Center.  This frame always
//     remains visible while the user is in Learning Center.  It is an incorporated
//     part of the Learning Center frameset.
//
// For all examples below, we assume a Learning Center named "learning".
//
// Case 1:
// The first case is the simpler case.  In this case, you simply want to provide
// a way for a user to get to Learning Center.  Suppose in this case, you wanted
// the user to go into Learning Center and go straight to the "Course Search" page.
// 
// A proper URL for this would be:
//
//   <a href="javascript:DirectURL_CourseMenu(top,'learning',true)">Course Search</a>
//
// When clicked, this URL would target the "top" frame of the current browser window
// and build the URL:
//
//   http://<yourhost>/stc/learning/psciis.dll?coursemenu=learning&top_frame=1&linkid=<somevalue>
//
// This is functionally the same as the familiar:
//
//   http://<yourhost>/stc/learning/psciis.dll?coursemenu=learning
//
// with the addition of the unique "linkid" which avoids browser caching.
// The "top_frame" parameter will be explained further in Case 2.
//
// Case 2:
// A frame that is incorporated into the Learning Center frameset via the Layout Process
// should not target the "top" with its Direct URL calls.  Doing so would cause the
// Learning Center frameset to be unloaded and reloaded each time one of the Direct URLs
// is called.  Hence, such Direct URLs should target the Learning Center main viewable frame.
// This frame is named "stcMain".  In addition, the URL passed should indicate that the
// "top_frame" is already loaded and need not be loaded again.
//
// In a "left-side navigation frame", for example, one would rewrite the URL
// from Case 1 to be:
//
//   <a href="javascript:DirectURL_CourseMenu(parent.stcMain,'learning',false)">Course Search</a>
//
// When clicked, this URL would target the "stcMain" frame of the Learning Center frameset
// and build the URL:
//
//   http://<yourhost>/stc/learning/psciis.dll?coursemenu=learning&top_frame=0&linkid=<somevalue>
//

// The first parameter to each DirectURL function call is the "target frame".
// In Case 1, the target frame is the builtin "top" frame.
// In Case 2, the target frame is "stcMain" frame of the parent frameset.
//            (The "parent" frameset for this left-side navigation frame is the
//            Learning Center frameset.)
//
// The second parameter to each DirectURL function call is the name of the Learning Center.
// This is the same in both Case 1 and Case 2.
//
// The third parameter to each DirectURL function call is a flag which indicates
// whether the DirectURL function call should force the loading of the Learning Center
// frameset.
// In Case 1, the URL is in a page completely external to Learning Center, so "true"
//            should be passed (yes, load the top frameset).
// In Case 2, the URL is in a page that is incorporated into the Learning Center frameset
//            and is only going to be displayed if Learning Center is already loaded
//            into the browser window, so "false" should be passed (no, do not re-load
//            the top frameset).


// "Public" functions

function DirectURL_APPLink( w, sLearningCenter, fTopFrame, sTheURL )
	{
	return DirectURL(w, "applink", sLearningCenter, fTopFrame, 'url=' + escape(sTheURL))
	}

function DirectURL_Bulletins( w, sLearningCenter, fTopFrame )
	{
	return DirectURL(w, "bulletins", sLearningCenter, fTopFrame)
	}

function DirectURL_ClassMenu( w, sLearningCenter, fTopFrame, sOption, sText, saFields, saValues )
	{
	// Build the parameter string if doing a class search
	var sParams = "";
	if ( sOption != null )
		{
		sParams += "searchoption=" + escape(sOption);
		// If doing a standard search, add the text to search on
		if ( !isEmptyString( sText ) )
			{
			sParams += "&searchtext=" + escape(sText);
			}
		else
			{
			// If searching for field/value pairs, add them
			for ( var i = 0; i < saFields.length; i++ )
				{
				sParams += "&field" + i + "=" + escape(saFields[i]);
				sParams += "&value" + i + "=" + escape(saValues[i]);
				}
			}
		sParams += "&fIsDirectList=1"
		}
	
	return DirectURL(w, "classmenu", sLearningCenter, fTopFrame, sParams)
	}

function DirectURL_CourseMenu( w, sLearningCenter, fTopFrame, sOption, sText, saFields, saValues )
	{
	// Build the parameter string if doing a course search
	var sParams = "";
	if ( sOption != null )
		{
		sParams += "searchoption=" + escape(sOption);
		// If doing a standard search, add the text to search on
		if ( !isEmptyString( sText ) )
			{
			sParams += "&searchtext=" + escape(sText);
			}
		else
			{
			// If searching for field/value pairs, add them
			for ( var i = 0; i < saFields.length; i++ )
				{
				sParams += "&field" + i + "=" + escape(saFields[i]);
				sParams += "&value" + i + "=" + escape(saValues[i]);
				}
			}
		sParams += "&fIsDirectList=1"
		}

	return DirectURL(w, "coursemenu", sLearningCenter, fTopFrame, sParams)
	}

function DirectURL_Evaluate( w, sLearningCenter, fTopFrame )
	{
	return DirectURL(w, "evaluate", sLearningCenter, fTopFrame)
	}

function DirectURL_Finalize( w, sLearningCenter, fTopFrame )
	{
	return DirectURL(w, "finalize", sLearningCenter, fTopFrame)
	}

function DirectURL_Help( w, sLearningCenter, fTopFrame )
	{
	return DirectURL(w, "help", sLearningCenter, fTopFrame)
	}

function DirectURL_MainMenu( w, sLearningCenter, fTopFrame )
	{
	return DirectURL(w, "mainmenu", sLearningCenter, fTopFrame)
	}

function DirectURL_Message( w, sLearningCenter, fTopFrame )
	{
	return DirectURL(w, "message", sLearningCenter, fTopFrame)
	}

function DirectURL_MyCal( w, sLearningCenter, fTopFrame )
	{
	return DirectURL(w, "mycal", sLearningCenter, fTopFrame)
	}

function DirectURL_News( w, sLearningCenter, fTopFrame )
	{
	return DirectURL(w, "news", sLearningCenter, fTopFrame)
	}

function DirectURL_PersonData( w, sLearningCenter, fTopFrame )
	{
	return DirectURL(w, "persondata", sLearningCenter, fTopFrame)
	}

function DirectURL_DevelopmentPlan( w, sLearningCenter, fTopFrame )
	{
	return DirectURL(w, "plan", sLearningCenter, fTopFrame)
	}

function DirectURL_RecordCompletedTraining( w, sLearningCenter, fTopFrame )
	{
	return DirectURL(w, "reccomp", sLearningCenter, fTopFrame)
	}

function DirectURL_Requests( w, sLearningCenter, fTopFrame )
	{
	return DirectURL(w, "requests", sLearningCenter, fTopFrame)
	}

function DirectURL_Roster( w, sLearningCenter, fTopFrame )
	{
	return DirectURL(w, "roster", sLearningCenter, fTopFrame)
	}

function DirectURL_SignOn( w, sLearningCenter, fTopFrame )
	{
	return DirectURL(w, "signon", sLearningCenter, fTopFrame)
	}

function DirectURL_SignOff( w, sLearningCenter, fTopFrame )
	{
	return DirectURL(w, "signoff", sLearningCenter, fTopFrame)
	}

function DirectURL_Transcript( w, sLearningCenter, fTopFrame )
	{
	return DirectURL(w, "transcript", sLearningCenter, fTopFrame)
	}

function DirectURL_Virtual( w, sLearningCenter, fTopFrame )
	{
	return DirectURL(w, "virtual", sLearningCenter, fTopFrame)
	}

function DirectURL_Curriculums( w, sLearningCenter, fTopFrame )
	{
	return DirectURL(w, "curriculums", sLearningCenter, fTopFrame)
	}

function DirectURL_Catalog( w, sLearningCenter, fTopFrame )
	{
	return DirectURL(w, "catalog", sLearningCenter, fTopFrame)
	}

function DirectURL_Chatrooms( w, sLearningCenter, fTopFrame )
	{
	return DirectURL(w, "chatrooms", sLearningCenter, fTopFrame)
	}

function DirectURL_ChangePassword( w, sLearningCenter, fTopFrame )
	{
	return DirectURL(w, "changepwd", sLearningCenter, fTopFrame)
	}

function DirectURL_Class( w, sLearningCenter, fTopFrame, sLocator )
	{
	return DirectURL(w, "class", sLearningCenter, fTopFrame, 'loc=' + escape(sLocator))
	}

function DirectURL_ClassSchedule( w, sLearningCenter, fTopFrame, sCode )
	{
	return DirectURL(w, "class", sLearningCenter, fTopFrame, 'code=' + escape(sCode))
	}

function DirectURL_Course( w, sLearningCenter, fTopFrame, sCode )
	{
	return DirectURL(w, "course", sLearningCenter, fTopFrame, 'code=' + escape(sCode))
	}

function DirectURL_SkillTranscript( w, sLearningCenter, fTopFrame )
	{
	return DirectURL(w, "skilltranscript", sLearningCenter, fTopFrame)
	}

function DirectURL_SkillPlan( w, sLearningCenter, fTopFrame )
	{
	return DirectURL(w, "skillplan", sLearningCenter, fTopFrame)
	}

function DirectURL_ProfileCompare( w, sLearningCenter, fTopFrame )
	{
	return DirectURL(w, "profilecompare", sLearningCenter, fTopFrame)
	}

function DirectURL_360Assessments( w, sLearningCenter, fTopFrame )
	{
	return DirectURL(w, "360", sLearningCenter, fTopFrame)
	}

function DirectURL_RegApprovals( w, sLearningCenter, fTopFrame )
	{
	return DirectURL(w, "regapprovals", sLearningCenter, fTopFrame)
	}

function DirectURL_PersonAdd( w, sLearningCenter, fTopFrame )
	{
	return DirectURL(w, "personadd", sLearningCenter, fTopFrame)
	}

function DirectURL_Manager( w, sLearningCenter, fTopFrame )
	{
	return DirectURL(w, "manager", sLearningCenter, fTopFrame)
	}

// "Private" functions

function BuildDirectURL( sPage, sLearningCenter, fTopFrame, sParams )
{
	if ( sParams == null )
		sParams = '';
	else
		sParams = '&' + sParams;

	var app = "psciis.dll?"
	if ( self.app != null )
		app = self.app

	return app
	     + "linkid=" + (new Date().getTime() % 1000000)
		 + "&" + sPage + "=" + sLearningCenter
		 + "&top_frame=" + (fTopFrame ? "1" : "0")
		 + sParams;
}

function DirectURL( w, sPage, sLearningCenter, fTopFrame, sParams )
{
	var sURL = BuildDirectURL(sPage, sLearningCenter, fTopFrame, sParams)

	if ( w == null )
		return sURL

	w.location.href=sURL
}

// Added isEmptyString() because it is now used by the DirectURL_ClassMenu and CourseMenu
// direct URLs.  I added it here to prevent existing users from having to include External.js
// in their existing custom pages that use Direct URLs.
// Function isEmptyString()
// Returns true if the specified string is:
//    * null
//    * zero length
//    * comprised only of space, tab, and newline characters
function isEmptyString( sString )
{
	return ( ( sString == null ) ||
	         ( sString == '' ) ||
	         ( sString.search(/[^ \t\n]/) < 0 )
	       );
}
