/* ----------------------------------------------------------------------------
	Events & Initialisation
---------------------------------------------------------------------------- */
function manageEvents(sType, oObject, sEvent, sFunction) {
	if (oBrowser.IE)
		if (sType == "add")
			oObject.attachEvent("on" + sEvent , sFunction);
		else
			oObject.detachEvent("on" + sEvent , sFunction);
	else
		if (sType == "add")
			oObject.addEventListener(sEvent, sFunction, false);
		else
			oObject.removeEventListener(sEvent, sFunction, false);
};

function detectBrowser() {
	var sUserAgent = navigator.userAgent.toLowerCase();
	this.sUserAgent = sUserAgent;
	
	// Browser Type
	this.IE = ((sUserAgent.indexOf("msie") != -1) && (sUserAgent.indexOf("opera") == -1));
	this.Firefox = (sUserAgent.indexOf("firefox") != -1);
	this.Safari = (sUserAgent.indexOf("mac") != -1 && sUserAgent.indexOf("safari") != -1);
	this.Opera = (sUserAgent.indexOf("opera") != -1);
	
	// IE version
	if (this.IE) this.IEVersion = parseInt(parseFloat(navigator.appVersion.toLowerCase().split("msie")[1]));
	
	// Operating System
	this.Win   = (sUserAgent.indexOf("win") != -1);
	this.Win32 = (this.Win && (sUserAgent.indexOf("95") != -1 || sUserAgent.indexOf("98") != -1 || sUserAgent.indexOf("nt") != -1 || sUserAgent.indexOf("win32") != -1 || sUserAgent.indexOf("32bit") != -1));
	this.Mac   = (sUserAgent.indexOf("mac") != -1);
};
var oBrowser = new detectBrowser();


/* ----------------------------------------------------------------------------
	Namespaces
---------------------------------------------------------------------------- */
if (typeof Deepend == "undefined") Deepend = new Object;

Deepend.namespace = function(eNS) {
	if (!eNS || !eNS.length) return null;
	
	var aNames = eNS.split(".");
	var oNS = Deepend;
	
	for (var i = (aNames[0] == "Deepend" ? 1 : 0); i < aNames.length; i++) {
		oNS[aNames[i]] = oNS[aNames[i]] || new Object;
		oNS = oNS[aNames[i]];
	}
	
	return oNS;
};


/* ----------------------------------------------------------------------------
	Prototypes
---------------------------------------------------------------------------- */
// HTMLElement in Safari
if (!window.HTMLElement && /Safari/.test(navigator.userAgent)) {
	var HTMLElement = {};
	HTMLElement.prototype = document.createElement("div").__proto__;
}

// insertAdjacentElement, insertAdjacentHTML, insertAdjacentText and removeNode
if (typeof HTMLElement != "undefined" && !HTMLElement.prototype.insertAdjacentElement) {
	HTMLElement.prototype.insertAdjacentElement = function(position, parsedNode) {
		switch (position) {
			case "beforeBegin": this.parentNode.insertBefore(parsedNode,this); break;
			case "afterBegin": this.insertBefore(parsedNode, this.firstChild); break;
			case "beforeEnd": this.appendChild(parsedNode); break;
			case "afterEnd":
				if (this.nextSibling)
					this.parentNode.insertBefore(parsedNode, this.nextSibling);
				else
					this.parentNode.appendChild(parsedNode);
				break;
		}
	};
	HTMLElement.prototype.insertAdjacentHTML = function (position, sHTML) {
		var eRange = this.ownerDocument.createRange();
		eRange.setStartBefore(this);
		var parsedHTML = eRange.createContextualFragment(sHTML);
		this.insertAdjacentElement(position, parsedHTML)
	};
	HTMLElement.prototype.insertAdjacentText = function (position, sText) {
		var parsedText = document.createTextNode(sText)
		this.insertAdjacentElement(position, parsedText)
	};
	HTMLElement.prototype.removeNode = function(removeChildren) {
		if (Boolean(removeChildren))
			return this.parentNode.removeChild(this);
		else {
			var eRange = document.createRange();
			eRange.selectNodeContents(this);
			return this.parentNode.replaceChild(eRange.extractContents(), this);
		}
	};
}

// implementation of srcElement non-IE browsers
if (oBrowser.Firefox) {
	Event.prototype.__defineGetter__("srcElement", function() {
		var eElement = this.target;
		return eElement;
	});
}

// implementation of selectNodes/selectSingleNodes for FireFox
if (document.implementation.hasFeature("XPath", "3.0")) {
	XMLDocument.prototype.selectNodes = function(cXPathString, xNode) { 
		if (!xNode) xNode = this;
		var oNSResolver = this.createNSResolver(this.documentElement);
		var aItems = this.evaluate(cXPathString, xNode, oNSResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null) 
		var aResult = [];
		for (var i = 0; i < aItems.snapshotLength; i++)
			aResult[i] = aItems.snapshotItem(i);
		return aResult;
	};
	Element.prototype.selectNodes = function(cXPathString) {
		if (this.ownerDocument.selectNodes) return this.ownerDocument.selectNodes(cXPathString, this);
	};
	
	XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode) { 
		if (!xNode) xNode = this;
		var xItems = this.selectNodes(cXPathString, xNode);
		if (xItems.length > 0)
			return xItems[0];
		else
			return null;
	};
	Element.prototype.selectSingleNode = function(cXPathString) {
		if (this.ownerDocument.selectSingleNode) return this.ownerDocument.selectSingleNode(cXPathString, this);
	};
	
	// Opera 9 fix
	if (typeof XMLDocument == "undefined") XMLDocument = Document;
}

// implementation of custom function for finding text nodes
/*if (!window.Element) var Element = {};
Element.extend = function(element) {
	return new Object();
};
Element.prototype.__defineGetter__("selectTextNode", function() {
	oNode = this;
	do { oNode = oNode.firstChild; }
	while (oNode.nodeType != 3);
	return oNode.nodeValue;
});*/


/* ----------------------------------------------------------------------------
	Helpers
---------------------------------------------------------------------------- */
function getRan() {
	var ran = new Date();
	ran = ran.getDate() +""+ ran.getMonth() +""+ ran.getYear() +""+ ran.getHours() +""+ ran.getMinutes() +""+ ran.getSeconds() +""+ ran.getMilliseconds();
	return ran;
};

function focusLogin() {
	document.getElementById("UsernameTextbox").focus();
};

function trace(sOutput) {
	var eTrace;
	var sClose = "<div id=\"close\" style=\"position:absolute;top:-1px;right:-1px;background-color:#000;font-size:9px;\">"
		+ "<a href=\"javascript:void(0);\" onclick=\"this.parentNode.parentNode.removeNode(true)\" style=\"color:#FFF;text-decoration:none;\">&nbsp;X&nbsp;</a>"
		+ "</div>";
	
	if (document.getElementById("close")) document.getElementById("close").removeNode(true);
	
	if (document.getElementById("trace"))
		eTrace = document.getElementById("trace");
	else {
		eTrace = document.createElement("div");
		with (eTrace) {
			setAttribute("id", "trace");
			style.position = "absolute";
			style.top = "30px";
			style.right = "20px";
			style.zIndex = "500";
			style.fontSize = "10px";
			style.backgroundColor = "#FFF";
			style.lineHeight = "15px";
			style.border = "1px solid #000";
			style.padding = "14px 10px 10px 10px";
		}
		document.body.appendChild(eTrace);
	}
	eTrace.innerHTML = eTrace.innerHTML + sOutput + "<br />" + sClose;
};