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