function Trim(str)
{
	var myStr = new String(str);
	for (i = 0; i < myStr.length; i++)
	{
		if (myStr.charAt(i) != " ")
			break;
	}
	myStr = myStr.substr(i);
	for (i = myStr.length - 1; i >= 0; i--)
	{
		if (myStr.charAt(i) != " ")
			break;
	}
	myStr = myStr.substring(0, i + 1);
	return myStr;
}


function Cookie(name, durationValue, durationType, path, domain, secure)
{
   this.affix = "";
   
   if( durationValue )
   {   	  
      var date = new Date();
      var curTime = date.getTime();
			
      switch (durationType) {
		case "m":
			date.setTime(curTime + (1000 * 60 * durationValue));
	  		break;
	  
	  	case "h":
	  		date.setTime(curTime + (1000 * 3600 * durationValue));
	  		break;
	  		
	  	case "d":
	  		date.setTime(curTime + (1000 * 3600 * 24 * durationValue));
	  		break;
	  	
	  	case "w":
	  		date.setTime(curTime + (1000 * 3600 * 24 * 7 * durationValue));
	  		break;
	  	
	  	case "M":
	  		var curMonth = date.getMonth();
	  		var nbYearsToAdd = (curMonth + durationValue) % 11;
	  		var newMonth = curMonth + durationValue - (11 * nbYearsToAdd) - 1;
	  		date.setFullYear(date.getFullYear() + nbYearsToAdd, newMonth);
	  		break;
	  		
	  	case "y":
	  		date.setFullYear(date.getFullYear() + durationValue);
	  		break;
      }       
	  this.affix = "; expires=" + date.toGMTString();
   }
   if( path )
   {
      this.affix += "; path=" + path;
   }
   
   if( domain )
   {
      this.affix += "; domain=" + domain;
   }

   if( secure )
   {
      this.affix += "; secure=" + secure;
   }
   
      
   function getValue()
   {
	  var m = document.cookie.match(new RegExp("(" + name + "=[^;]*)(;|$)"));
	  return m ? m[1] : null;   
   }
   
   this.cookieExists = function()
   {
      return getValue() ? true : false;
   }
      
   this.expire = function()
   {
      var date = new Date();
	  date.setFullYear(date.getFullYear() - 1);
	  document.cookie=name + "=noop; expires=" + date.toGMTString(); 
   }
        
   this.setSubValue = function(key, value)
   {
      var ck = getValue();

      if( /[;, ]/.test(value) )
      {
         //Mac IE doesn't support encodeURI
		 value = window.encodeURI ? encodeURI(value) : escape(value);
      }
      
      if( value )
      {
		 var attrPair = "@" + key + value;
         
         if( ck )         
         {
			 if( new RegExp("@" + key).test(ck) )
	         {
		        document.cookie = ck.replace(new RegExp("@" + key + "[^@;]*"), attrPair) + this.affix;
	         }
	         else
	         {
		        document.cookie = ck.replace(new RegExp("(" + name + "=[^;]*)(;|$)"), "$1" + attrPair) + this.affix;
	         }
         }
         else
         {
			document.cookie = name + "=" + attrPair + this.affix;
         }
      }
      else
      {      
	     if( new RegExp("@" + key).test(ck) )
	     {
	        document.cookie = ck.replace(new RegExp("@" + key + "[^@;]*"), "") + this.affix;
	     }
      }
   }

      
   this.getSubValue = function(key)
   {
      var ck = getValue();

      if( ck )
      {
         var m = ck.match(new RegExp("@" + key + "([^@;]*)"));

	     if( m )
	     {
	        var value = m[1];
			
			if( value )
	        {	        
	           //Mac IE doesn't support decodeURI
			   return window.decodeURI ? decodeURI(value) : unescape(value);
	        }
	     }
      }
   }
   
   this.setFieldFromCookie = function(field)
   {
	  var cookieVal = this.getSubValue(field.id);	   
	  if( cookieVal )
	  {		 
	     field.value = Trim(cookieVal);	     
	  }
   }   
   
   this.saveFieldToCookie = function(field)
   {
		var fieldVal = Trim(field.value);		
		this.setSubValue(field.id, fieldVal);
   }   
		   
}


 
