function ltrim(str) {
	while (str.charAt(0) == " ")
		str = str.substring(1, str.length);
	return str;
}

function newClass (parent, prop) {
	var inst = function () {
		if (inst.preparing) return delete (inst.preparing);
		
		if (inst.constr) {
			this.constructor = inst;
			inst.constr.apply (this, arguments);
		}
	}
	inst.prototype = {};
	if (parent) {
		parent.preparing = true;
		inst.prototype = new parent;
		inst.prototype.constructor = parent;
		inst.constr = parent;
	}
	if (prop) {
		var cname = "constructor";
		for (var k in prop) {
			if (k != cname) inst.prototype [k] = prop [k];
		}
		if (prop [cname] && prop [cname] != Object)
			inst.constr = prop [cname];
	}
	return inst;
}

var Uni_DocHandler = newClass(null, {
	win: window.top,
	doc: window.top.document,

  constructor: function() {
    var obj = this;
    obj.win.$ = function(id) {
      return obj.doc.getElementById(id);
    }
  },
	
	createXmlDoc: function() {
		try {
			if (document.implementation && document.implementation.createDocument) {
				var doc = document.implementation.createDocument("", "", null);
				
				if (doc.readyState == null) {
					doc.readyState = 1;
					doc.addEventListener("load", function () {
					   doc.readyState = 4;
					   if (typeof doc.onreadystatechange == "function")
					      doc.onreadystatechange();
					}, false);
				}
				return doc;
			}
			if (window.ActiveXObject)
				return new ActiveXObject(this.getControlPrefix() + ".XmlDom");
		}
		catch (ex) {}
		throw new Error("Your browser does not support XmlDocument objects");
	},
	
	getControlPrefix: function() {
		if (this.getControlPrefix.prefix)
			return this.getControlPrefix.prefix;

		var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
		var o, o2;
		for (var i = 0; i < prefixes.length; i++) {
		  try {
		     o  = new ActiveXObject(prefixes[i] + ".XmlHttp");
		     o2 = new ActiveXObject(prefixes[i] + ".XmlDom");
		     return this.getControlPrefix.prefix = prefixes[i];
		  }
		  catch (ex) {};
		}
		throw new Error("Could not find an installed XML parser");
	},

	redirect: function(addr) {
		window.top.document.location.href = addr;
	},
	
	openSingleWin: function(win, id, url, features) {
		if ((!win) || ((win) && (win.closed)))
			win = this.win.open(url, id, features);
		else
      win.focus();
    
    return win;
	}
});

var Uni_CtrlHandler = newClass(Uni_DocHandler, {
	addEventTo: function (obj, event, handler) {
		var altEN = 'on' + event;
		if (obj.attachEvent)
			obj.attachEvent (altEN, handler);
		else if (obj.addEventListener)
			obj.addEventListener (event, handler, false);
		else
			alert ("You browser doesn't support " + altEN + " event.");
	},
	
	removeEvent: function (obj, event, handler) {
		var altEN = 'on' + event;
		if (obj.detachEvent)
			obj.detachEvent (altEN, handler);
		else if (obj.removeEventListener)
			obj.removeEventListener (event, handler, false);
		else
			alert ("You browser doesn't support " + altEN + " event.");
	},
	
	deleteChildren: function(obj) {
		if (obj) {
			while(obj.hasChildNodes()) {
				obj.removeChild(obj.firstChild);
			}
		}
	},
	
	getNodeText: function(node) {
		return node.firstChild ? node.firstChild.nodeValue : '';
	},
	
	setSelectValue: function(val, node) {
		if (node.hasChildNodes) {
			for (var i = 0; i < node.childNodes.length; i++) {
				child = node.childNodes[i];
				if (child.nodeName == 'OPTION') {
					if (child.value == val || (!val && i == 0)) {
						child.selected = 'SELECTED';
					}
					else if (child.selected) {
						child.selected = false;
					}
				}
			}
		}
	},
	
	handleByAttr: function(attrName, pattern, tagName, callback) {
    var results = new Array();
		var pattern = new RegExp("^" + pattern, "i");
		var name, boxes = this.doc.getElementsByTagName(tagName);
		
		for (i = 0; i < boxes.length; i++) {
			name = '' + (attrName == 'class'
        ? boxes[i].className 
        : boxes[i].getAttribute(attrName));

			if (name.match(pattern)) {
				if (callback != null)
					callback(boxes[i]);

				results.push(boxes[i]);
			}
		}
		return results;
	},

	handleById: function(pattern, tagName, callback) {
		return this.handleByAttr('id', pattern, tagName, callback);
	},
	
	handleByName: function(pattern, tagName, callback) {
		return this.handleByAttr('name', pattern, tagName, callback);
	},

  handleByClass: function(pattern, tagName, callback) {
		return this.handleByAttr('class', pattern, tagName, callback);
	}
});

var Uni_XHandler = newClass(Uni_CtrlHandler, {
  createTable: function(rows, attrs, styles) {
    var table = this.doc.createElement('TABLE');
    var tbody = table.appendChild(this.doc.createElement('TBODY'));
    for (var i = 0; i < rows.length; i++)
      tbody.appendChild(row[i]);
    
    for (var i in styles) table.style[i] = styles[i];
    return this.setAttributes(attrs, table);
  },

  createTableRow: function(cells, attrs, styles) {
    var row = this.doc.createElement('TR');
    for (var i = 0; i < cells.length; i++)
      row.appendChild(cells[i]);

    for (var i in styles) row.style[i] = styles[i];
    return this.setAttributes(attrs, row);
  },

  createTableCell: function(content, attrs, styles) {
    var cell = this.doc.createElement('TD');
    cell.appendChild(typeof(content) == 'string' ? this.doc.createTextNode(content) : content);

    for (var i in styles) cell.style[i] = styles[i];
    return this.setAttributes(attrs, cell);
  },

  setAttributes: function(attrSet, targetElem) {
    for (var attrName in attrSet)
    {
      switch(attrName) {
        case 'class':
          targetElem.className = attrSet[attrName];
          break;
        case 'id':
          targetElem.id = attrSet[attrName];
          break;
        case 'type':
          targetElem.type = attrSet[attrName];
          break;
        default:
          targetElem.setAttribute(attrName, attrSet[attrName]);
          break;
      }
    }
    return targetElem;
  }
});

try {
	Document.prototype.loadXML = function(s) {
	   var doc2 = (new DOMParser()).parseFromString(s, "text/xml");
	   while (this.hasChildNodes())
	      this.removeChild(this.lastChild);
	
	   for (var i = 0; i < doc2.childNodes.length; i++) {
		  this.appendChild(this.importNode(doc2.childNodes[i], true));
	   }
	};
}
catch (err) { 
}
