/* Subversion: $Id: claero.js 139 2006-06-21 19:41:47Z rhorton $ */
/*
	claero.js
	davidfmiller
	http://www.claero.com
	july 16, 2004

	the following utility functions are
	
	changelog:
		july 16 2004: - created
		feb  11 2005: - added getByClassName() function
*/



/*---------------------------------------------------------------------------
								constants
-----------------------------------------------------------------------------*/

// dom node types
ATTRIBUTE_NODE = 2;
CDATA_SECTION_NODE = 4;
COMMENT_NODE = 8;
DOCUMENT_FRAGMENT_NODE = 11;
DOCUMENT_NODE = 9;
DOCUMENT_TYPE_NODE = 10;
ELEMENT_NODE = 1;
ENTITY_NODE = 6;
ENTITY_REFERENCE_NODE = 5;
NOTATION_NODE = 12;
PROCESSING_INSTRUCTION_NODE = 7;
TEXT_NODE = 3;

// xml request states
UNINITIALIZED = 0;
LOADING = 1;
LOADED = 2;
INTERACTIVE = 3;
COMPLETE = 4;

// server responses
var OK = 200;
var NOT_FOUND = 404;

// add other classes that shouldn't have their background colours over-ridden here:
var noStripe = new Array("multipleHeader", "blah");

/*---------------------------------------------------------------------------
								convenience functions
-----------------------------------------------------------------------------*/

function flipAffectedRecords() {
  var id = 'affectedRecords';
    var target = get(id);
    if (! target) { return false; }

    if (target.style.display == "none") {
        show(id);
        setText('affectedAction', "hide");
    } else {
        hide(id);
        setText('affectedAction', "view");
    }

    return false;
}



// escapes a string
function encode(s) {

	var l = s.length;
	var cooked = "";
	for (var i = 0; i < l; i++) {
		var c = s.charAt(i);
		switch (c) {
			case '\'':
				cooked += "&apos;";
				break;
			case '<':
				cooked += "&lt;";
				break;
			case '>':
				cooked += "&gt;";
				break;
			case '&':
				cooked += "&amp;";
				break;
			case "\"":
				cooked += "&quot;";
				break;
			default:
				if (c >= 128) {
					cooked += "&#" + (s.charCodeAt(i)) + ";";
				}
				break;
		}
	}	
	return cooked;
}


// if target is visible, then it becomes hidden;
// if target is hidden, it becomes visible
function hideAndSeek(target) {

	var t = get(target);
	if (! t) { return false; }
	
	if (t.style.display == "") {
		t.style.display = "none";
	} else {
		t.style.display = "";
	}	
	return true;
}

// makes a node visible

// makes a node visible
function show(target) {

	var t = get(target);
	if (! t) { return false; }

	t.style.display = "";
	return true;
}

// hides a node
function hide(target) {

	var t = get(target);
	if (! t) { return false; }

	target.style.display = "none";
	
	return true;
}


// hides all the children of a node
function hideChildren(target) {

	var t = get(target);
	if (! t) { return false; }

	var children = t.childNodes;

	// loop through all the children of this node and hide them
	for (var i = 0; i < children.length; i++) {
		if (children[i].nodeType == ELEMENT_NODE) { 
			children[i].style.display = "none";
		}
	}

	return true;
}


// sets the text of a node (or its first child)
function setText(id, words) {
	var e = get(id);
	if (! e) { return false; }

	if (e.nodeType == TEXT_NODE) {
		e.nodeValue = words;
	} else {
		while (e.childNodes.length > 0) {
			e.removeChild(e.childNodes[0]);
		}
		e.appendChild(text(words));		
	}
	
	return true;
}


// reverses order of a node's children
function reverseChildren(id) {

	var parent = get(id);
	if (! parent) { return false; }
	
	var children = parent.childNodes;
	var nodes = Array();
	var count = children.length;

	for (var i = 0; i < count; i++) {
		nodes[i] = children.item(0);
		parent.removeChild(children[0]);
	}
	for (var i = nodes.length - 1; i >= 0; i--) {
		parent.appendChild(nodes[i]);
	}
	
	return true;
}

