String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}

String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

function isEmpty( str ) {
	//log( "isEmpty("+str+")" );
	//log( "typeof(str): " + typeof(str) );
	return 	str == null
		|| str == undefined
		|| ( typeof(str)=="string" && str.trim() == "" );
}

// XML helpers

function getNode( parent, childName ) {
	if ( !parent ) {
		alert( "getNode(): parent is empty" );
		return;
	}
	if ( isEmpty(childName) ) {
		alert( "getNode(): childName is empty" );
		return;
	}
	var tags = parent.getElementsByTagName( childName );
	if ( tags == null ) {
		return null;
	}
	else {
		return tags[0];
	}
}

function getNodes( parent, childName ) {
	return parent.getElementsByTagName( childName );
}

function getNodeValue( parent, childName ) {
	var node = getNode( parent, childName );
	if ( node == null ) {
		return null;
	}
	else if ( node.firstChild == null ) {
		return "";
	}
	else {
		return node.firstChild.nodeValue;
	}
}

function getNodeNumber( parent, childName ) {
	var value = getNodeValue( parent, childName );
	if ( value != null )
		value = parseFloat( value );
	return value;
}

// XML Object ////////////////////////////////////////////////////////////////////////////////

// Example:
// 
// var callback = new Callback( this, "onEventFunction" );
// var request = new Request( "http://link", null, callback );
// 

function Request( url, params, callback ) {
	var request = this;
	
	if ( isEmpty(url) ) {
		alert( "Error in Request(), url is not set." );
		return;
	}
	
	//log( "callback: " + callback );
	//log( "-isEmpty(callback): " + isEmpty(callback) );
	//return;
	
	if ( isEmpty(callback) ) {
		alert( "Error in Request(), callback is not set." );
		return;
	}
	
	var method;
	if ( isEmpty(params) )
		method = "GET";
	else
		method = "POST";
	
	this.callback = callback;
	this.request = createXmlHttpRequest();
	
	this.request.open( method, url );
	this.request.onreadystatechange = function() {
		request.onreadystatechange();
	}
	
	//if ( method == "POST" ) {
		// POST request, build a parameter string
		var paramStr = "";
		for ( var i in params ) {
			if ( paramStr != "" )
				paramStr += "&";
			paramStr += escape(i) + "=" + escape(params[i]);
		}

		this.request.setRequestHeader( "Content-type", "application/x-www-form-urlencoded" );
		//this.request.setRequestHeader( "Content-length", params.length );
		//this.request.setRequestHeader( "Connection", "close" );
		this.request.send( paramStr );
	/*}
	// TODO: BUG: This doesn't work in Mozilla...
	else {
		// GET request
		this.request.send();
	}*/
}

Request.prototype.abort = function() {
	if ( this.request )
		this.request.abort();
	this.callback = null;
	this.request = null;
}

Request.prototype.onreadystatechange = function() {
	if ( this.request.readyState == 4 ) {
		if ( this.request.responseXML ) {
			if ( this.callback ) {
				var root = this.request.responseXML.documentElement;
				//alert( "Request.onreadystatechange(): root="+root );
				this.callback.call( new Array( root ) );
			}
		}
		else {
			alert( "Request:onreadystatechange(): Error, received: " + this.request.responseText );
		}
	}
}

////////////////////////////////////////////////////////////////////////////////











// NOTE: Use "Request" object instead! //

function XML() {
}

XML.prototype.load = function( url ) {
	var xml = this;
	this.request = createXmlHttpRequest();
	this.url = url;
	this.request.open( "GET", url, true );
	this.request.onreadystatechange = function() {
		xml.onreadystatechange();
	}
	this.root = undefined;
	this.request.send( "" );
}

XML.prototype.abort = function() {
	if ( this.request )
		this.request.abort();
}

XML.prototype.onreadystatechange = function() {
	if ( this.request.readyState == 4 ) {
		if ( this.request.responseXML ) {
			this.root = this.request.responseXML.documentElement;
			this.onload();
		}
		else {
			alert( "XML:onreadystatechange(): Error, received: " + this.request.responseText );
			//alert( "XML:onreadystatechange(): xml: " + this.request.responseXML );
		}
	}
}

XML.prototype.onload = function() {
	alert("ERROR: XML.onload() not set");
}

// Helper functions ////////////////////////////////////////////////////////////////////////////////

function createXmlHttpRequest() {
	var xmlhttp;
	if ( window.XMLHttpRequest ) {
		xmlhttp = new XMLHttpRequest();
	}
	else if ( window.ActiveXObject ) {
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
			}
		}
	}
	return xmlhttp;
}
