//var serverAjax = new Server("http://84.221.6.78:18000/apache2-default/meduri/handler.php"); //L'handler di ajax

var serverAjax = new Server(getUrl());
//alert(getUrl());
var oneToOne = 0;
var resultServerRequest= undefined;
var nomeCookie= "PHPSESSID";
var listaLink= undefined;



/** 
	Funzione richiamata pigiando un bottone grafico 
*/
function SelectActivity(obiForm) 
{
	//alert("Lanciata la funzione SelectActivity con "+obiForm.id);
	if(document.getElementById && document.appendChild && document.createElement /*&& document.getAttribute*/)
	{
		var campo = document.getElementById("settore");
		var campoHidden = document.getElementById("settoreScelto");
		setStyle(document.getElementById("sezioniSetting"), cssClass.greenBackGround, undefined);
		setStyle(document.getElementById("attivitaSetting"), cssClass.redBackGround, undefined);
		refreshLink();
		campoHidden.value=obiForm.id;
		campo.value=obiForm.id;
		if(!campo.disabled) campo.disabled=true;	
		//else campo.disabled=true;
		//campo.disabled=false;
		var radio1 = document.getElementById("modalita_modify");
		var radio2 = document.getElementById("modalita_add");
		var inviaDati = document.getElementById("inviaDati");
		if(obiForm.id != "link")
		{
			if(radio1) 
				if(radio1.disabled) radio1.disabled=false;
				if(radio1.checked)  radio1.checked = false;
			if(radio2) 
				if(radio2.disabled) radio2.disabled=false;
				if(radio2.checked)  radio2.checked = false;
			//if(radio2 && radio2.disabled) radio2.disabled=false;
			if(inviaDati && inviaDati.disabled) inviaDati.disabled=false;
		}
		else
		{
			setStyle(document.getElementById("attivitaSetting"), cssClass.greenBackGround, undefined);
			if(inviaDati && !inviaDati.disabled)
				inviaDati.disabled=true;
			if(radio1 && radio1.disabled) 
				radio1.disabled=false;
			radio1.checked=true;
		}

		//Preparo la Form
		var figlioId = "_"+obiForm.id;
		if(obiForm.id == "accordi verbali" 
		|| obiForm.id == "convocazioni" 
		|| obiForm.id == "turni" 
		|| obiForm.id == "documenti di settore")
			figlioId = "_divisionDocs";
		

		var code = -1; //to identify the comunicati brother
		if(obiForm.id == "assemblee" 
		|| obiForm.id == "normative" 
		|| obiForm.id == "ccnl" 
		|| obiForm.id == "informativa"
		|| obiForm.id == "notiziario")
		{
			figlioId = "_comunicati";
			if(obiForm.id == "assemblee") code = 2;
			if(obiForm.id == "normative") code = 3;
			if(obiForm.id == "ccnl") code = 4;
			if(obiForm.id == "informativa") code = 5;
			if(obiForm.id == "notiziario") {code = 6; }
		}
		if(obiForm.id == "comunicato") code = 1;

		var figlio = document.getElementById(figlioId).cloneNode("true");		
		figlio.id= figlioId+"_modificabile";
		
		if(obiForm.id == "accordi verbali" 
		|| obiForm.id == "convocazioni" 
		|| obiForm.id == "turni" 
		|| obiForm.id == "documenti di settore")
		{
				for(var ind=0; ind < figlio.childNodes.length; ind++){
					if(figlio.childNodes[ind].nodeType==1  && figlio.childNodes[ind].getAttribute('lang')!= undefined){
						if(obiForm.id == "turni")	figlio.childNodes[ind].style.visibility="hidden";
						else				figlio.childNodes[ind].style.visibility="visible";
					}
				}
		}
		var padre  = document.getElementById("modificabile");
		var nuovoPadre = document.createElement("div");
		nuovoPadre.id="modificabileTemp";
		var nonno  = padre.parentNode;
		nonno.removeChild(padre);		
		nuovoPadre.appendChild(figlio);
		//nonno.replaceChild(nuovoPadre, padre);
		nuovoPadre.id="modificabile";
		nonno.appendChild(nuovoPadre);
		var iLink = document.getElementById("Associazioni Link");		
		if(obiForm.id!= "utenti" && obiForm.id!= "divisioni")	
			iLink.style.visibility="visible";
		else
			iLink.style.visibility="hidden";
		
		var legend = document.getElementById("legendFile");
		if(obiForm.id== "link")
		{
			for(var ind=0; ind < legend.childNodes.length; ind++)
			{
				if(legend.childNodes[ind].nodeType==3)
					legend.childNodes[ind].nodeValue= "File Presenti";	
			}
		}
		else
		{
			for(var ind=0; ind < legend.childNodes.length; ind++)
			{
				if(legend.childNodes[ind].nodeType==3)
					legend.childNodes[ind].nodeValue= "Associa File";	
			}	
			var valueCookie = getCookie(nomeCookie); 
			serverAjax.paramPost(nomeCookie, valueCookie);
			serverAjax.paramPost("tematica", obiForm.id);
			
			if(code >0)
				serverAjax.paramPost("code", code);
			var response = serverAjax.rpc("POST", "ajaxArea");
			var byServer = object_from_json(response);
			resultServerRequest = byServer.val;
			replaceSelect(resultServerRequest, obiForm.id);
			var codeDom = document.getElementById("code");
			switch(obiForm.id)
			{
				case "news": oneToOne = 0; 		break;
				case "comunicati": oneToOne = 1; 	codeDom.value="1";	break;
				case "turni": oneToOne = 1; 		break;
				case "convocazioni": oneToOne = 1; 	break;
				case "documenti di settore": oneToOne = 1; 	break;
				case "accordi verbali": oneToOne = 1; 	break;	
				case "documenti": oneToOne = 1;		break;
				case "divisioni": oneToOne = 0;		break;
				case "link": oneToOne = 0; 		break;
				case "aree":oneToOne = 0;		break;
				case "utenti": oneToOne = 0; 		break;
				case "assemblee": oneToOne = 1; 	codeDom.value="2";	break;
				case "normative": oneToOne = 1; 	codeDom.value="3";	break;
				case "ccnl": oneToOne = 1;		codeDom.value="4"; break;
				case "informativa": oneToOne = 1; 	codeDom.value="5"; break;
				case "notiziario": oneToOne = 1; 	codeDom.value="6"; break;
				case "iniziative": oneToOne = 0; 	break;
			}
		}
 		
	}
	else alert("<SelectActivity>Il Browser non supporta funzioni di elaborazione su DOM");
}

