﻿function BerryCookie(){this.realTime=true;this.expireDays=7;this.domain=location.hostname;this.path=location.pathname.substring(0,location.pathname.lastIndexOf("/")+1);this.cache=new Array();this.load=BerryCookie_load;this.save=BerryCookie_save;this.toChangeCommand=BerryCookie_toChangeCommand;this.toRemoveCommand=BerryCookie_toRemoveCommand;this.get=BerryCookie_get;this.set=BerryCookie_set;this.remove=BerryCookie_remove;this.load();}function BerryCookie_load(){pairs=document.cookie.split(";");for(i=0;i<pairs.length;i++){pair=unescape(pairs[i]).replace(/(^\s*)|(\s*$)/,"");eqpos=pair.indexOf("=");key=pair.substring(0,eqpos);value=pair.substring(eqpos+1);cookie=new Array();cookie["value"]=value;cookie["status"]="ok";this.cache[key]=cookie;}}function BerryCookie_save(){for(key in this.cache){switch(this.cache[key]["status"]){case "changed":document.cookie=this.toChangeCommand(key,this.cache[key]["value"]);this.cache[key]["status"]="ok";break;case "removed":document.cookie=this.toRemoveCommand(key);delete this.cache[key];break;}}}function BerryCookie_toChangeCommand(key,value){command="";entry=key+"="+value+"; ";command+=entry;entry="domain="+this.domain+"; ";command+=entry;entry="path="+this.path+"; ";command+=entry;millis=86400000;date=new Date();date.setTime(date.getTime()+millis*this.expireDays);entry="expires="+date.toGMTString()+";";command+=entry;return command;}function BerryCookie_toRemoveCommand(key){date=new Date();date.setTime(0);command=key+"=; expires="+date.toGMTString()+";";return command;}function BerryCookie_get(key){if(this.cache[key]!=undefined){if(this.cache[key]["status"]!="removed"){return this.cache[key]["value"];}}return undefined;}function BerryCookie_set(key,value){cookie=new Array();cookie["value"]=value;cookie["status"]="changed";this.cache[key]=cookie;if(this.realTime)this.save();}function BerryCookie_remove(key){if(this.cache[key]!=undefined){this.cache[key]["status"]="removed";if(this.realTime)this.save();}}
