/*
#######################################
	DOM UTILITY
#######################################
*/

function get_el(ID)	//Cross browser version of getElementById
{
  if( document.getElementById ) // standard way
    elem = document.getElementById( ID );
  else if( document.all ) // old msie version
      elem = document.all[ID];
  else if( document.layers ) //  nn4 version
    elem = document.layers[ID];
return elem;
}

function el_vis(ID,disp)	//Set the display of the object with th passed ID
{
//Get element data
elem = get_el(ID);
//Get element style
vis = elem.style;
//Set style
vis.display = disp;
}

//
//#######################################
//	AJAX UTILITY
//#######################################
//
	
	// set an XMLHttpRequest object
		function setXMLHttpRequest() {
			var
				XHR = null,
				browser = navigator.userAgent.toUpperCase();
			if(typeof(XMLHttpRequest) === "function" || typeof(XMLHttpRequest) === "object")
				XHR = new XMLHttpRequest();
			else if(window.ActiveXObject && browser.indexOf("MSIE 4") < 0) {
				if(browser.indexOf("MSIE 5") < 0)
					XHR = new ActiveXObject("Msxml2.XMLHTTP");
				else
					XHR = new ActiveXObject("Microsoft.XMLHTTP");
			}
			return XHR;
		};



	// verify state
		var readyState = {
			NOT_ACTIVE:	0,
			READY:		1,
			REQUEST:	2,
			REPLY:		3,
			COMPLETED:	4
		};

	// array which describes server responses
		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 iterate_check_state(action2do) {
	string = "ajax.onreadystatechange = ajax_check_state('ajax_action');";
	string = string.replace('ajax_action',action2do);
	setTimeout ( string, 100 );
	
}

function ajax_check_state(action2do) {
		// check the state
		if(ajax.readyState === readyState.COMPLETED) {
			// check the server response
			if(statusText[ajax.status] === "OK")
			{
				// everything fine
				r = ajax.responseText;
			}
			else {
			// error
				r = "error: " + statusText[ajax.status];
			} 
			action2do = action2do.replace("ajax_result","r");
			setTimeout ( action2do, 0 );
		}			
	else {
			iterate_check_state(action2do);
	}
}	//Close control function



//Ajax request preset function using post
function ajax_request(r_path,send_var) {
ajax = setXMLHttpRequest();

	if (ajax) {
		// prepare request with POST
	    	ajax.open("post", r_path, true);

		// set the right header
		ajax.setRequestHeader("content-type", "application/x-www-form-urlencoded");

		// unset header "connection" from "keep alive"
		ajax.setRequestHeader("connection", "close");
	 
		// send request
		ajax.send(send_var);
		//alert('In ajax_request, attivo la funzione di controllo stato');
		// set the control function
    		
	}
	else {
		//Ajax undefined
		r = "error: Ajax not supported";
		alert(r);
	}   
}	//CLOSE FUNCTION ajax_request