/** Questa funzione viene invocata quando l'utente decide di */
function modificabile(obiCheck)
{
	if(document.getElementById)
	{
		var ctrled = document.getElementById("voceSelect");
		var elimina = document.getElementById("submitElimina");
		
		setStyle(document.getElementById("attivitaSetting"), cssClass.greenBackGround, undefined);
		
		if(ctrled == null || ctrled == undefined)
			alert(ctrled);
		if(obiCheck.value=="add") 
		{
			ctrled.disabled=true;
			elimina.disabled=true;
		}
		else
		{ 
			ctrled.disabled=false;
			elimina.disabled=false; 	
		}
	}
	else alert("<modificabile>Il Browser non supporta funzioni di elaborazione su DOM");
}



function radioUploadFile(obiCheck)
{
	if(document.getElementById)
	{
		var loadLink = document.getElementById("loadLink");
		var loadFile = document.getElementById("loadFile");
		if(loadLink == null || loadLink == undefined)
			alert(ctrled);
		if(obiCheck.value=="esternal") 
		{
			loadLink.disabled=false;
			loadFile.disabled=true;
		}
		else if(obiCheck.value=="internal")
		{ 
			loadLink.disabled=true;
			loadFile.disabled=false;	
		}
		else
		{
			loadLink.disabled=true;
			loadFile.disabled=true;
		}
	}
	else alert("<modificabile>Il Browser non supporta funzioni di elaborazione su DOM");
}



