﻿var AJAX = {

	/* ritorna l'elemento dell' id specifcato */
	getEl: function (id) {
		var el;
		if(document.getElementById) {
			el = document.getElementById(id);
		} else {
			el = document.all[id];
		}
		return el;
	},
	
	/* crea un elemento DOM */
	createEl: function (element) { 
		return document.createElement(element); 
	},
	
	/* ritorna la posizione di un'elemento */
	getElXY: function(obj) {	
		var xp, yp, op;
		xp = obj.offsetLeft;
		yp = obj.offsetTop;
		while (obj.offsetParent) {
			op = obj.offsetParent;
			xp = xp + op.offsetLeft;
			yp = yp + op.offsetTop;
			obj = obj.offsetParent;
		}
	return new Array(xp,yp);
	},

	/* ritorna un oggetto XMLHttpRequest o NULL */
	newXHR: function () {
		var	XHRobj = null;
		var browserUtente = navigator.userAgent.toUpperCase();
		if(typeof(XMLHttpRequest) === "function" || typeof(XMLHttpRequest) === "object") {
			XHRobj = new XMLHttpRequest();
		} else if(window.ActiveXObject && browserUtente.indexOf("MSIE 4") < 0) {
			if(browserUtente.indexOf("MSIE 5") < 0) {
				XHRobj = new ActiveXObject("Msxml2.XMLHTTP");
			} else {
				XHRobj = new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
		return XHRobj;
	},
	
	/* valida risposta xml */
	validateXml: function (XHRobj) {
		if(AJAX.statusText[XHRobj.status] === "OK") {
			if(XHRobj.responseXML) {
				return true;
			} else {
				alert("errore ajax: xml non valido");
			}
		} else {
			alert("errore ajax: " + AJAX.statusText[XHRobj.status]);
		}
		return false;
	},

	/* elenco degli stati ajax */
	readyState: {
		INATTIVO:	0,
		INIZIALIZZATO:	1,
		RICHIESTA:	2,
		RISPOSTA:	3,
		COMPLETATO:	4
	},
	
	statusText: new Array()
}

/* elenco descrittivo dei codici restituiti dal server */
AJAX.statusText[100] = "Continue";
AJAX.statusText[101] = "Switching Protocols";
AJAX.statusText[200] = "OK";
AJAX.statusText[201] = "Created";
AJAX.statusText[202] = "Accepted";
AJAX.statusText[203] = "Non-Authoritative Information";
AJAX.statusText[204] = "No Content";
AJAX.statusText[205] = "Reset Content";
AJAX.statusText[206] = "Partial Content";
AJAX.statusText[300] = "Multiple Choices";
AJAX.statusText[301] = "Moved Permanently";
AJAX.statusText[302] = "Found";
AJAX.statusText[303] = "See Other";
AJAX.statusText[304] = "Not Modified";
AJAX.statusText[305] = "Use Proxy";
AJAX.statusText[306] = "(unused, but reserved)";
AJAX.statusText[307] = "Temporary Redirect";
AJAX.statusText[400] = "Bad Request";
AJAX.statusText[401] = "Unauthorized";
AJAX.statusText[402] = "Payment Required";
AJAX.statusText[403] = "Forbidden";
AJAX.statusText[404] = "Not Found";
AJAX.statusText[405] = "Method Not Allowed";
AJAX.statusText[406] = "Not Acceptable";
AJAX.statusText[407] = "Proxy Authentication Required";
AJAX.statusText[408] = "Request Timeout";
AJAX.statusText[409] = "Conflict";
AJAX.statusText[410] = "Gone";
AJAX.statusText[411] = "Length Required";
AJAX.statusText[412] = "Precondition Failed";
AJAX.statusText[413] = "Request Entity Too Large";
AJAX.statusText[414] = "Request-URI Too Long";
AJAX.statusText[415] = "Unsupported Media Type";
AJAX.statusText[416] = "Requested Range Not Satisfiable";
AJAX.statusText[417] = "Expectation Failed";
AJAX.statusText[500] = "Internal Server Error";
AJAX.statusText[501] = "Not Implemented";
AJAX.statusText[502] = "Bad Gateway";
AJAX.statusText[503] = "Service Unavailable";
AJAX.statusText[504] = "Gateway Timeout";
AJAX.statusText[505] = "HTTP Version Not Supported";
AJAX.statusText[509] = "Bandwidth Limit Exceeded";	