function duplicate(tag, zid) {

	var e = get(tag);
	if (! e) { return false; }

	if (e.nodeType == TEXT_NODE) {
		var d = text(e.nodeValue);
		return d;
	} else {

		var d = document.createElement(e.tagName);

		if (e.hasAttributes()) {
			attrs = e.attributes;
			for (var i = 0; i < attrs.length; i++) {
				var av = e.getAttribute(attrs[i].nodeName);
				var a = attrs[i].nodeName;
				if (a == 'id' && arguments.length == 2) {
					var r = new RegExp(":i:", "g");
					av = av.replace(r, zid);
				} 
				d.setAttribute(a, av);
			}
		}

		if (e.hasChildNodes()) {
			var count = e.childNodes.length;
			for (var i = 0; i < count; i++) {
				
				if (arguments.length == 2) {
					var d2 = duplicate(e.childNodes[i], zid);
				} else {
					var d2 = duplicate(e.childNodes[i]);
				}
				d.appendChild(d2);	
			}
		} 
	}
	return d;
}

// disables a form element
function disable(element, enabled) {

	if (arguments.length == 1) {
		enabled = false;
	}

	enable(element, enabled);

	return true;
}

// enables a form element
function enable(element, enabled) {

	if (arguments.length == 1) {
		enabled = true;
	}

	var e = get(element);
	if (!e) { return false; }

	var name = e.tagName.toLowerCase();
	if (name == "form") {
		var tags = getByTagName(e, "fieldset");
		if (tags) {
			for (var i = 0; i < tags.length; i++) {
				enable(tags[i], enabled);
			}
		}

		// enable/disable all form elements that are NOT in a fieldset
		var tags = getByTagName(e, ["input", "select", "textarea"]);
		if (tags) {
			for (var i = 0; i < tags.length; i++) {
				disable(tags[i], enabled);
			}
		}
		
	} else if (name == "fieldset") {

		var tags = getByTagName(e, ["input", "select", "textarea"]);
		if (tags) {
			for (var i = 0; i < tags.length; i++) {
				enable(tags[i], enabled);
			}
		}

	} else if (name == "input" || name == "select" || name == "textarea") {

		if (enabled) {
//			if (e.hasAttribute("disabled")) {
				e.removeAttribute("disabled");
//			}
		} else {
			e.setAttribute("disabled", "disabled");
		}
	}

	return true;
}

// get a reference to an element by id
function get(id) {
	if (typeof id == "string") {
		return document.getElementById(id);
	} else {
		return id;
	}
}

// create a text node
function text(chars) {
	return document.createTextNode(chars);
}

// append a child node to its parent
function add(parent, child) {
	var p = get(parent);
	if (!p ) { return false; }
	p.appendChild(node(child));
	return true;
}

// remove a child node from the tree
function snip(child) {
	var c = get(child);
	if (c) {
		var p = c.parentNode;
		if (! p) { return false; }
		p.removeChild(c);
	}
	return c;
}

// removes all child nodes from a parent
function trim(parent) {
	var p = get(parent);
	if (!p) { return false; } 
	while (p.hasChildNodes()) {
		var t = p.firstChild;
		p.removeChild(t);
	}
	
	return true;
}

// returns the first child of tag whose node name is "name",
// or false if no such node exists
function getFirstChildNamed(tag, name) {

    var parent = get(tag);
    var nodes = getByTagName(parent, name);
    
    if (nodes && nodes.length > 0) {
        return nodes[0];
    }

    return false;
    
/*    var children = parent.childNodes;
    for (var i = 0; i < children.length; i++) {
        var child = children[i];
        if (child.nodeType == ELEMENT_NODE && child.nodeName.toUpperCase() == name.toUpperCase()) {
            return child;
        }
    }
  */  
}

