function buildLookupSQL($field, $table, $parentVal, $childVal = "", $doCategoryFilter = true, $doValueFilter = false, $addCategoryField = false, $doWhereFilter = true, $oneRecordMode = false)
{
    global $strTableName;
    if (!strlen($table)) {
        $table = $strTableName;
    }
    //	read settings
    $nLookupType = GetFieldData($table, $field, "LookupType", LT_LISTOFVALUES);
    if ($nLookupType != LT_LOOKUPTABLE) {
        return "";
    }
    $bUnique = GetFieldData($table, $field, "LookupUnique", false);
    $strLookupWhere = LookupWhere($field, $table);
    $strOrderBy = GetFieldData($table, $field, "LookupOrderBy", "");
    $bDesc = GetFieldData($table, $field, "LookupDesc", false);
    $strCategoryFilter = GetFieldData($table, $field, "CategoryFilter", "");
    if ($doCategoryFilter) {
        $parentVal = make_db_value(CategoryControl($field, $table), $parentVal);
    }
    if ($doValueFilter) {
        $childVal = make_db_value($field, $childVal);
    }
    //	build SQL string
    $LookupSQL = "SELECT ";
    if ($oneRecordMode) {
        $LookupSQL .= "top 1 ";
    }
    if ($bUnique) {
        $LookupSQL .= "DISTINCT ";
    }
    $LookupSQL .= GetLWLinkField($field, $table);
    $LookupSQL .= "," . GetLWDisplayField($field, $table);
    if ($addCategoryField && strlen($strCategoryFilter)) {
        $LookupSQL .= "," . AddFieldWrappers($strCategoryFilter);
    }
    $LookupSQL .= " FROM " . AddTableWrappers(GetLookupTable($field, $table));
    //	build Where clause
    $categoryWhere = "";
    $childWhere = "";
    if (UseCategory($field, $table) && $doCategoryFilter) {
        $condition = "=" . $parentVal;
        if ($childVal === "null") {
            $condition = " is null";
        }
        $categoryWhere = AddFieldWrappers($strCategoryFilter) . $condition;
    }
    if ($doValueFilter) {
        $condition = "=" . $childVal;
        if ($childVal === "null") {
            $condition = " is null";
        }
        $childWhere = AddFieldWrappers(GetLWLinkField($field, $table)) . $condition;
    }
    $strWhere = "";
    if ($doWhereFilter && strlen($strLookupWhere)) {
        $strWhere = "(" . $strLookupWhere . ")";
    }
    if (strlen($categoryWhere)) {
        if (strlen($strWhere)) {
            $strWhere .= " AND ";
        }
        $strWhere .= $categoryWhere;
    }
    if (strlen($childWhere)) {
        if (strlen($strWhere)) {
            $strWhere .= " AND ";
        }
        $strWhere .= $childWhere;
    }
    if (strlen($strWhere)) {
        $LookupSQL .= " WHERE " . $strWhere;
    }
    //	order by clause
    if (strlen($strOrderBy)) {
        $LookupSQL .= " ORDER BY " . AddTableWrappers(GetLookupTable($field, $table)) . "." . AddFieldWrappers($strOrderBy);
        if ($bDesc) {
            $LookupSQL .= " DESC";
        }
    }
    return $LookupSQL;
}
if ($hasWhere) {
    if (!$found) {
        // try to make query without WHERE expression
        foreach ($fieldsArr as $f) {
            $fEditFormat = GetFieldData($strTableName, $f, 'EditFormat', '');
            if ($fEditFormat != EDIT_FORMAT_LOOKUP_WIZARD || GoodFieldName($f) != $field) {
                continue;
            }
            $origfield = GetFieldByGoodFieldName($field);
            $lookupValue = make_db_value($origfield, $lookupValue);
            $LookupType = GetFieldData($strTableName, $f, 'LookupType', '');
            if ($LookupType == LT_LOOKUPTABLE) {
                $LookupSQL = "SELECT count(*)";
                $LookupSQL .= " FROM " . AddTableWrappers(GetFieldData($strTableName, $f, 'LookupTable', '')) . " ";
                if (strlen(LookupWhere($f, $strTableName))) {
                    $LookupSQL .= "where (" . LookupWhere($f, $strTableName) . ")  AND " . GetLWLinkField($f, $strTableName, true) . "=" . $lookupValue;
                } else {
                    $LookupSQL .= " WHERE " . GetLWLinkField($f, $strTableName, true) . "=" . $lookupValue;
                }
                if (GetFieldData($strTableName, $f, 'UseCategory', false)) {
                    $cvalue = make_db_value(GetFieldData($strTableName, $f, 'CategoryControl', ''), postvalue("category"));
                    $LookupSQL .= " AND " . AddFieldWrappers(GetFieldData($strTableName, $f, 'CategoryFilter', '')) . "=" . $cvalue;
                }
            }
        }
        $rs1 = db_query($LookupSQL, $conn);
        $datacount = db_fetch_numarray($rs1);
        if (!$datacount[0]) {
            foreach ($fieldsArr as $f) {
                $fEditFormat = GetFieldData($strTableName, $f, 'EditFormat', '');
                if ($fEditFormat != EDIT_FORMAT_LOOKUP_WIZARD || GoodFieldName($f) != $field) {
 function initLookupParams()
 {
     $this->parId = postvalue("parId");
     $this->firstTime = postvalue("firsttime");
     $this->mainField = postvalue("field");
     $this->lookupControl = postvalue("control");
     $this->lookupCategory = postvalue("category");
     $this->mainTable = postvalue("table");
     global $tables_data;
     include_once getabspath('include/' . GetTableURL($this->mainTable) . '_settings.php');
     $this->lookupParams = "mode=lookup&id=" . $this->id . "&parId=" . $this->parId . "&field=" . rawurlencode($this->mainField) . "&control=" . rawurlencode($this->lookupControl) . "&category=" . rawurlencode($this->lookupCategory) . "&table=" . rawurlencode($this->mainTable) . "&editMode=" . postvalue('editMode');
     $this->sessionPrefix = $this->tName . "_lookup_" . $this->mainTable . '_' . $this->mainField;
     $this->linkField = GetLWLinkField($this->mainField, $this->mainTable, false);
     $this->dispField = GetLWDisplayField($this->mainField, $this->mainTable, false);
     if (GetFieldData($this->mainTable, $this->mainField, 'CustomDisplay', false)) {
         $this->customField = $this->linkField;
         $this->dispFieldAlias = 'dispField1';
     }
     $this->outputFieldValue($this->linkField, 2);
     $this->outputFieldValue($this->dispField, 2);
     if (UseCategory($this->mainField, $this->mainTable)) {
         $this->categoryField = GetFieldData($this->mainTable, $this->mainField, 'CategoryFilter', '');
     }
     $this->strLookupWhere = LookupWhere($this->mainField, $this->mainTable);
     if ($this->dispFieldAlias && AppearOnListPage($this->dispField)) {
         $this->lookupSelectField = $this->linkField;
     } elseif (AppearOnListPage($this->dispField)) {
         $this->lookupSelectField = $this->dispField;
     } else {
         $this->lookupSelectField = $this->listFields[0]['fName'];
     }
     if ($this->categoryField) {
         if (!strlen(GetFullFieldName($this->categoryField))) {
             $this->categoryField = "";
         }
     }
     if (!$this->categoryField) {
         $this->lookupCategory = "";
     }
 }