function VisTab(scheda) {
	switch (scheda) {
		case "DESC":
			document.getElementById('DESC').style.display = "block";
			document.getElementById('cardMenuDESC').className = "current";
			document.getElementById('STRU').style.display = "none";
			document.getElementById('cardMenuSTRU').className = "notCurrent";
			break
		case "STRU":
			document.getElementById('DESC').style.display = "none";
			document.getElementById('cardMenuDESC').className = "notCurrent";
			document.getElementById('STRU').style.display = "block";
			document.getElementById('cardMenuSTRU').className = "current";
			break
	}
	return false;
}


function ControllaCreaArchivio() {
	var formVuota = 0;
	msg_alert = "Attenzione!\nTutti i dati sono obbligatori!\n";
	//name
	if(document.getElementById('name').value == ""){
		formVuota = formVuota + 1;
	}
	//name_breve
	if(document.getElementById('name_breve').value == ""){
		formVuota = formVuota + 1;
	}
	//type
	if(document.getElementById('type').value == ""){
		formVuota = formVuota + 1;
	}
	//description
	if(document.getElementById('descrizione').value == ""){
		formVuota = formVuota + 1;
	}
	//type
	if(document.getElementById('fulltext').value == ""){
		formVuota = formVuota + 1;
	}
	if(formVuota > 0){
		alert(msg_alert);
		return false;
	}else{
		return true;
	}
}

function ControllaFormRicerca() {

	var formVuota = 0;
	msg_alert = "Attenzione!\nNessun archivio selezionato!\n";
	//ul
	if(document.getElementById('ul').value == "null"){
		formVuota = formVuota + 1;
	}else{
		FormOBJ = document.getElementById('search_form');
		conta_archivi_ricerca = 0;
		for(i=0;i<FormOBJ.length;i++){
			if(FormOBJ.elements[i].id == "ul"){
				if(FormOBJ.elements[i].checked){
					conta_archivi_ricerca++;
				}
			}
		}
		if (conta_archivi_ricerca == 0){
			formVuota = formVuota + 1;
		}
		
	}
	
	if(formVuota > 0){
		alert(msg_alert);
		return false;
	}else{
		return true;
	}
}

function ControllaCreaUtente() {
	var pass = "true";
	msg_alert = "Attenzione!\nNon sono stati compilati correttamente i seguenti campi:\n";
	//Controllo Nome
/*	if(document.getElementById('nome').value == ""){
		pass = "false";
		msg_alert += "- Nome (campo obbligatorio)\n";
	}
	//Controllo Cognome
	if(document.getElementById('cognome').value == ""){
		pass = "false";
		msg_alert += "- Cognome (campo obbligatorio)\n";
	}
	//Controllo Email
	if(document.getElementById('email').value == ""){
		pass = "false";
		msg_alert += "- Email (campo obbligatorio)\n";
	}
*/
	//Controllo Usename
	if(document.getElementById('username').value == ""){
		pass = "false";
		msg_alert += "- Username (campo obbligatorio)\n";
	}
	//Controllo Password
	if(document.getElementById('password').value == ""){
		pass = "false";
		msg_alert += "- Password (campo obbligatorio)\n";
	}else{
		if(document.getElementById('password').value != document.getElementById('conferma_password').value){
			pass = "false";
			msg_alert += "- Password e Conferma password (i dati non coincidono)\n";
		}
	}
	//Controllo UserType
	if(document.getElementById('user_type').value == ""){
		pass = "false";
		msg_alert += "- Tipo utente (campo obbligatorio)\n";
	}
	
	if(pass == "false"){
		alert(msg_alert);
		return false;
	}else{
		return true;
	}
}