function getParentNamed(tag, name, level) {

	if (! level) {
		level = 1;
	}
	var n = get(tag);
	var parent;
	var count = 0;

	while (parent = n.parentNode) {
		if (parent.nodeName.toLowerCase() == name.toLowerCase()) {
			count++;
			if (count == level) {
				return parent;
			}
		}
		n = parent;
	}

	return false;
}

function getByClassName(parent, elementName, className) {

	var tag = false;
	if (arguments.length == 1) {
		tag = document;
		elementName = '*';
		className = arguments[0];
	} else if (arguments.length == 2) {
		tag = document;
		className = elementName;
		elementName = parent;

	} else {
		tag = get(parent);
	}

	if (! tag) { return false; }
	var nodes = new Array();

	var elements = getByTagName(tag, elementName);
	if (! elements) { return false; }

	for (var i = 0; i < elements.length; i++) {
		var c = hasClass(elements[i]);
		if (c && inArray(c.split(' '), className)) {
			nodes[nodes.length] = elements[i];
		}
	}

	return nodes;
}


// retrieves a list of all child tags with the appropriate name
function getByTagName(tag, name) {
	if (arguments.length == 1) {
		return document.getElementsByTagName(tag);
	} else {
		var t = get(tag);
		if (! t) { return false; }
		return t.getElementsByTagName(name);
	}
}

// returns the text value of a text node, or the text value of an element's first 
function getNodeValue(node) {
	var n = get(node);
	if (!n) { return false; }
	
	if (n.nodeType == TEXT_NODE) {
		return n.nodeValue;
	} else if (n.nodeType == ELEMENT_NODE && n.childNodes.length > 0) {
		return getNodeValue(n.firstChild);
	} else {
		return false;
	}
}


// selects all options of a select box (optionally provides a way to de-select)
function selectAll(select, b) {
	var s = get(select);
	if (b == null) { b = true; }
	
	var options = getByTagName(s, "option");
	for (var i = 0; i < options.length; i++) {
		options[i].selected = b;
	}
}

// returns the number of selected options in a <select> element
function countSelected(select) {
	var s = get(select);
	var count = 0;
	
	var options = getByTagName(s, "option");
	for (var i = 0; i < options.length; i++) {
		if (options[i].selected) count++;
	}
	
	return count;
}

// returns an array of the form (selected[id] = value) corresponding to the selected options of a select
function getSelectedValue(select) {

  var s = get(select);
  if (! s) { return false; }
  var count = 0;
  
  var selected = new Array();

  var options = getByTagName(s, "option");
  if (! options) { return false; }
  
  var j = 0; // the number of selected options
  for (var i = 0; i < options.length; i++) {
  	if (options[i].selected) {
  	  selected[j] = options[i].value;
  	  j++;
  	}
  }

  if (j > 0) {
    return selected;
  } else {
    return false;
  }
}

// an alias for getSelectedText
function getSelected(select) {
    return getSelectedText(select);
}


function getSelectedText(select) {

  var s = get(select);
  if (! s) { return false; }
  var count = 0;
  
  var selected = new Array();

  var options = getByTagName(s, "option");
  if (! options) { return false; }
  var j = 0;
  for (var i = 0; i < options.length; i++) {
  	if (options[i].selected) {
  	  selected[options[i].value] = options[i].text;
      j++;
  	}
  }  
  if (j > 0) {
      return selected;
  } else {
    return false;
  }
}


// an alias for removeOptionByValue
function removeOption(select, value) {
	return removeOptionByValue(select, value);
}

// removes all options from a select box
function removeOptions(select) {

	var s = get(select);
	if (! s) { return false; }

	var options = getByTagName(s, "option");
	for (var i = 0; i < s.childNodes.length; i = 0) {
		s.removeChild(s.lastChild);
	}
	return true;
}


// removes an option from a <select> element based on the option's caption/text
function removeOptionByText(select, text) {

	var s = get(select);
	if (! s) { return false; }
	var flag = false;
	
	var options = getByTagName(s, "option");
	for (var i = 0; i < options.length; ) {
		if (getNodeValue(options[i]) == text) {
			s.removeChild(options[i]);
			flag = true;
		} else {
			i++;
		}
	}
	return flag;
}


