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 day1 = this.getTime();
86 var day2 = otherDate.getTime();
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();
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));
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)
118 var day = '' + this.getDate();
126 Date.prototype.getHourString = function() {
127 var newString = this.getHours() + '00';
128 if (newString.length == 3)
129 newString = '0' + newString;
134 Date.prototype.getDisplayHoursString = function() {
135 var hoursString = "" + this.getUTCHours();
136 if (hoursString.length == 1)
137 hoursString = '0' + hoursString;
139 var minutesString = "" + this.getUTCMinutes();
140 if (minutesString.length == 1)
141 minutesString = '0' + minutesString;
143 return hoursString + ":" + minutesString;
146 Date.prototype.stringWithSeparator = function(separator) {
147 var month = '' + (this.getMonth() + 1);
148 var day = '' + this.getDate();
149 var year = this.getYear();
151 year = '' + (year + 1900);
152 if (month.length == 1)
157 if (separator == '-')
158 str = year + '-' + month + '-' + day;
160 str = day + '/' + month + '/' + year;
165 Date.prototype.sogoFreeBusyStringWithSeparator = function(separator) {
166 return this.sogoDayName() + ", " + this.stringWithSeparator(separator);
169 Date.prototype.addDays = function(nbrDays) {
170 var milliSeconds = this.getTime();
171 milliSeconds += 86400000 * nbrDays;
172 this.setTime(milliSeconds);
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())
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())
194 Date.prototype.beginOfWeek = function() {
196 var dayNumber = this.getDay();
197 if (weekStartIsMonday) {
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);
216 Date.prototype.endOfWeek = function() {
218 var dayNumber = this.getDay();
219 if (weekStartIsMonday) {
227 var endOfWeek = new Date();
228 endOfWeek.setTime(this.getTime());
229 endOfWeek.addDays(6 + beginNumber - dayNumber);
231 endOfWeek.setHours(23);
232 endOfWeek.setMinutes(59);
233 endOfWeek.setSeconds(59);
234 endOfWeek.setMilliseconds(999);
239 String.prototype._base64_keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
240 String.prototype.base64encode = function () {
242 var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
245 var input = this.utf8encode();
247 while (i < input.length) {
248 chr1 = input.charCodeAt(i++);
249 chr2 = input.charCodeAt(i++);
250 chr3 = input.charCodeAt(i++);
253 enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
254 enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
259 } else if (isNaN(chr3)) {
264 this._base64_keyStr.charAt(enc1) + this._base64_keyStr.charAt(enc2) +
265 this._base64_keyStr.charAt(enc3) + this._base64_keyStr.charAt(enc4);
271 String.prototype.base64decode = function() {
273 var chr1, chr2, chr3;
274 var enc1, enc2, enc3, enc4;
277 var input = this.replace(/[^A-Za-z0-9\+\/\=]/g, "");
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++));
285 chr1 = (enc1 << 2) | (enc2 >> 4);
286 chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
287 chr3 = ((enc3 & 3) << 6) | enc4;
289 output = output + String.fromCharCode(chr1);
292 output = output + String.fromCharCode(chr2);
295 output = output + String.fromCharCode(chr3);
299 return output.utf8decode();
302 String.prototype.utf8encode = function() {
303 var string = this.replace(/\r\n/g,"\n");
306 for (var n = 0; n < this.length; n++) {
307 var c = this.charCodeAt(n);
310 utftext += String.fromCharCode(c);
312 else if((c > 127) && (c < 2048)) {
313 utftext += String.fromCharCode((c >> 6) | 192);
314 utftext += String.fromCharCode((c & 63) | 128);
317 utftext += String.fromCharCode((c >> 12) | 224);
318 utftext += String.fromCharCode(((c >> 6) & 63) | 128);
319 utftext += String.fromCharCode((c & 63) | 128);
326 String.prototype.utf8decode = function() {
331 while (i < string.length) {
332 c = utftext.charCodeAt(i);
335 string += String.fromCharCode(c);
338 else if((c > 191) && (c < 224)) {
339 c2 = this.charCodeAt(i+1);
340 string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
344 c2 = this.charCodeAt(i+1);
345 c3 = this.charCodeAt(i+2);
346 string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
354 String.prototype.cssSafeString = function() {
355 var newString = this.replace("#", "_", "g");
356 newString = newString.replace(".", "_", "g");
357 newString = newString.replace("@", "_", "g");
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;
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;