var iMinLenUsername = 6;
var iMaxLenUsername = 20;
var iMinLenPassword = 8;
var iMaxLenPassword = 12;
var iPasswordMaxCharsUguali= 3;
var iPasswordNumericChar = 2;
var iPasswordSpecChars= 1;
var sUsernamePattern = new RegExp("^[a-zA-Z0-9]+$");	
var sPasswordSpecChars = "!?#@$%&*+-/.,:;=_()[]{}";	
var sStrPasswordPattern= "abcdefghijklmnopqrstuvwxyz0123456789!?#@$%&*+-/.,:;=_()[]{}" 
var sMobilePattern = new RegExp("^[\+](39){1}[0-9]+$");	
var SMS_maxchar = 130;

function bCheckValidPassword(sPassword, sErrors) {
	var sTemp= "";
	var iNumInteger= 0;
	var iNumSpecChars= 0;
	var iCt;
	var vChars= new MakeArray(sStrPasswordPattern.length+1);
		
	sErrors= "";	

	// Controllo della linghezza minima
	if (sPassword.length < iMinLenPassword) {
		sErrors= sErrors + "La password ha una lunghezza troppo piccola.\nLunghezza minima richiesta " + iMinLenPassword + " caratteri.\n";		
	}

	// Controllo della linghezza massima
	if (sPassword.length > iMaxLenPassword) {
		sErrors= sErrors + "La password ha una lunghezza troppo grande.\nLunghezza massima consentita " + iMaxLenPassword + " caratteri.\n";		
	}

	// Controllare che non ci sia lo stesso carattere per più di tre volte
	// Controllare che ci siano almeno 2 numeri
	// Controllare che ci sia almeno 1 carattere speciale
	iNumInteger= 0;
	iNumSpecChars= 0;
	iInvalidChars= 0;
	// alert(vChars.length);	
	for(iCt= 0; iCt <= sPassword.length -1; iCt++) {		
		sTemp= sPassword.charAt(iCt);			
		iPos = sStrPasswordPattern.indexOf(sTemp);
		
		if (iPos>=0) {						
			vChars[iPos]= vChars[iPos] +1;		
						
			if(isNaN(sTemp)==false) {
				iNumInteger= iNumInteger +1;
			}
			/*
			iPos = sPasswordSpecChars.indexOf(sTemp);			
			alert ("SC iPos =" + iPos);
			if (iPos>=0)  {
			*/
			if(sPasswordSpecChars.indexOf(sTemp)>=0) {
				
				iNumSpecChars= iNumSpecChars +1;
			}
		}
		else {	
			iInvalidChars= iInvalidChars +1;
		}				 
	}
	
	if(iInvalidChars>0) {
		sErrors = sErrors + "La password contiene " + iInvalidChars + " caratteri non ammessi!\n"		
	}
	else {
		for(iCt=1; iCt <= vChars.length; iCt++) {			
			if(vChars[iCt]>iPasswordMaxCharsUguali) {
				sErrors = sErrors + "La password contiene caratteri uguali ripertuti per più di " + iPasswordMaxCharsUguali + " volte!\n"		
			}
		}
	}

	if (iNumInteger < iPasswordNumericChar) {
		sErrors = sErrors + "La password deve contenere almeno " + iPasswordNumericChar + " caratteri numerici.\n";
	}
	 
	if (iNumSpecChars < iPasswordSpecChars) {
		sErrors = sErrors + "La password deve contenere almeno " + iPasswordSpecChars + " carattere speciale.\n";
	}

	if (sErrors=="") {		
		return true;
	}
	else { 
		alert(sErrors);	
		return false;
	}
}