// removes an option from a <select> element based on the value attribute
function removeOptionByValue(select, value) {
	var s = get(select);
	if (! s) { return false; }
	
	var options = getByTagName(s, "option");
	for (var i = 0; i < options.length; i++) {
		if (options[i].value == value) {
			s.removeChild(options[i]);
			return true;
		}
	}
	return false;
}

// used to sort option elements by their value
function _valueSort(a, b) {
	if (a.value < b.value) { 
		return -1;
	} else if (a.value > b.value) {
		return 1;
	} else { return 0; }
}

// used to sort option elements by their caption
function _captionSort(a, b) {

	if (a.text < b.text) {
		return -1;
	} else if (a.text > b.text) {
		return 1;
	} else { return 0; } 
}

// sorts a select element's options 
function sortOptions(select, f) {
	
	if (f == null) { 
		f = _valueSort;
	}
	
	var s = get(select);
	var options = getByTagName(s, "option");
	var holder = Array();
	for (var i = 0; i < options.length; i++) {
		holder[i] = options[i];
		s.removeChild(options[i]);
	}	
	holder.sort(f);
	for (var i = 0; i < holder.length; i++) {
		s.appendChild(holder[i]);
	}
}	

// gets the value
function getRadioValue(f, name) {
	
	var r = false;
	var t = get(f);
	if (! t) { return false; }
	
	var inputs = getByTagName(t, "input");
	for (var i=0; i < inputs.length; i++) {
		if (inputs[i].type=="radio" && inputs[i].checked) {
            if (name) {
                if (inputs[i].name.toUpperCase() == name.toUpperCase()) {
                    return inputs[i].value
                }
            } else {
                return inputs[i].value;
			}
		}
	}
	
	return false;
}

// gets the text label
function getRadioText(f, name) {

	var t = get(f);
	if (! t) { return false; }
	
	var inputs = getByTagName(t, "input");
	for (var i = 0; i < inputs.length; i++) {
		if (inputs[i].type=="radio" && inputs[i].checked) {
            if (name) {            
                if (inputs[i].name.toUpperCase() == name.toUpperCase()) {
                    // get the label that contains this radio button
                    var label = getParentNamed(inputs[i], "label");
                    return getNodeValue(label);
                }
            } else {
                var label = getParentNamed(inputs[i], "label");
                return getNodeValue(label);
			}
		}
	}
	
	return false;
}


/*---------------------------------------------------------------------------
								common elements
-----------------------------------------------------------------------------*/


// arguments: 
// 	#0 : href
// 	#1 : title
// 	#2 : class
// 	#3 : id
// 	#4 : style
// 	#5 : child element
function a(href, title, c, id, s, onclick, child) {
	
	var a = _createElement("a", c, id, s, child);
	
	// required attributes
	a.setAttribute("href", href);
	a.setAttribute("title", title);

	if (onclick) {
		a.setAttribute("onclick", onclick);
	}
	
	return a;
}


// arguments: 
// 	#0 : src
// 	#1 : alt
// 	#2 : width
// 	#3 : height
// 	#4 : class
// 	#5 : id
// 	#6 : style
function img(src, alt, c, id, s, width, height) {
	
	var img = _createElement("img", c, id, s);
	
	// required attributes
	img.setAttribute("src", arguments[0]);
	img.setAttribute("alt", arguments[1]);
	
	// optional attributes
	if (width != null) { img.setAttribute("width", width); } 
	if (height != null) { img.setAttribute("height", height); } 

	return img;
}

// creates a <p>
function p(t, c, id, s) {
	return _createElement("p", c, id, s, t);
}

// creates a <div>
function div(c, id, s, child) {
	return _createElement("div", c, id, s, child);
}

// creates a <span>
function span(child, c, id, s) {
	return _createElement("span", c, id, s, child);	

}

// creates an <hr>
function hr(c, id, s) {
	return _createElement("hr", c, id, s);
}