// imposta il cookie sNome = sValore
// source: http://www.webmasterpoint.org/appunti/javascript/19.asp
// per la durata di iGiorni
function setCookie(sNome, sValore, iGiorni) 
{
  var dtOggi = new Date();
  var dtExpires = new Date();
  dtExpires.setTime(dtOggi.getTime() + 24 * iGiorni * 3600000);
  document.cookie = sNome + "=" + escape(sValore) + "; expires=" + dtExpires.toGMTString();
}

// restituisce il valore del cookie sNome
function getCookie(sNome) 
{
  // genera un array di coppie "Nome = Valore"
  // NOTA: i cookies sono separati da ';'
  var asCookies = document.cookie.split("; ");
  // ciclo su tutti i cookies
  for (var iCnt = 0; iCnt < asCookies.length; iCnt++)
  {
    // leggo singolo cookie "Nome = Valore"
	var asCookie = asCookies[iCnt].split("=");
    	if (sNome == asCookie[0]) 
		return (unescape(asCookie[1]));
  }

  // SE non esiste il cookie richiesto
  return("");
}

// rimuove un cookie
function delCookie(sNome)
{
	setCookie(sNome, "");
}


function replaceSelect(arrayItem, requestID)
{
	if(document.getElementById && document.appendChild && document.replaceChild)
	{
                  var figlioVecchio = document.getElementById("voceSelect");
                  var radice = figlioVecchio.parentNode;
                  var figlioVecchio2 = document.getElementById("voceSelectDopo");
                  var radice2 = figlioVecchio2.parentNode;
                  var oSel=document.createElement("select");
                  var oSel2=undefined;
                  if(requestID=="aree") oSel2= document.createElement("select");
                  oSel.setAttribute("onChange", "Menu(this);");
                  oSel.id= "voceSelect";
                  oSel.name=requestID;
                  var radio1 = document.getElementById("modalita_modify");
                  if(!radio1.checked)
                          oSel.disabled=true;
                  if(requestID=="aree")
                  {
                          oSel2.id= "voceSelectDopo";
                          oSel2.name=requestID+"Dopo";
                  }
                  var oOpt= null;
                  //<option value="pagina1.htm">Pagina 1</option>
                  oOpt = document.createElement("option");
                  oOpt.value="undefined";
                  var oText = document.createTextNode("---");
                  oOpt.appendChild(oText);
                  oSel.appendChild(oOpt);
                  if(requestID=="aree")
                  {
                          oSel2.appendChild(oOpt.cloneNode("true"));
                          oOpt = document.createElement("option");
                          oOpt.value= "0";	
                          oText = document.createTextNode("Nessuno");
                          oOpt.appendChild(oText);
                          oSel2.appendChild(oOpt);	
                  }
                  for (i=0; i< arrayItem.length; i++)
                  {
                          oOpt = document.createElement("option");
                          oOpt.value= arrayItem[i].id;
                          switch(requestID)
                          {
                                  case "aree":				
                                          oText = document.createTextNode(arrayItem[i].nome);
                                          break;
                                  case "assemblee":
                                  case "normative":
                                  case "ccnl":
                                  case "informativa":
                                  case "notiziario":
                                  case "comunicati":				
                                          oText = document.createTextNode("del "+arrayItem[i].data);
                                          break;
                                  case "iniziative":	
                                  case "news":
                                          oText = document.createTextNode(arrayItem[i].titolo+"["+arrayItem[i].data+"]");
                                          break;
                                  case "divisioni": 					
                                          oText = document.createTextNode(arrayItem[i].nome);
                                          break;
                                  case "convocazioni":
                                  case "accordi verbali":
                                  case "documenti di settore":
                                  case "turni":
                                          oText = document.createTextNode("["+arrayItem[i].titolo+"] del "+ arrayItem[i].data);
                                          break;
                                  case "documenti":
                                          oText = document.createTextNode(arrayItem[i].titolo+" ["+arrayItem[i].data+"]");
                                          break;
                                  case "utenti":
                                          var testo=arrayItem[i].nome+" "+arrayItem[i].cognome;
                                          if(arrayItem[i].isAdmin=="1")
                                                  testo+=" [A]";
                                          oText = document.createTextNode(testo);
                                          break;
                        }
                        oOpt.appendChild(oText);
                	oSel.appendChild(oOpt);	
                	if(requestID=="aree") oSel2.appendChild(oOpt.cloneNode("true"));
                }
                radice.replaceChild(oSel, figlioVecchio);	
                if(requestID=="aree") radice2.replaceChild(oSel2, figlioVecchio2);

		//Padre               Figlio vecchio, Figlio nuovo   //EditingMaker:210
		//radiceOpBase.replaceChild(radiceOp,_radiceOp);			
        }
	else alert("<replaceSelect>Il Browser non supporta funzioni di elaborazione su DOM");
}


