]> err.no Git - scalable-opengroupware.org/blob - UI/WebServerResources/JavascriptAPIExtensions.js
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1209 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / UI / WebServerResources / JavascriptAPIExtensions.js
1 String.prototype.trim = function() {
2   return this.replace(/(^\s+|\s+$)/g, '');
3 }
4
5 String.prototype.formatted = function() {
6   var newString = this;
7
8   for (var i = 0; i < arguments.length; i++)
9     newString = newString.replace("%{" + i + "}", arguments[i], "g");
10
11   return newString;
12 }
13
14 String.prototype.repeat = function(count) {
15    var newString = "";
16    for (var i = 0; i < count; i++) {
17       newString += this;
18    }
19
20    return newString;
21 }
22
23 String.prototype.capitalize = function() {
24   return this.replace(/\w+/g,
25                       function(a) {
26                         return ( a.charAt(0).toUpperCase()
27                                  + a.substr(1).toLowerCase() );
28                       });
29 }
30
31 String.prototype.decodeEntities = function() {
32   return this.replace(/&#(\d+);/g,
33                       function(wholematch, parenmatch1) {
34                         return String.fromCharCode(+parenmatch1);
35                       });
36 }
37
38 String.prototype.asDate = function () {
39   var newDate;
40   var date = this.split("/");
41   if (date.length == 3)
42     newDate = new Date(date[2], date[1] - 1, date[0]);
43   else {
44     date = this.split("-");
45     if (date.length == 3)
46        newDate = new Date(date[0], date[1] - 1, date[2]);
47     else {
48        if (this.length == 8) {
49           newDate = new Date(this.substring(0, 4),
50                              this.substring(4, 6) - 1,
51                              this.substring(6, 8));
52        }
53     }
54   }
55
56   return newDate;  
57 }
58
59 Date.prototype.sogoDayName = function() {
60   var dayName = "";
61
62   var day = this.getDay();
63   if (day == 0) {
64     dayName = labels['a2_Sunday'];
65   } else if (day == 1) {
66     dayName = labels['a2_Monday'];
67   } else if (day == 2) {
68     dayName = labels['a2_Tuesday'];
69   } else if (day == 3) {
70     dayName = labels['a2_Wednesday'];
71   } else if (day == 4) {
72     dayName = labels['a2_Thursday'];
73   } else if (day == 5) {
74     dayName = labels['a2_Friday'];
75   } else if (day == 6) {
76     dayName = labels['a2_Saturday'];
77   }
78
79   return dayName;
80 }
81
82 Date.prototype.daysUpTo = function(otherDate) {
83   var days = new Array();
84
85   var day1 = this.getTime();
86   var day2 = otherDate.getTime();
87   if (day1 > day2) {
88     var tmp = day1;
89     day1 = day2;
90     day2 = tmp;
91   }
92 //   var day1Date = new Date();
93 //   day1Date.setTime(this.getTime());
94 //   day1Date.setHours(0, 0, 0, 0);
95 //   var day2Date = new Date();
96 //   day2Date.setTime(otherDate.getTime());
97 //   day2Date.setHours(23, 59, 59, 999);
98 //   var day1 = day1Date.getTime();
99 //   var day2 = day2Date.getTime();
100
101   var nbrDays = Math.floor((day2 - day1) / 86400000) + 1;
102   for (var i = 0; i < nbrDays; i++) {
103     var newDate = new Date();
104     newDate.setTime(day1 + (i * 86400000));
105     days.push(newDate);
106   }
107
108   return days;
109 }
110
111 Date.prototype.getDayString = function() {
112    var newString = this.getYear();
113    if (newString < 1000) newString += 1900;
114    var month = '' + (this.getMonth() + 1);
115    if (month.length == 1)
116      month = '0' + month;
117    newString += month;
118    var day = '' + this.getDate();
119    if (day.length == 1)
120      day = '0' + day;
121    newString += day;
122
123    return newString;
124 }
125
126 Date.prototype.getHourString = function() {
127    var newString = this.getHours() + '00';
128    if (newString.length == 3)
129      newString = '0' + newString;
130
131    return newString;
132 }
133
134 Date.prototype.getDisplayHoursString = function() {
135    var hoursString = "" + this.getUTCHours();
136    if (hoursString.length == 1)
137      hoursString = '0' + hoursString;
138
139    var minutesString = "" + this.getUTCMinutes();
140    if (minutesString.length == 1)
141      minutesString = '0' + minutesString;
142
143    return hoursString + ":" + minutesString;
144 }
145
146 Date.prototype.stringWithSeparator = function(separator) {
147   var month = '' + (this.getMonth() + 1);
148   var day = '' + this.getDate();
149   var year = this.getYear();
150   if (year < 1000)
151     year = '' + (year + 1900);
152   if (month.length == 1)
153     month = '0' + month;
154   if (day.length == 1)
155     day = '0' + day;
156
157   if (separator == '-')
158     str = year + '-' + month + '-' + day;
159   else
160     str = day + '/' + month + '/' + year;
161
162   return str;
163 }
164
165 Date.prototype.sogoFreeBusyStringWithSeparator = function(separator) {
166   return this.sogoDayName() + ", " + this.stringWithSeparator(separator);
167 }
168
169 Date.prototype.addDays = function(nbrDays) {
170    var milliSeconds = this.getTime();
171    milliSeconds += 86400000 * nbrDays;
172    this.setTime(milliSeconds);
173 }
174
175 Date.prototype.earlierDate = function(otherDate) {
176    var workDate = new Date();
177    workDate.setTime(otherDate.getTime());
178    workDate.setHours(0);
179    return ((this.getTime() < workDate.getTime())
180            ? this : otherDate);
181 }
182
183 Date.prototype.laterDate = function(otherDate) {
184    var workDate = new Date();
185    workDate.setTime(otherDate.getTime());
186    workDate.setHours(23);
187    workDate.setMinutes(59);
188    workDate.setSeconds(59);
189    workDate.setMilliseconds(999);
190    return ((this.getTime() < workDate.getTime())
191            ? otherDate : this);
192 }
193
194 Date.prototype.beginOfWeek = function() {
195    var beginNumber;
196    var dayNumber = this.getDay();
197    if (weekStartIsMonday) {
198      beginNumber = 1;
199      if (dayNumber == 0)
200         dayNumber = 7;
201    }
202    else
203      beginNumber = 0;
204
205    var beginOfWeek = new Date();
206    beginOfWeek.setTime(this.getTime());
207    beginOfWeek.addDays(beginNumber - dayNumber);
208    beginOfWeek.setHours(0);
209    beginOfWeek.setMinutes(0);
210    beginOfWeek.setSeconds(0);
211    beginOfWeek.setMilliseconds(0);
212
213    return beginOfWeek;
214 }
215
216 Date.prototype.endOfWeek = function() {
217    var beginNumber;
218    var dayNumber = this.getDay();
219    if (weekStartIsMonday) {
220       beginNumber = 1;
221       if (dayNumber == 0)
222          dayNumber = 7;
223    }
224    else
225       beginNumber = 0;
226
227    var endOfWeek = new Date();
228    endOfWeek.setTime(this.getTime());
229    endOfWeek.addDays(6 + beginNumber - dayNumber);
230
231    endOfWeek.setHours(23);
232    endOfWeek.setMinutes(59);
233    endOfWeek.setSeconds(59);
234    endOfWeek.setMilliseconds(999);
235
236    return endOfWeek;
237 }
238
239 String.prototype._base64_keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
240 String.prototype.base64encode = function () {
241   var output = "";
242   var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
243   var i = 0;
244  
245   var input = this.utf8encode();
246
247   while (i < input.length) {
248     chr1 = input.charCodeAt(i++);
249     chr2 = input.charCodeAt(i++);
250     chr3 = input.charCodeAt(i++);
251  
252     enc1 = chr1 >> 2;
253     enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
254     enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
255     enc4 = chr3 & 63;
256  
257     if (isNaN(chr2)) {
258       enc3 = enc4 = 64;
259     } else if (isNaN(chr3)) {
260       enc4 = 64;
261     }
262  
263     output = output +
264       this._base64_keyStr.charAt(enc1) + this._base64_keyStr.charAt(enc2) +
265       this._base64_keyStr.charAt(enc3) + this._base64_keyStr.charAt(enc4);
266   }
267  
268   return output;
269 };
270
271 String.prototype.base64decode = function() { 
272   var output = "";
273   var chr1, chr2, chr3;
274   var enc1, enc2, enc3, enc4;
275   var i = 0;
276  
277   var input = this.replace(/[^A-Za-z0-9\+\/\=]/g, "");
278
279   while (i < input.length) {
280     enc1 = this._base64_keyStr.indexOf(input.charAt(i++));
281     enc2 = this._base64_keyStr.indexOf(input.charAt(i++));
282     enc3 = this._base64_keyStr.indexOf(input.charAt(i++));
283     enc4 = this._base64_keyStr.indexOf(input.charAt(i++));
284
285     chr1 = (enc1 << 2) | (enc2 >> 4);
286     chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
287     chr3 = ((enc3 & 3) << 6) | enc4;
288  
289     output = output + String.fromCharCode(chr1);
290  
291     if (enc3 != 64) {
292       output = output + String.fromCharCode(chr2);
293     }
294     if (enc4 != 64) {
295       output = output + String.fromCharCode(chr3);
296     }
297   }
298
299   return output.utf8decode();
300 };
301
302 String.prototype.utf8encode = function() {
303   var string = this.replace(/\r\n/g,"\n");
304   var utftext = "";
305  
306   for (var n = 0; n < this.length; n++) {
307     var c = this.charCodeAt(n);
308
309     if (c < 128) {
310       utftext += String.fromCharCode(c);
311     }
312     else if((c > 127) && (c < 2048)) {
313       utftext += String.fromCharCode((c >> 6) | 192);
314       utftext += String.fromCharCode((c & 63) | 128);
315     }
316     else {
317       utftext += String.fromCharCode((c >> 12) | 224);
318       utftext += String.fromCharCode(((c >> 6) & 63) | 128);
319       utftext += String.fromCharCode((c & 63) | 128);
320     }
321   }
322  
323   return utftext;
324 };
325
326 String.prototype.utf8decode = function() {
327   var string = "";
328   var i = 0;
329   var c = c1 = c2 = 0;
330
331   while (i < string.length) {
332     c = utftext.charCodeAt(i);
333  
334     if (c < 128) {
335       string += String.fromCharCode(c);
336       i++;
337     }
338     else if((c > 191) && (c < 224)) {
339       c2 = this.charCodeAt(i+1);
340       string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
341       i += 2;
342     }
343     else {
344       c2 = this.charCodeAt(i+1);
345       c3 = this.charCodeAt(i+2);
346       string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
347       i += 3;
348     }
349   }
350  
351   return string;
352 };
353
354 String.prototype.cssSafeString = function() {
355   var newString = this.replace("#", "_", "g");
356   newString = newString.replace(".", "_", "g");
357   newString = newString.replace("@", "_", "g");
358
359   return newString;
360 }
361
362 window.width = function() {
363   if (window.innerWidth)
364     return window.innerWidth;
365   else if (document.body && document.body.offsetWidth)
366     return document.body.offsetWidth;
367   else
368     return 0;
369 }
370
371 window.height = function() {
372   if (window.innerHeight)
373     return window.innerHeight;
374   else if (document.body && document.body.offsetHeight)
375     return document.body.offsetHeight;
376   else
377     return 0;
378 }