// creates a <br>
function br() {
	return document.createElement("br");
}

// creates a <strong>
function strong(t, c, id, s) {
	var e = _createElement("strong", c, id, s);
	if (t != null) {
		e.appendChild(node(t));	
	}
	return e;
}

// creates an <em>
function em(t, c, id, s) {
	var e = _createElement("em", c, id, s);
	if (t != null) {
		e.appendChild(node(t));	
	}
	return e;
}

// creates a <pre>
function pre(t, c, id, s) {
	var e = _createElement("pre", c, id, s);
	if (t != null) {
		e.appendChild(node(t));
	}
}

// creates a <code>
function code(t, c, id, s) {
	var e = _createElement("code", c, id, s);
	if (t != null) {
		e.appendChild(node(t));
	}
}

function dfn(t, c, id, s) {
	var e = _createElement("dfn", c, id, s);
	if (t != null) {
		e.appendChild(node(t));
	}
}

function cite(t, c, id, s) {
	var e = _createElement("cite", c, id, s);
	if (t != null) {
		e.appendChild(node(t));
	}
}

function del(t, cite, datetime, c, id, s) {
	var e = _createElement("del", c, id, s);
	if (t != null) {
		e.appendChild(node(t));
	}
	// TODO
	
}

function ins(t, cite, datetime, c, id, s) {
	var e = _createElement("cite", c, id, s);
	if (t != null) {
		e.appendChild(node(t));
	}
	
	// TODO
}

function blockquote(t, c, id, s) {
	var e = _createElement("quote", c, id, s);
	if (t != null) {
		e.appendChild(node(t));
	}
}

function address(t, c, id, s) {
	var e = _createElement("address", c, id, s);
	if (t != null) {
		e.appendChild(node(t));
	}	
}

function acronym(t, lang, c, id, s) {
	var e = _createElement("address", c, id, s);
	if (t != null) {
		e.appendChild(node(t));
	}
	
	if (lang != null) {
		e.setAttribute("lang", lang);
	}
}

function abbr(t, lang, c, id, s) {
	var e = _createElement("abbr", c, id, s);
	if (t != null) {
		e.appendChild(node(t));
	}	
	if (lang != null) {
		e.setAttribute("lang", lang);
	}
}

/*---------------------------------------------------------------------------
								form elements
-----------------------------------------------------------------------------*/


function checkbox(name, value, checked, onclick, c, id, s) {
	var c = _createElement("input", c, id, s);
	c.setAttribute("type", "checkbox");
	c.setAttribute("name", name);
	
	if (onclick != null) {
		c.setAttribute("onclick", onclick);
	}
	
	if (value != null) {
		c.setAttribute("value", value);
	}
	
	if (checked != null && checked) { 
		c.setAttribute("checked", "checked");
	}
	
	return c;
}

function radio(name, value, checked, onclick, c, id, s) {
	var c = _createElement("input", c, id, s);
	c.setAttribute("type", "radio");
	c.setAttribute("value", value);
	c.setAttribute("name", name);
	if (onclick != null) {
		c.setAttribute("onclick", onclick);
	}
	if (checked != null && checked) { 
		c.setAttribute("checked", "");
	}
	
	return c;
}

function label(t, input, c, id, s) {

	var l = _createElement("label", arguments[2], arguments[3], arguments[4]);
	l.appendChild(node(t));
	l.appendChild(node(input));	
	
	return l;
}

function select(c, id, s, onchange, size, multiple) {
	var s =  _createElement("select", c, id, s);

	if (size != null) {
		s.setAttribute("size", size);
	}
	
	if (multiple) {
		s.setAttribute("multiple", "multiple");
	}
	
	if (onchange != null) {
		s.setAttribute("onchange", onchange);
	}
	
	return s;
}

function button(name, value, onclick, c, id, s) {
	var b = _createElement("input", c, id, s);
	b.setAttribute("type", "button");
	b.setAttribute("name", name);
	b.setAttribute("value", value);
	
	if (onclick != null) {
		b.setAttribute("onclick", onclick);	
	}
	
	return b;
}

