
<!-- Massimiliano Valiani ( EDS Informatica e Cultura ) -->


// Form

function SubmitForm( Form, URL ){

	if ( URL !=null ){
		Form.action = URL;
	}
	
	if ( Form.onsubmit() ){
		Form.submit();
	}
}

function Form_Action( Form, URL ){

	Form.action = URL;
}

function Form_Reset( Form ){                   

	self.document.Form.reset();
}

function CheckBox_CAll( chk ){ 

	for ( i = 0; i < chk.length; i++ ){ 
	
		 chk[i].checked = true;
	}
	
	return true;
}

function Select_SAll( cbo ){ 

	for ( i = 0; i < cbo.length; i++ ){ 
	
		 cbo[i].selected = true;
	}
	
	return true;
}

function Select_Flt( cboFlt, arrValue, Flt_Value ){
	
	with ( cboFlt ){
		
     	while ( options.length > 0 ) options[0] = null;
		
 		var x = 0;
		
 		for ( i = 0; i < arrValue.length; i++ ){	 
		
			if ( arrValue[i][0] == Flt_Value || Empty( arrValue[i][0] )){
				
				options[x] = new Option( RTrim(arrValue[i][2]) );
				options[x].value = arrValue[i][1];
				
				x++; 
			}
			
     		options.selectedIndex = 0;		
     	}
  	}
}

function Select_MFlt( cboFromFlt, cboToFlt, arrValue, Flt_Value, Check ){

	with ( cboFromFlt ){
		
		var x = length;

		if ( Check ){

			for ( i = 0; i < arrValue.length; i++ ){
	
				if ( arrValue[i][0] == Flt_Value ){
		
					options[x] = new Option( RTrim(arrValue[i][2]) );
					options[x].value = arrValue[i][1];
					
					x++;
				}
				
     			options.selectedIndex = 0;
			}
		}
		
		else{
		
			for ( i = 0; i < arrValue.length; i++ ){
	
				if ( arrValue[i][0] == Flt_Value ){
		
                   		for( x = 0; x < length; x++ ){
                   
                   			if( arrValue[i][1] == options[x].value ){
                       
                       			options[x] = null;
                       		}
                   		}

                   		if ( cboToFlt != null ){
                   
                         	for( xx = 0; xx < cboToFlt.length; xx++ ){
                     
                     			if ( arrValue[i][1] == cboToFlt.options[xx].value ){
                            
                        				cboToFlt.options[xx] = null;
                            			xx = -1;    
                            		}
                     		}
                   		}
				}
			}
		}
	}
}

function Select_Del( cboDel ){

 	var t_cboDel = BrwCmp_Lvl( cboDel );
	
	if ( document.all ){
   	    	
		var obj = eval( "document.all." + t_cboDel.name + ".options;" );	
    	}
     else if ( document.getElementById ){ 
		   
    		var obj = eval( "document.getElementById('" + t_cboDel.name + "').options;" );
    	}
	
	if ( true ){
	
		var Sel = document.createElement( 'SELECT' );
	 	
		for ( x = 0; x < obj.length ; x++ ){
				  			 	 	    		 				 								 	 
			if (!t_cboDel.options[x].selected ){	
				  			
				var Opt = document.createElement( 'OPTION' );
				    Opt = new Option();
				
				    Opt.text  = t_cboDel.options[x].text;
		    		    Opt.value = t_cboDel.options[x].value;
				    
				Sel.options[Sel.length] = Opt;												
			}					
		}	
			
		var t_cboDel_length = t_cboDel.length;
			
		for ( x = t_cboDel_length; x >= 0 ; x-- ){
			
			t_cboDel.options[x] = null;		
		}			
			
		for ( x = 0; x < Sel.length ; x++ ){
						 	
			t_cboDel.options[x] = new Option();
			
			t_cboDel.options[x].text  = Sel.options[x].text;
			t_cboDel.options[x].value = Sel.options[x].value;				
		}
									
		t_cboDel.selectedIndex=-1;
	}		 
}

