// -- Cookie Handling --------------------------------------------------------------------------------------------------------------------------
//
// Author:	Martijn Polak <martijn.polak@amgate.com>
// Description: Cookie handling functions
//

// Constructor
function objectCookie(name, expires, path, domain, secure) {

	this.name = escape(name);
	this.expires = expires;
	this.path = path;
	this.domain = domain;
	this.secure = secure;
	this.data = this.get();

}

// Get cookie
objectCookie.prototype.get = function() {

	var value = '';
	var posName = document.cookie.indexOf(this.name + '=');
	
	if (posName != -1) {

		var posValue = posName + (this.name + '=').length;
		var endPos = document.cookie.indexOf(';', posValue);

		if (endPos != -1) {
			value = unescape(document.cookie.substring(posValue, endPos));
		} else {
			value = unescape(document.cookie.substring(posValue));
		}

	}

	return value;

}

// Set cookie
// The specification for cookies says that browsers need support no more than 300 different cookies.
// Of those, no more than 20 should be associated with any particular server. 
// For each cookie, the data stored is not expected to exceed 4 kilobytes.
objectCookie.prototype.set = function(value) {

	document.cookie = this.name + '=' + escape(value)
		+ (this.expires ? '; EXPIRES=' + this.expires.toGMTString() : '')
		+ (this.path ? '; PATH=' + this.path : '')
		+ (this.domain ? '; DOMAIN=' + this.domain : '')
		+ (this.secure ? '; SECURE' : '');

	this.data = this.get();

}

// Delete entire cookie
objectCookie.prototype.clear = function() {

	document.cookie = this.name + '=; expires=' + this.getexpirydate(-1);
	this.data = this.get();

}


// Returns a date in UTC format which can be used to set an expiration date for a cookie
objectCookie.prototype.getexpirydate = function(days){

	var today = new Date();
	var ms = Date.parse(today);
	today.setTime (ms + days * 24 * 60 * 60 * 1000);
	return today.toUTCString();

}

// Appends a key/value
objectCookie.prototype.append = function(key, value) {

	var cnt = new RegExp(key + ":[^|]*", "ig");
	var newkoek = this.data + key + ':' + value + '|';
	var m = this.data.match(cnt);

	//if ((!m || !m[0]) && newkoek.length <= 4096) this.set(newkoek);
	if (!this.has(key) && newkoek.length <= 4096) this.set(newkoek);

}

// Deletes a key/value
objectCookie.prototype.remove = function(key) {

	var cnt = new RegExp(key + ":[^|]*", "ig");
	var m = this.data.match(cnt);

	//if (m && m[0]) this.set(this.data.replace(m[0] + '|', ''));
	if (this.has(key)) this.set(this.data.replace(m[0] + '|', ''));

}

// Gets a specific value
objectCookie.prototype.value = function(key) {

	var cnt = new RegExp(key + ":[^|]*", "ig");
	var m = this.data.match(cnt);

	if (m && m[0]) {
		var s = m[0].split(':');
		if (s) if (s[1]) return s[1];
	}

}

// Checks to see if a specific key is stored in this cookie
objectCookie.prototype.has = function(key) {

	var cnt = new RegExp(key + ":[^|]*", "ig");
	var m = this.data.match(cnt);

	return (m && m[0] ? true : false);

}
