/* file: _cookie.js

   This file contains three functions that are handy for working with cookies.

   _setCookie(name,value,days,path)
     can be used to store a cookie value. Cookie values are stored in
     name=value pairs, with a specified expiration date. Call _setCookie
     to store a cookie with specified arguments.

     Sample use:
       _setCookie("user","Allison",3650)

     This will store the cookie string user=Allison. The cookie will expire
     3650 days (approximately 10 years) from today.

     Pass a zero (0) for days if you want the cookie to last for the 
     current browser session.

   _getCookie(name)
     can be used to fetch a cookie value. Cookie values are stored in
     name=value pairs. Call _getCookie() with the name portion of the
     cookie to retrieve the associated value. For example, if you stored
     a user's name as the cookie string user=Allison, the call

       userName=_getCookie("user")

     would return the value Allison for oUserName.

     _getCookie will return a null string if no associated cookie value exists.
     You should check the length property of the returned string to see if a
     cookie value was found.

     Sample use:
       userName=_getCookie("user");
       if(userName.length==0)
       {
         alert("no cookie found")
       }
       else
       {
         alert("hello "+userName);
       }

   _clearCookie(name,path)
     can be used to clear a cookie value. Call _clearCookie to clear
     a specified cookie string.

     Sample use:
       _clearCookie("user")

     _clearCookie works by setting the expiration date for the cookie to
     the previous day.

*/

//***************************************************************************

function _setCookie(name,value,days,path)
{
  var string,date;

  string=name+'='+value;
  if(path)
  {
    string=string+';path='+path;
  }
  if(days!=0)
  {
    date=new Date();
    date.setTime(date.getTime()+86400000*days);
    string=string+';expires='+date.toGMTString();
  }
  document.cookie=string;
}

//***************************************************************************

function _clearCookie(name,path)
{
  var string,date;

  string=name+'=dummy';
  if(path)
  {
    string=string+';path='+path;
  }
  date=new Date();
  date.setTime(date.getTime()-8640000);
  string=string+';expires='+date.toGMTString();
  document.cookie=string;
  document.cookie=name+'=dummy;expires='+date.toGMTString();
  document.cookie=name+'=dummy;path=/;expires='+date.toGMTString();
}

//***************************************************************************

function _getCookie(name)
{
  var cookie,list,i,equals,match;

  cookie=document.cookie;
  if(cookie.length==0)
  {
    return '';
  }
  else
  {
    list=cookie.split(';');
    for(i=0;i<list.length;i=i+1)
    {
      equals=list[i].indexOf('=');
      match=list[i].substring(0,equals);
      if(match.substr(0,1)==' ')
      {
        match=match.substr(1);
      }
      if(match==name)
      {
        return list[i].substring(equals+1,list[i].length);
      }
    }
    return '';
  }
}

//***************************************************************************

function _getAllCookies()
{
  var cookieArray,cookie,list,i,equals,match;

  cookieArray=new Array();
  cookie=document.cookie;
  if(cookie.length>0)
  {
    cookieArray=cookie.split(';');
  }
  return cookieArray;
}

//***************************************************************************

function _getAllCookieNames()
{
  var cookieArray,cookie,i;

  cookieArray=new Array();
  cookie=document.cookie;
  if(cookie.length>0)
  {
    cookieArray=cookie.split(';');
    for(i=0;i<cookieArray.length;i++)
    {
      nameValue=cookieArray[i].split('=');
      cookieArray[i]=nameValue[0];
    }
  }
  return cookieArray;
}

//***************************************************************************

function _clearAllCookies()
{
  _clearAllCookiesExcept(new Array());
}

//***************************************************************************

function _clearAllCookiesExcept(saveArray)
{
  var cookieArray,cookie,i,j,flag;

  masterSaveArray=new Array(4);
  masterSaveArray[0]='webCalLoginBypass';
  masterSaveArray[1]='webCalMobileBypass';
  masterSaveArray[2]='webCalMobile';
  masterSaveArray[3]='miswebFontSize';
  cookie=document.cookie;
  if(cookie.length>0)
  {
    cookieArray=cookie.split(';');
    for(i=0;i<cookieArray.length;i++)
    {
      nameValue=cookieArray[i].split('=');
      nameValue[0]=nameValue[0].replace(/^ +/,'');
      nameValue[0]=nameValue[0].replace(/ +$/,'');
      flag=0;
      for(j=0;j<saveArray.length;j++)
      {
        if(nameValue[0]==saveArray[j])
        {
          flag=1;
          break;
        }
      }
      if(flag==0)
      {
        for(j=0;j<masterSaveArray.length;j++)
        {
          if(nameValue[0]==masterSaveArray[j])
          {
            flag=1;
            break;
          }
        }
      }
      if(flag==0)
      {
        _clearCookie(nameValue[0]);
      }
    }
  }
  return;
}

function _encode(s)
{
  return s;
}
