// Note keys must include the "=" as in "key="

function getCookie( szKey )
{
  // alert("Looking for: " + szKey + " in " + document.cookie );
  var szAllCooks = document.cookie;
  // alert( "All cookies are: " + szAllCooks );
  var iPos = szAllCooks.indexOf( szKey );
  if (iPos == -1) return null;
  iPos = iPos + szKey.length;
  var iEnd = szAllCooks.indexOf( ";", iPos )
  if (iEnd == -1) iEnd = szAllCooks.length;
  return szAllCooks.substring(iPos, iEnd);
}

function clearCookie( szKey )
{
  var dtExpired = new Date();
  dtExpired.setFullYear(dtExpired.getFullYear()-1);
  szCook = szKey + "null; expires=" + dtExpired.toGMTString() + "; path=/";
  document.cookie = szCook;
}

function setCookie( szKey, szValue, dtExpires )
{
  szCurrent = document.cookie;
  if (szCurrent.indexOf( szKey ) != szCurrent.lastIndexOf(szKey)) {
    // Try to delete duplicate cookies
    sz = document.location.pathname;
    while (sz != null) {
      document.cookie = szKey + "; path=" + sz + "; expires Thu, 01-Jan-1970 00:00:01 GMT"
      document.cookie = szKey + "; path=" + sz + "/; expires Thu, 01-Jan-1970 00:00:01 GMT"
      szNoSlash = sz.substring(1,sz.length);
      document.cookie = szKey + "; path=" + szNoSlash + "; expires Thu, 01-Jan-1970 00:00:01 GMT"
      document.cookie = szKey + "; path=" + szNoSlash + "/; expires Thu, 01-Jan-1970 00:00:01 GMT"
      iPos = sz.lastIndexOf( "/" );
      if (iPos < 0) {
        sz = null;
      } else {
        sz = sz.substring(0, iPos);
      }
    }
    document.cookie = szKey + "; path=/; expires Thu, 01-Jan-1970 00:00:01 GMT"
    document.cookie = szKey + "; path=; expires Thu, 01-Jan-1970 00:00:01 GMT"
    document.cookie = szKey + "; expires Thu, 01-Jan-1970 00:00:01 GMT"
  }
  if (dtExpires == null) {
    var dtExpires = new Date();
    dtExpires.setFullYear(dtExpires.getFullYear()+20); // 20 years is enough
  }
  szCook = szKey + szValue +
             "; expires=" + dtExpires.toGMTString() + "; path=/";
  document.cookie = szCook;
}

function getURLParameter(sName) 
{
  var sURL = window.document.URL.toString();
  if (sURL.indexOf("?") > 0){
    var arrParams = sURL.split("?");
    var arrURLParams = arrParams[1].split("&");
    var i = 0;
    for (i=0;i<arrURLParams.length;i++) {
      var sParam = arrURLParams[i].split("=");
      if (sName == sParam[0]) return unescape(sParam[1]);
    }
    return null;
  } else {
    return null;
  }
}