456ed2ee20cba43d0c2f89242cc8899eb4f5c6c7
[Evergreen.git] / Open-ILS / web / opac / common / js / CGI.js
1 function CGI() {
2         /* load up the url parameters */
3
4         this._keys = new Array();
5         this.data = new Object();
6
7         var string = location.search.replace(/^\?/,"");
8
9         var key = ""; 
10         var value = "";
11         var inkey = true;
12         var invalue = false;
13
14         for( var idx = 0; idx!= string.length; idx++ ) {
15
16                 var c = string.charAt(idx);
17
18                 if( c == "=" )  {
19                         invalue = true;
20                         inkey = false;
21                         continue;
22                 } 
23
24                 if(c == "&" || c == ";") {
25                         inkey = 1;
26                         invalue = 0;
27                         if( ! this.data[key] ) this.data[key] = [];
28                         this.data[key].push(decodeURIComponent(value));
29                         this._keys.push(key);
30                         key = ""; value = "";
31                         continue;
32                 }
33
34                 if(inkey) key += c;
35                 else if(invalue) value += c;
36         }
37
38         if( ! this.data[key] ) this.data[key] = [];
39         this.data[key].push(decodeURIComponent(value));
40         this._keys.push(key);
41 }
42
43 /* returns the value for the given param.  If there is only one value for the
44    given param, it returns that value.  Otherwise it returns an array of values
45  */
46 CGI.prototype.param = function(p) {
47         if(this.data[p] == null) return null;
48         if(this.data[p].length == 1)
49                 return this.data[p][0];
50         return this.data[p];
51 }
52
53 /* returns an array of param names */
54 CGI.prototype.keys = function() {
55         return this._keys;
56 }
57
58 /* debuggin method */
59 CGI.prototype.toString = function() {
60         var string = "";
61         var keys = this.keys();
62
63         for( var k in keys ) {
64                 string += keys[k] + " : ";
65                 var params = this.param(keys[k]);
66
67                 for( var p in params ) {
68                         string +=  params[p] + " ";
69                 }
70                 string += "\n";
71         }
72         return string;
73 }
74
75