// HIDE OR SHOW STATE
  function toggleState() {
    var state = $('state');
    var stateLabel = $(state.name+"_label");
    if ($('country').value.toLowerCase() == "us") {
      state.up().show();
      stateLabel.up().up().show();
    }
    else if (state.visible()) {
      state.up().hide();
      stateLabel.up().up().hide();
    }
  }

// LOAD PAGE
  document.observe("dom:loaded", function() {

  // GET FORM
    var currentForm = $(CONTROLLERSETTINGS.formName);

  // ON SUBMIT
    currentForm.observe('submit', formsubmit);
    $('submitButton').observe('click', formsubmit.bindAsEventListener(currentForm));

  // HIDE OR SHOW STATE
    var countrySelectNode = $('country');
    if (CONTROLLERSETTINGS.countrySelect && countrySelectNode) {
      countrySelectNode.observe('change', function(e){
        toggleState();
      });
      toggleState();
    }

  // FORM ELEMENTS LOOP
    currentForm.getElements().each(function(el) {

    // FOCUS
      Event.observe(el, 'focus', function(event) {
        el.up().addClassName('focus');
      });

    // BLUR
      Event.observe(el, 'blur', function(event) {
        el.up().removeClassName('focus');
      });

    // CURRENCY
      if (CONTROLLERSETTINGS.currencyRadio) {
        if (el.type == 'radio' && el.name == 'currency') {
          if (el.checked)  {
            el.up().addClassName('selected');
          }
        // CHANGE STYLE
          el.observe('click', function(event){
            $$("[name='currency']").each(function(element) {
              element.up().removeClassName('selected');
            });
            this.up().addClassName('selected');
          });
        }
      }
    });

  // FOCUS ON FIRST ELEMENT
    if (CONTROLLERSETTINGS.firstElement) {
      $(CONTROLLERSETTINGS.firstElement).focus();
      $(CONTROLLERSETTINGS.firstElement).up().addClassName('focus');
    }
  });

// ON FUNCTION SUBMIT
  function formsubmit(e) {
  // STOP ACTION
    Event.stop(e);
    var thisform = this;

  // PRACTICE OR LIVE ACCOUNT
    var requestParams = {};
    if (CONTROLLERSETTINGS.requestParams) {
      requestParams = CONTROLLERSETTINGS.requestParams;
    }

  // SEND XHR TO FORM ACTION
    thisform.request({
    // REQUEST FOR JSON RESPONSE
      parameters: requestParams,

    // DISABLE FORM
      onCreate: function() {
        thisform.disable();
      },

    // RESPONSE data
      onComplete: function(transport) {
        var data = transport.responseJSON;
      // REMOVE ERRORS
        thisform.getElements().each(function(el) {
          var ell = $(el.name+"_label");
          if (ell) {
            ell.up().removeClassName('error');
          }
          el.up().removeClassName('error');
        });

      // CLEAR ERROR MESSAGES AND HIDE LAYER
        var emx = $('errorMsgs');
        if (emx) {
          emx.update("");
        }
        emx = $('errorPane');
        if (emx) {
          emx.hide();
        }

      // ENABLE FORM
        thisform.enable();

      // success = false JSON RESPONSE
        if (data.success === false) {
        // SHOW FORM FIELDS ERRORS
          var errorMessage = "";
          data.errors.each(function(el) {
            var ell = $(el.name+"_label");
            if (ell) {
              ell.up().addClassName('error');
            }
            var input = $(el.name);
            if (input) {
              input.up().addClassName('error');
            }
            errorMessage += el.value + "<br />";
          });

        // SHOW ERROR MESSAGES
          $('errorMsgs').update(errorMessage);
          $('errorPane').show();
        }
      // success = true JSON RESPONSE
        else if (data.success === true) {
        // LOCATION REDIRECT
          if (CONTROLLERSETTINGS.onSuccess.allowScript && data.script) {
            eval(data.script);
          }
          if (CONTROLLERSETTINGS.onSuccess.redirect && data.redirectUrl) {
            if (CONTROLLERSETTINGS.onSuccess.target) {
              if (CONTROLLERSETTINGS.onSuccess.target == '_top') {
                top.location = data.redirectUrl;
              }
              else if (CONTROLLERSETTINGS.onSuccess.target == '_blank') {
                window.open(data.redirectUrl);
              }
            }
            else {
              window.location = data.redirectUrl;
            }
          }
          else if (CONTROLLERSETTINGS.onSuccess.post && data.postUrl) {
            thisform.action = data.postUrl;
            thisform.submit();
          }
          else {
          // RESET FORM
            thisform.reset();

          // HIDE LAYERS
            CONTROLLERSETTINGS.onSuccess.hide.each(function(name) {
              var el = $(name);
              if (el) {
                el.hide();
              }
            });

          // SHOW LAYERS
            CONTROLLERSETTINGS.onSuccess.show.each(function(name) {
              var el = $(name);
              if (el) {
                el.show();
              }
            });
          }
        }
      },

    // EXCEPTION
      onException: function(requester , e) {
      // ENABLE FORM
        thisform.enable();
      },

    // ERROR
      onFailure: function(e) {
      // ENABLE FORM
        thisform.enable();
      }
    });
  }