<?php

  // =============================================================================================== // 
  //                                           support-login
  // -----------------------------------------------------------------------------------------------
  //
  //  This page prompts support admins to log in to the support center to access tickets from clients
  // 
  // ------------------------------------------- CHANGE LOG ----------------------------------------
  //
  // 2010-04-10 - Origination
  //
  // =============================================================================================== 

  $this_page = "support-login";

  include "inc_functions.php";
  include "inc_db.php";
  include "inc_vars.php";

  $cookie_access_error = $_COOKIE["cookie_access_error"];

  $f_submit = $_POST["f_submit"];
  $f_login  = $_POST["f_login"];
  $f_password = $_POST["f_password"];

  if ($cookie_access_error == 'Y')
  {
    $error_msg = "You must login to access administrative area.";
    $msg = "You must login to access administrative area.";
    setcookie("cookie_access_error", '', time()+60*20, "/", false, 0);
  }
  elseif ($cookie_access_error == 'N')
  {
    $msg = "You have been logged out.";
    setcookie("cookie_access_error", '', time()+60*20, "/", false, 0);
  }

  if ($f_submit == 'Y')
  {
    if (empty($f_login))
    {
      $error_msg = "Login ID is required to login.";
      $focus_loc = "document.f.f_login.focus(); ";
      $f_password = "";
    }

    elseif (empty($f_password))
    {
      $error_msg = "Password is required to login.";
      $focus_loc = "document.f.f_password.focus(); ";
    }

    else
    {
      $query = "SELECT REC, ADMIN_PW, ACTIVE_STATUS
                  FROM SUPPORT_ADMIN
                 WHERE ADMIN_ID = '$f_login' ";

      if (!($result = @ mysql_query($query, $connection)))
        showsqlerror("SELECT","0100 - $this_page \n\n $query");

      if ( mysql_num_rows($result) == 0 )
      {
        $error_msg = "The specified User ID was not identified as a valid login.";
        $focus_loc = "document.f.f_login.focus(); ";
      }
      else
      {
        $row = mysql_fetch_array($result);

        $rec = $row[REC];

        if ($row["ADMIN_PW"] != $f_password)
        {
          $error_msg = "The Password is invalid.  Try again.";
          $f_password = "";
          $focus_loc = "document.f.f_password.focus(); ";
        }

        elseif ($row[ACTIVE_STATUS] != 'A')
        {
          $error_msg = "User is no longer active.";
          $f_password = "";
          $focus_loc = "document.f.f_login.focus(); ";
        }

        // At this point, the login has been verified.  Update database with stats
        else
        {
          $access_code = str_replace('2', 'a', str_replace('6', 's', str_replace('7', 'i', mt_rand(100000, 999999))));

          $query = "UPDATE SUPPORT_ADMIN
                       SET IP_ADDRESS = '$REMOTE_ADDR'
                         , ACTION_DATE = CURRENT_DATE
                         , ACTION_TIME = CURRENT_TIME
                         , ACCESS_CODE = '$access_code' 
                     WHERE ADMIN_ID = '$f_login'";

          if (!(@ mysql_query($query, $connection)))
             showsqlerror("UPDATE","0200 - $this_page \n\n $query ");

          $timeout_seconds = $admin_timeout_minutes;
          $cookie_access_code = $rec . '-' . $access_code;
//        setcookie(Name, Value, Expire, Path, Domain, Secure, httponly);
          setcookie("cookie_access_code", $cookie_access_code, 0, "/", false, 0, 1);

          $link_to = $src_dir . "support-tickets/P";
          header("Location: $link_to");
          exit;
        }
      }
    }
  }
  else
  {
      $focus_loc = "document.f.f_login.focus(); ";
  }

  // ==================================================
  // HTML
  // ==================================================

  // No need to check to timeout b/c user is not logged in

  echo 
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' . 
'<html xmlns="http://www.w3.org/1999/xhtml">' . 


'<head>' .  
   '<title>IMG Support Login</title>' . 
   '<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">' . 
   '<meta name="description" content="">' . 
   '<meta name="keywords" content="">' . 
   '<link rel="shortcut icon" href="' . $img_dir . 'favicon.ico" />' .

$js_disabled_logic;
check_cookies();

  echo
   '<style>';
 
 // 2014-10-09 - Adding margin:auto to center for chrome but a defined width is also required
 if ($system == 'prod')
   echo 
      'body { ' . 
	   'background-color: #ffffff; ' .
         'margin: auto; ' . 
         'width: 445px; ' . 
      '} ';
 else
   echo 
      'body { ' . 
	   'background: url(http://test.international-monetary-group.com/img/bg_test_300x300.jpg) top left; ' .
         'margin: auto; ' . 
         'width: 445px; ' .
      '} ';

 echo
      'table { ' .
         'width: 445px; ' .
         'height: 286px; ' .  
         'font-family: arial; ' . 
	   'background: #ffffff url(http://www.international-monetary-group.com/img/bg-login-box.jpg) no-repeat;' .
      '} ' . 

      'td { ' .
         'padding: 10px; ' .
         'text-align: left; ' .   
      '} ' . 

      'td.rt { ' .
         'padding: 10px; ' .
         'text-align: right; ' .   
      '} ' . 
   '</style>' . 
   '<script> ' . 
      'function StartUp() ' . 
      '{ '; 
      if (!empty($error_msg)) echo ' alert("' . $error_msg . '"); ';
      echo $focus_loc . 
      ' }' . 
   '</script>' . 
'</head>' . 
'<center align="center">' . 

'<body onload="StartUp();">' .    
   '<div style="padding:30px;text-align:center">' .
      '<img src="' . $img_dir . 'logo_350x123.png" />' . 
   '</div>' .  

   '<form name=f method=post action="' . $src_dir . 'support-login">' . 
   '<input type="hidden" name="f_submit" value="Y">' . 
      '<table>' . 
         '<tr>' . 
            '<td colspan="2" height="25px" valign="center" style="font-weight:bold;font-size:18px;text-align:center">' . 
               'Support Login' .  
            '</td>' . 
         '</tr>' . 

         '<tr>' . 
            '<td colspan="2" height="25px" valign="center" style="color:red;font-size:12px;text-align:center">' . 
               $msg . 
            '</td>' . 
         '</tr>' . 

         '<tr>' . 
            '<td width="45%" class="rt"><b>User ID:</b></td>' . 
            '<td width="55%"><input type="text" name="f_login" class="input" value="' . $f_login . '"></td>' . 
         '</tr>' . 

         '<tr>' . 
            '<td class="rt"><b>Password:</b></td>' . 
            '<td><input type="password" name="f_password" value="' . $f_password . '"></td>' . 
         '</tr>' . 

         '<tr>' . 
            '<td></td>' . 
            '<td align="left"><input type="submit" value="Login"></td>' .  
         '</tr>' . 

         '<tr>' . 
            '<td colspan="2" height="50px"></td>' . 
         '</tr>' . 
      '</table>' . 
   '</form>' .           
'</body>' . 
'</html>';


?>
