		var readyState = {
			INATTIVO:	      0,
			INIZIALIZZATO:	1,
			RICHIESTA:	      2,
			RISPOSTA:	      3,
			COMPLETATO:  	4
		};

		var statusText = new Array();
		statusText[100] = "Continue";
		statusText[101] = "Switching Protocols";
		statusText[200] = "OK";
		statusText[201] = "Created";
		statusText[202] = "Accepted";
		statusText[203] = "Non-Authoritative Information";
		statusText[204] = "No Content";
		statusText[205] = "Reset Content";
		statusText[206] = "Partial Content";
		statusText[300] = "Multiple Choices";
		statusText[301] = "Moved Permanently";
		statusText[302] = "Found";
		statusText[303] = "See Other";
		statusText[304] = "Not Modified";
		statusText[305] = "Use Proxy";
		statusText[306] = "(unused, but reserved)";
		statusText[307] = "Temporary Redirect";
		statusText[400] = "Bad Request";
		statusText[401] = "Unauthorized";
		statusText[402] = "Payment Required";
		statusText[403] = "Forbidden";
		statusText[404] = "Not Found";
		statusText[405] = "Method Not Allowed";
		statusText[406] = "Not Acceptable";
		statusText[407] = "Proxy Authentication Required";
		statusText[408] = "Request Timeout";
		statusText[409] = "Conflict";
		statusText[410] = "Gone";
		statusText[411] = "Length Required";
		statusText[412] = "Precondition Failed";
		statusText[413] = "Request Entity Too Large";
		statusText[414] = "Request-URI Too Long";
		statusText[415] = "Unsupported Media Type";
		statusText[416] = "Requested Range Not Satisfiable";
		statusText[417] = "Expectation Failed";
		statusText[500] = "Internal Server Error";
		statusText[501] = "Not Implemented";
		statusText[502] = "Bad Gateway";
		statusText[503] = "Service Unavailable";
		statusText[504] = "Gateway Timeout";
		statusText[505] = "HTTP Version Not Supported";
		statusText[509] = "Bandwidth Limit Exceeded";

function AJAXGetElementById(id) {
  var elemento;
 if(document.getElementById)
    elemento = document.getElementById(id);
	else
   elemento = document.all[id];
 return elemento;
};
	
function AJAXGetXMLHttpRequest() {
	
var xmlhttp = false;
			
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    
    // Se IE
    } else if (window.ActiveXObject) {
        try {
  	    // Versione 5.5 o inferiore
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
  	        // Versione 5.5 o superiore
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
				 try {
  	                  //Quante versioni Hanno fatto ????
                        xmlhttp = new ActiveXObject("Msxml3.XMLHTTP");
                        } catch (e) {
				             alert("XMLHttpRequest non attivo");
						}
				}
        }

    }
   
    if (!xmlhttp) {
	alert("ERRORE: AJAX non supportato da questo browser!");
	return false;
    }

 return xmlhttp;
};



function AJAXOpenUrl(url,nome_elmento,method,defualt_html_element) {




  var  ajax = AJAXGetXMLHttpRequest(),
  elemento = AJAXGetElementById(nome_elmento);

  if (!defualt_html_element) var defualt_html_element = "Loading ...";



  if(ajax) {
  
   if(!url) return false;


   //XML Format Mozilla è necessario per questi Browsers
   if (ajax.overrideMimeType) ajax.overrideMimeType('text/xml');
   
   if(!method) var method = "GET";//Metodo di default è GET
   method = method.toUpperCase();
   
   //Creiamo un time per eliminare il problema della Cache di IE
   // Ringrazio Microsoft per avermi fatto debuggare i loro programmi !!
   var now = "uid=" + new Date().getTime();
   url += (url.indexOf("?")+1) ? "&" : "?";
   url += now;


   var parameters = null;

    if(method=="POST") {
	var parts = url.split("\?");
	url = parts[0];
	parameters = parts[1];
	}

	ajax.open(method, url, true);
   
     	if(method=="POST") {
			ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			ajax.setRequestHeader("Content-length", parameters.length);
	}
  

    // rimozione dell'header "connection" come "keep alive" del server
    // non stressate i server per niente !!
  
     ajax.setRequestHeader("connection", "close");


    ajax.onreadystatechange = function() {

    
      elemento.innerHTML = defualt_html_element;

      if(ajax.readyState === readyState.COMPLETATO) {
		
        if(statusText[ajax.status] === "OK")
          elemento.innerHTML = ajax.responseText;
        else {
          elemento.innerHTML += "Error :" + statusText[ajax.status];
        }
      } 
    }
	

    // Si invia la richiesta al server

    ajax.send(parameters);
  }

  return true;
} 

function AJAXURLEncode(CODE){

var plaintext = CODE;

	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					
	var HEX = "0123456789ABCDEF";
	
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" + ch + "' cannot be encoded using standard URL encoding.\n" +
				        "(URL encoding only supports 8-bit characters.)\n" +
						"A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	// VALORE CODIFICATO
	return encoded;
};



////////
var Base64 = {

	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;

		input = Base64._utf8_encode(input);

		while (i < input.length) {

			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);

			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;

			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}

			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

		}

		return output;
	},

	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;

		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

		while (i < input.length) {

			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));

			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;

			output = output + String.fromCharCode(chr1);

			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}

		}

		output = Base64._utf8_decode(output);

		return output;

	},

	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}

}


