/**
  * Return JS for preload dependent ctrl for search fields
  *
  * @param string $fName - field name
  * @param string $fval - value of field
  * @return array
  */
 function getSearchPreloadArr($fName, $fval)
 {
     // if no parent in project settings
     if (GetEditFormat($fName) != EDIT_FORMAT_LOOKUP_WIZARD && !UseCategory($fName, $this->tName)) {
         return false;
     }
     // if parent exist in settings
     $parentFieldName = CategoryControl($fName, $this->tName);
     $parentVal = $this->getParentVal($fName);
     $doFilter = true;
     // if no filter f parent doesn't exist or it's value is empty
     if ($parentVal === false || $parentVal === '') {
         $doFilter = false;
     }
     $output = loadSelectContent($fName, $parentVal, $doFilter, $fval[$fName]);
     return array('vals' => $output, "fVal" => $fval[$fName]);
 }
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;
}