1 String.prototype.trim = function() {
2 return this.replace(/(^\s+|\s+$)/g, '');
5 String.prototype.formatted = function() {
8 for (var i = 0; i < arguments.length; i++)
9 newString = newString.replace("%{" + i + "}", arguments[i], "g");
14 String.prototype.repeat = function(count) {
16 for (var i = 0; i < count; i++) {
23 String.prototype.capitalize = function() {
24 return this.replace(/\w+/g,
26 return ( a.charAt(0).toUpperCase()
27 + a.substr(1).toLowerCase() );
31 String.prototype.decodeEntities = function() {
32 return this.replace(/&#(\d+);/g,
33 function(wholematch, parenmatch1) {
34 return String.fromCharCode(+parenmatch1);
38 String.prototype.asDate = function () {
40 var date = this.split("/");
42 newDate = new Date(date[2], date[1] - 1, date[0]);
44 date = this.split("-");
46 newDate = new Date(date[0], date[1] - 1, date[2]);
48 if (this.length == 8) {
49 newDate = new Date(this.substring(0, 4),
50 this.substring(4, 6) - 1,
51 this.substring(6, 8));
59 Date.prototype.sogoDayName = function() {
62 var day = this.getDay();
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'];
82 Date.prototype.daysUpTo = function(otherDate) {
83 var days = new Array();
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();
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));
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)
111 var day = '' + this.getDate();
119 Date.prototype.getHourString = function() {
120 var newString = this.getHours() + '00';
121 if (newString.length == 3)
122 newString = '0' + newString;
127 Date.prototype.getDisplayHoursString = function() {
128 var hoursString = "" + this.getUTCHours();
129 if (hoursString.length == 1)
130 hoursString = '0' + hoursString;
132 var minutesString = "" + this.getUTCMinutes();
133 if (minutesString.length == 1)
134 minutesString = '0' + minutesString;
136 return hoursString + ":" + minutesString;
139 Date.prototype.stringWithSeparator = function(separator) {
140 var month = '' + (this.getMonth() + 1);
141 var day = '' + this.getDate();
142 var year = this.getYear();
144 year = '' + (year + 1900);
145 if (month.length == 1)
150 if (separator == '-')
151 str = year + '-' + month + '-' + day;
153 str = day + '/' + month + '/' + year;
158 Date.prototype.sogoFreeBusyStringWithSeparator = function(separator) {
159 return this.sogoDayName() + ", " + this.stringWithSeparator(separator);
162 Date.prototype.addDays = function(nbrDays) {
163 var milliSeconds = this.getTime();
164 milliSeconds += 86400000 * nbrDays;
165 this.setTime(milliSeconds);
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())
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())
187 Date.prototype.beginOfWeek = function() {
189 var dayNumber = this.getDay();
190 if (weekStartIsMonday) {
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);
209 Date.prototype.endOfWeek = function() {
211 var dayNumber = this.getDay();
212 if (weekStartIsMonday) {
220 var endOfWeek = new Date();
221 endOfWeek.setTime(this.getTime());
222 endOfWeek.addDays(6 + beginNumber - dayNumber);
224 endOfWeek.setHours(23);
225 endOfWeek.setMinutes(59);
226 endOfWeek.setSeconds(59);
227 endOfWeek.setMilliseconds(999);
232 String.prototype._base64_keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
233 String.prototype.base64encode = function () {
235 var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
238 var input = this.utf8encode();
240 while (i < input.length) {
241 chr1 = input.charCodeAt(i++);
242 chr2 = input.charCodeAt(i++);
243 chr3 = input.charCodeAt(i++);
246 enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
247 enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
252 } else if (isNaN(chr3)) {
257 this._base64_keyStr.charAt(enc1) + this._base64_keyStr.charAt(enc2) +
258 this._base64_keyStr.charAt(enc3) + this._base64_keyStr.charAt(enc4);
264 String.prototype.base64decode = function() {
266 var chr1, chr2, chr3;
267 var enc1, enc2, enc3, enc4;
270 var input = this.replace(/[^A-Za-z0-9\+\/\=]/g, "");
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++));
278 chr1 = (enc1 << 2) | (enc2 >> 4);
279 chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
280 chr3 = ((enc3 & 3) << 6) | enc4;
282 output = output + String.fromCharCode(chr1);
285 output = output + String.fromCharCode(chr2);
288 output = output + String.fromCharCode(chr3);
292 return output.utf8decode();
295 String.prototype.utf8encode = function() {
296 var string = this.replace(/\r\n/g,"\n");
299 for (var n = 0; n < this.length; n++) {
300 var c = this.charCodeAt(n);
303 utftext += String.fromCharCode(c);
305 else if((c > 127) && (c < 2048)) {
306 utftext += String.fromCharCode((c >> 6) | 192);
307 utftext += String.fromCharCode((c & 63) | 128);
310 utftext += String.fromCharCode((c >> 12) | 224);
311 utftext += String.fromCharCode(((c >> 6) & 63) | 128);
312 utftext += String.fromCharCode((c & 63) | 128);
319 String.prototype.utf8decode = function() {
324 while (i < string.length) {
325 c = utftext.charCodeAt(i);
328 string += String.fromCharCode(c);
331 else if((c > 191) && (c < 224)) {
332 c2 = this.charCodeAt(i+1);
333 string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
337 c2 = this.charCodeAt(i+1);
338 c3 = this.charCodeAt(i+2);
339 string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
347 String.prototype.cssSafeString = function() {
348 var newString = this.replace("#", "_", "g");
349 newString = newString.replace(".", "_", "g");
350 newString = newString.replace("@", "_", "g");