function get(id) {return document.getElementById(id);}
//Dato un array di param -> value
//con i nuovi parametri per richiamare la pagina corrente
//restituisce l'url da richiamare
//@param arr deve contenere un array con param e value
//Se il parametro e' gia' presente sostituisce il valore
function url_replace (arr) {
	var param="";
	var new_doc = new String(window.location);
	if (!window.location.search) {
		new_doc+="?";
	}
	for (i=0; i<arr.length; i++) {
		param=arr[i][0];
		newvalue=arr[i][1];
		var value=getParameter(arr[i][0]);
		if (newvalue=="") { //se il nuovo valore e' null
			if (value) { //tolgo il valore dall'url attuale
				param=param.replace("[", "\\[");
				param=param.replace("]", "\\]");
				var re = new RegExp ('&'+param+'='+value, 'gi') ;
				var new_doc = new_doc.replace(re, "") ;
			}
		} else {
		    newstr=param+'='+newvalue;
			if (value) { //sostituisco il valore
			param=param.replace("[", "\\[");
			param=param.replace("]", "\\]");
				var re = new RegExp (param+'='+value, 'gi') ;
				var new_doc = new_doc.replace(re, newstr) ;
			} else {
				new_doc=new_doc+"&"+param+"="+newvalue;
			}
		}
	}
	return new_doc;
}
//Richiama la stessa pagina (con lo stesso querysring) con il nuovo valore
//arr deve contenere un array con param e value
//Se il parametro e' gia' presente sostituisce il valore
function pag_change (arr) {
	document.location=url_replace(arr);
}
function pop_change (arr) {
	pag="ges_entity.php?";
	for (i=0; i<arr.length; i++) {
		param=arr[i][0];
		value=arr[i][1];
		pag+=param+"="+value+"&";
	}
	//Aggiunge il parametro popup in modo che la pagina chiamata sappia
	//di essere in una popup
	openW(pag+"&popup=1", 'dettaglio', 700, 500, 1);
}
function getParameter(name){
	var txt=window.location.search.replace("?", "");
	arr = txt.split("&")
	for (var i = 0; i < arr.length;i++) {
		param=arr[i].split("=");
		if (param[0]==name) {
			return param[1];
		}
	}
	return "";
}
//Gestione delle date
function openCalendar(idCampo) {
	window.open("ges_popdate.html?data="+document.getElementById(idCampo).value+"&idCampo="+idCampo, "calend", "width=220, height=170, resizable");
}
function openCalendar_class(idCampo) {
	window.open("ges_popdate.html?data="
				+document.getElementById(idCampo+"_mm").value+"/"
				+document.getElementById(idCampo+"_dd").value+"/"
				+document.getElementById(idCampo+"_yy").value
				+"&tipo=class"
				+"&idCampo="+idCampo,
				 "calend", "width=260, height=180, resizable");
}
function setNewDate(idCampo, value, description) {
	document.getElementById(idCampo).value=value;
	if (document.getElementById("txt_"+idCampo)) {
		document.getElementById("txt_"+idCampo).innerHTML = description;
	}
}
function setNewDate_class(idCampo, date_mm, date_dd, date_yy, description) {
	document.getElementById(idCampo+"_mm").value=date_mm;
	document.getElementById(idCampo+"_dd").value=date_dd;
	document.getElementById(idCampo+"_yy").value=date_yy;
	if (document.getElementById("txt_"+idCampo)) {
		document.getElementById("txt_"+idCampo).innerHTML = description;
	}
}
function checkDate(idCampo) {
	var txtDate=document.getElementById(idCampo).value;
	var description="";
	if (txtDate!="") {
		var day = new Date(txtDate);
		description=(	isNaN(day.getYear())	)
						?"#"
						:getDateDescription(day);
	}
	if (document.getElementById("txt_"+idCampo)) {
		document.getElementById("txt_"+idCampo).innerHTML = description;
	}
}
function checkDate_class(idCampo) {
	var txtDate=document.getElementById(idCampo+"_mm").value	+"/"
			+	document.getElementById(idCampo+"_dd").value	+"/"
			+	document.getElementById(idCampo+"_yy").value;
	var description="";
	if (txtDate!="") {
		var day = new Date(txtDate);
		description=(	isNaN(day.getYear())	)
						?"#"
						:getDateDescription(day);
	}
	if (document.getElementById("txt_"+idCampo)) {
		document.getElementById("txt_"+idCampo).innerHTML = description;
	}
}
function getComboMonth (otherparams) {
	out="<select name='date_mm' id='month' "+otherparams+">";
	for (var i = 0; i < txtMonth.length; i++) {
		out+="<option value="+(i+1)+">"+txtMonth[i]+"</option>";
	}
	out+="</select>";
	return out;
}
function getDateDescription (day) {
	var description = txtDay[day.getDay()]+" " + day.getDate() + " "+txtMonth[day.getMonth()]+" "+day.getFullYear();
	return description;
}

