//var req; // global variable to hold request object
 
function loadXMLDoc(url, params) {
	loadXMLDoc(url, params, true);
}

function loadXMLDoc(url, params,synchro) {
	var req = false;
	if(window.XMLHttpRequest) {
		try {
			req = new XMLHttpRequest();
			/*if (req.overrideMimeType) {
				req.overrideMimeType('text/xml');
				// Vedi note sotto
			}*/
		} catch(e) {
			req = false;
		}
	} 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() { processReqChange(req); };//processReqChange;
		req.open("POST", url, synchro);//true);//
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		req.send(params);
		return true;
	}
	return req;
}

function processReqChange(req) {
//alert(req.readyState +"__"+ req.status);
	if(req.readyState == 4 && req.status == 200) {
		//var response  = req.responseXML.documentElement;
		var response1  = req.responseText;
//alert(response +"###"+ response1);
		//var commands = response.getElementsByTagName('command');
		//for(var i=0; i < commands.length; i++) {
		//method = commands[i].getAttribute('method');
		method = 'setcontentHTML';
		switch(method) {
			case 'dummy' :
			break;

			case 'openurl':
				var address = response1.substring(response1.indexOf('![CDATA[')+8,response1.indexOf(']]>'));
				window.open(address);
			break;

			case 'alert':
				var message = getNodeValue(commands[i], 'message');
				alert(message);
			break;

			case 'setvalue':
				var target = getNodeValue(commands[i], 'target');
				var value = getNodeValue(commands[i], 'value');
				if(target && value) {
					document.getElementById(target).value = value;
				}
			break;

			case 'addChild' :
				var target = getNodeValue(commands[i], 'target');
				var content = response1.substring(response1.indexOf('![CDATA[')+8,response1.indexOf(']]>'));
				addChildLuigi(content);
			break;

			case 'setvaluecontentXML':
				var target = getNodeValue(commands[i], 'target');
				var value = response1.substring(response1.indexOf('![CDATA[')+8,response1.indexOf(']]>'));
				if(target && value) {
					var valueXML = "<?xml version='1.0' encoding='UTF-8' standalone='yes'?> " + value;
					document.getElementById(target).value = valueXML;
				}
			break;

			case 'setdefault':
				var target = getNodeValue(commands[i], 'target');
				if(target) {
					document.getElementById(target).value = document.getElementById(target).defaultValue;
				}
			break;

			case 'focus':
				var target = getNodeValue(commands[i], 'target');
				if(target) {
					document.getElementById(target).focus();
				}
			break;

			case 'setcontentHTML':
				var target = response1.substring(response1.indexOf('<target>')+8,response1.indexOf('</target>'));//'tagSuggestion';//getNodeValue(commands[i], 'target');//
				var content = response1.substring(response1.indexOf('![CDATA[')+8,response1.indexOf(']]>'));
				if(target && content) {
					document.getElementById(target).innerHTML = content;
				}
			break;

			case 'setcontent':
				var target = getNodeValue(commands[i], 'target');
				var content = getNodeValue(commands[i], 'content');
				if(target && content) {
					document.getElementById(target).innerHTML = content;
				}
			break;

			case 'setstyle':
				var target = getNodeValue(commands[i], 'target');
				var property = getNodeValue(commands[i], 'property');
				var value = getNodeValue(commands[i], 'value');
				if(target && property && value) {
					document.getElementById(target).style[property] = value;
				}
			break;

			case 'setlist':
				var target = getNodeValue(commands[i], 'target');
				var property = getNodeValue(commands[i], 'property');
				//var value = getNodeValue(commands[i], 'value');
				FillDropDown(target,property);
			break;

			case 'setproperty':
				var target = getNodeValue(commands[i], 'target');
				var property = getNodeValue(commands[i], 'property');
				var value = getNodeValue(commands[i], 'value');
				if(value == "true") value = true;
				if(value == "false") value = false;
				if(target) {
					document.getElementById(target)[property] = value;
				}
			break;

			default:
				alert("Error: unrecognised method '" + method + "' in processReqChange()");
		}
	//}
	}
}

function getNodeValue(parent, tagName) {
	var node = parent.getElementsByTagName(tagName)[0];
	return (node && node.firstChild) ? node.firstChild.nodeValue : '';
}

function FillDropDown(target,idDoRidim) {
	var xmlDoc = req.responseXML.documentElement;
	var xSel = xmlDoc.getElementsByTagName('selectElement')[0];

	var objDDL  = document.getElementById(target);
	objDDL.options.length = 0;
	var xRows = xmlDoc.getElementsByTagName('entry');
	for(i=0;i < xRows.length ;i++) {
		//alert(xRows.getElementsByTagName('optionText')[i]);
		var theText = getNodeValue(xRows[i], 'optionText');
		var theValue = getNodeValue(xRows[i], 'optionValue');
		//var theValue = xRows.getElementsByTagName('optionValue')[i].firstChild.nodeValue;
		var option = new Option(theText, theValue);
		objDDL.options.add(option, objDDL.options.length);
	}
	doRidim(idDoRidim);
}

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";