/*****************************************************************************
 *
 * File     : Util.js
 * 
 * Project  : WMS Viewer Client
 * 
 * Contents : Global utility methods
 *
 * Author: Milan Trninic
 *
 * Copyright 1999-2005 Galdos Systems, Inc.
 * All rights reserved.
 * 
 ***|***************************|***********************|*******************|*/

/************************************************************************
*
*	Global declarations
*
*	
*
*************************************************************************/

/************************************************************************
*
*	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 Collection();
	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.add(child);
		else result.addAll(selectNodes(child, remainder));
	}	
	
	return result;
}

/************************************************************************
*
*	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:	target
*
*	purpose:	Used in the Event Handler Adapter pattern - please consult the
*						documentation for explanation 
*
*************************************************************************/
function target(event, clazz)
{
	var element = event.srcElement;
	while (element != null)
	{
		var target = element.dataTransfer;
		if (target != null && isInstanceOf(target, clazz)) return target;
		element = element.parentNode;
	}
	return null;
}

/************************************************************************
*
*	function:	isInstanceOf
*
*	purpose:	 
*
*************************************************************************/
function isInstanceOf(object, clazz)
{
	// this is plain comparison - no inheritance: object is of clazz type
	if (object.constructor == clazz) return true;

	// if not, check whether object derives from clazz
	// unfortunatelly - we can only check the top class in hierarchy this way
	if (object._superClass != null && object._superClass == clazz) return true;

	return false;
}

/************************************************************************
*
*	function:	transform
*
*	purpose:	 Transforms the given document with the xslt stylesheet
*                provided.
*
*************************************************************************/
function transform(doc, xsl) {
    // load XSLT stylesheet document
    var stylesheet = new ActiveXObject("Msxml2.DOMDocument.3.0");    
    stylesheet.async = false;
    stylesheet.setProperty("SelectionNamespaces", "xmlns:fps='http://www.galdosinc.com/fps' xmlns:wms='http://www.opengis.net/wms'");
    stylesheet.load(xsl);
    var result = doc.transformNode(stylesheet);
    return result;
}

