function getElementsByName(name){
	var temp = document.all;
    	var matches = [];
    	for(var i=0;i<temp.length;i++){
	    	if(temp[i].name == name){
			matches.push(temp[i]);
	      	}
    	}
	return matches;
}

function hasClass(ele,cls) {
	return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
}
function addClass(ele,cls) {
	if (!this.hasClass(ele,cls)) ele.className += " "+cls;
}
function removeClass(ele,cls) {
	if (hasClass(ele,cls)) {
		var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
		ele.className=ele.className.replace(reg,' ');
	}
}

function addEvent(obj, evType, fn){ 
	if (obj.addEventListener){
		obj.addEventListener(evType, fn, false);
		return true;
	} else if (obj.attachEvent){
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	}
	return false;
}

function removeEvent(obj, evType, fn){
	if (obj.removeEventListener){
		obj.removeEventListener(evType, fn, false);
		return true;
	} else if (obj.detachEvent){
		var r = obj.detachEvent("on"+evType, fn);
		return r;
	}
	return false;
}

function getClientWidth() {
	var bWidth;
	if (window.innerWidth) {
		return (window.innerWidth);
	} else if (document.documentElement && document.documentElement.clientWidth != 0) {
		return (document.documentElement.clientWidth);
	} else if (document.body){
		return (document.body.clientWidth);
	} else {
		var tk = java.awt.Toolkit;
		var size = new java.awt.Dimension(tk.getDefaultToolkit().getScreenSize());
		return (size.width);
	}
} //END GETCLIENTWIDTH()

function getClientHeight() {
	var bHeight;
	if (window.innerHeight) {
		return (window.innerHeight);
	} else if (document.documentElement && document.documentElement.clientWidth != 0) {
		return (document.documentElement.clientHeight);
	} else if (document.body){
		return (document.body.clientHeight);
	} else {
		var tk = java.awt.Toolkit;
		var size = new java.awt.Dimension(tk.getDefaultToolkit().getScreenSize());
		return (size.height);
	}
} //END GETCLIENTHEIGHT ()

// Function mainSetSize(incoming)
// Sets the size of the main body div
// Incoming: passed div element

function mainSetSize(incoming, size) {
	var div = null;
	if (typeof incoming == 'string') { div = document.getElementById(incoming); }
	else { div = incoming; }
	var bWidth = getClientWidth();
	var bHeight = getClientHeight();
	if (size != null) {
		div.style.width = size;
		div.style.left = "0px";
		div.style.top = "141px";
	} else {
		div.style.width = (bWidth - 301) + "px";
		div.style.left = "301px";
	}

	div.style.height = (bHeight - 141) + "px";
}

// Function menuSetSize(incoming)
// Sets the size of the left menu div
// Incoming: passed div element

function menuSetSize(incoming) {
	var div = null;
	if (typeof incoming == 'string') { div = document.getElementById(incoming); }
	else { div = incoming; }
	var bWidth = getClientWidth();
	var bHeight = getClientHeight();
	div.style.width = "300px";
	div.style.height = (bHeight - 181) + "px";
}

function v9_mainSetSize(incoming, size) {
	var div = null;
	if (typeof incoming == 'string') { div = document.getElementById(incoming); }
	else { div = incoming; }
	var bWidth = getClientWidth();
	var bHeight = getClientHeight();
	div.style.width = "100%";
	div.style.left = "0px";
	div.style.height = bHeight + "px";
}

function v9_menuSetSize(incoming) {
	var div = null;
	if (typeof incoming == 'string') { div = document.getElementById(incoming); }
	else { div = incoming; }
	var bWidth = getClientWidth();
	var bHeight = getClientHeight();
	div.style.maxHeight = (bHeight - 181) + "px";
	var temp = document.getElementById("listings");
	if (temp != null) { temp.style.maxHeight = (bHeight - 196) + "px"; }
}

