// testUtils.js -- A few Javascript DOM-searching functions
// written by David Turover circa 2002-2005. Public domain.
// Everything in here has been eclipsed by JQuery. 
// Use JQuery instead: http://jquery.com/



var jsOkay=0;
var jsHasHttp=0;

var defaultObject;
var defaultParent;


function err(msg){
	alert(msg);
}

function jsInit_internal (){
	jsOkay = 0;
	jsHasHttp = 0; 
	if(document){
		if(document.getElementById){ jsOkay = 1; }
		if(document.xmlHttpRequest){ jsHasHttp = 1; }
	}
	return;
}


// myGet(str) - Wrapper for getElementById that throws an error if the
// passed id does not exist.
function myGet(id){
	if(jsOkay){
		var gotItem = document.getElementById(id);
		if(!gotItem){
			err("getElementById failed to get item " + id);
			jsOkay = 0;
		}
		return gotItem; // item if good, null if bad
	}
	return null;
}


// nattr(node, attribute_name) - Wrapper for node.attributes.getNamedItem
// that does what getNamedItem should do without crashing if passed bad
// data 
function nattr(n, name){
	if(!name){ return null; }
	if(!name.length){ return null; }
	if(n){
		var a = n.attributes;
		if(a){
			if(a.getNamedItem){
				var attrObj = a.getNamedItem(name)
				if(attrObj){
					var value = attrObj.value;
					if(value){
						var l = value.length;
						if(l){
							if(l > 0){
								return value;
							}
						}
					}
				}
			}
		}
	}
	return null;
}


// nodeDescribe(node) -- Returns a text description of a node
function nodeDescribe(n){
	var tagName = "[null]";
	var id = "[null]";
	var nodeClass = "[null]";
	var name = "[null]";
	if(n){
		if(n.nodeName){ tagName = n.nodeName; }
		var tmp;
		tmp = nattr(n,"id"); if(tmp){ id = tmp; }
		tmp = nattr(n,"name"); if(tmp){ name = tmp; }
		tmp = nattr(n,"class"); if(tmp){ nodeClass = tmp; }
	}
	var descStr = tagName + "#" + id + "." + nodeClass + " name:" + name;
	return descStr; 
}


function seekSiblingByName (n, seekName, reverse){
	var done = 0;
	var s = seekName.toLowerCase();
	var f = n.nextSibling;

	while(!done){
		if(n){
			if(reverse == 1){ n = n.previousSibling;}
			else { n = n.nextSibling; } // Default 
		}
		if(!n) { 
			err("seekSiblingByName from " + nodeDescribe(n) + " failed for " +seekName);
			return null; }
		// alert("DEBUG: Testing seekSibling " + seekName + " against " + nodeDescribe(n));

		var a = n.nodeName;
		if(a){
			a = a.toLowerCase(); 
			if(a == s){ done = 1; }
		}
	}
	return n;
}

function seekSiblingByAttr (n, attr, value, reverse){
	var origNode = n;
	var done = 0;
	while(!done){
		if(n){
			if(reverse == 1){ n = n.previousSibling;}
			else { n = n.nextSibling; } // Default 
		}
		if(!n) {
			err("seekSibling from " + nodeDescribe(origNode) + " failed for " + attr + ":" + value);
			return null; }
		//alert("DEBUG: Testing seekSibling " + attr + ":" + value + " against " + nodeDescribe(n));
		var v = nattr(n,attr);
		if(v == value){ done = 1; }
	}
	return n;
}

function seekChildNodeNamed(n, seekName){
	if(n){
		var nSet = n.getElementsByTagName(seekName);
		return nSet[0];
	}
	return null;
}



function getSelected(n){ // Get selected Options from a select tag
	var optionSet = n.getElementsByTagName("option");
	var resultSet = new Array(); 
	if(optionSet){
		for(oID in optionSet){ // foreach only gives array indexes
			var o = optionSet[oID]; // get the actual objects
			// alert("DEBUG getSelected: o " + nodeDescribe(o) + " value " + o.innerHTML + " selected " + o.selected);
			if(o.selected){
				resultSet.push(o); 
			}
		}
	}
	return resultSet; 
}


function strStripWS(s){
	s = s.replace("/^\s*/", "");
	s = s.replace("/\s*$/", "");
	return s; 
}

// Massive kluge to pull info from a node
function stripItem(n){
	var itemName;
	var indexName;
	var check;

	n = n.childNodes[0]; // Drop down into child nodes 
	n = seekSiblingByAttr(n,"class", "itemName"); // seek to "itemName"

	n = seekChildNodeNamed(n, "select"); // drop to "itemSelector"
	var nSet = getSelected(n);
	for (oID in nSet){
		var o = nSet[oID];
		// TODO: allow for more than one answer 
		itemName = o.innerText;
	}
	// n=n.parentNode; // Rise back up to "itemName"

	// Pull out input text, if any
	n = seekSiblingByAttr(n, "name", "formItemName");
	var tmpText = strStripWS(n.value);
	if(tmpText.length > 0){
		// TODO: check for accuracy?
		// TODO: Elsewhere: have javascript kill textbox when
		// the selector is used
		itemName = tmpText;
	}

	n=n.parentNode; // Rise back up to "itemName" 

	// Pull out selections from name="indexName"
	n = seekSiblingByName(n, "select"); 
	nSet = getSelected(n);
	for (oID in nSet){
		var o = nSet[oID];
		// TODO: allow for more than one answer 
		indexName = o.innerText;
	}

	// Pull out check box from type=checkbox
	n = seekSiblingByAttr(n, "type", "checkbox"); 
	if(n.checked){ check = 1; }
	else { check = 0; }

	var descStr = "Item:" + itemName + " index:" + indexName + " checked:" + check;
	return descStr;
	
}

function stripAllItems(n){
	var str;
	n = n.childNodes[0];
	if(nattr(n, "class") == "item"){
		str = str + stripItem(n) + "\n";
	}

	while((n = seekSiblingByAttr(n,"class", "item"))){
		str = str + stripItem(n) + "\n";
	}
	return str;
}
