var EmailPopupDialog = function(config) {
  return ( function(c) {
      var obj = {
        config: c,
        loggedIn: c().getLoggedIn(),
        loadDialogOnDocumentReady: c().getAutoLoad(),
        contactDialogInitialized: false,

        // "Cancel": function() { config().getDialogElement().dialog("close"); }, 
        // "Send!":  function() { console.dir(this); this.submitForm(); } 

        btnSet_Disabled:  { "Please Wait..." : function() {} },

        initialize: function(){
            this.contactDialogInitialized = true;
            if(this.config().loadDialogOnDocumentReady) { this.show(); }
        }, // initialize

        initRecaptcha: function() {
          try {
          if(!this.config().getLoggedIn()) {
            Recaptcha.create(this.config().getRecaptchaPubKey(), this.config().getRecaptchaElementId(), { theme: "white" });
          } // if
          } catch(e) { console.dir(e); }
        }, // initRecaptcha

        show: function( onDialogClose ) {
          try {
            if(!this.contactDialogInitialized) {
              console.dir('Not initialized!'); 
              return false;
            } // if 
            this.initRecaptcha();
            this.config().getDialogElement().dialog({ 
              autoOpen: false, 
              modal: true,
              resizable: false,
              position: ['top', 'center'],
              width: 650,
              zIndex: 11000,
              close: function () { 
                config().getDialogElement().dialog('destroy'); 
                if(typeof(onDialogClose) != "undefined") {
                  onDialogClose(); 
                }
              },
              title: this.config().getDialogTitle(),
              buttons: this.btnSet_Enabled 
            }); // dialog
            this.config().getDialogElement().dialog('option', 'buttons', this.btnSet_Enabled);
            this.config().getDialogElement().dialog('enable');
            this.config().getDialogElement().dialog().dialog('open');
            this.config().getRecaptchaOuterElement().show();
          } catch(e) { console.dir(e); }
        }, // show

        submitForm: function()  {
          this.config().getFormElement().onsubmit();
          this.config().getDialogElement().dialog('disable');
          var errorElement = this.config().getErrorElement()
          if(errorElement.visible()) { errorElement.hide(); }
          this.config().getDialogElement().dialog('option', 'buttons', this.btnSet_Disabled);
        }, //submitForm

        cb_validContactMessage: function() {
          this.config().getDialogElement().dialog('option', 'buttons', this.btnSet_Enabled);
          this.config().getDialogElement().dialog('enable');
          var errorElement = this.config().getErrorElement()
          if(errorElement.visible()) { errorElement.hide(); }
          this.config().getDialogElement().dialog('close');
        },

        cb_invalidContactMessage: function(errors) {
          try {
            var errorListElement = this.config().getErrorListElement()
            errorListElement.innerHTML = '';
            for(var i = 0; i < errors.length; i++) {
              var li = document.createElement("li");
              li.innerHTML = errors[i];
              errorListElement.appendChild(li);
            }
            this.config().getErrorElement().show();
            this.initRecaptcha();
          } catch(e) {
            console.dir(e);
          }

          this.config().getDialogElement().dialog('option', 'buttons', this.btnSet_Enabled);
          this.config().getDialogElement().dialog('enable');
        },  // cb_invalidContactMessage

      };

      obj.btnSet_Enabled = {
        "Cancel": function() { config().getDialogElement().dialog("close"); }, 
        "Send!":  function() { obj.submitForm(); } 
      };

      return(obj);
    }(config) ) // return
}; // EmailPopupDialog

