/******************************************************************************/
// lib_cookies - 07/2003
// written by Pascal Carles (p.carles@aprim-t.com)
// You can use, modify or distribute freely this code as far as you mention
// my tribute to it somewhere in the sources...
/******************************************************************************/

function Cookie(aName, aValue, aExpire, aPath, aDomain, aSecure) {
	aSecure = aSecure || false;
	aDomain = aDomain || false;
	aPath = aPath || "/";
	aExpire = aExpire || false;
	aValue = aValue || "";
	
	this.name=aName;
	this.value=aValue;
	this.hasKeys=false;
	this.isCrypt=true;
	this.expire=aExpire;
	this.path=aPath;
	this.domain=aDomain;
	this.secure=false;
	this.set=setCookie;
	this.get=getCookie;
	this.del=delCookie;
	this.getKey = getKeyCookie;
	this.setKey = setKeyCookie;
	this.delKey = delKeyCookie;
}

function setCookie() {
  var str = this.name + "=" + escape(this.value);
  str += (this.expire) ? ";expires=" + this.expire.toGMTString() : "";
  str += (this.path) ? ";path=" + this.path : "";
  str += (this.domain) ? ";domain=" + this.domain : "";
  str += (this.secure) ? ";secure" : "";
  document.cookie = str;
}

function getCookie() {
  this.value=unescape(getKeyVal(this.name, document.cookie, ";"));
}

function delCookie() {
  this.get();
  if (this.value) {
    this.expire = new Date(1970, 1, 01);
	this.set();
  }
}

function getKeyCookie(aKey) {
	return getKeyVal(aKey, this.value);
}

function setKeyCookie(aKey, aValue) {
	this.value = setKeyVal(aKey, aValue, this.value);
}

function delKeyCookie(aKey) {
	this.value = delKeyVal(aKey, this.value);
}

function getKeyVal(aKey, aKeysList, aKeySep) {
  aKeySep = aKeySep || "&";
  if(aKeysList) {
	  var prefix = aKey + "=";
	  var begin = aKeysList.indexOf(prefix);
	  if (begin == -1) return false;
	  var end = aKeysList.indexOf(aKeySep, begin);
	  if (end == -1) end = aKeysList.length;
	  return aKeysList.substring(begin + prefix.length, end);
  }
  else {
  	  return aKeysList;
  }
}

function setKeyVal(aKey, aValue, aKeysList, aKeySep) {
  aKeySep = aKeySep || "&";
  var newStr = "";
  var prefix = aKey + "=";
  var begin = aKeysList.indexOf(prefix);
  if (begin == -1) {
    if(aKeysList) {
      	newStr = aKeysList + aKeySep + prefix + aValue;
    }
    else {
    	newStr = prefix + aValue;
    }
  } 
  else {
  	newStr = aKeysList.substring(0, begin + prefix.length);
  	var end = aKeysList.indexOf(aKeySep, begin + prefix.length);
  	if (end == -1) {
    	newStr += aValue;
  	}
  	else {
  		newStr += aValue + aKeysList.substr(end);
  	}
  }
  return newStr;
}

function delKeyVal(aKey, aKeysList, aKeySep) {
  aKeySep = aKeySep || "&";
  var newStr = "";
  if(aKeysList) {
	  var prefix = aKey + "=";
	  var begin = aKeysList.indexOf(prefix);
	  if (begin == -1) {
    	newStr = aKeysList;
	  } 
	  else {
	  	if(begin!=0) begin=begin-1;
	  	newStr = aKeysList.substring(0, begin);
	  	var end = aKeysList.indexOf(aKeySep, begin + prefix.length);
	  	if (end != -1) {
	  		if(begin==0) end=end+1;
	  		newStr += aKeysList.substr(end);
	  	}
	  }
  }
  return newStr;
}

function theEndDate() {
	return new Date(2050, 4, 13);
}

