<?php
/*=======================================================================
 *
 * cs_csc_list.php
 *
 * Autor: Marc Vollmann
 *
  =======================================================================*/

include_once ("../include/mcglobal.inc.php");
include_once ("../include/auth.inc.php");

// Get the rights of the employee logged in and check the accessibility
if (!(authCheckEmployeeRights($emp_id, "0") || authCheckEmployeeRights($emp_id, "7"))) : die ("$PHP_SELF: Access denied!"); endif;

// Check HTTP-Parameters
getSecHttpVars("1",array("f_act", "generic", "genNum", "genJsParName", "f_mode", "companyId", "orderClause", "statusMessage", "f_cmp_authenticated", "f_searchmode", "f_cmp_match",
                         "f_cmp_comp", "f_cmp_comp2", "f_cs_eid", "f_usr_name", "f_usr_phone", "f_usr_email", "f_csc_name", "f_csc_is_extern",
                         "f_street", "f_hsno", "f_zipcode", "f_city", "f_country", "f_remark", "f_person", "f_phone", "tourno", "csc_id", "f_show_invisible",
                         "cscIdRoot", "customerId", "cscIdActual", "f_hq_id", "callOpenerFunction", "deactivateMenu", "allowAllCustomerTypes", "job_hq_id"));

// Execution-Time for script
set_time_limit(120);

// Select user-type for mode of security check
$userType = getFieldValueFromId("user","usr_id",$usr_id,"usr_type");
// $userTypeName = getUserTypeName($userType);

// Check authentication verifying emmployee an his/her costcenter- and customer-association
if ( !( ($userType == "1") || authCheck($hq_id,$usr_id,$emp_id,$cscIdRoot,$customerId,$cscIdActual) ) ) :
   die ("$PHP_SELF: Access denied!");
endif;

getLanguage(__FILE__);

$deactivateMenuStatic = "1";
$pageTitel = getLngt("KUNDENKOSTENSTELLEN");
include_once ("../admin/menu.php");
include_once ("../include/html.inc.php");
getCurrentScript(__FILE__);

$numOfRows = 0;
$htmlClass01 = "class=\"smaller\"";   // input,select
$tableOfRows = "";

if ($job_hq_id != "")
  $hq_id = $job_hq_id;

$hqId = 0;
  
// Mandator filter
if ($f_hq_id == "") : $f_hq_id = array(); endif;
if (count($f_hq_id) == 0) : array_push($f_hq_id, $hq_id); endif;

if ($f_searchmode == "") : $f_searchmode = "1"; endif;
if ($f_csc_is_extern == "") : $f_csc_is_extern = "0"; endif;

if ($genNum == "") : $genNum = ""; endif;   // "Generic number" to use more javascript parameters ("g_", "g2_" <=> genNum = "2", "g3_" <=> genNum = "3")
if ($genJsParName == "") : $genJsParName = ""; endif;   // Name of a special JS parameter name of the caller page, the value will be associated

mcTrim($f_cmp_comp); mcTrim($f_cmp_comp2); 
mcTrim($f_cmp_iln); mcTrim($f_cmp_tax_idno); mcTrim($f_cmp_bank); mcTrim($f_cmp_bankno); mcTrim($f_cmp_bankacc);


// Parameter for displayed column SEQUENCE
$displayedListCols = getParameterValue($emp_id, "MASK_CSCSC_LIST_COLS");
if ($displayedListCols == "") :
   $displayedListCols = getParameterValue("0", "MASK_CSCSC_LIST_COLS", $hqId);
   if ($displayedListCols == "") :
      // Default settings
      $displayedListCols = "cmp_comp,cmp_comp2,cs_eid,csc_name,cmp_match,cmp_authenticated_img,ad_street,cscad_hsno,ad_zipcode,ad_city,cscad_remark,cscad_person,cscad_phone,statistic";
   endif;
endif;
$displayedListColsArray = spliti(",",$displayedListCols);
$displayedListColsArrayLen = count($displayedListColsArray);

// Parameter for displayed column LENGTH
$displayedListLenCols = getParameterValue($emp_id, "MASK_CSCSC_LIST_LEN_COLS");
if ($displayedListLenCols == "") :
   $displayedListLenCols = getParameterValue("0", "MASK_CSCSC_LIST_LEN_COLS", $hqId);
   if ($displayedListLenCols == "") :
      // Default settings
      $displayedListLenCols = "30,15,11,20,30,5,20,5,5,20,20,20,15";
   endif;
endif;
$displayedListLenColsArray = spliti(",",$displayedListLenCols);


// Get constants for fields to be searched in
$constSearchCscsclistCmpcompCmpmatch = SEARCH_CSCSCLIST_CMPCOMP_CMPMATCH;
$constSearchCscsclistCmpcompCseid = SEARCH_CSCSCLIST_CMPCOMP_CSEID;
$constSearchCscsclistCmpcompCscadphone = SEARCH_CSCSCLIST_CMPCOMP_CSCADPHONE;

// Check consistence of used fields per JavaScript
$tableHeaderSearchFieldsExtra = "";    // Defines the extra hidden fields to be integrated for search
$j = array_search("cmp_match",$displayedListColsArray);
if ($j === FALSE) : $tableHeaderSearchFieldsExtra .= "<input type=\"hidden\" name=\"f_cmp_match\" value=\"\">"; endif;
$j = array_search("cs_eid",$displayedListColsArray);
if ($j === FALSE) : $tableHeaderSearchFieldsExtra .= "<input type=\"hidden\" name=\"f_cs_eid\" value=\"\">"; endif;
$j = array_search("cscad_phone",$displayedListColsArray);
if ($j === FALSE) : $tableHeaderSearchFieldsExtra .= "<input type=\"hidden\" name=\"f_phone\" value=\"\">"; endif;

$f_cmp_match = trim($f_cmp_match);
if ($constSearchCscsclistCmpcompCmpmatch == "1" && $f_cmp_match == "") : $f_cmp_match = trim($f_cmp_comp); endif;
$f_cs_eid = trim($f_cs_eid);
if ($constSearchCscsclistCmpcompCseid == "1" && $f_cs_eid == "") : $f_cs_eid = trim($f_cmp_comp); endif;
$f_phone = trim($f_phone);
if ($constSearchCscsclistCmpcompCscadphone == "1" && $f_phone == "") : $f_phone = trim($f_cmp_comp); endif;

$f_csc_name = trim($f_csc_name);
$f_usr_name = trim($f_usr_name);
$f_usr_phone = trim($f_usr_phone);
$f_usr_email = trim($f_usr_email);
$f_street = trim($f_street);
$f_hsno = trim($f_hsno);
$f_zipcode = trim($f_zipcode);
$f_city = trim($f_city);
$f_country = trim($f_country);
$f_remark = trim($f_remark);
$f_person = trim($f_person);
$f_phone = trim($f_phone);

// Flag for "show invisible customers"
if ($f_show_invisible == "" || count($f_show_invisible) == 0) :
   $f_show_invisible = "0";
else: 
   $f_show_invisible = "1";
endif;

// Check for displaying prospects. First check for option "permanently"
$displayedProspectsPermanently = getParameterValue("0", "MASK_CSCSC_LIST_SHOW_PROSPECTS_PERMANENTLY", $hqId);
if ($displayedProspectsPermanently == "1") :
   $allowAllCustomerTypes = "1";
else :
   if ($allowAllCustomerTypes == "" || (is_array($allowAllCustomerTypes) && count($allowAllCustomerTypes) == 0)) :
      $allowAllCustomerTypes = "0";
   else: 
      $allowAllCustomerTypes = "1";
   endif;
endif;

// Check for displaying prospects. First check for option "permanently"
$activateCscLinkForProspects = getParameterValue("0", "MASK_CSCSC_LIST_ACTIVATE_PROSPECT_LINKS_FOR_JOBS", $hqId);

$numOfHq = getCountOfTable("headquarters", "hq_disabled = 0");
$filterMandators = "<div style=\"float:left;\">&nbsp;" . getLngt("Niederlassungen:") . "&nbsp;&nbsp;" . addCheckboxesFromTable("f_hq_id","headquarters","hq_id","hq_mnemonic","hq_mnemonic","hq_disabled = 0",$f_hq_id, "&nbsp;&nbsp;&nbsp;","") . "</div\n>";
$filterMandators .= defineButtonType08("Alle", "f_hq_check_all", "hqCheckAll(" . $numOfHq . ");", "50px", "left", "3");
$filterMandators .= defineButtonType08("Keine", "f_hq_uncheck_all", "hqUncheckAll(" . $numOfHq . ");", "50px", "left");

