core.js 6.5 KB

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