// LiveValidation 1.3 (standalone version)
// Copyright (c) 2007-2008 Alec Hill (www.livevalidation.com)
// LiveValidation is licensed under the terms of the MIT License
/*********************************************** LiveValidation class ***********************************/
/**
 *	validates a form field in real-time based on validations you assign to it
 *	
 *	@var element {mixed} - either a dom element reference or the string id of the element to validate
 *	@var optionsObj {Object} - general options, see below for details
 *
 *	optionsObj properties:
 *							validMessage {String} 	- the message to show when the field passes validation
 *													  (DEFAULT: "Thankyou!")
 *							onValid {Function} 		- function to execute when field passes validation
 *													  (DEFAULT: function(){ this.insertMessage(this.createMessageSpan()); this.addFieldClass(); } )	
 *							onInvalid {Function} 	- function to execute when field fails validation
 *													  (DEFAULT: function(){ this.insertMessage(this.createMessageSpan()); this.addFieldClass(); })
 *							insertAfterWhatNode {Int} 	- position to insert default message
 *													  (DEFAULT: the field that is being validated)	
 *              onlyOnBlur {Boolean} - whether you want it to validate as you type or only on blur
 *                            (DEFAULT: false)
 *              wait {Integer} - the time you want it to pause from the last keystroke before it validates (ms)
 *                            (DEFAULT: 0)
 *              onlyOnSubmit {Boolean} - whether should be validated only when the form it belongs to is submitted
 *                            (DEFAULT: false)						
 */
var LiveValidation = function(element, optionsObj){
  	this.initialize(element, optionsObj);
}
LiveValidation.VERSION = '1.3 standalone';
/** element types constants ****/
LiveValidation.TEXTAREA 		= 1;
LiveValidation.TEXT 			= 2;
LiveValidation.PASSWORD 		= 3;
LiveValidation.CHECKBOX 		= 4;
LiveValidation.SELECT 			= 5;
LiveValidation.FILE 			= 6;
/****** Static methods *******/
/**
 *	pass an array of LiveValidation objects and it will validate all of them
 *	
 *	@var validations {Array} - an array of LiveValidation objects
 *	@return {Bool} - true if all passed validation, false if any fail						
 */
