/* */ var cookie_js_version = "Version 3.0S jmyers:Mar-12-2008 Last modified Mar 12, 2008; 12.0/biocyc.org-htdocs version"; var TheCookie; function Cookie_read_parse_cookie() //Reads in the cookie so we can work with it. { //Gets the document.cookie, splits it apart into names,values lists. //Cookie is format name1=val1; name2=val2; etc. Skips/ignores if no = sign. //Returns a new object with slots names[], values[], count. Lists 0-based. var cookie = document.cookie; //Read the magic string. if(cookie == "") { return null; } var pairs = cookie.split("; "); var j = 0; for(var i = 0; i < pairs.length; i++) { var pos = pairs[i].indexOf('='); if(pos == -1) { continue; } var cookie_name = pairs[i].substring(0,pos); var cookie_value = pairs[i].substring(pos+1); if( cookie_name == "expires" ) { this.expires = cookie_value; } else { j++; //array now 1-based. this.names[j] = cookie_name; this.values[j] = cookie_value; this.lookup[cookie_name] = cookie_value; } } this.count = j; } function writePersistentCookie (CookieName, CookieValue, periodType, offset) { var expireDate = new Date (); offset = offset / 1; var myPeriodType = periodType; switch (myPeriodType.toLowerCase()) { case "years": var year = expireDate.getYear(); // Note some browsers give only the years since 1900, and some since 0. if (year < 1000) year = year + 1900; expireDate.setYear(year + offset); break; case "months": expireDate.setMonth(expireDate.getMonth() + offset); break; case "days": expireDate.setDate(expireDate.getDate() + offset); break; case "hours": expireDate.setHours(expireDate.getHours() + offset); break; case "minutes": expireDate.setMinutes(expireDate.getMinutes() + offset); break; default: alert ("Invalid periodType parameter for writePersistentCookie()"); break; } document.cookie = escape(CookieName ) + "=" + escape(CookieValue) + "; expires=" + expireDate.toGMTString() + "; path=/"; } function getCookieValue (cookieName) { var exp = new RegExp (escape(cookieName) + "=([^;]+)"); if (exp.test (document.cookie + ";")) { exp.exec (document.cookie + ";"); return unescape(RegExp.$1); } else return false; } function testSessionCookie () { document.cookie ="testSessionCookie=Enabled"; if (getCookieValue ("testSessionCookie")=="Enabled") return true else return false; } function testPersistentCookie () { writePersistentCookie ("testPersistentCookie", "Enabled", "minutes", 1); if (getCookieValue ("testPersistentCookie")=="Enabled") return true else return false; } function deleteCookie (cookieName) { if (getCookieValue (cookieName)) writePersistentCookie (cookieName,"Pending delete","years", -1); return true; } function Cookie_Save() //Writes out the cookie, saves it for next time. { var date = new Date(); date.setTime(date.getTime()+(120*24*60*60*1000));//set cookie to expire in 120 days var expiresPair = "expires="+date.toGMTString(); var cookieout = ""; for(var i = 1; i <= this.count; i++) { cookieout = this.names[i] + "=" + this.values[i] + "; " ; //alert("Saving " + cookieout + expiresPair ); document.cookie = cookieout + expiresPair + "; path=/"; //Write string out to magic variable, it gets recorded. } } function Cookie_Session_Save() //Writes out the cookie, saves it for next time. { var cookieout = ""; Cookie_Wipe(); //Kill any permanent cookie with same data. Use sledgehammer. for(var i = 1; i <= this.count; i++) { cookieout = this.names[i] + "=" + this.values[i] + "; path=/" + "; " ; //alert("Saving " + cookieout ); document.cookie = cookieout; //Write string out to magic variable, it gets temporarily recorded. } } function Cookie_Wipe() //Wipes out the cookie. Lets it be a session cookie. //Apparently not guaranteed to work well on all browsers... { var date = new Date(); date.setTime(date.getTime()-(120*24*60*60*1000));//set cookie to already expire 120 days ago var expiresPair = "expires="+date.toGMTString(); var cookieout = ""; for(var i = 1; i <= this.count; i++) { cookieout = this.names[i] + "=" + this.values[i] + "; path=/" + "; " ; //alert("Saving " + cookieout + expiresPair ); document.cookie = cookieout + expiresPair; //Write string out to magic variable, it gets recorded. } } function Cookie_findindex(varname) { for(var i = 1; i <= this.count; i++) { //alert( "Test: '" + this.names[i] + "' == '" + varname + "'? " + (this.names[i] == varname) ); if(this.names[i] == varname) { //alert("Found " + varname + " at " + i ); return(i); } } //alert( "Didn't find " + varname + " returning 0 "); return(0); } function Cookie_Add(varname, value) { var j; if( !(j = this.findindex(varname)) ) //not already there? { //new, add this.count++; j = this.count; this.names[j] = varname; } //alert( "Add[" + j + "] " + varname +"="+ value ); this.values[ j ] = value; this.lookup[ varname ] = value; } function Cookie() //Class for handling the cookie. //Yes JS is case sensitive. { //slots this.names = new Array; this.values = new Array; this.lookup = new Array; this.count = 0; this.expires = 0; //methods this.read_parse_cookie = Cookie_read_parse_cookie; this.Save = Cookie_Save; this.Session_Save = Cookie_Session_Save; this.findindex = Cookie_findindex; this.Add = Cookie_Add; //inits this.read_parse_cookie(); //Go get it, fill it up. } TheCookie = new Cookie(); //inhales it, too. //alert( "Cookie count = "+TheCookie.count); //for( var i=1; i<=TheCookie.count; i++) // alert( "cookieVal " + i + ":" + TheCookie.names[i] + "=" + TheCookie.values[i] );