function CheckChangePassword(theForm) {
	var OldPwd;
	var NewPwd;
	var ConPwd;
	var sErrors="";
		
	OldPwd= CutSpace(theForm.OldPassword.value);
	if (OldPwd.length==0) {
		alert("inserire la password attuale!");
		theForm.OldPassword.focus();
		return false;
	}
				
	NewPwd= CutSpace(theForm.NewPassword.value);
	if (NewPwd.length==0) {
		alert("Inserire la nuova password!");
		theForm.NewPassword.focus();
		return false;
	}
	
	ConPwd= CutSpace(theForm.ConPassword.value);
	if (ConPwd.length==0) {
		alert("Inserire la conferma della nuova password!");
		theForm.ConPassword.focus();
		return false;
	}
	
	if (NewPwd != ConPwd) {
		alert("La nuova password e quella di conferma non sono uguali!");
		theForm.NewPassword.focus();
		return false;				
	}

	if (NewPwd == OldPwd) {
		alert("La nuova password nopn può essere uguale a quella vecchia!");
		theForm.NewPassword.focus();
		return false;				
	}

	if (NewPwd.length < iMinLenPassword) {
		alert("La nuova password ha una lunghezza troppo piccola.\nLunghezza minima richiesta " + iMinLenPassword +" caratteri");
		theForm.NewPassword.focus();
		return false;
	}	

	if (NewPwd.length > iMaxLenPassword) {
		alert("La nuova password ha una lunghezza troppo grande.\nLunghezza massima consentita " + iMaxLenPassword + " caratteri");
		theForm.NewPassword.focus();
		return false;
	}

	if (bCheckValidPassword(theForm.NewPassword.value, sErrors)==false) {
		// alert(sErrors);
		theForm.NewPassword.focus();
		return false;
	}
	
	theForm.ACT.value= "SAVEPWD";
	
	return true;
}

