// sets a cookie to future date
function setCookie(name,value)
{  var expires = new Date(2037,5,25)
	document.cookie = name + "=" + escape(value) + "; path=/" + ((expires == null) ? "" : ("; expires=" + expires.toGMTString()))
}

function toogleCookie(obj,name)
{  if (obj.checked) { setCookie(name,"Y") }
	else { setCookie(name,"N") }
}

// Used for writing data to specified keys in the "SESSION" cookie
function WriteSessionCookieKey(strNewKeyName, NewKeyValue)
{  var strNewKeyValue = NewKeyValue ;
   var strOldSessionCookieValue = GetSessionCookie() ;
   var strNewKey = strNewKeyName+"="+strNewKeyValue ;
   if (strOldSessionCookieValue == null) { document.cookie = "SESSION=" + strNewKey + "; path=/" ;  }
   else
   {  var strOldKey = ReadSessionCookieKey(strNewKeyName, false, false) ;
      if (strOldKey == null) { document.cookie = strOldSessionCookieValue + "&" + strNewKey + "; path=/" ;  }
      else
      {  strNewCookieValue = strOldSessionCookieValue.replace(strOldKey,strNewKey);
         document.cookie = strNewCookieValue + "; path=/" ;
      }
   }
}

function GetSessionCookie()
{  var strCookies = document.cookie ;
   var strSessionCookie = null ;
   if (strCookies.length > 0)
   {  var strSessionCookieStart = "SESSION=" ;
      intSessionCkStart = strCookies.indexOf(strSessionCookieStart) ;
      if (intSessionCkStart != -1)
      {  intSessionCkEnd = strCookies.indexOf(";", intSessionCkStart) ;
         if (intSessionCkEnd == -1) { intSessionCkEnd = strCookies.length ;  }
         strSessionCookie = strCookies.substring(intSessionCkStart, intSessionCkEnd) ;
      }
   }
   return strSessionCookie ;
}

function ReadSessionCookieKey(KeyName, blnJustValue, blnConvert)
{
   var strSessionCookieVal = GetSessionCookie() ;
   var strSessionCookieKey = null ;

   if (typeof(blnJustValue) == "undefined") { blnJustValue = true ;}
   if (typeof(blnConvert) == "undefined") { blnConvert = true ; }

   if ((strSessionCookieVal != null)&&(KeyName != ""))
   {  var intCkKeyStart = strSessionCookieVal.indexOf(KeyName+"=");
      if (intCkKeyStart != -1) //Key exists
      {  if (blnJustValue) { intCkKeyStart += KeyName.length + 1 ; } // set index of beginning of key value
          intCkKeyEnd = strSessionCookieVal.indexOf("&", intCkKeyStart); // set index of end of key value
          if (intCkKeyEnd == -1) { intCkKeyEnd = strSessionCookieVal.indexOf(";", intCkKeyStart) ; }
          if (intCkKeyEnd == -1) { intCkKeyEnd = strSessionCookieVal.length  ; }
          strSessionCookieKey = strSessionCookieVal.substring(intCkKeyStart, intCkKeyEnd)  ;
       }
    }
    return strSessionCookieKey ;
}
