/* ------------------------------------------------------------------------------- */
/* Nasconde/mostra un elemento */
/* ------------------------------------------------------------------------------- */
function switch_el(el)
{
	if (document.getElementById(el).style.display == 'none') { document.getElementById(el).style.display = ''; } 
	else { document.getElementById(el).style.display = 'none'; }
}

/* ------------------------------------------------------------------------------- */
/* Cambia l'icona */
/* ------------------------------------------------------------------------------- */
function switch_icon(el, iconID, open_icon, close_icon)
{
	if (document.getElementById(el).style.display == 'none') { document[iconID].src = '../images/admin/'+open_icon; }
	else { document[iconID].src = '../images/admin/'+close_icon; }
} 

/* ------------------------------------------------------------------------------- */
/* Sulla base della risoluzione dello schermo ritorna la larghezza di un popup sulla base della grandezza specificata */
/* ------------------------------------------------------------------------------- */
function GetPopUpWidth(size)
{
scr_width = screen.width;
if (size == "verysmall") {reduct = 0.2;}	
if (size == "small") {reduct = 0.3;}	
if (size == "smallmedium") {reduct = 0.4;}	
if (size == "medium") {reduct = 0.5;}	
if (size == "mediumlarge") {reduct = 0.6;}	
if (size == "large") {reduct = 0.7;}	
if (size == "verylarge") {reduct = 0.8;}	
popup_width = scr_width * reduct;
popup_width = Math.round(popup_width);
return popup_width;
}

/* ------------------------------------------------------------------------------- */
/* Sulla base della larghezza del popup ritorna la sua altezza */
/* ------------------------------------------------------------------------------- */
function GetPopUpHeight(width)
{
popup_height = width * 0.77;
popup_height = Math.round(popup_height);
return popup_height;
}

/* ------------------------------------------------------------------------------- */
/* Apre un popup centrato nello schermo */
/* ------------------------------------------------------------------------------- */
function OpenPopUp(mypage, myname, w, h, scroll)
{
LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
settings = 'height='+h+', width='+w+', top='+TopPosition+', left='+LeftPosition+', scrollbars='+scroll+', resizable'
return window.open(mypage, myname, settings)
}

/* ------------------------------------------------------------------------------- */
/* Chiude il popup e ricarica la pagina padre */
/* ------------------------------------------------------------------------------- */

function ClosePopUpAndReloadParent()
{
	window.opener.focus();
	window.opener.location.reload();
	window.close();
}


/* ------------------------------------------------------------------------------- */
/* Chiude il popup e aggiunge la descrizione e l'id dell'allegato al parent        */
/* ------------------------------------------------------------------------------- */

function ClosePopUpAndAddAttachment(id, description, type, img_const, pdf_const)
{
	window.opener.focus();
	window.opener.AddAttachmentOnLayout(id, description, type, img_const, pdf_const)
	window.close();
}


/* ------------------------------------------------------------------------------- */
/* Chiude il popup e aggiorna l'area principale via ajax                           */
/* ------------------------------------------------------------------------------- */

function ClosePopUpAndReloadMainArea(area, cat, $current_page)
{
	window.opener.focus();
	window.opener.InitAdminRequestMainArea('', area, cat, $current_page);
	window.close();
}


/* ------------------------------------------------------------------------------- */
/* Chiude il popup ma non ricarica la pagina padre */
/* ------------------------------------------------------------------------------- */

function ClosePopUp()
{
	window.close();
}


/* ------------------------------------------------------------------------------- */
/* Redirect alla pagina passata come parametro */
/* ------------------------------------------------------------------------------- */

function RedirectToPage(page)
{
	window.location = page;
}


/* ------------------------------------------------------------------------------- */
/* Redirect alla pagina passata come parametro */
/* ------------------------------------------------------------------------------- */

function ShowToolTip(content, width)
{
	return '&lt;div class=&quot;popupEventTitle&quot; style=&quot;width:' + width + 'px&quot;&gt;' + content + '&lt;/div&gt;';
}


/* ------------------------------------------------------------------------------- */
/* Aggiunge un campo per l'inserimento di un'immagine */
/* ------------------------------------------------------------------------------- */
function CheckLimit(type, n, limit, img_const, pdf_const)
{
	var ret = true;
		
	// immagini
	if (type == img_const)
	{
		singolare = 'una nuova immagine';
		plurale = 'immagini';
	}
	// pdf
	else if (type == pdf_const)
	{
		singolare = 'un nuovo documento';
		plurale = 'documenti';
	}
	
	if (n == limit && limit != -1) 
	{
		alert('Raggiunto il numero massimo di '+plurale+', impossibile aggiungere '+singolare);
		ret = false;
	}
	return ret;
}


/* ------------------------------------------------------------------------------- */
/* Aggiunge un campo per l'inserimento di un'immagine */
/* ------------------------------------------------------------------------------- */
function AddAttachment(type, n, limit, inserisci_popup_w, inserisci_popup_h, img_const, pdf_const, allegati_const, insert_const)
{
	if (CheckLimit(type, n, limit, img_const, pdf_const))
	{
		OpenPopUp('./insert.php?area='+allegati_const+'&cat='+type+'&action='+insert_const+'&add_attach=1', 'inserisci2', inserisci_popup_w, 
			inserisci_popup_h, 'yes');
	}
}