LiveValidation.massValidate = function(validations){
  var returnValue = true;
	for(var i in validations)
	{
		//MB aggiunto IF qui sotto per problemi compatibilita IE
		if (validations[i].validate)
		{
			var valid = validations[i].validate();
			if(returnValue) returnValue = valid;
		}
	}
	return returnValue;
}
/****** prototype ******/
LiveValidation.prototype = {
    validClass: 'LV_valid',
    invalidClass: 'LV_invalid',
    messageClass: 'LV_validation_message',
    validFieldClass: 'LV_valid_field',
    invalidFieldClass: 'LV_invalid_field',
    /**
     *	initialises all of the properties and events
     *
     * @var - Same as constructor above
     */
    initialize: function(element, optionsObj){
      var self = this;
      if(!element) throw new Error("LiveValidation::initialize - No element reference or element id has been provided!");
    	this.element = element.nodeName ? element : document.getElementById(element);
    	if(!this.element) throw new Error("LiveValidation::initialize - No element with reference or id of '" + element + "' exists!");
      // default properties that could not be initialised above
    	this.validations = [];
      this.elementType = this.getElementType();
      this.form = this.element.form;
      // options
    	var options = optionsObj || {};
    	this.validMessage = options.validMessage || '';
    	var node = options.insertAfterWhatNode || this.element;
		this.insertAfterWhatNode = node.nodeType ? node : document.getElementById(node);
      this.onValid = options.onValid || function(){ this.insertMessage(this.createMessageSpan()); this.addFieldClass(); };
      this.onInvalid = options.onInvalid || function(){ this.insertMessage(this.createMessageSpan()); this.addFieldClass(); };	
    	this.onlyOnBlur =  options.onlyOnBlur || false;
    	this.wait = options.wait || 0;
      this.onlyOnSubmit = options.onlyOnSubmit || false;
      // add to form if it has been provided
      if(this.form){
        this.formObj = LiveValidationForm.getInstance(this.form);
        this.formObj.addField(this);
      }
      // events
      // collect old events
      this.oldOnFocus = this.element.onfocus || function(){};
      this.oldOnBlur = this.element.onblur || function(){};
      this.oldOnClick = this.element.onclick || function(){};
      this.oldOnChange = this.element.onchange || function(){};
      this.oldOnKeyup = this.element.onkeyup || function(){};
      this.element.onfocus = function(e){ self.doOnFocus(e); return self.oldOnFocus.call(this, e); }
      if(!this.onlyOnSubmit){
        switch(this.elementType){
          case LiveValidation.CHECKBOX:
            this.element.onclick = function(e){ self.validate(); return self.oldOnClick.call(this, e); }
          // let it run into the next to add a change event too
          case LiveValidation.SELECT:
          case LiveValidation.FILE:
            this.element.onchange = function(e){ self.validate(); return self.oldOnChange.call(this, e); }
            break;
          default:
            if(!this.onlyOnBlur) this.element.onkeyup = function(e){ self.deferValidation(); return self.oldOnKeyup.call(this, e); }
      	    this.element.onblur = function(e){ self.doOnBlur(e); return self.oldOnBlur.call(this, e); }
        }
      }
    },
	
	/**
     *	destroys the instance's events (restoring previous ones) and removes it from any LiveValidationForms
     */
    destroy: function(){
  	  if(this.formObj){
		// remove the field from the LiveValidationForm
		this.formObj.removeField(this);
		// destroy the LiveValidationForm if no LiveValidation fields left in it
		this.formObj.destroy();
	  }
      // remove events - set them back to the previous events
	  this.element.onfocus = this.oldOnFocus;
      if(!this.onlyOnSubmit){
        switch(this.elementType){
          case LiveValidation.CHECKBOX:
            this.element.onclick = this.oldOnClick;
          // let it run into the next to add a change event too
          case LiveValidation.SELECT:
          case LiveValidation.FILE:
            this.element.onchange = this.oldOnChange;
            break;
          default:
            if(!this.onlyOnBlur) this.element.onkeyup = this.oldOnKeyup;
      	    this.element.onblur = this.oldOnBlur;
        }
      }
      this.validations = [];
	  this.removeMessageAndFieldClass();
    },
    
    /**
     *	adds a validation to perform to a LiveValidation object
     *
     *	@var validationFunction {Function} - validation function to be used (ie Validate.Presence )
     *	@var validationParamsObj {Object} - parameters for doing the validation, if wanted or necessary
     * @return {Object} - the LiveValidation object itself so that calls can be chained
     */
    add: function(validationFunction, validationParamsObj){
      this.validations.push( {type: validationFunction, params: validationParamsObj || {} } );
      return this;
    },
    
	/**
     *	removes a validation from a LiveValidation object - must have exactly the same arguments as used to add it 
     *
     *	@var validationFunction {Function} - validation function to be used (ie Validate.Presence )
     *	@var validationParamsObj {Object} - parameters for doing the validation, if wanted or necessary
     * @return {Object} - the LiveValidation object itself so that calls can be chained
     */
    remove: function(validationFunction, validationParamsObj){
	  var found = false;
	  for( var i = 0, len = this.validations.length; i < len; i++ ){
	  		if( this.validations[i].type == validationFunction ){
				if (this.validations[i].params == validationParamsObj) {
					found = true;
					break;
				}
			}
	  }
      if(found) this.validations.splice(i,1);
      return this;
    },
    
    removeAll: function(){
    	
	  for( var i = 0, len = this.validations.length; i < len; i++ ){
	  	
	  		this.validations.splice(i,1);
	  }
      return this;
      
    },    
	
    /**
     * makes the validation wait the alotted time from the last keystroke 
     */
    deferValidation: function(e){
      if(this.wait >= 300) this.removeMessageAndFieldClass();
    	var self = this;
      if(this.timeout) clearTimeout(self.timeout);
      this.timeout = setTimeout( function(){ self.validate() }, self.wait); 
    },
        
    /**
     * sets the focused flag to false when field loses focus 
     */
    doOnBlur: function(e){
      this.focused = false;
      this.validate(e);
    },
        
    /**
     * sets the focused flag to true when field gains focus 
     */
    doOnFocus: function(e){
      this.focused = true;
      this.removeMessageAndFieldClass();
    },
    
    /**
     *	gets the type of element, to check whether it is compatible
     *
     *	@var validationFunction {Function} - validation function to be used (ie Validate.Presence )
     *	@var validationParamsObj {Object} - parameters for doing the validation, if wanted or necessary
     */
    getElementType: function(){
      switch(true){
        case (this.element.nodeName.toUpperCase() == 'TEXTAREA'):
        return LiveValidation.TEXTAREA;
      case (this.element.nodeName.toUpperCase() == 'INPUT' && this.element.type.toUpperCase() == 'TEXT'):
        return LiveValidation.TEXT;
      case (this.element.nodeName.toUpperCase() == 'INPUT' && this.element.type.toUpperCase() == 'PASSWORD'):
        return LiveValidation.PASSWORD;
      case (this.element.nodeName.toUpperCase() == 'INPUT' && this.element.type.toUpperCase() == 'CHECKBOX'):
        return LiveValidation.CHECKBOX;
      case (this.element.nodeName.toUpperCase() == 'INPUT' && this.element.type.toUpperCase() == 'FILE'):
        return LiveValidation.FILE;
      case (this.element.nodeName.toUpperCase() == 'SELECT'):
        return LiveValidation.SELECT;
        case (this.element.nodeName.toUpperCase() == 'INPUT'):
        	throw new Error('LiveValidation::getElementType - Cannot use LiveValidation on an ' + this.element.type + ' input!');
        default:
        	throw new Error('LiveValidation::getElementType - Element must be an input, select, or textarea!');
      }
    },
    
    /**
     *	loops through all the validations added to the LiveValidation object and checks them one by one
     *
     *	@var validationFunction {Function} - validation function to be used (ie Validate.Presence )
     *	@var validationParamsObj {Object} - parameters for doing the validation, if wanted or necessary
     * @return {Boolean} - whether the all the validations passed or if one failed
     */
    doValidations: function(){
    	this.message = "";
      	this.validationFailed = false;
      	for(var i = 0, len = this.validations.length; i < len; ++i){
    	 	var validation = this.validations[i];
    		switch(validation.type){
    		   	case Validate.Presence:
                case Validate.Confirmation:
                case Validate.Acceptance:
    		   		this.displayMessageWhenEmpty = true;
    		   		this.validationFailed = !this.validateElement(validation.type, validation.params); 
    				break;
    		   	default:
    		   		this.validationFailed = !this.validateElement(validation.type, validation.params);
    		   		break;
    		}
    		if(this.validationFailed) return false;	
    	}
    	if(this.message=="")
    		this.message = this.validMessage;
      	return true;
    },
    
    /**
     *	performs validation on the element and handles any error (validation or otherwise) it throws up
     *
     *	@var validationFunction {Function} - validation function to be used (ie Validate.Presence )
     *	@var validationParamsObj {Object} - parameters for doing the validation, if wanted or necessary
     * @return {Boolean} - whether the validation has passed or failed
     */
    validateElement: function(validationFunction, validationParamsObj){
      	var value = (this.elementType == LiveValidation.SELECT) ? ((this.element.selectedIndex==-1)?"":this.element.options[this.element.selectedIndex].value) : this.element.value;     
        if(validationFunction == Validate.Acceptance){
    	    if(this.elementType != LiveValidation.CHECKBOX) throw new Error('LiveValidation::validateElement - Element to validate acceptance must be a checkbox!');
    		value = this.element.checked;
    	}
        var isValid = true;
      	try{    
    		validationFunction(value, validationParamsObj,this.element);//MB aggiunto parametro
    	} catch(error) {
    	  	if(error instanceof Validate.Error){
    			if( value !== '' || (value === '' && this.displayMessageWhenEmpty) ){
    				this.validationFailed = true;
    				this.message = error.message;
    				isValid = false;
    			}
    	  	}	
    	  	else if(error instanceof Validate.NoError)
    		{
    			//alert("SI");
    			this.message = error.message;  			
    		}
    		else
    		{
    		  	throw error;
    		}
    	}finally{
    	    return isValid;
        }      
    },
    
    /**
     *	makes it do the all the validations and fires off the onValid or onInvalid callbacks
     *
     * @return {Boolean} - whether the all the validations passed or if one failed
     */
    validate: function(){
      if(!this.element.disabled){
		var isValid = this.doValidations();
		if(isValid){
			this.onValid();
			return true;
		}else {
			this.onInvalid();
			return false;
		}
	  }else{
      return true;
    }
    },
	
 /**
   *  enables the field
   *
   *  @return {LiveValidation} - the LiveValidation object for chaining
   */
  enable: function(){
  	this.element.disabled = false;
	return this;
  },
  
  /**
   *  disables the field and removes any message and styles associated with the field
   *
   *  @return {LiveValidation} - the LiveValidation object for chaining
   */
  disable: function(){
  	this.element.disabled = true;
	this.removeMessageAndFieldClass();
	return this;
  },
    
    /** Message insertion methods ****************************
     * 
     * These are only used in the onValid and onInvalid callback functions and so if you overide the default callbacks,
     * you must either impliment your own functions to do whatever you want, or call some of these from them if you 
     * want to keep some of the functionality
     */
    
    /**
     *	makes a span containg the passed or failed message
     *
     * @return {HTMLSpanObject} - a span element with the message in it
     */

    createMessageSpan: function(){
        var span = document.createElement('span');
    	var textNode = document.createTextNode(this.message);
      	span.appendChild(textNode);
        return span;
    },

    
    /**
     *	inserts the element containing the message in place of the element that already exists (if it does)
     *
     * @var elementToIsert {HTMLElementObject} - an element node to insert
     */
    insertMessage: function(elementToInsert){
      	this.removeMessage();
      	
      	// CD 10/12/09 Serve per non far comparire il box del messaggio, nel caso che non ci sia nessun messaggio
      	if (this.message != '')
		{      	
	      	if( (this.displayMessageWhenEmpty && (this.elementType == LiveValidation.CHECKBOX || this.element.value == ''))
	    	  || this.element.value != '' ){
	            var className = this.validationFailed ? this.invalidClass : this.validClass;
	    	  	elementToInsert.className += ' ' + this.messageClass + ' ' + className;
	            if(this.insertAfterWhatNode.nextSibling){
	    		  		this.insertAfterWhatNode.parentNode.insertBefore(elementToInsert, this.insertAfterWhatNode.nextSibling);
	    		}else{
	    			    this.insertAfterWhatNode.parentNode.appendChild(elementToInsert);
	    	    }
	    	}
    	}
    },
    
    
    /**
     *	changes the class of the field based on whether it is valid or not
     */
    addFieldClass: function(){
        this.removeFieldClass();
        if(!this.validationFailed){
            if(this.displayMessageWhenEmpty || this.element.value != ''){
                if(this.element.className.indexOf(this.validFieldClass) == -1) this.element.className += ' ' + this.validFieldClass;
            }
        }else{
            if(this.element.className.indexOf(this.invalidFieldClass) == -1) this.element.className += ' ' + this.invalidFieldClass;
        }
    },
    
    /**
     *	removes the message element if it exists, so that the new message will replace it
     */
    removeMessage: function(){
    	var nextEl;
    	var el = this.insertAfterWhatNode;
    	while(el.nextSibling){
    	    if(el.nextSibling.nodeType === 1){
    		  	nextEl = el.nextSibling;
    		  	break;
    		}
    		el = el.nextSibling;
    	}
      	if(nextEl && nextEl.className.indexOf(this.messageClass) != -1) this.insertAfterWhatNode.parentNode.removeChild(nextEl);
    },
    
    /**
     *	removes the class that has been applied to the field to indicte if valid or not
     */
    removeFieldClass: function(){
      if(this.element.className.indexOf(this.invalidFieldClass) != -1) this.element.className = this.element.className.split(this.invalidFieldClass).join('');
      if(this.element.className.indexOf(this.validFieldClass) != -1) this.element.className = this.element.className.split(this.validFieldClass).join(' ');
    },
        
    /**
     *	removes the message and the field class
     */
    removeMessageAndFieldClass: function(){
      this.removeMessage();
      this.removeFieldClass();
    }
} // end of LiveValidation class
/*************************************** LiveValidationForm class ****************************************/
/**
 * This class is used internally by LiveValidation class to associate a LiveValidation field with a form it is icontained in one
 * 
 * It will therefore not really ever be needed to be used directly by the developer, unless they want to associate a LiveValidation 
 * field with a form that it is not a child of
 */
