// cookie handling class by Greg Brown, IBDG
function CookieMonster(){};
CookieMonster.prototype.get = function (nameOfCookie){
	if (document.cookie.length > 0){
		var regexp = new RegExp(nameOfCookie + '(\\[.*?\\])?' + '=[^;]*', 'g'),
				matches = document.cookie.match(regexp),
				returnValue = {}, temp, nameParts;
		
		if (matches){
			for (var i = 0; i < matches.length; i++){
				// seperate cookie's name and value
				matches[i] = matches[i].split('=');
				// get name parts
				nameParts = matches[i][0].split(/\]?\[/);
				
				// set temp as a pointer to the return value- this pointer will be moved up the returnValue tree so we can set the appropriate property of the object
				temp = returnValue; 
				
				for (var j = 0, jl = nameParts.length; j < jl; j++){
					nameParts[j] = unescape(nameParts[j]) || 0;
					
					// if last time through loop, do the actual setting of the value
					if (j == jl - 1){
						// need to get rid of the last ] in case of name[index][index2] etc - other [ and ] will be removed when the string is split
						nameParts[j] = nameParts[j].replace(/\]$/, '');
						// set cookie value in returnValue
						temp[nameParts[j]] = unescape(matches[i][1]);
					}
					else {
						// otherwise move pointer
						if (!temp[nameParts[j]] || typeof temp[nameParts[j]] !== 'object')
							temp[nameParts[j]] = {};

						temp = temp[nameParts[j]];
					}
				}
			}
			
		}
		
		return returnValue[nameOfCookie];
	}
	return null;
};
// handles object/array values
CookieMonster.prototype.set = function (nameOfCookie, value, expireDays){
	var ExpireDate = new Date (),
			plus = '++'.substring(0,1),
			that = this;
			
	ExpireDate.setTime(ExpireDate.getTime() + (expireDays * 24 * 3600 * 1000));
	
	// recurse on arrays and objects
	if (value instanceof Array){
		for (var i = 0; i < value.length; i++)
			this.set(nameOfCookie + '[' + i + ']', value[i], expireDays);
	}
	else if (typeof value == 'object'){
		for (var i in value)
			this.set(nameOfCookie + '[' + escape(i) + ']', value[i], expireDays);
	}
	else {
		// otherwise set the cookie
		document.cookie = nameOfCookie + "=" + escape(value) + ((expireDays == null) ? "" : "; expires=" + ExpireDate.toGMTString()) + "; path=/";
	}

};
CookieMonster.prototype.del = function (nameOfCookie){
	if (document.cookie.length > 0){
		var regexp = new RegExp(nameOfCookie + '(\\[.*?\\])?' + '=[^;]*', 'g'),
				matches = document.cookie.match(regexp), name;
		
		// loop through all the bits of this cookie and kill them individually
		if (matches){
			for (var i = 0; i < matches.length; i++){
				name = matches[i].split('=')[0];
				alert(name);
				document.cookie = name + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT; path=/";
			}
		}
	}
};



var ghost_Cookies = new CookieMonster();
