// COOKIES
function getCookie(NameOfCookie){
	if (document.cookie.length > 0){
		begin = document.cookie.indexOf(NameOfCookie+"=");
		if (begin != -1) {
			begin += NameOfCookie.length+1;
			end = document.cookie.indexOf(";", begin);
			if (end == -1) end = document.cookie.length;
			return unescape(document.cookie.substring(begin, end));
		}
	}
	return null;
}
function setCookie(NameOfCookie, value, expiredays, path, domain, secure) {
	var cookie;
	var ExpireDate = new Date ();
	ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
	cookie = NameOfCookie + "=" + escape(value);
	cookie = cookie + ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
	cookie = cookie + ((path == null) ? "" : "; path=" + path);
	cookie = cookie + ((domain == null) ? "" : "; domain=" + domain);
	cookie = cookie + ((secure == null) ? "" : "; secure=" + secure);
	document.cookie = cookie;
}
function delCookie(NameOfCookie) {
	if (getCookie(NameOfCookie)) {
		document.cookie = NameOfCookie + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}
// ADD EVENTS: Aggiunge una funzione da eseguire al verificarsi di un evento
function addEvent(obj,ev,fn){
	if(obj.addEventListener) { //alert('w3c'); // metodo w3c 
		obj.addEventListener(ev, fn, false);
	} else if(obj.attachEvent) { //alert('IE'); // metodo IE
		obj.attachEvent('on'+ev, fn);
	} else { //otherwise alert('otherwise');
		if(typeof(obj['on'+ev])=='function'){
			var f=obj['on'+ev];			
			obj['on'+ev]=function(){f();fn();}
		}
		else { obj['on'+ev]=fn; }		
	}
}
//MESSAGE BOX
function hideMsg() {
	var msgBox;
	msgBox = document.getElementById('msgBox');
	if (msgBox) msgBox.style.display = 'none';
};
function delayHideMsg(sec) {
	window.setTimeout('hideMsg()', (sec*1000) );
};
function showMsg(sec) {
	var msgText, msgBox;
	msgText = document.getElementById('msg');
	msgBox = document.getElementById('msgBox');
	
	if (msgText && msgBox) {
		if ( msgText.innerHTML != '') {
			msgBox.style.display = 'block';
			delayHideMsg(sec);
		}
	}
};
function setMsg(txt) {
	var msgText;
	msgText = document.getElementById('msg');
	if (msgText) msgText.innerHTML = txt;
}
function onLoadShowMsg() { showMsg(3); }			
addEvent(window, 'load', onLoadShowMsg);
//Apre una finestra popup
function openAsPopup(obj) {
	window.open( obj.href, 'popup', 'width=800px,height=600px');
	return false;	
}
// MENU
//Gestisce l'apertura e la chiusura dei menu
var exploded_menus = new Array(); 
function onMenuClick(id) {
	var submenu = document.getElementById(id);
	var cookie = getCookie('exploded_menus');
	if (cookie) cookie = new String(cookie); else cookie='';
	cookie = cookie.split(',');	
	delCookie('exploded_menus');
	if( !exploded_menus[id] ) {
		submenu.className = 'show';
		exploded_menus[id] = true;
		cookie.push(id);		
		setCookie('exploded_menus', cookie.join(','), null, '/' );
	} else {
		submenu.className='hide';		
		exploded_menus[id] = false;		
		cookie[cookie.indexOf(id)] = '';
		setCookie('exploded_menus', cookie.join(','), null, '/' );
	}
}
//Apre il menu memorizzato nel cookie
function explodeCurrentMenus() {
	var i, id, submenu;
	var cookie = getCookie('exploded_menus');
	if (cookie) cookie = new String(cookie); else cookie='';
	cookie = cookie.split(',');		
	for ( i=0; i<cookie.length; i++ ) {
		id = cookie[i];
		if (id) {			
			submenu = document.getElementById(id);	
			submenu.className = 'show';
			exploded_menus[id] = true;
		}
	}
}
//addEvent(window, 'load', explodeCurrentMenus);

//FORM: Controlli sull'inserimento dei dati 
function formRequired(form, name, msg) { //Campo obbligatorio
	if (!form) alert('form non č definito! '+name);
	if (!form[name].value) {
		alert(msg);
		form[name].focus();
		return false;
	}
	return true;			
}
function formChecked(form, name, msg) { //Campo obbligatorio
	if (!form[name].checked) {
		alert(msg);
		form[name].focus();
		return false;
	}
	return true;			
}
function formInteger(form, name, msg) { //Campo intero
	if ( form[name].value) {
		if (isNaN( form[name].value) ) {
			alert(msg);
			form[name].focus();
			return false;
		}
		if (parseInt(form[name].value) != parseFloat(form[name].value)) {
			alert(msg);
		} else form[name].value = parseInt(form[name].value);
	}
	return true;
}
function formNumber(form, name, msg) { //Campo Numerico
	if ( form[name].value ) {
		if (isNaN( form[name].value) ) form[name].value = form[name].value.replace(',','.');
		
		if (isNaN( form[name].value) ) {
			alert(msg);
			form[name].focus();
			return false;
		} else form[name].value = parseFloat(form[name].value);
	}
	return true
}

function formEmail(form, name, msg) { //Campo Email
	var regExp_email = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/;		// @
	if(!form[name].value.match(regExp_email)) {
		alert(msg);
		form[name].focus();
		return false;
	}
	return true;	
}

function formDate(form, name, msg) { //Campo data 
	var regExp_date = /\b(0?[1-9]|[12][0-9]|3[01])[\/-](0?[1-9]|1[012])[\/-](19|20)?[0-9]{2}\b/;		//gg/mm/aaaa
	if(!form[name].value.match(regExp_date)) {
		alert(msg);
		form[name].focus();
		return false;
	}			
	return true;	
}
function formYear(form, name, msg) { //Campo anno
	var regExp_date = /\b(19|20)?[0-9]{2}\b/;		//aaaa
	if(!form[name].value.match(regExp_date)) {
		alert(msg);
		form[name].focus();
		return false;
	}			
	return true;	
}
function formTime(form,name,msg){//Campo Tempo
	var regExp_time = /^([01]\d|2[0-3]):([0-5]\d)$/; //HH:mm
	if(!form[name].value.match(regExp_time)){
		alert(msg);
		form[name].focus();
		return false;
	}
	return true;
}
//UTILITI
//Restituisce gg-mm-aaaa con un giorno in pių prende gg-mm-aaaa
function addDay(data){
	var datasplit = data.split("-");
	newdata = new Date(datasplit[2],(datasplit[1]-1),datasplit[0]);
	newdata.setDate(newdata.getDate()+1);
	var sGiorno = (newdata.getDate()<10) ? "0" + newdata.getDate().toString() : newdata.getDate().toString(); 
	var sMese = (newdata.getMonth()<9) ? "0" + (newdata.getMonth() + 1).toString() : (newdata.getMonth() + 1).toString();
	var sAnno = newdata.getFullYear().toString();
	var sDataCompleta = "".concat(sGiorno,"-",sMese,"-",sAnno); 
	return sDataCompleta;
}