/**
   *	handles validation of LiveValidation fields belonging to this form on its submittal
   *	
   *	@var element {HTMLFormElement} - a dom element reference to the form to turn into a LiveValidationForm
   */
var LiveValidationForm = function(element){
  this.initialize(element);
}
/**
 * namespace to hold instances
 */
LiveValidationForm.instances = {};
/**
   *	gets the instance of the LiveValidationForm if it has already been made or creates it if it doesnt exist
   *	
   *	@var element {HTMLFormElement} - a dom element reference to a form
   */
LiveValidationForm.getInstance = function(element){
  var rand = Math.random() * Math.random();
  if(!element.id) element.id = 'formId_' + rand.toString().replace(/\./, '') + new Date().valueOf();
  if(!LiveValidationForm.instances[element.id]) LiveValidationForm.instances[element.id] = new LiveValidationForm(element);
  return LiveValidationForm.instances[element.id];
}
LiveValidationForm.prototype = {
  
  /**
   *	constructor for LiveValidationForm - handles validation of LiveValidation fields belonging to this form on its submittal
   *	
   *	@var element {HTMLFormElement} - a dom element reference to the form to turn into a LiveValidationForm
   */
  initialize: function(element){
  	this.name = element.id;
    this.element = element;
    this.fields = [];
    // preserve the old onsubmit event
	this.oldOnSubmit = this.element.onsubmit || function(){};
    var self = this;
    this.element.onsubmit = function(e){
      return (LiveValidation.massValidate(self.fields)) ? self.oldOnSubmit.call(this, e || window.event) !== false : false;
    }
  },
  
  /**
   *	adds a LiveValidation field to the forms fields array
   *	
   *	@var element {LiveValidation} - a LiveValidation object
   */
  addField: function(newField){
    this.fields.push(newField);
  },
  
  /**
   *	removes a LiveValidation field from the forms fields array
   *	
   *	@var victim {LiveValidation} - a LiveValidation object
   */
  removeField: function(victim){
  	var victimless = [];
  	for( var i = 0, len = this.fields.length; i < len; i++){
		if(this.fields[i] !== victim) victimless.push(this.fields[i]);
	}
    this.fields = victimless;
  },
  
  /**
   *	destroy this instance and its events
   *
   * @var force {Boolean} - whether to force the detruction even if there are fields still associated
   */
  destroy: function(force){
  	// only destroy if has no fields and not being forced
  	if (this.fields.length != 0 && !force) return false;
	// remove events - set back to previous events
	this.element.onsubmit = this.oldOnSubmit;
	// remove from the instances namespace
	LiveValidationForm.instances[this.name] = null;
	return true;
  }
   
}// end of LiveValidationForm prototype
/*************************************** Validate class ****************************************/
/**
 * This class contains all the methods needed for doing the actual validation itself
 *
 * All methods are static so that they can be used outside the context of a form field
 * as they could be useful for validating stuff anywhere you want really
 *
 * All of them will return true if the validation is successful, but will raise a ValidationError if
 * they fail, so that this can be caught and the message explaining the error can be accessed ( as just 
 * returning false would leave you a bit in the dark as to why it failed )
 *
 * Can use validation methods alone and wrap in a try..catch statement yourself if you want to access the failure
 * message and handle the error, or use the Validate::now method if you just want true or false
 */
