<?php
require_once('Connections/EVENTS.php');

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") {
  global $EVENTS;
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }
  switch ($theType) {
    case "text":
      $theValue = ($theValue !== "") ? "'" . $EVENTS->real_escape_string($theValue) . "'" : "NULL";
      break;
    case "long":
    case "int":
      $theValue = ($theValue !== "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue !== "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue !== "") ? "'" . $EVENTS->real_escape_string($theValue) . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue !== "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

if (!isset($_SESSION)) {
  session_start();
}

$loginFormAction = $_SERVER['PHP_SELF'];
if (isset($_GET['accesscheck'])) {
  $_SESSION['PrevUrl'] = $_GET['accesscheck'];
}

if (isset($_POST['usuario'])) {
  $loginUsername = $_POST['usuario'];
  $password = $_POST['contrase'];
  $MM_redirectLoginSuccess = "micalendario/index.php";
  $MM_redirectLoginFailed = "login_usuario_error.php";

  $query = sprintf(
    "SELECT usuarios, contrase FROM login_usuario WHERE usuarios=%s AND contrase=%s", 
    GetSQLValueString($loginUsername, "text"),
    GetSQLValueString($password, "text")
  );

  $result = $EVENTS->query($query);
  if (!$result) {
      die('Error en la consulta: ' . $EVENTS->error);
  }
  
  $loginFoundUser = $result->num_rows;
  if ($loginFoundUser) {
    if (PHP_VERSION >= 5.1) session_regenerate_id(true);
    $_SESSION['MM_Username'] = $loginUsername;
    $_SESSION['MM_UserGroup'] = "";
    $redirect = isset($_SESSION['PrevUrl']) ? $_SESSION['PrevUrl'] : $MM_redirectLoginSuccess;
    header("Location: " . $redirect);
    exit;
  } else {
    header("Location: " . $MM_redirectLoginFailed);
    exit;
  }
}
?>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Documento sin título</title>
<link href="mis_css/login_usuario.css" rel="stylesheet" type="text/css" />
<script src="SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>
<link href="SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css" />
</head>
<body>

<div class="cuerpo">

<table width="604" border="1">
  <tr class="mancomeral" >
    <td width="351" align="center" valign="middle" bgcolor="#FFFFCC">MANCOMERAL S.C.</td>
  </tr>
</table>
<table width="604" border="1">
  <tr>
    <td height="43" colspan="3" align="center" valign="middle" bgcolor="#FFFFCC"><strong>INGRESO DE USUARIO</strong></td>
  </tr>
  <tr>
    <td width="150" height="181"><img src="micalendario/mis_images/candado.png" width="150" height="150" /></td>
    <td width="438" colspan="2"><form id="form1" name="form1" method="POST" action="<?php echo $loginFormAction; ?>">
      <table width="415" border="1">
        <tr class="textologinusuario" >
          <td width="106" align="center" valign="middle"><strong>USUARIO</strong></td>
          <td width="268"><span id="sprytextfield1">
            <input type="text" name="usuario" id="usuario3" />
            <span class="textfieldRequiredMsg">Se necesita un nombre de usuario.</span></span></td>
        </tr>
        <tr class="textologinusuario" >
          <td align="center" valign="middle"><strong>CONTRASEÑA</strong></td>
          <td><span id="sprytextfield2">
            <input type="password" name="contrase" id="contrase" />
            <span class="textfieldRequiredMsg">Se necesita una contraseña.</span></span></td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td><input type="submit" name="enviar" id="enviar" value="Enviar" /></td>
        </tr>
      </table>
    </form></td>
  </tr>
</table>
<script type="text/javascript">
var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1");
var sprytextfield2 = new Spry.Widget.ValidationTextField("sprytextfield2");
</script>

</div>

</body>
</html>