function getXmlText(xml, nodo, obbligatorio) {
try {
	var valore = xml.getElementsByTagName(nodo)[0].firstChild.nodeValue;
} catch(error) {
	if(obbligatorio == true) {
		//alert("Errore: nodo vuoto");
	}
	return "";
}
return valore;
}


function checkXml(xml) {
	
try {
	document.body.style.cursor = 'default';
	var result = xml.getElementsByTagName('result')[0];
} catch(error) {
	alert("Errore: impossibile contattare il server");
	return false;
}

try {
	var code_result = result.getAttribute("code");
	if(code_result == "1") {
		try {
			var msg = result.getElementsByTagName('message')[0].firstChild.nodeValue;
			alert(msg);
		} catch(error) {
			alert("Errore: risposta del server errata");
		}
		return false;
	}
	return true;

} catch(error) {
	alert("Errore: impossibile contattare il server");
	return false;
}

}


function formSend(idForm, urlAjax, get, post, retFunct, retType) {
try {
	var valori = "";
	var form = document.getElementById(idForm);

	var inputs = form.getElementsByTagName('input');
	for(var i = 0; i < inputs.length; i++) {
		var input = inputs[i];

		switch(input.type) {
			case "text":
			case "hidden":
				valori += input.name + "=" + input.value + "&";
				break;

			case "radio":
				if(input.checked == true)
					valori += input.name + "=" + input.value + "&";
				break;

			case "checkbox":
				if(input.checked == true)
					valori += input.name + "=" + input.value + "&";
				break;

			case "password":
				valori += input.name + "=" + input.value + "&";
				break;
		
			case "file":
			default:
				break;
		}
	}
	
	var selects = form.getElementsByTagName('select');
	for(var i = 0; i < selects.length; i++) {
		var select = selects[i];
		var options = select.getElementsByTagName('option');
		var option = options[select.selectedIndex];
		
		valori += select.name + "=" + option.value + "&";
	}

	var textareas = form.getElementsByTagName('textarea');
	for(var i = 0; i < textareas.length; i++) {
		var textarea = textareas[i];
		valori += textarea.name + "=" + escape(textarea.value) + "&";
	}
	
	if(form.method == "post") {
		if(post != null)
			post = valori + post;
		else
			post = valori;

	} else if(form.method == "get") {
		if(get != null)
			get = valori + get;
		else
			get = valori;

	} else {
		alert("Errore: method della form sconosciuto");
	}
} catch(error) {
	alert(error);
	return;
}

xmlAjaxRequest(urlAjax, get, post, retFunct, retType);

}


function xmlAjaxRequest(urlAjax, get, post, retFunct, retType) {

if(retType == "xml")
	document.body.style.cursor = 'wait';

if(window.XMLHttpRequest) {
	var http_request = new XMLHttpRequest();
} else if(window.ActiveXObject) {
	var http_request = new ActiveXObject("Microsoft.XMLHTTP");
}

http_request.onreadystatechange = function() {
	if(http_request.readyState == 4) {
		document.body.style.cursor = 'default';
		if(http_request.status == 200) {
			//alert(http_request.responseText);
			if(retType == "text") {
				retFunct(http_request.responseText);
							
			} else if(retType == "xml") {
				//alert(http_request.responseText);
				retFunct(http_request.responseXML);
			}
		
		}
		// else {
			// var errorStatus = http_request.status + '';
			// alert("HTTP Error: " + errorStatus);
		// }
	}
}

if(post == null) {
	http_request.open("GET", urlAjax + "?" + get, true);
	http_request.send(null);

} else {
	var pieces = post.split("&");
	var parNum = pieces.length;
	http_request.open("POST", urlAjax + "?" + get, true);
	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http_request.setRequestHeader('Content-length',parNum);
	http_request.send(post);
} 

}



function getOggi() {

var giorno = new Date();
var oggi = giorno.getDate() + "/" + (giorno.getMonth() + 1) + "/" + giorno.getFullYear();
return oggi;

}