function addRowToTable()
{
  var ArrControllo = new Array();
  var ArrControllo = document.getElementById('ArrControllo').value.split(",");
  var tbl = document.getElementById('STRUtbl');
  var lastRow = tbl.rows.length;
  // if there's no header row in the table, then iteration = lastRow + 1
  //var iteration = lastRow;
  
  var iteration = document.getElementById('NewDocNameID').value;
  
  var row = tbl.insertRow(lastRow);
  
  // left cell
  var cellLeft = row.insertCell(0);
  var el = document.createElement('input');
  el.type = 'text';
  el.name = 'docName[]';
  el.id = 'docName_' + iteration;
  el.size = 15;
  el.maxlength = 15;
  cellLeft.appendChild(el);
   
  // select cell
  var cellRightSel = row.insertCell(1);
  var sel = document.createElement('select');
  sel.name = 'dataType[]';
  sel.id = 'dataType_' + iteration;
  sel.options[0] = new Option('Stringa (255 char)', 'stringa255');
  sel.options[1] = new Option('Stringa (100 char)', 'stringa100');
  sel.options[2] = new Option('Testo lungo', 'testo');
  sel.options[3] = new Option('Data (YYYY-MM-DD)', 'data');
  sel.options[4] = new Option('Intero', 'intero');
  cellRightSel.appendChild(sel);
  
  
   // Del Img
  var cellRightDel = row.insertCell(2);
  var imgDel = document.createElement('img');
  
  imgDel.name = 'imgDel';
  imgDel.src = 'images/ico_deleteField_on.gif';
  
  imgDel.onclick = function () {deleteCurrentRow(this,'docName_'+iteration);};
  cellRightDel.appendChild(imgDel);
  
  
  ArrControllo.push(el.id);
  document.getElementById('ArrControllo').value = ArrControllo;
  
  var newID = parseInt(iteration) + 1;
  document.getElementById('NewDocNameID').value = newID;
}


function keyPressTest(e, obj)
{
  var validateChkb = document.getElementById('chkValidateOnKeyPress');
  if (validateChkb.checked) {
    var displayObj = document.getElementById('spanOutput');
    var key;
    if(window.event) {
      key = window.event.keyCode; 
    }
    else if(e.which) {
      key = e.which;
    }
    var objId;
    if (obj != null) {
      objId = obj.id;
    } else {
      objId = this.id;
    }
    displayObj.innerHTML = objId + ' : ' + String.fromCharCode(key);
  }
}


function removeRowFromTable()
{
  var tbl = document.getElementById('STRUtbl');
  var lastRow = tbl.rows.length;
  if (lastRow > 4) tbl.deleteRow(lastRow - 1);
}


function deleteCurrentRow(obj,nome)
{
		var ArrControllo = new Array();
		ArrControllo = document.getElementById('ArrControllo').value.split(",");
		var NEWArrControllo = new Array();
		//alert(ArrControllo);
		for(i=0;i<ArrControllo.length;i++){
			if(nome != ArrControllo[i]){
				NEWArrControllo.push(ArrControllo[i]);
			}
		}
		document.getElementById('ArrControllo').value = NEWArrControllo;
		
		var delRow = obj.parentNode.parentNode;
		var tbl = delRow.parentNode.parentNode;
		var rIndex = delRow.sectionRowIndex;
		var rowArray = new Array(delRow);
		if(document.getElementById('ArrDelete')){
			var ArrDelete = new Array(document.getElementById('ArrDelete').value);
			var elName = document.getElementById(nome);
			var OLDelName = document.getElementById('OLD'+nome);
			if(OLDelName){
				ArrDelete.push(OLDelName.value);
				document.getElementById('ArrDelete').value = ArrDelete;
			}
		}
		deleteRows(rowArray);
}


function deleteRows(rowObjArray)
{
		for (var i=0; i<rowObjArray.length; i++) {
			var rIndex = rowObjArray[i].sectionRowIndex;
			rowObjArray[i].parentNode.deleteRow(rIndex);
		}
}


function openInNewWindow(frm)
{
  // open a blank window
  var aWindow = window.open('', 'TableAddRowNewWindow',
   'scrollbars=yes,menubar=yes,resizable=yes,toolbar=no,width=400,height=400');
   
  // set the target to the blank window
  frm.target = 'TableAddRowNewWindow';
  
  // submit
  frm.submit();
}


