<?php require_once('Connections/EVENTS.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $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 != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
?>
<?php
// *** Validate request to login to this site.
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_fldUserAuthorization = "";
  $MM_redirectLoginSuccess = "micalendario/index.php";
  $MM_redirectLoginFailed = "login_usuario_error.php";
  $MM_redirecttoReferrer = false;
  mysql_select_db($database_EVENTS, $EVENTS);
  
  $LoginRS__query=sprintf("SELECT usuarios, contrase FROM login_usuario WHERE usuarios=%s AND contrase=%s",
    GetSQLValueString($loginUsername, "text"), GetSQLValueString($password, "text")); 
   
  $LoginRS = mysql_query($LoginRS__query, $EVENTS) or die(mysql_error());
  $loginFoundUser = mysql_num_rows($LoginRS);
  if ($loginFoundUser) {
     $loginStrGroup = "";
    
	if (PHP_VERSION >= 5.1) {session_regenerate_id(true);} else {session_regenerate_id();}
    //declare two session variables and assign them
    $_SESSION['MM_Username'] = $loginUsername;
    $_SESSION['MM_UserGroup'] = $loginStrGroup;	      

    if (isset($_SESSION['PrevUrl']) && false) {
      $MM_redirectLoginSuccess = $_SESSION['PrevUrl'];	
    }
    header("Location: " . $MM_redirectLoginSuccess );
  }
  else {
    header("Location: ". $MM_redirectLoginFailed );
  }
}
?>
<!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"><label for="usuario3"></label>
            <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><label for="contrase"></label>
            <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>