function getDataSelect(id, valore, action) {

var mesiArray = new Array("Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno", "Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre");

if(valore == null) {
	var oggi = new Date();
	var giorno = oggi.getDate();
	var mese = oggi.getMonth() + 1;
	var anno = oggi.getFullYear();
} else {
	var giorno = valore.getDate();
	var mese = valore.getMonth() + 1;
	var anno = valore.getFullYear();
}

valore_ok = true;

var giorniMese = 32 - (new Date(anno, mese - 1, 32).getDate());

var input = "<span id='" + id + "_giorno_span'>";
input += "<select name='" + id + "_giorno' onchange='" + action + "' id='" + id + "_giorno' class='calendario_giorno'>";
for(var i = 1; i <= giorniMese; i++) {
	input += "<option value='" + i + "'";
	if(giorno == i && valore_ok == true)
		input += " selected='selected'";
	input += ">" + i + "</option>";
}
input += "</select>";
input += "</span>";

input += "<select onchange='updateGiorniCalendario(\"" + id + "_giorno_span\", \"" + id + "_mese\", \"" + id + "_anno\"); " + action + "' name='" + id + "_mese' id='" + id + "_mese' class='calendario_mese'>";
for(var i = 1; i <= mesiArray.length; i++) {
	input += "<option value='" + i + "'";
	if(mese == i && valore_ok == true)
		input += " selected='selected'";
	input += ">" + mesiArray[i - 1] + "</option>";
}
input += "</select>";

input += "<select onchange='updateGiorniCalendario(\"" + id + "_giorno_span\", \"" + id + "_mese\", \"" + id + "_anno\"); " + action + "' name='" + id + "_anno' id='" + id + "_anno' class='calendario_anno'>";
for(var i = (anno - 2); i <= (anno + 2); i++) {
	input += "<option value='" + i + "'";
	if(anno == i && valore_ok == true)
		input += " selected='selected'";
	input += ">" + i + "</option>";
}
input += "</select>";

return input;

}


function updateGiorniCalendario(id_giorno_select_span, id_mese_select, id_anno_input) {

var input = document.getElementById(id_anno_input);
var anno_attuale = input.value;

var select = document.getElementById(id_mese_select);
var mese_attuale = select.value;

var span = document.getElementById(id_giorno_select_span);
select = span.getElementsByTagName('select')[0];
var name_select = select.getAttribute('name');
var id_select = select.getAttribute('id');
giorno_attuale = select.value;

var giorniMese = 32 - (new Date(anno_attuale, mese_attuale - 1, 32).getDate());

var input = "<select id='" + id_select + "' name='" + name_select + "' class='calendario_giorno'>";
input +=  "<option value='0'>&nbsp;</option>";

if(giorno_attuale > giorniMese)
	giorno_attuale = 1;

for(var i = 1; i <= giorniMese; i++) {
	input += "<option value='" + i + "'";
	if(giorno_attuale == i)
		input += " selected='selected'";
	input += ">" + i + "</option>";
}

input += "</select>";

span.innerHTML = input;

}


function checkData(data) {

var splitted = data.split("/");
if(splitted.length != 3)
	return false;

var giorno = parseInt(parseVal(splitted[0]));
var mese = parseInt(parseVal(splitted[1]));
var anno = parseInt(parseVal(splitted[2]));

if(isNaN(giorno) || isNaN(mese) || isNaN(anno))
	return false;

if(anno < 1900)
	return false;

if((mese < 1) || (mese > 12)) 
	return false;

if((giorno < 1) || (giorno > 31))
	return false;

if((giorno > 29) && (mese == 2))
	return false;

if(giorno > 30 && ((mese == 4) || (mese == 6) || (mese == 9) || (mese == 11)))
	return false;

return true;

}


function checkAmericanData(data) {

var splitted = data.split("/");
if(splitted.length != 3)
	return false;

var giorno = parseInt(parseVal(splitted[2]));
var mese = parseInt(parseVal(splitted[1]));
var anno = parseInt(parseVal(splitted[0]));

if(isNaN(giorno) || isNaN(mese) || isNaN(anno))
	return false;

if(anno < 1900)
	return false;

if((mese < 1) || (mese > 12)) 
	return false;

if((giorno < 1) || (giorno > 31))
	return false;

if((giorno > 29) && (mese == 2))
	return false;

if(giorno > 30 && ((mese == 4) || (mese == 6) || (mese == 9) || (mese == 11)))
	return false;

return true;

}


function parseVal(val) {

var len = val.length;

while((len > 0) && (val.charAt(0) == '0')) {
	val = val.substring(1, val.length);
	len = len - 1;
}

if(len == 0)
	return '0';

return val;

}


function checkInteger(val) {

if(parseInt(parseVal(val)) != (val - 0))
	return false;

return true;

}


