function enableSubmit() {
  $('submit').disabled = false; //block it from clicking again
}
window.addEvent("domready", function() {

    var timerId;
    // Variable that holds the effects.
    var fx = {
    	"loading": new Fx.Tween( "loading", {property: 'opacity', duration: 200 }),
    	"success": new Fx.Tween( "success", {property: 'opacity', duration: 200 }),
    	"fail": new Fx.Tween( "fail", {property: 'opacity', duration: 200 } )
    };

    // Hides the loading div, and shows the el div for
    // a period of five seconds.
    var showHide = function( el ){
      fx.loading.set(0);
    	(fx[ el ]).start(0,1);
    	(function(){ (fx[ el ]).start(1,0); }).delay( 5000 );
    };

    // Listen for click events on the submit button.
    $$('#form-prev input[type=submit]').each(function(submitElement) {
      submitElement.addEvent( "click", function(evt){

    	// Stops the submission of the form.
    	new Event(evt).stop();
    	if (evt.target) targ = evt.target
      else if (evt.srcElement) targ = evt.srcElement
    
      formId = targ.form.id;

    	// Sends the form to the action path, which is "script.php".
    	//url: $(formId).action,
    	var myform = $(formId);
      var url = myform.getProperty('action');
      new Request({ 
        method: 'post',
        url: myform.getProperty('action'),
        data:  myform.toQueryString(),
    		onRequest: function(){
    			// Show loading div.
    			//fx.loading.start( 1,0 );
    			showHide( "loading" );
    			$('submit').disabled = true; //block it from clicking again
    			timerId = setTimeout('enableSubmit()', 5000);
    			//alert('request');
    		},
    		onSuccess: function(result){
    		  // Hide loading and show success for 3 seconds.
          
          resultText = result;
    		  var result = eval('(' + result + ')');
          if (result.testing == 1) //in testing mode show ajax return value
    		    alert(resultText);    		    		  
    		  
    			/*result = JSON.parse(result, function (key, value) {
    			    //src=http://www.json.org/js.html
              var type;
              if (value && typeof value === 'object') {
                  type = value.type;
                  if (typeof type === 'string' && typeof window[type] === 'function') {
                      return new (window[type])(value);
                  }
              }
              return value;
          });*/
    			
    			//reset old errors
    			/*$$('input[type="checkbox"]').each(function(checkbox) {
        		if (checkbox.id == 'data') 
                checkbox.setProperty('checked', true);
        		else 
                checkbox.setProperty('checked', false);
        	});*/
    		  

          $$('#'+formId+' input,textarea,button,select').each(function(el) {
            //if (el.type == 'checkbox')
              if ($(el.id+'_lb') != undefined) {
                $(el.id+'_lb').style.backgroundColor = "";
              } 
              else if ((el.type=='radio') && ($(el.name+'_lb') != undefined)) 
                $(el.name+'_lb').setStyle('backgroundColor','');
              else
                el.setStyle('backgroundColor', '');
          });
               		  
          //alert(result.message);
    			if (result.success == 0) {
            $('fail').innerHTML = result.message;  //result.message
            $('submit').disabled = false; //block it from clicking again
            showHide( "fail" );
            $('loading').setStyle('visibility', 'hidden');
                              			
            //colorFields
            for (fld in result.fields) {
                //alert(result.fields[fld]);
                //alert (result.fields[fld]+'_lb'+ '   '+$(result.fields[fld]+'_lb'));
                if ($(result.fields[fld]) != undefined) {
                  if ($(result.fields[fld]).type == 'checkbox') {
                    if ($(result.fields[fld]+'_lb') != undefined)
                      $(result.fields[fld]+'_lb').style.backgroundColor="#CC6633"; 
                    else
                      $(result.fields[fld]).style.backgroundColor="#CC6633";
                  } else {
                    if ($(result.fields[fld]+'_lb') != undefined)
                      //alert('here: '+$(result.fields[fld]).name);
                      $(result.fields[fld]+'_lb').style.backgroundColor="#CC6633";
                    else {
                      //alert (fld+'_lb'+ '     '+result.fields[fld]);
                      $(result.fields[fld]).style.backgroundColor="#CC6633";
                    }
                  }
                } else {  //it's gotta be radio
                  if ($(result.fields[fld]+'_lb') != undefined)
                    $(result.fields[fld]+'_lb').style.backgroundColor="#CC6633";
                  else
                    $(result.fields[fld]).style.backgroundColor="#CC6633";
                }
            }
            
    			} else {
    			   showHide( "success" );
             clearTimeout(timerId);
             $('loading').setStyle('visibility', 'hidden');
    			   $('success').innerHTML = result.message;  //result.message
    			   if (result.testing == 0) //don't disable in testing mode
                $('submit').disabled = true; //block it from sending again the form                 			   
          }
          
          if ((result.testing == 1) && (result.success == 1)) {
              if ($('dumpTestResults') == null) {
                $elem = new Element('div', {id: 'dumpTestResults'});
                $elem.inject($(formId),'bottom');
              }
              $elem.innerHTML = result.testMessage;
          }
    			
    		},
    		onFailure: function(instance){
    			// Hide loading and show fail for 3 seconds.
    			if (instance.responseText != null)
    			   alert(instance.responseText);
    			$('fail').innerHTML = 'Per favore inviate di nuovo il modulo, premendo su <b>Invia</b>';
    			showHide( "fail" );
    		}
    	}).send();
    } );
    });
    
});