function POST(parent, command, action){
	var request = getAjaxHandle();
	var root 	= document.getElementById(parent);
	var vals	= root.getElementsByTagName("input");
	var urlstr 	= "";
	var url 	= command;
	for(var i=0; i<vals.length; i++){
		if(vals[i].type == "radio"){
			if(vals[i].checked)
				urlstr += vals[i].name + "=" + vals[i].value + "&";
		}else if(vals[i].type == "checkbox"){
			if(vals[i].checked)
				urlstr += vals[i].name + "=" + vals[i].value + "&";
			else
				urlstr += vals[i].name + "=0&";
		}else if(vals[i].type != "button"){
			urlstr += vals[i].name + "=" + vals[i].value + "&";
		}
	}
	var vals = root.getElementsByTagName("textarea");
	for(var i=0; i<vals.length; i++){
		urlstr += vals[i].name + "=" + vals[i].value + "&";
	}
	var vals = root.getElementsByTagName("select");
	for(var i=0; i<vals.length; i++){
		urlstr += vals[i].name + "=" + vals[i].value + "&";
	}
	//alert(urlstr);
	request.onreadystatechange = function(){
		var a = action;
		if(request.readyState == 4){
			//CURRENT_PAGE.debugJS(request.responseText);
			var toDo = "";
			if(request.responseXML != null && !request.responseText.match("S_OK") && !request.responseText.match("E_FAIL")){
				//alert(request.responseXML + "::" + request.responseText);
				a(request.responseXML);
			}else{
				a(request.responseText.substring(2));
			}
		}
	};
	request.open("POST", url , true);
	request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    request.setRequestHeader("Content-length", urlstr.length);
    request.setRequestHeader("Connection", "close");
	request.send(urlstr);
}

function e_POST(parent, command, action){
	var request = getAjaxHandle();
	var root 	= parent;
	var vals	= root.getElementsByTagName("input");
	var urlstr 	= "";
	var url 	= command;
	for(var i=0; i<vals.length; i++){
		if(vals[i].type == "radio"){
			if(vals[i].checked)
				urlstr += vals[i].name + "=" + vals[i].value + "&";
		}else if(vals[i].type == "checkbox"){
			if(vals[i].checked)
				urlstr += vals[i].name + "=" + vals[i].value + "&";
			else
				urlstr += vals[i].name + "=0&";
		}else if(vals[i].type != "button"){
			urlstr += vals[i].name + "=" + vals[i].value + "&";
		}
	}
	var vals = root.getElementsByTagName("textarea");
	for(var i=0; i<vals.length; i++){
		urlstr += vals[i].name + "=" + vals[i].value + "&";
	}
	var vals = root.getElementsByTagName("select");
	for(var i=0; i<vals.length; i++){
		urlstr += vals[i].name + "=" + vals[i].value + "&";
	}
	request.onreadystatechange = function(){
		var a = action;
		if(request.readyState == 4){
			var toDo = "";
			if(request.responseXML != null && !request.responseText.match("S_OK") && !request.responseText.match("E_FAIL")){
				toDo = request.responseXML;
			}else{
				toDo = a + "('" + request.responseText.substring(2) + "');";
			}
			if(request.responseXML){
				a(request.responseXML);
			}else{
				a(request.responseText.substring(2));
			}
		}
	};
	request.open("POST", url , true);
	request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    request.setRequestHeader("Content-length", urlstr.length);
    request.setRequestHeader("Connection", "close");
	request.send(urlstr);
}

function s_POST(params, command, action){
	var request = getAjaxHandle();
	request.onreadystatechange = function(){
		var a = action;
		if(request.readyState == 4){
			if(request.responseXML != null && !request.responseText.match("S_OK") && !request.responseText.match("E_FAIL")){
				a(request.responseXML);
			}else{
				a(request.responseText.substring(2));
			}
		}
	};
	request.open("POST", command , true);
	request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    request.setRequestHeader("Content-length", params.length);
    request.setRequestHeader("Connection", "close");
	request.send(params);
}

function h_POST(params, command, action){
	var request = getAjaxHandle();
	request.onreadystatechange = function(){
		var a = action;
		if(request.readyState == 4){
			a(request.responseText);
		}
	};
	request.open("POST", command , true);
	request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    request.setRequestHeader("Content-length", params.length);
    request.setRequestHeader("Connection", "close");
	request.send(params);
}

function trim(str){
	s = str.replace(/^(\s)*/, '');
	s = s.replace(/(\s)*$/, '');
	return s;
}

function setSelectSelected(select, value, default_val){
	var s;
	if (typeof select == "string") {
		s = getObj(select);
	} else if (select.nodeName == "SELECT") {
		s = select;
	} else { return; }
	var opts 	= s.options;
	var f 		= false;
	if(opts == null)
		return;
	
	var x = 0;
	for(x=0; x<opts.length && opts[x].value!=value; x++){}
	
	if(x<opts.length){
		s.selectedIndex = x;
		f = true;
	}
	if(!f && default_val){
		setSelectSelected(select, default_val);
	}
}

//Taken from quirksmode.org
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}

function loadSearch(){
	try {
		search();
	} catch(e) {
		SEARCH_WAITING = true;
		getPage("search", "searchDiv");	
	}
}

