
// 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, thisValue ){
	
	var Group = false; 
	
     while ( cboFlt.options.length > 0 ) cboFlt.options[0] = null;
		
 	var y = 0;
				
	for ( i = 0; i < arrValue.length; i++ ){	 
		
		if ( arrValue[i][0] == thisValue || Empty( arrValue[i][0] )){
			
			Group = false;
			
			if ( arrValue[0].length > 3 ){
				Group = ( arrValue[i][3] = '1' );
			}
			
			if ( !Group  ){
			
				cboFlt.options[y] = new Option( RTrim(arrValue[i][2]) );
				cboFlt.options[y].value = arrValue[i][1];
			}
			else {
				
				cboFlt.options[y] = new Option( RTrim(arrValue[i][2]) );
				cboFlt.options[y].value = arrValue[i][1];
			}
			
			y++; 
		}
			
     	cboFlt.options.selectedIndex = 0;		
     }
	
	cboFlt.fireEvent('onchange');
}

function Select_MFlt_cbo( cboFromFlt, cboToFlt, arrValue, cboThis ){

     while ( cboFromFlt.options.length > 0 ) cboFromFlt.options[0] = null;

 	var y = 0;
	
		for ( i = 0; i < arrValue.length; i++ ){
	
			if ( Empty( arrValue[i][0] ) && !Empty( arrValue[i][1] )){
		
				cboFromFlt.options[y] = new Option( RTrim(arrValue[i][2]) );
				cboFromFlt.options[y].value = arrValue[i][1];
					
				y++;
			}
		}
	
	for ( x = 0; x < cboThis.length; x++ ){
		
		for ( i = 0; i < arrValue.length; i++ ){
	
			if ( arrValue[i][0] == cboThis.options[x].value ){
		
				cboFromFlt.options[y] = new Option( RTrim(arrValue[i][2]) );
				cboFromFlt.options[y].value = arrValue[i][1];
					
				y++;
			}
		}
	}
	
	var Selected;

	for ( y1 = 0; y1 < cboToFlt.length; y1++ ){
		
		Selected = false;
		
		for ( y2 = 0; y2 < cboFromFlt.length; y2++ ){
                     
			if ( cboToFlt.options[y1].value == cboFromFlt.options[y2].value ){
                    Selected = true;        
			}
		}
		
		if ( !Selected ){
			
			cboToFlt.options[y1] = null;
			y1 = -1;    
		}
	}

	cboToFlt.fireEvent('onchange');
}
	
function Select_MFlt_opg( cboFromFlt, cboToFlt, arrValue, thisValue, Checked ){

	var y = cboFromFlt.length;

	if ( Checked ){

		for ( i = 0; i < arrValue.length; i++ ){
	
			if ( arrValue[i][0] == thisValue ){
		
				cboFromFlt.options[y] = new Option( RTrim(arrValue[i][2]) );
				cboFromFlt.options[y].value = arrValue[i][1];
					
				y++;
			}
		}
	}
		
	else {
		
		for ( i = 0; i < arrValue.length; i++ ){
	
			if ( arrValue[i][0] == thisValue ){
		
                 	for ( y = 0; y < cboFromFlt.length; y++ ){
                   
                   		if ( arrValue[i][1] == cboFromFlt.options[y].value ){
                       
                      		cboFromFlt.options[y] = null;
                          	y = -1;    
                       	}
                   	}

                    for ( y = 0; y < cboToFlt.length; y++ ){
                     
                    	if ( arrValue[i][1] == cboToFlt.options[y].value ){
                            
                        		cboToFlt.options[y] = null;
                            	y = -1;    
                         }
               	}
			}
		}
	}
	
	cboToFlt.fireEvent('onchange');
}

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;
		t_cboDel.fireEvent('onchange');
	}		 
}

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
	}
		
	t_cboTo.fireEvent('onchange');

	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 Checked = false;

		for ( I = 0; I < ControlToValidate.length; I++ ){
		
			if ( ControlToValidate[I].checked ){
			
				Checked = true;
			
				if ( ControlToValidate[I].value == InitialValue ){
				
					ErrorMessage = ControlCaption + VRequired_Change_No;	
				}
				
				break;
			}
		}
		
	     if ( !Checked ){
		
			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 );	
		
		if ( RMin != null )
			DigitMin = parseFloat(RMin);
		if ( RMax != null )
			DigitMax = parseFloat(RMax);

		if ( RMin != null && RMax == null ){
		
			if ( DigitToValidate < DigitMin ){
				ErrorMessage = ControlCaption + VRangeMin_No + RMin;
			}		
		}
		
		if ( RMin == null && RMax != null ){
		
			if ( DigitToValidate > DigitMax ){
				ErrorMessage = ControlCaption + VRangeMax_No + RMax;		
			}
		} 
		
		if ( RMin != null && RMax != null ){
		
			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 );
		
		if ( RMin != null )	
			DataMin = StringToDate(RMin);
		if ( RMax != null )
			DataMax = StringToDate(RMax);

		if ( RMin != null && RMax == null ){
			
			if ( DataToValidate < DataMin ){
				ErrorMessage = ControlCaption + VRangeMin_No + RMin;
			}		
		}
		
		if ( RMin == null && RMax != null ){
			
			if ( DataToValidate > DataMax ){
				ErrorMessage = ControlCaption + VRangeMax_No + RMax;
			}		
		} 
						
		if ( RMin != null && RMax != null ){
			
			if ( DataToValidate < DataMin || DataToValidate > DataMax ){
				ErrorMessage = ControlCaption + VRange_No + RMin + VRange_ + RMax;
			}
		}
	}
	else {
		
		if ( RMin != null && RMax == null ){
			
			if ( ControlToValidate.value < RMin ){
				ErrorMessage = ControlCaption + VRangeMin_No + RMin;
			}		
		}
		
		if ( RMin == null && RMax != null ){
			
			if ( ControlToValidate.value > RMax ){
				ErrorMessage = ControlCaption + VRangeMax_No + RMax;
			}		
		} 	
		
		if ( RMin != null && RMax != null ){
			
			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++ ){ 
 			
    		if ( CharToAdd == undefined ){		
	
        		OutputString = OutputString + ' ';           
        	}
        	else{
        		
        		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++ ){ 
	
    		if ( CharToAdd == undefined ){		
	
        		OutputString = ' ' + OutputString;           
        	}
        	else{
        		
        		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 );
	}
}
