function getXMLDocument(xml)
{
	var xmlDocument;
	// obtain xml document
	if(xml.documentElement)
		xmlDocument = xml.documentElement
	else
	{
		xmlDocument = new ActiveXObject("Microsoft.XMLDOM");
		xmlDocument.loadXML(xml);
	}
	
	return xmlDocument;
}

function getXMLDocument2(req)
{
    var xmlDoc;

    if(!req) return false;
    if(!req.responseXML) return false;

    if(req.responseXML.documentElement) // firefox
    {
        xmlDoc = req.responseXML.documentElement;
    }
    else // IE
    {
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.loadXML(req.responseXML);
    }
    
    return xmlDoc;
}

/*
getNodeValueByTagName() attempts to obtain a value within a tag such as <tag>value</tag>,
if tag is not found or if there is no value, this function returns false
*/
function getNodeValueByTagName(xmlDoc, tag)
{
    if(!xmlDoc) return false;

    var node = xmlDoc.getElementsByTagName(tag);

    if(node && node.length > 0)
        return node[0].firstChild ? node[0].firstChild.nodeValue : false;

    return false;
}

function getXMLNodesByTag(xmlResponse, tag)
{
    var xmlDocument;
	if(xmlResponse.documentElement) 
	    xmlDocument = xmlResponse.documentElement
	else 
	{
	    xmlDocument = new ActiveXObject("Microsoft.XMLDOM");
		xmlDocument.loadXML(xmlResponse);
	}

	return xmlDocument.getElementsByTagName(tag);
}

// get the text content between begin and end tags of the node
// (i.e. given <node>Text</node> function return "Text")
function getNodeText(node)
{
    if(document.all)
        return node.text;
    else
        return node.textContent;
}

// get the text content between begin and end tags of the node using a different mechanism
// (i.e. given <node>Text</node> function return "Text")
function getNodeValue(node)
{
	if(node && node.firstChild)
		return node.firstChild.nodeValue;
	return "";
}

/* 
Following are functions common to several search pages in the application
They all require the AJAX logic contained in AjaxRequest.js 
*/

// --------------------- //
// input variables:
// selectObj - select object we are filling up
// values - a list of values separated by 'separator'
// numTextElems - number of text elements after the 'separator' before the value element 
// (i.e. values = ('one','last name', 'first name'), text elements are 'last name', 'first name', numTextElems = 2
/*function fillSelectOptions(selectObj, values, separator, numTextElems, valsToKeep)
{
	if(values != null && values != "")
	{
		// disable the select and set the length to the number of elements
		// we would like to keep
		selectObj.disabled = true;
		selectObj.options.length = 1;
		
		// split the values by the separator
		var vals = values.split(separator);
		
		// start indexing options at 1 to keep "Loading ... text" in the window
		var optInd = 1;
		
		// set the increment value to skip over textual values
		var incrementBy = 1 + numTextElems;
		
		for(var i=0; i<vals.length; i += incrementBy)
		{
			newOption = document.createElement('OPTION');
			newOption.value = vals[i];
			newOption.text = vals[i+1];
			if(numTextElems > 1) 
			{
				newOption.text += ", " + vals[i+2];
			}
			selectObj.options.add(newOption, optInd);
			optInd++;
		}
		
		// change the text of the default, empty element from "Loading..." to ""
		selectObj.options[0].text = "";
		// reset the selected index to the first entry in the list
		selectObj.selectedIndex = 1;
		selectObj.disabled = false;
	}
}

// --------------------- //
function commonSelectChange(dbopsurl, separator, selObj) 
{
	
	var selInd = selObj.selectedIndex;
	var selVal = selObj.options[selInd].value;
	
	var params = "";
	var waitText = "";
	var numTextElems = 1;
	
	if(selObj.name == "userID")  
	{
		params = refSelectChange(selVal);
		if(params != "") 
		{
			orgIDParam = getSelectedOrgs();
			if(orgIDParam != "") 
			{
				params += "&orgIDs=" + orgIDParam;
			}	
		}
		numTextElems = 2;
		waitText = "Please wait, loading referee list ...";
	}
	else if(selObj.name == "locationID") 
	{
		params = locSelectChange(selVal);
		waitText = "Please wait, loading location list ...";
	}
	else if(selObj.name == "fieldID") 
	{
		params = fieldSelectChange(selVal);	
		waitText = "Please wait, loading field list ...";
	}
	
	//alert("Params: " + params);
	if(params != "") // if params are empty don't request anything
	{	
		selObj.options.length = 1;
		selObj.options[0].text = waitText;
		selObj.selectedIndex = 0;
		url = dbopsurl + params;
		
		// alert(url);
		// requires AjaxRequest.js to be included at some point
		AjaxRequest.get(
			{
			    'url':url
			    ,'onSuccess':function(req) { fillSelectOptions(selObj, req.responseText, separator, numTextElems, 1); }
		  	}
		);
	  	
		// make the request for data if params are set
		//var http_request_onready_func = function(textResponse) { fillSelectOptions(selObj, textResponse, separator, numTextElems, 1); }
		//makeHttpRequest(url, "text", true, http_request_onready_func);
	}
}


function refSelectChange(selVal) 
{
	var params = "";
	if(selVal == "all_refs") 
	{  // set the parameter only when show all referees or show all referees for org is selected
		params = "referees=1";
	}
	return params;
}

// ---------------------------- //
function locSelectChange(selVal) 
{
	var params = "";
	
	// any time the location is changed, update the field select 
	fieldSelect = document.getElementsByName('fieldID')[0];
	fieldSelect.options.length = 1;
	opt = document.createElement('OPTION');
	opt.value = "all_fields";
	
	if(selVal != "") 
	{	// update the field select depending on the value of location
		if(selVal == "all_locs") 
		{
			params = "locations=1";
		} 
		opt.text = "Show all fields for location";
	}
	else 
	{
		opt.text = "Show all fields";
	}
	
	// add an option to select
	addOptionToSelect(fieldSelect, opt);
	fieldSelect.selectedIndex = 0;
	return params;
}

function fieldSelectChange(selVal) 
{
	var params = "";
	if(selVal == "all_fields") 
	{	// if fields for location or all fields are selected
		params = "fields=1";
		locSelect = document.getElementsByName("locationID")[0];
		// if object exists and something is selected index of 0 is ""
		if(locSelect != null && locSelect.selectedIndex > 0) 
		{
			params += "&locID=" + locSelect.value;
		}
	}
	return params;
}
*/