function MoveTo( cboFrom, cboTo, cboOptMax, cboCaption, DisplayErrorMessage ){

	var t_cboFrom = BrwCmp_Lvl( cboFrom );
	var t_cboTo   = BrwCmp_Lvl( cboTo );
	
	var tt_cboTo = t_cboTo;
	
	if ( t_cboFrom.selectedIndex == -1 && t_cboTo.selectedIndex != -1 && false ){ 
	
		var t_cbo     = t_cboFrom;
		    t_cboFrom = t_cboTo;
              t_cboTo   = t_cbo;
	}
		    
	if ( document.all ){
	
	     var obj = eval( "document.all." + t_cboFrom.name + ".options;" );	
     }
     else if ( document.getElementById ){
     
          var obj = eval( "document.getElementById('" + t_cboFrom.name + "').options;" );
     }
	
	for ( x = 0; x < obj.length ; x++ ){
	
		if (( t_cboFrom.selectedIndex == -1 && t_cboTo.selectedIndex == -1 ) || ( t_cboFrom.selectedIndex != -1 && t_cboTo.selectedIndex != -1 )){
		
		      t_cboFrom.selectedIndex = -1;
                t_cboTo.selectedIndex   = -1;
			 			
		      return;
		}	
		
		if (( t_cboFrom.length == 0 ) && ( t_cboTo.length == 0 )){ 
		
			 t_cboFrom.selectedIndex = -1;
		      t_cboTo.selectedIndex   = -1;
			 	
		      return;
		}	

	     var Sel = document.createElement( 'SELECT' );
	     var Opt = document.createElement( 'OPTION' );
				
          if ( document.all ){
		
	          Sel = eval( "document.all." + cboTo );
          }
          else if ( document.getElementById ){
              
	          Sel = eval( "document.getElementById('" + cboTo + "')" );
          }
		
		if ( document.all ){ 
					
			Sel = document.all( t_cboTo.name );
		}
		else if ( document.getElementById ){

			Sel = eval( "document.getElementById('" + t_cboTo.name + "')" );
		}
		
		if ( Sel.options.length == cboOptMax && tt_cboTo.selectedIndex == -1 && t_cboFrom.options[x].selected ){ 
			
			if ( DisplayErrorMessage ){
					
	      		alert( MoveTo_Max + cboOptMax +' '+ cboCaption );
			}
			
			t_cboFrom.selectedIndex = -1;
 			t_cboTo.selectedIndex   = -1;
						
			return;
		}
		
		if ( t_cboFrom.options[x].selected ){

			var Opt = new Option();
			
		         Opt.text  = t_cboFrom.options[x].text;
		         Opt.value = t_cboFrom.options[x].value;
			    
			Sel.options[Sel.length] = Opt;
		}	

		Sel.option
	}
	
	return;
}


// Validate Form

function VRequired( ControlToValidate, ControlCaption, InitialValue, DisplayErrorMessage ){

	var ErrorMessage = '';
	
	if ( eval( 'ControlToValidate[0]' ) == '[object]' && ( ControlToValidate[0].type == 'radio' || 
												ControlToValidate[0].type == 'checkbox' )){ 	
																							   
	//   Controllo di Tipo 'Radio' o 'Checkbox' a gruppi
	
	     var Check = false;

		for( I = 0; I < ControlToValidate.length; I++ ){
		
			if ( ControlToValidate[I].checked ){
			
				Check = true;
			
				if ( ControlToValidate[I].value == InitialValue ){
				
					ErrorMessage = ControlCaption + VRequired_Change_No;	
				}
				
				break;
			}
		}
		
	     if ( !Check ){
		
			ErrorMessage = ControlCaption + VRequired_No;
		}

	}
	else {
	
		if ( Empty( ControlToValidate.value ) || Trim( ControlToValidate.value ) == '<p>&nbsp;</p>' || 
										 Trim( ControlToValidate.value ) == '<P>&nbsp;</P>'    ){
	
			ErrorMessage = ControlCaption + VRequired_No;		
		}
		else if ( Trim( ControlToValidate.value ) == InitialValue ){
	
			ErrorMessage = ControlCaption + VRequired_Change_No;	
		}
		
	}
	
	if ( ErrorMessage!= '' && DisplayErrorMessage ){	
		alert( ErrorMessage );		
	}

	return ErrorMessage;
}

	function VValue( ControlToValidate ){
		
	     var VValue = '';
		
		if ( eval( 'ControlToValidate[0]' ) == '[object]' && ( ControlToValidate[0].type == 'radio' || 
													ControlToValidate[0].type == 'checkbox' )){ 	
																							   
		//   Controllo di Tipo 'Radio' o 'Checkbox' a gruppi

			for( I = 0; I < ControlToValidate.length; I++ ){
		
				if ( ControlToValidate[I].checked ){
			
					VValue = ControlToValidate[I].value;
				
					break;
				}
			}

		}
		else {
			
			VValue = ControlToValidate.value	
		}
		
		return VValue;
	}
	