function validateRow(frm)
{
  var chkb = document.getElementById('chkValidate');
  if (chkb.checked) {
    var tbl = document.getElementById('STRUtbl');
    var lastRow = tbl.rows.length - 1;
    var i;
    for (i=1; i<=lastRow; i++) {
      var aRow = document.getElementById('txtRow' + i);
      if (aRow.value.length <= 0) {
        alert('Row ' + i + ' is empty');
        return;
      }
    }
  }
  openInNewWindow(frm);
}


function ControllaCreaStruttura()
{
	var tbl = document.getElementById('STRUtbl');
  	var lastRow = tbl.rows.length-1;
  	var righeAggiunte = lastRow - 1;
	var i;
	var ArrNomiCampi = new Array("ID","nome_file","SYS_creation_user","SYS_creation_date","SYS_update_user","SYS_update_date");
	
    var ArrControllo = new Array();
	ArrControllo = document.getElementById('ArrControllo').value.split(",");
	//alert(ArrControllo);
	for(indice=0;indice<ArrControllo.length;indice++){
	  
	  numeroRiga = indice+1;
	  var aRow = document.getElementById(ArrControllo[indice]);

	  if(aRow){
	  	  if (aRow.value.length <= 0) {
	        alert('Nome campo della riga ' + numeroRiga + ' deve essere compilato');
	        return false;
	      }else{
	      	myString = aRow.value;
	      	if (myString.match(/\W/)) {
	      		alert('Nome campo della riga ' + numeroRiga + ' contiene caratteri non consentiti');
	        	return false;
	      	}else{
	      		var aggiungiNome = "FALSE";
	      		for(iNomi = 0; iNomi < ArrNomiCampi.length; iNomi++){
	      			if(aRow.value.toUpperCase() == ArrNomiCampi[iNomi].toUpperCase()){
	      				alert('Il \"Nome campo = '+aRow.value+'\" inserito nella riga ' + numeroRiga + ' è già utilizzato o non è consentito');
	      				return false;
	      			}else{
	      				aggiungiNome = "TRUE";
	      			}
	      		}
	      		if(aggiungiNome == "TRUE"){
	      			ArrNomiCampi.push(aRow.value);
	      		}
	      	}
	      }
	  }
    }
}



function submit_form(dest,element_name,form_name) {																		 
	if (element_name == "search"){							 
		if(document.getElementById('search_field').value == "#"){
			alert("Attenzione!!\nE necessario specificare un campo di ricerca");	
			return false;	
		}
		if(document.getElementById('search_value').value == ""){
			alert("Attenzione!!\nDigitare la parola da cercare");	
			return false	
		}				  
	}
	document[form_name].action = dest;
	document[form_name].submit();
	return true;

}

function submit_back(form_name) {
	document[form_name].submit();
	return true;
}

function annulla_ricerca(dest,form_name) {
	document.getElementById('search_field').value = "#";
	document.getElementById('search_value').value = "";
	document[form_name].action = dest;
	document[form_name].submit();
	return true;
}


function paginatore(dest,form_name) {
	document[form_name].action = dest;
	document[form_name].submit();
	return true;
}

function SchedaDocRicercaAvanzata(dest,form_name) {
	document[form_name].action = dest;
	document[form_name].submit();
	return true;
}

function BackSchedaDocRicercaAvanzata(dest,form_name) {
	document[form_name].action = dest;
	document[form_name].submit();
	return true;
}

function ControllaInserFile(form_name) {
	var formControllo = document.getElementById(form_name);
	var ArrControllo = new Array();
	ArrControllo = document.getElementById('ArrControllo').value.split(",");

	var campi_compilati = 0;
	var estensione_non_consentita = "FALSE";
	for(indice=0;indice<ArrControllo.length;indice++){
		if(document.getElementById(ArrControllo[indice]).value != ""){
			campi_compilati++;
			if(ArrControllo[indice] == "nome_file"){
				ControlloEstensione = document.getElementById('nome_file').value.split(".");
				estensione = ControlloEstensione[(ControlloEstensione.length)-1].toUpperCase();
				//Escludo le estensioni EXE,SH,PHP,ASP,BAT,BIN,PL,CGI
				if((estensione == "EXE") || (estensione == "SH") || (estensione == "PHP") || (estensione == "ASP") || (estensione == "BAT") || (estensione == "BIN") || (estensione == "PL") || (estensione == "CGI")){
					estensione_non_consentita = "TRUE";
				}
			}
		}
	}
	if(campi_compilati == 0){
		alert("Attenzione! Per salvare è necessario completare almeno un campo.");
		return false;
	}else{
		if(estensione_non_consentita == "TRUE"){
			alert("Attenzione! Estensione file non consentita.");
			return false;
		}else{
			return true;
		}
	}
}

