+};
+
+qwebirc.util.DaysOfWeek = {
+ 0: "Sun",
+ 1: "Mon",
+ 2: "Tue",
+ 3: "Wed",
+ 4: "Thu",
+ 5: "Fri",
+ 6: "Sat"
+};
+
+qwebirc.util.MonthsOfYear = {
+ 0: "Jan",
+ 1: "Feb",
+ 2: "Mar",
+ 3: "Apr",
+ 4: "May",
+ 5: "Jun",
+ 6: "Jul",
+ 7: "Aug",
+ 8: "Sep",
+ 9: "Oct",
+ 10: "Nov",
+ 11: "Dec"
+};
+
+qwebirc.util.NBSPCreate = function(text, element) {
+ var e = text.split(" ");
+ for(var i=0;i<e.length;i++) {
+ var tn = document.createTextNode(e[i]);
+ element.appendChild(tn);
+
+ if(i != e.length - 1) {
+ var e2 = new Element("span");
+ e2.set("html", " ");
+ element.appendChild(e2);
+ }
+ }
+};
+
+qwebirc.util.longtoduration = function(l) {
+ var seconds = l % 60;
+ var minutes = Math.floor((l % 3600) / 60);
+ var hours = Math.floor((l % (3600 * 24)) / 3600);
+ var days = Math.floor(l / (24*3600));
+
+ return days + " days " + hours + " hours " + minutes + " minutes " + seconds + " seconds";
+}
+
+qwebirc.util.pad = function(x) {
+ x = "" + x;
+ if(x.length == 1)
+ return "0" + x;
+ return x
+}
+
+RegExp.escape = function(text) {
+ return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
+};
+
+RegExp.fromIRCPattern = function(t) {
+ /* escape everything but ? . and * */
+ var t = t.replace(/[-[\]{}()+,\\^$|#\s]/g, "\\$&");
+ t = t.split("");
+ var out = [];
+
+ /* now process the rest */
+ for(var i=0;i<t.length;i++) {
+ var c = t[i];
+ switch(c) {
+ case '.':
+ out.push("\\.");
+ break;
+ case '?':
+ out.push(".");
+ break;
+ case '*':
+ out.push(".*");
+ break;
+ default:
+ out.push(c);
+ }
+ }
+ return out.join("");
+}
+
+qwebirc.ui.insertAt = function(position, parent, element) {
+ if(!parent.childNodes || (position >= parent.childNodes.length)) {
+ parent.appendChild(element);
+ } else {
+ parent.insertBefore(element, parent.childNodes[position]);
+ }
+}
+
+qwebirc.util.setCaretPos = function(obj, pos) {
+ if($defined(obj.selectionStart)) {
+ obj.focus();
+ obj.setSelectionRange(pos, pos);
+ } else if(obj.createTextRange) {
+ var range = obj.createTextRange();
+ range.move("character", pos);
+ range.select();
+ }
+}
+
+qwebirc.util.setAtEnd = function(obj) {
+ qwebirc.util.setCaretPos(obj.value.length);
+}
+
+qwebirc.util.getCaretPos = function(element) {
+ if($defined(element.selectionStart))
+ return element.selectionStart;
+
+ if(document.selection) {
+ element.focus();
+ var sel = document.selection.createRange();
+ sel.moveStart("character", -element.value.length);
+ return sel.text.length;
+ }
+}
+
+qwebirc.util.browserVersion = function() {
+ //return "engine: " + Browser.Engine.name + " platform: " + Browser.Platform.name + " user agent: " + navigator.userAgent;
+ return navigator.userAgent;
+}
+
+qwebirc.util.getEnclosedWord = function(text, position) {
+ var l = text.split("");
+ var buf = [];
+
+ if(text == "")
+ return;
+
+ var start = position - 1;
+ if(start < 0) {
+ /* special case: starting with space */
+ start = 0;
+ } else {
+ /* work back until we find the first space */
+ for(;start>=0;start--) {
+ if(l[start] == ' ') {
+ start = start + 1;
+ break;
+ }
+ }
+ }
+
+ if(start < 0)
+ start = 0;
+
+ var s = text.substring(start);
+ var pos = s.indexOf(" ");
+ if(pos != -1)
+ s = s.substring(0, pos);
+
+ return [start, s];
+}
+
+String.prototype.startsWith = function(what) {
+ return this.substring(0, what.length) == what;
+}
+
+/* NOT cryptographically secure! */
+qwebirc.util.randHexString = function(numBytes) {
+ var getByte = function() {
+ return (((1+Math.random())*0x100)|0).toString(16).substring(1);
+ };
+
+ var l = [];
+ for(var i=0;i<numBytes;i++)
+ l.push(getByte());
+
+ return l.join("");
+}
+
+qwebirc.util.importJS = function(name, watchFor, onload) {
+ var script = document.createElement("script");
+ script.type = "text/javascript";
+ script.src = name;
+
+ if(Browser.Engine.trident) {
+ /* HORRID */
+ var checkFn = function() {
+ if(eval("typeof " + watchFor) != "undefined") {
+ onload();
+ } else {
+ checkFn.delay(100);
+ }
+ }
+ checkFn();
+ } else {
+ script.onload = onload;
+ }
+ document.getElementsByTagName("head")[0].appendChild(script);
+}
+
+qwebirc.util.createInput = function(type, parent, name, selected, id) {
+ var created = false;
+ var r;
+ if (name)
+ name = "__input" + name;
+
+ if (Browser.Engine.trident) {
+ var name2;
+ if (name) {
+ name2 = " name=\"" + escape(name) + "\"";
+ } else {
+ name2 = "";
+ }
+ try {
+ var h = "<input type=\"" + type + "\"" + name2 + "/>";
+ r = $(document.createElement(h));
+ if (type == "radio") {
+ r.addEvent("click", function () {
+ $(document.body).getElements("input[name=" + name + "]").forEach(function (x) {
+ x.setAttribute("defaultChecked", x.checked ? "defaultChecked" : "");
+ });
+ });
+ }
+ created = true;
+ } catch (e) {
+ /* fallthough, trying it the proper way... */
+ }
+ }
+
+ if(!created) {
+ r = new Element("input");
+ r.setAttribute("type", type);
+ }
+ if(name)
+ r.setAttribute("name", name);
+ if(id)
+ r.setAttribute("id", id);
+ if(selected) {
+ r.setAttribute("checked", "checked");
+ if(type == "radio" && Browser.Engine.trident)
+ r.setAttribute("defaultChecked", "defaultChecked");
+ }
+
+ parent.appendChild(r);
+ return r;