function VLen( ControlToValidate, ControlCaption, LMin, LMax, DisplayErrorMessage ){

	var ErrorMessage = '';

	if ( ControlToValidate.value.length < LMin ){

		ErrorMessage = ControlCaption + VLen_Min + LMin + VLen_;		
	}
	else if ( ControlToValidate.value.length > LMax ){

		ErrorMessage = ControlCaption + VLen_Max + LMax + VLen_;
	}
	
	if ( ErrorMessage!= '' && DisplayErrorMessage ){
		alert( ErrorMessage );
	}		

	return ErrorMessage;
}

function VAllowChar( ControlToValidate, ControlCaption, AllowChar, DisplayErrorMessage ){

	var ErrorMessage = '';
	var AC_RegExp = new RegExp('[^'+AllowChar+']');

	if ( ControlToValidate.value.match( AC_RegExp ) ){

		ErrorMessage = ControlCaption + VAllowChar_No;		
	}
	
	if ( ErrorMessage!= '' && DisplayErrorMessage ){
		alert( ErrorMessage );
	}
	
	return ErrorMessage;
}

function VRange( ControlToValidate, ControlCaption, RMin, RMax, DisplayErrorMessage ){

	var ErrorMessage = '';
	
	if( isDigit( ControlToValidate.value ) && isDigit(RMin) && isDigit(RMax) ){
		
		DigitToValidate = parseFloat( ControlToValidate.value );
		DigitMax = parseFloat(RMax);
		DigitMin = parseFloat(RMin);

		if ( DigitToValidate<DigitMin || DigitToValidate>DigitMax ){

			ErrorMessage = ControlCaption + VRange_No + RMin + VRange_ + RMax;
		}
	}
	else if ( isDate( ControlToValidate.value ) && isDate(RMin) && isDate(RMax) ){ 
		
		DataToValidate = StringToDate( ControlToValidate.value );
		DataMin = StringToDate(RMin);
		DataMax = StringToDate(RMax);

		if ( DataToValidate<DataMin || DataToValidate>DataMax ){

			ErrorMessage = ControlCaption + VRange_No + RMin + VRange_ + RMax;
		}
	}
	else{

		if ( ControlToValidate.value<RMin || ControlToValidate.value>RMax ){

			ErrorMessage = ControlCaption + VRange_No + RMin + VRange_ + RMax;
		}
	}	
	
	if ( ErrorMessage!= '' && DisplayErrorMessage ){
		alert( ErrorMessage );
	}

	return ErrorMessage;
}
	
function VCompare( ControlToValidate, ControlToCompare, Operator ){

	var Result;
	
	if( isDigit( ControlToValidate.value ) && isDigit( ControlToCompare.value ) ){

		DigitToValidate = parseFloat( ControlToValidate.value );
		DigitToCompare = parseFloat( ControlToCompare.value );
		
		Compare = 'DigitToValidate' + Operator + 'DigitToCompare';
		Result = eval(Compare);
	}
	else if( isDate( ControlToValidate.value ) && isDate( ControlToCompare.value ) ){

		DateToValidate = StringToDate( ControlToValidate.value );
		DateToCompare = StringToDate( ControlToCompare.value );
		
		Compare = 'DateToValidate' + Operator + 'DateToCompare';
		Result = eval(Compare);			
	}
	else{
	
		Compare = 'ControlToValidate.value' + Operator + 'ControlToCompare.value';
		Result = eval(Compare);
	}

	return Result;
}