function setBG(object, color) {
	object.style.background = color;
}

//Implementation of getElementById that can be called on an element to get a child node
function getElementById(root, id){
	if(root){
		var y = root.childNodes.length;
		for(var x=0; x<y; x++){
			//If I'm the node return me.
			if(root.childNodes[x].id == id){
				return root.childNodes[x];
			}
			if(root.childNodes[x].hasChildNodes){
				var found = getElementById(root.childNodes[x], id);
				if(found != false){
					return found;
				}
			}
		}
	}
	return false;
}

function nodeListToArray(inc){
	var n_array = new Array();
	var y = inc.length;
	for(var x=0; x<y; x++){
		n_array[x] = inc.item(x);
	}
	return n_array;
}

function attach_script(inc, action){
	var s	= createObj("script");
	s.type	= "text/javascript";
	s.src	= inc;
	var h	= document.getElementsByTagName("head")[0];
	h.appendChild(s);
}

function clean_up_script(inc){
	var s = document.getElementById(inc);
	s.parentNode.removeChild(s);
}

//[0] = return code.
//[1] = message (if there is one empty otherwise)
function parse_return_code(inc){
	var tokens = "";
	var code = 0;
	var return_message = null;
	var error_message = null;
	if(inc.length){//A string response
		tokens = inc.split(":");
		if(tokens[1]){
			code = parseInt(tokens[1]);
		}else{
			code = -1;
		}
		if(tokens[2]){
			return_message = tokens[2];
		}
	}else{
		var info = inc.getElementsByTagName("info");
		info = info[0];
		code = parseInt(info.getAttribute("return_code"));
		if(info.getAttribute("return_message")){
			return_message = info.getAttribute("return_message");
		}
		error_message = info.textContent;
		
	}
	return new Array(code,return_message, error_message);
}

function booleanShift(incoming, offset, length) {
	return ((Math.pow(2, length) - 1) & (incoming >> offset));
}

function displayPO(incoming, headerContent, content) {
	var root 	= document.getElementsByTagName("body")[0];
		root.style.overflow = "hidden";
	var po_div		= createObj("div");
		if (incoming) { po_div.id = incoming + "_po"; }
		else { po_div.id = "disp_po"; }

	var po_header	= createObj("div");
		if (incoming) { po_header.id = incoming + "_po_header"; }
		else { po_header.id = "disp_po_header"; }

		var close	= "<a href='#' class= 'close' onclick=\"close_pm()\">Close</a>";
		var reply	= "<a href='#' class= 'reply' onclick=\"reply_pm(this)\" pm_id='" + pm.getAttribute("pm_id") + "'>Reply</a>";

		var header	= createObj("h1");
			header.innerHTML += close;
			header.innerHTML += reply;
			if (headerContent) { header.innerHTML 	+= headerContent; }
		
		po_header.appendChild(header);
		po_div.appendChild(pm_header);

	var pm_msg		= createObj("div");
		pm_msg.id	= "disp_msg";
		var from	= createObj("p");
		var subject	= createObj("p");
		subject.className = "text";
		var subjectLabel = createObj("label");
		var content	= createObj("p");
		content.className = "text";
		var contentLabel = createObj("label");
		
		from.innerHTML 		= "Sent by " + sender.getAttribute("user_name") + " -- " + pm.getAttribute("pm_timestamp");
		subject.innerHTML	= pm.getAttribute("pm_title");
		subjectLabel.innerHTML = "Subject:";
		content.innerHTML	= pm.getAttribute("pm_content");
		contentLabel.innerHTML = "Content:";

		pm_msg.appendChild(subjectLabel);
		pm_msg.appendChild(subject);
		pm_msg.appendChild(contentLabel);
		pm_msg.appendChild(content);
		pm_msg.appendChild(from);
		po_div.appendChild(pm_msg);
	
	po_div.style.width = (getClientWidth() / 2) + "px";
	po_div.style.left 		= (getClientWidth() / 4) + "px";
	po_div.style.top 		= (getClientHeight() / 4) + "px";

	var po_background = createObj("div");
	po_background.id = "po_background";
	po_background.innerHTML = "&nbsp;";

	root.appendChild(pm_background);
	if(document.getElementById("disp_po")){ root.removeChild(document.getElementById("disp_po")); }		
	root.appendChild(po_div);
}

function getObj(inc) { return document.getElementById(inc); }
function createObj(inc) { return document.createElement(inc); }

