1 // Core javascript helper functions
3 // basic browser identification & version
4 var isOpera = (navigator.userAgent.indexOf("Opera") >= 0) && parseFloat(navigator.appVersion);
5 var isIE = ((document.all) && (!isOpera)) && parseFloat(navigator.appVersion.split("MSIE ")[1].split(";")[0]);
7 // Cross-browser event handlers.
8 function addEvent(obj, evType, fn) {
10 if (obj.addEventListener) {
11 obj.addEventListener(evType, fn, false);
13 } else if (obj.attachEvent) {
14 var r = obj.attachEvent("on" + evType, fn);
21 function removeEvent(obj, evType, fn) {
23 if (obj.removeEventListener) {
24 obj.removeEventListener(evType, fn, false);
26 } else if (obj.detachEvent) {
27 obj.detachEvent("on" + evType, fn);
34 function cancelEventPropagation(e) {
39 e.cancelBubble = true;
40 if (e.stopPropagation) {
45 // quickElement(tagType, parentReference [, textInChildNode, attribute, attributeValue ...]);
46 function quickElement() {
48 var obj = document.createElement(arguments[0]);
50 var textNode = document.createTextNode(arguments[2]);
51 obj.appendChild(textNode);
53 var len = arguments.length;
54 for (var i = 3; i < len; i += 2) {
55 obj.setAttribute(arguments[i], arguments[i + 1]);
57 arguments[1].appendChild(obj);
61 // "a" is reference to an object
62 function removeChildren(a) {
64 while (a.hasChildNodes()) {
65 a.removeChild(a.lastChild);
69 // ----------------------------------------------------------------------------
70 // Find-position functions by PPK
71 // See http://www.quirksmode.org/js/findpos.html
72 // ----------------------------------------------------------------------------
73 function findPosX(obj) {
76 if (obj.offsetParent) {
77 while (obj.offsetParent) {
78 curleft += obj.offsetLeft - ((isOpera) ? 0 : obj.scrollLeft);
79 obj = obj.offsetParent;
81 // IE offsetParent does not include the top-level
82 if (isIE && obj.parentElement) {
83 curleft += obj.offsetLeft - obj.scrollLeft;
91 function findPosY(obj) {
94 if (obj.offsetParent) {
95 while (obj.offsetParent) {
96 curtop += obj.offsetTop - ((isOpera) ? 0 : obj.scrollTop);
97 obj = obj.offsetParent;
99 // IE offsetParent does not include the top-level
100 if (isIE && obj.parentElement) {
101 curtop += obj.offsetTop - obj.scrollTop;
109 //-----------------------------------------------------------------------------
110 // Date object extensions
111 // ----------------------------------------------------------------------------
114 Date.prototype.getTwelveHours = function() {
115 var hours = this.getHours();
120 return hours <= 12 ? hours : hours - 12;
124 Date.prototype.getTwoDigitMonth = function() {
125 return (this.getMonth() < 9) ? '0' + (this.getMonth() + 1) : (this.getMonth() + 1);
128 Date.prototype.getTwoDigitDate = function() {
129 return (this.getDate() < 10) ? '0' + this.getDate() : this.getDate();
132 Date.prototype.getTwoDigitTwelveHour = function() {
133 return (this.getTwelveHours() < 10) ? '0' + this.getTwelveHours() : this.getTwelveHours();
136 Date.prototype.getTwoDigitHour = function() {
137 return (this.getHours() < 10) ? '0' + this.getHours() : this.getHours();
140 Date.prototype.getTwoDigitMinute = function() {
141 return (this.getMinutes() < 10) ? '0' + this.getMinutes() : this.getMinutes();
144 Date.prototype.getTwoDigitSecond = function() {
145 return (this.getSeconds() < 10) ? '0' + this.getSeconds() : this.getSeconds();
148 Date.prototype.getHourMinute = function() {
149 return this.getTwoDigitHour() + ':' + this.getTwoDigitMinute();
152 Date.prototype.getHourMinuteSecond = function() {
153 return this.getTwoDigitHour() + ':' + this.getTwoDigitMinute() + ':' + this.getTwoDigitSecond();
156 Date.prototype.getFullMonthName = function() {
157 return typeof window.CalendarNamespace === "undefined"
158 ? this.getTwoDigitMonth()
159 : window.CalendarNamespace.monthsOfYear[this.getMonth()];
162 Date.prototype.strftime = function(format) {
164 B: this.getFullMonthName(),
166 d: this.getTwoDigitDate(),
167 H: this.getTwoDigitHour(),
168 I: this.getTwoDigitTwelveHour(),
169 m: this.getTwoDigitMonth(),
170 M: this.getTwoDigitMinute(),
171 p: (this.getHours() >= 12) ? 'PM' : 'AM',
172 S: this.getTwoDigitSecond(),
173 w: '0' + this.getDay(),
174 x: this.toLocaleDateString(),
175 X: this.toLocaleTimeString(),
176 y: ('' + this.getFullYear()).substr(2, 4),
177 Y: '' + this.getFullYear(),
180 var result = '', i = 0;
181 while (i < format.length) {
182 if (format.charAt(i) === '%') {
183 result = result + fields[format.charAt(i + 1)];
187 result = result + format.charAt(i);
194 // ----------------------------------------------------------------------------
195 // String object extensions
196 // ----------------------------------------------------------------------------
197 String.prototype.pad_left = function(pad_length, pad_string) {
198 var new_string = this;
199 for (var i = 0; new_string.length < pad_length; i++) {
200 new_string = pad_string + new_string;
205 String.prototype.strptime = function(format) {
206 var split_format = format.split(/[.\-/]/);
207 var date = this.split(/[.\-/]/);
209 var day, month, year;
210 while (i < split_format.length) {
211 switch (split_format[i]) {
227 // Create Date object from UTC since the parsed value is supposed to be
228 // in UTC, not local time. Also, the calendar uses UTC functions for
230 return new Date(Date.UTC(year, month, day));
234 // ----------------------------------------------------------------------------
235 // Get the computed style for and element
236 // ----------------------------------------------------------------------------
237 function getStyle(oElm, strCssRule) {
240 if(document.defaultView && document.defaultView.getComputedStyle) {
241 strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
243 else if(oElm.currentStyle) {
244 strCssRule = strCssRule.replace(/\-(\w)/g, function(strMatch, p1) {
245 return p1.toUpperCase();
247 strValue = oElm.currentStyle[strCssRule];