﻿Cookies = {
	
	add : function(cookie) {
		var cookieString = cookie.name + "=";
		cookieString += escape(cookie.value);

		if(cookie.expires != null) {
			cookieString += ";expires=" + cookie.expires.toGMTString();
		}
		
		if(cookie.path != null) {
			cookieString += ";path=" + cookie.path;
		}
		 
		document.cookie = cookieString;
	},
	
	remove : function(name) {
		Cookies.add(new Cookie(name, "", new Date()));
	},
	
	hasCookie : function(name) {
		return Cookie.get(name) != null;
	},
	
	get : function(name) {
		var cookies = Cookies.getList();
		for(var i = 0; i < cookies.length; i++) {
			var cookie = cookies[i];
			if(cookie.name == name) return cookie;
		}
		return null;
	},
	
	getList : function() {
		var list = new Array();
		var cookies = document.cookie.split(';');
		
		for(var i = 0; i < cookies.length; i++) {
			var cookie = Cookies.getCookieFromString(cookies[i]);			
			list.push(cookie);
		}
		
		return list;
	},
	
	getCookieFromString : function(cookieString) {
		var args = cookieString.split('=', 2);
		
		var cookie = new Cookie();
		cookie.name = Cookies.trim(args[0]);
		
		var value = args.length > 1 ? args[1] : null;
		if(value != null && typeof value != "undefined") {		
			cookie.value = unescape(Cookies.trim(value));
		}
		
		return cookie;
	},
	
	trim : function(val) {
		return val.replace(/^\s+|\s+$/g, '');
	}
}

function Cookie(name, value, expires, path) {
	this.name = name;
	this.value = value;
	this.expires = expires;
	this.path = path;
}