function writeCookie(cookiename, cookieval, cookieexpires) {
  if (cookieexpires == "") {
    expires = new Date();
    expires.setTime(expires.getTime() + 3652 * 24 * 60 * 60 * 1000);
  }
  else {
    expires = new Date(cookieexpires);
  }

  var cookie = cookiename + "=" + escape(cookieval) + 
		   ((expires) ? "; expires=" + expires.toGMTString() : "");
  document.cookie = cookie;
}

function getCookieVal(cookiename, cookiestring) {
   var startLoc = cookiestring.indexOf(cookiename);
   if (startLoc == -1)
      return("");     // cookie not found

   var sepLoc = cookiestring.indexOf("=",startLoc);
   var endLoc = cookiestring.indexOf(";",startLoc);

   if (endLoc == -1) // last one has no ;
      endLoc = cookiestring.length;
   return(cookiestring.substring(sepLoc+1,endLoc));
}
