
var isOpera = navigator.userAgent.indexOf("Opera") > -1;
var isSafari = navigator.userAgent.indexOf("Safari") > -1;
var isFirefox = navigator.userAgent.indexOf("Firefox") > -1 && navigator.userAgent.indexOf("Windows") > -1;
var isFirefoxMac = navigator.userAgent.indexOf("Firefox") > -1 && navigator.userAgent.indexOf("Mac") > -1;
var isExplorer = navigator.userAgent.indexOf("MSIE") > -1;
var isExplorer6 = navigator.userAgent.indexOf("MSIE 6") > -1;
var isExplorer7 = navigator.userAgent.indexOf("MSIE 7") > -1;

function convertToObject(id) {
	return typeof(id) == "string" ? document.getElementById(id) : id;
}

var fireOnObj;
function fireEventOnObject(obj, relatedObj, eventName) {
	obj = convertToObject(obj);
	relatedObj = convertToObject(relatedObj);
	
	if (fireOnObj == relatedObj) {
		fireOnObj = null;
		return;
	}
	
	fireOnObj = obj;
	
	if (isExplorer) obj.fireEvent(eventName);
	else {
		var evt = document.createEvent("MouseEvents");
		if (eventName.substring(0, 2) == "on") {
			eventName = eventName.substring(2);
		}
		evt.initMouseEvent(eventName, true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
		obj.dispatchEvent(evt);
	}
}

function getCookie(name) {
	if (document.cookie.length > 0) {
		var index = document.cookie.indexOf(name + "=")
		if (index != -1) { 
			index = index + name.length + 1;
			ends = document.cookie.indexOf(";", index)
			if (ends == -1) ends= document.cookie.length;
			return unescape(document.cookie.substring(index, ends))
		} 
	}
	return null;
}

function getFileExtension(path) {
	return path.substring(path.lastIndexOf("."), path.length);
}

function getOffsetLeftFromBody(obj) {
	obj = convertToObject(obj);
	if (typeof(obj) == "undefined" || obj == null) return 0;
	var temp = obj;
	var result = temp.offsetLeft;
	if (temp.tagName == "TABLE" && temp.align == "center") result = 0; //this needs to be looked at in detail
	while (temp.offsetParent != null) {
		temp = temp.offsetParent;
		result += temp.offsetLeft;
	}
	return result;
}

function getOffsetTopFromBody(obj) {
	obj = convertToObject(obj);
	if (typeof(obj) == "undefined" || obj == null) return 0;
	var temp = obj;
	var result = temp.offsetTop;
	while (temp.offsetParent.tagName != 'BODY') {
		temp = temp.offsetParent;
		result += temp.offsetTop;
	}
	return result;
}

function getOffsetParentOfType(obj, tagName) {
	if (obj == null) return null;
	if (typeof(obj) == 'undefined') return null;
	if (typeof(obj.tagName) == 'undefined') return null;
	while (obj.parentNode != null) {
		if (obj.parentNode.tagName.toUpperCase() == tagName.toUpperCase()) return obj.parentNode;
		obj = obj.parentNode;
	}
	return null;
}

function getRectangle(obj) {
	if (typeof(obj) == 'undefined') return null;
	if (typeof(obj.tagName) == 'undefined') return null;
	var ix = getOffsetLeftFromBody(obj);
	var iy = getOffsetTopFromBody(obj);
	var iw = obj.offsetWidth;
	var ih = obj.offsetHeight;
	return { x:ix, y:iy, width:iw, height:ih };
}

function hookupEvent(control, eventType, functionString, isPrefix) {
	if (typeof(isPrefix) == "undefined") isPrefix = true;
	var ev;
	eval("ev = control." + eventType + ";");
	if (typeof(ev) == "function") {
		ev = ev.toString();
		ev = ev.substring(ev.indexOf("{") + 1, ev.lastIndexOf("}"));
	}
	else {
		ev = "";
	}
	
	var func;
	if (navigator.appName.toLowerCase().indexOf('explorer') > -1) {
		if (isPrefix) func = new Function(functionString + " " + ev);
		else func = new Function(ev + " " + functionString);
	}
	else {
		if (isPrefix) func = new Function("event", functionString + " " + ev);
		else func = new Function("event", ev + " " + functionString);
	}
	eval("control." + eventType + " = " + func);
}

function registerNonIEFocusSetActiveElement() {
	if (!isExplorer) {
		document.addEventListener("focus", setActiveElementHandler, false);
	}
}
function registerNonIEClickSetActiveElement() {
	if (!isExplorer) {
		document.addEventListener("click", setActiveElementHandler, false);
	}
}

function setCookie(name, value, expires) {
	if (typeof(expires) == 'undefined') {
		expires = new Date();
		expires.setDate(expires.getDate() + 365);
	}
	document.cookie = name + "=" + escape(value) + ";expires=" + expires.toGMTString();
}

function setOpacity(obj, opacity) {
	var obj1 = convertToObject(obj);
	obj1.opacity = opacity;
	if (isFirefox || isSafari) obj1.style.opacity = opacity/100;
	else if (isExplorer) obj1.style.filter = "alpha(opacity="+opacity+")";
}

function swapImage(imageObj, imageUrl) {
	imageObj = convertToObject(imageObj);
	if (typeof(imageUrl) == "undefined") {
		imageUrl = imageObj.originalSrc;
	}
	else {
		if (typeof(imageObj.originalSrc) == "undefined") {
			imageObj.originalSrc = imageObj.src;
		}
	}
	imageObj.src = imageUrl;
}

function setActiveElementHandler(event) {
	document.activeElement = (event.target.nodeType == Node.TEXT_NODE) ? event.target.parentNode : event.target;
}

function setObjectFloat(obj, value) {
	if (isExplorer) obj.style.styleFloat = value;
	else obj.style.cssFloat = value;
}

var Tracer = function(width, height, left, top) {
	this.width = width || 500;
	this.height = height || 500;
	this.left = left || (document.body.offsetWidth - this.width - 23);
	this.top = top || (document.body.offsetHeight - this.height - 13);
	
	this.body = document.createElement('div');
	this.body.style.border = "#000 1px solid";
	this.body.style.clear = "both";
	this.body.style.padding = "2px";
	this.body.style.width = this.width + 'px';
	this.body.style.height = this.height + 'px';
	this.body.style.position = 'absolute';
	this.body.style.top = this.top + 'px';
	this.body.style.left = this.left + 'px';
	
	this.output = document.createElement('div');
	this.output.style.color = "#f00";
	this.output.style.textAlign = "left";
	this.output.style.width = (this.width - 4) + 'px';
	this.output.style.height = (this.height - 22) + 'px';
	this.output.style.overflow = 'scroll';
	
	var btn = document.createElement('button');
	btn.output = this.output;
	btn.innerHTML = "Clear";
	btn.onclick = function() { this.output.innerHTML = ''; }
	
	this.body.appendChild(btn);
	this.body.appendChild(this.output);
	document.body.appendChild(this.body);
}
Tracer.prototype.setWidth = function(value) {
	this.width = value;
	this.body.style.width = this.width;
}
Tracer.prototype.setWidth = function(value) {
	this.height = value;
	this.body.style.height = this.height;
}
Tracer.prototype.writeLine = function(message) {
	var div = document.createElement('DIV');
	div.innerHTML = message;
	this.body.childNodes[1].appendChild(div);
	div.scrollIntoView()
}