function CheckFormUtente(theForm) {
	var ris;	
	var sTemp;	
	var sErrors="";

	ris= CutSpace(theForm.Cognome.value);
	if (ris.length==0) {
		alert("Inserire il Cognome");
		theForm.Cognome.focus();
		return false;	    
	}

	ris= CutSpace(theForm.Nome.value);
	if (ris.length==0) {
	    alert("Inserire il Nome");
	    theForm.Nome.focus();
	    return false;	    
	}

	ris= CutSpace(theForm.Sesso.value);
	if (ris.length==0) {	
		alert("Inserire il Sesso");
		theForm.Sesso.focus();
		return false;	    
	}

  	ris= theForm.DataNascita.value;
  	if (!IsDate(ris)) {
  		alert("Data Data nascita non valida");			
  		theForm.DataNascita.focus();
  		return false;
  	}
  	
	ris= CutSpace(theForm.LuogoNascita.value);
	if (ris.length==0) {
		alert("Inserire il Luogo di Nascita");
		theForm.LuogoNascita.focus();
		return false;	    
	}
	
	ris= CutSpace(theForm.PrNascita.value);
	if (ris.length==0) {
		alert("Inserire la Provincia di Nascita");
		theForm.PrNascita.focus();
		return false;	    
	}
			
	if ((theForm.Username.value== "") || (theForm.Username.value.length < iMinLenUsername ))  {
		alert("Inserire lo username con lunghezza di almeno " + iMinLenUsername + " caratteri");
		theForm.Username.focus();
		return false;	    
	}
	if (!sUsernamePattern.test(theForm.Username.value) ) {
	    alert("Inserire nello username solo lettere e/o numeri!");
	    theForm.Username.focus();
	    return false;	    
	}

	if (bCheckValidPassword(theForm.Password.value, sErrors)==false) {
		alert(sErrors);
		theForm.Password.focus();
		return false;
	}
/*
	if ((theForm.Password.value== "") || (theForm.Password.value.length < iMinLenPassword ))  {
		alert("Inserire la password con lunghezza di almeno " + iMinLenPassword + " caratteri");
		theForm.Password.focus();
		return false;	    
	}
*/
	if (isEmail(theForm.Email.value) == false) {			
		alert("L'indirizzo Email inserito non è valido!\nRimuovere gli eventuali spazi!");
		theForm.Email.focus();
		return false;	 
	}

	ris= CutSpace(theForm.LinkType.value);
	if (ris.length==0) {		
	    alert("Scelgliere il tipo di collegamento ad Internet di cui dispone l'utente");
	    theForm.LinkType.focus();
	    return false;	    
	}

	ris= CutSpace(theForm.IDAzienda.value);
	if (ris.length==0) {	
	    alert("Inserire l'azienda a cui fa parte l'utente!");
	    theForm.IDAzienda.focus();
	    return false;	    
	}
	
	ris= CutSpace(theForm.Indirizzo.value);
	if (ris.length==0) {	
	    alert("Inserire l'indirizzo");
	    theForm.Indirizzo.focus();
	    return false;	    
	}

	ris= CutSpace(theForm.CAP.value);
	if (ris.length==0) {	
	    alert("Inserire il CAP");
	    theForm.CAP.focus();
	    return false;	    
	}
			
	ris= CutSpace(theForm.Citta.value);
	if (ris.length==0) {
	    alert("Inserire la Citta");
	    theForm.Citta.focus();
	    return false;	    
	}
	
	ris= CutSpace(theForm.Provincia.value);
	if (ris.length==0) {
	    alert("Inserire la Provincia");
	    theForm.Provincia.focus();
	    return false;	    
	}
	
/*	
	if ((theForm.Telefono.value== ""))  {
		alert("Inserire numero di telefono fisso.");
		theForm.Telefono.focus();
		return bReturn;	    
	}
*/	 	

	ris= CutSpace(theForm.Cellulare.value);
	if (ris.length!=0) {		
		if (!sMobilePattern.test(theForm.Cellulare.value) ) {
		    alert("Inserire il numero di Cellulare nel formato +39NNNNNNNNN\n\nOppure lasciare vuoto.");
		    theForm.Cellulare.focus();
		    return false;	    
		}		
	}	

	if (theForm.NotificheSMS.checked==true) {
		ris= CutSpace(theForm.Cellulare.value);
		if (ris.length!=0) {		
			if (!sMobilePattern.test(theForm.Cellulare.value) ) {
			    alert("Inserire il numero di Cellulare nel formato +39NNNNNNNNN\n\nOppure lasciare vuoto.");
			    theForm.Cellulare.focus();
			    return false;	    
			}		
		} else {
			alert("Per ricevere le notifiche via SMS è necessario specificare\nil numero (+39NNNNNNNNN) e l'operatore.");
		    theForm.Cellulare.focus();	
			return false;
		}	
		
		ris= CutSpace(theForm.CellulareOperatore.value);		
		if (ris.length==0) {
			alert("Per ricevere le notifiche via SMS è necessario selezionare l'operatore.");
		    theForm.CellulareOperatore.focus();
		    return false;			
		}			
	}
						
	return true;
}

