//  All code by Gunni Rode | © netbureauet ARANEUM 2000 | http://www.na.dk

NA_Cookie.id = "NACookie";

function NA_Cookie (pID, pDocument, pExpireDaysFromNow, pPath, pDomain, pSecure) {
  this.id       = NA_Cookie.id + pID
	this.document = pDocument || document;
  this.expires  = (pExpireDaysFromNow != null) ? new Date((new Date()).getTime() + pExpireDaysFromNow * 150000) : null;
  this.path     = pPath     || '/';
  this.domain   = pDomain   || null;
  this.secure   = pSecure   || null;
	this.cookie   = [];
  eval(this.id + ' = this');
}

NA_Cookie.prototype.value    =
  function () {
    if (arguments.length > 1) {
  		for (var i = 0; i < Math.floor(arguments.length / 2); i += 2) {
  			this.cookie[arguments[i]] = arguments[i + 1];
      }
		} else {
  		return this.cookie[arguments[0]];
    }
	}

NA_Cookie.prototype.removeValue =
  function () {
		for (var i = 0; i < arguments.length; i++) {
  		delete this.cookie[arguments[i]];
    }
	}

NA_Cookie.prototype.load =
  function () {
    var lCookies = this.document.cookie;
		if (lCookies != "") {
      var lStart = lCookies.indexOf(this.id + '=');
      if (lStart == -1) {
        return false;
      }
      lStart += this.id.length + 1;
      var lEnd = lCookies.indexOf(';', lStart);
      if (lEnd == -1) {
        lEnd = lCookies.length;
      }
      var lCookieValues = lCookies.substring(lStart, lEnd);
      var lPairs = lCookieValues.split('&');
      for (var i = 0; i < lPairs.length; i++) {
        var lPair = lPairs[i].split('=');
        this.cookie[lPair[0]] = unescape(lPair[1]);
			}
		} else {
      return false;
		}
    return true;
	}

NA_Cookie.prototype.save =
  function () {
    var lCookieValues = "";
		for (var lName in this.cookie) {
      lCookieValues += ((lCookieValues != "") ? '&' : '');
      lCookieValues += lName + '=' + escape(this.cookie[lName]);
		}
    if (lCookieValues != "") {
  		this.store(lCookieValues, (this.expires) ? this.expires.toGMTString() : null);
    }
	}

NA_Cookie.prototype.store =
  function (pValues, pExpire) {
    this.document.cookie = this.id + '=' + pValues + ';' +
                           ((this.path)   ? 'path='    + this.path   + ';' : '') +
                           ((this.domain) ? 'domain='  + this.domain + ';' : '') +
                           ((pExpire)     ? 'expires=' + pExpire     + ';' : '');
  }

NA_Cookie.prototype.remove =
  function () {
    this.store('', 'Fri, 02-Jan-1970 00:00:00 GMT');
		eval(this.id + '=null');
	}

NA_Cookie.prototype.toString =
  function () {
		var lString = '';

		for (var lAttribute in this) {
      if (lAttribute != 'cookie') {
  			lString += '\n' + lAttribute + ' (' + (typeof (this[lAttribute])) + '): ';
        if (typeof (this[lAttribute]) == 'function') {
          lString += '[..]';
			  } else {
          lString += this[lAttribute];
        }
		  }
    }
    lString += '\n\ncookievalues:';
		for (lAttribute in this.cookie) {
      lString += '\n  ' + lAttribute + ' = ' + this.cookie[lAttribute];
    }
		return lString;
	}