//Gestione dei campi time
function hour_add(obj, val) {
	h=(Number(obj.value)+val);
	if (h>23) h=0;
	if (h<0) h=23;
	h=""+h;
	if (h.length==1) h="0"+h;
	obj.value=h;	
}
//Gestione HyperCombo
function setHyperComboValue (idcampo, val) {
	document.getElementById(idcampo).value = val;
	document.getElementById(idcampo).focus();
}
function openSQLHyperCombo (table, column, where, idCampo, width, height) {
	url="ges_hypercombo_sql.php?table="+table+"&column="+column+"&where="+where;
	openHyperCombo (url, idCampo, idCampo, "", width, height, "")
}
function openHyperCombo (url, idWindow, idCampo, idReferer, width, height, otherScript) {
	if (otherScript) {
       	var nomeFn = otherScript.split("(")[0];
		if (eval(nomeFn)) { //Se la funzione esiste
			url = eval(otherScript);
        } else {
           	alert("ERRORE di sviluppo! \n\nTAGHyperCombo: \nLo script impostato come windowScript non esiste all'interno della pagina. Impossibile continuare.");
			return;
		}
	}
	if (url.indexOf('?')!=-1) 	url = url + "&idcampo="+idCampo;
	else						url = url + "?idcampo="+idCampo;
	if (idReferer) {
		if (document.getElementById(idReferer)) url = url + "&idreferer="+document.getElementById(idReferer).value;
		else									alert("ERRORE di sviluppo! \n\nTAGHyperCombo: \nIl referer impostato ["+idReferer+"] non esiste.\n\nIl referer verr? ignorato.");
	}
	openW(url, idWindow, width, height, 1);
}
function openW(win,title,width,height,scroll) {
	var x;
	var y;
	var param;
        //Valori di default
        if (!width) width='750';
        if (!height) height='400';
	x = screen.width / 2 - width / 2;
	y = screen.height / 2 - height / 2;
	param = "width="+width+",height="+height+",left="+x+",top="+y+",scrollbars="+scroll+",toolbar=0,location=0,directories=0,menuBar=0,resizable=1";
	window.open(win,title,param);
}
function vis_src(idName, bool) {
	if (!bool) {
		window["bk_"+idName]=get("div_"+idName).innerHTML;
		get("div_"+idName).innerHTML="";
		get("div_"+idName).style.display="none";
	} else {
		get("div_"+idName).style.display="block";
		get("div_"+idName).innerHTML=window["bk_"+idName];
	}
}
function ch_vis(id_name, save) {
	var obj=get(id_name);
	var img=obj.IMG;
	if (obj.style.display!="none") {
		obj.style.display="none";
		if (img && get(img)) {
			get(img).className=obj.IMGCLASS+"_on";
			get(img).title="Open";
		}
	} else {
		obj.style.display="block";
		if (img && get(img)) {
			get(img).className=obj.IMGCLASS;
			get(img).title="Close";
		}
	}
	//Salva le impostazioni sul cookie
	if (save) update_cookie(id_name);
}
//**
//** FUNZIONI PER I COOKIE
//**
function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1) endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)	return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break; 
	}
	return null;
}

function SetCookie (name, value) {
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (2 < argc) ? argv[2] : null;
	var path = (3 < argc) ? argv[3] : null;
	var domain = (4 < argc) ? argv[4] : null;
	var secure = (5 < argc) ? argv[5] : false;
	document.cookie = name + "=" + escape (value) 
		+ ((expires == null) ? "" : ("; expires=" + expires.toGMTString()))
		+ ((path == null) ? "" : ("; path=" + path))
		+ ((domain == null) ? "" : ("; domain=" + domain)) 
		+ ((secure == true) ? "; secure" : "");
}
function set_cookie(cookie_name, value) {
	var expdate = new Date();
	var visit;
	expdate.setTime(expdate.getTime() + (24 * 60 * 60 * 1000 * 365)); 
	SetCookie(cookie_name, value, expdate, "/", null, false);
}
//Utilizzo due Array, in uno salvo gli id, nell'alro le sequenze di valori
function update_cookie (id_name) {
	var updated=false;
	user_settings_key = GetCookie("user_settings_key")
	user_settings_val = GetCookie("user_settings_val")
	if(!user_settings_key) {
		user_settings_key=new Array();
		user_settings_val=new Array();
	} else {
		user_settings_key=user_settings_key.split(",")
		user_settings_val=user_settings_val.split(",")
	}
	for (var i = 0; i<user_settings_key.length; i++) {
		if(user_settings_key[i]==id_name) {
			user_settings_val[i]=get(id_name).style.display;
			set_cookie("user_settings_val", user_settings_val);
			updated=true;
		}
	}
	if (!updated) { //se non trovato nella lista lo aggiungo
		user_settings_key.push(id_name);
		set_cookie("user_settings_key", user_settings_key);
		user_settings_val.push(get(id_name).style.display);
		set_cookie("user_settings_val", user_settings_val);
	}
}
function get_user_settings() {
	//Da cookie: menu primo livello da visualizzare
	active_menu = GetCookie("active_menu");
	//ch_class(get("menu_"+active_menu), 'menu1_on');
	if(get("menu_"+active_menu) != null){ch_class(get("menu_"+active_menu), 'menu1_on');}
	//Impostazioni da cookie
	user_settings_key = GetCookie("user_settings_key")
	user_settings_val = GetCookie("user_settings_val")
	if(user_settings_key) {
		user_settings_key=user_settings_key.split(",")
		user_settings_val=user_settings_val.split(",")
		for (var i = 0; i<user_settings_key.length; i++) {
//			alert(user_settings_key+" - "+user_settings_val);
			if (obj=get(user_settings_key[i])) {
				img=get(obj.IMG);
				if (img && user_settings_val[i]=="block") {
					img.className=obj.IMGCLASS+"_on";
					img.title="Close";
				}
				obj.style.display=user_settings_val[i];
			}
		}
	}
	//Impostazioni di video
}
//*** FINE GESTIONE COOKIE

function inverti_check(nome_form) {
	var myForm=document.forms[nome_form];
	if (!myForm) return;
	for(i=0; i < myForm.length; i++) {
		if (myForm[i].type=="checkbox") {
			checked=(myForm[i].checked)?"":"checked";
			myForm[i].checked=checked;
//			myForm[i].onclick();
		}
	}
}