function checkPrezzo(val) {

var point = 0;

if(val.length == 0)
	return false;

while((point < val.length) && (val.charAt(point) != '.') && (val.charAt(point) != ','))
	point++;

if(point == val.length)
	return checkInteger(val);

var intero = val.substr(0,point);
if(checkInteger(intero) == false)
	return false;

cent = val.substr(point + 1);
if((checkInteger(cent) == false) || (cent.length > 2))
	return false;

return true;

}


function findBrowser() {

var useragent = navigator.userAgent;

if(useragent.indexOf("Konqueror") != -1)
	return "Konqueror";

else if(useragent.indexOf("Safari") != -1)
	return "Safari";

else if(useragent.indexOf("Opera") != -1)
	return "Opera";

else if(useragent.indexOf("Firefox") != -1)
	return "Firefox";

else if(useragent.indexOf("MSIE") != -1)
	return "IE";

return "";

}


function positionTo(dest, container) {
	var destT = getOffset(dest, "top");
	var containerT = getOffset(container, "top");

	var div = document.getElementById(container);
	div.scrollTop = destT - containerT;
}


function openLateral(idopener) {

var winW = getOffset('body', "width");
var winH = getOffset('body', "height");

var mainT = getOffset("mainarea", "top");
var mainL = getOffset("mainarea", "left");
var mainW = getOffset("mainarea", "width");
var mainH = getOffset("mainarea", "height");

var maxRight = mainL + mainW;
var maxBottom = mainT + mainH;

var openerW = getOffset(idopener, "width");
var openerH = getOffset(idopener, "height");
var openerT = getOffset(idopener, "top");
var openerL = getOffset(idopener, "left");

var div = document.getElementById('body');
div.style.filter = "Alpha(Opacity=30)";
div.style.opacity = ".30";

div = document.getElementById('mainarea');
mainST = div.scrollTop;

div = document.getElementById('popupStruct');
div.style.visibility = "visible";
div.style.display = "block";

div = document.getElementById('popupContainer');
div.style.height = winH + 'px';
div.style.width = winW + 'px';

var popupW = getOffset("popupStruct", "width");
var popupH = getOffset("popupStruct", "height");
var popupT = openerT + (openerH / 2) - (popupH / 2) - mainST;
var popupL = openerL + openerW;

if(popupL < mainL)
	popupL = mainL;

if(popupT < mainT)
	popupT = mainT;

if((popupL + popupW) > maxRight)
	popupL = maxRight - popupW;

if((popupT + popupH) > maxBottom)
	popupT = maxBottom - popupH;


div = document.getElementById('popupStruct');
div.style.top = popupT + 'px';
div.style.left = popupL + 'px';

lockLinks();

}


function closeLateral() {

unlockLinks();

var div = document.getElementById("popup");
div.innerHTML = "";

div = document.getElementById('body');
div.style.filter = "";
div.style.opacity = "1";

div = document.getElementById('popupContainer');
div.style.height = '0px';
div.style.width = '0px';

div = document.getElementById('popupStruct');
div.style.visibility = "hidden";
div.style.display = "none";
div.style.top = '0px';
div.style.left = '0px';

}


function openPopup() {

var winW = getOffset('body', "width");
var winH = getOffset('body', "height");

var div = document.getElementById('body');
div.style.filter = "Alpha(Opacity=30)";
div.style.opacity = ".30";

div = document.getElementById('popupContainer');
div.style.height = winH + 'px';
div.style.width = winW + 'px';

div = document.getElementById('popupStruct');
div.style.visibility = "visible";
div.style.display = "block";

var popupW = getOffset("popupStruct", "width");
var popupH = getOffset("popupStruct", "height");

var popupT = (winH  / 2) - (popupH / 2);
var popupL = (winW  / 2) - (popupW / 2);

div.style.top = popupT + 'px';
div.style.left = popupL + 'px';

lockLinks();

}


function closePopup() {

unlockLinks();

var div = document.getElementById("popup");
div.innerHTML = "";

div = document.getElementById('body');
div.style.filter = "";
div.style.opacity = "1";

div = document.getElementById('popupContainer');
div.style.height = '0px';
div.style.width = '0px';

div = document.getElementById('popupStruct');
div.style.visibility = "hidden";
div.style.display = "none";

}


function pluginClosePopup(request,win) {

win.close();
self.focus();

if(request != null)
        setTimeout(100,request);

}


