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

session_name('portal_absa');
session_start();

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;
}

?>