/*
 * 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 Community 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): 
 */
package com.silverwrist.venice.servlets.format;

import java.util.List;
import javax.servlet.ServletRequest;
import com.silverwrist.venice.core.*;
import com.silverwrist.venice.except.*;

public class NewAccountDialog extends ContentDialog
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private VeniceEngine engine = null;

  /*--------------------------------------------------------------------------------
   * Constructors
   *--------------------------------------------------------------------------------
   */

  public NewAccountDialog()
  {
    super("Create Account",null,"createform","account");
    setInstructions("To create a new account, please enter your information below.");
    setHiddenField("cmd","C");
    setHiddenField("tgt","");
    addFormField(new CDFormCategoryHeader("Name"));
    addFormField(new CDTextFormField("prefix","Prefix","(Mr., Ms., etc.)",false,8,8));
    addFormField(new CDTextFormField("first","First name",null,true,32,64));
    addFormField(new CDTextFormField("mid","Middle initial",null,false,1,1));
    addFormField(new CDTextFormField("last","Last name",null,true,32,64));
    addFormField(new CDTextFormField("suffix","Suffix","(Jr., III, etc.)",false,16,16));
    addFormField(new CDFormCategoryHeader("Location"));
    addFormField(new CDTextFormField("loc","City",null,true,32,64));
    addFormField(new CDTextFormField("reg","State/Province",null,true,32,64));
    addFormField(new CDTextFormField("pcode","Zip/Postal Code",null,true,32,64));
    addFormField(new CDCountryListFormField("country","Country",null,true));
    addFormField(new CDFormCategoryHeader("E-mail"));
    addFormField(new CDEmailAddressFormField("email","E-mail address",null,true,32,255));
    addFormField(new CDFormCategoryHeader("Account Information"));
    addFormField(new CDVeniceIDFormField("user","User name",null,true,32,64));
    addFormField(new CDPasswordFormField("pass1","Password",null,true,32,128));
    addFormField(new CDPasswordFormField("pass2","Password","(retype)",true,32,128));
    addFormField(new CDTextFormField("remind","Password reminder phrase",null,false,32,255));
    addCommandButton(new CDImageButton("create","bn_create.gif","Create",80,24));
    addCommandButton(new CDImageButton("cancel","bn_cancel.gif","Cancel",80,24));

  } // end constructor

  protected NewAccountDialog(NewAccountDialog other)
  {
    super(other);

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Overrides from class ContentDialog
   *--------------------------------------------------------------------------------
   */

  protected void validateWholeForm() throws ValidationException
  {
    if (!(getFieldValue("pass1").equals(getFieldValue("pass2"))))
      throw new ValidationException("The typed passwords do not match.");

    if (engine.isEmailAddressBanned(getFieldValue("email")))
      throw new ValidationException("This email address may not register a new account.");

  } // end validateWholeForm

  /*--------------------------------------------------------------------------------
   * External operations
   *--------------------------------------------------------------------------------
   */

  public void setEngine(VeniceEngine engine)
  {
    this.engine = engine;

  } // end setEngine

  public void setTarget(String target)
  {
    setHiddenField("tgt",target);

  } // end setTarget

  public UserContext doDialog(ServletRequest request)
      throws ValidationException, DataException, AccessError, EmailException
  {
    validate();   // validate the dialog

    // Create the new user account and set up its initial context.
    UserContext uc = engine.createNewAccount(request.getRemoteAddr(),getFieldValue("user"),
					     getFieldValue("pass1"),getFieldValue("remind"));

    // Set up the account's contact info.
    ContactInfo ci = uc.getContactInfo();
    ci.setNamePrefix(getFieldValue("prefix"));
    ci.setGivenName(getFieldValue("first"));
    String blort = getFieldValue("mid");
    if ((blort==null) || (blort.length()<1))
      ci.setMiddleInitial(' ');
    else
      ci.setMiddleInitial(blort.charAt(0));
    ci.setFamilyName(getFieldValue("last"));
    ci.setNameSuffix(getFieldValue("suffix"));
    ci.setLocality(getFieldValue("loc"));
    ci.setRegion(getFieldValue("reg"));
    ci.setPostalCode(getFieldValue("pcode"));
    ci.setCountry(getFieldValue("country"));
    ci.setEmail(getFieldValue("email"));

    // save the contact info for the user (this also sends email confirmation messages as needed)
    uc.putContactInfo(ci);

    return uc;

  } // end doDialog

  public void resetOnError(String message)
  {
    setErrorMessage(message);
    setFieldValue("pass1",null);
    setFieldValue("pass2",null);

  } // end resetOnError

  public Object clone()
  {
    return new NewAccountDialog(this);

  } // end clone

} // end class NewAccountDialog


