var cookielabel="";

//---utility function
function replace( originalsentence, oldword, newword )
{
  var newsentence='', walk=0, index=0, first='', last='';

  newsentence='' + originalsentence;
  index=newsentence.toLowerCase().indexOf(oldword.toLowerCase(),walk);

  while( (walk<newsentence.length) && (index!=(-1)) )
  {
    first=newsentence.substring(0,index);
    last=newsentence.substring(index+oldword.length,newsentence.length);
    newsentence=first + '' + newword + '' + last;
    walk=index+newword.length;
    index=newsentence.toLowerCase().indexOf(oldword.toLowerCase(),walk);
  }

  return(newsentence);
}

// ========================================================================================================================
function getCookie(name)
{
  var offset,search,end;
  search = name + cookielabel + "=";

  if (document.cookie.length > 0)                                // if there are any cookies
  {
    offset = document.cookie.indexOf(search)                     // look for this cookie
    if (offset != (-1))                                          // if this cookie exists 
    {                                            
      offset += search.length;                                   // set index of beginning of value, after cookie name
      end = document.cookie.indexOf(";", offset);                // set end index of end of cookie value
      if (end == (-1)) end = document.cookie.length;
	  //alert("found cookie " + name + " = " + document.cookie.substring(offset, end));
      return(document.cookie.substring(offset, end));  // return the cookie
    }
    else return("");
  }
  else return("");
}

// =========================================================================================================================
function setCookie(name, value, hours)
{
  var expDate, expires="";
  if(hours > 0)
  {
    expires="; expires=" + cookie_date(hours);
  }
  wholevalue = name + cookielabel + "=" + value + "; path=/" + expires;
  document.cookie = wholevalue;
  //alert("set cookie " + name + " to " + value);
  return(value);
}

// =========================================================================================================================
function deleteCookie(name)
{
  document.cookie = name + cookielabel + "=0; path=/; expires=Tuesday, 01-Jan-80 23:59:59 GMT";
}

//the following cookie functions compatible with ASP "Dictionary" cookies:
// ######################## Get Dictionary Cookie Function ########################
	function getDictCookie(name, key, value)
	{
	//this function Gets a single value from a cookie that holds multiple values to overcome the 20 cookie limit
	//uses getCookie(name)
	retVal = '';
	multi = getCookie(name);
	search = escape(key) + '=';
	delim = '&'
	if(multi != '')
	{
	offset = multi.indexOf(search)                     // look for this name
        if (offset != (-1))                                          // if this name exists 
        {                                            
          offset += search.length;                                   // set index of beginning of value, after  name
          end = multi.indexOf(delim, offset);                // set end index of end of cookie value
          if (end == (-1)) end = multi.length;
          retVal = unescape(multi.substring(offset, end));  // return the value
        }
		}
	return(retVal);
	}
	
// ######################## Set Dictionary Cookie Function ########################
	function setDictCookie(name, key, value, perm)
	{
	//this function sets a single cookie to hold multiple values to overcome the 20 cookie limit
	//uses getCookie(name) and setCookie(name, value)
	multi = getCookie(name);
	search = escape(key) + '=';
	newEntry = search + escape(value);
	delim = '&';
	
	if(multi != '')
	  {
		offset = multi.indexOf(search)                     // look for this name
        if (offset != (-1))                                          // if this name exists 
          {                                            
            end = multi.indexOf(delim, offset);                // set end index of end of cookie value
            if (end == (-1)) end = multi.length;
			else end = end + delim.length;
			multi = multi.substring(0, offset) + multi.substring(end, multi.length);
          }

		setCookie(name, multi + delim + newEntry, 365*24);
	  }
	else
		setCookie(name, newEntry, true, 365*24)
	}

	//########################return a date in a format that is accepted by an IE for cookies 
function cookie_date(hours)
{
    expDate=new Date();              //day*hr*mn*sc*mili
    expDate.setTime(expDate.getTime()+(hours*60*60*1000)); // sets expires to one year from now.
	expires = expDate.toGMTString();

	return expires;
}