function oneCheck(chiamante){

if(document.getElementById){
	if(oneToOne){
		
		if(!listaLink){
			var divJSon = document.getElementById("link_List");
			var byServer = object_from_json(divJSon.firstChild.nodeValue);
			listaLink = byServer.val;	
		}
		var temp;
		if(chiamante.checked){			
			chiamante.disabled=false;
			for(var i=0; i<listaLink.length; i++){
				if(listaLink[i]!=chiamante.id){
					temp=document.getElementById(listaLink[i]);
					temp.disabled=true;
				}
			}
		}
		else{			
			for(var i=0; i<listaLink.length; i++){
				if(listaLink[i]!=chiamante.id){
					temp=document.getElementById(listaLink[i]);
					temp.disabled=false;
				}
			}
		}
	}
	
}
else alert("<oneCheck>Il Browser non supporta funzioni di elaborazione su DOM");
}

function refreshLink(){
if(document.getElementById){
	if(!listaLink){
		var divJSon = document.getElementById("link_List");
		var byServer = object_from_json(divJSon.firstChild.nodeValue);
		listaLink = byServer.val;	
	}
	for(var i=0; i<listaLink.length; i++){		
			temp=document.getElementById(listaLink[i]);
			temp.checked=false;
			temp.disabled=false;
	}
}
	

else alert("<refreshLink>Il Browser non supporta funzioni di elaborazione su DOM");
}