// OUTPUT: Page header option fields
$pageHeaderOptionFields = "";
if (TRUE) :
   $pageHeaderOptionFields .= "<a href=\"javascript:document.forms[0].f_act.value='search';document.forms[0].submit();\">&nbsp;" . getLngt("Suchen") . "&nbsp;</a>\n";
   $pageHeaderOptionFields .= "&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;\n";
   $pageHeaderOptionFields .= "<a href=\"javascript:clearFields();\">" . getLngt("Felder&nbsp;zurücksetzen") . "</a>\n";
   $pageHeaderOptionFields .= "&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;\n";
   $pageHeaderOptionFields .= "Option:&nbsp;&nbsp;\n";
   $pageHeaderOptionFields .= "<input type=\"radio\" name=\"f_searchmode\" value=\"0\" " . ($f_searchmode == "0" ? "checked" : "") . ">&nbsp;" . getLngt("Präfix") . "\n";
   $pageHeaderOptionFields .= "<input type=\"radio\" name=\"f_searchmode\" value=\"1\" " . ($f_searchmode == "1" ? "checked" : "") . ">&nbsp;" . getLngt("Teilwort") . "\n";
   $pageHeaderOptionFields .= "&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;\n";
   // $pageHeaderOptionFields .= getLngt("Kostenstellen:") . "&nbsp;&nbsp;\n";
   // $pageHeaderOptionFields .= "<input type=\"radio\" name=\"f_csc_is_extern\" value=\"0\" " . ($f_csc_is_extern == "0" ? "checked" : "") . ">&nbsp;" . getLngt("Nur&nbsp;interne") . "&nbsp;\n";
   // $pageHeaderOptionFields .= "<input type=\"radio\" name=\"f_csc_is_extern\" value=\"1\" " . ($f_csc_is_extern == "1" ? "checked" : "") . ">&nbsp;" . getLngt("Nur&nbsp;externe") . "&nbsp;\n";
   // $pageHeaderOptionFields .= "<input type=\"radio\" name=\"f_csc_is_extern\" value=\"2\" " . ($f_csc_is_extern == "2" ? "checked" : "") . ">&nbsp;" . getLngt("Alle") . "&nbsp;\n";
   $pageHeaderOptionFields .= getLngt("Ausgeblendete") . "&nbsp;" . getLngt("anzeigen") . "&nbsp;\n";
   $pageHeaderOptionFields .= "<input type=\"checkbox\" name=\"f_show_invisible[]\" value=\"1\" " . ($f_show_invisible == "1" ? "checked" : "") . ">&nbsp;&nbsp;&nbsp;\n";
   $pageHeaderOptionFields .= getLngt("Mit Interessenten") . "&nbsp;\n";
   $pageHeaderOptionFields .= "<input type=\"checkbox\" name=\"allowAllCustomerTypes[]\" value=\"1\" " . ($allowAllCustomerTypes == "1" ? "checked" : "") . ">\n";
   $pageHeaderOptionFields .=  $filterMandators;
endif;