function toggleDiv(incoming, outgoing) {
	if (outgoing !=null) {
		if (typeof outgoing === 'object' && outgoing instanceof Array) {
			for (var x=0;x<outgoing.length;x++) { getObj(outgoing[x]).style.display = "none"; }
		} else { getObj(outgoing).style.display = "none"; }
	}
	if (incoming!=null) {
		if (typeof incoming === 'object' && incoming instanceof Array) {
			for (var x=0;x<incoming.length;x++) { getObj(incoming[x]).style.display = "block"; }
		} else { getObj(incoming).style.display = "block"; }
	}
}

function toggleMenu(inc){
	var li = inc.parentNode;
	if(li.className.match(/open/i)) li.className = "closed";
	else li.className = "open";
}


function getUrlVars() {
	var vars = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	for(var i = 0; i < hashes.length; i++) {
		hash = hashes[i].split('=');
		vars.push(hash[0]);
		vars[hash[0]] = hash[1];
	}
	return vars;
}
function getDOMText(incoming) {
	if (incoming.text) { return incoming.text; }
	else if (incoming.textContent) { return incoming.textContent; } 
	else { return ""; }
}

function NDModal(id, params) {
	if(!id || typeof id != "string") return;
	
	if(!getObj(id)) {
		this.main = createObj('div');
		this.main.id = id;
		this.main.className="overlay";
		document.body.appendChild(this.main);
		
		this.close = createObj('div');
		this.close.className = "close";
		this.main.appendChild(this.close);
		
		this.header = createObj('div');
		this.header.className = "header";
		this.main.appendChild(this.header);
		
		this.guts = createObj('div');
		this.guts.className = "guts";
		this.main.appendChild(this.guts);
	} else {
		this.main = getObj(id);
		var divs = this.main.getElementsByTagName("div");
		for (var x = 0; x < divs.length; x++) {
			switch(divs[x].className) {
			case "close":
				this.close = divs[x];
				break;
			case "header":
				this.header = divs[x];
				break;
			case "guts":
				this.guts = divs[x];
				break;
			}
		}
		if (!this.header) {
			this.header = createObj('div');
			this.header.className = "header";
			this.main.appendChild(this.header);
		}
		if (!this.close) {
			this.close = createObj('div');
			this.close.className = "close";
			this.main.appendChild(this.close);
		}
		if (!this.guts) {
			this.guts = createObj('div');
			this.guts.className = "guts";
			this.main.appendChild(this.guts);
		}
	} 
	
	if (!params) return this;
	this.setStyles(params);
}

NDModal.prototype.setStyles = function(params) {
	if (typeof params == "string") {
		var temp = params.split(":");
		if (temp.length == "2") { this.setStyle(temp[0], temp[1]); }
	} else {
		for (var style in params) {
			this.setStyle(style, params[style]);
		}
	}
};

NDModal.prototype.setStyle = function(param, value) {
	if (!param) return;
	switch (param) {
	case "height": this.main.style.height = value; break;
	case "width": this.main.style.width = value; break;
	case "visibility": this.main.style.visibility = value; break;
	case "display": this.main.style.display = value; break;
	case "position": this.main.style.position = value; break;
	case "top": this.main.style.top = value; break;
	case "bottom": this.main.style.bottom = value; break;
	case "left": this.main.style.left = value; break;
	case "right": this.main.style.right = value; break;
	case "border": this.main.style.border = value; break;
	case "border-radius": this.main.style.MozBorderRadius = value; this.main.style.WebkitBorderRadius = value; this.main.style.borderRadius = value; break;
	case "line-height": this.main.style.lineHeight = value; break;
	case "clear": this.main.style.clear = value; break;
	case "margin": this.main.style.margin = value; break;
	case "padding": this.main.style.padding = value; break;
	case "color": this.main.style.color = value; break;
	case "float": this.main.style.cssFloat = value; break;
	}
};

NDModal.prototype.clear = function(inc) { if (!inc) return; if(inc.hasChildNodes()) { while(inc.childNodes.length > 0) inc.removeChild(inc.firstChild); }};