function VIsDate( ControlToValidate, ControlCaption, DisplayErrorMessage ){

	var ErrorMessage = '';		
	
	if ( !isDate( ControlToValidate.value ) ){
	
		ErrorMessage = ControlCaption + VIs_No ;		
	}
	
	if ( ErrorMessage!= '' && DisplayErrorMessage ){	
		alert( ErrorMessage );		
	}

	return ErrorMessage;
}

function VIsDigit( ControlToValidate, ControlCaption, DisplayErrorMessage ){

	var ErrorMessage = '';		
	
	if ( !isDigit( ControlToValidate.value ) ){
	
		ErrorMessage = ControlCaption + VIs_No;		
	}
	
	if ( ErrorMessage!= '' && DisplayErrorMessage ){	
		alert( ErrorMessage );		
	}

	return ErrorMessage;
}

function VEMail( ControlToValidate, ControlCaption, DisplayErrorMessage ){

	if ( Empty( ControlToValidate.value ) ){
		return ''		
	}

	var ErrorMessage = '';
	var Filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;

	if (!Filter.test( ControlToValidate.value ) ){
	
		ErrorMessage= ControlCaption + VIs_No;
	}

	if ( ErrorMessage !='' && DisplayErrorMessage ){
		alert( ErrorMessage );
	}

	return ErrorMessage;
}


// ... altro

function isDigit( DigitStr ){

	ffloat = parseFloat( DigitStr );

	if ( isNaN( ffloat ) ){
		return false;
	}

	return true;
}

function isDate( DateStr ){

	var DatePattern = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var matchArray = DateStr.match( DatePattern ); 

	if ( matchArray == null ){ 
		return false;
	}

	day = matchArray[1];
	month = matchArray[3]; 
	year = matchArray[5];

	if ( month < 1 || month > 12 ){ 
		return false;
	}

	if ( day < 1 || day > 31 ){
		return false;
	}

	if (( month==4 || month==6 || month==9 || month==11 ) && day==31 ){ 
		return false;
	}

	if ( month == 2 ){
 
		var isleap = ( year % 4 == 0 && ( year % 100 != 0 || year % 400 == 0 ));

		if ( day > 29 || ( day==29 && !isleap )){ 
			return false;
		}
	}

	return true; 
}

function StringToDate( DateStr ){

	var DatePattern = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var matchArray = DateStr.match( DatePattern ); 
	
	day = matchArray[1];	
	month = matchArray[3];
	year = matchArray[5];
	
	var date = new Date( year, month-1, day );

	return date;
}

function LTrim( s ){

	return s.replace( /^\s*/, "" )
}

function RTrim( s ){

	return s.replace( /\s*$/, "" )
}

function Trim( s ){ 

	return RTrim(LTrim( s ))
}

function Empty( s ){ 

	return ( Trim( s ) == '' )
}

function PadR( InputString, OutputStringLength, CharToAdd ){
		
	OutputString = new String( InputString );	
	OutputString = Trim( OutputString );
	
	for( x = OutputString.length;  x < OutputStringLength; x++ ){ 
 			
		OutputString = OutputString + CharToAdd;
	}
	
	return OutputString;
}

function PadL( InputString, OutputStringLength, CharToAdd ){
	
	OutputString = new String( InputString );
	
	OutputString = Trim( OutputString.Trim );
	
	for( x = OutputString.length; x < OutputStringLength; x++ ){ 
	
		OutputString = CharToAdd + OutputString; 
	}
	
	return OutputString;
}

function BrwCmp_Lvl( p ){

	if ( document.getElementById ){
    	     return document.getElementById( p );
	}
	else if ( document.all ){
     	return document.all( p );
	}
	else if ( document.layer ){
     	return document.layer( p );
	}
}