function Menu(chiamante)
{
	if(document.getElementById)
	{
              //alert("Il chiamante è "+chiamante.name+"   "+chiamante.value);
              refreshLink();
              switch(chiamante.name)
              {	
                      case "news":
                              var textArea = document.getElementById("areaDescriptionNews");
                              var titolo = document.getElementById("titoloNews");
                              var cond = true;
                              for(var index=0; index <resultServerRequest.length && cond; index++)
                              {
                                      if(resultServerRequest[index].id == chiamante.value)
                                      {
                                              textArea.value=resultServerRequest[index].descrizione;
                                              titolo.value= resultServerRequest[index].titolo;
                                              cond=false;
                                      }
                              }
                              var valueCookie = getCookie(nomeCookie); 
                              serverAjax.paramPost(nomeCookie, valueCookie);
                              serverAjax.paramPost("id_News", chiamante.value);
                              serverAjax.paramPost("tematica", "news_Link");
                              var response = serverAjax.rpc("POST", "ajaxArea");
                              var byServer = object_from_json(response);
                              var result = byServer.val;
                              for(var index=0; index <result.length; index++)
                              {
                                      linkAssociato = document.getElementById("link_"+result[index]);
                                      linkAssociato.checked=true;
                              }		
                      break;
                      case "assemblee":
                      case "normative":
                      case "ccnl":
                      case "informativa":
                      case "notiziario":
                      case "comunicati":
                              var argomento = document.getElementById("argomento");
                              var dataArg = document.getElementById("dataArgomento");
                              var note = document.getElementById("note");
                              var cond = true;
                              for(var index=0; index <resultServerRequest.length && cond; index++)
                              {
                                      if(resultServerRequest[index].id == chiamante.value)
                                      {
                                              argomento.value=resultServerRequest[index].argomento;
                                              dataArg.value= resultServerRequest[index].data;
                                              if(resultServerRequest[index].note!= undefined)
                                                      note.value= resultServerRequest[index].note;	
                                              cond=false;		
                                      }
                              }
                              var valueCookie = getCookie(nomeCookie); 
                              serverAjax.paramPost(nomeCookie, valueCookie);
                              serverAjax.paramPost("id_Comunicato", chiamante.value);
                              serverAjax.paramPost("tematica", "comunicati_Link");
                              var response = serverAjax.rpc("POST", "ajaxArea");
                              var byServer = object_from_json(response);		
                              var result = byServer.val;
                              for(var index=0; index <result.length; index++)
                              {
                                      linkAssociato = document.getElementById("link_"+result[index]);
                                      linkAssociato.checked=true;
                                      oneCheck(linkAssociato);
                              }
                      break;
                      case "turni":
                              var dataTurno = document.getElementById("dataDivisionDocs");
                              var divisioneSelect= document.getElementById("divisioneSelect");
                              var cond = true, cond2=true;
                              for(var index=0; index <resultServerRequest.length && cond; index++)
                              {
                                      if(resultServerRequest[index].id == chiamante.value)
                                      {
                                              dataTurno.value=resultServerRequest[index].data;
                                              for(var ind=0; ind< divisioneSelect.length&& cond2; ind++)
                                              {
                                                      if(divisioneSelect.childNodes[ind].value == resultServerRequest[index].divisione)
                                                      {
                                                              cond2=false;
                                                              divisioneSelect.childNodes[ind].selected=true;
                                                      }
                                              }
                                              cond=false;
                                      }
                              }
                              var valueCookie = getCookie(nomeCookie); 
                              serverAjax.paramPost(nomeCookie, valueCookie);
                              serverAjax.paramPost("id_divisionDocs", chiamante.value);
                              serverAjax.paramPost("tematica", "divisionDocs_Link");
                              var response = serverAjax.rpc("POST", "ajaxArea");
                              var byServer = object_from_json(response);		
                              var result = byServer.val;
                              for(var index=0; index <result.length; index++)
                              {
                                      linkAssociato = document.getElementById("link_"+result[index]);
                                      linkAssociato.checked=true;
                                      oneCheck(linkAssociato);
                              }

                      break;
                      case "convocazioni":
                      case "accordi verbali":
                      case "documenti di settore":
                              var titolo = document.getElementById("titolodivisionDocs");
                              var dataTurno = document.getElementById("dataDivisionDocs");
                              var divisioneSelect= document.getElementById("divisioneSelect");
                              var cond = true, cond2=true;			
                              for(var index=0; index <resultServerRequest.length && cond; index++)
                              {
                                      if(resultServerRequest[index].id == chiamante.value)
                                      {
                                              dataTurno.value=resultServerRequest[index].data;
                                              titolo.value= resultServerRequest[index].titolo;
                                              for(var ind=0; ind< divisioneSelect.length&& cond2; ind++)
                                              {
                                                      if(divisioneSelect.childNodes[ind].value == resultServerRequest[index].divisione)
                                                      {
                                                              cond2=false;
                                                              divisioneSelect.childNodes[ind].selected=true;
                                                      }
                                              }
                                              cond=false;
                                      }
                              }
                              var valueCookie = getCookie(nomeCookie); 
                              serverAjax.paramPost(nomeCookie, valueCookie);
                              serverAjax.paramPost("id_divisionDocs", chiamante.value);
                              serverAjax.paramPost("tematica", "divisionDocs_Link");
                              var response = serverAjax.rpc("POST", "ajaxArea");
                              var byServer = object_from_json(response);		
                              var result = byServer.val;
                              if(result.length>0)
                              {
                                      for(var index=0; index <result.length; index++)
                                      {
                                              linkAssociato = document.getElementById("link_"+result[index]);
                                              linkAssociato.checked=true;
                                              oneCheck(linkAssociato);
                                      }
                              }	
                              else	refreshLink();

                      break;
                      case "documenti":
                              var titolo = document.getElementById("titoloDocumento");
                              var dataDocumento = document.getElementById("dataDocumento");
                              var documentiDescription= document.getElementById("documentiDescription");
                              var cond = true;
                              for(var index=0; index <resultServerRequest.length && cond; index++)
                              {
                                      if(resultServerRequest[index].id == chiamante.value)
                                      {
                                              titolo.value=resultServerRequest[index].titolo;
                                              dataDocumento.value=resultServerRequest[index].data;
                                              documentiDescription.value=resultServerRequest[index].descrizione;
                                              var linkAssociato = document.getElementById("link_"+resultServerRequest[index].id_Link);
                                              linkAssociato.checked=true;
                                              oneCheck(linkAssociato);
                                              cond=false;
                                      }
                              }
                      break;
                      case "divisioni":
                              var sede = document.getElementById("nomeDivisione");
                              var cond = true;
                              for(var index=0; index <resultServerRequest.length && cond; index++)
                              {
                                      if(resultServerRequest[index].id == chiamante.value)
                                      {
                                              sede.value=resultServerRequest[index].nome;
                                              cond=false;
                                      }
                              }
                      break;
                      case "aree":
                              var textArea = document.getElementById("areaDescription");
                              var nomeArea = document.getElementById("nomeArea");
                              var actualPosition = document.getElementById("actualPosition");
                              var cond = true;
                              for(var index=0; index <resultServerRequest.length && cond; index++)
                              {
                                      if(resultServerRequest[index].id == chiamante.value)
                                      {
                                              textArea.value=resultServerRequest[index].descrizione;
                                              nomeArea.value=resultServerRequest[index].nome;
                                              actualPosition.value = resultServerRequest[index].posizione;
                                              cond=false;		
                                      }
                              }
                              var valueCookie = getCookie(nomeCookie); 
                              serverAjax.paramPost(nomeCookie, valueCookie);
                              serverAjax.paramPost("id_Area", chiamante.value);
                              serverAjax.paramPost("tematica", "aree_Link");
                              var response = serverAjax.rpc("POST", "ajaxArea");
                              var byServer = object_from_json(response);
                              var result = byServer.val;
                              for(var index=0; index <result.length; index++)
                              {
                                      linkAssociato = document.getElementById("link_"+result[index]);
                                      linkAssociato.checked=true;
                              }
                      break;
                      case "iniziative":
                              var textArea = document.getElementById("areaDescriptionIniziativa");
                              var titolo = document.getElementById("titoloIniziativa");
                              var cond = true;
                              for(var index=0; index <resultServerRequest.length && cond; index++)
                              {
                                      if(resultServerRequest[index].id == chiamante.value)
                                      {
                                              textArea.value=resultServerRequest[index].descrizione;
                                              titolo.value= resultServerRequest[index].titolo;
                                              cond=false;
                                      }
                              }
                              var valueCookie = getCookie(nomeCookie); 
                              serverAjax.paramPost(nomeCookie, valueCookie);
                              serverAjax.paramPost("id_Iniziativa", chiamante.value);
                              serverAjax.paramPost("tematica", "iniziative_Link");
                              var response = serverAjax.rpc("POST", "ajaxArea");
                              var byServer = object_from_json(response);
                              var result = byServer.val;
                              for(var index=0; index <result.length; index++)
                              {
                                      linkAssociato = document.getElementById("link_"+result[index]);
                                      linkAssociato.checked=true;
                              }		
			break;

			case "utenti":
                              var usernameUtente = document.getElementById("usernameUtente");
                              var nomeUtente = document.getElementById("nomeUtente");
                              var cognomeUtente = document.getElementById("cognomeUtente");			
                              var cond = true;
                              for(var index=0; index <resultServerRequest.length && cond; index++)
                              {
                                      if(resultServerRequest[index].id == chiamante.value)
                                      {	
                                              usernameUtente.value=resultServerRequest[index].username;
                                              nomeUtente.value= resultServerRequest[index].nome;
                                              cognomeUtente.value= resultServerRequest[index].cognome;
                                              if(resultServerRequest[index].isAdmin == 1)
                                              document.getElementById("isAdmin_1").checked= true;
                                              else
                                                document.getElementById("isAdmin_0").checked= true;
                                              cond=false;		
                                      }
                              }
                      break;	
              }
      }
else alert("<Menu>Il Browser non supporta funzioni di elaborazione su DOM");
}


