/*	=======================================================================	*/
/// <remarks>
///		Copyright:	umlungu consulting (pty) ltd
///		Author:		Alan Benington	
///		Started:	2006-09-19
///		Status:		release
///		Version:	0.0.6
///		Build:		20061030
///		License:	GNU General Public License
/// </remarks>
/*	=======================================================================	*/
/// <remarks>
/// Global Variables
/// </remarks>
/*	=======================================================================	*/
/// <remarks>
/// Class: ClassBase
/// The ClassBase object class has the following purpose/function:
///	- Provides a base class inheritance by other classes
/// </remarks>
/*	-----------------------------------------------------------------------	*/
/// <summary>
/// The ClassBase object class is constructed as follows:
///		var thisobj = new ClassBase(dbg);
/// - where:
///		- dbg:	debugging switch
/// </summary>
function ClassBase(dbg) {
	/*	-------------------------------------------	*/
	/// <remarks>Properties: Public</remarks>
	/*	-------------------------------------------	*/
	this.XmlHttp = null;
	/*	-------------------------------------------	*/
	/// <remarks>Properties: Private</remarks>
	/*	-------------------------------------------	*/
	var _self = this;	// self allows for a method/property to be called internally
	var _debug = (dbg)? dbg : false;
	var _id = "ClassBase";
	var _xmlhttp = null;
	var _xmldom = false;
	var XMLHTTP_LEVEL1	= "Msxml2.XMLHTTP";
	var XMLHTTP_LEVEL2	= "Microsoft.XMLHTTP";
	/*	-------------------------------------------	*/
	/// <remarks>Region: Accessor methods</remarks>
	/*	-------------------------------------------	*/
	this.GetID = function() { return(_id); }
	this.SetDebug = function(dbg) { _debug = (dbg)? dbg : false; }
	this.SelectNode = function(str) { var sel = "//"; var xml = _self.XmlHttp.responseXML; return (_xmldom)? xml.selectSingleNode(sel.concat(str)) : xml.getElementsByTagName(str)[0]; }
	this.SelectNodes = function(str) { var sel = "//"; var xml = _self.XmlHttp.responseXML; return (_xmldom)? xml.selectNodes(sel.concat(str)) : xml.getElementsByTagName(str); }
	this.SelectSingleNode = function(nde, str) { return (_xmldom)? nde.selectSingleNode(str) : nde.getElementsByTagName(str)[0]; }
	this.GetAttribute = function(nde, nme) { return (_xmldom)? nde.getAttribute(nme) : nde.attributes[nme].nodeValue;}
	this.NodeValue = function(nde) { return (_xmldom)? nde.text : nde.firstChild.nodeValue;}
	/*	-------------------------------------------	*/
	/// <remarks>Region: Public methods</remarks>
	/*	-------------------------------------------	*/
	this.GetUrl = _GetUrl;
	this.PostUrl = _PostUrl;
	this.RaiseError = _RaiseError;
	this.Debug = _Debug;
	this.Sniffer = new ClassSniffer();
	/// <summary>Method:_GetUrl</summary>
	function _GetUrl(url, func) {
		_Debug("_GetUrl:", "url", url);
		geturl(true, url, func);
	}
	/// <summary>Method:_PostUrl</summary>
	function _PostUrl(url, func, post) {
		geturl(false, url, func, post);
	}
	/// <summary>Method:_RaiseError</summary>
	function _RaiseError(thiserr) {
		_Debug("RaiseError:", "thiserr", thiserr);
		throw (thiserr);
	}
	/// <summary>Method:_Debug - write debugging message. First param is the method label, followed by name, value pairs</summary>
	function _Debug(arg) {
		if (_debug) {
			var s = "<!-- ClassBase::"
			s = s.concat(arguments[0], ":");
			var argn = arguments.length;
			for(var i=1; i<argn; i=i+2)
				s = s.concat(arguments[i], ":", arguments[i+1], " ");
			alert(s.concat("-->\n"));
		}
	}
	/*	-------------------------------------------	*/
	/// <remarks>Region: Private methods</remarks>
	/*	-------------------------------------------	*/
	/// <summary>Get the core request object</summary>
	function getHttpReq() {
		_Debug("getHttpReq");
		try {
			_xmlhttp = new ActiveXObject(XMLHTTP_LEVEL1);
			_xmldom = true;
		} catch(e){
			try {
				_xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				_xmldom = true;
			} catch(e){
				_xmlhttp = null;
			}
		} 
		//  (see http://developer.mozilla.org/en/docs/AJAX:Getting_Started)
		//	Note: Mozilla
		//	===============
		//	Moz does not support selectNodes and selectSingleNodes
		//	need to apply some fix (have tried the mozxpath.js (see \\swami\..\OpenSource\Ajax) - unsuccessfully)
		if(!_xmlhttp && typeof(XMLHttpRequest) != "undefined") {
			_xmlhttp = new XMLHttpRequest();
			_xmlhttp.overrideMimeType('text/xml');	
		}
		_Debug("getHttpReq", "_xmlhttp", typeof(_xmlhttp));
		return _xmlhttp;
	}
	/// <summary>Call a url via a get or post</summary>
	function geturl(isGet, ul, fn, post) {
		//_Debug("geturl", "isGet", isGet, "callback", typeof(fn), "timeout", (_xmlhttp.setTimeouts));
		if (!_xmlhttp) {
			_Debug("geturl", "_xmlhttp", typeof(_xmlhttp));
			return;
		}
		try {
			_xmlhttp.open((isGet)? "GET" : "POST", ul, true);
			_xmlhttp.onreadystatechange = fn;
			if (isGet) {
				_xmlhttp.send(null);
			} else {
				_xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
				_xmlhttp.send(post);
			}
		} catch (e) {
			_Debug("geturl", "readyState", _xmlhttp.readyState);
			_Debug("geturl", "code", _xmlhttp.status, "error", _xmlhttp.statusText);
			var err = "error_remote";
			// Handle the following errors:
			// 12029 - 'A connection with the server could not be established'
			// 12002 - 'The operation timed out'
			switch (e.number & 0xffff) {
				case 12029:	err = err.concat("_connection");	break;
				case 12002:	err = err.concat("_timeout");		break;
			}
			_RaiseError(err);	// rather throw error to be handled
		}
		//_Debug("geturl", "status", _xmlhttp.status);
	}
	/*	-------------------------------------------	*/
	/// <remarks>Properties: Public</remarks>
	/*	-------------------------------------------	*/
	_self.XmlHttp = getHttpReq();
}

