// Returns a string with all the cookie information in it
function GetCookieString( oCookie, name )
    {
    var aCookieList = oCookie.split( ";" )
    for (var i = 0; i < aCookieList.length; i++)
        {
        // See if we have the correct cookie
        var aCookieIndex = aCookieList[i].split("=");

        // Use regular expression, because sometime the 
        // cookie has spaces between each cookie
        re = new RegExp(name, "i");  
        if ( aCookieIndex[0].search(re) >= 0 )
            {
				if ( aCookieIndex[1] == null )
					return null;

				if ( aCookieIndex[1].length == 0 )
					return null;

            return unescape(aCookieIndex[1] );
            }
        }
        return null;
    }  

function GetCookies( oCookie, name )
    {
	if ( !document.layers ) // Just skip this check for NN4.
		{
		if (!navigator.cookieEnabled)
			{
			top.location.href = "noCookie.htm";
			}
		}

    var sCookieString = GetCookieString( oCookie, name );
    if (sCookieString == null)
        return new Array(); // Return an Empty array

    return  sCookieString.split(",");
    }

function ReorderCookie( oDocumentOb, name, Index )
    {
    var aAllCookies = GetCookies( oDocumentOb.cookie, name );

    if ( aAllCookies.length < Index - 1 )
        {
        return;
        }

    // Reorder the Items
    var tmpItem = aAllCookies[Index];
    for (h = Index; h < aAllCookies.length - 1; h++)
        {
        aAllCookies[h] = aAllCookies[h + 1];
        }
    aAllCookies[aAllCookies.length - 1] = tmpItem;

    var sNewCookie = new String();
    for (i = 0; i < aAllCookies.length; i++)
        {
        if (i == aAllCookies.length - 1)
            {
            sNewCookie += aAllCookies[i];   // The last one doesn't get a comma
            }
        else
            {
            sNewCookie += aAllCookies[i] + ",";
            }
        }

    oDocumentOb.cookie = name + "=" + escape(sNewCookie) + ";  expires=" + GetExpireDate();
    }

function SetCookies( oDocumentOb, name, sValue, maxCookieString )
    {
	if ( !document.layers ) // Just skip this check for NN4.
		{
		if (!navigator.cookieEnabled)
			{
			top.location.href = "noCookie.htm";
			}
		}

    var aAllCookies = GetCookies( oDocumentOb.cookie, name );

    if (aAllCookies.length >= maxCookieString)
        {
        // Remove the first element
        aAllCookies = aAllCookies.slice(1);
        }
    aAllCookies[aAllCookies.length] = sValue;

    var sNewCookie = new String();
    for (i = 0; i < aAllCookies.length; i++)
        {
        if (i == aAllCookies.length - 1)
            {
            sNewCookie += aAllCookies[i];   // The last one doesn't get a comma
            }
        else
            {
            sNewCookie += aAllCookies[i] + ",";
            }
        }

    oDocumentOb.cookie = name + "=" + escape(sNewCookie) + "; expires=" + GetExpireDate();
    }

function RemoveCookieItem( oDocumentOb, name, sValue )
    {
    var aAllCookies = GetCookies( oDocumentOb.cookie, name );

    var sNewCookie = new String();
    for ( var i = 0; i < aAllCookies.length; i++)
        {
		  if ( sValue != null )
				{
				if ( aAllCookies[i] != sValue )
					{
					if (i == 0)
						{
						sNewCookie += aAllCookies[i];
						}
					else
						{
						sNewCookie += "," + aAllCookies[i];
						}
					}
				}
        }

    if (sNewCookie.length < 1)
        {
		  // Clear the values for the cookie name
        oDocumentOb.cookie = name + "=; expires=" + GetExpireDate();
        }
    else
        {
        oDocumentOb.cookie = name + "=" + escape(sNewCookie) + "; expires=" + GetExpireDate();
        }
    }

function FixCookieDate (date)
    {
    var base = new Date(0);
    var skew = base.getTime(); 
    if (skew > 0)    // Except on the Mac - ahead of its time
        date.setTime (date.getTime() - skew);
    }

function GetExpireDate()
    {
    expDate = new Date();
    FixCookieDate (expDate); // Correct for Mac date bug - call only once for given Date object!
    expDate.setTime(expDate.getTime() + 31536000000);

    return expDate.toGMTString();
    }

function GetCookieItemIndex( oDocumentOb, sName, sValue )
	{
	var arCookies = GetCookies( oDocumentOb.cookie, sName );
	for ( var i = 0; i < arCookies.length; i++ )
		{
		if ( arCookies[i].toUpperCase() == sValue.toUpperCase() )
			return i;
		}
	return -1;
	}

function GetCookieItemCount( oDocumentOb, sName )
	{
	var arCookies = GetCookies( oDocumentOb.cookie, sName );
	return arCookies.length;
	}


/////////////////////////////////////////////////////////////////////////////////////////
//////////  These are WSS functions merged from StudentCtr/js/generic/cookies.js  ///////
/////////////////////////////////////////////////////////////////////////////////////////

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;
}

function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

function SetCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}
