function cookieManager() {
}

cookieManager.prototype.load = function(name) {
	var all_ck = document.cookie;
	var start_idx = all_ck.indexOf(name+"=");
	if (start_idx < 0)
		return "";
	
	var end_idx = all_ck.indexOf(";", start_idx);
	var body = "";
	if (end_idx < 0)
		body = all_ck.substring( start_idx+name.length+1);
	else
		body = all_ck.substring( start_idx+name.length+1, end_idx );
	
	body = unescape(body);
	
	end_idx = body.indexOf("}");
	start_idx = body.indexOf("{");
	if ((start_idx == 0) && (end_idx != -1)) {
		ret = eval("("+body+")");
	} else 
		ret = body;
	
	return ret;
}

cookieManager.prototype.save = function(name, obj, date) {
	if (!name)
		throw new Error("Invalid cookie name");
	
	var proto = "proto";
	var fnct = function() {
		return null;
	};
	
	var txt = "";
	if (typeof(obj) == typeof(proto))
		txt = obj;
	else {
		// "{ property: "value", property: value }"
		txt = "{";
		var sep = "";
		for (var prp in obj) {
			var property = obj[prp];
			if (typeof(property) != typeof(fnct)) {
				txt += sep+prp+": \""+property+"\"";
				sep = ",";
			}
		}
		txt += "}";
	}
	
	//save cookie
	txt = name+"="+escape(txt);
	if (date)
		txt += "; expires="+date;
	document.cookie = txt;
}

cookieManager.prototype.erase = function(name) {
	var dt = new Date();
	dt.setYear( dt.getYear()+1900-2 );
	this.save(name, "", dt.toString());
}
