var ERROR_NOT_LOGGED = "login";
var LOGIN_PAGE = "login.php";

var ELEMENT_NODE = 1;
var ATTRIBUTE_NODE = 2;
var TEXT_NODE = 3;
var CDATA_SECTION_NODE = 4;
var ENTITY_REFERENCE_NODE = 5;
var ENTITY_NODE = 6;
var PROCESSING_INSTRUCTION_NODE = 7;
var COMMENT_NODE = 8;
var DOCUMENT_NODE = 9;
var DOCUMENT_TYPE_NODE = 10;
var DOCUMENT_FRAGMENT_NODE = 11;
var NOTATION_NODE = 12;

var request_amount = 0;

function AjaxException(msg) {
	var str = (msg.message) ? msg.message : msg;
	this.message = "AjaxException: " + str;
}

function RequesterException(msg) {
	var str = (msg.message) ? msg.message : msg;
	this.message = "RequesterException: " + str;
}

var Ajax = {
	/* - - - CONSTANTES - - - */
	UNINITIALIZED: 0,
	LOADING: 1,
	LOADED: 2,
	INTERACTIVE: 3,
	COMPLETE: 4,
	NOT_FOUND: 404,
	OK: 200,

	getTransport: function () {
		var rV = null;

		if (window.XMLHttpRequest) {
			rV = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			var versions = ["Msxml2.XMLHTTP.7.0", "Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.5.0", "Microsoft.XMLHTTP", "Msxml2.XMLHTTP", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0"];
			//var versions = ["Msxml2.XMLHTTP.7.0", "Msxml2.XMLHTTP", "Microsoft.XMLHTTP", "Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0"];

			for (var i=0; i < versions.length ; i++) {
				try{
					rV = new ActiveXObject(versions[i]);
					break;
				}catch(e){}
			}
		}

		return rV;
	}
}

function Requester() {
	this.transport = Ajax.getTransport();
	this._method = arguments.length ? arguments[0] : "GET";

	this.setMethod = function(m) { this._method = m; }

	this.request = function(url, fields, callback) {
		this.showStatus();
		if (!this.transport) throw "No Transport found";

		if (!fields) fields = "dummy=1";
		this.transport.onreadystatechange = this._createCallback(this, this.transport, callback);

		this.transport.open(this._method, url, true);
		if (this._method.toUpperCase() == "POST") {
			this.transport.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
		}			
		this.transport.send(fields);
	}

	this._createCallback = function(obj, trsprt, callback) { 
		retValue = function() {
			if (trsprt.readyState == Ajax.COMPLETE) {
				if (trsprt.status == Ajax.OK) {
					response = trsprt.responseXML.documentElement;
					
					var ok = true;
					try{
						var _status = response.getElementsByTagName('status');
						if (_status.length) {
							if (_status[0].nodeType == TEXT_NODE) {
								ok = (_status[0].nodeValue != ERROR_NOT_LOGGED);
							} else {
								ok = (_status[0].firstChild.data != ERROR_NOT_LOGGED);
							}
						}
					}catch(e){
						/*alert(
							"ERROR\n" + e + 
							"\nStatus: " + trsprt.statusText +
							"\nContent-Type: " + trsprt.getResponseHeader("Content-Type") +
							"\nTextRecived: " + trsprt.responseText
						);*/
					}
					

					if (!ok)	document.location = LOGIN_PAGE;
					else		callback.apply(this, [response]);

					obj.hideStatus();
				} else {
					//var pp = new RequesterException("There was a problem retrieving the XML data:\n" + trsprt.statusText);
					//alert("There was a problem retrieving the XML data:\n" + trsprt.statusText);
				}
			}
		}

		return retValue;
	}

	this.showStatus = function() {
		request_amount++;
		var status = document.getElementById("Status");
		if (status) {
			status.className = status.className.replaceAll("Hidden", "");
		}
	}

	this.hideStatus = function() {
		request_amount--;
		if (request_amount <= 0) {
			var status = document.getElementById("Status");
			if (status) {
				status.className += " Hidden";
			}
			request_amount = 0;
		}
	}
}
