function bakeCookie(name,value) {
   argv=arguments;
   argc=arguments.length;
   expires=(argc>2) ? argv[2] : null;
   path=(argc>3) ? argv[3] : null;
   domain=(argc>4) ? argv[4] : null;
   secure=(argc>5) ? argv[5] : false;
   document.cookie=name+"="+escape(value) +
     ((expires === null) ? "" : ("; expires="+expires.toUTCString())) +
     ((path === null) ? "" : ("; path="+path)) +
     ((domain === null) ? "" : ("; domain="+domain)) +
     ((secure === true) ? "; secure" : "");
}
function eatCookieVal(offset) {
   endstr=document.cookie.indexOf(";",offset);
   if (endstr === -1) {endstr=document.cookie.length;}
   return unescape(document.cookie.substring(offset,endstr));
}
function eatCookie(name) {
   arg=name+"=";
   alen=arg.length;
   clen=document.cookie.length;
   i=0;
   while (i<clen) {
      j=i+alen;
      if (document.cookie.substring(i,j) === arg) {
          return eatCookieVal(j);
          }
      i=document.cookie.indexOf(" ",i) + 1;
      if (i === 0) {break;}
   }
   return null;
}
function tossCookie(name) {
   threeDays=3*24*60*60*1000; //in millisecounds
   expDate=new Date();
   expDate.setTime(expDate.getTime()-threeDays);
   document.cookie=name+'=foobar; expires='+expDate.toGMTString();
}

function addMHCCookie(n)
{
  var currentCookie=eatCookie("myCams");
  var newCookie="";
  
  var added=false;
  var wCookie="";
  if(currentCookie==null||currentCookie=="")
  {
    wCookie=n;
    document.getElementById("mhcSpan").firstChild.data="Remove from My Highwaycams";
    added=true;
  }
  else
  {
    var ccSplit=currentCookie.split(",");
    var alreadyIn=false;
    var fst=false;
    
    for(var i=0; i<ccSplit.length; i++)
    {
      if(ccSplit[i]==n) 
      {
        alreadyIn=true;
      }
      else 
      {
        if(!fst) fst=true;
        else wCookie+=",";
        wCookie+=ccSplit[i];
      }
    }
    
    if(!alreadyIn)
    {
      if(fst) wCookie+=","+n;
      else wCookie=n;
      
      document.getElementById("mhcSpan").firstChild.data="Remove from My Highwaycams";
    }
    else
    {
      document.getElementById("mhcSpan").firstChild.data="Add to My Highwaycams";
    }
  }
  
  var expdate=new Date();
  expdate.setFullYear(expdate.getFullYear()+2);
  
  bakeCookie("myCams", wCookie, expdate);
}

function getMHCList()
{
  var mhcCookie=eatCookie("myCams");
  if(mhcCookie==null) return new Array();
  else return mhcCookie.split(",");
}

function checkMHCLink(webcamId)
{
  var mhcList=getMHCList();
  document.getElementById("addToMyCamsLink").style.display="block";
  for(var i=0; i<mhcList.length; i++) 
  {
    if(mhcList[i]==webcamId) 
    {
      document.getElementById("mhcSpan").firstChild.data="Remove from My Highwaycams";
    }
  }
}