function CheckProfiloUtente(theForm) {			
	var ris;		
	var sTemp;	
			  	
	ris= CutSpace(theForm.Cognome.value);
	if (ris.length==0) {
		alert("Inserire il Cognome");
		theForm.Cognome.focus();
		return false;	    
	}

	ris= CutSpace(theForm.Nome.value);
	if (ris.length==0) {
	    alert("Inserire il Nome");
	    theForm.Nome.focus();
	    return false;	    
	}

	ris= CutSpace(theForm.Sesso.value);
	if (ris.length==0) {	
		alert("Inserire il Sesso");
		theForm.Sesso.focus();
		return false;	    
	}

	ris= CutSpace(theForm.DataNascita.value) ;       
	if (!IsDate(ris)) {
		alert("Data Nascita non valida!");
		theForm.DataNascita.focus();
		return false;
	}	

	ris= CutSpace(theForm.LuogoNascita.value);
	if (ris.length==0) {
		alert("Inserire il Luogo di Nascita");
		theForm.LuogoNascita.focus();
		return false;	    
	}
	
	ris= CutSpace(theForm.PrNascita.value);
	if (ris.length==0) {
		alert("Inserire la Provincia di Nascita");
		theForm.PrNascita.focus();
		return false;	    
	}
			
	ris= CutSpace(theForm.LinkType.value);
	if (ris.length==0) {		
	    alert("Scelgliere il tipo di collegamento ad Internet di cui dispone l'utente");
	    theForm.LinkType.focus();
	    return false;	    
	}
	
	ris= CutSpace(theForm.Indirizzo.value);
	if (ris.length==0) {	
	    alert("Inserire l'indirizzo");
	    theForm.Indirizzo.focus();
	    return false;	    
	}

	ris= CutSpace(theForm.CAP.value);
	if (ris.length==0) {	
	    alert("Inserire il CAP");
	    theForm.CAP.focus();
	    return false;	    
	}
			
	ris= CutSpace(theForm.Citta.value);
	if (ris.length==0) {
	    alert("Inserire la Citta");
	    theForm.Citta.focus();
	    return false;	    
	}
	
	ris= CutSpace(theForm.Provincia.value);
	if (ris.length==0) {
	    alert("Inserire la Provincia");
	    theForm.Provincia.focus();
	    return false;	    
	}

	ris= CutSpace(theForm.Cellulare.value);
	if (ris.length!=0) {		
		if (!sMobilePattern.test(theForm.Cellulare.value) ) {
		    alert("Inserire il numero di Cellulare nel formato +39NNNNNNNNN\n\nOppure lasciare vuoto.");
		    theForm.Cellulare.focus();
		    return false;	    
		}		
	}	
	
	if (theForm.NotificheSMS.checked==true) {
		ris= CutSpace(theForm.Cellulare.value);
		if (ris.length!=0) {		
			if (!sMobilePattern.test(theForm.Cellulare.value) ) {
			    alert("Inserire il numero di Cellulare nel formato +39NNNNNNNNN\n\nOppure lasciare vuoto.");
			    theForm.Cellulare.focus();
			    return false;	    
			}		
		} else {
			alert("Per ricevere le notifiche via SMS è necessario specificare\nil numero (+39NNNNNNNNN) e l'operatore.");
		    theForm.Cellulare.focus();	
			return false;
		}	
		
		ris= CutSpace(theForm.CellulareOperatore.value);		
		if (ris.length==0) {
			alert("Per ricevere le notifiche via SMS è necessario selezionare l'operatore.");
		    theForm.CellulareOperatore.focus();
		    return false;			
		}			
	}		
	
	return true;
}

function CheckFormSearchUser(theForm) {	
	var ris;		
	ris= CutSpace(theForm.IDRuolo.value);
	if (ris.length != 0) {		
		ris= CutSpace(theForm.IDApplicazione.value);
		if (ris.length == 0) {
			alert ("Se si specifica il ruolo è necessarrio\nspecificare anche l'applicazione!");
			theForm.IDApplicazione.focus();
			return false;
		}
	}    
	
	return true;
}

function CheckLostPassword(theForm) {			
	if (isEmail(theForm.EMail.value) == false) {			
		alert("L'indirizzo Email inserito non è valido!\nRimuovere gli eventuali spazi!");
		theForm.EMail.focus();
		return false;	 
	}	

	return true;
}

function CheckOkUserPwd(frmLogin)
{
    var ris;
    ris= CutSpace(frmLogin.username.value);
    if (ris.length == 0) {
        alert ("Lo Username non può essere nullo");
        frmLogin.username.focus();
        return false;
    } 
     
    ris= CutSpace(frmLogin.password.value);
    if (ris.length == 0) {
        alert ("La Password non può essere nulla");
        frmLogin.password.focus();
        return false;
    }   
    return true;
}

