]> err.no Git - scalable-opengroupware.org/blob - UI/WebServerResources/JavascriptAPIExtensions.js
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1166 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 day1Date = new Date();
86   day1Date.setTime(this.getTime());
87   day1Date.setHours(0, 0, 0, 0);
88   var day2Date = new Date();
89   day2Date.setTime(otherDate.getTime());
90   day2Date.setHours(23, 59, 59, 999);
91   var day1 = day1Date.getTime();
92   var day2 = day2Date.getTime();
93
94   var nbrDays = Math.floor((day2 - day1) / 86400000) + 1;
95   for (var i = 0; i < nbrDays; i++) {
96     var newDate = new Date();
97     newDate.setTime(day1 + (i * 86400000));
98     days.push(newDate);
99   }
100
101   return days;
102 }
103
104 Date.prototype.getDayString = function() {
105    var newString = this.getYear();
106    if (newString < 1000) newString += 1900;
107    var month = '' + (this.getMonth() + 1);
108    if (month.length == 1)
109      month = '0' + month;
110    newString += month;
111    var day = '' + this.getDate();
112    if (day.length == 1)
113      day = '0' + day;
114    newString += day;
115
116    return newString;
117 }
118
119 Date.prototype.getHourString = function() {
120    var newString = this.getHours() + '00';
121    if (newString.length == 3)
122      newString = '0' + newString;
123
124    return newString;
125 }
126
127 Date.prototype.getDisplayHoursString = function() {
128    var hoursString = "" + this.getHours();
129    if (hoursString.length == 1)
130      hoursString = '0' + hoursString;
131
132    var minutesString = "" + this.getMinutes();
133    if (minutesString.length == 1)
134      minutesString = '0' + minutesString;
135
136    return hoursString + ":" + minutesString;
137 }
138
139 Date.prototype.stringWithSeparator = function(separator) {
140   var month = '' + (this.getMonth() + 1);
141   var day = '' + this.getDate();
142   var year = this.getYear();
143   if (year < 1000)
144     year = '' + (year + 1900);
145   if (month.length == 1)
146     month = '0' + month;
147   if (day.length == 1)
148     day = '0' + day;
149
150   if (separator == '-')
151     str = year + '-' + month + '-' + day;
152   else
153     str = day + '/' + month + '/' + year;
154
155   return str;
156 }
157
158 Date.prototype.sogoFreeBusyStringWithSeparator = function(separator) {
159   return this.sogoDayName() + ", " + this.stringWithSeparator(separator);
160 }
161
162 Date.prototype.addDays = function(nbrDays) {
163    var milliSeconds = this.getTime();
164    milliSeconds += 86400000 * nbrDays;
165    this.setTime(milliSeconds);
166 }
167
168 Date.prototype.earlierDate = function(otherDate) {
169    var workDate = new Date();
170    workDate.setTime(otherDate.getTime());
171    workDate.setHours(0);
172    return ((this.getTime() < workDate.getTime())
173            ? this : otherDate);
174 }
175
176 Date.prototype.laterDate = function(otherDate) {
177    var workDate = new Date();
178    workDate.setTime(otherDate.getTime());
179    workDate.setHours(23);
180    workDate.setMinutes(59);
181    workDate.setSeconds(59);
182    workDate.setMilliseconds(999);
183    return ((this.getTime() < workDate.getTime())
184            ? otherDate : this);
185 }
186
187 Date.prototype.beginOfWeek = function() {
188    var beginNumber;
189    var dayNumber = this.getDay();
190    if (weekStartIsMonday) {
191      beginNumber = 1;
192      if (dayNumber == 0)
193         dayNumber = 7;
194    }
195    else
196      beginNumber = 0;
197
198    var beginOfWeek = new Date();
199    beginOfWeek.setTime(this.getTime());
200    beginOfWeek.addDays(beginNumber - dayNumber);
201    beginOfWeek.setHours(0);
202    beginOfWeek.setMinutes(0);
203    beginOfWeek.setSeconds(0);
204    beginOfWeek.setMilliseconds(0);
205
206    return beginOfWeek;
207 }
208
209 Date.prototype.endOfWeek = function() {
210    var beginNumber;
211    var dayNumber = this.getDay();
212    if (weekStartIsMonday) {
213       beginNumber = 1;
214       if (dayNumber == 0)
215          dayNumber = 7;
216    }
217    else
218       beginNumber = 0;
219
220    var endOfWeek = new Date();
221    endOfWeek.setTime(this.getTime());
222    endOfWeek.addDays(6 + beginNumber - dayNumber);
223
224    endOfWeek.setHours(23);
225    endOfWeek.setMinutes(59);
226    endOfWeek.setSeconds(59);
227    endOfWeek.setMilliseconds(999);
228
229    return endOfWeek;
230 }
231
232 String.prototype._base64_keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
233 String.prototype.base64encode = function () {
234   var output = "";
235   var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
236   var i = 0;
237  
238   var input = this._base64_utf8_encode();
239
240   while (i < input.length) {
241     chr1 = input.charCodeAt(i++);
242     chr2 = input.charCodeAt(i++);
243     chr3 = input.charCodeAt(i++);
244  
245     enc1 = chr1 >> 2;
246     enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
247     enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
248     enc4 = chr3 & 63;
249  
250     if (isNaN(chr2)) {
251       enc3 = enc4 = 64;
252     } else if (isNaN(chr3)) {
253       enc4 = 64;
254     }
255  
256     output = output +
257       this._base64_keyStr.charAt(enc1) + this._base64_keyStr.charAt(enc2) +
258       this._base64_keyStr.charAt(enc3) + this._base64_keyStr.charAt(enc4);
259   }
260  
261   return output;
262 };
263
264 String.prototype.base64decode = function() { 
265   var output = "";
266   var chr1, chr2, chr3;
267   var enc1, enc2, enc3, enc4;
268   var i = 0;
269  
270   var input = this.replace(/[^A-Za-z0-9\+\/\=]/g, "");
271
272   while (i < input.length) {
273     enc1 = this._base64_keyStr.indexOf(input.charAt(i++));
274     enc2 = this._base64_keyStr.indexOf(input.charAt(i++));
275     enc3 = this._base64_keyStr.indexOf(input.charAt(i++));
276     enc4 = this._base64_keyStr.indexOf(input.charAt(i++));
277
278     chr1 = (enc1 << 2) | (enc2 >> 4);
279     chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
280     chr3 = ((enc3 & 3) << 6) | enc4;
281  
282     output = output + String.fromCharCode(chr1);
283  
284     if (enc3 != 64) {
285       output = output + String.fromCharCode(chr2);
286     }
287     if (enc4 != 64) {
288       output = output + String.fromCharCode(chr3);
289     }
290   }
291
292   return output._base64_utf8_decode();
293 };
294
295 String.prototype._base64_utf8_encode = function() {
296   var string = this.replace(/\r\n/g,"\n");
297   var utftext = "";
298  
299   for (var n = 0; n < this.length; n++) {
300     var c = this.charCodeAt(n);
301
302     if (c < 128) {
303       utftext += String.fromCharCode(c);
304     }
305     else if((c > 127) && (c < 2048)) {
306       utftext += String.fromCharCode((c >> 6) | 192);
307       utftext += String.fromCharCode((c & 63) | 128);
308     }
309     else {
310       utftext += String.fromCharCode((c >> 12) | 224);
311       utftext += String.fromCharCode(((c >> 6) & 63) | 128);
312       utftext += String.fromCharCode((c & 63) | 128);
313     }
314   }
315  
316   return utftext;
317 };
318
319 String.prototype._base64_utf8_decode = function() {
320   var string = "";
321   var i = 0;
322   var c = c1 = c2 = 0;
323
324   while (i < string.length) {
325     c = utftext.charCodeAt(i);
326  
327     if (c < 128) {
328       string += String.fromCharCode(c);
329       i++;
330     }
331     else if((c > 191) && (c < 224)) {
332       c2 = this.charCodeAt(i+1);
333       string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
334       i += 2;
335     }
336     else {
337       c2 = this.charCodeAt(i+1);
338       c3 = this.charCodeAt(i+2);
339       string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
340       i += 3;
341     }
342   }
343  
344   return string;
345 };