/**
* $Id: helpers.js 366 2007-11-24 02:27:32Z wingedfox $
* $HeadURL: https://svn.debugger.ru/repos/jslibs/BrowserExtensions/tags/BrowserExtensions.015/helpers.js $
*
* File contains differrent helper functions
*
* @author Ilya Lebedev <ilya@lebedev.net>
* @license LGPL
* @version $Rev: 366 $
*/
function isUndefined(i){return(typeof i=='undefined');}function isFunction(i){return(typeof i=='function');}function isString(i){return(typeof i=='string');}function isNumber(i){return(typeof i=='number');}function isNumeric(i){return(isNumber(i)||isString(i))&&!isNaN(parseInt(i))&&isFinite(parseInt(i));}function isArray(i){return(i instanceof Array);}function isRegExp(i){return(i instanceof RegExp);}function isBoolean(i){return('boolean'==typeof i);}function isScalar(i){return isNumeric(i)||isString(i)||isBoolean(i);}function isEmpty(i){if(isBoolean(i))return false;if(isRegExp(i)&&new RegExp("").toString()==i.toString())return true;if(isString(i)||isNumber(i))return!i;if(Boolean(i)&&false!=i){for(var I in i)if(i.hasOwnProperty(I))return false}return true}function gluePath(){var i=arguments.length,I=i-2,l=arguments[i-1];for(;I>=0;I--)l=((!isString(arguments[I])&&!isNumber(arguments[I]))||isEmpty(arguments[I])?l:arguments[I]+'\x00'+l);return l?l.replace(/\/*\x00+\/*/g,"/"):""}function findPath(i){var I=document.getElementsByTagName('html')[0].innerHTML,l=new RegExp('<scr'+'ipt[^>]+?src\s*=\s*["\']?([^>]+?/)'+i+'[^>]*>.?</scr'+'ipt>','i'),o=I.match(l);if(o){if(o[1].match(/^((https?|file)\:\/{2,}|\w:[\\])/))return o[1];if(o[1].indexOf("/")==0)return o[1];b=document.getElementsByTagName('base');if(b[0]&&b[0].href)return b[0].href+o[1];return(document.location.href.match(/(.*[\/\\])/)[0]+o[1]).replace(/^\/+/,"");}return null}function getScriptQuery(i){var I=document.getElementsByTagName('html')[0].innerHTML,l=new RegExp('<scr'+'ipt[^>]+?src\s*[^>]+?/'+i+'([^#"\']*).+?</scr'+'ipt>','i'),o=I.match(l);if(o)return parseQuery(o[1].replace(/^[^?]*\?([^#]+)/,"$1"));return{}}function parseQuery(i){if('string'!=typeof i||i.length<2)return{};i=i.split(/&|&/g);for(var I=0,l=i.length,o={},O,Q;I<l;I++){O=i[I].split("=");O[0]=O[0].replace(/[{}\[\]]*$/,"");Q=o[O[0]];O[1]=unescape(O[1]?O[1].replace("+"," "):"");if(Q)if('array'==typeof(Q))o[O[0]][o[O[0]].length]=O[1];else o[O[0]]=[o[O[0]],O[1]];else o[O[0]]=O[1]}return o}function table2array(i,I,l,o){if(isString(i))i=document.getElementById(i);if(!i||!DOM.hasTagName(i,['table','tbody,','thead','tfoot']))return null;if(!isEmpty(l)&&(!isString(l)||!(i=i.getElementsByTagName(l))))return null;if(!isEmpty(o)&&(!isNumber(o)||o<0||!(i=i[o])))return null;if(isUndefined(i.rows))return null;var O=[],Q=document.createElement('span'),_=null,c=null;for(var C=0,e=i.rows.length;C<e;C++){var v=[];if(isArray(I)){for(var V=0,x=I.length;V<x;V++){c=i.rows[C].cells[I[V]];if(c){Q.innerHTML=c.innerText?c.innerText:c.innerHTML.replace(/<script\s+(.|\r?\n)*?<\/script>|<[^>]*>/g,"");Q.normalize();v[v.length]=Q.firstChild?Q.firstChild.nodeValue.trim(" \xA0"):""}else{v[v.length]=""}}}else{for(var V=0,X=i.rows[C].cells.length;V<X;V++){cd=i.rows[C].cells[V];Q.innerHTML=c.innerText?c.innerText:c.innerHTML.replace(/<script\s+(.|\r?\n)*?<\/script>|<[^>]*>/g,"");Q.normalize();v[v.length]=Q.firstChild?Q.firstChild.nodeValue.trim(" \xA0"):""}}if(!isEmpty(v))O[O.length]=v}return O}document.createElementExt=function(i,I){var l,o,O,Q=document.createElement(i);if(!Q)return null;for(o in I){if(!I.hasOwnProperty(o))continue;switch(o){case"class":Q.setAttribute('className',I[o]);Q.setAttribute('class',I[o]);break;case"style":for(O in I[o]){if(!I[o].hasOwnProperty(O))continue;Q.style[O]=I[o][O]}break;case"event":for(O in I[o]){if(!I[o].hasOwnProperty(O))continue;Q.attachEvent(O,I[o][O]);}break;case"child":l=I[o].length;for(O=0;O<l;O++)Q.appendChild(I[o][O]);break;case"param":for(O in I[o]){if(!I[o].hasOwnProperty(O))continue;try{Q[O]=I[o][O]}catch(e){}}break}}return Q};function playInterval(I,o,O){return setInterval(function(){(O instanceof Array)?I.apply(this,O):I.call(this,O)},o)}function playTimeout(I,o,O){return setTimeout(function(){(O instanceof Array)?I.apply(this,O):I.call(this,O)},o)}function cloneObject(i){if(isScalar(i)||isFunction(i)||null==i)return i;try{var I=new i.constructor();}catch(e){return null}if(isArray(I)){for(var l=0,o=i.length;l<o;l++){I[l]=cloneObject(i[l]);}}else{for(var l in i){if(!i.hasOwnProperty(l))continue;I[l]=cloneObject(i[l]);}}return I}function mergeObject(){var i={},I,l;for(var o=0,O=arguments.length;o<O;o++){l=arguments[o];for(var Q in l){if(!l.hasOwnProperty(Q))continue;I=l[Q];if(null==I){if(!i.hasOwnProperty(Q))i[Q]=I}else if(isArray(I)){if(isArray(i[Q]))i[Q]=i[Q].concat(I).unique();else i[Q]=I.slice(0);}else if(isScalar(I)||isFunction(I)){i[Q]=I}else{if(i.hasOwnProperty(Q))i[Q]=mergeObject(i[Q],I);else i[Q]=cloneObject(I);}}}return i}function loadStyleSheet(i){var I=document.getElementsByTagName('head')[0],l=I.getElementsByTagName('link'),o=l.length,O;while(--o>-1){O=l[o];if(!O.rel||O.rel.toLowerCase()!='stylesheet'||O.src!=i)continue;return O}var Q=document.createElementExt('link',{'param':{'rel':'stylesheet','type':'text/css','href':i}});I.appendChild(Q);return Q}if('undefined'==typeof Object.hasOwnProperty){Object.prototype.hasOwnProperty=function(i){return!('undefined'==typeof this[i]||this.constructor&&this.constructor.prototype[i]&&this[i]===this.constructor.prototype[i]);}}if(isUndefined(Array.prototype.concat)){Array.prototype.concat=function(i){var I=this.copy();for(var l=0,o=i.length;l<o;l++){I[I.length]=i[l]}return I}}if(isUndefined(Array.prototype.copy)){Array.prototype.copy=function(){var i=new Array();for(var I=0,l=this.length;I<l;I++){i[I]=this[I]}return i}}if(isUndefined(Array.prototype.pop)){Array.prototype.pop=function(){var i=undefined;if(this.length>0){i=this[this.length-1];this.length--}return i}}if(isUndefined(Array.prototype.push)){Array.prototype.push=function(){var i=this.length;for(var I=0;I<arguments.length;I++){this[i+I]=arguments[I]}return this.length}}if(isUndefined(Array.prototype.shift)){Array.prototype.shift=function(){var i=this[0];for(var I=0,l=this.length-1;I<l;I++){this[I]=this[I+1]}this.length--;return i}}if(isUndefined(Array.prototype.slice)){Array.prototype.slice=function(i,I){var l;if(I==null||I=='')I=this.length;else if(I<0)I=this.length+I;if(i<0)i=this.length+i;if(I<i){l=I;I=i;i=l}var o=new Array();for(var O=0;O<I-i;O++){o[O]=this[i+O]}return o}}if(isUndefined(Array.prototype.splice)){Array.prototype.splice=function(i,I){if(I==null||I=='')I=this.length-i;var l=this.copy();for(var o=i,O=i+arguments.length-2;o<O;o++){this[o]=arguments[o-i+2]}var Q=I-arguments.length+2;for(var o=i+arguments.length-2,_=this.length-I+arguments.length-2;o<_;o++){this[o]=l[o+Q]}this.length=this.length-Q;return l.slice(i,i+I);}}if(isUndefined(Array.prototype.unshift)){Array.prototype.unshift=function(i){for(var I=this.length-1;I>=0;I--){this[I+1]=this[I]}this[0]=i;return this.length}}Array.prototype.indexOf=function(i,I){for(var l=(null==I||isNaN(I)||I<0)?0:Math.round(I),o=this.length,O=-1;O==-1&l<o;l++){O=(this[l]==i)?l:O}return O};Array.prototype.lastIndexOf=function(i,I){for(var l=(null==I||isNaN(I)||I>this.length)?this.length-1:Math.round(I),o=-1;o==-1&l>-1;l--){o=(this[l]==i)?l:o}return o};if(isUndefined(Array.prototype.map)){Array.prototype.map=function(i){if('function'!=typeof i)return this;var I=[];for(var l=this.length-1;l>=0;l--){I[l]=i(this[l]);}return I}}if(isUndefined(Array.prototype.unique)){Array.prototype.unique=function(){var i=[];for(var I=0,l=this.length;I<l;I++){if(i.indexOf(this[I])<0)i[i.length]=this[I]}return i}}if(isUndefined(Array.prototype.flatten)){Array.prototype.flatten=function(i,I){if(this.length<1)return[];if(isNumeric(i))i=[i];var l=false;if(isArray(i)){l={};for(var o=0,O=i.length;o<O;o++)l[i[o]]=true}var Q=[];for(var o=0,_=this.length;o<_;o++){if(isUndefined(this[o]))continue;if(!isArray(this[o])){if(false===l)Q[Q.length]=this[o]}else{for(var c=0,O=this[o].length;c<O;c++){if(false===l||l.hasOwnProperty(c))Q[Q.length]=this[o][c]}}}return Q}}if(isUndefined(Array.prototype.filter)){Array.prototype.filter=function(i){if(!isFunction(i))i=null;for(var I=0,l=this.length,o=[],O=null;I<l;I++){O=i?i(this[I]):this[I];if(!isEmpty(O))o[o.length]=O}return o}}if(isUndefined(Array.prototype.binSearch)){Array.prototype.binSearch=function(i,I){var l=0,o=this.length,O=Math.max(o-1,0),Q=Math.ceil(o/2),_=0;if(null!=I)while((!this[Q]||i!=this[Q][I])&&o>=l){if(this[Q]&&i>this[Q][I])l=Q+1;else o=Q-1;Q=Math.max(0,Math.ceil((o+l)/2))}else while(i!=this[Q]&&o>=l){if(i>this[Q])l=Q+1;else o=Q-1;Q=Math.max(0,Math.ceil((o+l)/2));}return Q}}Array.prototype.heapSort=function(){if(!this.sift){this.sift=function(o,O){var Q,i=this[o];while(true){Q=(o<<1)+1;if(Q>O)break;if(Q<O&&this[Q+1][0]>this[Q][0])Q++;if(i[0]>=this[Q][0])break;this[o]=this[Q];o=Q}this[o]=i}}var i,I=this.length-1,l=I<<1;while(l>=0){this.sift(l--,I)};l=I;while(l>0){i=this[0];this[0]=this[l];this[l]=i;this.sift(0,--l);}};if(isUndefined(Array.range)){Array.range=function(i,I,l){if(!isNumber(i))return null;if(!isNumber(l))l=1;if(!isNumber(I))I=0;var o=[],O=Math.min(I,i),Q=Math.max(I,i),_=Math.abs(l),c=-1;do{c++;o[c]=O;O+=_}while(O<=Q);return l>0?o:o.reverse();}}if(isUndefined(DOM))var DOM={};DOM.getParent=function(i,I,l){if(i==null)return null;else if(i.nodeType==1&&((!isUndefined(l)&&i[I]==l)||('string'==typeof I&&DOM.hasTagName(i,I))||i==I))return i;else return arguments.callee(i.parentNode,I,l);};DOM.getOffset=function(i){var I=true,l=i,o=0,O=0,Q=0,_=0,c=null,C=null;if(l==null)return null;C=l.offsetParent;var e=l,i=l;while(i.parentNode!=null){i=i.parentNode;if(i.offsetParent!==null){var v=true;if(I&&window.opera){if(i==e.parentNode||i.nodeName=="TR"){v=false}}if(v){if(i.scrollTop&&i.scrollTop>0){O-=i.scrollTop}if(i.scrollLeft&&i.scrollLeft>0){o-=i.scrollLeft}}}if(i==C){o+=l.offsetLeft;if(i.clientLeft&&i.nodeName!="TABLE"){o+=i.clientLeft}O+=l.offsetTop;if(i.clientTop&&i.nodeName!="TABLE"){O+=i.clientTop}l=i;if(l.offsetParent==null){if(l.offsetLeft){o+=l.offsetLeft}if(l.offsetTop){O+=l.offsetTop}}C=l.offsetParent}}if(e.offsetWidth){Q=e.offsetWidth}if(e.offsetHeight){_=e.offsetHeight}return{'x':o,'y':O,'width':Q,'height':_}};DOM.getClientWidth=function(){var i=0;if(self.innerHeight)i=self.innerWidth;else if(document.documentElement&&document.documentElement.clientWidth)i=document.documentElement.clientWidth;else if(document.body)i=document.body.clientWidth;return i};DOM.getClientHeight=function(){var i=0;if(self.innerHeight)i=self.innerHeight;else if(document.documentElement&&document.documentElement.clientHeight)i=document.documentElement.clientHeight;else if(document.body)i=document.body.clientHeight;return i};DOM.getBodyScrollTop=function(){return self.pageYOffset||(document.documentElement&&document.documentElement.scrollTop)||(document.body&&document.body.scrollTop);};DOM.getBodyScrollLeft=function(){return self.pageXOffset||(document.documentElement&&document.documentElement.scrollLeft)||(document.body&&document.body.scrollLeft);};DOM.getCursorPosition=function(i){if(i.pageX||i.pageY)return{'x':i.pageX,'y':i.pageY};var I=document.documentElement||document.body;return{'x':i.clientX+I.scrollLeft-(I.clientLeft||0),'y':i.clientY+I.scrollTop-(I.clientTop||0)}};DOM.hasTagName=function(i,I){if(isString(I))I=[I];if(!isArray(I)||isEmpty(I)||isUndefined(i)||isEmpty(i.tagName))return false;var l=i.tagName.toLowerCase();for(var o=0,O=I.length;o<O;o++){if(I[o].toLowerCase()==l)return true}return false};DOM.color2rgb=function(i){var I;if(/^([a-z]+)($|\s[a-z]+)/i.test(i)){var l=document.body,o=l.vLink;l.vLink=i.split(" ")[0];i=l.vLink;l.vLink=o}try{if(I=i.match(/^#([\da-f]{6})$/i))return I=parseInt(I[1],16),[(I&0xff0000)>>16,(I&0xff00)>>8,(I&0xff)];else if(I=i.match(/^#([\da-f]{3})$/i)){return I=parseInt(I[1],16),[((I&0xf00)>>8)*0x11,((I&0xf0)>>4)*0x11,(I&0xf)*0x11]}else return(i.match(/([\d%]+)/g).splice(0,3).map(function(O){return/%/.test(O)?(parseInt(O)*2.55).toFixed(0):parseInt(O)}))}catch(err){}};DOM.CSS=function(l){var o=this;o.addClass=function(){var O=isArray(arguments[0])?arguments[0]:Array.prototype.slice.call(arguments);o.removeClass(O);l.className=l.className+" "+Array.prototype.join.call(O," ");return o};o.removeClass=function(){var O=Array.prototype.join.call((isArray(arguments[0])?arguments[0]:arguments),"|");if(!arguments.callee.cache)arguments.callee.cache={};var Q=arguments.callee.cache;if(!Q.hasOwnProperty(O))Q[O]=new RegExp("(^|\\s+)("+O+")(\\s+|$)","g");l.className=l.className.replace(Q[O]," ");return o};o.hasClass=function(I){re=new RegExp("(^|\\s+)"+I+"(\\s+|$)");return l.className.match(re," "+I+" ");};o.getClass=function(){return l.className};o.getClassValue=function(I){var O=l.className.match(new RegExp("(^|\\s)"+I+":([^\\s]+)"));return O?((O[2].indexOf(":")+1)?O[2].split(":"):O[2]):null};o.getComputedStyle=function(I){var O;if(l.currentStyle)O=I?l.currentStyle[I]:l.currentStyle;else if(window.getComputedStyle){O=document.defaultView.getComputedStyle(l,null);if(I)O=O[I]}else{O=null}return O};return this};var EM=new function(){var C=this;var e=[];var V=0;var x={'UEID':'__eventManagerUniqueElementId'};var z=function(i){w(i);var l=null,O=null,Q=i.target,c=true,X=true;if(!i.currentTarget||!(l=i.currentTarget[x.UEID])||!(O=e[l].handler[i.type]))return;try{for(var S=0,k=O.length;S<k;S++)if(isFunction(O[S]))X=X&&!(false===O[S].call(i.currentTarget,i));}catch(err){setTimeout(function(){throw new Error("Event handler for ["+i.type+"] has failed with exception: \""+err.message+"\"");},10)}return X};var Z=function(i){for(var l=e.length-1,o=null,Q=null;l>=0;l--){if(e[l]&&(Q=(o=e[l]).node)){for(var _ in o.handler){if(!o.handler.hasOwnProperty(_))continue;if(Q.removeEventListener){Q.removeEventListener(_,o.rootEHCaller?o.rootEHCaller:z,false);}else if(Q.detachEvent){Q.detachEvent('on'+_,o.rootEHCaller?o.rootEHCaller:z);}o.handler[_].length=0}}Q=o.node=null}if(window.removeEventListener){window.removeEventListener(_,arguments.callee,false);}else{window.detachEvent('on'+_,arguments.callee);}};var w=function(I){var l=C.EU.length,o,O,Q,_;while(l--){o=C.EU[l];if(o[0].test(I.type)){Q=o[1].length;_=null;while(Q--){O=o[1][Q];if('init'==O[0])_=O[1];else if(!I[O[0]])I[O[0]]=O[1]}if(_)_.call(I);}}if(!I.target&&I.type!='unload')I.target=I.srcElement;return I};var W=function(i,I){return i[x.UEID]||(I&&(i[x.UEID]=++V));};C.addEventListener=function(i,l,o){if(!i||!isFunction(o))return false;var Q=W(i,true),_=null,v=null;if(!e[Q]){e[Q]={'node':i,'handler':{}}};_=e[Q];if(!_.handler.hasOwnProperty(l)){_.handler[l]=[];if(i.addEventListener){i.addEventListener(l,z,false);}else if(i.attachEvent){_.rootEHCaller=function(I){I.currentTarget=_.node;var c=z(I);I.currentTarget=null;return c};i.attachEvent('on'+l,_.rootEHCaller);}};v=_.handler[l];if(v.indexOf(o)==-1){v[v.length]=o;return true}return false};C.removeEventListener=function(i,l,o){if(!i||!isFunction(o))return false;var Q=W(i),_=e[Q],v=null;if(_&&(v=_.handler[l])){v.splice(v.indexOf(o),1);if(0==v.length){delete _.handler[l];if(i.removeEventListener){i.removeEventListener(l,_.rootEHCaller?_.rootEHCaller:z,false);}else if(i.detachEvent){i.detachEvent('on'+l,_.rootEHCaller?_.rootEHCaller:z);}}return true}return false};C.dispatchEvent=function(i){var I=z(i);return I};C.registerEvent=function(i,l,o,O){var Q=W(i,true);if(!e[Q]){e[Q]={'node':i,'handler':[]}}else{e[Q].node=i}return new EM.EventTarget(i,l,o,O);};var s=function(){if(window.attachEvent&&!window.addEventListener){window.attachEvent('onunload',Z);}};s();};EM.preventDefaultAction=function(i){i.preventDefault();};EM.stopPropagationAction=function(i){i.stopPropagation();};EM.EventTarget=function(O,e,v,V){var x=this;var X=!!v;var z=isFunction(V)?V:null;x.trigger=function(W,s){if(!(arguments.length-1)&&W!=O){s=W;W=null}if(!W)W=O;var S={},k=true,K=true,q=null;for(var E in s){if(s.hasOwnProperty(E))S[E]=s[E]}X=!!v;z=V;do{S.preventDefault=Z;S.stopPropagation=w;S.target=W;S.currentTarget=W;S.type=e;q=EM.dispatchEvent(S);K&=(isUndefined(q));k&=!(false===q);}while((W=W.parentNode)&&X);if(isFunction(z)&&k&&!K){z(S);}return(z&&k&&!K);};var Z=function(){z=null};var w=function(){X=false}};EM.EU=[[/./,[['preventDefault',function(){this.returnValue=false}],['stopPropagation',function(){this.cancelBubble=true}]]],[/^mouse(over|out|down|up)/,[['getButton',function(){return this.button==2?2:1}],['EM_MB_LEFT','1'],['EM_MB_RIGHT','2']]],[/^key(down|up|press)/,[['getKeyCode',function(){switch(this.keyCode){case 189:return 109;case 187:return 61;case 186:return 59;default:return this.keyCode}}],['getRepeat',function(){return arguments.callee.repeat}],['init',function(){var i=this.getRepeat;if('keyup'==this.type){i.repeat=0;i.keyCode=0}else if('keydown'==this.type){i.repeat=i.keyCode==this.keyCode;i.keyCode=this.keyCode}}]]]];(function(){var O=EM.registerEvent(window,'domload'),Q=false,_=function(){EM.removeEventListener(document,'propertychange',c.ie);EM.removeEventListener(document,'DOMContentLoaded',c.mz);EM.removeEventListener(window,'load',c.mz);},c={'ie':function(o){if(window.event.propertyName=='activeElement'&&!Q){O.trigger(window);_();Q=true}},'mz':function(l){if(!Q)O.trigger(window);Q=true}};EM.addEventListener(document,'propertychange',c.ie);EM.addEventListener(document,'DOMContentLoaded',c.mz);if(/WebKit|Khtml/i.test(navigator.userAgent)||(window.opera&&parseInt(window.opera.version())<9))(function(){/loaded|complete/.test(document.readyState)?(O.trigger(window),Q=true):setTimeout(arguments.callee,100)})();EM.addEventListener(window,'load',c.mz);})();RegExp.escape=function(i){if(!arguments.callee.sRE){var I=['/','.','*','+','?','|','(',')','[',']','{','}','$','^','\\'];arguments.callee.sRE=new RegExp('(\\'+I.join('|\\')+')','g');}return isString(i)?i.replace(arguments.callee.sRE,'\\$1'):(isArray(i)?i.map(RegExp.escape).join("|"):"");};String.prototype.entityDecode=function(){if(!arguments.callee.span)arguments.callee.span=document.createElement('span');var i=arguments.callee.span;i.innerHTML=this;return i.firstChild?i.firstChild.nodeValue:""};String.prototype.ltrim=function(i){if(isString(i))i=i.split("");if(isArray(i)||isUndefined(i)){i=isEmpty(i)?"\\s":RegExp.escape(i);i=new RegExp("^(?:"+i+")+","g");return this.replace(i,"");}return this};String.prototype.rtrim=function(i){if(isString(i))i=i.split("");if(isArray(i)||isUndefined(i)){i=isEmpty(i)?"\\s":RegExp.escape(i);i=new RegExp("(?:"+i+")+$","g");return this.replace(i,"");}return this};String.prototype.trim=function(i){if(isString(i))i=i.split("");if(isArray(i)||isUndefined(i)){i=isEmpty(i)?"\\s":RegExp.escape(i);i=new RegExp("^(?:"+i+")+|(?:"+i+")+$","g");return this.replace(i,"");}return this};String.prototype.dup=function(){var i=this.valueOf();return[i,i].join("");};String.prototype.repeat=function(i){if(isNaN(i=parseInt(i))||i<0)return"";return Array(i+1).join(this.valueOf());};String.prototype.padding=function(i,I){var l=this.valueOf();i=parseInt(i);if(!i)return l;if(isUndefined(I))I=" ";var o=String(I).charAt(0).repeat(Math.abs(i)-this.length);return(i<0)?o+l:l+o};String.prototype.padLeft=function(i,I){return this.padding(-Math.abs(i),I);};String.prototype.padRight=function(i,I){return this.padding(Math.abs(i),I);};String.prototype.sprintf=function(){var l=isArray(arguments[0])?arguments[0]:arguments,o=0,O=this.replace(/%%/g,"\0\0"),Q= /%((?:\d+\$)?)((?:[-0+# ])?)((?:\d+|\*(?:\d+\$)?)?)((?:.(?:\d+|\*(?:\d+\$)?))?)([bcdeEfosuxX])/g;O=O.replace(Q,function(){var _=arguments,c=false,C;if(!isUndefined(_[3])&&_[3].indexOf("*")==0){_[3]=parseInt(_[3].replace(/\D/g,""));if(isNaN(_[3])){_[3]=l[o];o++}else{_[3]=l[_[3]]}}if(""!=_[4]){if(_[4].indexOf("*")==1){_[4]=parseInt(_[4].replace(/\D/g,""));if(isNaN(_[4])){_[4]=l[o];o++}else{_[4]=l[_[4]]}}else{_[4]=_[4].replace(/\D/,"")}_[4]=Math.abs(_[4]);}_[1]=parseInt(_[1]);var C;if(isNumeric(_[1])){C=l[_[1]]}else{C=l[o];o++}switch(_[5]){case"b":if(C<0)C=0x10000000000000000+parseInt(C);C=Number(C).bin(_[4]);if(_[4])C=C.substr(0,_[4]);if(_[2]=='#')C='0b'+C;break;case"c":C=String.fromCharCode(C);break;case"u":C=Math.abs(C);case"d":C=Math.round(C);if(C<0){C="-"+Math.abs(C).dec(_[4]);}else{C=Number(C).dec(_[4]);c=(_[2]==' '||_[2]=='+');}break;case"e":case"E":if(C>0){c=(_[2]==' '||_[2]=='+');}C=Number(C).toExponential(_[4]?_[4]:6);if(_[5]=='E')C=C.toUpperCase();break;case"f":if(C>0){c=(_[2]==' '||_[2]=='+');}C=Number(C).toFixed(isNumeric(_[4])?_[4]:6);break;case"o":if(C<0)C=0x10000000000000000+parseInt(C);C=Number(C).toString(8);if(_[4])C=C.substr(0,_[4]);if(_[2]=='#'&&C!=0)C='0'+C;break;case"s":C=String(C);if(_[4])C=C.substr(0,_[4]);break;case"x":case"X":if(C<0)C=0x10000000000000000+parseInt(C);C=Number(C).hex(-_[4]);if(_[4])C=C.substr(0,_[4]);if(_[2]=='#')C='0x'+C;if(_[5]=='X')C=C.toUpperCase();break}if(c)C=_[2]+C;if(_[3])C=(_[2]=='-'||_[3]<0)?C.padRight(_[3]):C.padLeft(_[3],_[2]=='0'?0:" ");return C});return O.replace(/\0\0/g,"%");};DocumentSelection=new function(){var O=this;var Q={'prevCalcNode':'__prevCalcNode'};var _=function(l,o){var C=o[0],e,v="";if(!C||!C.tagName)return false;switch(o[0].tagName.toLowerCase()){case'input':if(C.type&&C.type!='text'&&C.type!='password')return false;case'textarea':v="input";break;case'iframe':v="frame";o[0]=C.contentWindow;break;default:return false}if('function'==typeof O.module[v])O.module[v]=new O.module[v](Q);if(!O.module[v]||!O.module[v][l])throw new Error('Method \''+l+'\' is not implemented for DocumentSelection \''+v+'\' module.');return O.module[v][l].apply(O,o);};var c=function(I,l,o){if(window.getSelection){var C=O.getSelectionOffset(I);if(I.contentWindow)I=I.contentWindow.document.body;if(l>C.y)I.scrollTop=C.y;else if(l+I.clientHeight>C.y)I.scrollTop=l;else I.scrollTop=C.y-I.clientHeight/2;if(o>C.x)I.scrollLeft=C.x;else if(o+I.clientWidth>C.x)I.scrollLeft=o;else I.scrollLeft=C.x-I.clientWidth/2}};O.setRange=function(I,C,e,v){var V=I.scrollTop,x=I.scrollLeft;if(v){var X=O.getStart(I);e=X+e;C=X+C}if(C<0)C=0;if(e<C)e=C;_('setRange',[I,C,e]);c(I,V,x);};O.getSelection=function(i){return _('getSelection',[i]);};O.getStart=function(i){return _('getPos',[i,true]);};O.getEnd=function(i){return _('getPos',[i,false]);};O.getCursorPosition=function(I){return O.getStart(I);};O.insertAtCursor=function(i,I,C){var e=i.scrollTop,v=i.scrollLeft;if(!C){_('del',[i]);}var V=_('ins',[i,I]);c(i,e,v);return V};O.wrapSelection=function(I,l,o){var C=O.getCursorPosition(I),e=O.getEnd(I);if(C==e){O.insertAtCursor(I,l+o);}else{O.insertAtCursor(I,l,true);O.setRange(I,e+l.length,e+l.length);O.insertAtCursor(I,o,true);}};O.deleteAtCursor=function(I,l){if(!O.getSelection(I)){if(l)O.setRange(I,0,1,true);else O.setRange(I,-1,0,true);}return O.deleteSelection(I);};O.deleteSelection=function(i){var I=i.scrollLeft,C=i.scrollTop,e=_('del',[i]);c(i,C,I);return e};O.getSelectionOffset=function(i){return _('getSelectionOffset',[i],true);}};DocumentSelection.module={'input':function(o){var O=this;var Q=null;O.getPos=function(i,I){var l;try{i.setActive();if(I)l=Math.abs(document.selection.createRange().moveStart("character",-100000000));else l=Math.abs(document.selection.createRange().moveEnd("character",-100000000));if(i.tagName.toLowerCase()!='input'){var _=document.body.createTextRange();_.moveToElementText(i);var c=Math.abs(_.moveStart("character",-100000000));l-=c}}catch(e){try{l=(I?i.selectionStart:i.selectionEnd);}catch(e){l=0}}return l};O.del=function(i){var l="",_=O.getPos(i,true),c=O.getPos(i,false);if(_!=c){var C=document.selection&&!window.opera?i.value.replace(/\r/g,""):i.value;l=C.substring(_,c);i.value=C.substring(0,_)+C.substring(c,C.length);O.setRange(i,_,_);}return l};O.ins=function(i,l){var _="",c=O.getPos(i,true);var C=document.selection&&!window.opera?i.value.replace(/\r/g,""):i.value;i.value=C.substring(0,c)+l+C.substring(c,C.length);c+=l.length;O.setRange(i,c,c);return c};O.getSelection=function(i){var l=O.getPos(i,true),_=O.getPos(i,false);if(_<l)_=l;var c=document.selection&&!window.opera?i.value.replace(/\r/g,""):i.value;return c.substring(l,_);};O.setRange=function(i,I,l){if('function'==typeof i.setSelectionRange){try{i.setSelectionRange(I,l)}catch(e){}}else{var _;try{_=i.createTextRange();}catch(e){try{_=document.body.createTextRange();_.moveToElementText(i);}catch(e){return false}}_.collapse(true);_.moveStart("character",I);_.moveEnd("character",l-I);_.select();}};O.getSelectionOffset=function(I){var _;if('function'==typeof I.setSelectionRange){if(!Q){Q=document.createElement('td');document.body.appendChild(Q);}if(Q[o.prevCalcNode]!=I){Q[o.prevCalcNode]=I;var c=document.defaultView.getComputedStyle(I,null);for(var C in c){try{if(c[C])Q.style[C]=c[C]}catch(e){}}Q.style.overflow='auto';Q.style.position='absolute';Q.style.visibility='hidden';Q.style.zIndex='-10';Q.style.left="-10000px";Q.style.top="-10000px";Q.style.backgroundColor='yellow'}var _=document.createRange(),v=I.value||" ";if('input'==I.tagName.toLowerCase()){Q.style.width='auto';Q.style.whiteSpace='nowrap'}else{Q.style.whiteSpace='off'==I.getAttribute('wrap')?"pre":""}v=v.replace(/\x20\x20/g,"\x20\xa0").replace(/</g,"<").replace(/>/g,">");Q.innerHTML=(v.substring(0,I.selectionStart-1)+"<span>"+v.substring(I.selectionStart-1,I.selectionStart)+"</span>"+v.substring(I.selectionStart)).replace(/\n/g,"<br />").replace(/\t/g,"<em style=\"white-space:pre\">\t</em>");var V=Q.getElementsByTagName('span')[0];V.style.border='1px solid red';_.offsetLeft=V.offsetLeft;_.offsetTop=V.offsetTop;_.offsetHeight=V.offsetHeight;if("\n"==v.charAt(I.selectionStart-1))_.offsetTop+=_.offsetHeight*2;V=null}else if(document.selection&&document.selection.createRange){_=document.selection.createRange();_.offsetHeight=Math.round(_.boundingHeight/(_.text.replace(/[^\n]/g,"").length+1));if(I.tagName&&'textarea'==I.tagName.toLowerCase()){var x=DOM.getOffset(I);_={'offsetTop':_.offsetTop-x.y+DOM.getBodyScrollTop(),'offsetLeft':_.offsetLeft-x.x+DOM.getBodyScrollLeft(),'offsetHeight':_.offsetHeight}}}if(_){return{'x':_.offsetLeft,'y':_.offsetTop,'h':_.offsetHeight}}return{'x':0,'y':0,'h':0}}},'frame':function(){var I=this;I.getPos=function(i,l){var o=0;if('function'==typeof i.getSelection){var O=i.getSelection(),Q=O.anchorNode,_=O.anchorOffset,c=O.focusNode,C=O.focusOffset,e=false,v=false,V=0,x=0,X,z=document.createTreeWalker(i.document.body,NodeFilter.SHOW_TEXT,null,false);if(Q.nodeType!=3){Q=Q.childNodes[_];_=0}if(c.nodeType!=3){c=c.childNodes[C];C=0}while(X=z.nextNode()){if(X==c){x+=C;v=true}if(X==Q){V+=_;e=true}if(!v)x+=X.nodeValue.length;if(!e)V+=X.nodeValue.length;if(v&&e)break}o=l?Math.min(x,V):Math.max(x,V)}else{i.document.body.setActive();o=Math.abs(i.document.selection.createRange()[l?"moveStart":"moveEnd"]("character",-100000000));}return o};I.del=function(i){if('function'==typeof i.getSelection){var l=i.getSelection(),o=l.rangeCount;while(--o>-1)l.getRangeAt(o).deleteContents();}else if(i.document&&i.document.selection){i.document.selection.createRange().text="";i.document.selection.createRange().select();}};I.ins=function(l,o){var O=I.getPos(l,true)+o.length;if('function'==typeof l.getSelection){var Q=l.document.createTextNode(o),_=l.getSelection();_.getRangeAt(0).insertNode(Q);Q.parentNode.normalize();}else if(l.document&&l.document.selection){l.document.body.setActive();l.document.selection.createRange().text=o}I.setRange(l,O,O);return O};I.getSelection=function(i,l,o){if('function'==typeof i.getSelection){var l=i.getSelection();return l?l.toString():""}else if(i.document&&i.document.selection){return i.document.selection.createRange().text}};I.setRange=function(i,l,o){if('function'==typeof i.getSelection){var O=i.getSelection();O.removeAllRanges();var Q=i.document.createRange(),_=0,c=0,C,e,v=document.createTreeWalker(i.document.body,NodeFilter.SHOW_TEXT,null,false);while((C=v.nextNode())&&(!C.nodeValue.length||(_+C.nodeValue.length<l))){e=C;_+=C.nodeValue.length}if(C||(C=e)){Q.setStart(C,l-_);Q.setEnd(C,l-_);}if(C){do{if(C.nodeType!=3)continue;if(_+C.nodeValue.length<o){_+=C.nodeValue.length}else{Q.setEnd(C,o-_);break}}while(C=v.nextNode())}O.addRange(Q);}else if(i.document&&i.document.selection){i.document.body.setActive();var Q=i.document.selection.createRange();Q.moveToElementText(i.document.body);Q.move("character",l);Q.moveEnd("character",o-l);Q.select();}};I.getSelectionOffset=function(i){var l={'x':0,'y':0,'h':0};if('function'==typeof i.getSelection){var o=i.getSelection().getRangeAt(0),O=o.endOffset,Q=i.document.createElement('span'),_=Q;Q.style.borderLeft='1px solid red';o.insertNode(Q);l.h=_.offsetHeight;while(_.offsetParent){l.x+=_.offsetLeft;l.y+=_.offsetTop;_=_.offsetParent}Q.parentNode.removeChild(Q);if(O-o.endOffset){o.setEnd(o.endContainer.nextSibling,O-o.endOffset);i.getSelection().addRange(o)}}else if(i.document&&i.document.selection){var o=i.document.selection.createRange();l.h=o.boundingHeight;l.x=o.offsetLeft;l.y=o.offsetTop}return l}}};