

   /*
   ** Constructor - Sets up the dialog box and ajax parms; 
   ** Takes as input: 
   */
   function LnwrEmailDlg(parms) 
   {
      this.dlgSelect =       null;   // Selector for the dialog box itself
      this.dlgTitle  =       null;   // Title displayed in dialog box
      this.emailAdrSelect =  null;   // Selector for the textbox that contains the user entered email address
      this.emailNameSelect = null;   // Selector for the textbox that contains the user entered email address
      this.emailToAdr =      null;   // Email recipient
      this.statMsgSelect =   null;   // Selector for the message field.
      this.emailSubSelect =  null;   // Selector for the subject field
      this.emailMsgSelect =  null;   // Selector for the message field
//    this.phpUrl =         "/php/send_email.php";    // Url to process the AJAX request.
      this.phpUrl =         "/php/send_email.php?XDEBUG_SESSION_START=netbeans-xdebug";    // URL FOR DEBUGGING WITH NETBEANS
      this.clickSelect =    null;   // Selector to click to get the dialog box.
      this.sendEmail =     false;  // When true, the email dialog includes user entered subject and message.
      this.sendingMsgWithThis = null;
      this.sendingMsgText = null;
      
      if(lnwrUtil.isValid(parms.sendEmail) === false)
      {
         return;   // THIS IS A PROGRAMMING ERROR - CALLER MUST PROVIDE DIALOG TYPE
      }

      this.dlgSelect = parms.dlgSelect; 
      this.dlgTitle = parms.dlgTitle;
      this.emailAdrSelect = parms.emailAdrSelect;
      this.emailNameSelect = parms.emailNameSelect;
      this.statMsgSelect = parms.statMsgSelect;
      this.clickSelect = parms.clickSelect;
      this.emailToAdr = parms.emailToAdr;
      if(lnwrUtil.isValid(parms.phpUrl)) 
         this.phpUrl = parms.phpUrl; 
      if(parms.sendEmail === true)
      {
         this.sendEmail = true;  // if here, we are sending an email message, not registering
         this.emailSubSelect = parms.emailSubSelect;
         this.emailMsgSelect = parms.emailMsgSelect;
      }
      this.retry = false;

      //  Set the "this" pointers for the callbacks
      var okWithThis = $.proxy(this.okHandler, this);
      var closeWithThis = $.proxy(this.clear, this);
      var cancelWithThis = $.proxy(this.clearAndClose, this);
      var openWithThis = $.proxy(this.open, this);

      // Get the dialog box options set up.
      var buttons =  
      {
         OK: okWithThis,
         Cancel: cancelWithThis
      };   
      // set up the options for the dialog, 
      var options = 
      { 
         title: this.dlgTitle,
         close: closeWithThis,
         buttons: buttons,          
         autoOpen: false,
         modal: true,
         hide: "scale",
         minWidth: this.minWidth
      };
      if(lnwrUtil.isValid(parms.minWidth))  // If requested by caller, set a minimum dialog width
         options.minWidth = parms.minWidth; 

      $(this.dlgSelect).dialog(options);    

      // Make the cursor look like a link for the clickable text
      lnwrUtil.cursorLikeLink(this.clickSelect);  

      // Register the click event handler to show our dialog
      $(this.clickSelect).click(openWithThis);

      // The following disables form submission on presssing enter key - without this the
      // page will refresh itself and our form will be lost.
      $(this.dlgSelect).submit(function(e)
      {
         return false;
      }
      );

   }

   /* 
   ** errorHandler - Called by jquery when an AJAX error occurs 
   */
   LnwrEmailDlg.prototype.errorHandler = function (jqXHR, textStatus, errorThrown)
   {
      $(this.statMsgSelect).stopTime("sendingMsg");
      if(this.sendEmail === true)
         $(this.statMsgSelect).css( { color: "red" }).text("Error occurred: email was not sent");
      else
         $(this.statMsgSelect).css( { color: "red" }).text("Error occurred: email address was not registered");
      lnwrUtil.cursorWait(false);
   };

   /* 
   ** successHandler - Called by jquery when an AJAX request completes successfully 
   */
   LnwrEmailDlg.prototype.successHandler = function (data, textStatus, jqXHR)
   {
      var cancelWithThis = null;
      var successMsg;
      var errorMsg;
      
      $(this.statMsgSelect).stopTime("sendingMsg");
      if(this.sendEmail === true)
      {
         successMsg = "Email was sent";
         errorMsg = "Error occurred: email was not sent, click OK to try again";
      }
      else
      {
         successMsg = "Email address successfully registered";
         errorMsg = "Error occurred: email address was not registered, click OK to try again";
      }

      if(data.status == "success")
      {
         $(this.statMsgSelect).css( { color: "black" } ).text(successMsg);
         cancelWithThis = $.proxy(this.clearAndClose, this);
         lnwrUtil.cursorWait(false);
         $(this.dlgSelect).oneTime(2000, cancelWithThis);
      }
      else
      {
         lnwrUtil.cursorWait(false);
         $(this.statMsgSelect).css( { color: "red" }).text(errorMsg);
      }
   };

   /* 
   ** okHandler - Called by jquery when the dialog box OK button is clicked
   */
   LnwrEmailDlg.prototype.okHandler = function()
   {
      $(this.statMsgSelect).text("");
      var ajaxOptions =  
      {
         type: "POST",
         url: this.phpUrl,
         dataType: "json",
         timeout: 5000,
         context: this,
         success: this.successHandler,
         error: this.errorHandler
      };   
      var dlgEmailAdr = $(this.emailAdrSelect).val();
      var dlgEmailName = $(this.emailNameSelect).val();
      var valid = lnwrUtil.valEmailAdr(dlgEmailAdr);
      if(valid === true)
      {
         var input = {};
         input["send_email"] = false;  // Init to register email address
         input["email_req_adr"] = dlgEmailAdr;  // user's email address
         input["email_to_adr"] = this.emailToAdr;  // email address we're sending to
         ajaxOptions.data = input;
         if(this.sendEmail === true)
         {
            var emailSubject = $(this.emailSubSelect).val();  // if here, we are sending an email, not registering
            if(emailSubject !== "")   // ensure there is a subject
            {
               var emailMsg = $(this.emailMsgSelect).val();
               if(emailMsg !== "")    // ensure there is a message
               {
                  input["send_email"] = true;
                  input["email_subject"] = emailSubject;
                  input["email_msg"] = emailMsg;
                  lnwrUtil.cursorWait(true);    
                  this.sendAjaxReq(ajaxOptions);
               }
               else
                  $(this.statMsgSelect).css( { color: "red" } ).text("Please enter a message");
            }
            else
               $(this.statMsgSelect).css( { color: "red" } ).text("Please provide a subject for you email message");
         }
         else
         {
            input["email_req_name"] = dlgEmailName;  // user's name
            lnwrUtil.cursorWait(true);
            this.sendAjaxReq(ajaxOptions);
         }
      }
      else
         $(this.statMsgSelect).css( { color: "red" } ).text("Email address is not valid, please re-enter");
   };

   /*
   ** sendAjaxReq - Does an ajax request to the server to send the email
   */
   LnwrEmailDlg.prototype.sendAjaxReq = function(ajaxOpts)
   {
      $.ajax(ajaxOpts);   
      this.sendingMsgText = "please wait .";
      $(this.statMsgSelect).css( { color: "black" } ).text(this.sendingMsgText);
      this.sendingMsgWithThis = $.proxy(this.sendingMsg, this);
      $(this.statMsgSelect).everyTime(500, "sendingMsg", this.sendingMsgWithThis);
   };

   /*
   ** open - Opens the dialog box;
   */
   LnwrEmailDlg.prototype.open = function()
   {
      $(this.dlgSelect).dialog("open");   
   };
   
   /*
   ** sendingMsg
   */
   LnwrEmailDlg.prototype.sendingMsg = function()
   {
      this.sendingMsgText += ".";
      $(this.statMsgSelect).text(this.sendingMsgText);
   };
   
   /*    
   ** clear - Clears the fields in the dialog
   */
   LnwrEmailDlg.prototype.clear = function()
   {
      $(this.emailAdrSelect).val("");  // .text call will work but .val call is generic call for form input items.
      $(this.statMsgSelect).text("");
      if(this.sendEmail === true)
      {
         $(this.emailSubSelect).val("");
         $(this.emailMsgSelect).val("");
      }
   };

   /*
   **  clearAndClose Clears the fields and closes the dialog
   */
   LnwrEmailDlg.prototype.clearAndClose = function()
   {
      this.clear();
      $(this.dlgSelect).dialog("close");   
   };
   
   


