// Cookie routines.
function WM_readCookie(name, NS_outputElement, IE_outputElement) {

  // if there's no cookie, don't do anything
  if(document.cookie != '') {
    var actualValue, outputElement, firstChar, lastChar, tname;

    // Make sure we find the exact name.
	tname = name + "=";

    // get the entire cookie string (this may have other name=value pairs in it
    var theBigCookie = document.cookie;

    // grab just this cookie from theBigCookie string
    // find the start of 'name'
    firstChar = theBigCookie.indexOf(tname);

    // if you found it
    if(firstChar != -1) {

      // skip 'name' and '='
      firstChar += tname.length;

      // find the end of the value string (i.e. the next ';')
      lastChar = theBigCookie.indexOf(';', firstChar);
      if(lastChar == -1) lastChar = theBigCookie.length;

      // return the value
      actualValue = unescape(theBigCookie.substring(firstChar, lastChar));

    } else {

      // if there was no cookie, return false
      actualValue = false;

    }
    outputElement = (navigator.appName == 'Netscape')?NS_outputElement:IE_outputElement;

    //fix layer ref if not supported (thanks, Ken Sundermeyer!)
    if ((outputElement.indexOf('document.layers[')==0 && document.layers==null)|| (outputElement.indexOf('document.all[')   ==0 && document.all   ==null)) {
      outputElement = 'document'+outputElement.substring(outputElement.substring(0,outputElement.lastIndexOf('.')).lastIndexOf('.'),outputElement.length);
    }


    if(outputElement && actualValue) {
// thanks to Matthew Toledo for pointing out IE5 not handling some evals..
fehIE5=eval(outputElement);
fehIE5.value = actualValue;
    }

  }

}

function WM_setCookie (name, NS_value, IE_value, hours, path, domain, evalValue) {

    daValue = (navigator.appName == 'Netscape')?NS_value:IE_value;

    //fix layer ref if not supported (thanks, Ken Sundermeyer!)
    if ((daValue.indexOf('document.layers[')==0 && document.layers==null)|| (daValue.indexOf('document.all[')==0 && document.all==null)) {
      daValue = 'document'+daValue.substring(daValue.substring(0,daValue.lastIndexOf('.')).lastIndexOf('.'),daValue.length);
    }
    if(evalValue == 1) daValue = eval(daValue + '.value');

  // set the cookie, adding any parameters that were specified
  // (convert hours to milliseconds (*3600000) and then to a GMTString)
    document.cookie = name + '=' + escape(daValue) + ((hours)?(';expires=' + ((new Date((new Date()).getTime() + parseInt(hours)*3600000)).toGMTString())):'') + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:'');
}

function WM_killCookie(name, path, domain) {

  var firstChar, lastChar, theValue, tname;

  // get the entire cookie string (this may have other name=value pairs in it
  var theBigCookie = document.cookie;

  // grab just this cookie from theBigCookie string
  // find the start of 'name'

  tname = name + "=";
  firstChar = theBigCookie.indexOf(tname);

  // if you found it
  if(firstChar != -1) {

    // skip 'name' and '='
    firstChar += tname.length;

    // find the end of the value string (i.e. the next ';')
    lastChar = theBigCookie.indexOf(';', firstChar);
    if(lastChar == -1) lastChar = theBigCookie.length;

    // set theValue
    theValue = theBigCookie.substring(firstChar, lastChar);

  } else {

    // there is no such cookie
    theValue = false;

  }

  // assuming there actually is such a cookie
  if(theValue) {

    // set an expired cookie, adding 'path' and 'domain' if they were given
    document.cookie = tname  + theValue + '; expires=Fri, 13-Apr-1970 00:00:00 GMT' + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:'');

  }
}