function uscita(obi)
{
//alert("richiamata in uscita");
	setStyle(obi, cssClass.OutAttivitas, undefined);
}

function validaForm()
{
	if(document.getElementById)
	{
		var val = document.getElementById("settoreScelto").value;
		var results = new Array();
		switch(val)
		{
			case "news": 
				results[0] = validaRadio();	
				results[1] = validaCasellaTesto("titoloNews", "Titolo" ,"default");
				results[2] = validaCasellaTesto("areaDescriptionNews", "Descrizione" ,"default");
				break;
			case "comunicati": 
			case "assemblee":
			case "normative":
			case "ccnl":
			case "informativa":
			case "notiziario":
				results[0] = validaRadio();	
				results[1] = validaCasellaTesto("dataArgomento", "Data" ,"default");
				results[2] = validaCasellaTesto("argomento", "Argomento" ,"default");
				break;
			case "turni":	
			case "convocazioni":
			case "accordi verbali":
			case "documenti di settore":	
				results[0] = validaRadio();
				results[1] = validaCasellaTesto("dataDivisionDocs", "Data" ,"default");	
				results[2] = validaSelect("divisioneSelect", "Divisione", "undefined");
				if(val != "turni")
					results[3] = validaCasellaTesto("titolodivisionDocs", "Titolo" ,"default");
				break;
			case "documenti":
				results[0] = validaRadio();	
				results[1] = validaCasellaTesto("titoloDocumento", "Titolo" ,"default");
				results[2] = validaCasellaTesto("dataDocumento", "Data" ,"default");
				results[3] = validaCasellaTesto("documentiDescription", "Descrizione" ,"default");
				break;
			case "divisioni": 
				results[0] = validaRadio();	
				results[1] = validaCasellaTesto("nomeDivisione", "Nome" ,"default");
				break;
			case "aree":
				results[0] = validaRadio();	
				results[1] = validaCasellaTesto("nomeArea", "Nome" ,"default");
				results[2] = validaSelect("voceSelectDopo", "Inserisci dopo", "undefined");
				results[3] = validaCasellaTesto("areaDescription", "Descrizione" ,"default");
				break;
			case "iniziative": 
				results[0] = validaRadio();	
				results[1] = validaCasellaTesto("titoloIniziativa", "Titolo" ,"default");
				results[2] = validaCasellaTesto("areaDescriptionIniziativa", "Descrizione" ,"default");
				break;
			case "utenti":
				results[0] = validaRadio();	
				results[1] = validaCasellaTesto("usernameUtente", "Username" ,"default");
				results[2] = validaCasellaTesto("oldpasswd", "Old Passwd" ,"default");
				results[3] = validaCasellaTesto("newpasswd", "New Passwd" ,"default");
				results[4] = validaCasellaTesto("nomeUtente", "Nome" ,"default");
				results[5] = validaCasellaTesto("cognomeUtente", "Cognome" ,"default");
				var ids_elementi = new Array("isAdmin_1", "isAdmin_0");
				results[6] = validaRadioGen(ids_elementi, "Amministratore");				
				break;
		}
		return validaCampi(val, results);
	}
	else
	{
		alert("<Menu>Il Browser non supporta funzioni di elaborazione su DOM");
		return false;
	}
	return false;
}


