
/* Hacking around in an attempt to fix various browser incompatibilities (mainly for the buggy IE). {{{ */

/* Attempt to add this to Opera and Safari */
if(typeof(Array)!=undefined && Array.prototype && Array.prototype.indexOf==undefined) {
	Array.prototype.indexOf=function(e,start) {
		var i;
		for(i=(start==null)?0:start;i<self.length;i++) {
			if(self[i]==e) {
				return(i);
			}
		}
		return(-1);
	}
}

function ie_kludge(name,func,dummy) {	/* for AddEventListener() support in IE. */
	eval("this.on"+name+"=function (e) {e=window.event;e.getParentByTag=_pbt;e.target=e.srcElement;if(e.keyCode){e.which=e.keyCode;}return(func(e));}");
}

function ie_fixup(o) {
	if(o!=null) {
		if(!o.addEventListener && o.attachEvent) {
			o.addEventListener = ie_kludge;
		}
		if(!o.getParentByTag) {
			o.getParentByTag=_pbt;
		}
	}
}

/*}}}*/

/* And a useful extension. {{{ */
if((navigator.userAgent.toLowerCase().indexOf("msie")==-1) && (Object!=undefined) && (Object.prototype!=undefined)) {
	if(!Object.prototype.getParentByTag) {
		Object.prototype.getParentByTag = function (tag) {
			var o=this;
			while(o!=null && o.tagName!=tag) {
				o=o.parentNode;
			}
			return(o);
		}
	}
} else {
	function _pbt(tag) {
		var o=this;
		while(o!=null && o.tagName!=tag) {
			o=o.parentNode;
		}
		return(o);
	}
}

/*}}}*/

/*
 * Retrieves an elements by its id attribute and applies browser fixups.
 */
function getById(id)
{
	var o=null;
	if(document.getElementById) {
		o=(document.getElementById(id));
	} else if(document.all) {
		o=(document.all[id]);
	} else if(document.layers) {
		o=(document.layers[id]);
	}

	ie_fixup(o);
	return o;
}

/*
 * Retrieves the first direct child with the specified tag or "def" if not found.
 */
function getFirstByTag(o,t,def) {	
	var i;
	for(i=0;i<o.childNodes.length;i++) {
		if(o.childNodes[i].tagName==t) {
			ie_fixup(o.childNodes[i]);
			return(o.childNodes[i]);
		}
	}
	return(def);
}

/*
 * Append an element to another and returns the newly created element.
 */
function EAppend(parent,name)
{
var o=document.createElement(name);
if(o) {
	ie_fixup(o);
	parent.appendChild(o);
} else {
	alert("Failed to create "+name);
}
return(o);
}

/*
 * Appends a text-node with the specified text to an object.
 */
function TAppend(parent,text)
{
	parent.appendChild(document.createTextNode(text));
}

/* Convert a date object to a "legible" string. */
function datestr(date)
{
var y,m,d,h,min;
y=date.getFullYear();
m=date.getMonth()+1;
d=date.getDate();
h=date.getHours();
min=date.getMinutes();
return(y+"-"+((m<10)?"0":"")+m+"-"+((d<10)?"0":"")+d+((h<10)?" 0":" ")+h+((min<10)?":0":":")+min);
}

/* This is the primitive function underlying the getList() and getRecord() functions. */
function GET_URL(url) {
	var req=null;
	try {
		if(window.XMLHttpRequest) {
			req=new XMLHttpRequest();
		} else if(window.ActiveXObject) {
			req=new ActiveXObject("Microsoft.XMLHTTP");
		}
	} catch(e) {
		alert("Error: "+e);	/* Error */
	}
	if(req) {
		try {
			req.open("GET",url,false);
			req.send("");
			if(req.readyState!=4) {
				req=null;
			}
		} catch(e) {
			alert("Asynchronous error: "+e);
			req=null;
		}
	}
	return(req);
}

function POST_URL(url,content) {
	var req=null;
	try {
		if(window.XMLHttpRequest) {
			req=new XMLHttpRequest();
		} else if(window.ActiveXObject) {
			req=new ActiveXObject("Microsoft.XMLHTTP");
		}
	} catch(e) {
		alert("Error: "+e);	/* Error */
	}
	if(req) {
		try {
			req.open("POST",url,false);
			req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			req.setRequestHeader("Content-Length",content.length);
			req.send(content);
			if(req.readyState!=4) {
				req=null;
			}
		} catch(e) {
			alert("Asynchronous error: "+e);
			req=null;
		}
	}
	return(req);
	
}

/*
 * Helper function to recursively parse XML records.
 */
function utility_parseRecord(xml) {
	var i,node=null,o;
	node=xml.getAttributeNode("array");
	if(node && node.value=="true") {
		o=new Array();
		for(i=0;i<xml.childNodes.length;i++) {
			node=xml.childNodes[i];
			if(node.tagName=="item") {
				o.push(utility_parseRecord(node));
			}
		}
	} else if(xml.childNodes.length==1 && xml.firstChild.nodeType==3/*Node.TEXT_NODE*/) {	/* Text node */
		o=xml.firstChild.nodeValue;
	} else if(xml.childNodes.length==0) {	/* Empty node */
		o=null;
	} else {
		o=new Object();
		for(i=0;i<xml.childNodes.length;i++) {
			node=xml.childNodes[i];
			if(node.nodeType==1 /*Node.ELEMENT_NODE*/) {
				o[node.tagName]=utility_parseRecord(node);
			}
		}
	}
	return(o);
}

