// --------------------------------------------------------------------------
//  ONCLICK SUI LINK di NAVIGAZIONE
// --------------------------------------------------------------------------
function goPage(newAction, navigazioneValue)
{			
	doSubmit('formNavigazione', newAction, navigazioneValue);		
}

// --------------------------------------------------------------------------
//  ONCLICK SU HELP
// --------------------------------------------------------------------------
function openHelp(contextPath)
{
   url=contextPath +'/help/help.jsp';
   w="600";
   h="450";
   LeftPosition=(screen.width)?(screen.width-w)/2:100;
   TopPosition=(screen.height)?(screen.height-h)/2:100;
   settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars=yes,status=yes,menubar=no,toolbar=no,resizable=no';
   openHelpWin = window.open(url, 'windOpenHelp', settings);
}

// --------------------------------------------------------------------------
//  ESEGUE IL SUBMIT PER TUTTE LE FORM
// --------------------------------------------------------------------------
function doSubmit(formName, newAction, navigazioneValue)
{			
	formObj = document.getElementById(formName);	
	if(newAction != "")  formObj.action = newAction;
	formObj.navigazione.value = navigazioneValue;
	formObj.submit();			
}

// --------------------------------------------------------------------------
//  RESTITUISCE L'ELEMENT-FORM in modo dinamico
// --------------------------------------------------------------------------
function getFormElement(formObj, paramName) {
	return eval('document.' + formObj.name + '.' + paramName);
}
function getFormElementByFormId(formId, paramName) {
	return eval("document.getElementById('" +formId+ "')." +paramName+ ';');
}


// --------------------------------------------------------------------------
//  RIPULISCE IL CAMPO DI UNA FORM
// --------------------------------------------------------------------------
function doClear(elementId)
{	
	var dataElement = document.getElementById(elementId);
	if(dataElement)  dataElement.value = "";
}

// --------------------------------------------------------------------------
//  CONTROLLA SE UN CAMPO E' NUMERICO
// --------------------------------------------------------------------------
function isNumerico(campo)
{	
	if(campo.value!="")
	{
		if(isNaN(campo.value) || parseInt(campo.value) < 0 )
		{
			alert("Il campo deve avere un formato numerico!");	   		
	   		return false;
		}		
	}
	return true;
}

// ----------------------------------------------------------------------------
//  CONTROLLA SE L'IMPORTO IN EURO E' NUMERICO
// ----------------------------------------------------------------------------
function isEuroImportNumeric(importo){
	
	if(importo.value!=""){
		var nEuro = importo.value;
		nEuro = nEuro.replace(/\./g,"");
		nEuro = nEuro.replace(/,/g,".");	
		if(isNaN(nEuro)){
			alert("Il campo deve avere un formato numerico!");		
	   		return false;
		}
	}
	return true;
}

// ----------------------------------------------------------------------------
//  CONTROLLA SE L'IMPORTO IN EURO E' DIVERSO DA 0
// ----------------------------------------------------------------------------
function isEuroImportNotZero(importo){
	
	if(importo.value!=""){
		var nEuro = importo.value;
		nEuro = nEuro.replace(/\./g,"");
		nEuro = nEuro.replace(/,/g,".");	
		if(!isNaN(nEuro)){
			
			var numparts = nEuro.split(".");
	      	if (numparts.length < 2){      		
	      		if(numparts[0]=="0"){
	        		alert("L'Importo non pu\u00F2 essere uguale a 0");
		   			return false;
		   		}
	        }else{
	        	if(numparts[0]=="0" && numparts[1].length==1 && numparts[1]=="0"){        		
	        		alert("L'Importo non pu\u00F2 essere uguale a 0");
		   			return false;
		   		}
		   		if(numparts[0]=="0" && numparts[1].length==2 && numparts[1]=="00"){        		
	        		alert("L'Importo non pu\u00F2 essere uguale a 0");
		   			return false;
		   		}
	        }			
		}//fine if	
	}
	return true;
}