function validaCampi(val, results)
{
	var res = esaminaResults(results);
	if(val != "divisioni" && val != "utenti")
	{	
		if(res[0])
			return validaListaFile();
		else
		{
			alert(res[1]);
			return res[0];
		}
	}
	else
	{
		if(!res[0])
			alert(res[1]);
		return res[0];
	}
}


/** Funzione di utilità per controllare tutti i risultati*/
function esaminaResults(results)
{
    var result = new Array();
    result[0] = true;
    result[1] = "";
    for(var sc =0 ; sc < results.length; sc++)
    {
            if(!results[sc][0])
                    result[1] += results[sc][1];
    }		
    if(result[1].length >0)
            result[0] = false;
    return result;
}

/**  Questa funzione serve per validare le caselle di testo
*/
function validaCasellaTesto(id_elemento, nomeDaUsare, pattern)
{
	var result = new Array();
	if(document.getElementById)
	{
		var elem = document.getElementById(id_elemento);
		if(elem.value.length == 0)
		{
			result[1] = "<validaCasellaTesto>Il campo "+nomeDaUsare+" è vuoto. Va riempito per poter inviare i dati al server!\n";
			result[0] = false;
			return result;
		}
		if(pattern != "default" && !pattern.test(elem.value))
		{
			result[1] = "<validaCasellaTesto>Il campo "+nomeDaUsare+" contiene dei caratteri regolari. Va corretto per poter inviare i dati al server!\n";
			result[0] = false;
			return result;
		}
		result[0] = true;
		return result;
	}
	else
	{
		result[1] = "<validaCasellaTesto>Il Browser non supporta funzioni di elaborazione su DOM\n";
		result[0] = false;
		return result;
	}
	
}


