|
1 // Core javascript helper functions |
|
2 |
|
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]); |
|
6 |
|
7 // Cross-browser event handlers. |
|
8 function addEvent(obj, evType, fn) { |
|
9 if (obj.addEventListener) { |
|
10 obj.addEventListener(evType, fn, false); |
|
11 return true; |
|
12 } else if (obj.attachEvent) { |
|
13 var r = obj.attachEvent("on" + evType, fn); |
|
14 return r; |
|
15 } else { |
|
16 return false; |
|
17 } |
|
18 } |
|
19 |
|
20 function removeEvent(obj, evType, fn) { |
|
21 if (obj.removeEventListener) { |
|
22 obj.removeEventListener(evType, fn, false); |
|
23 return true; |
|
24 } else if (obj.detachEvent) { |
|
25 obj.detachEvent("on" + evType, fn); |
|
26 return true; |
|
27 } else { |
|
28 return false; |
|
29 } |
|
30 } |
|
31 |
|
32 // quickElement(tagType, parentReference, textInChildNode, [, attribute, attributeValue ...]); |
|
33 function quickElement() { |
|
34 var obj = document.createElement(arguments[0]); |
|
35 if (arguments[2] != '' && arguments[2] != null) { |
|
36 var textNode = document.createTextNode(arguments[2]); |
|
37 obj.appendChild(textNode); |
|
38 } |
|
39 var len = arguments.length; |
|
40 for (var i = 3; i < len; i += 2) { |
|
41 obj.setAttribute(arguments[i], arguments[i+1]); |
|
42 } |
|
43 arguments[1].appendChild(obj); |
|
44 return obj; |
|
45 } |
|
46 |
|
47 // ---------------------------------------------------------------------------- |
|
48 // Cross-browser xmlhttp object |
|
49 // from http://jibbering.com/2002/4/httprequest.html |
|
50 // ---------------------------------------------------------------------------- |
|
51 var xmlhttp; |
|
52 /*@cc_on @*/ |
|
53 /*@if (@_jscript_version >= 5) |
|
54 try { |
|
55 xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); |
|
56 } catch (e) { |
|
57 try { |
|
58 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); |
|
59 } catch (E) { |
|
60 xmlhttp = false; |
|
61 } |
|
62 } |
|
63 @else |
|
64 xmlhttp = false; |
|
65 @end @*/ |
|
66 if (!xmlhttp && typeof XMLHttpRequest != 'undefined') { |
|
67 xmlhttp = new XMLHttpRequest(); |
|
68 } |
|
69 |
|
70 // ---------------------------------------------------------------------------- |
|
71 // Find-position functions by PPK |
|
72 // See http://www.quirksmode.org/js/findpos.html |
|
73 // ---------------------------------------------------------------------------- |
|
74 function findPosX(obj) { |
|
75 var curleft = 0; |
|
76 if (obj.offsetParent) { |
|
77 while (obj.offsetParent) { |
|
78 curleft += obj.offsetLeft - ((isOpera) ? 0 : obj.scrollLeft); |
|
79 obj = obj.offsetParent; |
|
80 } |
|
81 // IE offsetParent does not include the top-level |
|
82 if (isIE && obj.parentElement){ |
|
83 curleft += obj.offsetLeft - obj.scrollLeft; |
|
84 } |
|
85 } else if (obj.x) { |
|
86 curleft += obj.x; |
|
87 } |
|
88 return curleft; |
|
89 } |
|
90 |
|
91 function findPosY(obj) { |
|
92 var curtop = 0; |
|
93 if (obj.offsetParent) { |
|
94 while (obj.offsetParent) { |
|
95 curtop += obj.offsetTop - ((isOpera) ? 0 : obj.scrollTop); |
|
96 obj = obj.offsetParent; |
|
97 } |
|
98 // IE offsetParent does not include the top-level |
|
99 if (isIE && obj.parentElement){ |
|
100 curtop += obj.offsetTop - obj.scrollTop; |
|
101 } |
|
102 } else if (obj.y) { |
|
103 curtop += obj.y; |
|
104 } |
|
105 return curtop; |
|
106 } |
|
107 |
|
108 //----------------------------------------------------------------------------- |
|
109 // Date object extensions |
|
110 // ---------------------------------------------------------------------------- |
|
111 Date.prototype.getCorrectYear = function() { |
|
112 // Date.getYear() is unreliable -- |
|
113 // see http://www.quirksmode.org/js/introdate.html#year |
|
114 var y = this.getYear() % 100; |
|
115 return (y < 38) ? y + 2000 : y + 1900; |
|
116 } |
|
117 |
|
118 Date.prototype.getTwoDigitMonth = function() { |
|
119 return (this.getMonth() < 9) ? '0' + (this.getMonth()+1) : (this.getMonth()+1); |
|
120 } |
|
121 |
|
122 Date.prototype.getTwoDigitDate = function() { |
|
123 return (this.getDate() < 10) ? '0' + this.getDate() : this.getDate(); |
|
124 } |
|
125 |
|
126 Date.prototype.getTwoDigitHour = function() { |
|
127 return (this.getHours() < 10) ? '0' + this.getHours() : this.getHours(); |
|
128 } |
|
129 |
|
130 Date.prototype.getTwoDigitMinute = function() { |
|
131 return (this.getMinutes() < 10) ? '0' + this.getMinutes() : this.getMinutes(); |
|
132 } |
|
133 |
|
134 Date.prototype.getTwoDigitSecond = function() { |
|
135 return (this.getSeconds() < 10) ? '0' + this.getSeconds() : this.getSeconds(); |
|
136 } |
|
137 |
|
138 Date.prototype.getISODate = function() { |
|
139 return this.getCorrectYear() + '-' + this.getTwoDigitMonth() + '-' + this.getTwoDigitDate(); |
|
140 } |
|
141 |
|
142 Date.prototype.getHourMinute = function() { |
|
143 return this.getTwoDigitHour() + ':' + this.getTwoDigitMinute(); |
|
144 } |
|
145 |
|
146 Date.prototype.getHourMinuteSecond = function() { |
|
147 return this.getTwoDigitHour() + ':' + this.getTwoDigitMinute() + ':' + this.getTwoDigitSecond(); |
|
148 } |
|
149 |
|
150 // ---------------------------------------------------------------------------- |
|
151 // String object extensions |
|
152 // ---------------------------------------------------------------------------- |
|
153 String.prototype.pad_left = function(pad_length, pad_string) { |
|
154 var new_string = this; |
|
155 for (var i = 0; new_string.length < pad_length; i++) { |
|
156 new_string = pad_string + new_string; |
|
157 } |
|
158 return new_string; |
|
159 } |
|
160 |
|
161 // ---------------------------------------------------------------------------- |
|
162 // Get the computed style for and element |
|
163 // ---------------------------------------------------------------------------- |
|
164 function getStyle(oElm, strCssRule){ |
|
165 var strValue = ""; |
|
166 if(document.defaultView && document.defaultView.getComputedStyle){ |
|
167 strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule); |
|
168 } |
|
169 else if(oElm.currentStyle){ |
|
170 strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){ |
|
171 return p1.toUpperCase(); |
|
172 }); |
|
173 strValue = oElm.currentStyle[strCssRule]; |
|
174 } |
|
175 return strValue; |
|
176 } |