ctx_old attribute removed (we will use ctx_old_len only), url_complete js function...
[xynt.git] / web / xynt / xynt-commons.js
1 var cookiepath = "/xynt/";
2
3 function $(id) { return document.getElementById(id); }
4
5 function getStyle(x,IEstyleProp, MozStyleProp) 
6 {
7     if (x.currentStyle) {
8         var y = x.currentStyle[IEstyleProp];
9     } else if (window.getComputedStyle) {
10         var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(MozStyleProp);
11     }
12     return y;
13 }
14
15   /* replacement of setInterval on IE */
16 (function(){
17     /*if not IE, do nothing*/
18     if(!document.uniqueID){return;};
19
20     /*Copy the default setInterval behavior*/
21     var nativeSetInterval = window.setInterval;
22     window.setInterval = function(fn,ms) {              
23         var param = [];
24         if(arguments.length <= 2)       {
25             return nativeSetInterval(fn,ms);
26         }
27         else {
28             for(var i=2;i<arguments.length;i+=1) {
29                 param[i-2] =  arguments[i];
30             }   
31         }
32         
33         if(typeof(fn)=='function') {
34             
35             return (function (fn,ms,param) {
36                 var fo = function () {                                                          
37                     fn.apply(window,param);
38                 };                      
39                 return nativeSetInterval(fo,ms); 
40             })(fn,ms,param);
41         }
42         else if(typeof(fn)=='string')
43         {
44             return  nativeSetInterval(fn,ms);
45         }
46         else
47         {
48             throw Error('setInterval Error\nInvalid function type');
49         };
50     };
51
52     /*Copy the default setTimeout behavior*/
53     var nativeSetTimeout = window.setTimeout;
54     window.setTimeout = function(fn,ms) {               
55         var param = [];
56         if(arguments.length <= 2)       {
57             return nativeSetTimeout(fn,ms);
58         }
59         else {
60             for(var i=2;i<arguments.length;i+=1) {
61                 param[i-2] =  arguments[i];
62             }   
63         }
64         
65         if(typeof(fn)=='function') {
66             
67             return (function (fn,ms,param) {
68                 var fo = function () {                                                          
69                     fn.apply(window,param);
70                 };                      
71                 return nativeSetTimeout(fo,ms); 
72             })(fn,ms,param);
73         }
74         else if(typeof(fn)=='string')
75         {
76             return  nativeSetTimeout(fn,ms);
77         }
78         else
79         {
80             throw Error('setTimeout Error\nInvalid function type');
81         };
82     };
83
84 })()
85
86 function getStyle(x,IEstyleProp, MozStyleProp) 
87 {
88     if (x.currentStyle) {
89         var y = x.currentStyle[IEstyleProp];
90     } else if (window.getComputedStyle) {
91         var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(MozStyleProp);
92     }
93     return y;
94 }
95
96
97
98 function doSomething(e) {
99         if (!e) var e = window.event
100         // handle event
101         e.cancelBubble = true;
102         if (e.stopPropagation) e.stopPropagation();
103 }
104
105 /*
106  *  GESTIONE DEI COOKIES
107  */
108 function createCookie(name,value,hours,path) {
109         if (hours) {
110                 var date = new Date();
111                 date.setTime(date.getTime()+(hours*60*60*1000));
112                 var expires = "; expires="+date.toGMTString();
113         }
114         else var expires = "";
115         document.cookie = name+"="+value+expires+"; path="+path;
116 }
117
118 function readCookie(name) {
119         var nameEQ = name + "=";
120         var ca = document.cookie.split(';');
121         for(var i=0;i < ca.length;i++) {
122                 var c = ca[i];
123                 while (c.charAt(0)==' ') c = c.substring(1,c.length);
124                 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
125         }
126         return null;
127 }
128
129 function eraseCookie(name) {
130         createCookie(name,"",-1);
131 }
132
133 function url_append_arg(url, name, value)
134 {
135     var pos, sep, pref, rest;
136
137     if ((pos = url.indexOf('?'+name+'=')) == -1) {
138         pos = url.indexOf('&'+name+'=');
139     }
140     if (pos == -1) {
141         if ((pos = url.indexOf('?')) != -1) 
142             sep = '&';
143         else
144             sep = '?';
145         
146         return (url+sep+name+"="+encodeURIComponent(value));
147     }
148     else {
149         pref = url.substring(0, pos+1);
150         rest = url.substring(pos+1);
151         // alert("rest: "+rest+"  pos: "+pos);
152         if ((pos = rest.indexOf('&')) != -1) {
153             rest = rest.substring(pos);
154         }
155         else {
156             rest = "";
157         }
158         return (pref+name+"="+encodeURIComponent(value)+rest);
159     }
160 }
161
162 function url_append_args(url)
163 {
164     var i, ret;
165
166     ret = url;
167     for (i = 1 ; i < arguments.length-1 ; i+= 2) {
168         ret = url_append_arg(ret, arguments[i], arguments[i+1]);
169     }
170
171     return (ret);
172 }
173
174 function url_complete(parent, url)
175 {
176     var p, p2, rest;
177     var host = "", path = "";
178
179     // host extraction
180     p = parent.indexOf("://");
181     if (p > -1) {
182         rest = parent.substring(p+3);
183         p2 = rest.indexOf("/");
184         if (p2 > -1) {
185             host = parent.substring(0, p+3+p2);
186             rest = parent.substring(p+3+p2);
187         }
188         else {
189             host = rest;
190             rest = "";
191         }
192     }
193     else {
194         rest = parent;
195     }
196
197     // path extraction
198     p = rest.lastIndexOf("/");
199     if (p > -1) {
200         path = rest.substring(0, p+1);
201     }
202
203     // alert("host: ["+host+"]  path: ["+path+"]");
204     if (url.substring(0,6) == 'http:/' || url.substring(0,7) == 'https:/') {
205         return (url);
206     }
207     else if (url.substring(0,1) == '/') {
208         return (host+url);
209     }
210     else {
211         return (host+path+url);
212     }
213 }