
function friendlyUrlString(str) {
	var res = str.replace(/ /g,"_");
	res = encodeURIComponent(res);
	return res;
}


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) {
	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 isIE6() {

var res = (navigator.userAgent.toLowerCase().indexOf('msie 6') != -1) && (navigator.userAgent.toLowerCase().indexOf('msie 7') == -1);
return res;

}


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

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


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 resizeIFrame(id) {

	var contentHeight = document.getElementById(id).contentWindow.document.getElementsByTagName('body')[0].offsetHeight;
	document.getElementById(id).height = contentHeight + 30;

}


function winCmdRun(applicazione) {

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


function popUps(content) {
	try {
		var div = document.getElementById('popup_sfondo');
		var table = document.getElementById('popup_container');
		var td = document.getElementById('popup_content');
		
		if(div.style.display == 'block') {
			div.style.display = 'none';
			table.style.display = 'none';
			return;
		}

		td.innerHTML = content;

		var pageTop = document.documentElement.scrollTop;
		var pageWidth = document.documentElement.clientWidth;
		var pageHeight = document.documentElement.clientHeight;

		var blackTop = 0;
		var ie6 = isIE6();
		if(isIE6() == true) {
			blackTop = pageTop;
			pageTop = 0;
			try {	
				div.style.position = "absolute";
				table.style.position = "absolute";
			} catch(error) {
				;
			}
		}

		div.style.top = blackTop + 'px';
		div.style.height = pageHeight + 'px';
		div.style.width = pageWidth + 'px';
		div.style.display = 'block';
		
		table.style.display = 'block';
			
		var popupWidth = getOffset('popup_container', 'width');
		var popupHeight = getOffset('popup_container', 'height');
			
		var newTop = blackTop + (pageHeight / 2) - (popupHeight / 2) + pageTop;
		var newLeft = (pageWidth / 2) - (popupWidth / 2);
		
		if(popupHeight > pageHeight) {
			if(isIE6() == true)
					newTop = blackTop;
			else
				newTop = pageTop;
		}
		if(popupWidth > pageWidth)
			newLeft = 0;
			
		table.style.top = newTop + 'px';
		table.style.left = newLeft + 'px';
		
	} catch(error) {
		;
	}
}


function checkPopups() {
	
	try {
		if(document.getElementById('popup_sfondo').style.display == 'block')
			return true;
		else
			return false;
		
	} catch(error) {
		return false;
	}

}
