<?
/*
Autor: Lic. Carlos Alberto Vaca Coronado
Version: 3.2.0
*/

session_name('portal_absa');session_start();if(!isset($_SESSION['usuario_p_prov_absa']) || trim($_SESSION['usuario_p_prov_absa']) == "" || trim(strtoupper($_SESSION["URL_PORTAL"]."admin.php")) != trim(strtoupper($_SERVER['HTTP_REFERER']))){?><script>window.location.href="<?=$_SESSION["URL_PORTAL"]."index.php"?>"</script><?exit;}

require "../conexion/conectar.php";

$conexion = new CONDB();
$conexion->conectar();

if(trim($_POST['valor']) == trim($_POST['valorenbd']))
	return false;

switch(trim(strtoupper($_POST["tipo"]))){
	case "DUPLICADO": if(!validaDuplicado($_POST['tabla'],$_POST['valor'],$_POST['campo'],$_POST['valkeys'])){
						?><script>alert("El dato: <?=$_POST['valor']?> no esta disponible."); document.getElementById('<?=$_POST['campo']?>').value="<?=$_POST['valorenbd']?>";</script><?
					  }
}

function validaDuplicado($tabla,$valor,$campo,$valkeys){
	global $conexion;
$complemento="";
if($valkeys != ""){
	$camposValor = explode(",",$valkeys);
	for($i = 0; $i < count($camposValor); $i++){
		$key_val = explode(":",$camposValor[$i]);
			if($campo != trim($key_val[0]))
				$complemento.=" and ".trim($key_val[0]).str_replace("igual","=",str_replace("dif","<>",$_POST['operador']))."'".trim($key_val[1])."'";
	}
}

$sql="select $campo from $tabla where $campo='$valor' $complemento";
echo $sql;
$result = mysqli_query($conexion->CONEXION,$sql);

if(mysqli_num_rows($result) > 0)
	return false;
else
	return true;
}

?>