
/* Modified the SetCookie and DeleteCookie routines so that OFFLINE:
   1. They don't cause an error condition.
   2. They write to document.cookie so that one can "Get" them in the
      same document object.  (This works in the TOC even after closing
      and re-opening the course window -- not sure how.)
   3. Note that these offline psuedo cookies never appear on the hard drive.
   kdg, 5/13/01.
*/
if (document.URL.substring(0, 4)=="file") mode = "offline";
else mode = "online";

//################################################################################
//##	Function QueryString:
//##		Retrieves specific values from the querystring of the current page
//##     If second arg is 'parent' retrieves values from parent (kdg, 4/11/01)
//##
//##	Parameters:
//##		param	- the key name for a key/value pair in the querystring
//##
//##	Returns:
//##		- the value associated with the given key (param)
//##		- ex: www.myurl.com?userid=12&name=john
//##			- getFromQueryString('userid') returns '12'
//##			- getFromQueryString('name') returns 'john'
//################################################################################
function QueryString(param)
{
   var sQuery = new String();
   var argv = QueryString.arguments;  
   var argc = QueryString.arguments.length;

   if ((argc > 1) && argv[1] == 'parent')
      sQuery = parent.location.search.substring(1);
   else
      sQuery = self.location.search.substring(1);

	var aQuery = sQuery.split("&");

	for ( var ii=0; ii < aQuery.length; ii++ )
	{
		pos = aQuery[ii].indexOf("=");

		if ( pos != -1 )
		{
			key = aQuery[ii].substring(0,pos);

			if ( key.toUpperCase() == param.toUpperCase() )
			{
				return(aQuery[ii].substring(pos + 1));
			}
		}
	}

	return('');
}

//################################################################################
//##	Function getBasePath:
//##		Returns full url path to the root of a page
//##
//##	Parameters:
//##		where	- the page object
//##
//##	Returns:
//##		- the url to the directory/folder containing the page object
//################################################################################
function getBasePath(where)
{
	var basepath="";

	var index1=where.location.href.indexOf("?");
	if(index1<1) index1=where.location.href.indexOf("#");
	if(index1<1) index1=where.location.href.length;

	var index=where.location.href.lastIndexOf("/",index1)+1;
	if(index<1) index=where.location.href.lastIndexOf("\\",index1)+1;
	if(index<1) index=where.location.href.length;

	basepath=where.location.href.substring(0,index);

	return(basepath);
}

//################################################################################
//##	Function preloadImage:
//##		Loads an on and off state of an image for mouse overs
//##
//##	Parameters:
//##		index	- the array index of the image array
//##		normal	- the url and filename for the image normal state
//##		over	- the url and filename for the image over state
//##
//##	Returns:
//##		- nothing
//################################################################################
function preloadImage(index, normal, over)
{
	if ( document.images )
	{
		aImages[index] = new Array();

		aImages[index][0] = new Image();
		aImages[index][0].src = normal;

		aImages[index][1] = new Image();
		aImages[index][1].src = over;
	}
}

//################################################################################
//##	Function on:
//##		Handles the image over state for mouse roll-overs
//##
//##	Parameters:
//##		d		- the document object of the calling page
//##		index	- the array index for the image array
//##		name	- the name of the image object
//##
//##	Returns:
//##		- nothing
//################################################################################
function on(d, name, index)
{
	if ( d.images )
		d.images[name].src = top.aImages[index][1].src;
}

//################################################################################
//##	Function off:
//##		Handles the return to normal image state for mouse roll-overs
//##
//##	Parameters:
//##		d		- the document object of the calling page
//##		index	- the array index for the image array
//##		name	- the name of the image object
//##
//##	Returns:
//##		- nothing
//################################################################################
function off(d, name, index)
{
	if ( d.images )
		d.images[name].src = top.aImages[index][0].src;
}

