r10212: An IE fix. Must set initial values in the onload function.
[kai/samba.git] / swat / scripting / common.js
1 /*
2         js functions and code common to all pages
3 */
4
5 /* define some global variables for this request */
6 global.page = new Object();
7
8 /* fill in some defaults */
9 global.page.title = "Samba Web Administration Tool";
10
11 libinclude("base.js");
12
13 /* to cope with browsers that don't support cookies we append the sessionid
14    to the URI */
15 global.SESSIONURI = "";
16 if (request['COOKIE_SUPPORT'] != "True") {
17         global.SESSIONURI="?SwatSessionId=" + request['SESSION_ID'];
18 }
19
20 /*
21   possibly adjust a local URI to have the session id appended
22   used for browsers that don't support cookies
23 */
24 function session_uri(uri) {
25         return uri + global.SESSIONURI;
26 }
27
28 /*
29   like printf, but to the web page
30 */
31 function writef()
32 {
33         write(vsprintf(arguments));
34 }
35
36 /*
37   like writef with a <br>
38 */
39 function writefln()
40 {
41         write(vsprintf(arguments));
42         write("<br/>\n");
43 }
44
45
46 /* if the browser was too dumb to set the HOST header, then
47    set it now */
48 if (headers['HOST'] == undefined) {
49         headers['HOST'] = server['SERVER_HOST'] + ":" + server['SERVER_PORT'];
50 }
51
52 /*
53   show the page header. page types include "plain" and "column" 
54 */
55 function page_header(pagetype, title, menu) {
56         global.page.pagetype = pagetype;
57         global.page.title = title;
58         global.page.menu = menu;
59         include("/scripting/header_" + pagetype + ".esp");
60 }
61
62 /*
63   show the page footer, getting the page type from page.pagetype
64   set in page_header()
65 */
66 function page_footer() {
67         include("/scripting/footer_" + global.page.pagetype + ".esp");
68 }
69
70
71 /*
72   check if a uri is one of the 'always allowed' pages, even when not logged in
73   This allows the login page to use the same style sheets and images
74 */
75 function always_allowed(uri) {
76         var str = string_init();
77         var s = str.split('.', uri);
78         if (s.length < 2) {
79                 return false;
80         }
81         var ext = s[s.length-1];
82         var allowed = new Array("ico", "gif", "png","css", "js");
83         for (i in allowed) {
84                 if (allowed[i] == ext) {
85                         return true;
86                 }
87         }
88         return false;
89 }
90
91 /*
92   display a table element
93 */
94 function table_element(i, o) {
95         write("<tr><td>" + i + "</td><td>");
96         if (typeof(o[i]) == "object") {
97                 var j, first;
98                 first = true;
99                 for (j in o[i]) {
100                         if (first == false) {
101                                 write("<br />");
102                         }
103                         write(o[i][j]);
104                         first = false;
105                 }
106         } else {
107                 write(o[i]);
108         }
109         write("</td></tr>\n");
110 }
111
112 /*
113   display a ejs object as a table. The header is optional
114 */
115 function simple_table(v) {
116         if (v.length == 0) {
117                 return;
118         }
119         write("<table class=\"data\">\n");
120         var r;
121         for (r in v) {
122                 table_element(r, v);
123         }
124         write("</table>\n");
125 }
126
127 /*
128   display an array of objects, with the header for each element from the given 
129   attribute
130 */
131 function multi_table(array, header) {
132         var i, n;
133         write("<table class=\"data\">\n");
134         for (i=0;i<array.length;i++) {
135                 var r, v = array[i];
136                 write('<tr><th colspan="2">' + v[header] + "</th></tr>\n");
137                 for (r in v) {
138                         if (r != header) {
139                             table_element(r, v);
140                         }
141                 }
142         }
143         write("</table>\n");
144 }
145