// ----------------------------------------------------------------------------
//  CONTROLLA LA CORRETTEZZA DI UNA DATA (effettuandone il parsing)
// ----------------------------------------------------------------------------
function isValidDateFormat (str_date) {
	
	var RE_NUM = /^\-?\d+$/;
	var NUM_CENTYEAR = 30;
	var arr_date = str_date.split('/');
	
	
	//CONTROLLI PRELIMINARI
	if (str_date.length != 10) {
		alert ("Formato di data invalido: '" + str_date + "'.\nIl formato consentito e' gg/mm/yyyy.");
		return false;
	}
	if (arr_date.length != 3) {
		alert ("Formato di data invalido: '" + str_date + "'.\nIl formato consentito e' gg/mm/yyyy.");
		return false;
	}
	
	//CONTROLLI SUL GIORNO
	if (!arr_date[0]) {
		alert ("Formato di data invalido: '" + str_date + "'.\nInserire il valore del giorno del mese.");
		return false;
	}
	if (arr_date[0].length != 2) {
		alert ("Formato di data invalido: '" + str_date + "'.\nIl valore del giorno deve essere di 2 cifre.");
		return false;
	}
	if (!RE_NUM.exec(arr_date[0])) {
		alert ("Valore del giorno del mese invalido: '" + arr_date[0] + "'.\nI valori consentiti sono interi senza segno.");
		return false;
	}
	
	//CONTROLLI SUL MESE
	if (!arr_date[1]){ 
		alert ("Formato di data invalido: '" + str_date + "'.\nInserire il valore del mese.");
		return false;
	}
	if (arr_date[1].length != 2){ 
		alert ("Formato di data invalido: '" + str_date + "'.\nIl valore del mese deve essere di 2 cifre.");
		return false;
	}
	if (!RE_NUM.exec(arr_date[1])) {
		alert ("Valore del mese invalido: '" + arr_date[1] + "'.\nI valori consentiti sono interi senza segno.");
		return false;
	}
	if (arr_date[1] < 1 || arr_date[1] > 12) {
		alert ("Valore del mese invalido: '" + arr_date[1] + "'.\nL'intervallo consentito e' 01-12.");
		return false;
	}
	
	//CONTROLLI SULL'ANNO
	if (!arr_date[2]) {
		alert ("Formato di data invalido: '" + str_date + "'.\nInserire il valore dell'anno.");
		return false;
	}
	if (arr_date[2].length != 4){ 
		alert ("Formato di data invalido: '" + str_date + "'.\nIl valore dell'anno deve essere di 4 cifre.");
		return false;
	}
	if (!RE_NUM.exec(arr_date[2])) {
		alert ("Valore dell'anno invalido:  '" + arr_date[2] + "'.\nI valori consentiti sono interi senza segno.");
		return false;
	}
	if (arr_date[2] < 1900) {
		alert ("Valore dell'anno invalido:  '" + arr_date[2] + "'.\nI valori consentiti sono gli anni successivi al 1900.");
		return false;
	}
	
	//CONTROLLO VALIDITA DEL GIORNO DEL MESE
	if (arr_date[2] < 100) {
		arr_date[2] = Number(arr_date[2]) + (arr_date[2] < NUM_CENTYEAR ? 2000 : 1900);
	}
	var dt_date = new Date();
	dt_date.setDate(1);
	dt_date.setMonth(arr_date[1]-1);	
	dt_date.setFullYear(arr_date[2]);
	var dt_numdays = new Date(arr_date[2], arr_date[1], 0);
	dt_date.setDate(arr_date[0]);
	
	if (dt_date.getMonth() != (arr_date[1]-1)){ 
		alert ("Valore del giorno del mese invalido: '" + arr_date[1] + "'.\nL'intervallo consentito e' 01-"+dt_numdays.getDate()+".");
		return false;
	}
	return true;
}

// --------------------------------------------------------------------------
//  ESEGUE LA TRIM DI UNA STRINGA
// --------------------------------------------------------------------------
function trimString(input){
	
	for(i=0; i<input.length; ){
		if(input.charAt(i)==" ")
			input=input.substring(i+1, input.length);
		else
			break;
	}	
	for(i=input.length-1; i>=0; i=input.length-1){
		if(input.charAt(i)==" ")
			input=input.substring(0,i);
		else
			break;
	}	
 	return input;
}

// --------------------------------------------------------------------------
//  ESEGUONO IL CONTROLLO DELLA FINE e L'INIZIO della STRINGA
// --------------------------------------------------------------------------
String.prototype.endsWith=endsWithFunc;
String.prototype.startsWith=startsWithFunc;
function endsWithFunc(match){
  	return ((this.substring(this.length - match.length).toLowerCase() == match.toLowerCase()) ? true : false);
}
function startsWithFunc(match){
  	return ((this.substring(0, match.length).toLowerCase() == match.toLowerCase()) ? true : false);
}

