/* ------------------------------------------------------------------------------- */
/* 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, pdf_riservati_const)
{
	window.opener.focus();
	window.opener.AddAttachmentOnLayout(id, description, type, img_const, pdf_const, pdf_riservati_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, pdf_riservati_const, cat_parent_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';
	}
	// pdf riservati
	else if (type == pdf_riservati_const) {
		singolare = 'un nuovo documento riservato';
		plurale = 'documenti riservati';
	}
	// categorie di appartenenza
	else if (type == cat_parent_const) {
		singolare = 'una nuova categoria di appartenenza';
		plurale = 'categorie';
	}
	
	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 (da nuova immagine)          */
/* ------------------------------------------------------------------------------- */
function AddAttachment(type, n, limit, inserisci_popup_w, inserisci_popup_h, img_const, pdf_const, pdf_riservati_const, cat_parent_const, allegati_const, 
		insert_const)
{
	if (CheckLimit(type, n, limit, img_const, pdf_const, pdf_riservati_const, cat_parent_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, pdf_riservati_const, cat_parent_const)
{
	if (CheckLimit(type, n, limit, img_const, pdf_const, pdf_riservati_const, cat_parent_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, pdf_riservati_const)
{
	var type_tmp;
	if (type==img_const) {
		n_current = n_img_current;
		type_tmp = img_const;
	}
	else if (type==pdf_const || type==pdf_riservati_const) {
		n_current = n_pdf_current;
		type_tmp = pdf_const;
	}
	if (description==""){
		description = "- DESCRIZIONE VUOTA -";
	}
	new_div = document.createElement('div');
	new_div.setAttribute('id', 'browse-'+type_tmp+'-'+n_current);
	if (type==pdf_riservati_const) {
		new_div.className = 'insert-attachment-description-pdf-riservati';
	}
	else {
		new_div.className = 'insert-attachment-description';
	}
	new_div.innerHTML += description;
	new_div.innerHTML += '<input type="hidden" name="browse-'+type_tmp+'-'+n_current+'" value="'+id+'">';
	document.getElementById('browse-place-'+type_tmp).appendChild(new_div);
	
	if (type==pdf_riservati_const) {
		new_select = document.createElement('select');
		new_select.setAttribute('name', 'browse-select-'+type_tmp+'-'+n_current);
		new_select.className = 'insert-attachment-select';
		
		new_option = document.createElement('option');
		new_option.setAttribute('value', 1);
		new_option.setAttribute('selected', 'selected');
		new_option.innerHTML = 'Italiano';
		new_select.appendChild(new_option);
		
		new_option = document.createElement('option');
		new_option.setAttribute('value', 2);
		new_option.innerHTML = 'Inglese';
		new_select.appendChild(new_option);
		
		new_option = document.createElement('option');
		new_option.setAttribute('value', 4);
		new_option.innerHTML = 'Francese';
		new_select.appendChild(new_option);
		
		new_option = document.createElement('option');
		new_option.setAttribute('value', 5);
		new_option.innerHTML = 'Spagnolo';
		new_select.appendChild(new_option);
		
		document.getElementById('browse-place-'+type_tmp).appendChild(new_select);
	}
	
	if (type==img_const) {
		n_img_current++;
		n_img_tot++;
	}
	else if (type==pdf_const || type==pdf_riservati_const) {
		n_pdf_current++;
		n_pdf_tot++;
	}
}


/* ------------------------------------------------------------------------------- */
/* Aggiunge un campo per l'inserimento di una categoria di appartenenza            */
/* ------------------------------------------------------------------------------- */
function AddCatParentOnLayout(id, description) {
	//crea div nome categoria
	var new_div = document.createElement('div');
	new_div.className = 'cat-parent-description';
	new_div.innerHTML += description;
	new_div.innerHTML += '<input type="hidden" id="browse-cat-parent-'+n_cat_parent+'" name="browse-cat-parent-'+n_cat_parent+'" value="'+id+'">';
	
	//crea link di cancellazione
	var new_del_a = document.createElement('a');
	new_del_a.className = 'cat-parent-delete';
	new_del_a.setAttribute('id', 'cat-parent-delete-'+n_cat_parent);
	new_del_a.setAttribute('title', 'Elimina categoria');
	new_del_a.i = n_cat_parent;
	new_del_a.onclick = function() { RemoveCatParentOnLayout(this.i) };
	new_del_a.setAttribute('href', 'javascript:void(0)');
		
	//crea div container
	var container_div = document.createElement('div');
	container_div.setAttribute('id', 'cat-parent-container-'+n_cat_parent);
	container_div.className = 'cat-parent-container';
	container_div.appendChild(new_div);
	container_div.appendChild(new_del_a);
	
	//appende il tutto
	document.getElementById('cat-parent-place').appendChild(container_div);
	n_cat_parent++;
}


/* ------------------------------------------------------------------------------- */
/* Rimuove un campo per l'inserimento di una categoria di appartenenza             */
/* ------------------------------------------------------------------------------- */
function RemoveCatParentOnLayout(n) {
	n_cat_parent--;
	document.getElementById('cat-parent-place').removeChild(document.getElementById('cat-parent-container-'+n));

	//itera su tutti i nodi "sottostanti" per sistemare gli id
	var len = document.getElementById('cat-parent-place').childNodes.length;
	var childNode;
	for (i=(n+1);i<(len+1);i++) {
		//sistema id div container
		childNode = document.getElementById('cat-parent-container-'+i);
		childNode.setAttribute('id', 'cat-parent-container-'+(i-1));
	
		//sistema id e name "input"
		childNode = document.getElementById('browse-cat-parent-'+i);
		childNode.setAttribute('id', 'browse-cat-parent-'+(i-1));
		childNode.setAttribute('name', 'browse-cat-parent-'+(i-1));
		
		//sistema id e onclick "a"
		childNode = document.getElementById('cat-parent-delete-'+i);
		childNode.setAttribute('id', 'cat-parent-delete-'+(i-1));
		childNode.i = (i-1);
		childNode.onclick = function() { RemoveCatParentOnLayout(this.i) };
	}	
}


/* ------------------ */
/* 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'); 
			}
	}
}