/**
 *  ajax.js
 * ----------------------------------------------------------------------------
 *	 Copyright 1995-2007 Intesys S.r.l. Verona (I). All rights reserved.
 * ----------------------------------------------------------------------------
 *  Data e versione:
 *	  29/06/2007 - 1.0
 * ----------------------------------------------------------------------------
 *  Descrizione:
 *	  Libreria Javascript per gestire chiamate ajax
 *   NB! Attenzione che con Firefox la chiamata deve essere sullo
 *       stesso server (HTML e Javascript)
 * ----------------------------------------------------------------------------
 */

var Request = new Object()

Request.send = function(url, method, callbackFunction, data) {

	var req = false	
	
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest()
	} else if (window.ActiveXObject) {
		try {
			req = new ActiveXObject('Msxml2.XMLHTTP');
		} catch (e) {
			try {
				req = new ActiveXObject('Microsoft.XMLHTTP');
			} catch (e) {
				req = false
			}
		}
	}

	if (req) {
		req.onreadystatechange = function() {
			if (req.readyState == 4) {
				if (req.status < 400) {// only if "OK"
					(method=='POST') ? callbackFunction(req) : callbackFunction(req, data)
				} else {
					alert("Error loading data :\n" + req.status + '/' + req.statusText)
				}
			}
		}
		if (method=='POST') {
			req.open('POST', url, true)
			req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
			req.send(data)
		} else {		
			req.open('GET', url, true)
			req.send(null)
		}
		return req
	} else {
		alert('XMLHttpRequest not supported')
	}
	
}


Request.doPOST = function(url, callbackFunction, data) {
	Request.send(url, 'POST', callbackFunction, data)
}


Request.doGET = function(url, callbackFunction, args) {
	return Request.send(url, 'GET', callbackFunction, args)
}