package GUI::Login;
use System;
use Client;

# submit field must be called ACTION
# login fields must start with LOGIN_
# pass SAVE_GO along


sub login {
  my($q) = @_;
  my $fail;
  my $renv = System->get_renv();

# skip login page on 2.4 edition when accessed from other machine
# than the server machine.

  if ( $renv->{version} >=  "2.4" && !$renv->{gui_debug} && 
      ($ENV{SERVER_ADDR} ne $ENV{HTTP_CLIENT_IP}) ) {
    Client->invalid_page(); 
    return;
  }

  if ($renv->{solution} ne "N" && ($ENV{REQUEST_URI} ne "/storade") &&
       -d "/usr/share/webconsole/console" && index($ENV{LOCAL_HOST}, ":7654") < 0 )  {
    print Html->text_header();
    print "<meta http-equiv=Refresh content=\"0;URL=/console/launch/Launch\">";
    return;
  }
  my $NARROW = Http->isNarrow();
  $extra = $NARROW ? "" : "background=gif/login2.jpg";

  print Html->body(undef, {extra => $extra, body => 1} );
  my $ruser = System->get_ruser();
  my $window = $ruser->{window} || $renv->{window};
  my $LB = Labels->read("GUI::Login");

  if (!$q->{REDIRECT} && $q->{GO} ne "idx" && $q->{GO} ne "" && $window ne "N") {
    print "
    <form target=_top autocomplete=off>
      <input type=hidden name=GO value=\"GUI::Login::login\">
      <input type=hidden name=REDIRECT value=1>
      <input type=hidden name=_INFO_ value=$q->{_INFO_}>
    </form>
    <script>
     document.forms[0].submit();
    </script>";
    return;
  }
  #Navigation::nf_top({OFF => 1});

  my $copyright = $NARROW ? "" : Html->readHtdocs("copyright2");
  my ($info, $info_img);
  if ($q->{ERR}) {
    $info_img = "<img src=/gif/error_large.gif>";
    $info     = "<font color=red><b>$LB->{invalid}</b></font>" ;

  } elsif ($q->{_INFO_} eq "TIMEOUT") {
     $info_img = "<img src=/gif/error_large.gif>";
     $info     = "<b><font color=red>$LB->{expired}</font></b>";

  } elsif ($q->{_INFO_} eq "LOGOUT") {
     $info_img = "<img src=/gif/info_large.gif>";
     $info = "<b><font color=navy>$LB->{logout}</font></b>";
  }
  my $sub =  Html::Screen->submitButton("ACTION", $LB->{submit});
  my $height = $NARROW ? 0 : 210;
  my $width  = $NARROW ? 0 : 140;
  if ($NARROW) {
    print "<table border=0 width=95% bgcolor=#E0E0E0 cellpadding=2>
    <tr><td bgcolor=$Style::SUNBLUE><img src=/gif/tab_logo2.gif></td><td><b><font color=navy><center>Storage Advanced Diagnostic Environment
    </table>";
  }
  print "
 <table border=0>
 <tr><td  width=1%><img src=gif/dot_clear.gif height=$height width=1></td>
 <tr><td width=1%><img src=gif/dot_clear.gif height=1 width=$width></td>
  <td>
 <form method=post autocomplete=off>
 <input type=hidden name=ACTION_LOGIN value=Submit>
 <table border=0 cellspacing=0 cellpadding=1 xbgcolor=white>
 <tr><td>&nbsp;</td>
     <td></td><td><table border=0 cellspacing=0><tr><td>$info_img&nbsp;</td><td>$info</table></td>
 <tr><td>&nbsp;</td>
     <td align=right><b>$LB->{server}:&nbsp;</td>
     <td>$renv->{hostname}</td>
 <tr><td>&nbsp;</td>
     <td align=right><b>$LB->{user}:&nbsp;</td>
     <td><input type=text name=LOGIN_login size=25> &nbsp;</td>

 <tr><td>&nbsp;</td>
     <td align=right><b>$LB->{password}:&nbsp;</td>
     <td><input type=password name=LOGIN_password size=25></td>
 <tr>
     <td colspan=2></td><td><b>
     <input type=hidden name=XRUN_URL value=\"http://sp-87:9080/se6000ui/login.do?username=\$u&password=\$p&force=force\">
      $sub 
 </td>
 </table>
 <tr><td width=1%><img src=/gif/dot_clear.gif height=130 width=1></td>
 <tr><td></td><td bgcolor=white>
     <table width=80% border=0><tr><td>$copyright</table>
     </td>
 </form>
</table>
";
}


1;