function formNews_submit(theForm)
{ 
	var ris;
    var datag;
    var datam;
    var dataa;

	datag   = StripLeading0(Strip(theForm.DataArticolo_g.value))
	datam   = StripLeading0(Strip(theForm.DataArticolo_m.value))
	dataa   = StripLeading0(Strip(theForm.DataArticolo_a.value))	
    if (check_data(datag,datam,dataa) == false) {
    	theForm.DataArticolo_g.focus();
    	return false;
    }

	datag   = StripLeading0(Strip(theForm.InEvidenzaFinoAl_g.value))
	datam   = StripLeading0(Strip(theForm.InEvidenzaFinoAl_m.value))
	dataa   = StripLeading0(Strip(theForm.InEvidenzaFinoAl_a.value))	
    if (check_data(datag,datam,dataa) == false) {
    	theForm.InEvidenzaFinoAl_g.focus();
    	return false;
    }
	            
   	ris= CutSpace(theForm.IDCategoria.value);	    
    if (ris.length ==0)  {    
        alert ("Inserire la Categoria.");
        theForm.IDCategoria.focus();
        return false;     
  	}
    
	ris= CutSpace(theForm.Titolo.value);	    
    if (ris.length ==0)  {   
        alert ("Inserire il Titolo.");
        theForm.Titolo.focus();
        return false;	    
  	}

  	ris= CutSpace(theForm.Articolo.value);	    
    if (ris.length ==0)  {    
        alert ("Inserire l'articolo.");
        theForm.Articolo.focus();
        return false;     
  	}

	datag   = StripLeading0(Strip(theForm.ValidaDal_g.value))
	datam   = StripLeading0(Strip(theForm.ValidaDal_m.value))
	dataa   = StripLeading0(Strip(theForm.ValidaDal_a.value))	
    if (check_data(datag,datam,dataa) == false) {
    	theForm.ValidaDal_g.focus();
    	return false;
    }

	datag   = StripLeading0(Strip(theForm.ValidaAl_g.value))
	datam   = StripLeading0(Strip(theForm.ValidaAl_m.value))
	dataa   = StripLeading0(Strip(theForm.ValidaAl_a.value))	
    if (check_data(datag,datam,dataa) == false) {
    	theForm.ValidaAl_g.focus();
    	return false;
    }
        
	return true;
}

function frmRuolo_submit(theForm) 
{
	var ris;
	ris= CutSpace(theForm.IDApplicazione.value);	
	if (ris==0) {
		alert("Selezionare l'applicazione");
		theForm.IDApplicazione.focus();
		return false;
	}

	ris= CutSpace(theForm.IDRuolo.value);	
	if (ris==0) {
		alert("Selezionare il ruolo");
		theForm.IDRuolo.focus();
		return false;
	}

	if (theForm.enabled.checked==false) {
		alert("Attenzione. Abilitazione mancante") 
	}
			
	return true;	
}	

function formRuoli_submit(theForm)
{ 
	var ris;
    
    ris= CutSpace(theForm.Descrizione.value);	    
 	if (ris.length ==0)  {   
    	alert ("Inserire la descrizione del ruolo.");
        theForm.Descrizione.focus();
        return false;
    }    	    

	return true;
}

function formApplicazioni_submit(theForm)
{ 
	var ris;

    ris= CutSpace(theForm.Titolo.value);	    
 	if (ris.length ==0)  {   
    	alert ("Inserire il Titolo dell'applicazione web.");
        theForm.Titolo.focus();
        return false;
    } 
        
    ris= CutSpace(theForm.Applicazione.value);	    
 	if (ris.length ==0)  {   
    	alert ("Inserire il nome dell'applicazione web.");
        theForm.Applicazione.focus();
        return false;
    }    	    

    ris= CutSpace(theForm.PathApplicazione.value);	    
 	if (ris.length ==0)  {   
    	alert ("Inserire il percorso della pagina di default dell'applicazione web.");
        theForm.PathApplicazione.focus();
        return false;
    }    	    

	return true;
}

function SMS_calcCharLeft(Target) {
	StrLen = Target.value.length;
	if (StrLen > SMS_maxchar ) {
		Target.value = Target.value.substring(0,SMS_maxchar);
		CharsLeft = 0;
		window.alert("Lunghezza stringa eccessiva di: " + (StrLen - SMS_maxchar) + " carattere/i");
	} else {
		CharsLeft = SMS_maxchar - StrLen;
	}
	Target.form.charsleft.value = SMS_maxchar - Target.value.length;
}