/* ------------------------------------------------------------------------------- */
/* Aggiunge un campo per l'inserimento di un'immagine (da sfoglia)                 */
/* ------------------------------------------------------------------------------- */
function AddExistingAttachment(type, n, limit, inserisci_popup_w, inserisci_popup_h, img_const, pdf_const)
{
	if (CheckLimit(type, n, limit, img_const, pdf_const))
	{
		OpenPopUp('./choose_attachment.php?cat='+type, 'choose', inserisci_popup_w,	inserisci_popup_h, 'yes');
	}
}


/* ------------------------------------------------------------------------------- */
/* Aggiunge un campo per l'inserimento di un'immagine                              */
/* ------------------------------------------------------------------------------- */
function AddAttachmentOnLayout(id, description, type, img_const, pdf_const)
{
	var num_temp;
	if (type==img_const)	{
		n_current = n_img_current;
	}
	else if (type==pdf_const)	{
		n_current = n_pdf_current;
	}
	if (description==""){
		description = "- DESCRIZIONE VUOTA -";
	}
	new_div = document.createElement('div');
	new_div.setAttribute('id', 'browse-'+type+'-'+n_current);
	new_div.className = 'insert-attachment-description';
	new_div.innerHTML += description;
	new_div.innerHTML += '<input type="hidden" name="browse-'+type+'-'+n_current+'" value="'+id+'">';
	document.getElementById('browse-place-'+type).appendChild(new_div);
	if (type==img_const)	{
		n_img_current++;
		n_img_tot++;
	}
	else if (type==pdf_const)	{
		n_pdf_current++;
		n_pdf_tot++;
	}
}


/* ------------------ */
/* Fade in e fade out */
/* ------------------ */
var opac = 0;

function ShowDivFadeIn(div)
{
	document.getElementById(div).style.display = '';
	if(opac != 100)
	{
		opac+=10;
		//document.getElementById(div).style.filter = 'alpha(opacity='+opac+')';
		if (document.getElementById(div).style.mozOpacity != null) document.getElementById(div).style.mozOpacity = opac/100;
		if (document.getElementById(div).style.khtmlOpacity != null) document.getElementById(div).style.khtmlOpacity = opac/100;
		if (document.getElementById(div).style.opacity != null) document.getElementById(div).style.opacity = opac/100;
		setTimeout('ShowDivFadeIn("'+div+'")', 20);
	}
}

function HideDivFadeOut(div)
{
	if(opac != 0)
	{
		opac-=10;
		//document.getElementById(div).style.filter = 'alpha(opacity='+opac+')';
		if (document.getElementById(div).style.mozOpacity != null) document.getElementById(div).style.mozOpacity = opac/100;
		if (document.getElementById(div).style.khtmlOpacity != null) document.getElementById(div).style.khtmlOpacity = opac/100;
		if (document.getElementById(div).style.opacity != null) document.getElementById(div).style.opacity = opac/100;
		setTimeout('HideDivFadeOut("'+div+'")', 20);
	}
	else
	{
		document.getElementById(div).style.display = 'none';
	}
}


/* ---------------------------------------------------------- */
/* Setta lo stato dei pulsanti modifica e cancella categorie  */
/* ---------------------------------------------------------- */
function SetCategoryButton(type, area, cat, tipo_categorie_const, insert_update_const, hide_insert_button)
{
	if (type == 0) {
		if (hide_insert_button)	{
			if (document.getElementById("pulsante-inserisci-categoria")){
				document.getElementById("pulsante-inserisci-categoria").onclick = 
					function() { 
						alert('Attenzione: profondita\' categorie massima raggiunta.'); 
					}
 			}
		}
		else {
			//aggiorna onclick del pulsante di inserimento
			if (document.getElementById("pulsante-inserisci-categoria")){
				document.getElementById("pulsante-inserisci-categoria").onclick = 
					function() { 
						OpenPopUp('./insert.php?area='+area+'&cat='+cat+'&item_type='+tipo_categorie_const+'&action='+insert_update_const, 'inserisci', 
							inserisci_popup_w, inserisci_popup_h, 'yes'); 
					}
			}
		}
	} else if (type == 1){
		//caso pulsante modifica
		document.getElementById("pulsante-modifica-categoria").style.display = "block";
		document.getElementById("pulsante-modifica-categoria").onclick = 
			function() { 
				OpenPopUp('./insert.php?area='+area+'&id='+cat+'&item_type='+tipo_categorie_const+'&action='+insert_update_const, 'inserisci', 
					inserisci_popup_w, inserisci_popup_h, 'yes'); 
			}
	
	} else if (type == 2){
		//caso pulsante cancella
		document.getElementById("pulsante-cancella-categoria").style.display = "block";
		document.getElementById("pulsante-cancella-categoria").onclick = 
			function() { 
				OpenPopUp('./delete.php?area='+area+'&id='+cat+'&item_type='+tipo_categorie_const, 'elimina', elimina_popup_w, elimina_popup_h, 'yes'); 
			}
	}
}