917aedf50f5ab57a09ce94c22f4ab35a1fd0cbbb
[platform/upstream/libwebsockets.git] / test-server / lws-common.js
1 /*
2  * This section around grayOut came from here:
3  * http://www.codingforums.com/archive/index.php/t-151720.html
4  * Assumed public domain
5  *
6  * Init like this in your main html script, this also reapplies the gray
7  *
8  *    lws_gray_out(true,{'zindex':'499'});
9  *
10  * To remove the gray
11  *
12  *    lws_gray_out(false);
13  *
14  */
15
16 function lws_gray_out(vis, options) {
17         var options = options || {};
18         var zindex = options.zindex || 50;
19         var opacity = options.opacity || 70;
20         var opaque = (opacity / 100);
21         var bgcolor = options.bgcolor || '#000000';
22         var dark = document.getElementById('darkenScreenObject');
23
24         if (!dark) {
25                 var tbody = document.getElementsByTagName("body")[0];
26                 var tnode = document.createElement('div');
27                 tnode.style.position = 'absolute';
28                 tnode.style.top = '0px';
29                 tnode.style.left = '0px';
30                 tnode.style.overflow = 'hidden';
31                 tnode.style.display ='none';
32                 tnode.id = 'darkenScreenObject';
33                 tbody.appendChild(tnode);
34                 dark = document.getElementById('darkenScreenObject');
35         }
36         if (vis) {
37                 dark.style.opacity = opaque;
38                 dark.style.MozOpacity = opaque;
39                 dark.style.filter ='alpha(opacity='+opacity+')';
40                 dark.style.zIndex = zindex;
41                 dark.style.backgroundColor = bgcolor;
42                 dark.style.width = gsize(1);
43                 dark.style.height = gsize(0);
44                 dark.style.display ='block';
45                 addEvent(window, "resize",
46                         function() {
47                                 dark.style.height = gsize(0);
48                                 dark.style.width = gsize(1);
49                         }
50                 );
51         } else {
52                 dark.style.display = 'none';
53                 removeEvent(window, "resize",
54                         function() {
55                                 dark.style.height = gsize(0);
56                                 dark.style.width = gsize(1);
57                         }
58                 );
59         }
60 }
61
62 function gsize(ptype)
63 {
64         var h = document.compatMode == 'CSS1Compat' &&
65                 !window.opera ?
66                         document.documentElement.clientHeight :
67                                                 document.body.clientHeight;
68         var w = document.compatMode == 'CSS1Compat' &&
69                 !window.opera ? 
70                         document.documentElement.clientWidth :
71                                                 document.body.clientWidth;
72         if (document.body && 
73                     (document.body.scrollWidth || document.body.scrollHeight)) {
74                 var pageWidth = (w > (t = document.body.scrollWidth)) ?
75                                         ("" + w + "px") : ("" + (t) + "px");
76                 var pageHeight = (h > (t = document.body.scrollHeight)) ?
77                                         ("" + h + "px") : ("" + (t) + "px");
78         } else if (document.body.offsetWidth) {
79                 var pageWidth = (w > (t = document.body.offsetWidth)) ?
80                                         ("" + w + "px") : ("" + (t) + "px");
81                 var pageHeight =(h > (t = document.body.offsetHeight)) ?
82                                         ("" + h + "px") : ("" + (t) + "px");
83         } else {
84                 var pageWidth = '100%';
85                 var pageHeight = '100%';
86         }
87         return (ptype == 1) ? pageWidth : pageHeight;
88 }
89
90 function addEvent( obj, type, fn ) {
91         if ( obj.attachEvent ) {
92                 obj['e' + type + fn] = fn;
93                 obj[type+fn] = function() { obj['e' + type+fn]( window.event );}
94                 obj.attachEvent('on' + type, obj[type + fn]);
95         } else
96                 obj.addEventListener(type, fn, false);
97 }
98
99 function removeEvent( obj, type, fn ) {
100         if ( obj.detachEvent ) {
101                 obj.detachEvent('on' + type, obj[type + fn]);
102                 obj[type + fn] = null;
103         } else
104                 obj.removeEventListener(type, fn, false);
105 }
106
107 /*
108  * end of grayOut related stuff
109  */
110  
111  
112 function lws_san(s)
113 {
114         if (s.search("<") != -1)
115                 return "invalid string";
116         
117         return s;
118 }