/*****************************************************************************
 *
 * File     : util.js
 * 
 * Project  : Whistler
 * 
 * Contents : Various utilities
 *
 * Author: 
 *
 * Copyright 2007 Whistler Info
 * http://www.whistlerinfo.ca
 * All rights reserved.
 * 
 ***|***************************|***********************|*******************|*/


/************************************************************************
*
*	function:	trim
*
*	purpose:	
*
*************************************************************************/
function trim(string)
{
	if (string == null) return null;
	var size = string.length;
	
	var index = 0;
	while (index < size && string.charAt(index) == " ") index++;
	var lastIndex = size;
	while (index >= 0 && string.charAt(index) == " ") index--;

	if (index > lastIndex) return "";
	return string.substring(index, lastIndex);
}

/************************************************************************
*
*	function:	isVisible
*
*	purpose:	
*
*************************************************************************/
function isVisible(view)
{
	if (view.style.display == "") return true;
	else return false;
}

/************************************************************************
*
*	function:	createXMLHTTPObject
*
*	purpose:	
*
*************************************************************************/
function createXMLHTTPObject() 
{
	var XMLHttpFactories = [
		function () {return new XMLHttpRequest()},
		function () {return new ActiveXObject("Msxml3.XMLHTTP")},
		function () {return new ActiveXObject("Msxml2.XMLHTTP")},
		function () {return new ActiveXObject("Microsoft.XMLHTTP")}
	];

	var xmlhttp = null;
	for (var index = 0; index < XMLHttpFactories.length; index++) 
	{
		try 
		{
			xmlhttp = XMLHttpFactories[index]();
		}
		catch (exception) 
		{
			continue;
		}
		break;
	}
	
	return xmlhttp;
}

/************************************************************************
*
*	function:	selectNodes(parent, xpath)
*
*	purpose:	Implements a simple step-xpath of the form:
*						step1/step2/.../stepn
*						This does not handle predicates
*
*************************************************************************/
function selectNodes(parent, xpath)
{
	var result = new Array();
	if (xpath == null || xpath == "") return result;
	if (parent == null) return result;
	var slashIndex = xpath.indexOf("/");
	if (slashIndex == -1) slashIndex = xpath.length;
	var token = xpath.substring(0, slashIndex);
	var remainder = xpath.substring(slashIndex + 1);
	
	for (var index = 0; index < parent.childNodes.length; index++)
	{
		var child = parent.childNodes.item(index);
		if (child.nodeName.toLowerCase() != token.toLowerCase()) continue;
		if (remainder == "") result.push(child);
		else result.addAll(selectNodes(child, remainder));
	}	
	
	return result;
}

/************************************************************************
*
*	function:	selectFirstNode(parent, xpath)
*
*	purpose:	
*
*************************************************************************/
function selectFirstNode(parent, xpath)
{
	var nodes = selectNodes(parent, xpath);
	if (nodes.length == 0) return null;
	else return nodes[0];
}