//################################################################################
//##	Function initialCaps:
//##		Given string in lower case, Initializes each word
//##     Furthermore, if there is an initial number, it creates a space between the
//##           number and the first word
//##	Parameters:
//##		inString, the lower-case input string
//##
//##	Returns:
//##		Initial capitalized string
//################################################################################
function initialCaps (inString) {
   outString = inString;
   var len = inString.length;
   if (len < 1) return "";

   // look for initial number in string, if any, and insert space
   var i = 0;
   while (i < len && inString.charCodeAt(i)>47 && inString.charCodeAt(i)<58 ) i++;

   if (i > 0 && inString.substring(i,i+1)!=" ") 
      outString = inString.substring(0,i) + " "+inString.substring(i,len);
 
   // Apply initial caps
   var splitOut = outString.split(" ");
   outString = "";
   var temp = new String;
   var charCode;

   for (i=0; i < splitOut.length; i++ ) {
      temp = splitOut[i];
      charCode = temp.charCodeAt(0);
      if (charCode > 96 && charCode < 123) {
         len = temp.length;
         temp = String.fromCharCode(charCode-32) + temp.substring(1,len);
      }
      if (i==0) outString = temp;
      else
         outString = outString + " " + temp;
   }
   return outString;
}


//Define various times to use with SetCookie
var expDays = 30;
var exp = new Date();
var expOneDay = new Date();
var expTenDays = new Date();

expOneDay.setTime(expOneDay.getTime() + (24*60*60*1000));
expTenDays.setTime(expTenDays.getTime() + (10 * 24*60*60*1000));
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
// SET COOKIE
function SetCookie (name, value) {  
   if (mode=="offline") {
      document.cookie = name+"="+escape (value);
   }
   else {  
      var argv = SetCookie.arguments;  
      var argc = SetCookie.arguments.length;  
      var expires = (argc > 2) ? argv[2] : null;
      var path = (argc > 3) ? argv[3] : "/";  
      var domain = (argc > 4) ? argv[4] : document.domain;  
      var secure = (argc > 5) ? argv[5] : false;  
      document.cookie = name + "=" + escape (value) + 
      ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
      ((path == null) ? "" : ("; path=" + path)) +  
      ((domain == null) ? "" : ("; domain=" + domain)) +    
      ((secure == true) ? "; secure" : "");
   }
}

function FixCookieDate (date) {  var base = new Date(0);
  var skew = base.getTime(); // dawn of (Unix) time - should be 0
  if (skew > 0)  // Except on the Mac - ahead of its time
    date.setTime (date.getTime() - skew);
}

// GET COOKIE VALUE
function getCookieVal (offset) {  
   var endstr = document.cookie.indexOf (";", offset);  
   if (endstr == -1)    
   endstr = document.cookie.length;  
   return unescape(document.cookie.substring(offset, endstr));
}

// GET COOKIE
function GetCookie (name) {  
   var arg = name + "=";  
   var alen = arg.length;  
   var clen = document.cookie.length;  
   var i = 0;  
   while (i < clen) {    
      var j = i + alen;    
      if (document.cookie.substring(i, j) == arg)      
      return getCookieVal (j);    
      i = document.cookie.indexOf(" ", i) + 1;    
      if (i == 0) break;   
   }  
   return null;
}

// DELETE COOKIE
function DeleteCookie (name) {
   var exp = new Date();  
   exp.setTime (exp.getTime() - 1);
   var cval = GetCookie (name);
   if (GetCookie (name)) {
      if (mode=="offline") {
         document.cookie = name+"=; expires=1";
      }
      else {
         document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString() + "; domain=" + document.domain + "; path=" + "/";
      }
      return name;
   }
   else {
      return null;
   }
}

var expdate = new Date ();
FixCookieDate (expdate); // Correct for Mac date bug - call only once for given Date object!
expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000)); // 24 hrs from now 

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

// Sets a cookie: name=lastname+courseID, value=URL to the page
// For starting at place where the user last read
function markPlace (courseID) {
   var lastName = GetCookie ('LASTNAME');
   var path = document.URL;

   if (courseID && lastName && path) {
      SetCookie(lastName+courseID, path, exp);
   }
}

// Gets the URL to a marked place
function getPlace (courseID) {
   var lastName = GetCookie ('LASTNAME');
   return GetCookie (lastName+courseID);
}

// deletes the cookie with name=lastname+courseID
// after querying the student
function deletePlace (courseID) {
   var lastName = GetCookie ('LASTNAME');
   var isCookie = GetCookie (lastName+courseID);
   if (isCookie && !confirm("Return to this page the next time\nyou open this course?\n\nSelect 'OK' to mark this page,\n'cancel' to continue and exit IBS.")) {
      DeleteCookie (lastName+courseID);
   }
}
