function clsCookie()
{
 //propriedades
 this.cookieName = "";
 this.strDominio = "";
 this.expdate = null;
 this.strPath = "";
 
 //metodos
 this.getCookieVal = getCookieVal;
 this.GetCookie = GetCookie;
 this.GetSubCookie = GetSubCookie;
 this.getSubCookieVal = getSubCookieVal;
 this.fetchPath = fetchPath;
 this.FixCookieDate = FixCookieDate;
 this.SetCookie = SetCookie;
 this.RemoveSubCookie = RemoveSubCookie;
 this.SetSubCookie = SetSubCookie;
 this.fnintInicializa = fnintInicializa;
 this.strPreparaPar = strPreparaPar;
 this.RemoveCookie = RemoveCookie;
 
 return this;
}

function fnintInicializa(parmstrCookieName,ParmstrDominio,ParmstrMaskIntervaloExpiracao,ParmintIntervaloExpiracao,ParmstrPath,ParmbolIniciaCookie,ParmstrCookie)
{
 this.cookieName = parmstrCookieName;
 this.strDominio = ParmstrDominio;
 this.strPath = ParmstrPath+"";
 
 var intIntervalo = 0;
 
 switch (ParmstrMaskIntervaloExpiracao)
 {
 				case "y":
						 intIntervalo = 31536000000 * ParmintIntervaloExpiracao;
						 break;
 				case "mi":
						 intIntervalo = 600000 * ParmintIntervaloExpiracao;
						 break;
 }
 
 this.expdate = new Date ();
 this.FixCookieDate (this.expdate);         // Correct for Mac date bug
 this.expdate.setTime (this.expdate.getTime() +  intIntervalo);  // 1 year from now

 //cria o cookie
 if (ParmbolIniciaCookie == 1)
 {
 	this.SetCookie(this.cookieName,ParmstrCookie,this.expdate,this.strPath)
 }
}

// "Internal" function to return the decoded value of a cookie
function getCookieVal (offset) 
{
   var endstr = document.cookie.indexOf (";", offset);
   if (endstr == -1)
      endstr = document.cookie.length;
   return unescape(document.cookie.substring(offset, endstr));
}

//  Function to return the value of the cookie specified by "name".
function GetCookie (name) 
{
   var arg = name + "=";
   var alen = arg.length;
   var clen = document.cookie.length;
   var i = 0;
   while (i < clen) {
      var j = i + alen;
      if (document.cookie.substring(i, j) == arg)
         return this.getCookieVal (j);
      i = document.cookie.indexOf(" ", i) + 1;
      if (i == 0) break;
   }
   return null;
}

//  Function to return the value of the "imbedded" cookie specified by "name".
function GetSubCookie (subcookieName) 
{
   var cookieName= this.cookieName;   // replace "Awsvmdemo" with your course's number
   var fullCookie= new String();
   fullCookie=this.GetCookie(cookieName);
   if (fullCookie!=null) {
      var arg = subcookieName + "=";
      var alen = arg.length;
      var clen = fullCookie.length;
      var i=0
         while (i < clen) {
            var j = i + alen;
            if (fullCookie.substring(i, j) == arg)
               return this.getSubCookieVal (fullCookie, j);
            i = fullCookie.indexOf(" ", i) + 1;
            if (i == 0) break;
         }
   }
   return null;
}

// "Internal" function to return the decoded value of a cookie
function getSubCookieVal (fullCookie, offset) 
{
   var endstr = fullCookie.indexOf (";", offset);
   if (endstr == -1)
      endstr = fullCookie.length;
   return unescape(fullCookie.substring(offset, endstr));
}

function fetchPath(loc)
{
   var myURL = loc + "";   // Coerce to string object for IE 3.x 
   var myPos = myURL.indexOf( "/", 8 ); // Skip past the "http://"
   var myPth = myURL.substring( myPos + 1 );
   myPos = myPth.lastIndexOf( "/" );    // Trim the .html filename
   if ( myPos != -1 )
      return myPth.substring( 0, myPos );
   else
      return "";
}

function RemoveCookie(name,path)
{
	var strDominio = this.strDominio;

      // Delete the old cookie for SVM
      document.cookie = name+"=nodata" +    // replace "Awsvmdemo" with your course's number
         "; expires=Fri, 02-Jan-1970 00:00:00 GMT; path=/" + fetchPath( path ) +
         ((strDominio) ? "; domain=" + strDominio : "" );
}

//  Function to correct for 2.x Mac date bug.  Call this function once to
//  fix a date object prior to passing it to SetCookie.
function FixCookieDate (date) 
{
   var base = new Date(0);
   var skew = base.getTime(); // dawn of (Unix) time - should be 0
   if (skew > 0)  // Except on the Mac - ahead of its time
      date.setTime (date.getTime() - skew);
}
			
//  Function to create or update a cookie.
function SetCookie(name,value,expdate,path) 
{
	var strDominio = this.strDominio;

  document.cookie = name + "=" + escape (value) +
	  "; expires=" + expdate.toGMTString() +
    "; path=/"   + fetchPath(path) +
	  ((strDominio) ? "; domain=" + strDominio : "" );
}			

function RemoveSubCookie(fullCookie, subcookieName)
{
   if (fullCookie!=null) {
      var arg = subcookieName + "=";
      var alen = arg.length;
      var clen = fullCookie.length;
      var i=0
         while (i < clen) {
            var j = i + alen;
            if (fullCookie.substring(i, j) == arg) {
               //subCookie found.  Remove it.
               var endstr = fullCookie.indexOf (";", i);
               if (endstr == -1)
                  return fullCookie.substring(0,i)
                  return fullCookie.substring(0,i) + fullCookie.substring(endstr +2,fullCookie.length);
            }
            i = fullCookie.indexOf(" ", i) + 1;
            if (i == 0) break;
         }
         return fullCookie;
   }
}			

// Function to create or update a subCookie
function SetSubCookie (subcookieName,value) 
{
   var cookieName=this.cookieName;   // replace "Awsvmdemo" with your course's number
  //determine if subCookie exists, and if so where it is
  var fullCookie= new String();
  fullCookie=this.GetCookie(cookieName);
  //determine if the cookie exists, if it does, remove it
  if (fullCookie!=null)
    fullCookie=this.RemoveSubCookie(fullCookie,subcookieName);
  else
    fullCookie="";

  //add the subcookie to the end
  value=fullCookie + subcookieName + "=" + value + "; "
  this.SetCookie(cookieName,value,this.expdate,this.strPath);
}

function strPreparaPar(subcookieName,value)
{
 return subcookieName + "=" + value + "; ";
}


