/*
	*Sostituzione forzata dei caratteri speciali
	*/
function replaceChar($text) {
    replace('à', '&agrave;', $text);    // Replace à with &agrave;
    replace('è', '&egrave;', $text);    // Replace è with &egrave;
    replace('é', '&eacute;', $text);    // Replace é with &egrave;
    replace('ì', '&igrave;', $text);    // Replace ì with &igrave;
    replace('ò', '&ograve;', $text);    // Replace ò with &ograve;
    replace('ù', '&ugrave;', $text);    // Replace ù with &ugrave;
    return $text;
}

// Apre il calendario GreyBox
function mostra_calendario(caption, url, height, width, callback_fn)
{
  var win = new GB_showCenter(caption,url,height,width,callback_fn);
}

/*
 * Ordina e aggiorna l'elenco in funzione della
	* colonna selezionata
	* - parm 1 nome della form
	* - parm 2 nome della colonna (nomecolonna + '-asc' o '-desc' )
	* - parm 3 nome del campo naschosto HTML per gestire ordinamento SQL
 */
function SortAndSubmit(nameForm, nameField, nameFieldOrder){
		setFieldValue(nameFieldOrder, nameField);
		document.forms[nameForm].submit();
	}
	
	/*
 * Aggiorna l'elenco in funzione della
	* colonna selezionata
	* - parm 1 nome della form
	* - parm 2 nome del campo utilizzato come azione
	* - parm 3 valore dell'azione da eseguire
 */
function Submit(nameForm, nameFieldAction, valueAction){
		setFieldValue(nameFieldAction, valueAction);
		document.forms[nameForm].submit();
	}
	/*
 * Controlla la Form e aggiorna l'elenco
	* - parm 1 nome della form
	* - parm 2 nome del campo utilizzato come azione
	* - parm 3 valore dell'azione da eseguire
 */
function CheckAndSubmit(nameForm, nameFieldAction, valueAction){
		if(CheckForm(nameForm)){
		Submit(nameForm, nameFieldAction, valueAction);
	 }
	}

	/*
 * Ordina e aggiorna l'elenco in funzione della
	* colonna selezionata
	* - parm 1 nome della form
 */
function SubmitOnly(nameForm){
	if(CheckForm(nameForm)){
		document.forms[nameForm].submit();
	 }
	}

	/*
 * Sottomette il Form e chiude la greybox Aperta
	* - parm 1 nome della form
 */
function SubmitAndCloseGreyBox(nameForm){
	if(CheckForm(nameForm)){
		document.forms[nameForm].submit();
		closePopUp();
	 }
	}

/*
 * Stampa la pagina corrente
 */
function Print(){
		self.print();
	}

/*
 * Passa alla pagina successiva e aggiorna l'elenco
	* - parm 1 nome della form
	* - parm 2 nome campo html che gestisce la funzione
 */
function AddPage(nameForm, funct){
		setFieldValue(funct, '+');
		document.forms[nameForm].submit();
	}

	/*
 * Passa alla pagina precedente e aggiorna l'elenco
	* - parm 1 nome della form
	* - parm 2  html che gestisce la funzione
 */
function SubPage(nameForm, funct){
		setFieldValue(funct, '-');
		document.forms[nameForm].submit();
	}
//
//  Impedisce che la text-area con id "id" contenga più di "max" caratteri
//
function max_descrizione(id,max){
    if (document.getElementById(id).value.length > max){
        alert("Testo troppo esteso (puoi inserire al massimo "+max+" caratteri)!");
        document.getElementById(id).value=document.getElementById(id).value.substring(0,max);
        return false;
    }
    return true;
}
//
//  Controllo delle credenziali di accesso
//
function controllo_credenziali()
{
    document.getElementById("no_username").style.display='none';
    document.getElementById("no_password").style.display='none';
    document.getElementById("messaggio").innerHTML="";
    var l=pulisci(document.getElementById("username").value);
    var p=pulisci(document.getElementById("password").value);
    var segnala="Attenzione:\n";
    var err=0;
    if(l=='')
         {
             segnala+="\n- Inserire il nome utente!";
             document.getElementById("no_username").style.display='inline';
             err=1;
         }
    if(p=='')
         {
             segnala+="\n- Inserire la password!";
             document.getElementById("no_password").style.display='inline';
             err=1;
         }
    if(err>0)
         {
           document.getElementById("messaggio").innerHTML="* Inserire le credenziali di accesso!";
           alert(segnala);
         }
    else
           document.getElementById("login").submit();
}
//
//  Toglie tutto quanto non è alfabetico o numerico dalla stringa
//
function pulisci(cosa)
{
    var esito="";
    cosa=cosa.toUpperCase();
    for(k=0;k<cosa.length;k++)
        if(cosa.charAt(k)>='A'&&cosa.charAt(k)<='Z'||cosa.charAt(k)>='0'&&cosa.charAt(k)<='9')
                esito=esito+cosa.charAt(k);
    return esito;
}
//
//  Gestisce [invio] (solo per maschera di login)
//
function controllo_invio(e,dove_posizionarsi)
{
    if(e.keyCode==13)
        document.getElementById(dove_posizionarsi).focus();
}

function AddHtmlElmToPage(idElm, txt){
var obj = getElm(idElm);
obj.innerHTML=txt;
}

function AddTextToElm(idElm, txt){
var obj = getElm(idElm);
var br = document.createElement("BR");
var p = document.createElement("FONT");
var text = document.createTextNode(txt);

obj.appendChild(p);
p.appendChild(text);
p.appendChild(br);
}

function clearDiv(idobj){
  var obj=getElm(idobj);
  obj.innerHTML="";
}

 function removeText(idobj){
  var last=getElm(idobj);
  last.removeChild(last.lastChild);
   }

 function getElm(Campo){
		var el1;

		/*if($A(Campo)!=null){
			return (el1=$A(Campo));
		}*/

		if($(Campo)==null){

		if(parent.document.getElementById(Campo)!=null){
				el1=parent.document.getElementById(Campo);
			}else{

				if(parent.parent.document.getElementById(Campo)!=null){
					el1=parent.parent.document.getElementById(Campo);
				}else{

     if(window.opener!=null){
					 if(window.opener.document.getElementById(Campo)!=null){
						 el1=window.opener.document.getElementById(Campo);
					 }
				 }else{
						el1=document.getElementById(Campo);
					}

				}

			}
		}else{
			el1=$(Campo);
		}
		return el1;
	}

 function $Array(Campo){
		var el1;

	
		if(parent.document.getElementsByName(Campo)!=null){
				el1=parent.document.getElementsByName(Campo);
			}else{

				if(parent.parent.document.getElementsByName(Campo)!=null){
					el1=parent.parent.document.getElementsByName(Campo);
				}else{

     if(window.opener!=null){
					 if(window.opener.document.getElementsByName(Campo)!=null){
						 el1=window.opener.document.getElementsByName(Campo);
					 }
				 }else{
						el1=document.getElementsByName(Campo);
					}

				}

			}

		return el1;
	}

	function $FArray(Campo, index){
		var el1;
		var i=parseInt(index);

		el1=$Array(Campo);
		return el1[i].value;
	}