function validaSelect(id_elemento, nomeDaUsare, valErrore)
{
	var result = new Array();
	result[0] = true;
	if(document.getElementById)
	{
		var elem = document.getElementById(id_elemento);
		if(elem.options[elem.selectedIndex].value == valErrore)
		{
			result[1] = "<validaSelect>Devi selezionare una voce del campo "+nomeDaUsare+"!\n";
			result[0] = false;
		}
	}
	else
	{
		result[1] = "<validaSelect>Il Browser non supporta funzioni di elaborazione su DOM\n";
		result[0] = false;
	}
	return result;
}

function validaListaFile()
{
	if(!listaLink)
	{
		var divJSon = document.getElementById("link_List");
		var byServer = object_from_json(divJSon.firstChild.nodeValue);
		listaLink = byServer.val;	
	}
	for(var i=0; i<listaLink.length; i++)
	{
		if(document.getElementById(listaLink[i]).checked)
			return true;
	}
	return confirm("Non hai associato alcun file della lista, va bene lo stesso?");
}

function validaRadio()
{
	var result = new Array();
	result[0] = true;
	var radio1 = document.getElementById("modalita_modify");
	var radio2 = document.getElementById("modalita_add");
	if(radio1 && radio1.checked)
	{
		//voceSelect
		var voceSelect = document.getElementById("voceSelect");
		if(voceSelect.options[voceSelect.selectedIndex].value == "undefined")
		{
			result[1] = "<validaRadio>Devi selezionare una voce del campo Voci già presenti!\n";
			result[0] = false;
		}
	}	
	else
	{
		if(!(radio2 && radio2.checked))
		{
			result[1] = "<validaRadio>Devi selezionare un'attività ( Modifica dato esistente | Aggiungi nuovo dato)\n";
			result[0] = false;
		}
	}
	return result;
}

function validaRadioGen(ids_elementi, nomeDaUsare)
{	
	var result = new Array();
	result[0] = true;	
	if(document.getElementById)
	{
		var radios = new Array();
		var problema = true;
		for(var i =0; i < ids_elementi.length; i++)
		{
			radios[i] = document.getElementById(ids_elementi[i]);
			if(radios[i] != null  && radios[i].checked)
				problema = false;
		}
		if(problema)
		{
			result[1] = "<validaRadioGen>Devi selezionare almeno un'opzione di "+nomeDaUsare+"\n";
			result[0] = false;
		}
		
	}
	else
	{
		result[1] = "<validaRadioGen>Il Browser non supporta funzioni di elaborazione su DOM\n";
		result[0] = false;		
	}
	return result;
}

/*
case "news":
case "comunicati":
case "turni":
case "convocazioni":
case "accordi verbali":
case "documenti di settore":
case "documenti":
case "divisioni":
case "aree":
case "utenti":
*/