// --------------------------------------------------------------------------
//  ESEGUE IL CHECK DELLA PASSWORD
// --------------------------------------------------------------------------
function ControllaPassword(password)
{
    var notValido=0;
    var maxLen=20;
    var minLen=8;
    
   /* verifico la lunghezza della stringa inserita */
	if (password.value.length>0 && (password.value.length<minLen || password.value.length>maxLen))
	{ /* visualizzo un messaggio di errore */
    	alert("La password deve contenere un minimo di 8 caratteri e un massimo di 20!!");
     	password.focus();
     	return false;
	} 
    else 
    {   
       /* ciclo su tutti i caratteri e verifico siano ammessi per la tipologia di campo */
    	for( i=0; i <password.value.length; i++ )
        {
          	/* leggo il carattere */
          	if ( !(password.value.charCodeAt(i) >=48 	&& 
          		 password.value.charCodeAt(i) <=57 		||
          		 password.value.charCodeAt(i) >=65 		&& 
          		 password.value.charCodeAt(i) <=90 		||
          		 password.value.charCodeAt(i) >=97 		&& 
          		 password.value.charCodeAt(i) <=122 ))
            { 
             	/* non e' un carattere consentito */
             	/* gli unici caratteri consentiti sono lettere (maiuscole e minuscole) e numeri */
              	notValido=1;
              	break;
            }
        }
	}
	
   	/* testo il risultato del controllo */
    if( notValido == 1 )
    {
      	/* visualizzo un messaggio di errore */
      	alert("La password contiene uno o più caratteri non validi!!");
      	password.focus();
      	return false;
    }
    else
    {
      	/* tutto bene, password corretta */
      	return true;
    }
}

// --------------------------------------------------------------------------
//  VERIFICA SE UNA COMBO E' STATA SELEZIONATA
// --------------------------------------------------------------------------
function isSelectedCombo(comboName)
{
	if(comboName.selectedIndex ==0 || comboName.selectedIndex ==-1)
	{
		comboName.focus();
		return false;
	}

	return true;
}

// --------------------------------------------------------------------------
//  VERIFICA SE UNA STRINGA HA UN CORRETTO FORMATO TELEFONICO
// --------------------------------------------------------------------------
function TelFax(objTelefono) 
{
  	for(var i = 0; i < objTelefono.value.length;i++) 
  	{
     	if(!((objTelefono.value.charAt(i)>=0)    && 
     	     (objTelefono.value.charAt(i)<=9)    || 
     	     (objTelefono.value.charAt(i)=='/')  ||
     	     (objTelefono.value.charAt(i)=='-'))) 
		{
			alert("Numero di telefono non corretto!");
	    	objTelefono.focus();
			return false;
     	}
   } 
   return true;
}

// --------------------------------------------------------------------------
//  VERIFICA SE UNA STRINGA HA UN CORRETTO FORMATO PER UN INDIRIZZO E-MAIL
// --------------------------------------------------------------------------
function ControllaMail(EmailAddr)
{
	Filtro = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
   	if (Filtro.test(EmailAddr.value))
   	{
      	return true
   	}  
   	else
    {
		alert("Controllare l'indirizzo e-mail inserito!");
		EmailAddr.focus();
		return false;
    }
}

// --------------------------------------------------------------------------
//  ESEGUE UN CONTROLLO FORMALE DI UN CODICE FISCALE
// --------------------------------------------------------------------------
function ControllaCodFisc(campoCodFiscale)
{
    var validi, i, s, set1, set2, setpari, setdisp;
    cf = campoCodFiscale.value;
    
    if( cf == "" ) {return true;}
    
    cf = cf.toUpperCase();
    if( cf.length != 16 )
    {
        alert("La lunghezza del codice fiscale non è\n"
        	 +"corretta: il codice fiscale dovrebbe essere lungo\n"
        	 +"esattamente 16 caratteri.\n");
        return false;
    }
    
    validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    for( i = 0; i < 16; i++)
    {
        if( validi.indexOf( cf.charAt(i) ) == -1)
        {
            alert("Il codice fiscale contiene un carattere non valido `" +
                  cf.charAt(i) +
                  "'.\nI caratteri validi sono le lettere e le cifre.\n");
            return false;
        }
    }
    
    set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
    setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
    s = 0;
    for( i = 1; i <= 13; i += 2 )
    {
    	s += setpari.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
    }
    
    for( i = 0; i <= 14; i += 2 )
    {
    	s += setdisp.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
    }
    
    if( s%26 != cf.charCodeAt(15)-'A'.charCodeAt(0) )
    {
        alert("Il codice fiscale non è corretto:\n"+
              "il codice di controllo non corrisponde.\n");
        return false;
    }
    
    return true;
}

function servizioDisa(id, form, action, navig)
{
	formObj = document.getElementById(form);
	formObj.idFunction.value = id;
	doSubmit(form, action, navig);
}

 
 