function SMS_checkForm(form) {
	var sTemp;
	SMS_calcCharLeft(form.charsleft);
	if (form.Message.value.length==0) {
		alert("Inserire un testo per il messaggio.");
		form.Message.focus();
		return false;
	}	
	sTemp = form.Receiver.value;
	if ((sTemp== "") || (sTemp.substr(0,4)!= "+393")) {
    		alert("Inserire il Numero di Cellulare nel formato +393...\nSolo numeri Vodafone.");
    		form.Receiver.focus();
		return false;	    
	}
	return true;
}

function SMS_CheckBroadCast(form) {
	var sTemp;
	SMS_calcCharLeft(form.charsleft);
	if (form.Message.value.length==0) {
		alert("Inserire un testo per il messaggio.");
		form.Message.focus();
		return false;
	}	
	return true;
}

function SMS_Rubrica_OnChange(object)
{
	if(object.value.length==0) {
		alert(object.value);
	}	
	else {		
		document.smsform.Receiver.value= object.value.substring(object.value.indexOf("/")+1,object.value.length)		
	}
}

function formReminder_submit(theForm)
{
	var ris;

    ris= CutSpace(theForm.TipoInvio.value);	 
    if (ris.length ==0)  {   
        alert ("Scegliere il tipo di invio!");
        theForm.TipoInvio.focus();
        return false;	    
  	}

    ris= CutSpace(theForm.Destinatario.value);	
 	if (ris.length ==0)  {   
        alert ("Inserire il destinatario");
        theForm.Destinatario.focus();
        return false;	    
  	}
  	
    ris= CutSpace(theForm.Mittente.value);	
 	if (ris.length ==0)  {   
        alert ("Inserire il mittente");
        theForm.Mittente.focus();
        return false;	    
  	}

    ris= CutSpace(theForm.Subject.value);	
 	if (ris.length ==0)  {   
        alert ("Inserire l'oggetto del messaggio!");
        theForm.Subject.focus();
        return false;	    
  	}

	datag = StripLeading0(Strip(theForm.DataInvio_g.value))
	datam = StripLeading0(Strip(theForm.DataInvio_m.value))
	dataa = StripLeading0(Strip(theForm.DataInvio_a.value))	
    if (check_data(datag,datam,dataa) == false) {
    	theForm.DataInvio_g.focus();
    	return false;
    }

	OraInvio_h = Strip(theForm.OraInvio_h.value)
    OraInvio_m = Strip(theForm.OraInvio_m.value)	

	if (check_ora(OraInvio_h) || check_minuti(OraInvio_m)) {
		alert("L'orario di invio inserito non è valida!");
	  	theForm.OraInvio_h.focus();
	  	return false;
	}
	      	
    ris= CutSpace(theForm.Destinatario.value);	
 	if (ris.length ==0)  {   
        alert ("Scegliere il tipo di invio!");
        theForm.Destinatario.focus();
        return false;	    
  	}
  	
    ris= CutSpace(theForm.Destinatario.value);	
 	if (ris.length ==0)  {   
        alert ("Scegliere il tipo di invio!");
        theForm.Destinatario.focus();
        return false;	    
  	}
  	  	  	  	
	return true;
}

function formFAQ_submit(theForm)
{ 
	var ris;
	ris= CutSpace(theForm.Titolo.value);	    
    if (ris.length ==0)  {   
        alert ("Inserire il Titolo.");
        theForm.Titolo.focus();
        return false;	    
  	}

  	ris= CutSpace(theForm.Help.value);	    
    if (ris.length ==0)  {    
        alert ("Inserire la descrizione dell'Help.");
        theForm.Help.focus();
        return false;     
  	}

	return true;
}

function formHelp_submit(theForm)
{ 
	var ris;
	ris= CutSpace(theForm.Titolo.value);	    
    if (ris.length ==0)  {   
        alert ("Inserire il Titolo.");
        theForm.Titolo.focus();
        return false;	    
  	}

  	ris= CutSpace(theForm.Help.value);	    
    if (ris.length ==0)  {    
        alert ("Inserire la descrizione dell'Help.");
        theForm.Help.focus();
        return false;     
  	}

	return true;
}