function resizeArea(id,width,startWidth,timeout,endAction) {

if(width == startWidth) {

	if(endAction.length > 0) {
		setTimeout(endAction, timeout);
	}

	return;
}

if(startWidth < width)
	var newWidth = width - 2;
else
	var newWidth = width + 2;

var div = document.getElementById(id);
div.style.left = newWidth+ 'px';

setTimeout("resizeArea('" + id + "'," + newWidth + "," + startWidth + "," + timeout + ",'" + endAction + "')", timeout);

}


function getOffset(id,type) {

var div = document.getElementById(id);
var offset = 0;

switch(type) {
	case "width":
		offset = div.offsetWidth;
		return offset;

	case "height":
		offset = div.offsetHeight;
		return offset;
}

do {
	var value = 0;

	switch(type) {
		case "top":
			value = div.offsetTop;
			break;

		case "left":
			value = div.offsetLeft;
			break;
	}

	offset = offset + value;
	div = div.offsetParent;
} while(div);

return offset;

}


function setLinkExternal() {

try {
	var anchors = document.getElementsByTagName("a");
	
	for(var i = 0; i < anchors.length; i++) {
		var a = anchors[i];
		if(a.rel == "external") {
			a.target = "_blank";
		}
	}

} catch(error) {
	;
}

}


function setLinkPopup() {

try {
	var hWind = Math.floor(screen.height / 2);
	var wWind = Math.floor(screen.width / 2);
	var hPos = Math.floor(screen.height / 4);
	var wPos = Math.floor(screen.width / 4);

	var anchors = document.getElementsByTagName("a");
	
	for(var i = 0; i < anchors.length; i++) {
		var a = anchors[i];

		if(a.rel == "popup") {
			newHref = "javascript: var win = window.open('" + a.href + "','popup','width=" + wWind + ",height=" + hWind + ",left=" + wPos + ",top=" + hPos + ",directories=no,location=no,menubar=no,status=no,resizable=yes,toolbar=no,scrollbars=yes'); win.focus();";
			a.href = newHref;
		}
	}

} catch(error) {
	;
}

}


function lockLinks() {

try {
	var anchors = document.getElementsByTagName("a");
	for(var i = 0; i < anchors.length; i++) {
		var a = anchors[i];
		a.setAttribute("oldhref", a.href);
		a.href = "javascript: ;";
	}
/*	
	var rows = document.getElementsByTagName("tr");
	for(var i = 0; i < rows.length; i++) {
		var tr = rows[i];
		tr.setAttribute("oldonclick", tr.onclick);
		tr.onclick = "javascript: ;";
	}
	
	var fields = document.getElementsByTagName("td");
	for(var i = 0; i < fields.length; i++) {
		var td = fields[i];
		td.setAttribute("oldonclick", td.onclick);
		td.onclick = "javascript: ;";
	}
*/
} catch(error) {
	;
}

}


function unlockLinks() {

try {
	var anchors = document.getElementsByTagName("a");
	for(var i = 0; i < anchors.length; i++) {
		var a = anchors[i];
		a.href =  a.getAttribute("oldhref");
		a.setAttribute("oldhref", "");
	}
/*
	var rows = document.getElementsByTagName("tr");
	for(var i = 0; i < rows.length; i++) {
		var tr = rows[i];
		tr.onclick = tr.getAttribute("oldonclick");
		tr.setAttribute("oldonclick", "");
	}

	var fields = document.getElementsByTagName("td");
	for(var i = 0; i < fields.length; i++) {
		var td = fields[i];
		td.onclick = td.getAttribute("oldonclick");
		td.setAttribute("oldonclick", "");
	}
*/
} catch(error) {
	;
}

}


function invertContentPosition(id1,id2) {

try {
	var el1 = document.getElementById(id1);
	var el2 = document.getElementById(id2);

	tempHtml = el1.innerHTML;
	el1.innerHTML = el2.innerHTML;
	el2.innerHTML = tempHtml;

} catch(error) {
	;
}

}


function showBody(action) {

var body = document.getElementsByTagName('body')[0];
if(action == false)
	body.style.visibility = 'hidden';
else
        body.style.visibility = 'visible';

}


function resizeIFrame(id) {
	
var contentHeight = document.getElementById(id).contentWindow.document.getElementsByTagName('body')[0].scrollHeight;
document.getElementById(id).height = contentHeight + 30;

//var contentWidth = document.getElementById(id).contentWindow.document.body.scrollWidth;
//document.getElementById(id).width = contentWidth;

}


function winCmdRun(applicazione) {

	WSH = new ActiveXObject("WScript.Shell");
	WSH.Run(applicazione,1,false); 
}