function ControllaEditFile(form_name) {
	var formControllo = document.getElementById(form_name);
	var ArrControllo = new Array();
	ArrControllo = document.getElementById('ArrControllo').value.split(",");

	var campi_compilati = 0;
	var estensione_non_consentita = "FALSE";
	for(indice=0;indice<ArrControllo.length;indice++){
		if(ArrControllo[indice] == "nome_file"){
			if((document.getElementById('stato_nome_file').value == "NEW") && (document.getElementById('nome_file').value != "")){
				campi_compilati++;
				ControlloEstensione = document.getElementById('nome_file').value.split(".");
				estensione = ControlloEstensione[(ControlloEstensione.length)-1].toUpperCase();
				//Escludo le estensioni EXE,SH,PHP,ASP,BAT,BIN,PL,CGI
				if((estensione == "EXE") || (estensione == "SH") || (estensione == "PHP") || (estensione == "ASP") || (estensione == "BAT") || (estensione == "BIN") || (estensione == "PL") || (estensione == "CGI")){
					estensione_non_consentita = "TRUE";
				}
			}
		}else{
			if(document.getElementById(ArrControllo[indice]).value != ""){
				campi_compilati++;
			}
		}
	}

	if(campi_compilati == 0){
		alert("Attenzione! Per salvare è necessario completare almeno un campo.");
		return false;
	}else{
		if(estensione_non_consentita == "TRUE"){
			alert("Attenzione! Estensione file non consentita.");
			return false;
		}else{
			return true;
		}
	}
}

function ordina(dest,form_name) {

	document[form_name].action = dest;
	document[form_name].submit();
	return true;
}


function goToURL(dest) {
  window.location = dest;
}


function goToSearchRes(dest,form_name) {
	document[form_name].action = dest;
	document[form_name].submit();
	return true;
}

function PagPrec(form_name,max_pagine) {
	
	sequenza = document.getElementById('seq').value;
	
	pag_prec = parseInt(sequenza);
	pag_succ = parseInt(sequenza);
	
	if((pag_prec - 1) >= 0){
		pag_prec--;
		document.getElementById('seq').value = pag_prec;
		document[form_name].submit();
		return true;
	}else{
		return false;
	}
}

function PagSucc(form_name,max_pagine) {
	sequenza = document.getElementById('seq').value;
	pag_succ = parseInt(sequenza);
	
	if((pag_succ + 1) < max_pagine){
		pag_succ++;
		document.getElementById('seq').value = pag_succ;
		document[form_name].submit();
		return true;
	}else{
		return false;
	}
}


