
/* UTIL *****************************************************************/

//return an element by id, or object reference
function getElement(obj){
	if (typeof(obj) == "string"){
		
		if (document.getElementById == undefined || !document.getElementById)
		return false;
	
		obj = document.getElementById(obj);	
	}
	
	if (obj == undefined || !obj)
		return false;
	
	return obj;
}

function toggle (obj, displayType, state){
	if (displayType == null || displayType == undefined)
		displayType = "block";

	obj = getElement(obj);

	if (!obj)
		return false;

	if (state != null && state != undefined){
		if (state)
			obj.style.display = displayType;
		else
			obj.style.display = "none";

	}else{
		if (!obj.style.display || obj.style.display == "none")
			obj.style.display = displayType;
		else
			obj.style.display = "none";
	}
}

function show(obj, displayType){
	toggle(obj, displayType, true);
}

function hide(obj, displayType){
	toggle(obj, displayType, false);
}

//set focus on the specified element
function setFocus(obj){
	obj = getElement(obj);
	
	if (exists(obj))
		obj.focus();
		
}

//take an object, or get an object with the given id, and set it's css class (if no class_name is specified, then restore the element's original class)
function setClass (obj, class_name){
	obj = getElement(obj);
	
	if (!obj)
		return false;
	
	if (!exists(class_name)){
		if (!exists(obj.originalClassName))
			obj.originalClassName = "";
		
		obj.className = obj.originalClassName;
	}else{
		obj.originalClassName = obj.className;
		obj.className = class_name;
	}
	
	return true;
}

//returns the event object
function getEvent(e){
	if (exists(e))
		return e;
	else if (exists(window.event))
		return window.event;	
	else
		return false;
}

//cancel bubbling (propagation) of events (call from an event handler, such as onclick="cancelEvent(event);")
function cancelEvent(e){
	
	e = getEvent(e);
	
	if (!exists(e))
		return false;
	
	if (exists(e.stopPropagation))
		e.stopPropagation();
	
	if (exists(e.preventDefault))
		e.preventDefault();
		
	//if (exists(e.cancelBubble))
		e.cancelBubble = true;
	
	//if (exists(e.returnValue))
		e.returnValue = false;	
}

//debug
function trace(msg, clear){
	var console = getElement("traceConsole");
	
	if (!console){
		//create console element
		console = document.createElement("div");
		console.id = "traceConsole";
		console.style.position = "absolute";
		console.style.overflow = "scroll";
		console.style.whiteSpace = "pre";
		console.style.textAlign = "left";
		console.style.fontSize = "10px";
		console.style.zIndex = 100000;
		console.style.width = "300px";
		console.style.height = "100px";
		console.style.backgroundColor = "#FFF";
		console.style.color = "#000";
		console.style.opacity = .8;
		if (exists(console.style.filter))
			console.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=80)"
		
		console.style.left = getScrollX()+"px";
		console.style.top = getScrollY()+"px";
		
		var consoleClick = function(){ console.innerHTML=""; };
		var pageScroll = function(){ console.style.left = getScrollX()+"px"; console.style.top = getScrollY()+"px"; };
		
		//set events
		registerEvent(console, "click", consoleClick);
		registerEvent(document.body, "scroll", pageScroll);
		window.onscroll = pageScroll;
		
		console.innerHTML = "";
		document.body.appendChild(console);
	}
	
	if (typeof(msg) == "undefined")
		clear = true;
	
	if (clear)
		msg = "";
	else
		msg = msg + "<br/>" + console.innerHTML;
		
	console.innerHTML = msg;
}

function getScrollX(){
	if (exists(window.pageXOffset))
		return window.pageXOffset;
	else if (exists(document.documentElement) && exists(document.documentElement.scrollLeft))
		return document.documentElement.scrollLeft;
	else if (exists(document.body.scrollLeft))
		return document.body.scrollLeft;
	else 
		return 0;
}

function getScrollY(){
	if (exists(window.pageYOffset))
		return window.pageYOffset;
	else if (exists(document.documentElement) && exists(document.documentElement.scrollTop))
		return document.documentElement.scrollTop;
	else if (exists(document.body.scrollTop))
		return document.body.scrollTop;
	else 
		return 0;
}

function htmlentities(input, breakTagsIntoLines){
	input = input.replace(new RegExp("&", "g"), "&amp;");
	
	input = input.replace(new RegExp("<", "g"), "&lt;");
	
	if (breakTagsIntoLines == true)
		input = input.replace(new RegExp(">", "g"), "&gt;<br/>");
	else
		input = input.replace(new RegExp(">", "g"), "&gt;");
	
	return input;
}

