<?php 
include "dbconection.php";

if(isset($_REQUEST['origen'])){
	$query = "SELECT * FROM tblusuarios WHERE strPassword='".$_REQUEST['txtUserPassword']."'";

	$queryResult = mysql_query($query);			
	$totalRows = mysql_num_rows($queryResult);
			
	if($totalRows > 0){
		echo "true";
	}else{
		echo "false";
	}

}else{
	$query = "SELECT t1.*, t2.intTipo as intTipoEmpresa FROM tblusuarios t1 left join tblempresas t2 ON(t1.intEmpresa=t2.intEmpresa)
	WHERE t1.strLogin='".$_REQUEST['txtUser']."' AND t1.strPassword='".$_REQUEST['txtUserPassword']."'";
	//exit( $query );
	
	/*
	$queryResult = mysql_query($query);
	if(mysql_num_rows($queryResult)){
		$row = mysql_fetch_assoc($queryResult);
		session_start();
		
		$_SESSION['user_id'] = $row['intUsuario'];
		$_SESSION['user_name'] = $row['strNombres']." ". $row['strApellidos'];
		$_SESSION['user_login'] = $row['strLogin'];
		$_SESSION['user_type'] = $row['intTipo'];
		$_SESSION['intEmpresa'] = $row['intEmpresa'];
		$_SESSION['intTipoEmpresa'] = $row['intTipoEmpresa'];
		
		header("Location: sistema.php");
	}else{
		header("Location: index.php?Leyenda=1");
	}
	*/

	$queryResult = $mysqli->query($query);

	/*
	echo "Orden inverso...\n";
	for ($num_fila = $resultado->num_rows - 1; $num_fila >= 0; $num_fila--) {
	    $resultado->data_seek($num_fila);
	    $fila = $resultado->fetch_assoc();
	    echo " id = " . $fila['id'] . "\n";
	}*/

	//echo "Orden del conjunto de resultados...\n";
	$queryResult->data_seek(0);
	/*while ($fila = $resultado->fetch_assoc()) {
	    echo " id = " . $fila['intUsuario'] . "\n";
	}*/
	if(mysqli_num_rows($queryResult)>0){
		$row = mysqli_fetch_assoc($queryResult);
		session_start();
		
		$_SESSION['user_id'] = $row['intUsuario'];
		$_SESSION['user_name'] = $row['strNombres']." ". $row['strApellidos'];
		$_SESSION['user_login'] = $row['strLogin'];
		
		$intTipo = intval($row['intTipo']);
		
		$_SESSION['user_type'] = $intTipo;
		
		if($intTipo==1){
		    $strTipo =  "Dr.";  
		}else if($intTipo==2){
		    $strTipo =  "Secretaria";  
		}else if($intTipo==3){
		    $strTipo =  "Monitor Tv";  
		}else if($intTipo==4){
		    $strTipo =  "Enfermera";  
		}
		 
		$_SESSION['str_type'] = $strTipo;
		$_SESSION['intEmpresa'] = $row['intEmpresa'];
		$_SESSION['intTipoEmpresa'] = $row['intTipoEmpresa'];
		
		header("Location: sistema.php");
	}else{
		header("Location: /sofmedi/index.php/pacientes/?Leyenda=1");
	}
}
?>