// Manifesto: $Id: ajaxdom.js,v 1.33 2007/01/08 19:16:24 spud Exp $
// license: GNU LGPL
// copyright 2001-2004: dada typo and contributors

Function.prototype.bind = function() {
  var __method = this, object = arguments[0];
  return function() {
    return __method.apply(object);
  }
}
function getObjectRef(doc,id) {
	if (typeof id == "string") {
		var obRef;
		if (document.getElementById) {
			obRef = doc.getElementById(id);
		} else if (document.all) {
			obRef = doc.all[id];
		}
		if (obRef != null && obRef != undefined) return obRef;
	} else if (typeof id == "object") {
		return id;
	}
	return false;
}
function closeParent(obj) {
	if (obj.parentNode && obj.parentNode.parentNode) {
		obj.parentNode.parentNode.removeChild(obj.parentNode);
	}
	return false;
}
function optionBlock(i) {
	var imag;
	dtDOM.showHide(i);
	if (imag = getObjectRef(document,i + "box")) {
		if (imag.src == js_g_url+"images/disclosure_right.gif") {
			imag.src = js_g_url+"images/disclosure_down.gif";
		} else {
			imag.src = js_g_url+"images/disclosure_right.gif";
		}
	}
	return false;
}
function DOMobj() {
	var ua = navigator.userAgent;
	this.isMSIE = (navigator.appName == "Microsoft Internet Explorer");
	this.isMSIE5 = this.isMSIE && (ua.indexOf("MSIE 5") != -1);
	this.isMSIE5_0 = this.isMSIE && (ua.indexOf("MSIE 5.0") != -1);
	this.isGecko = ua.indexOf("Gecko") != -1;
	this.isSafari = ua.indexOf("Safari") != -1;
	this.isOpera = ua.indexOf("Opera") != -1;
	this.isMac = ua.indexOf("Mac") != -1;
	this.isNS7 = ua.indexOf("Netscape/7") != -1;
	this.isNS71 = ua.indexOf("Netscape/7.1") != -1;
	this.g_url = "";
	this.doc = document;
}
dtDOM = new DOMobj();
dtDOM.g_url = js_g_url;
DOMobj.prototype.getAttr = function(obj,prop) {
	if (prop == "class" && this.isMSIE) {
		prop = "className";
	}
	if (obj[prop]) {
		return obj[prop];
	} else {
		if (this.isMSIE) {
			return obj.getAttribute(prop);
		} else {
			if (obj.hasAttribute(prop)) {
				return obj.getAttribute(prop);
			} else {
				return "";
			}
		}
	}
}
DOMobj.prototype.setAttr = function(obj,prop,value) {
	if (prop == "class" && this.isMSIE) {
		prop = "className";
	}
	return obj.setAttribute(prop,value);
}
DOMobj.prototype.removeAttr = function(obj,prop) {
	if (prop == "class" && this.isMSIE) {
		prop = "className";
	}
	return obj.removeAttribute(prop);
}
DOMobj.prototype.setHandler = function(obj,h,f) {
	if (this.isMSIE) {
		obj[h] = new Function(f);
	} else {
		dtDOM.setAttr(obj,h,f);
	}
};
DOMobj.prototype.addEvent = function(obj,ev,func) {
	if (this.isMSIE) {
		obj.attachEvent("on"+ev,func);
	} else {
		obj.addEventListener(ev,func,false);
	}
};
DOMobj.prototype.removeEvent = function(obj,ev,func) {
	if (this.isMSIE) {
		obj.detachEvent("on"+ev,func);
	} else {
		obj.removeEventListener(ev,func,false);
	}
};
DOMobj.prototype.getStyle = function(obj,prop) {
	var value = null;
	if (prop == "opacity" && obj.filters) {
		value = 1;
		try {
			value = obj.filters.item("DXImageTransform.Microsoft.Alpha").opacity/100;
		} catch(e) {
			try {
				value = obj.filters.item("alpha").opacity/100;
			} catch(e) {
				value = 1.0;
			}
		}
	} else if (obj.style[prop]) {
		value = obj.style[prop];
	} else if (obj.currentStyle && obj.currentStyle[prop]) {
		value = obj.currentStyle[prop];
	} else if (document.defaultView && document.defaultView.getComputedStyle) {
		thecss = document.defaultView.getComputedStyle(obj,null);
		if (thecss != null) {
			value = thecss.getPropertyValue(prop);
		}
	}
	return value;
};
DOMobj.prototype.setStyle = function(obj,prop,value) {
	try {
		if (typeof prop == "object") {
			for (var n in prop) {
				switch(n) {
					case "opacity":
						if (obj.filters) {
							obj.style.filter = "alpha(opacity="+prop[n]*100+")";
							if (!obj.currentStyle.hasLayout) obj.style.zoom = 1;
						} else {
							obj.style.opacity = prop[n];
							obj.style["-moz-opacity"] = prop[n];
							obj.style["-khtml-opacity"] = prop[n];
						}
						break;
					case "float":
						if (dtDOM.isMSIE) {
							obj.style["styleFloat"] = prop[n];
						} else {
							obj.style["cssFloat"] = prop[n];
						}
						break;
					default:
						obj.style[n] = prop[n];
				}
			}
		} else {
			switch(prop) {
				case "opacity":
					if (obj.filters) {
						obj.style.filter = "alpha(opacity="+value*100+")";
						if (!obj.currentStyle.hasLayout) obj.style.zoom = 1;
					} else {
						obj.style.opacity = value;
						obj.style["-moz-opacity"] = value;
						obj.style["-khtml-opacity"] = value;
					}
					break;
				case "float":
					if (dtDOM.isMSIE) {
						obj.style["styleFloat"] = value;
					} else {
						obj.style["cssFloat"] = value;
					}
					break;
				default:
					obj.style[prop] = value;
			}
		}
	}
	catch(e) {
		//alert("Object "+obj+" could not set "+prop+" to "+value);
	}
};
DOMobj.prototype.removeStyle = function(obj,prop) {
	if (obj.style.removeProperty) {
		obj.style.removeProperty(prop);
	} else {
		obj.style[prop] = "";
	}
};
DOMobj.prototype.hasClass = function(obj,value) {
	if (typeof obj == "string") obj = getObjectRef(this.doc,obj);
	var cclass = this.getAttr(obj,"class");
	if (cclass.indexOf(value) == -1) {
		return false;
	}
	return true;
};
DOMobj.prototype.addClass = function(obj,value) {
	if (typeof obj == "string") obj = getObjectRef(this.doc,obj);
	var cclass = this.getAttr(obj,"class");
	if (cclass == value) return;
	if (cclass.indexOf(value) == -1) {
		this.setAttr(obj,"class",cclass+" "+value);
	}
};
DOMobj.prototype.removeClass = function(obj,value) {
	if (typeof obj == "string") obj = getObjectRef(this.doc,obj);
	var cclass = dtDOM.getAttr(obj,"class");
	var cpos = cclass.indexOf(value);
	if (cpos == -1) {
		return;
	} else if (cpos == 0) {
		dtDOM.setAttr(obj,"class",cclass.replace(value+" ",""));
	} else {
		dtDOM.setAttr(obj,"class",cclass.replace(" "+value,""));
	}
};
DOMobj.prototype.toggleClass = function(obj,class1,class2) {
	if (typeof obj == "string") obj = getObjectRef(this.doc,obj);
	var eclass = this.getAttr(obj,"class");
	if (eclass == class1) {
		this.setAttr(obj,"class",class2);
	} else if (eclass == class2) {
		this.setAttr(obj,"class",class1);
	}
};
DOMobj.prototype.swapClass = function(obj,class1,class2) {
	if (typeof obj == "string") obj = getObjectRef(this.doc,obj);
	var eclass = this.getAttr(obj,"class");
	if (eclass != "") {
		this.setAttr(obj,"class",eclass.replace(class1,class2));
	}
};
DOMobj.prototype.getEventTarget = function(e) {
	if (e.srcElement) {
		return e.srcElement;
	} else if (e.target) {
		return e.target;
	}
	return null;
}
DOMobj.prototype.showHide = function(obj,force) {
	if (typeof obj == "string") obj = getObjectRef(this.doc,obj);
	var disp = dtDOM.getStyle(obj,"display");
	if (this.isSafari && disp == null) {
		disp = "";
		if (dtDOM.hasClass(obj,"hidden")) dtDOM.removeClass(obj,"hidden");
	}
	if (force) {
		if (this.isMSIE) {
			if (obj.nodeName == "TR" && force == "table-row") {
				force = "block";
			} else if (obj.nodeName == "TBODY" && force == "table-row-group") {
				force = "block";
			}
		}
		obj.style.display = force;
	} else if (disp != "none" && disp != "") {
		obj.style.display = "none";
	} else {
		switch(obj.nodeName) {
			case "TABLE":
				obj.style.display = "table";
			case "TBODY":
				if (this.isMSIE) {
					obj.style.display = "block";
				} else {
					obj.style.display = "table-row-group";
				}
				break;
			case "TR":
				if (this.isMSIE) {
					obj.style.display = "block";
				} else {
					obj.style.display = "table-row";
				}
				break;
			case "A":
			case "SPAN":
			case "IMG":
				obj.style.display = "inline";
				break;
			default:
				obj.style.display = "block";
		}
	}
	return false;
};
DOMobj.prototype.selectNode = function(win,elem) {
	if (this.isMSIE) {
	} else {
		var sel = window.getSelection();
		var rng = window.document.createRange();
		rng.selectNode(elem);
		sel.addRange(rng);
	}
	return false;
};
DOMobj.prototype.getParentArray = function(node) {
	var pArr = new Array();
	do {
		pArr.push(node);
	} while ((node = node.parentNode) != null && node.nodeType == 1 && node.nodeName != "HTML");
	return pArr;
};
DOMobj.prototype.seekParentElement = function(node,elem) {
	if (typeof elem == "undefined") {
		if (node.nodeType == 1) return node;
		while ((node = node.parentNode) != null && node.nodeType != 1) { }
		return node;
	} else if (node == null) {
		return null;
	} else if (node.nodeType == 1 && node.nodeName == elem) {
		return node;
	}
	while (node = node.parentNode) {
		if (node.nodeType == 1 && node.nodeName == elem) {
			return node;
		} else if (node.nodeName == "BODY") {
			return null;
		}
	}
	return null;
};
DOMobj.prototype.seekChildElement = function(node,elem) {
	if (node.hasChildNodes()) {
		for(var i=0;i<node.childNodes.length;i++) {
			if (node.childNodes[i].nodeName == elem) {
				return node.childNodes[i];
			} else if (node.childNodes[i].hasChildNodes()) {
				var r = this.seekChildElement(node.childNodes[i],elem);
				if (r != null) {
					return r;
				}
			}
		}
	}
	return null;
};
DOMobj.prototype.br = function() {
	return this.buildElement("BR",false,false);
}
DOMobj.prototype.buildElement = function(type,id,attrs) {
	if (arguments.length == 4) {
		var elem = arguments[3].createElement(type);
	} else {
		var elem = this.doc.createElement(type);
	}
	if (typeof id != "undefined" && id != "" && id != false) this.setAttr(elem,"id",id);
	if (attrs) {
		for(var i in attrs) {
			this.setAttr(elem,i,attrs[i]);
		}
	}
	return elem;
};
DOMobj.prototype.buildInputElement = function(type,name,id,value,arr) {
	if (typeof id == "undefined" || id == false || id == "") id = name;
	var elem = this.buildElement("INPUT",id, { "type": type, "name": name, "value": value });
	if (arr) {
		for (var n in arr) {
			this.setAttr(elem,n,arr[n]);
		}
	}
	return elem;
};
DOMobj.prototype.buildRadioElement = function(type,name,id,value,arr) {
	if (typeof id == "undefined" || id == false || id == "") id = name;
	if (this.isMSIE) {
		var elem = null;
		try {
			if (arr.checked == "checked") {
				elem = document.createElement('<input type="'+type+'" name="'+name+'" checked="checked">');
			} else {
				elem = document.createElement('<input type="'+type+'" name="'+name+'">');
			}
		} catch (e) {
			elem = document.createElement(type);
			elem.setAttribute("name",name);
		}
	} else {
		return this.buildInputElement(type,name,id,value,arr);
	}
	elem.setAttribute("id",id);
	elem.setAttribute("value",value);
	if (arr) {
		for (var n in arr) {
			this.setAttr(elem,n,arr[n]);
		}
	}
	return elem;
};
DOMobj.prototype.buildLinkElement = function(href,inside,arr) {
	var elem = this.buildElement("A",false, { "href": href });
	if (arr) {
		for (var n in arr) {
			this.setAttr(elem,n,arr[n]);
		}
	}
	if (typeof inside == "object") {
		elem.appendChild(inside);
	} else {
		elem.appendChild(document.createTextNode(inside));
	}
	return elem;
};
DOMobj.prototype.setSelectByValue = function(obj,value) {
	if (typeof obj == "string") obj = getObjectRef(this.doc,obj);
	for(var i=0;i<obj.options.length;i++) {
		if (obj.options[i].value == value) {
			obj.selectedIndex = i;
			break;
		}
	}
}
DOMobj.prototype.getSelectedOption = function(obj) {
	if (typeof obj == "string") obj = getObjectRef(this.doc,obj);
	return obj.options[obj.selectedIndex].value;
}
DOMobj.prototype.addOnload = function(f) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = f;
	} else {
		window.onload = function() {
			if (oldonload) oldonload();
			f();
		}
	}
}
function toggleProgbar() {
	var progbar = getObjectRef(document,"progbar");
	if (progbar.style.display == "block" || arguments[0] == 'off') {
		progbar.style.display = "none";
	} else {
		progbar.style.display = "block";
	}
}
function get_XMLHttpReq() {
	if (dtDOM.isMSIE) {
		try {
			httpObj = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			httpObj = new ActiveXObject("Microsoft.XMLHTTP");
		}
	} else {
		httpObj = new XMLHttpRequest();
	}
	return httpObj;
}
function AJAXobj(baseurl) {
	this.obj = new get_XMLHttpReq();
	this.baseurl = baseurl;
	this.method = "get";
	this.args = ""
	this.reqHeader = "";
	this.rHandler = false;
	this.responsetext = "";
	this.responseCount = 0;
	this.response = null;
	this.error = "";
	this.fielddelim = ";";
	this.linedelim = "|";
}
AJAXobj.prototype.dosend = function(args,rtype) {
	toggleProgbar();
	this.args = args;
	this.error = "";
	switch (rtype) {
		case 'boolean':
			this.obj.onreadystatechange = this.booleanResponse.bind(this);
			break;
		case 'text':
			this.obj.onreadystatechange = this.textResponse.bind(this);
			break;
		case 'array':
			this.obj.onreadystatechange = this.arrayResponse.bind(this);
			break;
		case 'object':
			this.obj.onreadystatechange = this.JSONResponse.bind(this);
			break;
	}
	if (this.method == "get") {
		this.obj.open("get",this.baseurl+"?"+args);
		this.obj.send(null);
	} else {
		this.obj.open("post",this.baseurl);
		this.obj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		this.obj.send(args);
	}
}
AJAXobj.prototype.textResponse = function() {
	if (this.obj.readyState == 4) {
		toggleProgbar();
		if (this.obj.status == 200) {
			this.response = this.obj.responseText;
		} else {
			this.error = "This script has failed to successfully contact the server.";
		}
		this.rHandler();
	}
}
AJAXobj.prototype.arrayResponse = function() {
	if (this.obj.readyState == 4) {
		toggleProgbar();
		if (this.obj.status == 200) {
			this.responsetext = this.obj.responseText;
			if (this.responsetext.indexOf(this.linedelim) != -1) {
				this.response = new Array();
				var linearr = this.responsetext.split(this.linedelim);
				this.responseCount = 0;
				for(i=0;i<linearr.length;i++) {
					if (linearr[i] != "") {
						var fieldarr = linearr[i].split(this.fielddelim);
						this.response[this.response.length] = fieldarr;
						this.responseCount++;
					}
				}
			} else if (this.responsetext != 0) {
				this.error = this.responsetext;
			}
		} else {
			this.error = "This script has failed to successfully contact the server.";
		}
		this.rHandler();
	}
}
AJAXobj.prototype.booleanResponse = function() {
	if (this.obj.readyState == 4) {
		toggleProgbar();
		if (this.obj.status == 200) {
			if (parseInt(this.obj.responseText) == NaN) {
				this.error = this.obj.responseText;
			} else {
				this.response = parseInt(this.obj.responseText);
			}
		} else {
			this.error = "This script has failed to successfully contact the server.";
		}
		this.rHandler();
	}
}
AJAXobj.prototype.JSONResponse = function() {
	if (this.obj.readyState == 4) {
		toggleProgbar();
		if (this.obj.status == 200) {
			if (this.obj.responseText == "") {
				this.error = "The request returned an empty object";
			} else {
				theObject = eval("(" + this.obj.responseText + ")");
				this.response = theObject;
			}
		} else {
			this.error = "This script has failed to successfully contact the server.";
		}
		this.rHandler();
	}
}
function sndUserCheck(username) {
	if (username != "") {
		uhttp = new AJAXobj(dtDOM.g_url+"rpc_reference.php");
		uhttp.rHandler = useralert;
		uhttp.dosend("t=name&str="+username,"boolean");
	}
	return false;
}
function useralert() {
	var userfield = getObjectRef(document,"form_newusername");
	if (uhttp.error != "") {
		alert(uhttp.error);
	} else if (uhttp.response) {
		window.alert("That username is already in use by a registered account. Please select another.");
		userfield.focus();
	}
}
function sndArbitraryUserCheck(fieldobj,username,menu) {
	if (username != "") {
		aufieldobj = fieldobj;
		aumenu = menu;
		uhttp = new AJAXobj(dtDOM.g_url+"rpc_reference.php");
		uhttp.rHandler = arbitraryuseralert;
		uhttp.dosend("t=name&str="+username,"boolean");
	}
	return false;
}
function arbitraryuseralert() {
	var menuobj = getObjectRef(document,aumenu);
	if (uhttp.error != "") {
		alert(uhttp.error);
	} else if (uhttp.response) {
		window.alert("That username is already in use by a registered account. Please select another.");
		aufieldobj.focus();
	} else {
		menuobj.selectedIndex = 0;
	}
}