// OUTPUT: Table header search fields
$tableHeaderSearchFields = "";
if (TRUE) :
   $dspColHeaderSearchFieldArray["cmp_comp"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_cmp_comp\" value=\"" . $f_cmp_comp . "\" onBlur=\"javascript:checkSearchParameters('cmp_comp');\" _SIZE_MACRO_>";
   $dspColHeaderSearchFieldArray["cmp_comp2"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_cmp_comp2\" value=\"" . $f_cmp_comp2 . "\" _SIZE_MACRO_>";
   $dspColHeaderSearchFieldArray["cs_eid"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_cs_eid\" value=\"" . $f_cs_eid . "\" _SIZE_MACRO_>";
   $dspColHeaderSearchFieldArray["csc_name"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_csc_name\" value=\"" . $f_csc_name . "\" _SIZE_MACRO_>";
   $dspColHeaderSearchFieldArray["cmp_match"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_cmp_match\" value=\"" . $f_cmp_match . "\" _SIZE_MACRO_>";
   $dspColHeaderSearchFieldArray["cmp_authenticated_img"] = "<a href=\"javascript:document.forms[0].f_act.value='search';document.forms[0].submit();\">&nbsp;" . getLngt("Suchen") . "&nbsp;</a>";
   $dspColHeaderSearchFieldArray["usr_name"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_usr_name\" value=\"" . $f_usr_name . "\" _SIZE_MACRO_>";
   $dspColHeaderSearchFieldArray["usr_phone"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_usr_phone\" value=\"" . $f_usr_phone . "\" _SIZE_MACRO_>";
   $dspColHeaderSearchFieldArray["usr_email"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_usr_email\" value=\"" . $f_usr_email . "\" _SIZE_MACRO_>";
   $dspColHeaderSearchFieldArray["ad_street"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_street\" value=\"" . $f_street . "\" _SIZE_MACRO_>";
   $dspColHeaderSearchFieldArray["cscad_hsno"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_hsno\" value=\"" . $f_hsno . "\" _SIZE_MACRO_>";
   $dspColHeaderSearchFieldArray["ad_zipcode"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_zipcode\" value=\"" . $f_zipcode . "\" _SIZE_MACRO_>";
   $dspColHeaderSearchFieldArray["ad_city"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_city\" value=\"" . $f_city . "\" _SIZE_MACRO_>";
   $dspColHeaderSearchFieldArray["cscad_remark"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_remark\" value=\"" . $f_remark . "\" _SIZE_MACRO_>";
   $dspColHeaderSearchFieldArray["cscad_person"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_person\" value=\"" . $f_person . "\" _SIZE_MACRO_>";
   $dspColHeaderSearchFieldArray["cscad_phone"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_phone\" value=\"" . $f_phone . "\" _SIZE_MACRO_>";
   $dspColHeaderSearchFieldArray["statistic"] = "<a href=\"javascript:document.forms[0].f_act.value='search';document.forms[0].submit();\">&nbsp;" . getLngt("Suchen") . "&nbsp;</a>";
   $dspColHeaderSearchFieldArray["cs_report"] = "<a href=\"javascript:document.forms[0].f_act.value='search';document.forms[0].submit();\">&nbsp;" . getLngt("Suchen") . "&nbsp;</a>";

   $tmpKeys = array_keys($dspColHeaderSearchFieldArray);
   for ($i = 0; $i < $displayedListColsArrayLen; $i++) {
       // Search for the value "$displayedListColsArray" in "$dspColArray" and get the key (index)
       $j = array_search($displayedListColsArray[$i], $tmpKeys);
       if (!($j === FALSE)) :
   	      // Define length (html attribute "size") of the field and substitute macro
   	      mcArrIsSet($displayedListLenColsArray, $i);
   	      if ($displayedListLenColsArray[$i] == "" || $displayedListLenColsArray[$i] == "0") :
             $dspColHeaderSearchFieldArray[$tmpKeys[$j]] = str_replace("_SIZE_MACRO_", "", $dspColHeaderSearchFieldArray[$tmpKeys[$j]]);
   	      else :
   	         $dspColHeaderSearchFieldArray[$tmpKeys[$j]] = str_replace("_SIZE_MACRO_", " size=\"" . $displayedListLenColsArray[$i] . "\" ", $dspColHeaderSearchFieldArray[$tmpKeys[$j]]);
   	      endif;
          $cellColor = getListColor(1, 1);
          $tableHeaderSearchFields .= "<td bgcolor=\"" . $cellColor . "\">" . $dspColHeaderSearchFieldArray[$tmpKeys[$j]] . "</td>";
       endif;
   }
endif;

// OUTPUT: Table header column links
$tableHeaderLinks = "";
if (TRUE) :
   $dspColHeaderLinksArray = array();
   $dspColHeaderLinksArray["cmp_comp"] = "<a href=\"javascript:submitPage('cmp.cmp_comp');\">&nbsp;" . getLngt("Firma") . "&nbsp;</a>";
   $dspColHeaderLinksArray["cmp_comp2"] = "<a href=\"javascript:submitPage('cmp.cmp_comp2');\">&nbsp;" . getLngt("Firma2") . "&nbsp;</a>";
   $dspColHeaderLinksArray["cs_eid"] = "<a href=\"javascript:submitPage('cs.cs_eid');\">&nbsp;" . getLngt("ExtID") . "&nbsp;</a>";
   $dspColHeaderLinksArray["csc_name"] = "<a href=\"javascript:submitPage('csc.csc_name');\">&nbsp;" . getLngt("Kostenstelle") . "&nbsp;</a>";
   $dspColHeaderLinksArray["cmp_match"] = "<a href=\"javascript:submitPage('cmp.cmp_match');\">&nbsp;" . getLngt("Freitext") . "&nbsp;</a>";
   $dspColHeaderLinksArray["cmp_authenticated_img"] = "&nbsp;" . getLngt("Freigabe") . "&nbsp;";
   $dspColHeaderLinksArray["usr_name"] = "<a href=\"javascript:submitPage('usr.usr_name');\">&nbsp;" . getLngt("Admin-Name") . "</a>";
   $dspColHeaderLinksArray["usr_phone"] = "<a href=\"javascript:submitPage('usr.usr_phone');\">&nbsp;" . getLngt("Admin-Telefon") . "</a>";
   $dspColHeaderLinksArray["usr_email"] = "<a href=\"javascript:submitPage('usr.usr_email');\">&nbsp;" . getLngt("Admin-Email") . "</a>";
   $dspColHeaderLinksArray["ad_street"] = "<a href=\"javascript:submitPage('ad.ad_street');\">&nbsp;" . getLngt("Straße") . "</a>";
   $dspColHeaderLinksArray["cscad_hsno"] = "<a href=\"javascript:submitPage('cscad.cscad_hsno');\">&nbsp;" . getLngt("Hausnr.") . "</a>";
   $dspColHeaderLinksArray["ad_zipcode"] = "<a href=\"javascript:submitPage('ad.ad_zipcode');\">&nbsp;" . getLngt("PLZ") . "</a>";
   $dspColHeaderLinksArray["ad_city"] = "<a href=\"javascript:submitPage('ad.ad_city');\">&nbsp;" . getLngt("Ort") . "</a>";
   $dspColHeaderLinksArray["cscad_remark"] = "<a href=\"javascript:submitPage('cscad.cscad_remark');\">&nbsp;" . getLngt("Bemerkung") . "</a>";
   $dspColHeaderLinksArray["cscad_person"] = "<a href=\"javascript:submitPage('cscad.cscad_person');\">&nbsp;" . getLngt("Person") . "</a>";
   $dspColHeaderLinksArray["cscad_phone"] = "<a href=\"javascript:submitPage('cscad.cscad_phone');\">&nbsp;" . getLngt("Telefon") . "</a>";
   $dspColHeaderLinksArray["statistic"] = "&nbsp;" . getLngt("Statistik") . "&nbsp;";
   $dspColHeaderLinksArray["cs_report"] = "&nbsp;" . getLngt("Berichte") . "&nbsp;";

   $tmpKeys = array_keys($dspColHeaderLinksArray);
   for ($i = 0; $i < $displayedListColsArrayLen; $i++) {
       // Search for the value "$displayedListColsArray" in "$dspColArray" and get the key (index)
       $j = array_search($displayedListColsArray[$i], $tmpKeys);
       if (!($j === FALSE)) :
          $cellColor = getListColor(0, 0);
          $tableHeaderLinks .= "<td bgcolor=\"" . $cellColor . "\">" . $dspColHeaderLinksArray[$tmpKeys[$j]] . "</td>";
       endif;
   }
endif;


// if called e.g. by the first time with empty search-fields
$searchValues = $f_cmp_comp . $f_cmp_comp2 . $f_cmp_match . $f_cs_eid . $f_csc_name . $f_street . $f_hsno . $f_zipcode . $f_city . $f_country . $f_remark . $f_person . $f_phone;
// $searchValues .= $f_usr_name . $f_usr_phone . $f_usr_email;

   // Generate search-resultset
   if ($f_act == "search" && $searchValues != "") :

      if (strlen($f_cmp_comp) > 2 || strlen($f_cmp_comp2) > 2 || strlen($f_cmp_match) > 2 || strlen($f_cs_eid) > 2 || strlen($f_csc_name) > 0 ||
          strlen($f_street) > 2 || strlen($f_hsno) > 0 || strlen($f_zipcode) > 2 || strlen($f_city) > 2 ||
          strlen($f_remark) > 2 || strlen($f_person) > 2 || strlen($f_phone) > 2) :

         // *******************************************
         // * Selection of the customers for the list *
         // *******************************************
         if ($f_searchmode == "1") : $prefix = "%"; else : $prefix = ""; endif;
         $whereClause = "";

         // Check for search value in cmp.cmp_comp OR cmp.cmp_match
         if ($f_cmp_comp != "") :
            $whereClause .= "cmp.cmp_comp LIKE '" . $prefix . $f_cmp_comp . "%'";
            if (($constSearchCscsclistCmpcompCmpmatch == "1" && $f_cmp_match != "") || 
                ($constSearchCscsclistCmpcompCseid == "1" && $f_cs_eid != "") || 
                ($constSearchCscsclistCmpcompCscadphone == "1" && $f_phone != "")) :
               $whereClause = " (" . $whereClause;
               if ($constSearchCscsclistCmpcompCmpmatch == "1" && $f_cmp_match != "") :
                  $whereClause .= " OR cmp.cmp_match LIKE '" . $prefix . $f_cmp_match . "%'"; 
               endif;
               if ($constSearchCscsclistCmpcompCseid == "1" && $f_cs_eid != "") :
                  $whereClause .= " OR cs.cs_eid LIKE '" . $prefix . $f_cs_eid . "%'";
               endif;
               if ($constSearchCscsclistCmpcompCscadphone == "1" && $f_phone != "") :
                  $whereClause .= " OR cscad.cscad_phone LIKE '" . $prefix . $f_phone . "%'";
               endif;            
               $whereClause .= ") ";
            endif;
         endif;
   
         if ($whereClause != "" && $f_cmp_comp2 != "") : $whereClause .= " AND "; endif;
         if ($f_cmp_comp2 != "") : $whereClause .= "cmp.cmp_comp2 LIKE '" . $prefix . $f_cmp_comp2 . "%'"; endif;
         
         // Check for search value in cmp.cmp_comp OR cmp.cmp_match
         if ($constSearchCscsclistCmpcompCmpmatch != "1") :
            if ($whereClause != "" && $f_cmp_match != "") : $whereClause .= " AND "; endif;
            if ($f_cmp_match != "") : $whereClause .= "cmp.cmp_match LIKE '" . $prefix . $f_cmp_match . "%'"; endif;
         endif;
   
         // Check for search value in cmp.cmp_comp OR cs.cs_eid
         if ($constSearchCscsclistCmpcompCseid != "1") :
            if ($whereClause != "" && $f_cs_eid != "") : $whereClause .= " AND "; endif;
            if ($f_cs_eid != "") : $whereClause .= "cs.cs_eid LIKE '" . $prefix . $f_cs_eid . "%'"; endif;
         endif;
         
         // Check for search value in cmp.cmp_comp OR cscad.cscad_phone
         if ($constSearchCscsclistCmpcompCscadphone != "1") :
            if ($whereClause != "" && $f_phone != "") : $whereClause .= " AND "; endif;
            if ($f_phone != "") : $whereClause .= "cscad.cscad_phone LIKE '" . $prefix . $f_phone . "%'"; endif;
         endif;
               
         if ($whereClause != "" && $f_csc_name != "") : $whereClause .= " AND "; endif;
         if ($f_csc_name != "") : $whereClause .= "csc.csc_name LIKE '" . $prefix . $f_csc_name . "%'"; endif;
         // if ($whereClause != "" && $f_usr_name != "") : $whereClause .= " AND "; endif;
         // if ($f_usr_name != "") : $whereClause .= "usr.usr_name LIKE '" . $prefix . $f_usr_name . "%'"; endif;
         // if ($whereClause != "" && $f_usr_phone != "") : $whereClause .= " AND "; endif;
         // if ($f_usr_phone != "") : $whereClause .= "usr.usr_phone LIKE '" . $prefix . $f_usr_phone . "%'"; endif;
         // if ($whereClause != "" && $f_usr_email != "") : $whereClause .= " AND "; endif;
         // if ($f_usr_email != "") : $whereClause .= "usr.usr_email LIKE '" . $prefix . $f_usr_email . "%'"; endif;
         if ($whereClause != "" && $f_cmp_iln != "") : $whereClause .= " AND "; endif;
         if ($f_cmp_iln != "") : $whereClause .= "cmp.cmp_iln LIKE '" . $prefix . $f_cmp_iln . "%'"; endif;
         if ($whereClause != "" && $f_cmp_tax_idno != "") : $whereClause .= " AND "; endif;
         if ($f_cmp_tax_idno != "") : $whereClause .= "cmp.cmp_tax_idno LIKE '" . $prefix . $f_cmp_tax_idno . "%'"; endif;
         if ($whereClause != "" && $f_cmp_bank != "") : $whereClause .= " AND "; endif;
         if ($f_cmp_bank != "") : $whereClause .= "cmp.cmp_bank LIKE '" . $prefix . $f_cmp_bank . "%'"; endif;
         if ($whereClause != "" && $f_cmp_bankno != "") : $whereClause .= " AND "; endif;
         if ($f_cmp_bankno != "") : $whereClause .= "cmp.cmp_bankno LIKE '" . $prefix . $f_cmp_bankno . "%'"; endif;
         if ($whereClause != "" && $f_cmp_bankacc != "") : $whereClause .= " AND "; endif;
         if ($f_cmp_bankacc != "") : $whereClause .= "cmp.cmp_bankacc LIKE '" . $prefix . $f_cmp_bankacc . "%'"; endif;
         if ($whereClause != "" && $f_street != "") : $whereClause .= " AND "; endif;
         if ($f_street != "") : $whereClause .= "ad.ad_street LIKE '" . $f_street . "%'"; endif;
         if ($whereClause != "" && $f_hsno != "") : $whereClause .= " AND "; endif;
         if ($f_hsno != "") : $whereClause .= "cscad.cscad_hsno LIKE '" . $f_hsno . "%'"; endif;
         if ($whereClause != "" && $f_zipcode != "") : $whereClause .= " AND "; endif;
         if ($f_zipcode != "") : $whereClause .= "ad.ad_zipcode LIKE '" . $f_zipcode . "%'"; endif;
         if ($whereClause != "" && $f_city != "") : $whereClause .= " AND "; endif;
         if ($f_city != "") : $whereClause .= "ad.ad_city LIKE '" . $f_city . "%'"; endif;
         if ($whereClause != "" && $f_country != "") : $whereClause .= " AND "; endif;
         if ($f_country != "") : $whereClause .= "ad.ad_country LIKE '" . $f_country . "%'"; endif;
         if ($whereClause != "" && $f_remark != "") : $whereClause .= " AND "; endif;
         if ($f_remark != "") : $whereClause .= "cscad.cscad_remark LIKE '" . $f_remark . "%'"; endif;
         if ($whereClause != "" && $f_person != "") : $whereClause .= " AND "; endif;
         if ($f_person != "") : $whereClause .= "cscad.cscad_person LIKE '" . $f_person . "%'"; endif;
   
   
         // Check authentication
   // Enable again 4 cashMode (... damit die "eigentliche" Funktion des Sperrens wieder aufgenommen werden kann !!!!!!!!!!!!!!!!!!)
   //      if ($whereClause != "" && $f_cmp_authenticated == "1") : $whereClause .= " AND "; endif;
   //      if ($f_cmp_authenticated == "1") : $whereClause .= "cmp.cmp_authenticated LIKE '" . $f_cmp_authenticated . "%'"; endif;
   
         // Show "invisible" customers
         $whereClauseCscVisible = "";
         if ($f_show_invisible != "1") :
            if ($whereClause != "") : $whereClause .= " AND "; endif;
            $whereClause .= " cmp.cmp_visible = '1'";
            $whereClauseCscVisible = " csc.csc_visible = '1' AND ";
         endif;
   
         if ($whereClause != "") : $whereClause .= " AND "; endif;


         // if (!(strpos($displayedListCols, "cmp_comp,") === FALSE)) :
            $constCscscListOrderBy = getParameterValue("0", "MASK_CSCSC_LIST_ORDER_BY", $hqId);
            if ($orderClause == "") :
               $orderClause = "cmp.cmp_comp";
               if ($constCscscListOrderBy == "1") :
                  if (!(strpos($displayedListCols, "csc_name,") === FALSE)) :
                     $orderClause .= ", csc.csc_name";
                  endif;
               endif;
            endif;
         // endif;         

         $whereClause2 = "";
   
         // Internal/External/All costcenters
         if ($f_csc_is_extern == "0") : $whereClause2 .= " AND csc.csc_is_extern = '0' "; endif;
         if ($f_csc_is_extern == "1") : $whereClause2 .= " AND csc.csc_is_extern = '1' "; endif;
         
         // filter csc_id_payer == CSC_ID_PAYER_CASH (real payer is cash payer) 
         // if ($tourno == -1) : $whereClause2 .= " AND cs.csc_id_payer != '" . CSC_ID_PAYER_CASH . "' "; endif;

         // Filter according to the customer type
         $whereClauseCustomerType = " cmp.cmp_type = '0' AND";
         if ($allowAllCustomerTypes == "1") :
            $whereClauseCustomerType = "";
         endif;

         $sqlquery = "SELECT csc.csc_id, csc.csc_name, csc.csc_is_extern, cs.cs_id, cs.cs_eid, cs.cs_eid AS cs_report, cs.csc_id_payer, cs.cs_id_parent, cs.cs_commission_no, cs.cs_jobbatch, cs.cs_discount, cs.cs_filter, cs.cs_fixprice_discount, "
                      . "    cs.cs_prov, cs.cs_jb_jam_waittime, cs.cs_tracking, cs.cs_invmode, cmp.cmp_id, cmp.cmp_type, cmp.cmp_comp, cmp.cmp_comp2, cmp.cmp_authenticated, cmp.cmp_remark, cmp.cmp_match, cmp.cmp_cashmode, cmp.cmp_stax_idno,"
                      . "    cmp.tx_id, cscad.ad_id, cscad.cscad_comp, cscad.cscad_comp2, cscad.cscad_hsno,"
                      . "    cscad.cscad_remark, cscad.cscad_person, cscad.cscad_phone,"
                      . "    ad.ad_street, ad.ad_zipcode, ad.ad_city, ad.ad_country, cmp2.cmp_comp AS payer_comp, cmp2.cmp_stax_idno AS payer_cmp_stax_idno, cs2.cs_commission_no AS payer_cs_commission_no, cs.cs_eid AS statistic"
                      . " FROM company AS cmp, costcenter AS csc, customer AS cs"
                      . "      LEFT JOIN costcenter AS csc2 ON csc2.csc_id = cs.csc_id_payer"
                      . "      LEFT JOIN customer AS cs2    ON cs2.cs_id = csc2.cs_id"
                      . "      LEFT JOIN company AS cmp2    ON cmp2.cmp_id = cs2.cmp_id,"
                      . "      costcenteraddress AS cscad, address AS ad"
                      . " WHERE " .$whereClause
                      . "       cs.hq_id IN " . getSQLMandatorArray($f_hq_id) . " AND"
                      . "       cmp.cmp_id = cs.cmp_id        AND"
                      . "       cmp.cmp_archived = '0'        AND"
                      . "       " . $whereClauseCustomerType
                      . "       cs.cs_id = csc.cs_id             "
                      . "       " . $whereClause2 . "         AND"
                      . "       " . $whereClauseCscVisible
                      . "       csc.csc_id = cscad.csc_id     AND"
                      . "       cscad.adt_id = '4'            AND"
                      . "       cscad.ad_id = ad.ad_id"
                      . " ORDER BY " . $orderClause;                  
// echo $sqlquery . "<br>";
         getDb2Connection();     // Try to connect request server because of performance
         // $result = $db2->query($sqlquery);
         // if (DB::isError($result)) die ("$PHP_SELF: '$sqlquery': " . $result->getMessage());         
         $result = $db2->dbQ($sqlquery);

   	     // Table with header
   	     $rowCounter = 0;
   	     $lineToggler = 0;
   	     while ($row = $result->fetch_assoc()):
   	           $numOfRows++;
   	           $dspColArray = array();

   	           $tableOfRows .= "<tr>";

               $dspColArray["cmp_comp"] = "<td__BGCOL__>&nbsp;__START__" . $row["cmp_comp"] . "__END__</td>";
               $dspColArray["cmp_comp2"] = "<td__BGCOL__>&nbsp;__START__" . $row["cmp_comp2"] . "__END__</td>";
               if ($allowAllCustomerTypes == "1" && $row["cmp_type"] != "0") :
                  $dspColArray["cs_eid"] = "<td__BGCOL__>&nbsp;<a href=\"javascript:openCustomerSpecial('" . ec($row["cmp_id"]) . "');\">" . $row["cs_eid"] . "</a>" . "</td>";
               else :
                  $dspColArray["cs_eid"] = "<td__BGCOL__>&nbsp;__START__" . $row["cs_eid"] . "__END__</td>";
               endif;

               $payer_comp = "";
               $payer_cmp_stax_idno = "";
               $payer_cs_commission_no = 0;
               if ($row["csc_id_payer"] != "" && $row["csc_id_payer"] != 0):
                  // $payer_comp = $db->getOne("SELECT cmp_comp FROM company, customer, costcenter WHERE company.cmp_id = customer.cmp_id AND customer.cs_id = costcenter.cs_id AND costcenter.csc_id = " . $row["csc_id_payer"]);
                  $payer_comp = $row["payer_comp"];
                  $payer_cmp_stax_idno = $row["payer_cmp_stax_idno"];
                  $payer_cs_commission_no = $row["payer_cs_commission_no"];                  
               endif;
               if (trim($row["cscad_comp"] == ""))
                  $row["cscad_comp"] = $row["cmp_comp"];
               $cs_comm_prefix = $db->getOne("SELECT par_value FROM parameter WHERE par_key = 'MASK_COMM_PREFIX_" . $row["cs_id"] . "'");
               $cs_jb_jam_waittime = $row["cs_jb_jam_waittime"];
               if ($cs_jb_jam_waittime > 0) {
                 $cs_jb_jam_waittime = $db->getOne("SELECT cs_jb_jam_waittime_minutes from customer WHERE cs_id = " . $row["cs_id"]);
                 if ($cs_jb_jam_waittime == 0) {
                   $cs_jb_jam_waittime = getParameterValue("0", "CS_JB_JAM_WAITTIME_AD_HOC_DEFAULT", "0");
                 }
               }
               $cs_invmail = $db->getOne("SELECT usr_inv_email from customer, employee, user WHERE cs_id = " . $row["cs_id"] . " AND customer.cs_admin = employee.emp_id AND employee.usr_id = user.usr_id");
               $cs_jb_incomplete = $db->getOne("SELECT par_value FROM parameter WHERE par_key = 'CUSTOMER_MASK_JB_INCOMPLETE_" . $row["cs_id"] . "'");

               // No Link for entries where csc_id_payer (i.e. real payer) is cash-payer (only when selection for payer only ($tourno == -1))
//               if ($tourno == -1 && $row["csc_id_payer"] == CSC_ID_PAYER_CASH) {
               if (($tourno == -1 && $row["cmp_authenticated"] != "1") || ($allowAllCustomerTypes == "1" && $row["cmp_type"] != "0" && $activateCscLinkForProspects != "1")) {
                 $dspColArray["csc_name"] = "<td__BGCOL__>&nbsp;__START__" . my_str_check_html($row["csc_name"]) . "__END__</a></td>";
               } else {
                  $dspColArray["csc_name"] = "<td__BGCOL__>&nbsp;<a href=\"javascript:finishPage('" . $row["cs_id"] . "','" . $row["csc_id"] . "','" . my_str_check_js($row["csc_name"]) . "','" . my_str_check_js($row["cscad_comp"]) . "','" . my_str_check_js($row["cscad_comp2"]) . "','" . my_str_check_js($row["ad_street"]) . "','" . $row["cscad_hsno"] . "','" . $row["ad_zipcode"] . "','" . my_str_check_js($row["ad_city"]) . "','" . my_str_check_js($row["ad_country"]) . "','" . my_str_check_js($row["cscad_remark"]) . "','" . my_str_check_js($row["cscad_person"]) . "','" . $tourno . "','" . my_str_check_js($row["csc_id_payer"]) . "','" . my_str_check_js($payer_comp) . "','" . $row["tx_id"] . "','" . $row["cs_eid"]  . "','" . $row["cs_commission_no"] . "','" . $row["cmp_authenticated"]  . "','" . my_str_check_js($row["cmp_remark"]) . "','" . $row["cs_jobbatch"] . "','" . $row["cs_discount"] . "','" . $row["cs_filter"] . "','" . my_str_check_js($row["cmp_comp"]) . "','" . my_str_check_js($row["cmp_comp2"]) . "','" . $row["cs_id_parent"] . "'," . ($row["csc_is_extern"] == 0 && $row["cmp_type"] == 0 ? "0" : "1") . "," . $row["cs_fixprice_discount"] . "," . $row["cs_prov"] . ",'" . my_str_check_js($row["cmp_stax_idno"]) . "','" . my_str_check_js($payer_cmp_stax_idno) . "'," . $payer_cs_commission_no . ",'" . $cs_comm_prefix . "'," . $cs_jb_jam_waittime . "," . $row["cs_tracking"] . ",'" . $cs_invmail . "', '" . $cs_jb_incomplete . "', " . $row["cs_invmode"] . ");\">__START__" . my_str_check_html($row["csc_name"]) . "__END__</a></td>";
               }

               $dspColArray["cmp_match"] = "<td__BGCOL__>&nbsp;__START__" . $row["cmp_match"] . "__END__</td>";
         
               $authImgName = "circle_red.png";
   	           if ($row["cmp_authenticated"] == "1") : $authImgName = "circle_green.png"; endif;
   	           if ($allowAllCustomerTypes == "1") :
                  if ($row["cmp_type"] == "1") : $authImgName = "circle_lightblue.png"; endif;
                  if ($row["cmp_type"] == "2") : $authImgName = "circle_lightblue.png"; endif;
                  if ($row["cmp_type"] == "3") : $authImgName = "circle_lightblue.png"; endif;
               endif;
   	           $dspColArray["cmp_authenticated_img"] = "<td align=\"center\"__BGCOL__><img src=\"../images/" . $authImgName . "\" border=\"0\" height=\"10\" width=\"25\"></td>";
         
   	           $cashmodeImgName = "circle_green.png";
   	           if ($row["cmp_cashmode"] == "1") : $cashmodeImgName = "circle_red.png"; endif;
               // Enable 4 cashMode
               // $dspColArray["usr_name"] = "<td align=\"center\"__BGCOL__><img src=\"../images/" . $cashmodeImgName . "\" border=\"0\" height=\"10\" width=\"25\"></td>";
         
               // $dspColArray["usr_name"] = "<td__BGCOL__>&nbsp;__START__" . $row["usr_name"] . "__END__</td>";
               // $dspColArray["usr_phone"] = "<td__BGCOL__>&nbsp;__START__" . $row["usr_phone"] . "__END__</td>";
               // $dspColArray["usr_email"] = "<td__BGCOL__>&nbsp;__START__" . $row["usr_email"] . "__END__</td>";
               $dspColArray["ad_street"] = "<td__BGCOL__>&nbsp;__START__" . $row["ad_street"] . "__END__</td>";
               $dspColArray["cscad_hsno"] = "<td__BGCOL__>&nbsp;__START__" . $row["cscad_hsno"] . "__END__</td>";
               $dspColArray["ad_zipcode"] = "<td__BGCOL__>&nbsp;__START__" . $row["ad_zipcode"] . "__END__</td>";
               $dspColArray["ad_city"] = "<td__BGCOL__>&nbsp;__START__" . $row["ad_city"] . "__END__</td>";
               $dspColArray["cscad_remark"] = "<td__BGCOL__>&nbsp;__START__" . $row["cscad_remark"] . "__END__</td>";
               $dspColArray["cscad_person"] = "<td__BGCOL__>&nbsp;__START__" . $row["cscad_person"] . "__END__</td>";
               $dspColArray["cscad_phone"] = "<td__BGCOL__>&nbsp;__START__" . $row["cscad_phone"] . "__END__</td>";
               // $dspColArray["statistic"] = "<td__BGCOL__>&nbsp;<a href=\"javascript:openCustomerStatistics('" . $row["statistic"] . "');\">" . getLngt("Statistik") . "</a></td>";
               // $dspColArray["cs_report"] = "<td__BGCOL__>&nbsp;<a href=\"javascript:openCustomerReports('" . $row["cs_eid"] . "');\">" . getLngt("Berichte") . "</a></td>";
               $dspColArray["statistic"] = "<td__BGCOL__>&nbsp;<a href=\"javascript:openCustomerStatistics('" . $row["statistic"] . "');\"><img src=\"../images/arrow_right.png\" border=\"0\" height=\"10\" width=\"25\"></a></td>";
               $dspColArray["cs_report"] = "<td__BGCOL__>&nbsp;<a href=\"javascript:openCustomerReports('" . $row["cs_eid"] . "');\"><img src=\"../images/arrow_right.png\" border=\"0\" height=\"10\" width=\"25\"></a></td>";

               // Generate list
               $rowCounter++;
               $tmpKeys = array_keys($dspColArray);
               for ($i = 0; $i < $displayedListColsArrayLen; $i++) {
                   // Search for the value "$displayedListColsArray" in "$dspColArray" and get the key (index)
                   $j = array_search($displayedListColsArray[$i], $tmpKeys);
                   if (!($j === FALSE)) :
         
   	                  // Define length (html attribute "size") of the field and substitute macro
   	                  if ($displayedListLenColsArray[$i] == "" || $displayedListLenColsArray[$i] == "0") :
                         $dspColArray[$tmpKeys[$j]] = str_replace("__START__", "", $dspColArray[$tmpKeys[$j]]);
                         $dspColArray[$tmpKeys[$j]] = str_replace("__END__", "", $dspColArray[$tmpKeys[$j]]);
   	                  else :
                         // content of db-fields
                         $tagBegin = "__START__";
                         $tagEnd = "__END__";
                         if (!(strpos($dspColArray[$tmpKeys[$j]], $tagBegin) === FALSE) && !(strpos($dspColArray[$tmpKeys[$j]], $tagEnd) === FALSE)) :
                         
                            $pos0 = strpos($dspColArray[$tmpKeys[$j]], $tagBegin);                         // begin of tagBegin (first occurrence)
                            $pos1 = strpos($dspColArray[$tmpKeys[$j]], $tagBegin) + strlen($tagBegin);     // end of tagBegin   (first occurrence)
                            $pos2 = strpos($dspColArray[$tmpKeys[$j]], $tagEnd);                           // begin of tagEnd   (first occurrence)
                            $pos3 = strpos($dspColArray[$tmpKeys[$j]], $tagEnd) + strlen($tagEnd);         // end of tagEnd     (first occurrence)
                            
                            if ($pos2 - $pos1 > 0) :
                               // first get the content of the tag
                               $fieldContent = substr($dspColArray[$tmpKeys[$j]], $pos1, $pos2 - $pos1);
                         
                               $fieldContent = substr($fieldContent, 0, $displayedListLenColsArray[$i]);
         
                               // replace original content with string-content
                               $dspColArray[$tmpKeys[$j]] = substr_replace($dspColArray[$tmpKeys[$j]], $fieldContent, $pos0, $pos3 - $pos0);
   	                        endif;
   	                     endif;
   	                  endif;
   	                  
   	                  if ($lineToggler == 0) : $lineToggler = 1; else : $lineToggler = 0; endif;
                      $cellColor = getListColor($rowCounter, $lineToggler);

                      $dspColArray[$tmpKeys[$j]] = str_replace("__START__", "", $dspColArray[$tmpKeys[$j]]);
                      $dspColArray[$tmpKeys[$j]] = str_replace("__END__", "", $dspColArray[$tmpKeys[$j]]);
                      $dspColArray[$tmpKeys[$j]] = str_replace("__BGCOL__", " bgcolor=\"" . $cellColor ."\"", $dspColArray[$tmpKeys[$j]]);

                      $tableOfRows .= $dspColArray[$tmpKeys[$j]] . "\n";
                   endif;
               }
               
               if ($lineToggler == 0) : $lineToggler = 1; else : $lineToggler = 0; endif;
   	           $tableOfRows .= "</tr>\n";
   	     endwhile;
   	     $result->free();

      else :
        $statusMessage = getLngt("Bei Eingabe weniger als 3 Zeichen in mindestens einem Feld erfolgt keine Suche! Mind. ein Zeichen bei Kostenstelle reicht aus!");
      endif;
   endif;
?>

<html>

<head>
<title><?php echo $pageTitel ?></title>

<link rel="stylesheet" type="text/css" href="../css/phoenix.css">
<style type="text/css">
  <?php include_once ("../css/navigation.css.php"); ?>
</style>

<?php include_once ("../include/js_framework.inc.php"); ?>

<script type="text/javascript">
<!--
// NAVIGATION
<?php echo $jsMenuOut; ?>

<?php if ($generic == "") : ?>
function finishPage(cs_id,csc_id,name,comp,comp2,street,hsno,zipcode,city,country,remark,person,tourno,csc_id_payer,payer_comp,tx_id,cs_eid,cs_commission_no,cmp_authenticated,cmp_remark,cs_jobbatch,cs_discount,cs_filter,cmp_comp,cmp_comp2,cs_id_parent,is_extern,cs_fixprice_discount,cs_prov,cmp_stax_idno,payer_cmp_stax_idno,payer_cs_commission_no,cs_comm_prefix,cs_jb_jam_waittime,cs_tracking,cs_invmail,cs_jb_incomplete,cs_invmode) {
  if (tourno > -1)
  {
    opener.setJobList(tourno, opener.jl_csc_id, csc_id);
    opener.setJobList(tourno, opener.jl_tr_comp, comp);
    opener.setJobList(tourno, opener.jl_tr_person, person);
    opener.setJobList(tourno, opener.jl_ad_street, street);
    opener.setJobList(tourno, opener.jl_tr_hsno, hsno);
    opener.setJobList(tourno, opener.jl_ad_zipcode, zipcode);
    opener.setJobList(tourno, opener.jl_ad_city, city);
    opener.setJobList(tourno, opener.jl_ad_country, country);
    opener.setJobList(tourno, opener.jl_tr_remark, remark);
    opener.setJobList(tourno, opener.jl_csc_id_payer, "false");
    opener.setJobList(tourno, opener.jl_hide_tr_remark, "true");
    if (csc_id_payer != "" && csc_id_payer != 0)
    {
      opener.setCsc_id_payer(csc_id_payer, payer_comp);
//      opener.parent.job_options.document.tourOptions.payer_is_default.value = "true";
      if (csc_id != csc_id_payer)
        opener.add_cs_eid(csc_id_payer, "", payer_cs_commission_no, payer_comp, "", "", "", "", "", "", "", "", "", "", "", payer_cmp_stax_idno, cs_comm_prefix, cs_jb_jam_waittime, cs_tracking, cs_invmail, cs_jb_incomplete, cs_invmode);
    }
  } else {
    opener.setCsc_id_payer(csc_id, comp);

    if (cs_tracking == 1) {
      opener.parent.job_tour.document.getElementById('tr_tracking_row').style.visibility = 'visible';
	  if (opener.getCurTourNo() > 0) {
        opener.parent.job_tour.document.getElementById('tr_tracking_von').style.visibility = 'visible';
      } else {
        opener.parent.job_tour.document.getElementById('tr_tracking_von').style.visibility = 'collapse';
      }
      opener.parent.job_tour.document.getElementById('tr_tracking_nach').style.visibility = 'visible';
    } else {
      opener.parent.job_tour.document.getElementById('tr_tracking_row').style.visibility = 'collapse';
      opener.parent.job_tour.document.getElementById('tr_tracking_nach').style.visibility = 'collapse';
      opener.parent.job_tour.document.getElementById('tr_tracking_von').style.visibility = 'collapse';
    }
<?php if (getParameterValue("0", "MASK_COPY_REMARK_OF PAYER_2_TR_REMARK", $hqId) == "1") : ?>
    // 26.10.2019: Die Drittzahlerbemerkung soll angehängt werden
    var cur_tr_remark = opener.getJobList(0, opener.jl_tr_remark);
    if (cur_tr_remark != "" && cur_tr_remark.substring(cur_tr_remark.length - 1) != "\n") {
      cur_tr_remark = cur_tr_remark + "\n";
    }
    var remString = "*** BEMERKUNG ZAHLER ***\n";
    var pos_remString = cur_tr_remark.indexOf(remString);
    if (pos_remString > -1) {
      cur_tr_remark = cur_tr_remark.substring(0, pos_remString);
    }
    opener.setJobList(0, opener.jl_tr_remark, cur_tr_remark + (remark != "" && cur_tr_remark != remark ? remString : "") + (cur_tr_remark != remark ? remark : ""));
<?php endif; ?>
    if (tx_id != "" && tx_id != 0)
    {
      opener.checkJb_sales_tax_rate_sign(tx_id);
      opener.checkCs_filter(cs_filter, cs_jb_incomplete);
    }
    opener.parent.job_options.jb_markup_overwrite = 1;
<?php if (MASK_CASH_PAYER_SELECT == "1") : ?>
    opener.parent.job_tour.document.tourForm.jb_costsplit.checked = true;
    opener.checkCostsplit(true);    
<?php endif; ?>
  }
  if (cs_eid != "" && cs_eid != 0)
  {
    opener.add_cs_eid(csc_id, cs_eid, cs_commission_no, comp, cmp_authenticated, cmp_remark, cs_jobbatch, cs_discount, cs_filter, tx_id, name, is_extern, cs_id, cs_fixprice_discount, cs_prov, cmp_stax_idno, cs_comm_prefix, cs_jb_jam_waittime, cs_tracking, cs_invmail, cs_jb_incomplete, cs_invmode);
    opener.checkFixprice(0);
  }
  self.setTimeout("opener.refreshFormFields()", 100);
  self.setTimeout("opener.resetSignificantPriceValues()", 150);
//  opener.refreshFormFields();
  self.setTimeout("opener.writeJob()", 200);
//  opener.checkSendTour();
  self.setTimeout("self.close()", 300);
//  self.close();
};
<?php else : ?>
// GENERIC function

  <?php if ($genJsParName == "") : ?>
function finishPage(cs_id,csc_id,name,comp,comp2,street,hsno,zipcode,city,country,remark,person,tourno,csc_id_payer,payer_comp,tx_id,cs_eid,cs_commission_no,cmp_authenticated,cmp_remark,cs_jobbatch,cs_discount,cs_filter,cmp_comp,cmp_comp2,cs_id_parent,is_extern,cs_fixprice_discount,cs_prov,cmp_stax_idno,payer_cmp_stax_idno,payer_cs_commission_no) {
    
      <?php if (substr($generic, 0,1) == "1") : ?> opener.document.forms[0].g<?php echo $genNum; ?>_cs_id.value = cs_id;         <?php endif; ?>
      <?php if (substr($generic, 1,1) == "1") : ?> opener.document.forms[0].g<?php echo $genNum; ?>_csc_id.value = csc_id;       <?php endif; ?>
      <?php if (substr($generic, 2,1) == "1") : ?> opener.document.forms[0].g<?php echo $genNum; ?>_csc_name.value = name;       <?php endif; ?>
      <?php if (substr($generic, 3,1) == "1") : ?> opener.document.forms[0].g<?php echo $genNum; ?>_cscad_comp.value = comp;     <?php endif; ?>
      <?php if (substr($generic, 4,1) == "1") : ?> opener.document.forms[0].g<?php echo $genNum; ?>_cscad_comp2.value = comp2;   <?php endif; ?>
      <?php if (substr($generic, 5,1) == "1") : ?> opener.document.forms[0].g<?php echo $genNum; ?>_ad_street.value = street;    <?php endif; ?>
      <?php if (substr($generic, 6,1) == "1") : ?> opener.document.forms[0].g<?php echo $genNum; ?>_cscad_hsno.value = hsno;     <?php endif; ?>
      <?php if (substr($generic, 7,1) == "1") : ?> opener.document.forms[0].g<?php echo $genNum; ?>_ad_zipcode.value = zipcode;  <?php endif; ?>
      <?php if (substr($generic, 8,1) == "1") : ?> opener.document.forms[0].g<?php echo $genNum; ?>_ad_city.value = city;        <?php endif; ?>
      <?php if (substr($generic, 9,1) == "1") : ?> opener.document.forms[0].g<?php echo $genNum; ?>_ad_country.value = country;  <?php endif; ?>
      <?php if (substr($generic,10,1) == "1") : ?> opener.document.forms[0].g<?php echo $genNum; ?>_cscad_remark.value = remark; <?php endif; ?>
      <?php if (substr($generic,11,1) == "1") : ?> opener.document.forms[0].g<?php echo $genNum; ?>_cscad_person.value = person; <?php endif; ?>
      <?php if (substr($generic,12,1) == "1") : ?> opener.document.forms[0].g<?php echo $genNum; ?>_cs_eid.value = cs_eid;       <?php endif; ?>
      <?php if (substr($generic,13,1) == "1") : ?> opener.document.forms[0].g<?php echo $genNum; ?>_cmp_comp.value = cmp_comp;   <?php endif; ?>
      <?php if (substr($generic,14,1) == "1") : ?> opener.document.forms[0].g<?php echo $genNum; ?>_cmp_comp2.value = cmp_comp2; <?php endif; ?>
      <?php if (substr($generic,15,1) == "1") : ?> opener.document.forms[0].g<?php echo $genNum; ?>_cs_id_parent.value = cs_id_parent; <?php endif; ?>
      <?php if (substr($generic,16,1) == "1") : ?> opener.document.forms[0].g<?php echo $genNum; ?>_cs_discount.value = cs_discount; <?php endif; ?>
      
      // Call opener function
      <?php if ($callOpenerFunction != "") : ?> opener.<?php echo $callOpenerFunction; ?>; <?php endif; ?>
    
      self.close();
    };

<?php else : ?>

    // Function ONLY if a special JS parameter name of the caller page is defined in $genJsParName
    function finishPage(cs_id,csc_id,name,comp,comp2,street,hsno,zipcode,city,country,remark,person,tourno,csc_id_payer,payer_comp,tx_id,cs_eid,cs_commission_no,cmp_authenticated,cmp_remark,cs_jobbatch,cs_discount,cs_filter,cmp_comp,cmp_comp2,cs_id_parent,is_extern,cs_fixprice_discount,cs_prov,cmp_stax_idno,payer_cmp_stax_idno,payer_cs_commission_no) {
    
      <?php if (substr($generic, 0,1) == "1") : ?> opener.document.forms[0].<?php echo $genJsParName; ?>.value = cs_id;        <?php endif; ?>
      <?php if (substr($generic, 1,1) == "1") : ?> opener.document.forms[0].<?php echo $genJsParName; ?>.value = csc_id;       <?php endif; ?>
      <?php if (substr($generic, 2,1) == "1") : ?> opener.document.forms[0].<?php echo $genJsParName; ?>.value = name;         <?php endif; ?>
      <?php if (substr($generic, 3,1) == "1") : ?> opener.document.forms[0].<?php echo $genJsParName; ?>.value = comp;         <?php endif; ?>
      <?php if (substr($generic, 4,1) == "1") : ?> opener.document.forms[0].<?php echo $genJsParName; ?>.value = comp2;        <?php endif; ?>
      <?php if (substr($generic, 5,1) == "1") : ?> opener.document.forms[0].<?php echo $genJsParName; ?>.value = street;       <?php endif; ?>
      <?php if (substr($generic, 6,1) == "1") : ?> opener.document.forms[0].<?php echo $genJsParName; ?>.value = hsno;         <?php endif; ?>
      <?php if (substr($generic, 7,1) == "1") : ?> opener.document.forms[0].<?php echo $genJsParName; ?>.value = zipcode;      <?php endif; ?>
      <?php if (substr($generic, 8,1) == "1") : ?> opener.document.forms[0].<?php echo $genJsParName; ?>.value = city;         <?php endif; ?>
      <?php if (substr($generic, 9,1) == "1") : ?> opener.document.forms[0].<?php echo $genJsParName; ?>.value = country;      <?php endif; ?>
      <?php if (substr($generic,10,1) == "1") : ?> opener.document.forms[0].<?php echo $genJsParName; ?>.value = remark;       <?php endif; ?>
      <?php if (substr($generic,11,1) == "1") : ?> opener.document.forms[0].<?php echo $genJsParName; ?>.value = person;       <?php endif; ?>
      <?php if (substr($generic,12,1) == "1") : ?> opener.document.forms[0].<?php echo $genJsParName; ?>.value = cs_eid;       <?php endif; ?>
      <?php if (substr($generic,13,1) == "1") : ?> opener.document.forms[0].<?php echo $genJsParName; ?>.value = cmp_comp;     <?php endif; ?>
      <?php if (substr($generic,14,1) == "1") : ?> opener.document.forms[0].<?php echo $genJsParName; ?>.value = cmp_comp2;    <?php endif; ?>
      <?php if (substr($generic,15,1) == "1") : ?> opener.document.forms[0].<?php echo $genJsParName; ?>.value = cs_id_parent; <?php endif; ?>
      <?php if (substr($generic,16,1) == "1") : ?> opener.document.forms[0].<?php echo $genJsParName; ?>.value = cs_discount;  <?php endif; ?>
      
      // Call opener function
      <?php if ($callOpenerFunction != "") : ?> opener.<?php echo $callOpenerFunction; ?>; <?php endif; ?>
    
      self.close();
    };

  <?php endif; ?>
<?php endif; ?>

function clearFields() {
  document.forms[0].f_cmp_comp.value = '';
  if (document.forms[0].f_cmp_comp2) {document.forms[0].f_cmp_comp2.value = ''};
  if (document.forms[0].f_cmp_match) {document.forms[0].f_cmp_match.value = ''};
  if (document.forms[0].f_cs_eid) {document.forms[0].f_cs_eid.value = ''};
  if (document.forms[0].f_csc_name) {document.forms[0].f_csc_name.value = ''};
  if (document.forms[0].f_street) {document.forms[0].f_street.value = ''};
  if (document.forms[0].f_hsno) {document.forms[0].f_hsno.value = ''};
  if (document.forms[0].f_zipcode) {document.forms[0].f_zipcode.value = ''};
  if (document.forms[0].f_city) {document.forms[0].f_city.value = ''};
  if (document.forms[0].f_remark) {document.forms[0].f_remark.value = ''};
  if (document.forms[0].f_person) {document.forms[0].f_person.value = ''};
  if (document.forms[0].f_phone) {document.forms[0].f_phone.value = ''};
};

function checkSearchParameters(field_from) {
  if (field_from == 'cmp_comp') {
     // Check for search value in cmp_name to be also searched in cmp_match
     if (<?php echo $constSearchCscsclistCmpcompCmpmatch ?> == 1) {
     	document.forms[0].f_cmp_match.value = document.forms[0].f_cmp_comp.value;
     }
     if (<?php echo $constSearchCscsclistCmpcompCseid ?> == 1) {
     	document.forms[0].f_cs_eid.value = document.forms[0].f_cmp_comp.value;
     }
     if (<?php echo $constSearchCscsclistCmpcompCscadphone ?> == 1) {
     	document.forms[0].f_phone.value = document.forms[0].f_cmp_comp.value;
     }     
  }
};

function submitPage(orderClause) {
  document.forms[0].orderClause.value = orderClause;
  document.forms[0].f_act.value='search';
  document.forms[0].submit();
};

function openCustomerStatistics(csEid) {
  var widthPopupWin = 900;
  var heightPopupWin = 700;
  var leftPopupWin = (screen.width / 2) - (widthPopupWin / 2) - 12;
  var topPopupWin = (screen.height / 2) - (heightPopupWin / 2) - 50;
  var popupWin;
  popupWin = window.open("../statistic/statistic.php?f_act=calculate&f_category=3&f_statistic=61&g_cs_eid="+csEid+"&noStatisticMenu=1&deactivateMenu=1&outputColumnMode=0&outputTitle=Monatsübersicht&specialAligns=l,l,l,l,r&day_from=01&month_from=01&year_from=<?php echo getDateTime('lastyear'); ?>&day_to=<?php echo getDateTime('day'); ?>&month_to=<?php echo getDateTime('month'); ?>&year_to=<?php echo getDateTime('year'); ?>","","dependent=yes,width=" + widthPopupWin + ",height=" + heightPopupWin +",left=" + leftPopupWin + ",top=" + topPopupWin + ",scrollbars=yes");
};

function openCustomerReports(csEid) {
  var widthPopupWin = 1200;
  var heightPopupWin = 800;
  var leftPopupWin = (screen.width / 2) - (widthPopupWin / 2) - 12;
  var topPopupWin = (screen.height / 2) - (heightPopupWin / 2) - 50;
  var popupWin;
  popupWin = window.open("../groupware/report.php?rpObjType=cs&g_cs_eid=" + csEid,"","dependent=yes,width=" + widthPopupWin + ",height=" + heightPopupWin +",left=" + leftPopupWin + ",top=" + topPopupWin + ",scrollbars=yes");
};

function openCustomerSpecial(cmpId) {
  var widthPopupWin = 1200;
  var heightPopupWin = 800;
  var leftPopupWin = (screen.width / 2) - (widthPopupWin / 2) - 12;
  var topPopupWin = (screen.height / 2) - (heightPopupWin / 2) - 50;
  var popupWin;
  popupWin = window.open("../admin/customer_special.php?companyId=" + cmpId ,"","dependent=yes,width=" + widthPopupWin + ",height=" + heightPopupWin +",left=" + leftPopupWin + ",top=" + topPopupWin + ",scrollbars=yes");
};
-->
</script>
</head>

<body onLoad="<?php echo $phpCurrentNavigationOnLoad ?>displayStatusMessage();">

<?php echo $phpMenuOut ?>
<?php echo $phpReducedMenuOut ?>
<?php echo $phpPageTitelOut ?>

<div class="maincontent" name="maincontent" id="maincontent">

  <form action="../admin/cs_csc_list.php" method="post">
  <input type="hidden" name="f_act" value="">
  <input type="hidden" name="orderClause" value="<?php echo $orderClause ?>">
  <input type="hidden" name="tourno" value="<?php echo $tourno ?>">
  <input type="hidden" name="generic" value="<?php echo $generic ?>">
  <input type="hidden" name="genNum" value="<?php echo $genNum ?>">
  <input type="hidden" name="genJsParName" value="<?php echo $genJsParName ?>">
  <input type="hidden" name="callOpenerFunction" value="<?php echo $callOpenerFunction ?>">  
  <input type="hidden" name="f_cmp_authenticated" value="<?php echo $f_cmp_authenticated ?>">
  <input type="hidden" name="job_hq_id" value="<?php echo $job_hq_id ?>">
  <?php echo $tableHeaderSearchFieldsExtra ?>
  <?php echo $phpCurrentNavigationInputHidden ?>
  <input type="hidden" name="deactivateMenu" value="<?php echo ec($deactivateMenu) ?>">
  
  <?php echo htmlDivLineSpacer("10px"); ?>

  <div class="f10bp1_blue">
    <?php echo $pageTitel ?>
  </div>
  <?php echo htmlDivLineSpacer("10px"); ?>

  <!-- Headquarters checkboxes -->
  <?php 
    if (authCheckEmployeeRights($emp_id, "10")) :
       $parMaskCscListHqDisabled = getParameterValue("0", "MASK_CSC_LIST_HQ_SELECTION_DISABLED", $hq_id);
       if ($parMaskCscListHqDisabled == "") : $parMaskCscListHqDisabled = getParameterValue("0", "MASK_CSC_LIST_HQ_SELECTION_DISABLED", "0"); endif;
       if ($parMaskCscListHqDisabled != "1") :
          echo getHeadquartersCheckboxes($f_hq_id);
          echo htmlDivLineSpacer("10px", "", "left");
       endif;
    endif;
  ?>
  <?php echo htmlDivLineSpacer("10px"); ?>
  
  <div>
    <?php echo $pageHeaderOptionFields ?>
  </div>
  <?php echo htmlDivLineSpacer("15px"); ?>
  
  <div>
    <table border="0">
      <tr>
        <td>
          <table border="0">
            <tr>
              <td>
                <table class="f8np1" border="0" cellpadding="0">
                  <tr>
                    <?php echo $tableHeaderSearchFields ?>
                  </tr>
                  <tr>
                    <?php echo $tableHeaderLinks ?>
                  </tr>
                  <?php echo $tableOfRows ?>
                </table>
                <br><br>
                <?php echo getLngt("Anzahl Einträge:") ?> <?php echo $numOfRows ?><?php if ($numOfRows == "0" && $f_act == "search" && $statusMessage == "") : echo " " . getLngt("(Keine Einträge gefunden.)"); endif; ?>
              </td>
            </tr>
          </table>
        </td>
    </table>
    <?php
      // Nur wenn eine csc_id übergeben und wenn ansonsten keine Einträge gefunden
      if ($csc_id != "" && $numOfRows == "0" && $f_act == "search" && $statusMessage == "") {
        $sqlquery = "SELECT jb_tourname, tr_sort, tr_comp, tr_person, tour.csc_id, ad_street, tr_hsno, ad_country, ad_zipcode, ad_city, tr_remark" .
          " FROM job, tour, address WHERE (csc_id_payer = " . $csc_id . " AND csc_id_payer_cash = '0') AND LENGTH(jb_tourname) > 0" .
          " AND job.jb_id = tour.jb_id AND tour.ad_id = address.ad_id ORDER BY jb_tourname, tr_sort" ;
        $res = $db2->query($sqlquery);
        // if ($res->numRows() > 0) {
        if ($db2->dbNumRows($res) > 0) {
          echo htmlDivLineSpacer("10px");
          echo htmlDivLineSpacer("10px");
          echo "<div class=\"f10bp1_blue\">" . "Adressen aus gespeicherten Touren"  . "</div>";
          echo htmlDivLineSpacer("10px");
          $bgCol = getListColor(0, 0);
          echo "<table border=\"0\"><tr><td><table border=\"0\"><tr><td>";
          echo "<table border=\"0\"><tr><td bgcolor=\"" . $bgCol . "\">&nbsp;Name&nbsp;</td><td bgcolor=\"" . $bgCol . "\">&nbsp;Station&nbsp;</td><td bgcolor=\"" . $bgCol .
            "\">&nbsp;</td><td bgcolor=\"" . $bgCol .
            "\">&nbsp;Firma&nbsp;</td><td bgcolor=\"" . $bgCol . "\">&nbsp;Person&nbsp;</td><td bgcolor=\"" . $bgCol . "\">&nbsp;Straße&nbsp;</td><td bgcolor=\"" . $bgCol .
            "\">&nbsp;Nr.&nbsp;</td><td bgcolor=\"" . $bgCol . "\">&nbsp;PLZ&nbsp;</td><td bgcolor=\"" . $bgCol . "\">&nbsp;Ort&nbsp;</td><td bgcolor=\"" . $bgCol .
            "\">&nbsp;Bemerkung&nbsp;</td></tr>";
        }
        if (DB::isError($res)) die ("$PHP_SELF: '$sqlquery': " . $res->getMessage());
        $lineToggler = 0;
   	    while ($row = $res->fetch_assoc()):
          if ($lineToggler == 0) : $lineToggler = 1; else : $lineToggler = 0; endif;
          $bgCol = getListColor($lineToggler, $lineToggler);
          echo "<tr><td bgcolor=\"" . $bgCol . "\">&nbsp;" . $row["jb_tourname"] . "</td><td bgcolor=\"" . $bgCol . "\">&nbsp;" . $row["tr_sort"] . "</td><td bgcolor=\"" . $bgCol . "\">" .            
            "<a href=\"javascript:finishPage('','" . $row["csc_id"] . "','','" . my_str_check_js($row["tr_comp"]) . "','','" . my_str_check_js($row["ad_street"]) . "','" . $row["tr_hsno"] . "','" .
            $row["ad_zipcode"] . "','" . my_str_check_js($row["ad_city"]) . "','" . my_str_check_js($row["ad_country"]) . "','" . my_str_check_js($row["tr_remark"]) . "','" .
            my_str_check_js($row["tr_person"]) . "','" . $tourno . "','','','','','','','','','','','','','',1,0,0,'','',0);\">" .
            "<img src=\"../images/arrow_right.png\" border=\"0\" height=\"10\" width=\"25\">" . "</a>" . "</td><td bgcolor=\"" . $bgCol .
            "\">&nbsp;" . $row["tr_comp"] . "</td><td bgcolor=\"" . $bgCol . "\">&nbsp;" . $row["tr_person"] . "</td><td bgcolor=\"" . $bgCol .
            "\">&nbsp;" . $row["ad_street"] . "</td><td bgcolor=\"" . $bgCol . "\">&nbsp;" . $row["tr_hsno"] . "</td><td bgcolor=\"" . $bgCol .
            "\">&nbsp;" . $row["ad_zipcode"] . "</td><td bgcolor=\"" . $bgCol . "\">&nbsp;" . $row["ad_city"] . "</td><td bgcolor=\"" . $bgCol .
            "\">&nbsp;" . $row["tr_remark"] . "</td></tr>";
   	    endwhile;
        // if ($res->numRows() > 0)
        if ($db2->dbNumRows($res) > 0)
          echo "</td></td></tr>";
          echo "</tr></td></table></tr></td></table>";
   	    $res->free();
   	  }
    ?>
  </div>

</form>
</div>
</body>

</html>
