<?php
include("postget.php");
include_once("common.php");
checklevel(2,$_REQUEST['clientid']);
echo '
<meta http-equiv="Content-Type" content="text/html; charset='.$config[72][1].'">
<link rel="stylesheet" type="text/css" media="screen" href="css/style.css">
';

/////// armar el query
$where = '';

if ($omitfirst==1) {
	$where.= "last LIKE '%$last%' ";
}else{
	$where.= "first LIKE '%$first%' ";
	$where.= "OR last LIKE '%$last%' ";
}


//descomponer la direccion
$address0 = $address;
$address = explode(' ',$address);
// almacenar en address2 los terminos de la direccion que tienen m�s de 3 caracteres
$i=0;
while ($i <= count($address)) {
	if (strlen($address[$i])>3) {
		$address2[]=$address[$i];
	}
	$i++;
}

//// colocar en el WHERE cada palabra de la direccion
$i=0;
while ($i < count($address2)) {
	$where.= "OR address LIKE '%$address2[$i]%' ";
	$i++;
}


////// descomponer la direccion de email
$email0 = $email;
$email = explode('@',$email);
////// colocar en el WHERE lo que est� delante de la arroba
$where.= "OR email LIKE '%$email[0]%' ";


//descomponer el telefono
$phone0 = $phone;
$phone = explode(' ',$phone);
// almacenar en phone2 los terminos de la direccion que tienen m�s de 5 caracteres
$i=0;
while ($i <= count($phone)) {
	if (strlen($phone[$i])>5) {
		$phone2[]=$phone[$i];
	}
	$i++;
}

//// colocar en el WHERE cada numero entero del telefono
$i=0;
while ($i < count($phone2)) {
	$where.= "OR phone LIKE '%$phone2[$i]%' ";
	$i++;
}



$query="SELECT * FROM hd_clients
WHERE (".$where.") 
AND clientid<>".$_SESSION['hd_clientid']." AND active<>'-1' 
ORDER BY clientid";

//echo $query;

$result=mysql_query($query,$db_link);

print('<table width="98%" class="table table-bordered">');

print("<tr>
	<th>"._CLIENT."</th> 
	<th>"._START_DATE."</th> 
	<th>"._ADDRESS."</th> 
	<th>"._PHONE."</th> 
	<th>"._EMAIL."</th></tr>");

$contador=0;
while($row = mysql_fetch_array($result))
{
	$contador++;

	if ($omitfirst<>1) {
		$row[first]=preg_replace ('/'. $first .'/i', "<b><font color='red'>".$first."</font></b>", $row[first]);
	}
	$row[last]=preg_replace ('/'. $last .'/i', "<b><font color='red'>".$last."</font></b>", $row[last]);


	//// son arrays
	$row[email]=preg_replace ('/'. $email[0].'/i', "<b><font color='red'>".$email[0]."</font></b>", $row[email]);

	$i=0;
	while ($i < count($address2)) {
		$row[address]=preg_replace ('/'. $address2[$i].'/i', "<b><font color='red'>".$address2[$i]."</font></b>", $row[address]);
		$i++;
	}

	$i=0;
	while ($i < count($phone2)) {
		$row[phone]=preg_replace ('/'. $phone2[$i].'/i', "<b><font color='red'>".$phone2[$i]."</font></b>", $row[phone]);
		$i++;
	}



	print("<tr><TD><a href='".$serverurl."/tickets.php?clientid=$row[clientid]'>$row[clientid]</a> ".$row[first]." ".$row[last]."</TD>");
	print("<TD align='left'>".date($df.' H:i',$row[start])." (");
	print("".TimeAgo($row[start],time()+$config[31][1]*3600).")</TD>");
	print("<TD align='left'>".$row[address]."</TD>");
	print("<TD align='left'>".$row[phone]."</TD>");
	print("<TD align='left'>".$row[email]."</TD></TR>");
}


print("<b>".$contador." "._RESULTS."</b> &nbsp;&nbsp;&nbsp;&nbsp;");
if ($omitfirst<>1) {
	print("<b><a href='".$serverurl."/duplicatedclients.php?clientid=".$_SESSION['hd_clientid']."&first=$first&last=$last&address=$address0&phone=$phone0&email=$email0&omitfirst=1'>".ucfirst(_OMIT)." ".strtolower(_FIRST_NAME)."</a></b>");
}

print("</table>");

?>