/**
 *  File:         cookie.js
 *
 *  This script provides the following functions and methods:
 *
 *  Usage:
 *
 *    <script type="text/javascript"
 *            src="http://intranet.bgsu.edu/scripts/cookie.js">
 *    </script>
 *
 *  Insert the <script> element into the <head> or <body> of your document.
 *
 *  Example:      cookieTest.html
 *
 *  This script provides the following functions and methods:
 *
 *  Method:       navigator.cookiesAreEnabled()
 *
 *  Arguments:    none
 *  Returns:      boolean
 *  Description:  Returns true if cookies are enabled in the browser;
 *                otherwise returns false.  (The test cookie is deleted
 *                before the function returns.)
 *  Example:      <script type="text/javascript">
 *                  if ( ! navigator.cookiesAreEnabled() ) {
 *                    window.alert( "Please enable cookies in your browser!" );
 *                  }
 *                </script>
 *
 *
 *  Method:       document.getBGAuthID()
 *
 *  Arguments:    none
 *  Returns:      string: value of cookie 'BGAuthID'
 *  Description:  If cookie 'BGAuthID' exists, return its value (a session ID); 
 *                otherwise returns null
 *  Example:      <script type="text/javascript">
 *                  if ( ! navigator.cookiesAreEnabled() ) {
 *                    document.write( "Please enable cookies in your browser!" );
 *                  } else if ( sessionID = document.getBGAuthID() ) {
 *                    document.write( "BGSU session ID = " + sessionID );
 *                  } else {
 *                    document.write( "BGSU cookie does not exist!" );
 *                  }
 *                </script>
 *
 *
 *  Function:     GetCookie( name )
 *
 *  This function returns the value of the named cookie or null if the 
 *  cookie does not exist.
 *
 *
 *  Function:     DeleteCookie( name, path, domain )
 *
 *  This function deletes the named cookie, if it exists.  The arguments
 *  'path' and 'domain' are optional.
 *
 *
 *  Function:     SetCookie( name, value, expires, path, domain, secure )
 *
 *  Creates the named cookie (only the first argument is required).
 *  Argument 'expires' is a JavaScript Date object.
 *
 *  ----------------------------------------------------------------------
 *
 *  See the article "Persistent Client State HTTP Cookies" for more
 *  information about cookies in general:
 *  http://www.netscape.com/newsref/std/cookie_spec.html
 *
 */

function _cookiesAreEnabled() {
  SetCookie( "foo", "bar" );
  if ( GetCookie( "foo" ) ) {
    DeleteCookie( "foo" );
    return true;
  } else {
    return false;
  }
}
navigator.cookiesAreEnabled = _cookiesAreEnabled;

function _getBGAuthID() {
  return GetCookie( "BGAuthID" );
}
document.getBGAuthID = _getBGAuthID;

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 DeleteCookie( name, path, domain ) {
  if ( GetCookie( name ) ) {
    document.cookie = name + "=" +
    ( ( path ) ? "; path=" + path : "" ) +
    ( ( domain ) ? "; domain=" + domain : "" ) +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

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" : "" );
}

/**
 *   Helper function for GetCookie()
 */
function getCookieVal( offset ) {
  var endstr = document.cookie.indexOf ( ";", offset );
  if ( endstr == -1 ) endstr = document.cookie.length;
  return unescape( document.cookie.substring( offset, endstr ) );
}
