var HOWF = window.HOWF || {};

/**********************************************************
* Singleton: HOWF.Page
**********************************************************/

HOWF.Page = (function() {

  return {

    /**
     * Capture 'enter' in the header login form, and submit.
     */
    headerSubmit : function(field,e) {

      var code;

      // select code based on browser model
      if (window.event) {

        code = window.event.keyCode;

      } else if (e) {

        code = e.which;

      } else {

        return true;

      }

      if (code == 13) {

        return this.processHeaderLogin();

      } else {

        return true;

      }

    },

    processHeaderLogin : function() {

      var form_validator = new HOWF.FormValidator(HOWF.headerLogin);
      var originalValue = "";

      // bypass the default value
      if (document.getElementById("header_username").value == "User Name") {
        originalValue = "User Name";
        document.getElementById("header_username").value = "";
      }

      if (HOWF.disableClientChecks || form_validator.validate_form()) {

        HOWF.headerLogin.action.value = "do_login";

        // bypass the default values
        if (document.getElementById("header_username").value != "User Name") {
          HOWF.headerLogin.header_username.value = document.getElementById("header_username").value;
        }

        if (document.getElementById("header_password").value != "Password") {
          HOWF.headerLogin.header_password.value = document.getElementById("header_password").value;
        }

        HOWF.headerLogin.login_redirect_page.value = HOWF.currentScript;

        var submission_form = new HOWF.SubmissionForm(HOWF.headerLogin,HOWF.authScript);

        // attach the form to the page
        submission_form.attach("howf_body");

        // build it
        submission_form.build();

        // submit it
        submission_form.submit();

      }

      if (originalValue != "") {
        document.getElementById("header_username").value = originalValue;
      }

      return false;

    },

    processHeaderLogout : function() {

      if (confirm("Are you sure you want to log out?")) {

        HOWF.headerLogout.logout_redirect.value = HOWF.currentScript;

        var submission_form = new HOWF.SubmissionForm(HOWF.headerLogout,HOWF.authScript);

        // attach the form to the page
        submission_form.attach("howf_body");

        // build it
        submission_form.build();

        // submit it
        submission_form.submit();

      }

      return false;

    },

    processForm : function(form,form_id) {

      var form_validator = new HOWF.FormValidator(form);

      if (HOWF.disableClientChecks || form_validator.validate_form()) {

        if (form_id) {
          document.getElementById(form_id).submit();
        }

        return true;

      } else {

        return false;

      }

    }

  };

})();
