function ReadCookie(name)
{
  var allCookie, CookieVal, length, start, end;
  CookieVal="";
  name=name+"="; //acrescenta sinais de igual para evitar falsas coincidencias
  allCookie=document.cookie;
  length=allCookie.length;
  if(length>0) //nenhum Cookie - o usuario provavelmente os incinerou
  {
    start=allCookie.indexOf(name,0)
    if (start!=-1)
    {
      start+=name.length;
      end=allCookie.indexOf(";",start);
      if(end==-1) {end=length;}
      CookieVal=unescape(allCookie.substring(start,end));
    }
  }
  return (CookieVal);
}

function WriteCookie(name,value,expires,domain,path,secure)
{
  var CookieVal,CookieError;
  CookieVal=CookieError="";
  if(name)
  {
    CookieVal=CookieVal+escape(name)+"="
    if((value) || (value == ""))
    {
      CookieVal=CookieVal+escape(value);
      if(expires) { CookieVal=CookieVal+"; expires="+expires.toGMTString(); }
      if(domain) { CookieVal=CookieVal+"; domain="+domain; }
      if(path) { CookieVal=CookieVal+"; path="+path; }
      if(secure) { CookieVal=CookieVal+"; secure"; }
    }
    else { CookieError=CookieError+"Falha no valor"; }
  }
  else { CookieError=CookieError+"Falha no nome"; }
  if(!CookieError)
  {
    document.cookie=CookieVal;
    if(value!=ReadCookie(name)) { CookieError="Falha na escrita"; }
  }
  return CookieError;
}

function trim(s) 
{
  // Remove espa?os em branco e ENTER ? esquerda  
  while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
  {
    s = s.substring(1,s.length);
  }

  // Remove espa?os em branco e ENTER ? direita
  while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
  {
    s = s.substring(0,s.length-1);
  }
  return s;
}
