/**
* Function to validate forms using AJAX requests
* Wouter van Koppen, wouter.vankoppen@marsmedia.nl
* 12 sept 07
*/
function verifyForm ($oForm, $sAjaxWrapperURL, $bPrefixFieldInErrorMessages, $bDebug, $sErrorClassName)
{
  // Set default params
  if ($sErrorClassName == null) {
    $sErrorClassName = 'error';
  }

  if ($bDebug == null) {
    $bDebug = false;
  }

  if ($bPrefixFieldInErrorMessages == null){
    $bPrefixFieldInErrorMessages = false;
  }
  
  // Part to make sure widgeditor content will be submitted
   if ($oForm.onsubmit)
      $oForm.onsubmit();
  window.iserror = 0;
  $sCheck = '';
  //Piet 18-1-2008 1:16:41
  // before serializing, make sure all disabled inputs are enabled again
  $oForm.getInputs().invoke('enable');
  $sCheck = $oForm.serialize(true);
  var $bError = false;
  var $sDebugging = '';

  if ($bDebug){
    alert ('Ready to make a request to "' + $sAjaxWrapperURL + '" with the following data: "' + $sCheck + '"');
  }

  new Ajax.Request($sAjaxWrapperURL,
  {
    method:'post',
    parameters: $sCheck,
    onSuccess: function(transport){
      var $sResponse = transport.responseText || "No response text";
      if ($bDebug){
        alert("Success! Received response:\n\n" + $sResponse);
      }
      $aResponse = $sResponse.evalJSON();
      $aResponseHash = $H($aResponse);

      $aResponseHash.each(function($aElement){
          if ($aElement.value != '')
          {
            // There is an error in the response for this field.
            window.iserror = 1;
            
            if ($($aElement.key)){
              $($aElement.key).addClassName($sErrorClassName);
            }else
            {
              $sDebugging = $sDebugging + 'ERR - Input holder (div) with id "' + $aElement.key + '" is not found!' + "\n";
            }
            // Check for field existence in the document. If we don't do this, the script will hang.
            if ($('error_message_' + $aElement.key))  
            {
              
              // Do we want a prefix that can identify the field in front of the error messages?
              if ($bPrefixFieldInErrorMessages)
              {
                $('error_message_' + $aElement.key).innerHTML =  $('description_' + $aElement.key).innerHTML + ': ' + $aElement.value;
              }
              else
              {
                $('error_message_' + $aElement.key).innerHTML =  $aElement.value;
              }
            }
          
          }
          else
          {
            // This field has no error!
            
            // Check for field existence in the document. If we don't do this, the script will hang.
            if ($($aElement.key))
            {
              $($aElement.key).removeClassName($sErrorClassName);
            }
            
            // Check for field existence in the document. If we don't do this, the script will hang.
            if ($('error_message_' + $aElement.key))
            {
              $('error_message_' + $aElement.key).innerHTML = '';
            }
           
          }

        
      });
      if ($bDebug)
      {
          alert ('Done.. ' + "\n" + $sDebugging);
      }
      if (window.iserror != 1)
      {  
        $oForm.submit();    
      	return true;     
      }
      // set a general error message if found
      else{
        if ($('error_message_general')){
          $('error_message_general').innerHTML =  'Niet alle gegevens zijn correct.';
        }     
      }
      
      return false;
  

    },
    onFailure: function(){ alert('Something went wrong during the form validation process!'); }
  }

  );
  
}