function reset(name, value, onclick, c, id, s) {
	var b = _createElement("input", c, id, s);
	b.setAttribute("type", "reset");
	b.setAttribute("name", name);
	b.setAttribute("value", value);
	
	if (onclick != null) {
		b.setAttribute("onclick", onclick);	
	}
	
	return b;
}

function submit(name, value, onclick, c, id, s) {

	var b = _createElement("input", c, id, s);
	b.setAttribute("type", "submit");
	b.setAttribute("name", name);
	b.setAttribute("value", value);
	
	if (onclick != null) {
		b.setAttribute("onclick", onclick);	
	}
	
	return b;
}

function option(value, name) {
	var o = _createElement("option");
	o.appendChild(node(name));
	o.setAttribute("value", value);
	
	return o;
}


function fileInput(name, c, id, s) {
	var f = _createElement("input", c, id, s);
	f.setAttribute("type", "file");
	
	return f;
}

function password(name, value, c, id, s) {
	
	var p = _createElement("input", c, id, s);
	p.setAttribute("type", "password");
	if (value != null) { 
		p.setAttribute("value", value);
	}
	
	return p;
}

function hiddenInput(name, value, id) {
	var e = _createElement("input");
	e.setAttribute("name", name);
	e.setAttribute("type", "hidden");
	if (value != null) { e.setAttribute("value", value); }
	if (id) {
		e.setAttribute("id", id);
	}
	
	return e;
}

function textInput(name, value, maxlength, size, onchange, id) {
	var t = _createElement("input");
	t.setAttribute("name", name);
	t.setAttribute("type", "text");
	t.setAttribute("value", (value != null ? value : ''));

	if (maxlength) {
		t.setAttribute("maxlength", maxlength);
	}
	
	if (size) {
		t.setAttribute("size", size);
	}

	if (onchange) {
		t.setAttribute("onchange", onchange);
	}

	if (id) {
		t.setAttribute("id", id);
	}
	
	return t;
}

function textarea(name, value, rows, cols, c, id, s) {
	var t = _createElement("textarea", c, id, s);
	t.appendChild(node(value));
	t.setAttribute("rows", rows);
	t.setAttribute("cols", cols);
	
	return t;
}


/*---------------------------------------------------------------------------
								table elements
-----------------------------------------------------------------------------*/

function table(c, id, s, spacing) {
	var t = _createElement("table", c, id, s);
	if (spacing != null) {
		t.setAttribute("cellspacing", spacing);
	}
	
	return t;
}

function tbody(c, id, s) {
	return _createElement("tbody", c, id, s);
}

function thead(c, id, s) {
	return _createElement("thead", c, id, s);
}

function tfoot(c, id, s) {
	return _createElement("tfoot", c, id, s);
}

function tr(c, id, s) { 
	return _createElement("tr", c, id, s);
}

function td(c, id, s, t) {
	var e = _createElement("td", c, id, s);
	if (t != null) { e.appendChild(node(t)); } 
	return e;
}

function th(c, id, s, t) {
	var e = _createElement("th", c, id, s);
	if (t != null) { e.appendChild(node(t)); } 
	return e;
}



/*---------------------------------------------------------------------------
								list elements
-----------------------------------------------------------------------------*/

function ol(c, id, s, start) {
	var e = _createElement("ol", c, id, s);
	if (start != null) {
		e.setAttribute("start", start);
	}
	return e;
}

function dl(c, id, s) {
	var e = _createElement("dl", c, id, s);
}

function ul(c, id, s) {

	var e = _createElement("ul", c, id, s);
	return e;
}

function li (child, c, id, s) {
	var l = _createElement("li", c, id, s);
	l.appendChild(node(child));
	
	return l;
}

function dt(child, c, id, s) {
	var l = _createElement("dt", c, id, s);
	l.appendChild(node(child));
	
	return l;
}

function dd(child, c, id, s) {
	var l = _createElement("dd", c, id, s);
	l.appendChild(node(child));
	return l;
}