function selPermission(obj,livello) {
	FormPermission = document.getElementById('formPermission');

	for(i=0;i<FormPermission.length;i++){
		if(FormPermission.elements[i].id){
			if(obj.checked == 0){
				var ArrRadice = FormPermission.elements[i].id.split(".");
				var StrRadice = "";
				for(el=0;el<livello+1;el++){
					StrRadice += ArrRadice[el]+".";
				}
				if((obj.id == FormPermission.elements[i].id) || (obj.id+"." == StrRadice)){
					FormPermission.elements[i].checked = 0;
					
					href_ID = FormPermission.elements[i].id;
					href_ID = href_ID.substr(3,(href_ID.length));
					href_ID = "A_"+href_ID;
					if(document.getElementById(href_ID)){
						link_adv_permission = document.getElementById(href_ID);
						link_adv_permission.style.display = "none";
					}
				
					//alert(obj.id+".  <--> "+StrRadice+" ("+FormPermission.elements[i].id+")");
				}
			}else if(obj.checked == 1){
				var ArrElSel = FormPermission.elements[i].id.split(".");
				livelloElSel = (ArrElSel.length)-1;
				
				var ArrObj = obj.id.split(".");
				var StrRadice = "";
				for(el=0;el<=livelloElSel;el++){
					StrRadice += ArrObj[el]+".";
					
				}
				
				StrRadice = StrRadice.substr(0,(StrRadice.length-1));
				if(livelloElSel <= livello){
					if(FormPermission.elements[i].id == StrRadice){
						FormPermission.elements[i].checked = 1;
						
						href_ID = FormPermission.elements[i].id;
						href_ID = href_ID.substr(3,(href_ID.length));
						href_ID = "A_"+href_ID;
						if(document.getElementById(href_ID)){
							link_adv_permission = document.getElementById(href_ID);
							link_adv_permission.style.display = "inline";
						}
						//alert(FormPermission.elements[i].id+"  <--> Livello "+livelloElSel+" ("+StrRadice+")");
					}
				}
			}
		}
	}
}


function ControllaADVpermission() {
	var pass = "true";
	msg_alert = "Attenzione! Almeno un campo deve essere in lettura. E' necessario usare il modulo \"Gestione permission archivi\" per disabilitare l\'accesso all\'archivio.\n";

	FormOBJ = document.getElementById('formADVpermission');
	numero_campi_tabella = document.getElementById('numero_campi').value;
	conta_campi_N = 0;
	for(i=0;i<FormOBJ.length;i++){
		if((FormOBJ.elements[i].id) && (FormOBJ.elements[i].id != "numero_campi")){
			if(FormOBJ.elements[i].checked){
				conta_campi_N++;
			}
		}
	}
	if(numero_campi_tabella == conta_campi_N){
		pass = "false";
	}
	
	if(pass == "false"){
		alert(msg_alert);
		return false;
	}else{
		return true;
	}
}


function ControllaArchiveADVpermission() {
	var pass = "true";
	msg_alert = "Attenzione! Almeno un campo deve essere in lettura. E' necessario usare il modulo della sezione \"Gestione utenti\" per disabilitare l\'accesso all\'archivio per gli utenti:\n";

	FormOBJ = document.getElementById('formADVpermission');
	numero_campi_tabella = document.getElementById('numero_campi').value;
	STRIDUtenti = document.getElementById('ArrIDUtenti').value;
	var ArrIDUtenti = STRIDUtenti.split(";");
	
	for(i_utente=0;i_utente<ArrIDUtenti.length;i_utente++){
		username = document.getElementById('username_'+ArrIDUtenti[i_utente]).value;
		conta_campi_N = 0;
		
		for(i=0;i<numero_campi_tabella;i++){
			controllo = document.getElementById('ID_N_'+ArrIDUtenti[i_utente]+'_'+i);
			if(controllo.checked){
				conta_campi_N++;
			}
		}
		if(numero_campi_tabella == conta_campi_N){
			pass = "false";
			msg_alert += "- "+username+"\n";
			
		}
	}

	if(pass == "false"){
		alert(msg_alert);
		return false;
	}else{
		return true;
	}
}


function ControllaRicercaDoc() {
	var pass = "true";
	msg_alert = "Attenzione!\nAlmeno un campo deve essere selezionato per la visualizzazione.\n";

	FormOBJ = document.getElementById('ricercaDoc');
	numero_campi_tabella = document.getElementById('numero_campi').value;
	conta_vis = 0;
	
	for(i=0;i<FormOBJ.length;i++){
		if((FormOBJ.elements[i].id) && (FormOBJ.elements[i].id != "numero_campi")){
			if(!(FormOBJ.elements[i].checked)){
				conta_vis++;
			}
		}
	}	
	
	if(numero_campi_tabella == conta_vis){
		pass = "false";
	}
	
	if(pass == "false"){
		alert(msg_alert);
		return false;
	}else{
		return true;
	}
}

