<?php

include_once ("_dirinfo.php");
include_once (PATH_TO_ROOT."init.php");
include_once (PATH_TO_ROOT."config.php");

include_once (PATH_TO_ROOT."CRM/_CRM_config.php");
include_once(PATH_TO_ROOT."CRM/class/class.Language-Pack.php");
include_once (PATH_TO_ROOT.'CRM/class/class.logger.php');
include_once(PATH_TO_ROOT."CRM/class/class.checkString.php");

$Logger = new LoggerX($db);

$LangPack = new LanguagePack($db, $aLanguage);

$table = "_visitStatistic";
if($_POST['action'] == 'submit'){
	
	$suffix = StringChecker::Check($_POST['suffix']);
	$dan_rojstva = (isset($_POST['dan_rojstva']) && StringChecker::Check($_POST['dan_rojstva']) != null) ? StringChecker::Check($_POST['dan_rojstva']) : 00;
	$mesec_rojstva = (isset($_POST['mesec_rojstva']) && StringChecker::Check($_POST['mesec_rojstva']) != null) ? StringChecker::Check($_POST['mesec_rojstva']) : 00;
	$leto_rojstva = StringChecker::Check($_POST['leto_rojstva']);
	$email = StringChecker::Check($_POST['email']);
	$id_tel = StringChecker::Check($_POST['id_tel']);
	$id_tel_type = StringChecker::Check($_POST['id_tel_type']);
	
	$ime = StringChecker::Check($_POST['ime']);
	$kraj = StringChecker::Check($_POST['kraj']);
	$naslov = StringChecker::Check($_POST['naslov']);
	$poklic = StringChecker::Check($_POST['poklic']);
	$postnaSt = StringChecker::Check($_POST['postna_st']);
	$priimek = StringChecker::Check($_POST['priimek']);
	$projectId = StringChecker::Check($_POST['projectId']);
	$tel1 = StringChecker::Check($_POST['tel1']);
	$tel2 = StringChecker::Check($_POST['tel2']);
	$tel3 = StringChecker::Check($_POST['tel3']);
	$spol = StringChecker::Check($_POST['spol']);
	$stan = StringChecker::Check($_POST['stan']);
	$trzenje = StringChecker::Check($_POST['trzenje_omogoceno']);
	
	$Termogram1 = StringChecker::Check($_POST['Termogram1']);
	$Termogram2 = StringChecker::Check($_POST['Termogram2']);
	$Kisik1 = StringChecker::Check($_POST['kisik1']);
	$Kisik2 = StringChecker::Check($_POST['kisik2']);
	
	if($trzenje == 'on')
	 	$trzenje=1;
	else {
		$trzenje=0;
	}
	
	//prodobitev: project name
	$sqlProjName = "SELECT proj_name FROM ".DB_WARPIT_WEBCATI_BASE.".`_Projects` WHERE id = $projectId";
    $res = $db->SQLexecute($sqlProjName);
 	$dat = $db->fetchAssoc($res);
 	$ProjectName = $dat['proj_name'];
	
 	if ($id_tel_type != 2) {
		//pridobitev: id tel
		$sqlCheckTel = "SELECT * FROM ".DB_WARPIT_WEBCATI_BASE.".".$ProjectName."_structure WHERE `status` = 1 AND id_tel = $id_tel AND status_rec = 1";
		$res = $db->SQLexecute($sqlCheckTel);
		
		if($db->NumRows($res) != 0){
			
			$data = $db->fetchAssoc($res);
			$array1 = explode(' ', $data['oseba_1'], 2);
			$array2 = explode(' ', $data['oseba_3'], 2); 
			$insertIme = $array1[0];
			$insertPriimek = $array1[1];
			$insertNaslov = $data['oseba_2'];
			$insertPostnaSt = $array2[0];
			$insertKraj = $array2[1];
			$dan = $data['dan'];
			$ura = $data['ura'];
			$dogodek = $data['dogodek_2'];
			$cati_location = StringChecker::Check($_POST['cati_location']);
			$projectZip = "0".$data['TP_OMS'].$data['TP_telephone'];
            $insertSpol = $data['spol'];
            $spol = $insertSpol;
			
			$sql = "SELECT * FROM ". WARPIT_CRM.".".$table ." WHERE id_tel = $id_tel";
			$res = $db->SQLexecute($sql);
			
			if($db->NumRows($res) == 0){
			
				$sqlInsert = "INSERT INTO ". WARPIT_CRM.".".$table ." (id_project, firstname, surname, address, post_code, city, dan, ura, dogodek, id_tel, cati_location, phone1, gender) 
						   	  VALUES ( '$projectId', '$insertIme', '$insertPriimek', '$insertNaslov', '$insertPostnaSt', '$insertKraj', '$dan', '$ura', '$dogodek', '$id_tel', '$cati_location', '$projectZip', '$insertSpol')";
			  
				$Logger->Log('dodajanje_uporabnika', 'multiple-filed-insert', 'id_project, firstname, surname, address, post_code, city, dan, ura, dogodek, id_tel, cati_location', 
				  			  ". WARPIT_CRM.".".$table .", " ", addslashes($sqlInsert));
				echo "/*$sqlInsert*/";
				$db->SQLexecute($sqlInsert);
			}		
		}
 	} 
 	if($leto_rojstva != 0)
		$datumRojstva = $dan_rojstva .".". $mesec_rojstva .".". $leto_rojstva;
	else
		$datumRojstva = NULL;
	$sql = "UPDATE ". WARPIT_CRM.".".$table ." SET firstname = '". $ime ."', 
				surname = '". $priimek ."', 
				gender = '". $spol ."', 
				post_code = '". $postnaSt ."', 
				city = '". $kraj ."', 
				address = '". $naslov ."', 
				email = '". $email ."',
				occupation = '". $poklic ."',
				marital_status = '". $stan ."',
				birth_date = STR_TO_DATE('$datumRojstva', '%d.%m.%Y'),
				direct_marketing = '". $trzenje ."',
				phone1 = '". $tel1 ."',
				phone2 = '". $tel2 ."',
				phone3 = '". $tel3 ."',
				Termogram1 = '$Termogram1',
				Termogram2 = '$Termogram2',
				Kisik1 = '$Kisik1',
				Kisik2 = '$Kisik2'
			WHERE ";
	if ( $id_tel_type == 2 ) $sql .= " id = ".$id_tel; else $sql .= " id_tel = ".$id_tel;
	
	//pridobitev stare vrednosti
	$sqlOld = "SELECT firstname, surname, gender, post_code, city, address, email, occupation, marital_status, birth_date, direct_marketing, phone1, phone2, phone3, Termogram1, Termogram1, Kisik1, Kisik1 
			   FROM ". WARPIT_CRM.".".$table ." WHERE ";
	if ( $id_tel_type == 2 ) $sqlOld .= " id = ".$id_tel; else $sqlOld .= " id_tel = ".$id_tel;
	$resOld = $db->SQLexecute($sqlOld);
	$dataOld = $db->fetchAssoc($resOld);
	
	$Logger->Log('posodabljanje_uporabnika', 'multiple-filed-update', 'firstname, surname, gender, post_code, city, address, email, occupation, marital_status, birth_date, direct_marketing, phone1, phone2, phone3, Termogram1, Termogram1, Kisik1, Kisik1', 
				 ". WARPIT_CRM.".".$table .", json_encode($dataOld), addslashes($sql));
				 
	$db->SQLexecute($sql);		
	echo "{ success: true, msg: '".$LangPack->GetLabel("edit_user_window", "update_user_success_msg","Oseba uspešno shranjena!")."', close: true }";
				
}elseif($_POST['action'] == 'addNew'){
	$suffix = StringChecker::Check($_POST['suffix']);
	$dan_rojstva = (isset($_POST['dan_rojstva']) && StringChecker::Check($_POST['dan_rojstva']) != null) ? StringChecker::Check($_POST['dan_rojstva']) : 00;
	$mesec_rojstva = (isset($_POST['mesec_rojstva']) && StringChecker::Check($_POST['mesec_rojstva']) != null) ? StringChecker::Check($_POST['mesec_rojstva']) : 00;
	$leto_rojstva = StringChecker::Check($_POST['leto_rojstva']);
	$email = StringChecker::Check($_POST['email']);
	$id_sub_tel = isset($_POST['id_tel']) ? StringChecker::Check($_POST['id_tel']) : 0;
	$id_tel_type = StringChecker::Check($_POST['id_tel_type']);
	$dan = StringChecker::Check($_POST['dan']);
	$ura = StringChecker::Check($_POST['ura']);
	
	$ime = StringChecker::Check($_POST['ime']);
	$kraj = StringChecker::Check($_POST['kraj']);
	$naslov = StringChecker::Check($_POST['naslov']);
	$poklic = StringChecker::Check($_POST['poklic']);
	$postnaSt = StringChecker::Check($_POST['postna_st']);
	$priimek = StringChecker::Check($_POST['priimek']);
	$projectId = StringChecker::Check($_POST['projectId']);
	$tel1 = StringChecker::Check($_POST['tel1']);
	$tel2 = StringChecker::Check($_POST['tel2']);
	$tel3 = StringChecker::Check($_POST['tel3']);
	$spol = StringChecker::Check($_POST['spol']);
	$stan = StringChecker::Check($_POST['stan']);
	$trzenje = StringChecker::Check($_POST['trzenje_omogoceno']);
	
	$Termogram1 = StringChecker::Check($_POST['Termogram1']);
	$Termogram2 = StringChecker::Check($_POST['Termogram2']);
	$Kisik1 = StringChecker::Check($_POST['kisik1']);
	$Kisik2 = StringChecker::Check($_POST['kisik2']);
	
	$cati_location = StringChecker::Check($_POST['cati_location']);
	
	if($trzenje == 'on')
	 	$trzenje=1;
	else {
		$trzenje=0;
	}
	if($leto_rojstva != 0)
		$datumRojstva = $dan_rojstva .".". $mesec_rojstva .".". $leto_rojstva;
	else
		$datumRojstva = NULL;
	
	$sql = "SELECT DISTINCT(presentation_location) FROM ".WARPIT_CRM."._PresentatorPermissions WHERE cati_location = ".$cati_location." AND id_project=".$projectId;

	$res = $db->SQLexecute($sql);
	$data = $db->fetchAssoc($res);
	$dogodek = $data['presentation_location'];
	
	
	$sql = "SELECT proj_name FROM ".DB_WARPIT_WEBCATI_BASE.".`_Projects` WHERE id = $projectId";
    $res = $db->SQLexecute($sql);
 	$dat = $db->fetchAssoc($res);
 	$ProjectName = $dat['proj_name'];
	
	$osebaFilter = "AND (oseba_1 LIKE '%$ime%$priimek%' OR oseba_1 LIKE '%$priimek%$ime%')";
	$sql = "SELECT * FROM ".DB_WARPIT_WEBCATI_BASE.".".$ProjectName."_structure WHERE `status` = 1 $osebaFilter AND status_rec = 1 AND dan = ".$dan." AND ura = ".$ura;
	$res = $db->SQLexecute($sql);
	if($db->NumRows($res) == 0){
		$sql = "SELECT * FROM ". WARPIT_CRM.".".$table ." WHERE firstname = '$ime' AND surname = '$priimek' AND id_project = '$projectId' AND dan = ".$dan." AND ura = ".$ura;
		$res = $db->SQLexecute($sql);
		if($db->NumRows($res) == 0){
			$sql = "INSERT INTO ". WARPIT_CRM.".".$table ." (id_project, id_sub_tel, firstname, surname, gender, post_code, city, address, email, dan, ura, dogodek, occupation, marital_status, birth_date, direct_marketing, phone1, phone2, phone3, Termogram1, Termogram2, Kisik1, Kisik2, cati_location)
			VALUES ('$projectId', '$id_sub_tel', '$ime', '$priimek', '$spol', '$postnaSt', '$kraj', '$naslov', '$email', '$dan', '$ura', '$dogodek', '$poklic', '$stan', STR_TO_DATE('$datumRojstva', '%d.%m.%Y'), '$trzenje', '$tel1', '$tel2', '$tel3', '$Termogram1', '$Termogram2', '$Kisik1', '$Kisik2', '$cati_location')";
			$Logger->Log('dodajanje_uporabnika', 'multiple-filed-insert', 'id_project, id_sub_tel, firstname, surname, gender, post_code, city, address, email, dan, ura, dogodek, occupation, marital_status, birth_date, direct_marketing, phone1, phone2, phone3, Termogram1, Termogram2, Kisik1, Kisik2', 
					  	  ". WARPIT_CRM.".".$table .", " ", addslashes($sql));
			$db->SQLexecute($sql);
			$sql = "SELECT MAX(id) as mId FROM ".WARPIT_CRM."._visitStatistic";
			$res = $db->SQLexecute($sql);
			$zad = $db->fetchAssoc($res);
			$id = $zad['mId'];
			$idField = "id_tel";
			$sql = "UPDATE ".WARPIT_CRM."._visitStatistic SET $idField = ".($id*-1)." WHERE id = $id";
			$db->SQLexecute($sql);
			echo "{ success: true, msg: '".$LangPack->GetLabel("edit_user_window", "add_user_success_msg","Oseba uspešno dodana!")."', close: true }";
		}else{
			echo "{ success: true, msg: '".$LangPack->GetLabel("edit_user_window", "user_exists_success_msg","Oseba že obstaja!")."', close: false }";	
		}
	}else{
		echo "{ success: true, msg: '".$LangPack->GetLabel("edit_user_window", "user_exists_success_msg","Oseba že obstaja!")."', close: false }";
	}
}elseif($_POST['action'] == "get"){
	$projectId = StringChecker::Check($_POST['projectId']);
	$id_tel = $_POST['id_tel'] != null ? StringChecker::Check($_POST['id_tel']) : 0;
	
	$sql = "SELECT proj_name FROM ".DB_WARPIT_WEBCATI_BASE.".`_Projects` WHERE id = $projectId";
	$res = $db->SQLexecute($sql);
	$dat = $db->fetchAssoc($res);
	$ProjectName = $dat['proj_name'];
	
	$sql = "SELECT * FROM ".WARPIT_CRM."._visitStatistic WHERE ";
	if ( $_POST['id_tel_type'] == 2 ) $sql .= " id = ".$id_tel; else $sql .= "id_tel = ".$id_tel;
	$res = $db->SQLexecute($sql);
	if($db->NumRows($res) != 0){
		$row = $db->fetchAssoc($res);
		$data['ime'] = $row['firstname'];
		$data['priimek'] = $row['surname'];
		$data['spol'] = $row['gender'];
		$data['naslov'] = $row['address'];
		$data['postna_st'] = $row['post_code'];
		$data['kraj'] = $row['city'];
		$data['email'] = $row['email'];
		$data['poklic'] = $row['occupation'];
		$data['zakonski_stan'] = $row['marital_status'];
		$datumRojstva = explode('-', $row['birth_date']);
		$data['dan_rojstva'] = $datumRojstva[2] == 0 ? NULL : $datumRojstva[2];
		$data['mesec_rojstva'] = $datumRojstva[1] == 0 ? NULL : $datumRojstva[1];
		$data['leto_rojstva'] = $datumRojstva[0] == 0 ? NULL : $datumRojstva[0];
		//$data['datum_rojstva'] = $row['birth_date'] != '0000-00-00' ? $row['birth_date'] : "";
		$data['trzenje_omogoceno'] = $row['direct_marketing'];
		$data['tel1'] = $row['phone1'];
		$data['tel2'] = $row['phone2'];
		$data['tel3'] = $row['phone3'];
		$data['Termogram1'] = $row['Termogram1'];
		$data['Termogram2'] = $row['Termogram2'];
		$data['kisik1'] = $row['Kisik1'];
		$data['kisik2'] = $row['Kisik2'];
	}else{
		$sql = "SELECT * FROM ".DB_WARPIT_WEBCATI_BASE.".".$ProjectName."_structure WHERE id_tel = $id_tel AND `status` = 1 AND status_rec = 1";
		$res = $db->SQLexecute($sql);
		$row = $db->fetchAssoc($res);
		
		$ime = explode(" ", $row['oseba_1'],2);
		
		$data['ime'] = $ime[0];
		$data['priimek'] = $ime[1];
		$data['naslov'] = $row['oseba_2'];
		$kraj = explode(" ", $row['oseba_3'],2);
		$data['postna_st'] = $kraj[0];
		$data['kraj'] = $kraj[1];
		$data['tel1'] = $row['TP_oms'].$row['TP_telephone'];
		
	}
	
	$results['success'] = true;
	$results['results'] = 1;
	$results['data'] = $data != "" ? $data : "";
	
	//echo "/*$sql*/\n";
	
	echo json_encode($results);
}else if($_POST['action'] == "getByName"){
	if(isset($_POST['query']) && $_POST['query'] != null){
		$ura = StringChecker::Check($_POST['ura']);
		$dan = StringChecker::Check($_POST['dan']);
		$cati_location = StringChecker::Check($_POST['cati_location']);
		
		$projects = array(DB_WARPIT_WEBCATI_BASE.".`teden_50_structure`", DB_WARPIT_WEBCATI_BASE.".`teden_51_structure`");
		
			/*
		$sql = "SELECT * FROM ".DB_WARPIT_WEBCATI_BASE."._Projects";
		$res = $db->SQLexecute($sql);
		while($row = $db->fetchAssoc($res)){
			$projects[] = $row['proj_name'];
		}
		*/
		
		$sqlAll = array();
		foreach ($projects as $project) {
			$tableName = explode(".",str_replace("`", "", $project));
			$sql = "(SELECT id, oseba_1, (SELECT table_name FROM information_schema.tables WHERE table_name = '".$tableName[1]."'
					) as tableName FROM ".$project." WHERE status = 1 AND status_rec = 1 AND ura = ".$ura." AND dan=".$dan." AND smpl_var = ".$cati_location." AND oseba_1 LIKE '%".StringChecker::Check($_POST['query'])."%')";
			array_push($sqlAll, $sql);
		}
		$sql = implode(" UNION ", $sqlAll);
		
		
		$res = $db->SQLexecute($sql);
		while($row = $db->fetchAssoc($res)){
			$dat['id'] = $row['id'];
			$temp = explode(" ", $row['oseba_1'],2);
			$row['name'] = $temp[0];
			$row['table'] = $row['tableName'];
			$data[] = $row;
		}
		
		$sql = "SELECT id, firstname FROM ".WARPIT_CRM."._visitStatistic WHERE ura = ".$ura." AND dan=".$dan." AND cati_location = ".$cati_location." AND firstname LIKE '%".StringChecker::Check($_POST['query'])."%'";
		$res = $db->SQLexecute($sql);
		while($row = $db->fetchAssoc($res)){
			$dat['id'] = $row['id'];
			$dat['name'] = $row['firstname'];
			$dat['table'] = "_visitStatistic";
			$data[] = $dat;
		}
	}
	
		
	$rez['success'] = true;
	$rez['results'] = sizeof($data);
	$rez['rows'] = $data != null ? $data : "";
	echo json_encode($rez);
}else if($_POST['action'] == "getBySurname"){
	if(isset($_POST['query']) && $_POST['query'] != null){
		$ura = StringChecker::Check($_POST['ura']);
		$dan = StringChecker::Check($_POST['dan']);
		$cati_location = StringChecker::Check($_POST['cati_location']);
		
		$projects = array(DB_WARPIT_WEBCATI_BASE.".`teden_50_structure`", DB_WARPIT_WEBCATI_BASE.".`teden_51_structure`");
		
		/*
		$sql = "SELECT * FROM ".DB_WARPIT_WEBCATI_BASE."._Projects";
		$res = $db->SQLexecute($sql);
		while($row = $db->fetchAssoc($res)){
			$projects[] = $row['proj_name'];
		}
		*/
		
		$sqlAll = array();
		foreach ($projects as $project) {
			$tableName = explode(".",str_replace("`", "", $project));
			$sql = "(SELECT id, oseba_1, (SELECT table_name FROM information_schema.tables WHERE table_name = '".$tableName[1]."'
					) as tableName FROM ".$project." WHERE status = 1 AND status_rec = 1 AND ura = ".$ura." AND dan=".$dan." AND smpl_var = ".$cati_location." AND oseba_1 LIKE '%".StringChecker::Check($_POST['query'])."%')";
			array_push($sqlAll, $sql);
		}
		$sql = implode(" UNION ", $sqlAll);
		
		
		$res = $db->SQLexecute($sql);
		while($row = $db->fetchAssoc($res)){
			$dat['id'] = $row['id'];
			$temp = explode(" ", $row['oseba_1'],2);
			$row['name'] = $temp[1];
			$row['table'] = $row['tableName'];
			$data[] = $row;
		}
		
		$sql = "SELECT id, surname FROM ".WARPIT_CRM."._visitStatistic WHERE ura = ".$ura." AND dan=".$dan." AND cati_location = ".$cati_location." AND surname LIKE '%".StringChecker::Check($_POST['query'])."%'";
		$res = $db->SQLexecute($sql);
		while($row = $db->fetchAssoc($res)){
			$dat['id'] = $row['id'];
			$dat['name'] = $row['surname'];
			$dat['table'] = "_visitStatistic";
			$data[] = $dat;
		}
	}
	
	
	$rez['success'] = true;
	$rez['results'] = sizeof($data);
	$rez['rows'] = $data != null ? $data : "";
	echo json_encode($rez);
}else if($_POST['action'] == "getByAddress"){
	if(isset($_POST['query']) && $_POST['query'] != null){
		$ura = StringChecker::Check($_POST['ura']);
		$dan = StringChecker::Check($_POST['dan']);
		$cati_location = StringChecker::Check($_POST['cati_location']);
		
		$projects = array(DB_WARPIT_WEBCATI_BASE.".`teden_50_structure`", DB_WARPIT_WEBCATI_BASE.".`teden_51_structure`");
		
		/*
		$sql = "SELECT * FROM ".DB_WARPIT_WEBCATI_BASE."._Projects";
		$res = $db->SQLexecute($sql);
		while($row = $db->fetchAssoc($res)){
			$projects[] = $row['proj_name'];
		}
		*/
		
		$sqlAll = array();
		foreach ($projects as $project) {
			$tableName = explode(".",str_replace("`", "", $project));
			$sql = "(SELECT id, oseba_2, (SELECT table_name FROM information_schema.tables WHERE table_name = '".$tableName[1]."'
					) as tableName FROM ".$project." WHERE status = 1 AND status_rec = 1 AND ura = ".$ura." AND dan=".$dan." AND smpl_var = ".$cati_location." AND oseba_2 LIKE '".StringChecker::Check($_POST['query'])."%')";
			array_push($sqlAll, $sql);
		}
		$sql = implode(" UNION ", $sqlAll);
		
		
		$res = $db->SQLexecute($sql);
		while($row = $db->fetchAssoc($res)){
			$dat['id'] = $row['id'];
			$row['name'] = $row['oseba_2'];
			$row['table'] = $row['tableName'];
			$data[] = $row;
		}
		
		$sql = "SELECT id, address FROM ".WARPIT_CRM."._visitStatistic WHERE ura = ".$ura." AND dan=".$dan." AND cati_location = ".$cati_location." AND address LIKE '".StringChecker::Check($_POST['query'])."%'";
		$res = $db->SQLexecute($sql);
		while($row = $db->fetchAssoc($res)){
			$dat['id'] = $row['id'];
			$dat['table'] = "_visitStatistic";
			$dat['name'] = $row['address'];
			$data[] = $dat;
		}
	}
	
	
	$rez['success'] = true;
	$rez['results'] = sizeof($data);
	$rez['rows'] = $data != null ? $data : "";
	echo json_encode($rez);
}else if($_POST['action'] == "getByPhone"){
	if(isset($_POST['query']) && $_POST['query'] != null){
		$ura = StringChecker::Check($_POST['ura']);
		$dan = StringChecker::Check($_POST['dan']);
		$cati_location = StringChecker::Check($_POST['cati_location']);
		
		$projects = array(DB_WARPIT_WEBCATI_BASE.".`teden_50_structure`", DB_WARPIT_WEBCATI_BASE.".`teden_51_structure`");
		
		/*
		$sql = "SELECT * FROM ".DB_WARPIT_WEBCATI_BASE."._Projects";
		$res = $db->SQLexecute($sql);
		while($row = $db->fetchAssoc($res)){
			$projects[] = $row['proj_name'];
		}
		*/		
		
		$sqlAll = array();
		foreach ($projects as $project) {
			$tableName = explode(".",str_replace("`", "", $project));
			$sql = "(SELECT id, CONCAT(TP_oms, TP_telephone) as phone, (SELECT table_name FROM information_schema.tables WHERE table_name = '".$tableName[1]."'
					) as tableName FROM ".$project." WHERE status = 1 AND status_rec = 1 AND ura = ".$ura." AND dan=".$dan." AND smpl_var = ".$cati_location." AND CONCAT(TP_oms, TP_telephone) LIKE '".StringChecker::Check($_POST['query'])."%')";
			array_push($sqlAll, $sql);
		}
		$sql = implode(" UNION ", $sqlAll);
		
		$res = $db->SQLexecute($sql);
		while($row = $db->fetchAssoc($res)){
			$dat['id'] = $row['id'];
			$row['name'] = $row['phone'];
			$row['table'] = $row['tableName'];
			$data[] = $row;
		}
		
		$sql = "SELECT id, phone1 FROM ".WARPIT_CRM."._visitStatistic WHERE ura = ".$ura." AND dan=".$dan." AND cati_location = ".$cati_location." AND phone1 LIKE '%".StringChecker::Check($_POST['query'])."%'";
		$res = $db->SQLexecute($sql);
		while($row = $db->fetchAssoc($res)){
			$dat['id'] = $row['id'];
			$dat['table'] = "_visitStatistic";
			$dat['name'] = $row['phone1'];
			$data[] = $dat;
		}
	}
	
	$rez['success'] = true;
	$rez['results'] = sizeof($data);
	$rez['rows'] = $data != null ? $data : "";
	echo json_encode($rez);
}elseif($_POST['action'] == "loadFromSelect"){
	$id = StringChecker::Check($_POST['id']);
	$table = StringChecker::Check($_POST['table']);
	
	$database = $table == "_visitStatistic" ? WARPIT_CRM : DB_WARPIT_WEBCATI_BASE;
	
	$sql = "SELECT * FROM ".$database.".".$table." WHERE id = ".$id;
	$res = $db->SQLexecute($sql);
	$row = $db->fetchAssoc($res);
	$ime = explode(" ", $row['oseba_1'],2);	
	$data['ime'] = $table == "_visitStatistic" ? $row['firstname'] : $ime[0];
	$data['priimek'] = $table == "_visitStatistic" ? $row['surname'] : $ime[1];
	$data['naslov'] = $table == "_visitStatistic" ? $row['address'] : $row['oseba_2'];
	$kraj = explode(" ", $row['oseba_3'],2);
	$data['postna_st'] = $table == "_visitStatistic" ? $row['post_code'] : $kraj[0];
	$data['kraj'] = $table == "_visitStatistic" ? $row['city'] : $kraj[1];
	$data['tel1'] = $table == "_visitStatistic" ? $row['phone1'] : $row['TP_oms'].$row['TP_telephone'];
	
	$rez['success'] = true;
	$rez['results'] = sizeof($data);
	$rez['data'] = $data != null ? $data : "";
	echo json_encode($rez);
}

?>