var Validate = {
	prodotto1: function(value, paramsObj, theObject)
	{	
	
		var divWait = document.getElementById(theObject.id+"_waitDiv");
		var imgTest = document.getElementById(theObject.id+"_waitImg");
		if (divWait && !imgTest)//evito che compaia due volte
		{
			var imgWait = document.createElement("img");
			var imgName = theObject.id+"_waitImg";
			imgWait.setAttribute('id',imgName);
			imgWait.setAttribute('src',"images/ajax-loader.gif");
			divWait.appendChild(imgWait);
		}		
		var message = paramsObj.failureMessage || "KO!";

		combo(document.forms.dati,'site','matricola',false);
		
		if (divWait)
		{
			divWait.removeChild(imgWait);
		}
		
		//alert(document.getElementById('lastCallResult').value);
		if (document.getElementById('lastCallResult').value=="0")
			Validate.fail(message);
		else
			return true;
    },	

    
	prodotto0: function(value, paramsObj, theObject)
	{	
		var divWait = document.getElementById(theObject.id+"_waitDiv");
		var imgTest = document.getElementById(theObject.id+"_waitImg");
		if (divWait && !imgTest)//evito che compaia due volte
		{
			var imgWait = document.createElement("img");
			var imgName = theObject.id+"_waitImg";
			imgWait.setAttribute('id',imgName);
			imgWait.setAttribute('src',"images/ajax-loader.gif");
			divWait.appendChild(imgWait);
		}		
		var message = paramsObj.failureMessage || "KO!";

		combo(document.forms.dati,'site','matricola2',false);
		
		if (divWait)
		{
			divWait.removeChild(imgWait);
		}
		
		//alert(document.getElementById('lastCallResult').value);
		if (document.getElementById('lastCallResult').value=="0")
		{
			Validate.ok(message);
		}
		else
		{
			return true;
		}
    },	
    
    
    
	
	/*
	Aggiunta Foreach 07-04-2009
	Esegue la validazione su due campi
	*/
	CustomAjaxCall: function(value, paramsObj, theObject)
	{	
		//campo aggiuntivo
		var voElem = document.getElementById(paramsObj.fieldAgg);
		value2 = voElem.value;
		
		//testo la validazione solo se anche il secondo campo e' compilato
		if (value2=="")
			return true;
		
		var divWait = document.getElementById(theObject.id+"_waitDiv");
		var imgTest = document.getElementById(theObject.id+"_waitImg");
		if (divWait && !imgTest)//evito che compaia due volte
		{
			var imgWait = document.createElement("img");
			var imgName = theObject.id+"_waitImg";
			imgWait.setAttribute('id',imgName);
			imgWait.setAttribute('src',"images/ajax-loader.gif");
			divWait.appendChild(imgWait);
		}		
		var message = paramsObj.failureMessage || "KO!";
		var url = paramsObj.url;
		var oXMLHttpRequest = new XMLHttpRequest;
		
		//compongo la richiesta
		if (url.indexOf("?"))
			url = url + "&value=" + value;
		else
			url = url + "?value=" + value;	
		
		url = url + "&value2=" + value2;
				
		oXMLHttpRequest.open("GET", url, false);
		oXMLHttpRequest.onreadystatechange = function() 
		{ 
			if (this.readyState == XMLHttpRequest.DONE) 
			{
				if (oXMLHttpRequest.responseText==1)
					vbReturn = true;
				else
					vbReturn = false;	
			} 
		} 
		oXMLHttpRequest.send(null); 	
		
		if (divWait)
		{
			divWait.removeChild(imgWait);
		}
		
		if (!vbReturn)
			Validate.fail(message);
		else
			return true;
    },
	
	
	/*
	Aggiunta Foreach 26-03-2009
	Permette di specificare un url da richiamare via ajax, a cui passare il valore, 
	che ritornera' 1 o 0
	
	failureMessage : messaggio se errato
	url: url da richiamare via ajax, a cui verra' concatenato il parametro value con il contenuto del campo
	*/
	AjaxCall: function(value, paramsObj, theObject)
	{	
		
		var divWait = document.getElementById(theObject.id+"_waitDiv");
		var imgTest = document.getElementById(theObject.id+"_waitImg");
		if (divWait && !imgTest)//evito che compaia due volte
		{
			var imgWait = document.createElement("img");
			var imgName = theObject.id+"_waitImg";
			imgWait.setAttribute('id',imgName);
			imgWait.setAttribute('src',"images/ajax-loader.gif");
			divWait.appendChild(imgWait);
		}		
		var message = paramsObj.failureMessage || "KO!";
		var url = paramsObj.url;
		var oXMLHttpRequest = new XMLHttpRequest;
		
		//compongo la richiesta
		if (url.indexOf("?"))
			url = url + "&value=" + value;
		else
			url = url + "?value=" + value;	
			
		oXMLHttpRequest.open("GET", url, false);
		oXMLHttpRequest.onreadystatechange = function() 
		{ 
			if (this.readyState == XMLHttpRequest.DONE) 
			{
				if (oXMLHttpRequest.responseText==1)
					vbReturn = true;
				else
					vbReturn = false;	
			} 
		} 
		oXMLHttpRequest.send(null); 	
		
		if (divWait)
		{
			divWait.removeChild(imgWait);
		}
		
		if (!vbReturn)
			Validate.fail(message);
		else
			return true;
    },	
	
	/*
	Aggiunta Foreach 26-03-2009
	Controlla se il valore e' un codice fiscale valido
	
	failureMessage : messaggio se errato
	*/
    CodFiscale: function(value, paramsObj){
      	var paramsObj = paramsObj || {};
    	var message = paramsObj.failureMessage || "Codice fiscale non valido";
    	
    	vsControllo = ControllaCF(value);
    	
    	if(vsControllo !== '')
    	{ 
    	  	Validate.fail(message);
    	}
    	return true;
    },
    
	/*
	Aggiunta Foreach 30-1-2010
	Controlla se il valore e' una partita iva valida
	
	failureMessage : messaggio se errato
	*/
    PIva: function(value, paramsObj){
      	var paramsObj = paramsObj || {};
    	var message = paramsObj.failureMessage || "Partita IVA non valida";
    	
    	vsControllo = ControllaPIVA(value);
    	
    	if(vsControllo !== '')
    	{ 
    	  	Validate.fail(message);
    	}
    	return true;
    },
    
    CodFiscaleOrPiva: function(value, paramsObj){
      	var paramsObj = paramsObj || {};
    	var message = paramsObj.failureMessage || "Codice fiscale non valido";
    	
    	vsControllo = ControllaCF(value);
    	if(vsControllo !== '')
	   vsControllo = ControllaPIVA(value);
	
    	if(vsControllo !== '')
    	{ 
    	  	Validate.fail(message);
    	}
    	return true;
    },
	/*
	Aggiunta Foreach 03-12-2010
	Controlla l'iban è corretto
	
	failureMessage : messaggio se errato
	*/
    IBAN: function(value, paramsObj){
      	var paramsObj = paramsObj || {};
    	var message = paramsObj.failureMessage || "IBAN non valido";
    	
    	text = document.getElementById(paramsObj.prefisso).value + document.getElementById(paramsObj.cin).value + document.getElementById(paramsObj.abi).value + document.getElementById(paramsObj.cab).value + document.getElementById(paramsObj.conto).value; 

    	vsControllo = ControllaIBAN(text);
 	
    	if(vsControllo !== '')
    	{ 
    	  	Validate.fail(message);
    	}
    	return true;
    },

    /*
	Aggiunta Foreach 14-01-2010
	Controlla se i link iniziano con http://
	
	failureMessage : messaggio se errato
	*/
	ContrLink: function(value, paramsObj){
	  	var paramsObj = paramsObj || {};
		var message = paramsObj.failureMessage || "Link non valido";
		
		vsControllo = ControllaLink(value);
		
		if (!vsControllo)
		{ 
		  	Validate.fail(message);
		}
		return true;
	},
	
	/*
	Aggiunta Foreach 15-01-2010
	Controlla che la data scadenza rds sia posteriore a oggi
	
	failureMessage : messaggio se errato
	*/
	ContrDataScad: function(value, paramsObj){
	  	var paramsObj = paramsObj || {};
		var message = paramsObj.failureMessage || "Data non Valida";
		
		vsControllo = ControllaDataScad(value);
		
		if (!vsControllo)
		{ 
		  	Validate.fail(message);
		}
		return true;
	},
	
	/*
	Aggiunta Foreach 15-01-2010
	Controlla che la data fase 1 deve essere posteriore a Data scadenza RDS
	
	failureMessage : messaggio se errato
	*/
	ContrDataFase1: function(value, paramsObj){
	  	var paramsObj = paramsObj || {};
		var message = paramsObj.failureMessage || "Data non Valida";
		vsControllo = ControllaDataFase1(value);
		if (!vsControllo)
		{ 
		  	Validate.fail(message);
		}
		return true;
	},
	
	/*
	Aggiunta Foreach 15-01-2010
	Controlla che la data fase 2 deve essere posteriore a Data ultima FASE 1
	
	failureMessage : messaggio se errato
	*/
	ContrDataFase2: function(value, paramsObj){
	  	var paramsObj = paramsObj || {};
		var message = paramsObj.failureMessage || "Data non Valida";
		vsControllo = ControllaDataFase2(value);
		if (!vsControllo)
		{ 
		  	Validate.fail(message);
		}
		return true;
	},
	

	/*
	Aggiunta Foreach 15-01-2010
	Controlla che la data fase 3 deve essere posteriore a Data ultima FASE 2
	
	failureMessage : messaggio se errato
	*/
	ContrDataFase3: function(value, paramsObj){
	  	var paramsObj = paramsObj || {};
		var message = paramsObj.failureMessage || "Data non Valida";
		vsControllo = ControllaDataFase3(value);
		if (!vsControllo)
		{ 
		  	Validate.fail(message);
		}
		return true;
	},
    
    
    /*
	Aggiunta Foreach 26-03-2009
	Controlla se il valore e' una data valida 
	la data deve arrivare in formato dd/mm/yyyy, puo' essere controllata con Format x regexp
	
	failureMessage : messaggio se errato
	*/
    Date: function(value, paramsObj){
      	var paramsObj = paramsObj || {};
    	var message = paramsObj.failureMessage || "Codice fiscale non valido";

    	if(!isDate(value))
    	{ 
    	  	Validate.fail(message);
    	}
    	return true;
    },
    
    
    ValidateFormat: function(value, paramsObj){

    	var value = String(value);
    	var paramsObj = paramsObj || {};
    	
    	var messageFail = paramsObj.errorMessage || "Formato non valido!";
    	var messageOk=paramsObj.okMessage || Validate.okMessage;
    	
        var pattern = new RegExp(paramsObj.pattern || "/./");
        var negate = paramsObj.negate || false;

      if(!negate && !pattern.test(value))
      	Validate.fail(messageFail); // normal 
      else if(negate && pattern.test(value)) 
      	Validate.fail(messageFail); // negated	
      else
      	Valide.ok(messegeOk);      	
     
    	return true;
    },
    
    
    /**
     *	validates that the field has been filled in
     *
     *	@var value {mixed} - value to be checked
     *	@var paramsObj {Object} - parameters for this particular validation, see below for details
     *
     *	paramsObj properties:
     *							failureMessage {String} - the message to show when the field fails validation 
     *													  (DEFAULT: "Can't be empty!")
     */
    Presence: function(value, paramsObj){
      	var paramsObj = paramsObj || {};
      	
    	if(value === '' || value === null || value === undefined)
    	{ 
    		
    	  	Validate.fail(paramsObj.errorMessage || 'Campo obbligatorio');
    	}
    	else
    	{
    		if (paramsObj.okMessage != undefined)
				Validate.ok(paramsObj.okMessage);
    		else
				Validate.ok(Validate.okMessage);
//			Validate.ok(paramsObj.okMessage || Validate.okMessage);

		}
    	return true;
    },
    
    okMessage: 'Ok',
    errorMessage: 'Ko',
       
    /**
     *	validates that the value is numeri-c, does not fall within a given range of numbers
     *	
     *	@var value {mixed} - value to be checked
     *	@var paramsObj {Object} - parameters for this particular validation, see below for details
     *
     *	paramsObj properties:
     *							notANumberMessage {String} - the message to show when the validation fails when value is not a number
     *													  	  (DEFAULT: "Must be a number!")
     *							notAnIntegerMessage {String} - the message to show when the validation fails when value is not an integer
     *													  	  (DEFAULT: "Must be a number!")
     *							wrongNumberMessage {String} - the message to show when the validation fails when is param is used
     *													  	  (DEFAULT: "Must be {is}!")
     *							tooLowMessage {String} 		- the message to show when the validation fails when minimum param is used
     *													  	  (DEFAULT: "Must not be less than {minimum}!")
     *							tooHighMessage {String} 	- the message to show when the validation fails when maximum param is used
     *													  	  (DEFAULT: "Must not be more than {maximum}!")
     *							is {Int} 					- the length must be this long 
     *							minimum {Int} 				- the minimum length allowed
     *							maximum {Int} 				- the maximum length allowed
     *                         onlyInteger {Boolean} - if true will only allow integers to be valid
     *                                                             (DEFAULT: false)
     *
     *  NB. can be checked if it is within a range by specifying both a minimum and a maximum
     *  NB. will evaluate numbers represented in scientific form (ie 2e10) correctly as numbers				
     */
    
    
    // da implementare
    
    Numerical: function(value, paramsObj){
        var suppliedValue = value;
        var value = Number(value);
    	var paramsObj = paramsObj || {};
        var minimum = ((paramsObj.minimum) || (paramsObj.minimum == 0)) ? paramsObj.minimum : null;
        var maximum = ((paramsObj.maximum) || (paramsObj.maximum == 0)) ? paramsObj.maximum : null;
    	var is = ((paramsObj.is) || (paramsObj.is == 0)) ? paramsObj.is : null;
        var notANumberMessage = paramsObj.notANumberMessage || "Must be a number!";
        var notAnIntegerMessage = paramsObj.notAnIntegerMessage || "Must be an integer!";
    	var wrongNumberMessage = paramsObj.wrongNumberMessage || "Must be " + is + "!";
    	var tooLowMessage = paramsObj.tooLowMessage || "Must not be less than " + minimum + "!";
    	var tooHighMessage = paramsObj.tooHighMessage || "Must not be more than " + maximum + "!";
        if (!isFinite(value)) Validate.fail(notANumberMessage);
        if (paramsObj.onlyInteger && (/\.0+$|\.$/.test(String(suppliedValue))  || value != parseInt(value)) ) Validate.fail(notAnIntegerMessage);
    	switch(true){
    	  	case (is !== null):
    	  		if( value != Number(is) ) Validate.fail(wrongNumberMessage);
    			break;
    	  	case (minimum !== null && maximum !== null):
    	  		Validate.Numericality(value, {tooLowMessage: tooLowMessage, minimum: minimum});
    	  		Validate.Numericality(value, {tooHighMessage: tooHighMessage, maximum: maximum});
    	  		break;
    	  	case (minimum !== null):
    	  		if( value < Number(minimum) ) Validate.fail(tooLowMessage);
    			break;
    	  	case (maximum !== null):
    	  		if( value > Number(maximum) ) Validate.fail(tooHighMessage);
    			break;
    	}
    	return true;
    },
    
    /**
     *	validates against a RegExp pattern
     *	
     *	@var value {mixed} - value to be checked
     *	@var paramsObj {Object} - parameters for this particular validation, see below for details
     *
     *	paramsObj properties:
     *							failureMessage {String} - the message to show when the field fails validation
     *													  (DEFAULT: "Not valid!")
     *							pattern {RegExp} 		- the regular expression pattern
     *													  (DEFAULT: /./)
     *             negate {Boolean} - if set to true, will validate true if the pattern is not matched
   *                           (DEFAULT: false)
     *
     *  NB. will return true for an empty string, to allow for non-required, empty fields to validate.
     *		If you do not want this to be the case then you must either add a LiveValidation.PRESENCE validation
     *		or build it into the regular expression pattern
     */
    Format: function(value, paramsObj){
      var value = String(value);
    	var paramsObj = paramsObj || {};
    	var message = paramsObj.failureMessage || "Formato non valido!";
      var pattern = paramsObj.pattern || /./;
      var negate = paramsObj.negate || false;
      if(!negate && !pattern.test(value)) Validate.fail(message); // normal
      if(negate && pattern.test(value)) Validate.fail(message); // negated
    	return true;
    },
    
    /**
     *	validates that the field contains a valid email address
     *	
     *	@var value {mixed} - value to be checked
     *	@var paramsObj {Object} - parameters for this particular validation, see below for details
     *
     *	paramsObj properties:
     *							failureMessage {String} - the message to show when the field fails validation
     *													  (DEFAULT: "Must be a number!" or "Must be an integer!")
     */
    Email: function(value, paramsObj){
    	var paramsObj = paramsObj || {};
    	var message = paramsObj.failureMessage || "Must be a valid email address!";
    	
    	//aggiunta di MB
    	if (paramsObj.trimValue)
    		value = trim(value);
    	
    	Validate.Format(value, { failureMessage: message, pattern: /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i } );
    	return true;
    },
    
    /**
     *	validates the length of the value
     *	
     *	@var value {mixed} - value to be checked
     *	@var paramsObj {Object} - parameters for this particular validation, see below for details
     *
     *	paramsObj properties:
     *							wrongLengthMessage {String} - the message to show when the fails when is param is used
     *													  	  (DEFAULT: "Must be {is} characters long!")
     *							tooShortMessage {String} 	- the message to show when the fails when minimum param is used
     *													  	  (DEFAULT: "Must not be less than {minimum} characters long!")
     *							tooLongMessage {String} 	- the message to show when the fails when maximum param is used
     *													  	  (DEFAULT: "Must not be more than {maximum} characters long!")
     *							is {Int} 					- the length must be this long 
     *							minimum {Int} 				- the minimum length allowed
     *							maximum {Int} 				- the maximum length allowed
     *
     *  NB. can be checked if it is within a range by specifying both a minimum and a maximum				
     */
    Length: function(value, paramsObj){
	
    	var value = String(value);
    	var paramsObj = paramsObj || {};
        var minimum = ((paramsObj.minimum) || (paramsObj.minimum == 0)) ? paramsObj.minimum : null;
    	var maximum = ((paramsObj.maximum) || (paramsObj.maximum == 0)) ? paramsObj.maximum : null;
    	var is = ((paramsObj.is) || (paramsObj.is == 0)) ? paramsObj.is : null;
        var wrongLengthMessage = paramsObj.wrongLengthMessage || "Must be " + is + " characters long!";
    	var tooShortMessage = paramsObj.tooShortMessage || "Must not be less than " + minimum + " characters long!";
    	var tooLongMessage = paramsObj.tooLongMessage || "Must not be more than " + maximum + " characters long!";
    	switch(true){
    	  	case (is !== null):
    	  		if( value.length != Number(is) ) Validate.fail(wrongLengthMessage);
    			break;
    	  	case (minimum !== null && maximum !== null):
    	  		Validate.Length(value, {tooShortMessage: tooShortMessage, minimum: minimum});
    	  		Validate.Length(value, {tooLongMessage: tooLongMessage, maximum: maximum});
    	  		break;
    	  	case (minimum !== null):
    	  		if( value.length < Number(minimum) ) Validate.fail(tooShortMessage);
    			break;
    	  	case (maximum !== null):
    	  		if( value.length > Number(maximum) ) Validate.fail(tooLongMessage);
    			break;
    		default:
    			throw new Error("Validate::Length - Length(s) to validate against must be provided!");
    	}
    	return true;
    },
    
    /**
     *	validates that the value falls within a given set of values
     *	
     *	@var value {mixed} - value to be checked
     *	@var paramsObj {Object} - parameters for this particular validation, see below for details
     *
     *	paramsObj properties:
     *							failureMessage {String} - the message to show when the field fails validation
     *													  (DEFAULT: "Must be included in the list!")
     *							within {Array} 			- an array of values that the value should fall in 
     *													  (DEFAULT: [])	
     *							allowNull {Bool} 		- if true, and a null value is passed in, validates as true
     *													  (DEFAULT: false)
     *             partialMatch {Bool} 	- if true, will not only validate against the whole value to check but also if it is a substring of the value 
     *													  (DEFAULT: false)
     *             caseSensitive {Bool} - if false will compare strings case insensitively
     *                          (DEFAULT: true)
     *             negate {Bool} 		- if true, will validate that the value is not within the given set of values
     *													  (DEFAULT: false)			
     */
    Inclusion: function(value, paramsObj){
    	var paramsObj = paramsObj || {};
    	var message = paramsObj.failureMessage || "Must be included in the list!";
      var caseSensitive = (paramsObj.caseSensitive === false) ? false : true;
    	if(paramsObj.allowNull && value == null) return true;
      if(!paramsObj.allowNull && value == null) Validate.fail(message);
    	var within = paramsObj.within || [];
      //if case insensitive, make all strings in the array lowercase, and the value too
      if(!caseSensitive){ 
        var lowerWithin = [];
        for(var j = 0, length = within.length; j < length; ++j){
        	var item = within[j];
          if(typeof item == 'string') item = item.toLowerCase();
          lowerWithin.push(item);
        }
        within = lowerWithin;
        if(typeof value == 'string') value = value.toLowerCase();
      }
    	var found = false;
    	for(var i = 0, length = within.length; i < length; ++i){
    	  if(within[i] == value) found = true;
        if(paramsObj.partialMatch){ 
          if(value.indexOf(within[i]) != -1) found = true;
        }
    	}
    	if( (!paramsObj.negate && !found) || (paramsObj.negate && found) ) Validate.fail(message);
    	return true;
    },
    
    /**
     *	validates that the value does not fall within a given set of values
     *	
     *	@var value {mixed} - value to be checked
     *	@var paramsObj {Object} - parameters for this particular validation, see below for details
     *
     *	paramsObj properties:
     *							failureMessage {String} - the message to show when the field fails validation
     *													  (DEFAULT: "Must not be included in the list!")
     *							within {Array} 			- an array of values that the value should not fall in 
     *													  (DEFAULT: [])
     *							allowNull {Bool} 		- if true, and a null value is passed in, validates as true
     *													  (DEFAULT: false)
     *             partialMatch {Bool} 	- if true, will not only validate against the whole value to check but also if it is a substring of the value 
     *													  (DEFAULT: false)
     *             caseSensitive {Bool} - if false will compare strings case insensitively
     *                          (DEFAULT: true)			
     */
    Exclusion: function(value, paramsObj){
      var paramsObj = paramsObj || {};
    	paramsObj.failureMessage = paramsObj.failureMessage || "Must not be included in the list!";
      paramsObj.negate = true;
    	Validate.Inclusion(value, paramsObj);
      return true;
    },
    
    /**
     *	validates that the value matches that in another field
     *	
     *	@var value {mixed} - value to be checked
     *	@var paramsObj {Object} - parameters for this particular validation, see below for details
     *
     *	paramsObj properties:
     *							failureMessage {String} - the message to show when the field fails validation
     *													  (DEFAULT: "Does not match!")
     *							match {String} 			- id of the field that this one should match						
     */
    Confirmation: function(value, paramsObj){
      	if(!paramsObj.match) throw new Error("Validate::Confirmation - Error validating confirmation: Id of element to match must be provided!");
    	var paramsObj = paramsObj || {};
    	var message = paramsObj.failureMessage || "Does not match!";
    	var match = paramsObj.match.nodeName ? paramsObj.match : document.getElementById(paramsObj.match);
    	if(!match) throw new Error("Validate::Confirmation - There is no reference with name of, or element with id of '" + paramsObj.match + "'!");
    	
    	//aggiunta di mb
    	value2 = match.value;
		if (paramsObj.trimValue)
		{
    		value = trim(value);    
    		value2 = trim(value2);	
		}
		
    	if(value != value2){ 
    	  	Validate.fail(message);
    	}
    	return true;
    },
    
    /**
     *	validates that the value is true (for use primarily in detemining if a checkbox has been checked)
     *	
     *	@var value {mixed} - value to be checked if true or not (usually a boolean from the checked value of a checkbox)
     *	@var paramsObj {Object} - parameters for this particular validation, see below for details
     *
     *	paramsObj properties:
     *							failureMessage {String} - the message to show when the field fails validation 
     *													  (DEFAULT: "Must be accepted!")
     */
    Acceptance: function(value, paramsObj){
      	var paramsObj = paramsObj || {};
    	var message = paramsObj.failureMessage || "Campo obbligatorio!";
    	if(!value){ 
    	  	Validate.fail(message);
    	}
    	return true;
    },
    
	 /**
     *	validates against a custom function that returns true or false (or throws a Validate.Error) when passed the value
     *	
     *	@var value {mixed} - value to be checked
     *	@var paramsObj {Object} - parameters for this particular validation, see below for details
     *
     *	paramsObj properties:
     *							failureMessage {String} - the message to show when the field fails validation
     *													  (DEFAULT: "Not valid!")
     *							against {Function} 			- a function that will take the value and object of arguments and return true or false 
     *													  (DEFAULT: function(){ return true; })
     *							args {Object} 		- an object of named arguments that will be passed to the custom function so are accessible through this object within it 
     *													  (DEFAULT: {})
     */
	Custom: function(value, paramsObj){
		var paramsObj = paramsObj || {};
		var against = paramsObj.against || function(){ return true; };
		var args = paramsObj.args || {};
		var message = paramsObj.failureMessage || "Not valid!";
	    if(!against(value, args)) Validate.fail(message);
	    return true;
	  },
	
    /**
     *	validates whatever it is you pass in, and handles the validation error for you so it gives a nice true or false reply
     *
     *	@var validationFunction {Function} - validation function to be used (ie Validation.validatePresence )
     *	@var value {mixed} - value to be checked if true or not (usually a boolean from the checked value of a checkbox)
     *	@var validationParamsObj {Object} - parameters for doing the validation, if wanted or necessary
     */
    now: function(validationFunction, value, validationParamsObj){
      	if(!validationFunction) throw new Error("Validate::now - Validation function must be provided!");
    	var isValid = true;
        try{    
    		validationFunction(value, validationParamsObj || {},this.element);//MB aggiunto parametro this.element
    	} catch(error) {
    		if(error instanceof Validate.Error){
    			isValid =  false;
    		}else{
    		 	throw error;
    		}
    	}finally{ 
            return isValid 
        }
    },
    
    /**
     * shortcut for failing throwing a validation error
     *
     *	@var errorMessage {String} - message to display
     */
    fail: function(errorMessage){
            throw new Validate.Error(errorMessage);
    },
    
    ok: function(message){
            throw new Validate.NoError(message);
    },

    
    Error: function(errorMessage){
    	this.message = errorMessage;
    	this.name = 'ValidationError';
    },
    NoError: function(message){
    	
    	this.message = message;
    	this.name = 'ValidationOk';
    }
}

