// basecmp extension for serverside validation with struts 2.1 and prototype

// convenience-function-shortcuts to validateWithPrototype
  function validateAndExecute(form, onValidHook, onInvalidHook) {
    validateWithPrototype(form, false, onValidHook, onInvalidHook);
  }
  function validateOnly(form, onValidHook, onInvalidHook) {
    validateWithPrototype(form, true, onValidHook, onInvalidHook);
  }

  // Ajax-Validierung via prototype 'on submit'.
  function validateWithPrototype(form, validateOnly, onValidHook, onInvalidHook) {

     var url;
     var parameters;
     if(Object.isHash(form)) {

       url = form.get("action");
       if(!url) {
         alert("key 'action' in hash undefined!");
         return;
       }
       form.unset("action");
       parameters = Object.toQueryString(form);

     } else {
       url = form.action;
       parameters = Form.serialize(form);
     }
     parameters += '&struts.enableJSONValidation=true&struts.validateOnly=' + validateOnly;

     new Ajax.Request(
        url,
        {
            method: 'post',
            encoding: 'UTF-8',
            parameters: parameters,
            evalJSON: false,
            onSuccess: function(response) { postValidation(response, form, onValidHook, onInvalidHook); },
            onException: handleException,
            on400: function() { alert("HTTP 400 Bad Request\n(formname=" + form.name + ")"); },
            on401: function() { alert("HTTP 401 Unauthorized\n(formname=" + form.name + ")"); },
            on403: function() { alert("HTTP 403 Forbidden\n(formname=" + form.name + ")"); },
            on404: function() { alert("HTTP 404 Not found\n(url=" + url + "\nformname=" + form.name + ")"); },
            on500: function() { alert("HTTP 500 Serverside Error has occured!"); }
        }
     );

  }

  function handleException(response, e) {
    alert("JavaScript-Exception\n\t" + e);
  }

  function postValidation(response, form, onValidHook, onInvalidHook) {

     var text = response.responseText;

     // Note: Struts2-JSON-Validator sends JSON inside JS-Comments!
     //       That's why prototype cant do the job during Ajax-Request and evalJSON is set false!
     // so we test if request is a 'Struts-own-JSON-Response'
     //if(!text.match(/\/\*/)) {
     //  alert("The AJAX-Response seems invalid! Expected JSON-Content, got this:\n\t" + text);
     //}

     // get errors from response
     var errorsObject = StrutsUtils.getValidationErrors(text);
     // if original data was a real form clear previous validation errors, if any
     if(!Object.isHash(form))
       StrutsUtils.clearValidationErrors(form);
     if(typeof clearGlobalError == 'function') {
         // execute only if 'clearGlobalError' is defined anyway
       clearGlobalError(form);
     }

     // show errors, if any
     if(errorsObject && errorsObject.fieldErrors) {

       // if original data was a real form show validation errors
       if(!Object.isHash(form))
         StrutsUtils.showValidationErrors(form, errorsObject);
       if(onInvalidHook && typeof onInvalidHook == 'function')
         onInvalidHook(response, form);
       else if(typeof onInvalid == 'function')
         onInvalid(response, form);

     } else if(errorsObject && errorsObject.errors) {

       if(typeof onGlobalError == 'function') {
         // execute only if 'onGlobalError' is defined anyway
         onGlobalError(form, errorsObject);
       }

     } else {

       if(onValidHook && typeof onValidHook == 'function')
         onValidHook(response, form);
       else if(typeof onValid == 'function')
         onValid(response, form);

     }

  }
