
    /*
      GENERAL CONTROLS FUNCTIONS
      These functions return values True or False depending of the control they need to do
      SUMMARY
            - Is_Text(text)
              control if the text is not empty
            - Is_Number(num)
              control that num is not empty and is a number
            - Is_Positive(num)
              control that null is a number greater than 0
            - Is_Integer(num)
              control that num is an Integer Number
            - Is_Email(text)
              control that text is a valid email address
            - Is_Web(text)
              control that text is a valid URL
            - Is_Selected(Selectfield)
              Control that the Selectfield have selected not the first item (that usually is 'Select a value')
            - Is_Checked(field)
              Control if the field or the list of field that are of 'radio' or 'checkbox' type are checked (at least one if is a list of fields)
			- Is_Color(value)
    */
	function p_err(field, ev, err){
		if (! eval(ev)){
			alert(err)
			try{field.focus();}catch(e){}
			return false
		}
		return true
	}
	
	function Is_Text(text){
        return (text!="")
    }

    function Is_Number(num){
       if (Is_Text(num)){
          if (isNaN(num)) return false
          return true
       }
       return false
    }

    function Is_Positive(num){
        if (Is_Number(num)){
           if (parseFloat(num)>0) return true
           return false
        }
        return false
    }

    function Is_Integer(num){
        if (Is_Number(num)){
          var RegNum=new RegExp("^[0-9]*$")
          return (RegNum.test(num))
        }
        return false
    }
	
    function Is_Email(text){
         if (Is_Text(text)){
            var RegMail=new RegExp("[A-Za-z0-9_.-]+@([A-Za-z0-9_]+\.)+[A-Za-z]{2,4}")
            return (RegMail.test(text))
         }
         return false
    }

    function Is_Web(text){
         if (Is_Text(text)){
            var RegWeb=new RegExp("^(http://|https://|ftp://){0,1}[A-Za-z0-9][A-Za-z0-9\-\.]+[A-Za-z0-9]\.[A-Za-z]{2,}[\43-\176]*$")
            return (RegWeb.test(text))
         }
         return false
    }
	
    function Is_Selected(Selectfield){
         return (Selectfield.options.length<2 || Selectfield.selectedIndex>0)
    }

    function Is_Checked(field){
        var i
		if (field.length){
           for (i=0;i<field.length;i++){
               if (field[i].checked) return true
           }
           return false
        }else{
              return field.checked
        }
    }
	function Is_Color(value){
		var RegNum=new RegExp("^\#[0-9,A-F]{6}$")//
          return (RegNum.test(value.toUpperCase()))
	}
	

	/*
      GENERAL UTILITY FUNCTIONS
	  SUMMARY:
             SelectFromValue (SelectField, Value)
			 	Select the option of the SelectField that have the specified value
			 SelectFromText (SelectField, Text)
			 	Select the option of the SelectField that have the specified text
			Check_All (Field, Selected)
				Check or uncked (depending from selected value) all the Field
			NestedSelect (ParentSelect, ChildSelect, ArrayName, TextToShow)
				This function allow (having the right arrays) to populate a select depending from the slected value of a parent select. TextToShow will be insert as first option of the select
    */

    function SelectFromValue(SelectField,Value){
             var i
			 for (i=0;i<SelectField.options.length;i++){
                 if (SelectField.options[i].value==Value) SelectField.selectedIndex=i
             }
    }

    function SelectFromText(SelectField,Text){
            var i
			 for (i=0;i<SelectField.options.length;i++){
                 if (SelectField.options[i].text==Text) SelectField.selectedIndex=i
             }
    }

    function Check_All(Field,Selected){
        var i
		if (Field.length){
          for (i=0;i<Field.length;i++)
              Field[i].checked=Selected
        }else{
              Field.checked=Selected
        }
    }
	
	function NestedSelect (ParentSelect, ChildSelect, ArrayName, TextToShow){
		var c=0, ArId, ArN, i
		for (i=ChildSelect.options.length;i>=0;i--){
			ChildSelect.options[i]=null
		}
		ChildSelect.options[0] = new Option (TextToShow)
		ChildSelect.options[0].value=''
		if (ParentSelect.options.selectedIndex>0){
			ArId = eval(ArrayName+ParentSelect.options[ParentSelect.selectedIndex].value+'Id')
			ArN = eval(ArrayName+ParentSelect.options[ParentSelect.selectedIndex].value+'Nome')
			c=ArId.length
			ChildSelect.disabled=(c==0)
			for (i=0;i<c;i++){
				ChildSelect.options[i+1]= new Option(ArN[i])
				ChildSelect.options[i+1].value= ArId[i]
				ChildSelect.options[i+1].text= ArN[i]
			}
		}else{
			ChildSelect.disabled=true
		}
		if (c==0) ChildSelect.options[0].text=TextToShow
	}

	function Check_File () {
	var i, s, ext, args = Check_File.arguments
		field=args[0]
		s= field.value.toLowerCase()
		ext=s.substr(s.lastIndexOf('.')+1)
		for (i=1; i<args.length; i++){
			if	(ext==args[i]) return true
		} 
		return false
	}
	
	
	//Rilevo il tipo di browser
	function CJL_BrowserSniffer()
	{
	   var ua = navigator.userAgent;
	
	   this.isOpera = function()
	   {
	      return /Opera/.test(ua);
	   }
	
	   this.isSafari = function()
	   {
	      return /Safari/.test(ua);
	   }
	
	   this.isGecko = function()
	   {
	      return navigator.product == "Gecko" &&
		     ! ( this.isOpera() || this.isSafari() );
	   }
	
	   this.isIEWin = function()
	   {
	      return window.external && /Win/.test(ua);
	   }
	
	   this.isIEMac = function()
	   {
	      return window.external && /Mac/.test(ua);
	   }
	
	   this.getVersion = function()
	   {
	      if( this.isIEWin() || this.isIEMac() )
	      {
	         return Number(ua.match(/MSIE ([0-9.]+)/)[1]);
	      }
	      else if( this.isSafari() )
	      {
	         return Number(ua.match(/[0-9.]+$/));
	      }
	      else if( this.isGecko() )
	      {
	         var n = ua.match(/rv:([0-9.]+)/)[1];
	
	         var ar = n.split(".");
	
	         var s = ar[0] + ".";
	
	         for(var i = 1; i < ar.length; ++i)
	         {
	            s += ("0" + ar[i]).match(/.{2}$/)[0];
	         }
	
	         return Number(s);
	      }
	      else if( this.isOpera() )
	      {
	         return Number(ua.match(/Opera.([0-9.]+)/)[1]);
	      }
	      else
	      {
	         return null;
	      }
	   }
	}
	
	function toHtml(val)
	{
		val = val.split("\"").join("&quot;");
		val = val.split("à").join("&agrave;");
		val = val.split("é").join("&eacute;");
		val = val.split("è").join("&egrave;");
		val = val.split("ò").join("&ograve;");
		val = val.split("ì").join("&igrave;");
		val = val.split("ù").join("&ugrave;");
		return val
	}
	
	// anteprima immagini
	/*function preview(nome) 	{	

	   immagine_da_caricare = document.getElementById(nome).value;
	
       sniffer = new CJL_BrowserSniffer();
	   
	   if( sniffer.isIEWin() )
	   {
		  document.getElementById('img'+nome).src = "file:///"+immagine_da_caricare
	      browserType = "Internet Explorer per Windows";
	   }
	   else if( sniffer.isIEMac() )
	   {
		  document.getElementById('img'+nome).src = "file:///"+immagine_da_caricare
	      browserType = "Internet Explorer per Macintosh";
	   }
	   else if( sniffer.isSafari() )
	   {
		  document.getElementById('img'+nome).src = "../images/noanteprima.jpg";
	      browserType = "Safari"
	   }
	   else if( sniffer.isGecko() )
	   {
		  document.getElementById('img'+nome).src = "../images/noanteprima.jpg";
	      browserType = "Gecko family";
	   }
	   else if( sniffer.isOpera() )
	   {
		  document.getElementById('img'+nome).src = "../images/noanteprima.jpg";
	      browserType = "Opera";
	   }
	   else
	   {
		  document.getElementById('img'+nome).src = "../images/noanteprima.jpg";
	      browserType = "Unknown";
	   }
		
	}*/