NDModal.prototype.setHeader = function(inc) {
	debug("Function call: Set header------------------------------------------------------->");
	debug("-----Typeof inc: " + typeof inc);
	if (!inc) return;
	var current = this.header.getElementsByTagName("h2");
	debug("-----Current H2 elements: " + current.length);
	if (current.length > 0) current = current[0];
	else current = null;
	if (typeof inc == "string") {
		if (current) {
			var images = current.getElementsByTagName("img");
			if (images.length > 0) {
				debug("-----Existing image");
				debug("----------saving original image and appending before new text.");
				var img = images[0];
				current.innerHTML = "";
				current.appendChild(img);
				current.innerHTML += inc;
			} else {
				debug("-----Setting current H2 innerHTML");
				current.innerHTML = inc;
			}
		} else {
			debug("-----Creating new H2");
			var h2 = createObj("h2");
			h2.innerHTML = inc;
			this.header.appendChild(h2);
		}
	} else if (inc.nodeName) {	
		debug("-----A DOM Node has been found!");
		debug("----------DOM Node Type:" + inc.nodeName);
		if (current) {
			switch (inc.nodeName) {
			case "H2":
				debug("-----Existing H2");
				debug("-----Setting current H2 innerHTML");
				current.innerHTML = inc.innerHTML;
				break;
			case "IMG":
				var img = current.getElementsByTagName("img"); 
				if (img.length > 0) {
					debug("-----Existing Header Image");
					img[0].setAttribute("src", inc.getAttribute("src"));
					debug("---------Reset Header Image SRC");
					img[0].setAttribute("alt", inc.getAttribute("alt"));
					debug("---------Reset Header Image ALT");
				} else {
					debug("-----No existing image DOM Image");
					var text = current.innerHTML;
					debug("---------Created cloned text node");
					current.innerHTML = "";
					debug("---------Reset current innerHTML");
					current.appendChild(inc);
					debug("---------Appended image");
					current.innerHTML += text;
					debug("---------Appended original text");
				}
			}
		} else {
			switch(inc.nodeName) {
			case "H2":
				this.header.appendChild(inc);
				break;
			case "IMG":
				var h2 = createObj("h2");
				this.header.appendChild(h2);
				h2.appendChild(inc);
			}
		}
	}
};

NDModal.prototype.setClose = function (inc) {
	this.clear(this.close);
	var img = createObj("img");
	if (!inc) img.setAttribute("src", "/search/img/bubble_close-hover.png");
	else img.setAttribute("src", inc);
	img.setAttribute("alt","Close");
	img.setAttribute("onclick", "closeModal('" + this.main.id + "')");
	this.close.appendChild(img);
};
//NDModal.prototype.clearClose = function() { if(this.close.hasChildNodes()) { while(this.close.childNodes.length > 0) this.close.removeChild(this.close.firstChild); }};

NDModal.prototype.setGuts = function(inc) {
	this.clear(this.guts);
	if (!inc) return;
	if (typeof inc == "string") {
		var p = createObj("p");
		p.innerHTML = inc;
		this.guts.appendChild(p);
	} else if (inc.nodeType) { 
		this.guts.appendChild(inc);	
	}
};

NDModal.prototype.show = function() {
	this.setStyles({visibility: "visible", display: "block"});
};

NDModal.prototype.hide = function() {
	this.setStyles({visibility: "hidden", display: "none"});
};

NDModal.prototype.die = function() {
	document.body.removeChild(this.main);
	this.close = null;
	this.header = null;
	this.guts = null;
	this.main = null;
	return null;
};

NDModal.prototype.setMovable = function(inc) {
	if (inc !== undefined || inc === false) {
		
	} else {
		
	}
};


function openModal(inc, x, y) {
	var width = getClientWidth();
	var height = getClientHeight();
	var overlayBG = getObj("overlayBG");
	overlayBG.style.visibility = "visible";
	overlayBG.style.display = "block";
	var overlay = getObj(inc);
	overlay.style.visibility = "visible";
	overlay.style.display = "block";
	if (x != null) overlay.style.width = x + "px";
	if (y != null) overlay.style.height = y + "px";
	var objWidth = overlay.offsetWidth;
	var objHeight = overlay.offsetHeight;
	overlay.style.left = ((width / 2) - (objWidth /2)) + "px";
	overlay.style.top = ((height / 2) - (objHeight /2)) + "px";
}

function closeModal(inc) {
	var obj = getObj(inc); 
	obj.style.visibility = "hidden";
	obj.style.display = "none";
	var bg = getObj("overlayBG");
	if (bg) {
		bg.style.visibility = "hidden";
		bg.style.display = "none";
	}
}

function setModalSize(inc,x,y) {
	var overlay = getObj(inc);
	if (!overlay) return;
	
}

function debug(msg) {
    if (window.console && window.console.log) {
        window.console.log(msg);
    }
}

function removeChildren(inc){ if (!inc) return; if (inc.hasChildNodes()) { while (inc.childNodes.length >= 1){ inc.removeChild(inc.firstChild ); }}}
