// =============================================================================
//
// DIG CMS Template                                                             
//                                                                      
//  $Rev: 625 $
//  $Author: herzog $
//  $Date: 2011-01-13 10:01:29 +0100 (Do, 13 Jan 2011) $
//
// =============================================================================

/**
 * Library for easy Cookie-Handling
 *
 * @author herzog
 * 
 */
function CookieHandler() {
	
	// array holding cookie's data
	// Note: the array's content is treated like a hash map!
	var data = new Array();
	
	// sets the given value for the given key
	// Note: property is a string containing the attributes of the cookie e.g. "path=/;port=80;expires=Tue, 29-Mar-2005 19:30:42 GMT;"
	this.setValue = function(key, value, property) {
	
		if(typeof key == "undefined" || typeof value == "undefined") throw new Exception("CookieHandler: parameter KEY/VALUE missing!");
		for(var i = 0; i < data.length; i++) {
		
			if(data[i].key == key) {
			
				data[i].value = value;
				data[i].property = property ? property : "";
				return;
			}
		}
		data.push( new function() { this.key = key; this.value = value; this.property = property ? property : "" } );
	};
	
	// returns the value belonging to the given key (or null if the key doesn't exist
	this.getValue = function(key) {
	
		if(typeof key == "undefined") throw new Exception("CookieHandler: parameter KEY missing!");
		for(var i = 0; i < data.length; i++) {
		
			if(data[i].key == key) return data[i].value;
		}
		return null;
	};
	
	// returns an array holding all keys
	this.getKeys = function() {
	
		var tmp = new Array();
		for(var i = 0; i < data.length; i++) {
		
			tmp.push(data[i].key);
		}
		return tmp;
	};
	
	// returns an array holding all values
	this.getValues = function() {
		
		var tmp = new Array();
		for(var i = 0; i < data.length; i++) {
		
			tmp.push(data[i].value);
		}
		return tmp;
	};
	
	// stores the cookie
	// Note: If you want to store a cookie, you should call the dataReset()-function before you set the
    //       the values you want to store.
	//       Not resetting the data causes all before stored cookies to be restored with improper properties!
	this.setCookie = function() {
		
		for(var i = 0; i < data.length; i++) {
			
			document.cookie = data[i].key + "=" + data[i].value + ";" + data[i].property;
		}
	};
	
	// returns true if there is a cookie set, false else
	this.hasCookie = function() {
		
		return document.cookie && document.cookie.length > 0;
	};
	
	// returns the number of kv-pairs stored in this cookie
	this.dataSize = function() {
		
		return  data.length;
	};
	
	// resets the Array containing the cookie data
	this.dataReset = function() {
		
		this.data = new Array();
	};
	
	// initialize object
	if(this.hasCookie()) {
		// parsing cookie
		var kv = document.cookie.split(";");
		var tmp;
		for(var i = 0; i < kv.length; i++) {
		
			tmp = kv[i].split("=");
			if(tmp.length > 1) {
				// perform trim() on key
				// cookies stored by this script don't need trim(), but it's necessary for foreign cookies
				this.setValue(tmp[0].replace(/^ */i, "").replace(/ *$/i, ""), tmp[1]);
			}
		}
	}
}