function registerEvent(obj, event, callback, capture){
	if (!exists(obj) || !exists(event) || !exists(callback))
		return false;
		
	if (typeof(capture) == "undefined")
		capture = true;
	
	if (exists(obj.attachEvent))
		return obj.attachEvent("on"+event, callback);
	else if (exists(obj.addEventListener))
		return obj.addEventListener(event, callback, capture);
	else
		return false;
}

function unregisterEvent(obj, event, callback, capture){
	if (!exists(obj) || !exists(event) || !exists(callback))
		return false;
	
	if (typeof(capture) == "undefined")
		capture = true;
	
	if (exists(obj.detachEvent))
		return obj.detachEvent("on"+event, callback);
	else if (exists(obj.removeEventListener))
		return obj.removeEventListener(event, callback, capture);
	else
		return false;
}

/* TESTS/CHECKS *****************************************************************/

//return true if the browser supports the specified flash version (or greater)
function hasFlash (version){
	
	if (!exists(navigator) || !exists(navigator.plugins) || !exists(navigator.mimeTypes))
		return false;
	
	if (navigator.plugins.length > 0){ //test firefox, netscape, etc
	
		var plugin = navigator.plugins["Shockwave Flash"];
		
		if (!exists(plugin) || parseInt(plugin.description.substr(plugin.description.lastIndexOf(".")-2)) < version)
			return false;
		
	}else if (exists(ActiveXObject)){ //test IE
		
		var e, flashVersion;
		
		try { 
			var axObj = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
			flashVersion = axObj.GetVariable("$version");
		} catch (e) {
			return false;
		}
		
		if (!flashVersion)
			return false;
			
		flashVersion = parseInt(flashVersion.substr(flashVersion.indexOf(" "), flashVersion.indexOf(",") - flashVersion.indexOf(" ")));
		
		if (flashVersion < version)
			return false;
			
	}else
		return false;
	
	return true;
}

//return true if a variable exists (is defined)
function exists (obj){
	if (typeof (obj) != "undefined" && obj != null)
		return true;
		
	return false;
}

//return true if the current browser is IE (or with similar javascript capabilities)
function isIE (){
	
	if (exists(document) && exists(document.body) && exists(document.body.attachEvent) && exists(document.body.setCapture)){ //other unique IE properties: document.body.style.filter
		return true;
	}
	
	return false;
}


//returns internet explorer version or false if it is another browser
function getIEVersion(){
	if (!isIE())
		return false;
	
	if (!exists(navigator) || !exists(navigator.userAgent) || !exists(RegExp))
		return false;
	
	if (navigator.userAgent.match(/MSIE\s+([0-9]{1,}[\.0-9]{0,})/))
		return parseFloat(RegExp.$1);
	
	return false;
}





/* GAME SPECIFIC *****************************************************************/

//hilite a row on mouse over
function rowOver(obj){
	//if this is ie <= 6 quit, since there are problems when setting classes
	var version = getIEVersion();
	if(version && version <= 6)
		return false;
	
	obj = getElement(obj);
	
	if (!obj)
		return false;
		
	if (obj.className.indexOf("row_over") >= 0)
		return false;
	
	setClass (obj, obj.className + " row_over");
}

//restore the row's previous class on mouse out
function rowOut(obj){
	obj = getElement(obj);
	
	if (!obj)
		return false;
		
	if (obj.className.indexOf("row_over") < 0)
		return false;
		
	setClass (obj);
}

//mark a row as active (or inactive if the value is 0)
function rowActive(row, input){
	
	input = getElement(input);
	
	if (!input)
		return false;
	
	if (parseInt(input.value) > 0)
		setClass (row, "row_active");
	else
		setClass (row, "");
}


//emulate a button click
function buttonClick(url, target, e){
	if (!exists(url) || url == "")
		return false;
	
	if (exists(e) && (e.ctrlKey || e.button == 1))
		if (target == "")
			target = "_blank";
	
	if (exists(target) && target != "")
		window.open (url, target);
	else
		document.location = url;
		
	if (exists(e))
		cancelEvent(e);
}

//change class of an object on mouse over
function buttonOver(obj, class_name){
	
	if (!class_name || class_name == "" || class_name == undefined)
		class_name = "button button-over";
	
	setClass (obj, class_name);
}

//change class of an object on mouse out
function buttonOut(obj){
	setClass (obj);
}