function conta_check() {
	var conta_checkbox = 0;
	FormFile = document.getElementById('form_index');
	FormComandi = document.getElementById('docToolbar');
	for (i=0; i < FormFile.length; i++) {
		var tempobj = FormFile.elements[i];
		if (tempobj.name.substring(0,11)=="ID_elemento" && tempobj.type=="checkbox" && tempobj.checked) {
			conta_checkbox ++;
		}
	}
	if (conta_checkbox == '1'){
		if (FormComandi['edit']){
			FormComandi['edit'].src = "../images/edit_on.gif"; 
			FormComandi['edit'].style['cursor'] = "hand";
		}
		if (FormComandi['delete']){ 
			FormComandi['delete'].src = "../images/delete_on.gif";
			FormComandi['delete'].style['cursor'] = "hand";
		}
	}
	else if (conta_checkbox > '1'){
		if (FormComandi['edit']){
			FormComandi['edit'].src = "../images/edit_off.gif";
			FormComandi['edit'].style['cursor'] = "";
		}
		if (FormComandi['delete']){ 
			FormComandi['delete'].src = "../images/delete_on.gif";
			FormComandi['delete'].style['cursor'] = "hand";
		}
	}else{
		if (FormComandi['edit']){
			FormComandi['edit'].src = "../images/edit_off.gif";
			FormComandi['edit'].style['cursor'] = "";
		}
		if (FormComandi['delete']){
			FormComandi['delete'].src = "../images/delete_off.gif";
			FormComandi['delete'].style['cursor'] = "";
		}
	}
}

function ModificaFile(form_name,img_name) {
	document[form_name]['stato_'+img_name].value = "NEW";
	document.getElementById('li_'+img_name).style.display = "none";
	document.getElementById('li_upload_'+img_name).style.display = "block";
}

function AnnullaModificaFile(form_name,img_name,value) {
	document[form_name]['stato_'+img_name].value = value;
	document.getElementById('li_'+img_name).style.display = "block";
	document.getElementById('li_upload_'+img_name).style.display = "none";
}

function ControlloFile(form_name,img_name){
	var tempobj = document[form_name][img_name];
	document[form_name][img_name].src = document[form_name][tempobj.name.substring(4,tempobj.name.length)].value;
}

function  ControllaInsertFile(form_name){
	var formControllo = document.getElementById(form_name);
	var file_da_importare = document.getElementById('file_importare');
	var estensione_non_consentita = "FALSE";

	if(file_da_importare.value != ""){
		ControlloEstensione = file_da_importare.value.split(".");
			estensione = ControlloEstensione[(ControlloEstensione.length)-1].toUpperCase();
			//Se l'estensione TXT
			if((estensione == "TXT")){
				return true;
			}else{
				alert("Attenzione!\nEstensione file non consentita.");
				return false;
			}
	}else{
		alert("Attenzione!\nNon è stato selezionato il file da importare.");
		return false;
	}
}

// LEFT PANE SHOW/HIDE
function toggle_visibility(id) {
var e = document.getElementById(id);
if(e.style.display == 'none') {
	
	//alert("visualizza");
	document.cookie="archiveToolbar=visibile";
	e.style.display = 'block';
	document.getElementById('rightPane').className = "half";
}else {
	//alert("nascondi");
	document.cookie="archiveToolbar=nascosta";
	e.style.display = 'none';
	document.getElementById('rightPane').className = "full";
	document.getElementById('rightPane').style.padding ="15px 20px 0 20px";
}}

function PagPrec(form_name,max_pagine) {
	
	sequenza = document.getElementById('seq').value;
	
	pag_prec = parseInt(sequenza);
	pag_succ = parseInt(sequenza);
	
	if((pag_prec - 1) >= 0){
		pag_prec--;
		document.getElementById('seq').value = pag_prec;
		document[form_name].submit();
		return true;
	}else{
		return false;
	}
}

function PagSucc(form_name,max_pagine) {
	sequenza = document.getElementById('seq').value;
	pag_succ = parseInt(sequenza);
	
	if((pag_succ + 1) < max_pagine){
		pag_succ++;
		document.getElementById('seq').value = pag_succ;
		document[form_name].submit();
		return true;
	}else{
		return false;
	}
}