/*********************************************************
 * You're free to use this class as long as you don't    *
 * remove the author information and this notice.        *
 *                                                       *
 * Author: Maksimović Darko <darko.maksimovic@gmail.com> *
 *********************************************************/
var AJAX = {
	getAJAXXMLHandler: function()
	{
		var xmlHttp = null;
		try {
			if (window.XMLHttpRequest) { // Mozilla, Safari,..., IE7
				xmlHttp = new XMLHttpRequest();
			} else if (window.ActiveXObject) { // IE
				try {
					xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
				} catch (e) {
					try {
						xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
					} catch (e) {}
				}
			}
		} catch ( e ) {}
		return xmlHttp;
	},
	
	getAJAXHandler: function()
	{
		var xmlHttp = null;
		try {
			if (window.XMLHttpRequest) // Mozilla, Safari,...
				xmlHttp = new XMLHttpRequest();
			else if (window.ActiveXObject) { // IE
				try {
					xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
				} catch (e) {
					try {
						xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
					} catch (e) {}
				}
			}
		} catch ( e ) {}
		return xmlHttp;
	},
	
	getXMLDocument: function( ajax )
	{
		if (typeof DOMParser == "undefined") {
			DOMParser = function()
			{};
	
			DOMParser.prototype.parseFromString = function(str, contentType)
			{
				if (typeof ActiveXObject != "undefined") {
					var doc = new ActiveXObject("MSXML.DomDocument");
					doc.loadXML(str);
					return doc;
				} else if ( typeof XMLHttpRequest != "undefined" ) {
					var req = new XMLHttpRequest();
					req.open("GET", "data:" + (contentType || "application/xml") + ";charset=utf-8," + encodeURIComponent(str), false);
					if ( req.overrideMimeType )
						req.overrideMimeType(contentType);
					req.send(null);
					return req.responseXML;
				} else
					throw new FatalException( "Can't find a valid xml parser", "AJAX::getXMLDocument()" );
			}
		}
		var strDocument = ajax.responseText;
		var xmlDocument = ajax.responseXML;
		try {
			if( ! xmlDocument || xmlDocument.childNodes.length === 0 )
				xmlDocument = (new DOMParser()).parseFromString( strDocument, "application/xml" );
			return xmlDocument;
		} catch( e ) {
			return null;
		}
	},

	HTTPStatus: function( ajaxObject )
	{
		var hst = 0;
		try {
			hst = ajaxObject.status;
		} catch ( e ) {
			hst = undefined;
		}
		if ( hst >= 200 && hst <= 299 )
			return true;
		else if ( typeof hst != "undefined" )
			return false;

		// if getting the .status attribute failed, we go the other way - checking
		// the response headers

		var httpStatus = null;
		var headers;
		try {
			headers = ajaxObject.getAllResponseHeaders();
			if ( headers == "" )
				return false;
			else
				return true;
		} catch ( e ) {
			return false;
		}
	}
};