/*---------------------------------------------------------------------------
								utility functions
-----------------------------------------------------------------------------*/


function node(arg) {

	if (typeof(arg) == "string") {
		return document.createTextNode(arg);
	} else if (typeof(arg) == "number") { 
		return document.createTextNode("" + arg);
	} else {
		return arg;
	}
}

function _createElement(type, c, id, s, child) {
	
	var e = document.createElement(type);
	if (arguments[1] != null) { e.setAttribute("class", c); } 
	if (arguments[2] != null) { e.setAttribute("id", id); }
	if (arguments[3] != null) { e.setAttribute("style", s); }
	if (arguments[4] != null) { e.appendChild(node(child)); }
	
	return e;
}

/*---------------------------------------------------------------------------
						stuff related to form validation
-----------------------------------------------------------------------------*/

// returns the number of checkboxes that are child elements of f and that are checked
function countCheckedBoxes(f, name) {
	var count = 0;
	var children = getByTagName(f, "input");
	for (var i = 0; i < children.length; i++) {
		if (children[i].type == "checkbox" && children[i].checked) {
			if (name) {
				if (children[i].name == name) {
					count++;
				} 
			} else {
				count++;
			}
		}
	}
	
	return count;
}

function show(id) {
	var target = document.getElementById(id);
	if (! target) { return false; }
	target.style.display = "";
}

function hide(id) {
	var target = document.getElementById(id);
	if (! target) { return false; }
	target.style.display = "none";
}

function flip(id) {	
	var target = document.getElementById(id);	
    if (! target) { return false; }
	if (target.style.display == "none") { show(id);}
	else { hide(id); }		
}


function _valueOfArgument(r) {
	return r;
}

function inArray(array, element, func) {

	if (! func) {
		func = _valueOfArgument;
	}

	var l = array.length;
	for (var i = 0; i < l; i++) {
		if (func(array[i]) == func(element)) {
			return true;
		}
	}
	return false;
}

Array.prototype.inArray = 
         function(search_term) {
   for (var i = 0; i < this.length; i++) {
      if (this[i] === search_term) {
         return true;
      }
   }
   return false;
}

function hasClass(obj) {
	var result = false;
	if (obj.getAttributeNode("class") != null) {
		result = obj.getAttributeNode("class").value;
	}
	return result;
}

function selectOption(s, nonZero) {

	if (arguments.length == 1) {
		nonZero = true;
	}

	var sel = get(s);
	if (! sel) { return false; }

	var options = sel.options; // getByTagName(sel, "option");
	if (! options) { return false; }

	if (nonZero && sel.value != 0) {
		return true;
	} else if (! nonZero && sel.value == 0) {
		return true;
	}

	for (var i = 0; i < options.length; i++) {
		if (nonZero && options[i].value != 0) {
			options[i].selected = true;
			return true;
		} else if (! nonZero && options[i].value == 0) {
			options[i].selected = true;
			return true;
		}
	}

	return false;
}


function stripe(id) {

	var even = false;

	var evenColor = arguments[1] ? arguments[1] : "#fff";
	var oddColor = arguments[2] ? arguments[2] : "#eee";

	var table = document.getElementById(id);
	if (! table) { return; }
	
	var tbodies = table.getElementsByTagName("tbody");

	for (var h = 0; h < tbodies.length; h++) {
		var trs = tbodies[h].getElementsByTagName("tr");
		for (var i = 0; i < trs.length; i++) {
	
			if (! trs[i].style.backgroundColor) {
				var tds = trs[i].getElementsByTagName("td");
				for (var j = 0; j < tds.length; j++) {
					var mytd = tds[j];
					if (! mytd.style.backgroundColor && ! inArray(noStripe, hasClass(mytd))) {
						mytd.style.backgroundColor = even ? evenColor : oddColor;
					}
				}
			}
			even = ! even;
		}
	}
}

// close a popup window and refresh it's parent at the same time
function CloseFocus() {
	top.opener.focus();
	window.opener.location.href = window.opener.location;
	parent.close();
}

