oops, Dynarch.Calendar was used by CXGN.Calendar
[cxgn-jslib.git] / CXGN / Cookie.js
blob7517b72875d6bcadc621fd50ba93237e931e3b5b
1 /**
2 * @fileoverview This file has been modified for ease of use. Please download the original from
3 * http://www.jaaulde.com/test_bed/cookieLib/ for the original (and latest) versions.
4 *
5 * @author James Auldridge, Copyright (c) 2005
6 * @version 1.2
7 */
9 /**
10 * A singleton Cookie-handling object
11 * @class Cookie
12 * Methods: get, set, del, test
15 JSAN.use("MochiKit.Logging");
17 var Cookie = window.Cookie || {};
18 Cookie = {
19 get: function(cookieName) {
20 var cookieNameStart,valueStart,valueEnd,value;
21 cookieNameStart = document.cookie.indexOf(cookieName+'=');
22 if (cookieNameStart < 0) {return null;}
23 valueStart = document.cookie.indexOf(cookieName+'=') + cookieName.length + 1;
24 valueEnd = document.cookie.indexOf(";",valueStart);
25 if (valueEnd == -1){valueEnd = document.cookie.length;}
26 value = document.cookie.substring(valueStart,valueEnd );
27 value = unescape(value);
28 if (value == "") {return null;}
29 return value;
31 set: function(cookieName,value,hoursToLive,path,domain,secure) {
32 var domainRegEx = /[^.]+\.[^.]+$/;
33 domain = window.location.hostname.match(domainRegEx);
34 if(domain == 'cornell.edu') domain = 'sgn.cornell.edu';
35 var expireString,timerObj,expireAt,pathString,domainString,secureString,setCookieString;
36 if (!hoursToLive || typeof hoursToLive != 'number' || parseInt(hoursToLive)=='NaN'){
37 expireString = "";
39 else {
40 timerObj = new Date();
41 timerObj.setTime(timerObj.getTime()+(parseInt(hoursToLive)*60*60*1000));
42 expireAt = timerObj.toGMTString();
43 expireString = "; expires="+expireAt;
45 pathString = "; path=";
46 (!path || path=="") ? pathString += "/" : pathString += path;
47 domainString = "; domain=";
48 if(!domain || domain==""){
49 domain = window.location.hostname;
51 // if(domain=="localhost") domain = "localhost.localdomain";
52 domainString += domain;
54 (secure === true) ? secureString = "; secure" : secureString = "";
55 value = escape(value);
56 setCookieString = cookieName+"="+value+expireString+pathString+domainString;
57 document.cookie = setCookieString;
59 del: function(cookieName,path,domain){
60 (!path || !path.length) ? path="" : path=path;
61 (!domain || !domain.length) ? domain="" : domain=domain;
62 Cookie.set(cookieName,"",-8760,path,domain);
64 test: function(){
65 Cookie.set('cT','acc');
66 var runTest = Cookie.get('cT');
67 if (runTest == 'acc'){
68 Cookie.del('cT');
69 testStatus = true;
71 else {
72 testStatus = false;
74 return testStatus;
78 var test = Cookie.test();
79 if(test){
80 MochiKit.Logging.logDebug("Javascript Cookie Setting: Works!");
82 else {
83 MochiKit.Logging.logError("Javascript Cookie Setting: Failed.");
84 var hn = window.location.hostname;
85 if(!/\./.test(hn)){
86 MochiKit.Logging.log(
87 "Cookie failure: You appear to be using a one-word hostname (" + hn + ") without a domain, " +
88 "which won't work with cookies. Try using something like " +
89 "localhost.localdomain, and set /etc/hosts accordingly");