//FUNZIONI AGGIUNTE DA FOREACH
function ControllaCF(cf)
{
    var validi, i, s, set1, set2, setpari, setdisp;
    if( cf == '' )  return '';
    cf = cf.toUpperCase();
    if( cf.length != 16 )
        return "La lunghezza del codice fiscale non ?\n"
        +"corretta: il codice fiscale dovrebbe essere lungo\n"
        +"esattamente 16 caratteri.\n";
    validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    for( i = 0; i < 16; i++ ){
        if( validi.indexOf( cf.charAt(i) ) == -1 )
            return "Il codice fiscale contiene un carattere non valido `" +
                cf.charAt(i) +
                "'.\nI caratteri validi sono le lettere e le cifre.\n";
    }
    set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
    setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
    s = 0;
    for( i = 1; i <= 13; i += 2 )
        s += setpari.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
    for( i = 0; i <= 14; i += 2 )
        s += setdisp.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
    if( s%26 != cf.charCodeAt(15)-'A'.charCodeAt(0) )
        return "Il codice fiscale non e' corretto:\n"+
            "il codice di controllo non corrisponde.\n";
    return "";
}
function ControllaPIVA(pi)
{
    if( pi == '' )  return '';
    if( pi.length != 11 )
        return "La lunghezza della partita IVA non e'\n" +
            "corretta: la partita IVA dovrebbe essere lunga\n" +
            "esattamente 11 caratteri.\n";
    validi = "0123456789";
    for( i = 0; i < 11; i++ ){
        if( validi.indexOf( pi.charAt(i) ) == -1 )
            return "La partita IVA contiene un carattere non valido " +
                pi.charAt(i) + "'.\nI caratteri validi sono le cifre.\n";
    }
    s = 0;
    for( i = 0; i <= 9; i += 2 )
        s += pi.charCodeAt(i) - '0'.charCodeAt(0);
    for( i = 1; i <= 9; i += 2 ){
        c = 2*( pi.charCodeAt(i) - '0'.charCodeAt(0) );
        if( c > 9 )  c = c - 9;
        s += c;
    }
    if( ( 10 - s%10 )%10 != pi.charCodeAt(10) - '0'.charCodeAt(0) )
        return "La partita IVA non e' valida:\n" +
            "il codice di controllo non corrisponde.\n";
    return '';
}

