
sub PreCreateForm
{
   my $cUserData;
   my $cUserName;
   my $cPassword;

   if($oSessionGL->getState("IsAnonymous"))
   {
      &MySetVal("State Init", "BtnNew", "-");
      &MySetVal("State Init", "BtnDef", "-");
      &MySetVal("Layout Label comment", "Text",
                b(i18n("Please note that anonymous users " .
                       "can't change the password.")));
   }


   if(!$oSessionGL->getState("ActualLogin") &&
      $oSessionGL->getState("login"))
   {
      &MySetVal("State Init", "BtnNew", "-");
      &MySetVal("State Init", "BtnDef", "-");
      &MySetVal("Layout Label comment", "Text",
                b(i18n("You must be logged in for " .
                       "changing the password.")));
   }

   if($oSessionGL->getState("login"))
   {

      $cUserData = $oDbSessionGL->SqlSelect
          ("SELECT key_value
            FROM wwwdb_state
            WHERE
               key_name   = 'login' and
               session_id = ?",
           &GetAttr("SessionId"))->[0]->[0];

      ($cUserName, $cPassword) = split /,/, $cUserData;

      &SetField("username", $cUserName);
      &MySetVal("Layout Field username", "Type", "Label");
   }
   else
   {
      &MySetVal("Layout Label old_password", "Text", " ");
      &MySetVal("Layout Field old_password", "Type", "Label");
      &MySetVal("Layout Field username", "Type", "Text");

      &MySetVal("State Init", "BtnNew", i18n("Create User"));

   }

}

sub Check_dummypassword_username ()
{
   my $cUserData;

   if(!$oSessionGL->getState("login"))
   {
      $cUserData = $oDbSessionGL->SqlSelect
          ("SELECT session_id
            FROM wwwdb_state
            WHERE
               key_name   =    'login' and
               key_value  LIKE ?",
           sprintf("%s,%%",
                   &GetField("username")))->[0]->[0];

      if($cUserData)
      {
         &AddError("username",
                   i18n("This user already exists, please re-enter!!"));
         &SetField("username", "");
      }
   }
}



sub Check_dummypassword_password1 ()
{
   if(&GetField("password1") ne &GetField("password2"))
   {
      &AddError("password1",
                i18n("Difference in passwords, please re-enter!!"));
      &SetField("password1", "");
      &SetField("password2", "");
   }
}



sub Check_dummypassword_old_password ()
{
   my $cUserData;
   my $cUserName;
   my $cPassword;

   if($oSessionGL->getState("login"))
   {

      $cUserData = $oDbSessionGL->SqlSelect
          ("SELECT key_value
            FROM wwwdb_state
            WHERE
               key_name   =    'login' and
               session_id = ?",
           &GetAttr("SessionId"))->[0]->[0];

      ($cUserName, $cPassword) = split /,/, $cUserData;

      printf STDERR ("%s <=> %s\n",
                     crypt(&GetField("old_password"), "WW"),
                     $cPassword);


      if(crypt(&GetField("old_password"), "WW") ne $cPassword)
      {
         &AddError("old_password",
                   i18n("The old password is wrong!!"));
      }
   }
}


sub MyBtnNew()
{

   if(!&GetAttr("RecordOk"))
   {
      &GenPage();
   }
   else
   {
      $oSessionGL->setState("login",
                            &GetField("username") . "," .
                            crypt(&GetField("password1"), "WW"));

      &OkForm("", i18n("Password changed!"));
      &MyExit();
   }
}



sub PostFirstColumn ()
{
   my $cResult = "";


   if(&IsAttribOK("ActualLogin") && !&IsAttribOK("IsAnonymous"))
   {
      $cResult .= (br() x 2 .
                   b(i18n("See also:")) .
                   br().
                   li(&ResolveRefField("wwwdb://WWWdb:Tools:UserPrefs",
                                       i18n("Set my prefs"))));
   }

   return small($cResult);

}

1;