function getRecord(url) {
	var r,req,l,i;
	r=new Object();
	req=GET_URL(url);
	if(req && req.status==200 && req.responseXML) {
		r=utility_parseRecord(req.responseXML.getElementsByTagName("record")[0]);
	} else {
		r=null;
	}
	return(r);
}

function getText(url) {
	var req,text;
	req=GET_URL(url);
	if(req && req.responseText) {
		text=req.responseText;
	} else {
		text="";
	}
	return(text);
}

function getList(url) {
	var req,list,i,l,o;
	req=GET_URL(url);
	if(req && req.responseXML) {
		/* IE test fix - this is needed for explorer when reading a local file. */
		if (!req.responseXML.documentElement && req.responseStream) {
			req.responseXML.load(req.responseStream);
		}
		list=utility_parseRecord(req.responseXML.getElementsByTagName("list")[0]);
	} else {
		list=new Array(0);
	}
	return(list);
}

/* Utility functions for handling <select></select> lists. */

function clear_all(list) {
	while(list.firstChild!=null) {
		list.removeChild(list.firstChild);
	}
}

function append_list(o,l,selected,item,vitem,itemstyle) {
	var opt,val,text,i;
	var gtext,gval,gstyle;

	if(typeof(item)=="undefined")	{
		gtext=function(x){return(x);};
	} else {
		if(typeof(item)!="function") {
			gtext=new Function("x","return(x[\""+item+"\"])");
		} else {
			gtext=item;
		}
	}

	if(typeof(vitem)=="undefined")	{
		gval=function(x){return(x);};
	} else {
		if(typeof(vitem)!="function") {
			gval=new Function("x","return(x[\""+vitem+"\"])");
		} else {
			gval=vitem;
		}
	}

	if(typeof(itemstyle)=="undefined") {
		gstyle=function(){return(null);};
	} else {
		if(typeof(itemstyle)!="function") {
			gstyle=new Function("return(\""+itemstyle+"\");");
		} else {
			gstyle=itemstyle;
		}
	}

	if(l!=null && o!=null) {
		for(i=0;i<l.length;i++) {
			opt=document.createElement("option");
			text=gtext(l[i],i);
			if(text==null) {
				continue;
			}
			val=gval(l[i],i);
			opt.setAttribute("value",val);
			if(arguments.length>=3 && selected==val) {
				opt.setAttribute("selected",true);
			}
			opt.setAttribute("class",gstyle(l[i],i));
			TAppend(opt,text);
			o.appendChild(opt);
		}
	}
}

/* Retrieves the value of an input element and encodes it in a form suitable for an URL. */
function BuildFormRequest(base) {
	var s,o,i;
	s=base;
	if(arguments.length>1) {
		s+="?";
		for(i=1;i<arguments.length;i+=2) {
			if(arguments[i]!=null) {
				o=getById(arguments[i]);
				if(o!=null && (o.value!=null || o.type=="checkbox")) {
					if(i>1) {
						s+="&";
					}
					if(o.type=="checkbox") {
						s+=arguments[i+1]+"="+((o.checked)?"true":"false");
					} else {
						s+=arguments[i+1]+"="+encodeURIComponent(o.value);
					}
				}
			} else {
				if(i>1) {
					s+="&";
				}
				s+=arguments[i+1];
			}
		}
	}
	return(s);
}

function clear_inputs() {
	var i;
	for(i=0;i<arguments.length;i++) {
		getById(arguments[i]).value="";
	}
}

function clear_table(t) {
	var o,head=null;
	o=t.getElementsByTagName("tbody");
	if(o!=null && o[0]!=undefined) {
		head=o[0].firstChild;
		t.removeChild(o[0]);
	}
	t=EAppend(t,"tbody");
	if(head!=null) {
		t.appendChild(head);
	}
	return(t);
}

function updateCookie(name,value,expires,path) {
	var cs,ename,i;
	var v,s;
	var date;
	ename=name+"=";
	cs=document.cookie;
	v=cs.match(new RegExp(ename+"[^;]*"));
	if(v!=null && v.length>0) {
		v=v[0].substring(ename.length);
	} else {
		v="";
	}

	if(arguments.length>1) {	/* If setting the cookie */
		if(value==null) {
			value=v;
		}
		if(expires==null) {
			expires=31536000;
		}
		if(path==null) {
			path="/";
		}
		date=new Date();
		date.setTime(date.getTime()+expires*1000);
		s=ename+value+";expires="+date.toGMTString()+";path="+path;
		document.cookie=s;
	}

	return(v);
}

/* Utility function to attach to a link for help texts. */

function utility_do_popup(evt,name,url,w,h) {
	evt.preventDefault && evt.preventDefault();
	window.open(url,name,"width="+w+",height="+h+",innerWidth="+w+",innerHeight="+h+",scrollbars=yes,resizable=yes,location=no");
	return(false);
}

function show_help(evt) {
	var link;
	link=evt.target.getParentByTag("A");
	return(utility_do_popup(evt,"HelpWindow",link.href,400,500));
}

function std_popup(evt) {
	var link;
	link=evt.target.getParentByTag("A");
	return(utility_do_popup(evt,"PopWindow",link.href,400,500));
}