function ControllaIBAN(b) 
{ 
        if (b.length < 5) return "La lunghezza è minore di 5 caratteri"; 
        s = b.substring(4) + b.substring(0, 4); 
        for (i = 0, r = 0; i < s.length; i++ ) 
        { 
                c = s.charCodeAt(i); 
                if (48 <= c && c <= 57) 
                { 
                        if (i == s.length-4 || i == s.length-3) return "Posizioni 1 e 2 non possono contenere cifre"; 
                        k = c - 48; 
                } 
                else if (65 <= c && c <= 90) 
                { 
                        if (i == s.length-2 || i == s.length-1) return "Posizioni 3 e 4 non possono contenere lettere"; 
                        k = c - 55; 
                } 
                else return "Sono ammesse solo cifre e lettere maiuscole";
                if (k > 9) 
                        r = (100 * r + k) % 97; 
                else 
                        r = (10 * r + k) % 97; 
        } 
        if (r != 1) return "Il codice di controllo è errato"; 
        return ''; 
} 

/*
function isDate(sDate) 
{
	var dArr = sDate.split("/");
	var d = new Date(parseInt(dArr[2],parseInt(dArr[1])-1,parseInt(dArr[0])));
	//d.setFullYear(parseInt(dArr[2]));
	//d.setMonth(parseInt(dArr[1])-1,parseInt(dArr[0]));

	//alert(parseInt(d.getMonth()) == (parseInt(dArr[1]) - 1));
	return (parseInt(d.getMonth()) == (parseInt(dArr[1]) - 1) ) && (d.getDate() == parseInt(dArr[0])) && (d.getFullYear() == parseInt(dArr[2]));
}*/

