]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/opac/common/js/Cookie.js
moved isXUL to RemoteRequest
[Evergreen.git] / Open-ILS / web / opac / common / js / Cookie.js
1 /*
2
3 DISCLAIMER: THESE JAVASCRIPT FUNCTIONS ARE SUPPLIED 'AS IS', WITH 
4 NO WARRANTY EXPRESSED OR IMPLIED. YOU USE THEM AT YOUR OWN RISK. 
5 PAUL STEPHENS DOES NOT ACCEPT ANY LIABILITY FOR 
6 ANY LOSS OR DAMAGE RESULTING FROM THEIR USE, HOWEVER CAUSED. 
7
8 Paul Stephens' cookie-handling object library
9
10 Version 2.1
11 2.0 - Introduces field names
12 2.1 - Fixes bug where undefined embedded fields[] elements weren't written to disk
13
14 www.paulspages.co.uk 
15
16 TO USE THIS LIBRARY, INSERT ITS CONTENTS IN THE <HEAD> SECTION 
17 OF YOUR WEB PAGE SOURCE, BEFORE ANY OTHER JAVASCRIPT ROUTINES.
18
19 (C) Paul Stephens, 2001-2003. Feel free to use this code, but please leave this comment block in. This code must not be sold, either alone or as part of an application, without the consent of the author.
20 */
21
22 function cookieObject(name, expires, accessPath) {
23 var i, j
24 this.name = name
25 this.fieldSeparator = "#"
26 this.found = false
27 this.expires = expires
28 this.accessPath = accessPath
29 this.rawValue = ""
30 this.fields = new Array()
31 this.fieldnames = new Array() 
32 if (arguments.length > 3) { // field name(s) specified
33   j = 0
34   for (i = 3; i < arguments.length; i++) {
35     this.fieldnames[j] = arguments[i]    
36     j++
37   }
38   this.fields.length = this.fieldnames.length 
39 }
40 this.read = ucRead
41
42 this.write = ucWrite
43
44 this.remove = ucDelete
45 this.get = ucFieldGet
46 this.put = ucFieldPut
47 this.namepos = ucNamePos
48 this.read()
49 }
50
51
52 function ucFieldGet(fieldname) {
53 var i = this.namepos(fieldname)
54 if (i >=0) {
55   return this.fields[i]
56 } else {
57   return "BadFieldName!"
58 }
59 }
60
61 function ucFieldPut (fieldname, fieldval) {
62 var i = this.namepos(fieldname)
63
64 if(i < 0) {
65         i = this.fieldnames.length;
66         this.fieldnames[i] = fieldname;
67 }
68
69 this.fields[i] = fieldval
70 return true
71 }
72
73 function ucNamePos(fieldname) {
74 var i 
75 for (i = 0; i < this.fieldnames.length; i++) {
76   if (fieldname == this.fieldnames[i]) {
77     return i
78   }
79 }
80 return -1
81 }
82
83
84 function ucWrite() {      
85   var cookietext = this.name + "=" 
86
87 // concatenate array elements into cookie string
88 // Special case - single-field cookie, so write without # terminator
89 if (this.fields.length == 1) {
90   cookietext += escape(this.fields[0])
91   } else { // multi-field cookie
92     for (i= 0; i < this.fields.length; i++) {
93       cookietext += escape(this.fields[i]) + this.fieldSeparator }
94   }
95
96
97 // Set expiry parameter, if specified
98     if (this.expires != null) {  
99       if (typeof(this.expires) == "number") { // Expiry period in days specified  
100         var today=new Date()     
101         var expiredate = new Date()      
102         expiredate.setTime(today.getTime() + 1000*60*60*24*this.expires)
103         cookietext += "; expires=" + expiredate.toGMTString()
104       } else { // assume it's a date object
105         cookietext +=  "; expires=" + this.expires.toGMTString()
106       } // end of typeof(this.expires) if
107     } // end of this.expires != null if 
108    
109 // add path, if specified
110    if (this.accessPath != null) {
111    cookietext += "; PATH="+this.accessPath }
112
113 // write cookie
114    // alert("writing "+cookietext)
115    document.cookie = cookietext 
116    return null  
117 }
118
119
120 function ucRead() {
121   var search = this.name + "="                       
122   var CookieString = document.cookie            
123   if(CookieString == null) CookieString = "";
124   this.rawValue = null
125   this.found = false     
126   if (CookieString.length > 0) {                
127     offset = CookieString.indexOf(search)       
128     if (offset != -1) {                         
129       offset += search.length                   
130       end = CookieString.indexOf(";", offset)   
131       if (end == -1) {  // cookie is last item in the string, so no terminator                        
132        end = CookieString.length }              
133       this.rawValue = CookieString.substring(offset, end)                                   
134       this.found = true 
135       } 
136     }
137    
138 if (this.rawValue != null) { // unpack into fields
139
140   var sl = this.rawValue.length
141   var startidx = 0
142   var endidx = 0
143   var i = 0
144
145 // Special case - single-field cookies written by other functions,
146 // so without a '#' terminator
147
148 if (this.rawValue.substr(sl-1, 1) != this.fieldSeparator) {
149   this.fields[0] = unescape(this.rawValue)
150   } else { // separate fields
151
152   do  
153   {
154    endidx = this.rawValue.indexOf(this.fieldSeparator, startidx)
155    if (endidx !=-1) {
156      this.fields[i] = unescape(this.rawValue.substring(startidx, endidx))
157      i++
158      startidx = endidx + 1}
159   }
160   while (endidx !=-1 & endidx != (this.rawValue.length -1));
161 }
162 } // end of unpack into fields if block
163   return this.found
164 } // end of function
165
166
167 function ucDelete() {
168   this.expires = -10
169   this.write()
170   return this.read()
171 }
172
173