// For each form(element) a request will be done to the 'wmpformclientsideframework'-servlet.
// It will add the corresponding formState object to the WebmanagerFormStateRegistry.
var WebmanagerFormStateRegistry = {};

$(document).ready(function(event) {
    // initial hide
    $('.hide').each(function() {
        $(this).hide();
    });

     // formState = new FormState();
    $('.wmpform').find(':input').change(function() {
        var formId = $(this).parents('form:first').attr("id");

        if (formId != 'undefined' && formId != '') {
            var formState = WebmanagerFormStateRegistry[formId];

            if (typeof formState != 'undefined' && formState != '' ) {

                attr = $(this).attr("name");
                if (typeof attr != 'undefined' && attr != '') {
                    value = $(this).val();
                    
                    // for checkboxes we pass the array of values
                    if ($(this).attr("type") == 'checkbox') {                      
                      values = $('input:checkbox[name=' + $(this).attr("name") + ']:checked') || [];
                      value = new Array();
                      
                      i=0;
                      values.each(function() {
                        value[i] = $(this).val();
                        i++;
                      });
                    }

                    changes = formState.setFragmentValue($(this).attr("name"),value);

                    for (var i = 0; i < changes.length; i++) {
                        var change = changes[i];
                        identifier = change.identifier.replace('.','_');
                                               
                        if (change.value == 'show') {
                          $('#' + identifier).show();
                        } else {
                          $('#' + identifier).hide();
                        }
                    }
                }
            }
        }
    });

    $(".wmpform").submit(function(event) {
        hasError = false;
        var formId = $(this).attr("id");
        
        $('#clientsideRouting').val("false");

        if (formId != 'undefined' && formId != '') {
            var formState = WebmanagerFormStateRegistry[formId];

            if (typeof formState != 'undefined' && formState != '' ) {
                $(':text').each(function() {

                  inputName = $(this).attr("name");
                  obj = $('#error_' + inputName.replace('.','_'));
                  if (obj.length > 0 && isVisible(obj)) {

                    fr_error = "";
                    arr = formState.validateAndReturnMessage(inputName,$(this).val());
                    for(var item in arr) {
                        hasError = true;
                        var value = arr[item];
                        fr_error += '<li>' + value + '</li>';
                    }
                    if (fr_error != '') {
                      fr_error = '<ul>' + fr_error + '</ul>';
                    }

                    obj.html(fr_error);
                    obj.show();
                  }
                });

                if (hasError) {
                    event.preventDefault();
                }
            }
        }
    });

    function isVisible(obj) {
      visible = true;
      obj.parents().each(function() {
        if ($(this).css("display") == 'none') {
          visible = false;
          return false;      
        }
      });
      return visible;
    }

    // List entries for debugging in FX.
    // for (entry in WebmanagerFormStateRegistry) {
    //     window.console.log(entry + ': %o', WebmanagerFormStateRegistry[entry]);
    // }
});