/*******
*	Controlla che siano stati compilati i campi obbligatori
*	di un form.
*	
*	utilizzo:
*		1- definire un array di stringhe ( needed ) contenente
*		   i nomi dei campi obbligatori
*		2- definire un array di stringhe ( labels ) contenente
*		   i nomi da visualizzare all'utente dei campi obbligatori
*		
*		<form ... onsubmit="return( checkFormData(this) );">
*	     oppure
*		<form ... onsubmit="return( checkFormData(document.nomeform) );">
*******/

function checkFormData(obj)
{
	var formname = '';
	
	if( typeof obj == 'object' && obj.constructor == 'String' )
		formname = obj+".";
	else
		formname = 'document.'+obj.name+'.';
	
	var returnValue = true;
	var message = new String('');
	
	for( var i = 0; i < needed.length; i++ )
	{
		if( typeof eval(formname+needed[i]) == 'object' && trimString(eval(formname+needed[i]+'.value')) == '' )
		{
			message += '\n'+labels[i];
			returnValue = false;
		}
	}
	
	if( !returnValue )
	{
		alert('Attenzione i seguenti campi sono obbligatori:'+message);
	}
	
	return(returnValue);
}

/*******
*	Utilizzata internamente da checkDate, checkFormData.
*	Elimina gli spazi all'inizio e alla fine della stringa
*	passata come argomento.
*	n.b.	č possibile aggiungere la funzione ad un oggetto
*		di tipo stringa.
*	
*	utilizzo:
*		
*		str = trimString('  una stringa ');	// str conterrā 'una stringa'
*	     oppure
*		str = '  una stringa ';
*		str.trim = trimString;
*		str.trim();	// str conterrā 'una stringa'
*******/
function trimString( str )
{
	str = ( this != window ) ? this : str;
	return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}
/*******
*	Limita la lunghezza di una textarea in base ai caratteri definiti nella 
*	proprietā MAXLENGTH
*	come argomento occorre passare l'oggetto textarea 
*	chiamare la funzione col metodo onBlur
*******/
function CheckMaxLength(obj, nome)
{		
 var str = obj.value;
            
	if( str.length > obj.maxlength && obj.maxlength > 1 )
    {
    	alert("Il campo ''" + nome + "'' ha troppi caratteri\nil numero massimo di caratteri previsti č''(" + obj.maxlength + ")'',\nquindi verrā ridotto!");
    obj.value = str.substring(0,obj.maxlength);
    }
 }	
 