<?php

//Sourcefile for Autocompletion
if (empty($_GET['term'])) {
    exit;
}
require_once "../inc/stdLib.php";
if ($_GET['case'] == 'name') {
    require_once "../inc/crmLib.php";
    require_once "../inc/FirmenLib.php";
    require_once "../inc/persLib.php";
    require_once "../inc/UserLib.php";
    $suchwort = mkSuchwort($_GET['term']);
    $rsC = getAllFirmen($suchwort, true, "C");
    $rsV = getAllFirmen($suchwort, true, "V");
    $rsK = getAllPerson($suchwort);
    $rsE = getAllUser($suchwort);
    $rs = array();
    if ($rsC) {
        foreach ($rsC as $key => $value) {
            if (count($rs) > 11) {
                break;
            }
            array_push($rs, array('label' => $value['name'], 'category' => '', 'src' => 'C', 'id' => $value['id']));
        }
    }
    if ($rsV) {
        foreach ($rsV as $key => $value) {
            if (count($rs) > 11) {
                break;
            }
            $i++;
            if ($i >= $_SESSION['listLimit']) {
                $d = "dialog_viele";
                break;
            }
        }
        echo "</tbody></table>\n<br>";
    } else {
        $d = "dialog_keine";
    }
} else {
    if ($_POST["submit"] == "adress") {
        include "../inc/FirmenLib.php";
        include "../inc/persLib.php";
        include_once "../inc/UserLib.php";
        $suchwort = mkSuchwort($_POST["swort"]);
        $rsE = $rsV = $rsC = $rsK = false;
        if ($_POST['swort'] != '') {
            $rsC = getAllFirmen($suchwort, true, "C");
            if ($rsC) {
                $anzahl = count($rsC);
            }
            if ($anzahl <= $_SESSION['listLimit']) {
                $rsV = getAllFirmen($suchwort, true, "V");
                if ($rsV) {
                    $anzahl += count($rsV);
                }
                if ($anzahl <= $_SESSION['listLimit']) {
                    $rsK = getAllPerson($suchwort);
                    if ($rsK) {
                        $anzahl += count($rsK);