// global request object
var request;

/**
 * Does a 'GET' XML HTTP Request.
 *
 * @param url the url to do the 'GET' operation against.
 * @param async should the call be asynchronous (YOU WANT TO USE TRUE... MOSTLY)
 *              IF NOT ASYNCHRONOUT THE CALLBACK IS NOT USED!!!!
 * @param callback the javascript callback function since this
 *                 operation is asynchronous.
 */
function XMLGetRequest(url, async, callback) 
{
	if(window.XMLHttpRequest)
	{
		// branch for native XMLHttpRequest object
		request = new XMLHttpRequest();
		request.onreadystatechange = callback;
		request.open("GET", url, async);
		request.send(null);
 	}      
	else if(window.ActiveXObject)
	{
		// branch for IE/Windows ActiveX version
		request = new ActiveXObject("Microsoft.XMLHTTP");
		if(request)
		{
			request.onreadystatechange = callback;
			request.open("GET", url, async);
			request.send();
		}
	}
	else
	{
		alert("This browser does not support XMLHTTPRequest.");
	}
}

/**
 * Does a 'POST' XML HTTP Request.
 *
 * @param url the url to do the 'POST' operation against.
 * @param async should the call be asynchronous (YOU WANT TO USE TRUE... MOSTLY)
 *              IF NOT ASYNCHRONOUT THE CALLBACK IS NOT USED!!!!
 * @param postData the post data.
 * @param callback the javascript callback function since this
 *                 operation is asynchronous.
 */
function XMLPostRequest(url, async, postData, callback) 
{
	if(window.XMLHttpRequest)
	{
		// branch for native XMLHttpRequest object
		request = new XMLHttpRequest();
		request.onreadystatechange = callback;
		request.open("POST", url, async);
		request.send(postData);
 	}      
	else if(window.ActiveXObject)
	{
		// branch for IE/Windows ActiveX version
		request = new ActiveXObject("Microsoft.XMLHTTP");
		if(request)
		{
			request.onreadystatechange = callback;
			request.open("POST", url, async);
			request.send(postData);
		}
	}
	else
	{
		alert("This browser does not support XMLHTTPRequest.");
	}
}

/**
 * Gets the request response DOM object.
 *
 * @return the request response DOM object.
 */
function XMLGetResponse()
{
	return request.responseXML;
}

/**
 * Gets the DOM document element from the request
 * response.
 *
 * @return the request response DOM document element.
 */
function XMLGetResponseDocumentElement()
{
	return request.responseXML.documentElement;
}

/**
 * Gets the text value stored in an XML tag.
 *
 * @param tagName the XML tag name.
 * @return the value or empty string on error.
 */
function XMLGetResponseNodeValue(tagName)
{
	if(request.responseXML.getElementsByTagName(tagName).item(0)!=null)
	{
		var node = request.responseXML.getElementsByTagName(tagName).item(0).firstChild;
		if(node!=null)
			return node.data;
		else
			return '';
	}
	else
	{
		return '';
	}
}


/**
 * Helper function to set the selected option on a select.
 * Does the selection by the value of the Option objects.
 *
 * @param theSelect The select object from the HTML page.
 * @param theValue The value to compare with.
 */
function setSelectedByValue(theSelect, theValue)
{
	var len = theSelect.options.length;
	for(loop=0;loop<len;loop++)
	{
		if(theSelect.options[loop].value==theValue)
		{
			theSelect.options[loop].selected = true;
			break;
		}
	}
	return;
} 
 
/**
 * Helper function to set the selected option on a select.
 * Does the selection by the text of the Option objects.
 *
 * @param theSelect The select object from the HTML page.
 * @param theText The text to compare with.
 */
function setSelectedByText(theSelect, theText)
{
	var len = theSelect.options.length;
	for(loop=0;loop<len;loop++)
	{
		if(theSelect.options[loop].text==theText)
		{
			theSelect.options[loop].selected = true;
			break;
		}
	}
	return;
}  
 
/**
 * Removes the Option entries for the select starting
 * at the given index to the  end of the Options array.
 *
 * @param theSelect The select object from the HTML page.
 * @param startingIndex the index to start clearing at.
 */
function emptySelect(theSelect, startingIndex)
{
	/* since setting an option to null collapses the array down
	 * we allways delete at the starting index.
	 */
	while(theSelect.options.length>startingIndex)
		theSelect.options[startingIndex] = null;
	return;	
}


/**
 * Populates a select.
 *
 * @param theSelect The select object from the HTML page.
 * @param XMLDocument the XMLHTTPResponse XML document generated.
 * @param attribTab the attribute name for the value of the HTML option entries.
  
 * @param startingIndex the index to start populating at. (ie clears 
 *                      from staring index then appends entries.)
 */
function populateSelect(theSelect, XMLDocument, attribTag, startingIndex)
{
	emptySelect(theSelect, startingIndex);
	
	var nodes = XMLDocument.getElementsByTagName('option');
	var optionValue;
	var optionText;
	
	for(loop=0;loop<nodes.length;loop++)
	{
		optionValue = nodes[loop].attributes.getNamedItem(attribTag).nodeValue;
		optionText = nodes[loop].childNodes.item(0).nodeValue
		theSelect.options[loop+startingIndex] = new Option(optionText, optionValue);
	}
}

/**
 * Gets the value of the currently selected entry for
 * a given HTML Select Object.
 *
 * @param select the HTML Select Object.
 * @return the selected value.
 */
function getSelectedValue(select)
{
	return select.options[select.selectedIndex].value;
}

/**
 * Use on XML entries that have a list of 'option' or other list type tags 
 * to count the number of entries.
 *
 * @param the XML DOM document holding the 'option' or other list type entries.
 * @param tagName the name of the XML tag entries that make up the list.
 * @return the count of entries.
 */
function getNodeCount(XMLDocument, tagName)
{
	var nodes = XMLDocument.getElementsByTagName(tagName);
	if(nodes!=null)
		return nodes.length;
	else
		return 0;
}
	
/**
 * Use on XML entries that have a list of 'option' or other list type tags 
 * to get an attribute of the entry.
 *
 * @param the XML DOM document holding the 'option' or other list type entries.
 * @param tagName the name of the XML tag entries that make up the list.
 * @param index the index of the entry in the list to do the lookup on.
 * @param attributeName the name of the attribute to lookup.
 * @return the attribute value.
 */
function getNodeAttribute(XMLDocument, tagName, index, attributeName)
{
	var nodes = XMLDocument.getElementsByTagName(tagName);
		
	if(nodes!=null)
	{
		if(index>=0 && index<nodes.length)
			return nodes[index].attributes.getNamedItem(attributeName).nodeValue;
		else
			return '';
	}
	else
	{
		return '';
	}
}

/**
 * Use on XML entries that have a list of 'option' or other list type tags 
 * to get the value of the entry.
 *
 * @param the XML DOM document holding the 'option' or other list type entries.
 * @param tagName the name of the XML tag entries that make up the list.
 * @param index the index of the entry in the list to do the lookup on.
 * @return the entry value.
 */
function getNodeValue(XMLDocument, tagName, index)
{
	var nodes = XMLDocument.getElementsByTagName(tagName);
	var option;
	
	if(nodes!=null)
	{
		if(index>=0 && index<nodes.length)
			return nodes[index].childNodes.item(0).nodeValue;
		else
			return '';
	}
	else
	{
		return '';
	}
}		

