]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/opac/common/js/Cookie.js
forcing array-ness on non-existant params
[Evergreen.git] / Open-ILS / web / opac / common / js / Cookie.js
1 /*
2 DISCLAIMER: THESE JAVASCRIPT FUNCTIONS ARE SUPPLIED 'AS IS', WITH 
3 NO WARRANTY EXPRESSED OR IMPLIED. YOU USE THEM AT YOUR OWN RISK. 
4 PAUL STEPHENS DOES NOT ACCEPT ANY LIABILITY FOR 
5 ANY LOSS OR DAMAGE RESULTING FROM THEIR USE, HOWEVER CAUSED. 
6
7 Paul Stephens' cookie-handling object library
8
9 Version 2.1
10 2.0 - Introduces field names
11 2.1 - Fixes bug where undefined embedded fields[] elements weren't written to disk
12
13 www.paulspages.co.uk 
14
15 TO USE THIS LIBRARY, INSERT ITS CONTENTS IN THE <HEAD> SECTION 
16 OF YOUR WEB PAGE SOURCE, BEFORE ANY OTHER JAVASCRIPT ROUTINES.
17
18 (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.
19 */
20
21 function cookieObject(name, expires, accessPath) {
22 var i, j
23 this.name = name
24 this.fieldSeparator = "#"
25 this.found = false
26 this.expires = expires
27 this.accessPath = accessPath
28 this.rawValue = ""
29 this.fields = new Array()
30 this.fieldnames = new Array() 
31 if (arguments.length > 3) { 
32   j = 0
33   for (i = 3; i < arguments.length; i++) {
34     this.fieldnames[j] = arguments[i]    
35     j++
36   }
37   this.fields.length = this.fieldnames.length 
38 }
39 this.read = ucRead
40 this.write = ucWrite
41 this.remove = ucDelete
42 this.get = ucFieldGet
43 this.put = ucFieldPut
44 this.namepos = ucNamePos
45 this.read()
46 }
47
48 function ucFieldGet(fieldname) {
49 var i = this.namepos(fieldname)
50 if (i >=0) {
51   return this.fields[i]
52 } else {
53   return "BadFieldName!"
54 }
55 }
56 function ucFieldPut (fieldname, fieldval) {
57 var i = this.namepos(fieldname)
58 if(i < 0) {
59         i = this.fieldnames.length;
60         this.fieldnames[i] = fieldname;
61 }
62 this.fields[i] = fieldval
63 return true
64 }
65 function ucNamePos(fieldname) {
66 var i 
67 for (i = 0; i < this.fieldnames.length; i++) {
68   if (fieldname == this.fieldnames[i]) {
69     return i
70   }
71 }
72 return -1
73 }
74 function ucWrite() {      
75   var cookietext = this.name + "=" 
76 if (this.fields.length == 1) {
77   cookietext += escape(this.fields[0])
78   } else { 
79     for (i= 0; i < this.fields.length; i++) {
80       cookietext += escape(this.fields[i]) + this.fieldSeparator }
81   }
82     if (this.expires != null) {  
83       if (typeof(this.expires) == "number") { 
84         var today=new Date()     
85         var expiredate = new Date()      
86         expiredate.setTime(today.getTime() + 1000*60*60*24*this.expires)
87         cookietext += "; expires=" + expiredate.toGMTString()
88       } else { 
89         cookietext +=  "; expires=" + this.expires.toGMTString()
90       } 
91     } 
92    if (this.accessPath != null) {
93    cookietext += "; PATH="+this.accessPath }
94    document.cookie = cookietext 
95    return null  
96 }
97 function ucRead() {
98   var search = this.name + "="                       
99   var CookieString = document.cookie            
100   if(CookieString == null) CookieString = "";
101   this.rawValue = null
102   this.found = false     
103   if (CookieString.length > 0) {                
104     offset = CookieString.indexOf(search)       
105     if (offset != -1) {                         
106       offset += search.length                   
107       end = CookieString.indexOf(";", offset)   
108       if (end == -1) {  
109        end = CookieString.length }              
110       this.rawValue = CookieString.substring(offset, end)                                   
111       this.found = true 
112       } 
113     }
114 if (this.rawValue != null) { // unpack into fields
115   var sl = this.rawValue.length
116   var startidx = 0
117   var endidx = 0
118   var i = 0
119 if (this.rawValue.substr(sl-1, 1) != this.fieldSeparator) {
120   this.fields[0] = unescape(this.rawValue)
121   } else { 
122   do  
123   {
124    endidx = this.rawValue.indexOf(this.fieldSeparator, startidx)
125    if (endidx !=-1) {
126      this.fields[i] = unescape(this.rawValue.substring(startidx, endidx))
127      i++
128      startidx = endidx + 1}
129   }
130   while (endidx !=-1 & endidx != (this.rawValue.length -1));
131 }
132
133   return this.found
134
135 function ucDelete() {
136   this.expires = -10
137   this.write()
138   return this.read()
139 }