// The contents of this file are subject to the Mozilla Public License Version 1.1
// (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
// WARRANTY OF ANY KIND, either express or implied. See the License for the specific
// language governing rights and limitations under the License.
// 
// The Original Code is the Venice Web Communities System.
// 
// The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
// for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
// Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
// 
// Contributor(s): 

importPackage(Packages.com.silverwrist.venice.core);
importPackage(Packages.com.silverwrist.venice.except);
importPackage(Packages.com.silverwrist.venice.ui);
importPackage(Packages.com.silverwrist.venice.ui.dlg);
importPackage(Packages.com.silverwrist.venice.ui.helpers);

// Get the request object and the target URI.
rinput = bsf.lookupBean("request");
target = rinput.getParameter("tgt");
if (target==null)
  target = "top.js.vs";

// If the user is not logged in, this is an error.
user = rinput.user;
if (!(user.isLoggedIn()))
{ // user not logged in - just bail on this
  vlib.output(new ErrorBox(null,"You must log in before you can verify your account's email address."));
  vlib.done();

} // end if

// If the user's E-mail address is already verified, this is a no-op.
if (user.isEmailVerified())
{ // user already verified - just bail on this
  vlib.output(new Redirect(target,LinkTypes.SERVLET));
  vlib.done();

} // end if

dlg = rinput.getDialog("verify");  // retrieve the verification dialog

if ("GET"==rinput.verb)
{ // display the dialog and have done with it
  dlg.setValue("tgt",target);
  rinput.location = target;
  rinput.displayLogin = false;
  vlib.output(dlg);
  vlib.done();

} // end if

// everything that follows is for a POST operation
op = dlg.whichButton(rinput) + "";
if (op=="cancel")
{ // user cancelled verify - bounce back to the target
  vlib.output(new Redirect(target,LinkTypes.SERVLET));
  vlib.done();

} // end if

dlg.load(rinput);        // load the dialog parameters
rc = null;               // return from this script
if (op=="sendagain")
{ // we want to resend the E-mail confirmation
  try
  { // do the resend!
    user.resendEmailConfirmation();

    // now display the dialog again
    dlg.clear();
    dlg.setValue("tgt",target);
    rinput.location = target;
    rinput.displayLogin = false;
    rc = dlg;

  } // end try
  catch (e)
  { // exception when resending the email confirmation
    etype = vlib.exceptionType(e) + "";
    if (etype.match("DataException"))
      rc = new ErrorBox("Database Error","Database error sending confirmation: " + e.message,target);
    else if (etype.match("EmailException"))
      rc = new ErrorBox("E-mail Error","E-mail error sending confirmation: " + e.message,target);
    else
      rc = e;

  } // end catch

} // end if
else if (op=="ok")
{ // actually perform the confirmation
  try
  { // validate the dialog first
    dlg.validate();

    // now attempt the confirmation
    user.confirmEmail(dlg.getValue("num"));
    
    // all done - go back to where we want to be
    rc = new Redirect(target,LinkTypes.SERVLET);

  } // end try
  catch (e)
  { // exception when confirming...
    etype = vlib.exceptionType(e) + "";
    if (etype.match("ValidationException"))
    { // validation error thrown by dialog
      dlg.setErrorMessage(e.message + " Please try again.");
      dlg.setValue("num",null);
      rinput.location = target;
      rinput.displayLogin = false;
      rc = dlg;

    } // end if
    else if (etype.match("DataException"))
      rc = new ErrorBox("Database Error","Database error verifying email: " + e.message,target);
    else if (etype.match("AccessError"))
    { // invalid confirmation number, perhaps?
      dlg.setErrorMessage(e.message);
      dlg.setValue("num",null);
      rinput.location = target;
      rinput.displayLogin = false;
      rc = dlg;

    } // end if
    else
      rc = e;

  } // end catch

} // end else if
else
{ // button not found...
  logger.error("no known button click on POST to verify_email.js");
  rc = new ErrorBox("Internal Error","Unknown command button pressed",target);

} // end else

vlib.output(rc);  // all done!