function trim(stringToTrim) 
{
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function ControllaLink(link)
{
	LeftString1 = link.slice(0,7);
	LeftString2 = link.slice(0,8);
	if( (LeftString1.toLowerCase() != 'http://') && (LeftString2.toLowerCase() != 'https://') )
	  return false;
	else
	  return true; 
}

function ControllaDataScad(data)
{
	vaPezzi = data.split("/") ;
	vaDataTest = new Date();
	vaDataTest.setDate(vaPezzi[0]);
	vaDataTest.setMonth(vaPezzi[1]-1);
	vaDataTest.setFullYear(vaPezzi[2]);
	vaDataOggi = new Date();
	
	if (vaDataTest.getTime() > vaDataOggi.getTime())
		return true;
	else
		return false;
}

function ControllaDataFase1(data)
{
	
	vsDataScadRds = document.getElementById("data:dt_ult_risp").value;
	
	vaPezziScad1 = data.split("/") ;
	vaDataScad1 = new Date();
	vaDataScad1.setFullYear(vaPezziScad1[2],vaPezziScad1[1]-1,vaPezziScad1[0]);
	
	vaPezziScadRds = vsDataScadRds.split("/") ;
	vaDataScadRds = new Date();
	vaDataScadRds.setFullYear(vaPezziScadRds[2],vaPezziScadRds[1]-1,vaPezziScadRds[0]);

	
	if (vaDataScad1.getTime() > vaDataScadRds.getTime())
		return true;
	else
		return false;
}

function ControllaDataFase2(data)
{
	
	vsDataFase2 = document.getElementById("data:fase1_dt_ult").value;
	
	vaPezziScad2 = data.split("/") ;
	vaDataScad2 = new Date();
	vaDataScad2.setFullYear(vaPezziScad2[2],vaPezziScad2[1]-1,vaPezziScad2[0]);
	
	vaPezziFase1 = vsDataFase2.split("/") ;
	vaDataFase1 = new Date();
	vaDataFase1.setFullYear(vaPezziFase1[2],vaPezziFase1[1]-1,vaPezziFase1[0]);

	
	if (vaDataScad2.getTime() > vaDataFase1.getTime())
		return true;
	else
		return false;
}


function ControllaDataFase3(data)
{
	
	vsDataFase3 = document.getElementById("data:fase2_dt_ult").value;
	
	vaPezziScad3 = data.split("/") ;
	vaDataScad3 = new Date();
	vaDataScad3.setFullYear(vaPezziScad3[2],vaPezziScad3[1]-1,vaPezziScad3[0]);
	
	vaPezziFase2 = vsDataFase3.split("/") ;
	vaDataFase2 = new Date();
	vaDataFase2.setFullYear(vaPezziFase2[2],vaPezziFase2[1]-1,vaPezziFase2[0]);

	
	if (vaDataScad3.getTime() > vaDataFase2.getTime())
		return true;
	else
		return false;
}


/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		//alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		//alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//alert("Please enter a valid date")
		return false
	}
return true
}
