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

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


getSecHttpVars("1", array("f_act", "f_mode", "genJsParName", "companyId", "orderClause", "statusMessage", "f_cmp_authenticated",
                          "f_cmp_comp", "f_cmp_comp2", "f_cmp_freetext1", "f_cr_sid", "f_cr_eid", "f_usr_name", "f_usr_firstname",
                          "f_usr_phone", "f_usr_email", "f_usr_account", "f_cr_mobile_pda", "f_cr_imei", "f_cr_vh_sign", "f_vht_id_inv",
                          "f_crvh_vh_sign", "f_crvh_comm_no", "f_crvh_mobile", "f_ad_street", "f_ad_zipcode", "f_ad_city", "g_filter",
                          "f_crvh_prov", "f_show_invisible", "f_show_invisible_internal", "f_hq_id", "f_contractors_only", "f_pda_infos",
                          "f_crvh_remark", "f_crvh_freetext_1", "f_crvh_measurement", "f_cr_serialno",
                          "f_crvh_length", "f_crvh_width", "f_crvh_height", "f_crvh_position", "f_crvh_payload", "f_crvh_totalweight",
                          "f_crvh_env_badge_level", "f_crvh_corporate_identity", "f_crvh_licence_date", "f_crvh_drivetype", "fileOutput",
                          "f_cr_pda_infos", "f_cr_pda_infos_device", "deactivateMenu", "objecttypemode"));

getLanguage(__FILE__);

if ($objecttypemode == "") : $objecttypemode = "cr_list"; endif;
$pageTitel = getLngt(wrapPhrase("TRANSPORTEURE", $objecttypemode));
include_once ("../admin/menu.php");
include_once ("../include/html.inc.php");
getCurrentScript(__FILE__);

// Check for authentication access and granted rights
$usrAccessArray["hq"] = "1";
authCheckForAccess($hq_id, $usr_id, $emp_id, "1", $customerId, $cscIdRoot, $cscIdActual);
authCheckEmployeeRights($emp_id, "1", "1");

// Get the emp_id of the root admin because he/she is allowed to change a password of an employee
if ($userTypeName == "hq") :
   $empIdRootAdmin = getEmpIdOfRootAdmin($userTypeName);
endif;

// Access to CSV export of current courier list
$empHasAccessCrCsvExport = authCheckEmployeeRights($emp_id, "48");

// Wrapper function for "str_replace()"
// E.g.: "%u20AC" => '&euro;'
function mcStr(&$aStr, $search = ";", $replace = "") {
	$aStr = str_replace("&nbsp;", " ", $aStr);
	$aStr = str_replace("%u20AC", '€', $aStr);
  $aStr = str_replace($search, $replace, $aStr);
  return $aStr;  // Optional return value
}

// Path for download
$tempPath = getParameterValue("0", "TEMP_PATH", $hq_id);
if ($tempPath == "") : $tempPath = getParameterValue("0", "TEMP_PATH", "0"); endif;
if ($tempPath == "") : $tempPath = "../temp/download/"; endif;

$numOfRows = 0;
$htmlClass01 = "class=\"smaller\"";  // input
$htmlClass02 = "class=\"f7np1\" style=\"width:100px; height:20px; border-radius: 5px;\"";  // select
$tableOfRows = "";
$pageHeadline = "";

$outArr2File = array();
$fileExportDelimiter = ";";

// Flag for "show invisible couriers"
if ($f_show_invisible == "" || count($f_show_invisible) == 0) :
   $f_show_invisible = "0";
else: 
   $f_show_invisible = "1";
endif;
// Flag for "show invisible INTERNAL couriers"
if ($f_show_invisible_internal == "" || count($f_show_invisible_internal) == 0) :
   $f_show_invisible_internal = "0";
else: 
   $f_show_invisible_internal = "1";
endif;
// Flag for contractors
if ($f_contractors_only == "" || count($f_contractors_only) == 0) :
   $f_contractors_only = "0";
else: 
   $f_contractors_only = "1";
endif;
// Flag for pda infos
if ($f_pda_infos == "" || count($f_pda_infos) == 0) :
   $f_pda_infos = "0";
else: 
   $f_pda_infos = "1";
endif;
// Drop down for devices in VPA infos
if (!isset($f_cr_pda_infos_device)) :
   $f_cr_pda_infos_device = "";
endif;
// Check access to CSV export of current courier list
if ($fileOutput == "" || count($fileOutput) == 0 || !$empHasAccessCrCsvExport) :
   $fileOutput = "0";
else :
   $fileOutput = "1";
endif;

// For usage of meta database get connection if requested
if ($f_pda_infos == "1") :
   /*
   $db_conn = getConnectionToMetaDB("temp","conn_context");
   if ($db_conn == "ERR_DB") :
      // $f_pda_infos = "0";
      $statusMessage = getLngt("Die VPA-Informationen können nur partiell abgerufen werden! Bitte kontaktieren Sie den Support!") . " ";
   endif;
   */
endif;


// 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;

mcTrim($f_cr_sid); mcTrim($f_cr_eid); mcTrim($f_cmp_comp); mcTrim($f_cmp_freetext1); mcTrim($f_usr_name); mcTrim($f_usr_firstname); mcTrim($f_usr_phone); 
mcTrim($f_usr_email); mcTrim($f_usr_account); mcTrim($f_cr_mobile_pda); mcTrim($f_cr_imei); mcTrim($f_crvh_prov); mcTrim($f_ad_street); mcTrim($f_ad_zipcode); 
mcTrim($f_ad_city); mcTrim($f_crvh_vh_sign); mcTrim($f_crvh_comm_no); mcTrim($f_crvh_mobile); mcTrim($f_crvh_remark); mcTrim($f_crvh_freetext_1); mcTrim($f_crvh_measurement); 
mcTrim($f_crvh_length); mcTrim($f_crvh_width); mcTrim($f_crvh_height); mcTrim($f_crvh_position); mcTrim($f_crvh_payload); mcTrim($f_crvh_totalweight); mcTrim($f_crvh_env_badge_level); 
mcTrim($f_crvh_corporate_identity); mcTrim($f_crvh_drivetype); mcTrim($f_vht_id_inv); mcTrim($f_cmp_iln); mcTrim($f_cmp_tax_idno); mcTrim($f_cmp_bank); mcTrim($f_cmp_bankno); 
mcTrim($f_cmp_bankacc);  

// $f_crvh_licence_date = str_replace("-", "", trim($f_crvh_licence_date));
$f_crvh_licence_date = preg_replace('/[^\d0-9]/i', '', $f_crvh_licence_date);

// Get vehicle types out of "metatype"
$vhtTypeArray = getColVectorFromDB2ArrayByClause("metatype", "mt_value", "mt_type = 'vehicletype'", "mt_sort", "mt_sort", "");

// Get the names of the "Corporate Identity" (CI) for vehicles out of "metatype"
$vhtCINamesArray = getColVectorFromDB2ArrayByClause("metatype", "mt_value", "mt_type = 'vehicle_ci'", "mt_sort", "mt_sort", "");

// Get drive types out of "metatype"
$vhtDriveTypeArray = getColVectorFromDB2ArrayByClause("metatype", "mt_value", "mt_type = 'drivetype'", "mt_sort", "mt_sort", "");


// Get the parameter to set the order of the columns to be displayed
$displayedListCols = getParameterValue($emp_id, "MASK_CR_LIST_COLS");
if ($displayedListCols == "") :
   $displayedListCols = getParameterValue("0", "MASK_CR_LIST_COLS", $hqId);
   if ($displayedListCols == "") :
      // Default settings
      if (CR_SID_STATUS == "1") :
         $displayedListCols = "cr_sid,cr_vh_sign,cr_eid,usr_name,usr_firstname,usr_phone,cr_mobile_pda,cmp_authenticated_img,cr_id_history,cr_id_report,cmp_id_edit,ad_zipcode,cr_serialno";
      else :
         $displayedListCols = "cr_sid,crvh_vh_sign,cr_eid,crvh_prov,usr_name,usr_firstname,usr_phone,cr_mobile_pda,cmp_authenticated_img,cr_id_history,cr_id_report,cmp_id_edit,ad_zipcode,cr_serialno";
      endif;
   endif;
endif;
$displayedListColsArray = explode(",",$displayedListCols);
$displayedListColsArrayLen = count($displayedListColsArray);


// If called e.g. by the first time with empty search-fields
$searchValues = $f_cr_sid . $f_cr_eid . $f_cmp_comp . $f_cmp_freetext1 . $f_usr_name . $f_usr_firstname . $f_usr_phone . $f_usr_email . $f_usr_account . $f_cr_mobile_pda . $f_cr_imei . $f_crvh_vh_sign . $f_crvh_comm_no . $f_crvh_mobile . $f_cr_vh_sign . $f_crvh_prov . $f_ad_street . $f_ad_zipcode . $f_ad_city . $f_crvh_remark . $f_crvh_freetext_1 . $f_crvh_measurement . $f_crvh_length . $f_crvh_width . $f_crvh_height . $f_crvh_position . $f_crvh_payload . $f_crvh_env_badge_level . $f_crvh_corporate_identity . $f_crvh_licence_date . $f_crvh_drivetype . $f_vht_id_inv . $f_crvh_totalweight . $f_cr_serialno;

$doSearch = FALSE;
if (strlen($searchValues) == 0) :
   if (getCountOfTable("courier") <= MAX_CARDINALITY) :
      $doSearch = TRUE;
   endif;
endif;
if ($g_filter != "") :
   $doSearch = TRUE;
endif;

// Gets VPA info header
function getVPAInfoHeader ($mode = "") {
	$retStr = getLngt("VPA-Infos");
  if (true) :
     if ($mode == "1") :
        $retStr .= ": ";
     else :
        $retStr .= "</br>";
     endif;
     $colArr = array("green", "black", "blue", "red", "yellow", "orange");
     // $vpaInfoHeaderArr = array("Modell", "Hersteller", "Betriebssystem", "BS&#8209;Version", "Framework", "APP&#8209;Version");  // All available infos
     $vpaInfoHeaderArr = array("APP&#8209;Version", "BS&#8209;Version", "Modell");
     $vpaInfoHeaderBaseArr = array("APP-Version", "BS-Version", "Modell");
     $vpaInfoHeaderArrLen = count($vpaInfoHeaderArr);     
     for ($i = 0; $i < $vpaInfoHeaderArrLen; $i++) :
         if ($mode == "1") :
            if ($retStr != getLngt("VPA-Infos") . ": ") : $retStr .= "|"; endif;
            $retStr .= getLngt($vpaInfoHeaderBaseArr[$i]);
         else :
            $retStr .= "<span style=\"color:" . $colArr[$i] . ";\">&nbsp;" . getLngt($vpaInfoHeaderArr[$i]) . "</span>&nbsp;";
         endif;
     endfor;
  endif;
  return $retStr;
}

// Gets VPA info data
function getVPAInfoData ($crPdaInfos, $mode = "") {
	$retStr = "";
  if ($crPdaInfos != "") :
     $crPdaInfoArr = getKeyValueArrayFromString($crPdaInfos);
     if (is_array($crPdaInfoArr)) :
        $colArr = array("green", "black", "blue", "red", "yellow", "orange");
        // $vpaKeyArr = array("mod", "man", "os", "os_ver", "fx", "app_ver");  // All available infos
        $vpaKeyArr = array("app_ver", "os_ver", "mod");
        $vpaKeyArrLen = count($vpaKeyArr);
        for ($i = 0; $i < $vpaKeyArrLen; $i++) :
        	  $val = $crPdaInfoArr[$vpaKeyArr[$i]];
            if ($val != "" && $val != "nil") :
               if ($mode == "1") :
                  if ($retStr != "") : $retStr .= "|"; endif;
                  $retStr .= $val;
               else :
                 $retStr .= "<span style=\"color:" . $colArr[$i] . ";\">&nbsp;" . $val . "</span>&nbsp;";
               endif;
            endif;
        endfor;
     endif;
  endif;
  return $retStr;
}

// OUTPUT: Table header search fields
$tableHeaderSearchFields = "";
if (TRUE) :
   $dspColHeaderSearchFieldArray = array();
   $dspColHeaderSearchFieldArray["cr_sid"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_cr_sid\" value=\"" . $f_cr_sid . "\" size=\"5\">";

   if (CR_SID_STATUS == "1") :
      $dspColHeaderSearchFieldArray["cr_vh_sign"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_cr_vh_sign\" value=\"" . $f_cr_vh_sign . "\" size=\"10\">";
      $dspColHeaderSearchFieldArray["cr_eid"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_cr_eid\" value=\"" . $f_cr_eid . "\" size=\"10\">";
   else :
      $dspColHeaderSearchFieldArray["crvh_vh_sign"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_crvh_vh_sign\" value=\"" . $f_crvh_vh_sign . "\" size=\"10\">";
      $dspColHeaderSearchFieldArray["crvh_comm_no"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_crvh_comm_no\" value=\"" . $f_crvh_comm_no . "\" size=\"10\">";
      $dspColHeaderSearchFieldArray["crvh_mobile"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_crvh_mobile\" value=\"" . $f_crvh_mobile . "\" size=\"10\">";
      $dspColHeaderSearchFieldArray["cr_eid"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_cr_eid\" value=\"" . $f_cr_eid . "\" size=\"10\">";
      $dspColHeaderSearchFieldArray["crvh_prov"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_crvh_prov\" value=\"" . $f_crvh_prov . "\" size=\"5\">";
      $dspColHeaderSearchFieldArray["crvh_remark"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_crvh_remark\" value=\"" . $f_crvh_remark . "\" size=\"30\">";
      $dspColHeaderSearchFieldArray["crvh_freetext_1"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_crvh_freetext_1\" value=\"" . $f_crvh_freetext_1 . "\" size=\"30\">";
      $dspColHeaderSearchFieldArray["crvh_measurement"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_crvh_measurement\" value=\"" . $f_crvh_measurement . "\" size=\"20\">";
      $dspColHeaderSearchFieldArray["crvh_length"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_crvh_length\" value=\"" . $f_crvh_length . "\" size=\"5\">";
      $dspColHeaderSearchFieldArray["crvh_width"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_crvh_width\" value=\"" . $f_crvh_width . "\" size=\"5\">";
      $dspColHeaderSearchFieldArray["crvh_height"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_crvh_height\" value=\"" . $f_crvh_height . "\" size=\"5\">";
      $dspColHeaderSearchFieldArray["crvh_position"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_crvh_position\" value=\"" . $f_crvh_position . "\" size=\"5\">";
      $dspColHeaderSearchFieldArray["crvh_payload"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_crvh_payload\" value=\"" . $f_crvh_payload . "\" size=\"5\">";
      $dspColHeaderSearchFieldArray["crvh_env_badge_level"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_crvh_env_badge_level\" value=\"" . $f_crvh_env_badge_level . "\" size=\"5\">";
      $dspColHeaderSearchFieldArray["crvh_corporate_identity"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_crvh_corporate_identity\" value=\"" . $f_crvh_corporate_identity . "\" size=\"5\">";
      $dspColHeaderSearchFieldArray["crvh_licence_date"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_crvh_licence_date\" value=\"" . $f_crvh_licence_date . "\" size=\"5\">";
      // $dspColHeaderSearchFieldArray["crvh_drivetype"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_crvh_drivetype\" value=\"" . $f_crvh_drivetype . "\" size=\"5\">";
      $dspColHeaderSearchFieldArray["crvh_drivetype"] = "<select " . $htmlClass02 . " name=\"f_crvh_drivetype\">" . addOptionsFromTable("metatype","mt_sort","mt_value","mt_sort","mt_type = 'drivetype'",$f_crvh_drivetype) . "</select>";
      $dspColHeaderSearchFieldArray["vht_id_inv"] = "<select " . $htmlClass02 . " name=\"f_vht_id_inv\">" . addOptionsFromTable("metatype","mt_sort","mt_value","mt_sort","mt_type = 'vehicletype'",$f_vht_id_inv) . "</select>";
      $dspColHeaderSearchFieldArray["crvh_totalweight"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_crvh_totalweight\" value=\"" . $f_crvh_totalweight . "\" size=\"5\">";
   endif;

   $dspColHeaderSearchFieldArray["usr_name"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_usr_name\" value=\"" . $f_usr_name . "\">";
   $dspColHeaderSearchFieldArray["usr_firstname"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_usr_firstname\" value=\"" . $f_usr_firstname . "\">";
   $dspColHeaderSearchFieldArray["usr_phone"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_usr_phone\" value=\"" . $f_usr_phone . "\" size=\"20\">";
   $dspColHeaderSearchFieldArray["usr_email"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_usr_email\" value=\"" . $f_usr_email . "\">";
   $dspColHeaderSearchFieldArray["usr_account"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_usr_account\" value=\"" . $f_usr_account . "\">";
   $dspColHeaderSearchFieldArray["cr_mobile_pda"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_cr_mobile_pda\" value=\"" . $f_cr_mobile_pda . "\" size=\"20\">";
   $dspColHeaderSearchFieldArray["cr_imei"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_cr_imei\" value=\"" . $f_cr_imei . "\" size=\"20\">";
   $dspColHeaderSearchFieldArray["cr_serialno"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_cr_serialno\" value=\"" . $f_cr_serialno . "\" size=\"20\">";
   $dspColHeaderSearchFieldArray["cmp_comp"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_cmp_comp\" value=\"" . $f_cmp_comp . "\" size=\"10\">";
   $dspColHeaderSearchFieldArray["cmp_freetext1"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_cmp_freetext1\" value=\"" . $f_cmp_freetext1 . "\" size=\"10\">";

   $dspColHeaderSearchFieldArray["cmp_authenticated_img"] = "<a href=\"javascript:document.forms[0].f_act.value='search';document.forms[0].submit();\">&nbsp;" . getLngt("Suchen") . "&nbsp;</a>";

   if ($f_mode != "0" && $f_mode != "1" && $f_mode != "2" && $f_mode != "3"):
      $dspColHeaderSearchFieldArray["cr_id_history"] = "&nbsp;";          // History
      $dspColHeaderSearchFieldArray["cr_id_report"] = "&nbsp;";           // Berichte
      $dspColHeaderSearchFieldArray["cmp_id_edit"] = "&nbsp;";            // Edit
      $dspColHeaderSearchFieldArray["cr_num_of_documents"] = "&nbsp;";    // Documents courier
      $dspColHeaderSearchFieldArray["crvh_num_of_documents"] = "&nbsp;";  // Documents vehicle
   endif;

   if ($f_pda_infos == "1") :
      $tmpDeviceTypeArr = getColVectorFromDB2ArrayByClause("courier", "SUBSTRING_INDEX(SUBSTRING(cr_device_info, 1, (INSTR(cr_device_info, '|') - 1)), '=', -1) AS cr_device_info", "cr_device_info != ''", "", "", "DISTINCT", "");
      /*
      if ($db_conn != "ERR_DB") :
         $tmpDeviceTypeArr2 = getColVectorFromDB2ArrayByClause("conn_context", "SUBSTRING_INDEX(SUBSTRING(cc_appver, 1, (INSTR(cc_appver, '|') - 1)), '=', -1) AS cr_device_info", "cust_id = '1' AND inst_id = '0' AND cc_appver != ''", "", "", "DISTINCT", $db_conn);
         $tmpDeviceTypeArr = array_merge($tmpDeviceTypeArr, $tmpDeviceTypeArr2);
         $tmpDeviceTypeArr = array_unique($tmpDeviceTypeArr);
         sort($tmpDeviceTypeArr);
      endif;
      */
      $dspColHeaderSearchFieldArray["cr_pda_infos"] = "<input type=\"text\" " . $htmlClass01 . " id=\"f_cr_pda_infos\" name=\"f_cr_pda_infos\" value=\"" . $f_cr_pda_infos . "\" size=\"10\">";
      $dspColHeaderSearchFieldArray["cr_pda_infos"] .= "<select " . $htmlClass02 . " id=\"f_cr_pda_infos_device\" name=\"f_cr_pda_infos_device\" onChange=\"$('#f_cr_pda_infos').val($(this).val());\">" . addOptionsFromArray($tmpDeviceTypeArr, "", "1") . "</select>";
   endif;

   $dspColHeaderSearchFieldArray["ad_street"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_ad_street\" value=\"" . $f_ad_street . "\">";
   $dspColHeaderSearchFieldArray["ad_zipcode"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_ad_zipcode\" value=\"" . $f_ad_zipcode . "\" size=\"5\">";
   $dspColHeaderSearchFieldArray["ad_city"] = "<input type=\"text\" " . $htmlClass01 . " name=\"f_ad_city\" value=\"" . $f_ad_city . "\">";

   // Use "check-array" to avoid search fields by multiple columns of the same (DB) field
   $chkArr = array();

   $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)) :
          $cellColor = getListColor(1, 1);
          if (!in_array($tmpKeys[$j], $chkArr)) :
             $tableHeaderSearchFields .= "<td bgcolor=\"" . $cellColor . "\">" . $dspColHeaderSearchFieldArray[$tmpKeys[$j]] . "</td>";
             array_push($chkArr, $tmpKeys[$j]);
          else :
             $tableHeaderSearchFields .= "<td bgcolor=\"" . $cellColor . "\">" . "&nbsp;" . "</td>";
          endif;
       endif;
   }
   // Add potential extra cols
   if ($f_pda_infos == "1") :
      $cellColor = getListColor(1, 1);
      $tableHeaderSearchFields .= "<td bgcolor=\"" . $cellColor . "\">" . $dspColHeaderSearchFieldArray["cr_pda_infos"] . "</td>";
   endif;
endif;


// OUTPUT: Table header column links
$tableHeaderLinks = "";
if (TRUE) :
   $dspColHeaderLinksArray = array();
   $titleArray = array();
   
   if (CR_SID_STATUS == "1") :
      $titleArray["cr_sid"] = getLngt("SID");        $dspColHeaderLinksArray["cr_sid"] = "<a href=\"javascript:document.forms[0].orderClause.value='cr.cr_sid';document.forms[0].f_act.value='search';document.forms[0].submit();\">&nbsp;" . $titleArray["cr_sid"] . "&nbsp;</a>";
      $titleArray["cr_vh_sign"] = getLngt("Kennz."); $dspColHeaderLinksArray["cr_vh_sign"] = "<a href=\"javascript:document.forms[0].orderClause.value='cr.cr_vh_sign';document.forms[0].f_act.value='search';document.forms[0].submit();\">&nbsp;" . $titleArray["cr_vh_sign"] . "&nbsp;</a>";
      $titleArray["cr_eid"] = getLngt("ExtID");      $dspColHeaderLinksArray["cr_eid"] = "<a href=\"javascript:document.forms[0].orderClause.value='cr.cr_eid';document.forms[0].f_act.value='search';document.forms[0].submit();\">&nbsp;" . $titleArray["cr_eid"] . "&nbsp;</a>";
   else :
      $titleArray["cr_sid"] = getLngt("SID");            $dspColHeaderLinksArray["cr_sid"] = "<a href=\"javascript:document.forms[0].orderClause.value='crvh.crvh_sid';document.forms[0].f_act.value='search';document.forms[0].submit();\">&nbsp;" . $titleArray["cr_sid"] . "&nbsp;</a>";
      $titleArray["crvh_vh_sign"] = getLngt("Kennz.");   $dspColHeaderLinksArray["crvh_vh_sign"] = "<a href=\"javascript:document.forms[0].orderClause.value='crvh.crvh_vh_sign';document.forms[0].f_act.value='search';document.forms[0].submit();\">&nbsp;" . $titleArray["crvh_vh_sign"] . "&nbsp;</a>";
      $titleArray["crvh_comm_no"] = getLngt("Kommiss."); $dspColHeaderLinksArray["crvh_comm_no"] = "<a href=\"javascript:document.forms[0].orderClause.value='crvh.crvh_comm_no';document.forms[0].f_act.value='search';document.forms[0].submit();\">&nbsp;" . $titleArray["crvh_comm_no"] . "&nbsp;</a>";
      $titleArray["crvh_mobile"] = getLngt("VPA-Nr");    $dspColHeaderLinksArray["crvh_mobile"] = "<a href=\"javascript:document.forms[0].orderClause.value='crvh.crvh_mobile';document.forms[0].f_act.value='search';document.forms[0].submit();\">&nbsp;" . $titleArray["crvh_mobile"] . "&nbsp;</a>";
      $titleArray["cr_eid"] = getLngt("ExtID");          $dspColHeaderLinksArray["cr_eid"] = "<a href=\"javascript:document.forms[0].orderClause.value='cr.cr_eid';document.forms[0].f_act.value='search';document.forms[0].submit();\">&nbsp;" . $titleArray["cr_eid"] . "&nbsp;</a>";
      $titleArray["crvh_prov"] = getLngt("Prov.");       $dspColHeaderLinksArray["crvh_prov"] = "<a href=\"javascript:document.forms[0].orderClause.value='crvh.crvh_prov';document.forms[0].f_act.value='search';document.forms[0].submit();\">&nbsp;" . $titleArray["crvh_prov"] . "&nbsp;</a>";
      $titleArray["crvh_remark"] = getLngt("Fzg.Bemerk.");  $dspColHeaderLinksArray["crvh_remark"] = "<a href=\"javascript:document.forms[0].orderClause.value='crvh.crvh_remark';document.forms[0].f_act.value='search';document.forms[0].submit();\">&nbsp;" . $titleArray["crvh_remark"] . "&nbsp;</a>";
      $titleArray["crvh_freetext_1"] = getLngt("Fzg.Freitext");  $dspColHeaderLinksArray["crvh_freetext_1"] = "<a href=\"javascript:document.forms[0].orderClause.value='crvh.crvh_freetext_1';document.forms[0].f_act.value='search';document.forms[0].submit();\">&nbsp;" . $titleArray["crvh_freetext_1"] . "&nbsp;</a>";
      $titleArray["crvh_measurement"] = getLngt("Fzg.Maße&nbsp;(L,B,H,S)"); $dspColHeaderLinksArray["crvh_measurement"] = "<a href=\"javascript:document.forms[0].orderClause.value='crvh_measurement';document.forms[0].f_act.value='search';document.forms[0].submit();\">&nbsp;" . $titleArray["crvh_measurement"] . "&nbsp;</a>";
      $titleArray["crvh_length"] = getLngt("Länge");  $dspColHeaderLinksArray["crvh_length"] = "<a href=\"javascript:document.forms[0].orderClause.value='crvh.crvh_length';document.forms[0].f_act.value='search';document.forms[0].submit();\">&nbsp;" . $titleArray["crvh_length"] . "&nbsp;</a>";
      $titleArray["crvh_width"] = getLngt("Breite");  $dspColHeaderLinksArray["crvh_width"] = "<a href=\"javascript:document.forms[0].orderClause.value='crvh.crvh_width';document.forms[0].f_act.value='search';document.forms[0].submit();\">&nbsp;" . $titleArray["crvh_width"] . "&nbsp;</a>";
      $titleArray["crvh_height"] = getLngt("Höhe");   $dspColHeaderLinksArray["crvh_height"] = "<a href=\"javascript:document.forms[0].orderClause.value='crvh.crvh_height';document.forms[0].f_act.value='search';document.forms[0].submit();\">&nbsp;" . $titleArray["crvh_height"] . "&nbsp;</a>";
      $titleArray["crvh_position"] = getLngt("Stellplätze"); $dspColHeaderLinksArray["crvh_position"] = "<a href=\"javascript:document.forms[0].orderClause.value='crvh.crvh_position';document.forms[0].f_act.value='search';document.forms[0].submit();\">&nbsp;" . $titleArray["crvh_position"] . "&nbsp;</a>";
      $titleArray["crvh_payload"] = getLngt("Nutzlast"); $dspColHeaderLinksArray["crvh_payload"] = "<a href=\"javascript:document.forms[0].orderClause.value='crvh.crvh_payload';document.forms[0].f_act.value='search';document.forms[0].submit();\">&nbsp;" . $titleArray["crvh_payload"] . "&nbsp;</a>";
      $titleArray["crvh_env_badge_level"] = getLngt("Umweltstufe"); $dspColHeaderLinksArray["crvh_env_badge_level"] = "<a href=\"javascript:document.forms[0].orderClause.value='crvh.crvh_env_badge_level';document.forms[0].f_act.value='search';document.forms[0].submit();\">&nbsp;" . $titleArray["crvh_env_badge_level"] . "&nbsp;</a>";
      $titleArray["crvh_corporate_identity"] = getLngt("CI");       $dspColHeaderLinksArray["crvh_corporate_identity"] = "<a href=\"javascript:document.forms[0].orderClause.value='crvh.crvh_corporate_identity';document.forms[0].f_act.value='search';document.forms[0].submit();\">&nbsp;" . $titleArray["crvh_corporate_identity"] . "&nbsp;</a>";
      $titleArray["crvh_licence_date"] = getLngt("Erstzulasung");   $dspColHeaderLinksArray["crvh_licence_date"] = "<a href=\"javascript:document.forms[0].orderClause.value='crvh.crvh_licence_date';document.forms[0].f_act.value='search';document.forms[0].submit();\">&nbsp;" . $titleArray["crvh_licence_date"] . "&nbsp;</a>";
      $titleArray["crvh_drivetype"] = getLngt("Antriebsart");       $dspColHeaderLinksArray["crvh_drivetype"] = "<a href=\"javascript:document.forms[0].orderClause.value='crvh.crvh_drivetype';document.forms[0].f_act.value='search';document.forms[0].submit();\">&nbsp;" . $titleArray["crvh_drivetype"] . "&nbsp;</a>";
      $titleArray["vht_id_inv"] = getLngt("Rechnungsfahrzeugtyp");  $dspColHeaderLinksArray["vht_id_inv"] = "<a href=\"javascript:document.forms[0].orderClause.value='cr.vht_id_inv';document.forms[0].f_act.value='search';document.forms[0].submit();\">&nbsp;" . $titleArray["vht_id_inv"] . "&nbsp;</a>";
      $titleArray["crvh_totalweight"] = getLngt("Zul.Ges.Gewicht"); $dspColHeaderLinksArray["crvh_totalweight"] = "<a href=\"javascript:document.forms[0].orderClause.value='crvh.crvh_totalweight';document.forms[0].f_act.value='search';document.forms[0].submit();\">&nbsp;" . $titleArray["crvh_totalweight"] . "&nbsp;</a>";
   endif;

   $titleArray["usr_name"] = getLngt("Name"); $dspColHeaderLinksArray["usr_name"] = "<a href=\"javascript:document.forms[0].orderClause.value='usr.usr_name';document.forms[0].f_act.value='search';document.forms[0].submit();\">&nbsp;" . $titleArray["usr_name"] . "&nbsp;</a>";
   $titleArray["usr_firstname"] = getLngt("Vorname"); $dspColHeaderLinksArray["usr_firstname"] = "<a href=\"javascript:document.forms[0].orderClause.value='usr.usr_firstname';document.forms[0].f_act.value='search';document.forms[0].submit();\">&nbsp;" . $titleArray["usr_firstname"] . "&nbsp;</a>";
   $titleArray["usr_phone"] = getLngt("Telefon"); $dspColHeaderLinksArray["usr_phone"] = "<a href=\"javascript:document.forms[0].orderClause.value='usr.usr_phone';document.forms[0].f_act.value='search';document.forms[0].submit();\">&nbsp;" . $titleArray["usr_phone"] . "&nbsp;</a>";
   $titleArray["usr_email"] = getLngt("E-Mail"); $dspColHeaderLinksArray["usr_email"] = "<a href=\"javascript:document.forms[0].orderClause.value='usr.usr_email';document.forms[0].f_act.value='search';document.forms[0].submit();\">&nbsp;" . $titleArray["usr_email"] . "&nbsp;</a>";
   $titleArray["usr_account"] = getLngt("Anmeldename"); $dspColHeaderLinksArray["usr_account"] = "<a href=\"javascript:document.forms[0].orderClause.value='usr.usr_account';document.forms[0].f_act.value='search';document.forms[0].submit();\">&nbsp;" . $titleArray["usr_account"] . "&nbsp;</a>";
   $titleArray["cr_mobile_pda"] = getLngt("Mobil-PDA"); $dspColHeaderLinksArray["cr_mobile_pda"] = "<a href=\"javascript:document.forms[0].orderClause.value='cr.cr_mobile_pda';document.forms[0].f_act.value='search';document.forms[0].submit();\">&nbsp;" . $titleArray["cr_mobile_pda"] . "&nbsp;</a>";
   $titleArray["cr_imei"] = getLngt("IMEI"); $dspColHeaderLinksArray["cr_imei"] = "<a href=\"javascript:document.forms[0].orderClause.value='cr.cr_imei';document.forms[0].f_act.value='search';document.forms[0].submit();\">&nbsp;" . $titleArray["cr_imei"] . "&nbsp;</a>";
   $titleArray["cr_serialno"] = getLngt("Seriennr."); $dspColHeaderLinksArray["cr_serialno"] = "<a href=\"javascript:document.forms[0].orderClause.value='cr.cr_serialno';document.forms[0].f_act.value='search';document.forms[0].submit();\">&nbsp;" . $titleArray["cr_serialno"] . "&nbsp;</a>";
   $titleArray["cmp_comp"] = getLngt("Firma"); $dspColHeaderLinksArray["cmp_comp"] = "<a href=\"javascript:document.forms[0].orderClause.value='cmp.cmp_comp';document.forms[0].f_act.value='search';document.forms[0].submit();\">&nbsp;" . $titleArray["cmp_comp"] . "&nbsp;</a>";
   $titleArray["cmp_freetext1"] = getLngt(wrapPhrase("Freitext", $objecttypemode)); $dspColHeaderLinksArray["cmp_freetext1"] = "<a href=\"javascript:document.forms[0].orderClause.value='cmp.cmp_freetext1';document.forms[0].f_act.value='search';document.forms[0].submit();\">&nbsp;" . $titleArray["cmp_freetext1"] . "&nbsp;</a>";

   $titleArray["cmp_authenticated_img"] = getLngt("Freigabe"); $dspColHeaderLinksArray["cmp_authenticated_img"] = "&nbsp;" . $titleArray["cmp_authenticated_img"] . "&nbsp;";
      
   if ($f_mode != "0" && $f_mode != "1" && $f_mode != "2" && $f_mode != "3"): 
      $titleArray["cr_id_history"] = ""; $dspColHeaderLinksArray["cr_id_history"] = "&nbsp;" . getLngt("Historie") . "&nbsp;";   // History
      $titleArray["cr_id_report"] = ""; $dspColHeaderLinksArray["cr_id_report"] = "&nbsp;" . getLngt("Berichte") . "&nbsp;";     // Berichte
      $titleArray["cmp_id_edit"] = ""; $dspColHeaderLinksArray["cmp_id_edit"] = "&nbsp;" . getLngt("Bearbeiten") . "&nbsp;";       // Edit

      $titleArray["cr_num_of_documents"] = getLngt("Tp.Dokumente");    $dspColHeaderLinksArray["cr_num_of_documents"] = "&nbsp;" . $titleArray["cr_num_of_documents"] . "&nbsp;";      // Documents courier
      $titleArray["crvh_num_of_documents"] = getLngt("Fzg.Dokumente"); $dspColHeaderLinksArray["crvh_num_of_documents"] = "&nbsp;" . $titleArray["crvh_num_of_documents"] . "&nbsp;";  // Documents vehicle
   endif;      

   if ($f_pda_infos == "1") :
      // $titleArray["cr_pda_infos"] = getLngt("VPA-Infos"); $dspColHeaderLinksArray["cr_pda_infos"] = "<a href=\"javascript:document.forms[0].orderClause.value='cr.cr_pda_infos';document.forms[0].f_act.value='search';document.forms[0].submit();\">&nbsp;" . $titleArray["cr_pda_infos"] . "&nbsp;</a>";
      // $titleArray["cr_pda_infos"] = getLngt("VPA-Infos"); $dspColHeaderLinksArray["cr_pda_infos"] = "&nbsp;" . $titleArray["cr_pda_infos"] . "&nbsp;";
      $titleArray["cr_pda_infos"] = getVPAInfoHeader("1"); $dspColHeaderLinksArray["cr_pda_infos"] = "&nbsp;" . getVPAInfoHeader() . "&nbsp;";
   endif;

   $titleArray["ad_street"] = getLngt("Strasse"); $dspColHeaderLinksArray["ad_street"] = "<a href=\"javascript:document.forms[0].orderClause.value='ad.ad_street';document.forms[0].f_act.value='search';document.forms[0].submit();\">&nbsp;" . $titleArray["ad_street"] . "&nbsp;</a>";
   $titleArray["ad_zipcode"] = getLngt("PLZ"); $dspColHeaderLinksArray["ad_zipcode"] = "<a href=\"javascript:document.forms[0].orderClause.value='ad.ad_zipcode';document.forms[0].f_act.value='search';document.forms[0].submit();\">&nbsp;" . $titleArray["ad_zipcode"] . "&nbsp;</a>";
   $titleArray["ad_city"] = getLngt("Ort"); $dspColHeaderLinksArray["ad_city"] = "<a href=\"javascript:document.forms[0].orderClause.value='ad.ad_city';document.forms[0].f_act.value='search';document.forms[0].submit();\">&nbsp;" . $titleArray["ad_city"] . "&nbsp;</a>";

   $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>";
          
          // File export
          if ($fileOutput == "1" && $titleArray[$tmpKeys[$j]] != "") :
             $outArr2File[0][] .= mcStr($titleArray[$tmpKeys[$j]]);
          endif;
       endif;
   }
   // Add potential extra cols
   if ($f_pda_infos == "1") :
      $cellColor = getListColor(0, 0);
      $tableHeaderLinks .= "<td bgcolor=\"" . $cellColor . "\">" . $dspColHeaderLinksArray["cr_pda_infos"] . "</td>";

      // File export
      if ($fileOutput == "1" && $titleArray["cr_pda_infos"] != "") :
         $outArr2File[0][] .= mcStr($titleArray["cr_pda_infos"]);
      endif;
   endif;
endif;


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

   if ($doSearch || strlen($f_cr_sid) >= 2 || strlen($f_cr_eid) >= 2 || strlen($f_usr_name) >= 2 || strlen($f_usr_firstname) >= 2 || strlen($f_cmp_comp) >= 2 || strlen($f_cmp_freetext1) >= 3 || 
                    strlen($f_usr_phone) >= 2 || strlen($f_usr_email) >= 2 || strlen($f_usr_account) >= 2 || strlen($f_cr_mobile_pda) >= 2 || strlen($f_cr_imei) >= 3 ||
                    strlen($f_cr_serialno) >= 3 || strlen($f_crvh_vh_sign) >= 2 || 
                    strlen($f_crvh_comm_no) >= 2 || strlen($f_crvh_mobile) >= 2 || strlen($f_cr_vh_sign) >= 2 || strlen($f_crvh_prov) >= 2 || 
                    strlen($f_ad_street) >= 3 || strlen($f_ad_zipcode) >= 3 || strlen($f_ad_city) > 2 ||
                    strlen($f_crvh_remark) >= 2 || strlen($f_crvh_freetext_1) >= 2 || strlen($f_crvh_measurement) >= 1 ||
                    strlen($f_crvh_length) >= 1 || strlen($f_crvh_width) >= 1 || strlen($f_crvh_height) >= 1 || strlen($f_crvh_position) >= 1 || 
                    strlen($f_crvh_payload) >= 1 || strlen($f_crvh_totalweight) >= 1 || strlen($f_crvh_env_badge_level) >= 1 || strlen($f_crvh_corporate_identity) >= 1 || 
                    strlen($f_crvh_licence_date) >= 4 || strlen($f_crvh_drivetype) >= 1 || strlen($f_vht_id_inv) >= 1 || ($f_pda_infos == "1" && strlen($f_cr_pda_infos) >= 3) ) :

      // *******************************************
      // * Selection of the customers for the list *
      // *******************************************
      
      $fieldClause = "";
      $fromClause = "company AS cmp LEFT JOIN address AS ad ON ad.ad_id = cmp.ad_id, courier AS cr LEFT JOIN genericdatacontainer AS gdc_1 ON cr.cr_id = gdc_1.gdc_obj_id AND gdc_1.gdc_obj_type = 'cr' AND gdc_1.gdc_gen_fieldname = 'num_of_documents', user AS usr";

      $whereClause = "";
      if ($f_cmp_comp != "") : $whereClause .= "cmp.cmp_comp LIKE '%" . $f_cmp_comp . "%'"; endif;
      if ($whereClause != "" && $f_cmp_comp2 != "") : $whereClause .= " AND "; endif;
      if ($f_cmp_comp2 != "") : $whereClause .= "cmp.cmp_comp2 LIKE '%" . $f_cmp_comp2 . "%'"; endif;
      if ($whereClause != "" && $f_cmp_freetext1 != "") : $whereClause .= " AND "; endif;
      if ($f_cmp_freetext1 != "") : $whereClause .= "cmp.cmp_freetext1 LIKE '%" . $f_cmp_freetext1 . "%'"; endif;

      if (CR_SID_STATUS == "1") :
         $fieldClause = ", cr.cr_vh_sign";
         if ($whereClause != "" && $f_cr_sid != "") : $whereClause .= " AND "; endif;
         if ($f_cr_sid != "") : $whereClause .= "cr.cr_sid LIKE '%" . $f_cr_sid . "%'"; endif;
         if ($whereClause != "" && $f_crvh_vh_sign != "") : $whereClause .= " AND "; endif;
      else:
         $fieldClause = ", crvh.crvh_id, crvh.crvh_sid, crvh.crvh_vh_sign, crvh.crvh_comm_no, crvh.crvh_mobile, crvh.crvh_prov, crvh.crvh_remark, crvh.crvh_freetext_1,";
         $fieldClause .= " crvh.crvh_length, crvh.crvh_width, crvh.crvh_height, crvh.crvh_position, crvh.crvh_payload, crvh.crvh_totalweight, crvh.crvh_env_badge_level, crvh.crvh_corporate_identity, crvh.crvh_drivetype,";
         $fieldClause .= "CONCAT(crvh.crvh_length,',',crvh.crvh_width,',',crvh.crvh_height,',',crvh.crvh_position) AS crvh_measurement, LEFT(crvh.crvh_licence_date,7) AS crvh_licence_date ";
         $fromClause = "company AS cmp LEFT JOIN address AS ad ON ad.ad_id = cmp.ad_id, user AS usr, courier AS cr LEFT JOIN couriervehicle AS crvh ON cr.cr_id = crvh.cr_id ";         
         $fromClause .= "LEFT JOIN genericdatacontainer AS gdc_1 ON cr.cr_id = gdc_1.gdc_obj_id AND gdc_1.gdc_obj_type = 'cr' AND gdc_1.gdc_gen_fieldname = 'num_of_documents' ";
         $fromClause .= "LEFT JOIN genericdatacontainer AS gdc_2 ON crvh.crvh_id = gdc_2.gdc_obj_id AND gdc_2.gdc_obj_type = 'crvh' AND gdc_2.gdc_gen_fieldname = 'num_of_documents' ";

         // if ($whereClause != "") : $whereClause .= " AND "; endif;
         // $whereClause .= "cr.cr_id = crvh.cr_id";
         if ($whereClause != "" && $f_cr_sid != "") :
             $whereClause .= " AND "; 
         endif;
         if ($f_cr_sid != "") :             
            $whereClause .= "crvh.crvh_sid LIKE '%" . $f_cr_sid . "%'"; 
         endif;
         if ($f_vht_id_inv != "" && is_numeric($f_vht_id_inv) && $f_vht_id_inv > "0") :
            if ($whereClause != "" && $f_vht_id_inv != "") : $whereClause .= " AND "; endif;
            $whereClause .= "cr.vht_id_inv = '" . $f_vht_id_inv . "'";
         endif;
         if ($f_crvh_vh_sign != "") :
            if ($whereClause != "" && $f_crvh_vh_sign != "") : $whereClause .= " AND "; endif;
            $whereClause .= "crvh.crvh_vh_sign LIKE '%" . $f_crvh_vh_sign . "%'"; 
         endif;
         if ($f_crvh_comm_no != "") :
            if ($whereClause != "" && $f_crvh_comm_no != "") : $whereClause .= " AND "; endif;
            $whereClause .= "crvh.crvh_comm_no LIKE '%" . $f_crvh_comm_no . "%'"; 
         endif;
         if ($f_crvh_mobile != "") :
            if ($whereClause != "" && $f_crvh_mobile != "") : $whereClause .= " AND "; endif;
            $whereClause .= "crvh.crvh_mobile LIKE '%" . $f_crvh_mobile . "%'";
         endif;
         if ($f_crvh_prov != "" && is_numeric($f_crvh_prov)) :
            if ($whereClause != "" && $f_crvh_prov != "") : $whereClause .= " AND "; endif;
            $whereClause .= "crvh.crvh_prov LIKE '%" . $f_crvh_prov . "%'";
         endif;
         if ($f_crvh_remark != "") :
            if ($whereClause != "" && $f_crvh_remark != "") : $whereClause .= " AND "; endif;
            $whereClause .= "crvh.crvh_remark LIKE '%" . $f_crvh_remark . "%'"; 
         endif;
         if ($f_crvh_freetext_1 != "") :
            if ($whereClause != "" && $f_crvh_freetext_1 != "") : $whereClause .= " AND "; endif;
            $whereClause .= "crvh.crvh_freetext_1 LIKE '%" . $f_crvh_freetext_1 . "%'"; 
         endif;
         if ($f_crvh_measurement != "") :
            if ($whereClause != "" && $f_crvh_measurement != "") : $whereClause .= " AND "; endif;
            $whereClause .= "(crvh.crvh_length = '" . $f_crvh_measurement . "' OR crvh.crvh_width = '" . $f_crvh_measurement . "' OR crvh.crvh_height = '" . $f_crvh_measurement . "' OR crvh.crvh_position = '" . $f_crvh_measurement . "')";
         endif;
         if ($f_crvh_length != "" && is_numeric($f_crvh_length)) :
            if ($whereClause != "" && $f_crvh_length != "") : $whereClause .= " AND "; endif;
            $whereClause .= "crvh.crvh_length >= '" . $f_crvh_length . "'";
         endif;
         if ($f_crvh_width != "" && is_numeric($f_crvh_width)) :
            if ($whereClause != "" && $f_crvh_width != "") : $whereClause .= " AND "; endif;
            $whereClause .= "crvh.crvh_width >= '" . $f_crvh_width . "'";
         endif;
         if ($f_crvh_height != "" && is_numeric($f_crvh_height)) :
            if ($whereClause != "" && $f_crvh_height != "") : $whereClause .= " AND "; endif;
            $whereClause .= "crvh.crvh_height >= '" . $f_crvh_height . "'";
         endif;
         if ($f_crvh_position != "" && is_numeric($f_crvh_position)) :
            if ($whereClause != "" && $f_crvh_position != "") : $whereClause .= " AND "; endif;
            $whereClause .= "crvh.crvh_position >= '" . $f_crvh_position . "'";
         endif;
         if ($f_crvh_payload != "" && is_numeric($f_crvh_payload)) :
            if ($whereClause != "" && $f_crvh_payload != "") : $whereClause .= " AND "; endif;
            $whereClause .= "crvh.crvh_payload >= '" . $f_crvh_payload . "'";
         endif;
         if ($f_crvh_totalweight != "" && is_numeric($f_crvh_totalweight)) :
            if ($whereClause != "" && $f_crvh_totalweight != "") : $whereClause .= " AND "; endif;
            $whereClause .= "crvh.crvh_totalweight >= '" . $f_crvh_totalweight . "'";
         endif;
         if ($f_crvh_env_badge_level != "" && is_numeric($f_crvh_env_badge_level)) :
            if ($whereClause != "" && $f_crvh_env_badge_level != "") : $whereClause .= " AND "; endif;
            $whereClause .= "crvh.crvh_env_badge_level >= '" . $f_crvh_env_badge_level . "'";
         endif;
         if ($f_crvh_corporate_identity != "" && is_numeric($f_crvh_corporate_identity)) :
            if ($whereClause != "" && $f_crvh_corporate_identity != "") : $whereClause .= " AND "; endif;
            $whereClause .= "crvh.crvh_corporate_identity >= '" . $f_crvh_corporate_identity . "'";
         endif;
         if ($f_crvh_licence_date != "" && is_numeric($f_crvh_licence_date)) :
            if ($whereClause != "" && $f_crvh_licence_date != "") : $whereClause .= " AND "; endif;
            $whereClause .= "DATE_FORMAT(crvh.crvh_licence_date,'%Y%m') >= '" . $f_crvh_licence_date . "'";
         endif;
         if ($f_crvh_drivetype != "" && is_numeric($f_crvh_drivetype) && $f_crvh_drivetype > "0") :
            if ($whereClause != "" && $f_crvh_drivetype != "") : $whereClause .= " AND "; endif;
            $whereClause .= "crvh.crvh_drivetype = '" . $f_crvh_drivetype . "'";
         endif;
      endif;                                    


      if ($whereClause != "" && $f_cr_eid != "") : $whereClause .= " AND "; endif;
      if ($f_cr_eid != "") : $whereClause .= "cr.cr_eid LIKE '%" . $f_cr_eid . "%'"; endif;
      if ($whereClause != "" && $f_cr_mobile_pda != "") : $whereClause .= " AND "; endif;
      if ($f_cr_mobile_pda != "") : $whereClause .= "cr.cr_mobile_pda LIKE '%" . $f_cr_mobile_pda . "%'"; endif;
      if ($whereClause != "" && $f_cr_imei != "") : $whereClause .= " AND "; endif;
      if ($f_cr_imei != "") : $whereClause .= "cr.cr_imei LIKE '%" . $f_cr_imei . "%'"; endif;
      if ($whereClause != "" && $f_cr_serialno != "") : $whereClause .= " AND "; endif;
      if ($f_cr_serialno != "") : $whereClause .= "cr.cr_serialno LIKE '%" . $f_cr_serialno . "%'"; endif;
      if ($whereClause != "" && $f_usr_name != "") : $whereClause .= " AND "; endif;
      if ($f_usr_name != "") : $whereClause .= "usr.usr_name LIKE '%" . $f_usr_name . "%'"; endif;
      if ($whereClause != "" && $f_usr_firstname != "") : $whereClause .= " AND "; endif;
      if ($f_usr_firstname != "") : $whereClause .= "usr.usr_firstname LIKE '%" . $f_usr_firstname . "%'"; endif;
      if ($whereClause != "" && $f_usr_phone != "") : $whereClause .= " AND "; endif;
      if ($f_usr_phone != "") : $whereClause .= "usr.usr_phone LIKE '%" . $f_usr_phone . "%'"; endif;
      if ($whereClause != "" && $f_usr_email != "") : $whereClause .= " AND "; endif;
      if ($f_usr_email != "") : $whereClause .= "usr.usr_email LIKE '%" . $f_usr_email . "%'"; endif;
      if ($whereClause != "" && $f_usr_account != "") : $whereClause .= " AND "; endif;
      if ($f_usr_account != "") : $whereClause .= "usr.usr_account LIKE '%" . $f_usr_account . "%'"; endif;
      if ($whereClause != "" && $f_cmp_iln != "") : $whereClause .= " AND "; endif;      
      if ($f_cmp_iln != "") : $whereClause .= "cmp.cmp_iln LIKE '%" . $f_cmp_iln . "%'"; endif;
      if ($whereClause != "" && $f_cmp_tax_idno != "") : $whereClause .= " AND "; endif;
      if ($f_cmp_tax_idno != "") : $whereClause .= "cmp.cmp_tax_idno LIKE '%" . $f_cmp_tax_idno . "%'"; endif;
      if ($whereClause != "" && $f_cmp_bank != "") : $whereClause .= " AND "; endif;
      if ($f_cmp_bank != "") : $whereClause .= "cmp.cmp_bank LIKE '%" . $f_cmp_bank . "%'"; endif;
      if ($whereClause != "" && $f_cmp_bankno != "") : $whereClause .= " AND "; endif;
      if ($f_cmp_bankno != "") : $whereClause .= "cmp.cmp_bankno LIKE '%" . $f_cmp_bankno . "%'"; endif;
      if ($whereClause != "" && $f_cmp_bankacc != "") : $whereClause .= " AND "; endif;
      if ($f_cmp_bankacc != "") : $whereClause .= "cmp.cmp_bankacc LIKE '%" . $f_cmp_bankacc . "%'"; endif;

      // VPA infos
      if ($f_pda_infos == "1") :
         if ($whereClause != "" && $f_cr_pda_infos != "") : $whereClause .= " AND "; endif;
         if ($f_cr_pda_infos != "") : $whereClause .= "cr.cr_device_info LIKE '%" . $f_cr_pda_infos . "%'"; endif;
      endif;
      
      // LEFT JOIN table(s) ...
      if ($whereClause != "" && $f_ad_street != "") : $whereClause .= " AND "; endif;
      if ($f_ad_street != "") : $whereClause .= "ad.ad_street LIKE '" . $prefix . $f_ad_street . "%'"; endif;
      if ($whereClause != "" && $f_ad_zipcode != "") : $whereClause .= " AND "; endif;
      if ($f_ad_zipcode != "") : $whereClause .= "ad.ad_zipcode LIKE '" . $prefix . $f_ad_zipcode . "%'"; endif;
      if ($whereClause != "" && $f_ad_city != "") : $whereClause .= " AND "; endif;
      if ($f_ad_city != "") : $whereClause .= "ad.ad_city LIKE '" . $prefix . $f_ad_city . "%'"; endif;

      // Check authentication
      if ($whereClause != "" && $f_cmp_authenticated == "1") : $whereClause .= " AND "; endif;
      if ($f_cmp_authenticated == "1") : $whereClause .= "cmp.cmp_authenticated LIKE '" . $f_cmp_authenticated . "%'"; endif;
      
      // Check visibles      
      $whereClauseVisibility = " cmp.cmp_visible = '1' ";
      if ($f_show_invisible == "1") : $whereClauseVisibility = ""; endif;
      if ($whereClause != "" && $whereClauseVisibility != "") : $whereClauseVisibility = " AND " . $whereClauseVisibility; endif;
      $whereClause .= $whereClauseVisibility;
      
      // Check visibles INTERNAL
      $whereClauseVisibilityInternal = " cmp.cmp_visible_internal = '0' ";
      if ($f_show_invisible_internal == "1" && $empIdRootAdmin == $emp_id) : $whereClauseVisibilityInternal = ""; endif;
      if ($whereClause != "" && $whereClauseVisibilityInternal != "") : $whereClauseVisibilityInternal = " AND " . $whereClauseVisibilityInternal; endif;
      $whereClause .= $whereClauseVisibilityInternal;
      
      // Check only contractors visible
      $whereClauseContractors = "";
      if ($f_contractors_only == "1") : $whereClauseContractors = " (isnull(cr.cr_id_parent) OR cr.cr_id_parent = '0') "; endif;

      if ($whereClause != "" && $whereClauseContractors != "") : $whereClauseContractors = " AND " . $whereClauseContractors; endif;
      $whereClause .= $whereClauseContractors;

      // Check filter
      if ($g_filter != "") :
         // Get all filter for checking the filter type
         $filterArr = getColVectorFromDB2ArrayByClause("courierfilter", "crf_type", "crf_type IN (0,1)", "crf_short", "crf_short", "", "");
         // Check type of selected filters and define clause
         $tmpFilter = spliti(",",$g_filter);
         $lenTmp = count($tmpFilter);
         for ($i = 0; $i < $lenTmp; $i++) :
             if ($whereClause != "") : $whereClause .= " AND "; endif;
             if ($filterArr[$tmpFilter[$i]] == "1") :
                $whereClause .= " cr.cr_filter LIKE '%," . $tmpFilter[$i] . ",%' ";
             else :
                $whereClause .= " crvh.crvh_filter LIKE '%," . $tmpFilter[$i] . ",%' ";
             endif;
         endfor;
      endif;
      
      if ($whereClause != "") : $whereClause .= " AND "; endif;

      if ($orderClause == "") : $orderClause = "cmp.cmp_comp"; endif;
      
      
      $sqlquery = "SELECT cmp.cmp_id, cmp.cmp_comp, cmp.cmp_comp2, cmp.cmp_iln, cmp.cmp_tax_idno,"
                   . "    cmp.cmp_bank, cmp.cmp_bankno, cmp.cmp_bankacc, cmp.cmp_freetext1, cmp.cmp_authenticated,"
                   . "    cr.cr_id, cr.cr_sid, cr.cr_eid, cr.vht_id, cr.vht_id_inv, cr.cr_maxweight, cr.cr_outlay,"
                   . "    cr.cr_available, cr.cr_mobile_pda, cr.cr_imei, cr.cr_serialno,"
                   . "    gdc_1.gdc_content AS cr_num_of_documents, gdc_2.gdc_content AS crvh_num_of_documents,"
                   . "    ad.ad_street, ad.ad_zipcode, ad.ad_city,"
                   . "    usr.usr_id, usr.usr_name, usr.usr_firstname, usr.usr_email, usr.usr_account, usr.usr_phone,"
                   . "    usr.usr_phone2, usr.usr_fax" . $fieldClause                   
                   . " FROM " . $fromClause
                   . " WHERE " . $whereClause 
                   . "       cmp.cmp_id = cr.cmp_id AND"
                   . "       cmp.cmp_archived = '0' AND"
                   . "       cr.usr_id = usr.usr_id AND"
                   . "       usr.hq_id IN " . getSQLMandatorArray($f_hq_id)
                   . " ORDER BY " . $orderClause;
       // echo $sqlquery . "<br>";
      $result = $db->query($sqlquery);
      if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());


      // Table body
      $wordingWrapperObjecttypemode = "";
      if ($objecttypemode != "") :
         $wordingWrapperObjecttypemode = "&objecttypemode=" . ec($objecttypemode);
      endif;      
      $rowCounter = 0;
      $lineToggler = 0;
      while ($row = $result->fetch_assoc()):
            $numOfRows++;
            $dspColArray = array();
            $fieldArray = array();
   
            $tableOfRows .= "<tr>";            
            $fieldArray["cmp_comp"] = $row["cmp_comp"]; $dspColArray["cmp_comp"] = "<td__BGCOL__>&nbsp;" . $fieldArray["cmp_comp"] . "</td>";
            $fieldArray["cmp_comp2"] = $row["cmp_comp2"]; $dspColArray["cmp_comp2"] = "<td__BGCOL__>&nbsp;" . $fieldArray["cmp_comp2"] . "</td>";
            $fieldArray["cmp_freetext1"] = nl2br($row["cmp_freetext1"]); $dspColArray["cmp_freetext1"] = "<td__BGCOL__>&nbsp;" . $fieldArray["cmp_freetext1"] . "</td>";

            if (CR_SID_STATUS == "1") :
               // SID comes out of the table "courier"
               if ($f_mode == "0") :
                  $fieldArray["cr_sid"] = $row["cr_sid"]; $dspColArray["cr_sid"] = "<td__BGCOL__>&nbsp;<a href=\"javascript:setParentGeneric('" . $row["cr_id"] . "','" . $row["cr_sid"] . "');\">" . $row["cr_sid"] . "</a></td>";
               elseif ($f_mode == "1") :
                  $fieldArray["cr_sid"] = $row["cr_sid"]; $dspColArray["cr_sid"] = "<td__BGCOL__>&nbsp;<a href=\"javascript:setParent('" . $row["cr_id"] . "','" . $row["cr_sid"] . "');\">" . $row["cr_sid"] . "</a></td>";
               elseif ($f_mode == "2") :
                  $fieldArray["cr_sid"] = $row["cr_sid"]; $dspColArray["cr_sid"] = "<td__BGCOL__>&nbsp;<a href=\"javascript:setCustomerRelation('" . $row["cr_id"] . "','" . $row["cr_eid"] . "','" . $row["cr_sid"] . "','" . $row["usr_firstname"] . "','" . $row["usr_name"] . "','" . $row["usr_phone"] . "','" . $row["usr_email"] . "');\">" . $row["cr_sid"] . "</a></td>";
               else:                  
                  $fieldArray["cr_sid"] = $row["cr_sid"] ; $dspColArray["cr_sid"] = "<td__BGCOL__>&nbsp;" . $fieldArray["cr_sid"] . "</td>";
               endif;
               $fieldArray["cr_eid"] = $row["cr_eid"]; $dspColArray["cr_eid"] = "<td__BGCOL__>&nbsp;" . $fieldArray["cr_eid"] . "</td>";
            else :
               // SID comes out of the table "couriervehicle"
               $fieldArray["cr_sid"] = $row["crvh_sid"]; $dspColArray["cr_sid"] = "<td__BGCOL__>&nbsp;" . $fieldArray["cr_sid"] . "</td>";  
               
               $fieldArray["crvh_vh_sign"] = $row["crvh_vh_sign"]; $dspColArray["crvh_vh_sign"] = "<td__BGCOL__>&nbsp;" . $fieldArray["crvh_vh_sign"] . "</td>";
               $fieldArray["crvh_comm_no"] = $row["crvh_comm_no"]; $dspColArray["crvh_comm_no"] = "<td__BGCOL__>&nbsp;" . $fieldArray["crvh_comm_no"] . "</td>";
               $fieldArray["crvh_mobile"] =  $row["crvh_mobile"]; $dspColArray["crvh_mobile"] = "<td__BGCOL__>&nbsp;" . $fieldArray["crvh_mobile"] . "</td>";
               if ($f_mode == "0") :
                  $fieldArray["cr_eid"] = $row["cr_eid"]; $dspColArray["cr_eid"] = "<td__BGCOL__>&nbsp;<a href=\"javascript:setParentGeneric('" . $row["cr_id"] . "','" . $row["cr_eid"] . "');\">" . $row["cr_eid"] . "</a></td>";
               elseif ($f_mode == "1") :
                  $fieldArray["cr_eid"] = $row["cr_eid"]; $dspColArray["cr_eid"] = "<td__BGCOL__>&nbsp;<a href=\"javascript:setParent('" . $row["cr_id"] . "','" . $row["cr_eid"] . "');\">" . $row["cr_eid"] . "</a></td>";
               elseif ($f_mode == "2") :
                  $fieldArray["cr_eid"] = $row["cr_eid"]; $dspColArray["cr_eid"] = "<td__BGCOL__>&nbsp;<a href=\"javascript:setCustomerRelation('" . $row["cr_id"] . "','" . $row["cr_eid"] . "','" . $row["crvh_sid"] . "','" . $row["usr_firstname"] . "','" . $row["usr_name"] . "','" . $row["usr_phone"] . "','" . $row["usr_email"] . "');\">" . $row["cr_eid"] . "</a></td>";
               elseif ($f_mode == "3") :
                  $fieldArray["cr_eid"] = $row["cr_eid"]; $dspColArray["cr_eid"] = "<td__BGCOL__>&nbsp;<a href=\"javascript:setSingleCourier('" . $row["cr_id"] . "','" . $row["cr_eid"] . "');\">" . $row["cr_eid"] . "</a></td>";
               else:
                  $fieldArray["cr_eid"] = $row["cr_eid"]; $dspColArray["cr_eid"] = "<td__BGCOL__>&nbsp;" . $row["cr_eid"] . "</td>";

                  // Overwrite!
                  $fieldArray["cr_sid"] = $row["crvh_sid"]; $dspColArray["cr_sid"] = "<td__BGCOL__>&nbsp;<a href=\"javascript:openCourierVehicles('" . ec($row["cr_id"]) . "','" . $row["crvh_id"] . "');\">" . $row["crvh_sid"] . "</a></td>";
               endif;
               $fieldArray["crvh_prov"] = $row["crvh_prov"]; $dspColArray["crvh_prov"] = "<td align=\"right\"__BGCOL__>&nbsp;" . $fieldArray["crvh_prov"] . "&nbsp;&nbsp;</td>";
               $fieldArray["crvh_remark"] = $row["crvh_remark"]; $dspColArray["crvh_remark"] = "<td align=\"left\"__BGCOL__>&nbsp;" . $fieldArray["crvh_remark"] . "&nbsp;&nbsp;</td>";
               $fieldArray["crvh_freetext_1"] = $row["crvh_freetext_1"]; $dspColArray["crvh_freetext_1"] = "<td align=\"left\"__BGCOL__>&nbsp;" . $fieldArray["crvh_freetext_1"] . "&nbsp;&nbsp;</td>";
               $fieldArray["crvh_measurement"] = $row["crvh_measurement"]; $dspColArray["crvh_measurement"] = "<td align=\"left\"__BGCOL__>&nbsp;" . $fieldArray["crvh_measurement"] . "&nbsp;&nbsp;</td>";
               $fieldArray["crvh_length"] = $row["crvh_length"]; $dspColArray["crvh_length"] = "<td align=\"left\"__BGCOL__>&nbsp;" . $fieldArray["crvh_length"] . "&nbsp;&nbsp;</td>";
               $fieldArray["crvh_width"] = $row["crvh_width"]; $dspColArray["crvh_width"] = "<td align=\"left\"__BGCOL__>&nbsp;" . $fieldArray["crvh_width"] . "&nbsp;&nbsp;</td>";
               $fieldArray["crvh_height"] = $row["crvh_height"]; $dspColArray["crvh_height"] = "<td align=\"left\"__BGCOL__>&nbsp;" . $fieldArray["crvh_height"] . "&nbsp;&nbsp;</td>";
               $fieldArray["crvh_position"] = $row["crvh_position"]; $dspColArray["crvh_position"] = "<td align=\"left\"__BGCOL__>&nbsp;" . $fieldArray["crvh_position"] . "&nbsp;&nbsp;</td>";
               $fieldArray["crvh_payload"] = $row["crvh_payload"]; $dspColArray["crvh_payload"] = "<td align=\"left\"__BGCOL__>&nbsp;" . $fieldArray["crvh_payload"] . "&nbsp;&nbsp;</td>";
               $fieldArray["crvh_env_badge_level"] = $row["crvh_env_badge_level"]; $dspColArray["crvh_env_badge_level"] = "<td align=\"left\"__BGCOL__>&nbsp;" . $fieldArray["crvh_env_badge_level"] . "&nbsp;&nbsp;</td>";
               // $fieldArray["crvh_corporate_identity"] = $vhtCINamesArray[$row["crvh_corporate_identity"]]; $dspColArray["crvh_corporate_identity"] = "<td align=\"left\"__BGCOL__>&nbsp;" . $vhtCINamesArray[$row["crvh_corporate_identity"]] . "&nbsp;[" . $row["crvh_corporate_identity"] . "]&nbsp;&nbsp;</td>";
               $tmpCI1 = ((int)$row["crvh_corporate_identity"] & 15);
               $tmpCI2 = (((int)$row["crvh_corporate_identity"] / 16) & 15);
               $fieldArray["crvh_corporate_identity"] = mcArrIsSet($vhtCINamesArray, $tmpCI1) . ($tmpCI1 > 0 || $tmpCI2 > 0 ? "&nbsp;|&nbsp;" : "") . mcArrIsSet($vhtCINamesArray, $tmpCI2); $dspColArray["crvh_corporate_identity"] = "<td align=\"left\"__BGCOL__>&nbsp;" . $fieldArray["crvh_corporate_identity"] . "&nbsp;&nbsp;</td>";
               $fieldArray["crvh_licence_date"] = $row["crvh_licence_date"]; $dspColArray["crvh_licence_date"] = "<td align=\"left\"__BGCOL__>&nbsp;" . $fieldArray["crvh_licence_date"] . "&nbsp;&nbsp;</td>";
               $fieldArray["crvh_drivetype"] = str_replace(" ","&nbsp;",mcArrIsSet($vhtDriveTypeArray, $row["crvh_drivetype"])); $dspColArray["crvh_drivetype"] = "<td align=\"left\"__BGCOL__>&nbsp;" . $fieldArray["crvh_drivetype"] . "&nbsp;&nbsp;</td>";
               $fieldArray["vht_id_inv"] = str_replace(" ","&nbsp;",mcArrIsSet($vhtTypeArray, $row["vht_id_inv"])); $dspColArray["vht_id_inv"] = "<td align=\"left\"__BGCOL__>&nbsp;" . $fieldArray["vht_id_inv"] . "&nbsp;&nbsp;</td>";
               $fieldArray["crvh_totalweight"] = $row["crvh_totalweight"]; $dspColArray["crvh_totalweight"] = "<td align=\"left\"__BGCOL__>&nbsp;" . $fieldArray["crvh_totalweight"] . "&nbsp;&nbsp;</td>";
            endif;

            // $fieldArray[""] = ; $dspColArray["usr_name"] = "<td__BGCOL__>&nbsp;" . $row["usr_name"] . "</td>";
            $fieldArray["usr_name"] = $row["usr_name"]; $dspColArray["usr_name"] = "<td__BGCOL__>&nbsp;<a href=\"../admin/courier_special.php?companyId=" . ec($row["cmp_id"]) . $wordingWrapperObjecttypemode . "\" target=\"_blank\">" . $fieldArray["usr_name"] . "</a></td>";
            $fieldArray["usr_firstname"] = $row["usr_firstname"]; $dspColArray["usr_firstname"] = "<td__BGCOL__>&nbsp;" . $fieldArray["usr_firstname"] . "</td>";
            $fieldArray["usr_phone"] = $row["usr_phone"]; $dspColArray["usr_phone"] = "<td__BGCOL__>&nbsp;" . $fieldArray["usr_phone"] . "</td>";
            $fieldArray["usr_email"] = $row["usr_email"]; $dspColArray["usr_email"] = "<td__BGCOL__>&nbsp;" . $fieldArray["usr_email"] . "</td>";
            $fieldArray["usr_account"] = $row["usr_account"]; $dspColArray["usr_account"] = "<td__BGCOL__>&nbsp;" . $fieldArray["usr_account"] . "</td>";
            $fieldArray["cr_mobile_pda"] = $row["cr_mobile_pda"]; $dspColArray["cr_mobile_pda"] = "<td__BGCOL__>&nbsp;" . $fieldArray["cr_mobile_pda"] . "</td>";
            $fieldArray["cr_imei"] = $row["cr_imei"]; $dspColArray["cr_imei"] = "<td__BGCOL__>&nbsp;" . $fieldArray["cr_imei"] . "</td>";
            $fieldArray["cr_serialno"] = $row["cr_serialno"]; $dspColArray["cr_serialno"] = "<td__BGCOL__>&nbsp;" . $fieldArray["cr_serialno"] . "</td>";

            if ($f_pda_infos == "1") :
               $crPdaInfos = getFieldValueFromId("courier", "cr_id", $row["cr_id"], "cr_device_info");
               /*
               if ($crPdaInfos == "" && $db_conn != "ERR_DB") :
                  $crPdaInfos = getFieldValueFromClause("conn_context","cc_appver","cust_id = '1' AND inst_id = '0' AND usr_id = '" . $row["usr_id"] . "'", $db_conn);
               endif;
               */
               $fieldArray["cr_pda_infos"] = getVPAInfoData($crPdaInfos, "1");  // Init
               $dspColArray["cr_pda_infos"] = "<td__BGCOL__>&nbsp;" . getVPAInfoData($crPdaInfos) . "</td>";
            endif;

            $fieldArray["ad_street"] = $row["ad_street"]; $dspColArray["ad_street"] = "<td__BGCOL__>&nbsp;" . $fieldArray["ad_street"] . "</td>";
            $fieldArray["ad_zipcode"] = $row["ad_zipcode"]; $dspColArray["ad_zipcode"] = "<td__BGCOL__>&nbsp;" . $fieldArray["ad_zipcode"] . "</td>";
            $fieldArray["ad_city"] = $row["ad_city"]; $dspColArray["ad_city"] = "<td__BGCOL__>&nbsp;" . $fieldArray["ad_city"] . "</td>";

            $authImgName = "circle_red.png";
            $fieldArray["cmp_authenticated_img"] = getLngt("Nein");
            if ($row["cmp_authenticated"] == "1") :
               $authImgName = "circle_green.png";
               $fieldArray["cmp_authenticated_img"] = getLngt("Ja");
            endif;
            $dspColArray["cmp_authenticated_img"] = "<td align=\"center\"__BGCOL__><img src=\"../images/" . $authImgName . "\" border=\"0\" height=\"10\" width=\"25\"></td>";

            if ($f_mode != "0" && $f_mode != "1" && $f_mode != "2" && $f_mode != "3") :
               
               // History of the courier
               // $fieldArray["cr_id_history"] = "";
               $dspColArray["cr_id_history"] = "<td align=\"center\"__BGCOL__><a href=\"../admin/history.php?history_mode=" . ec(1) . "&cr_id=" . ec($row["cr_id"]) . "&op=0\" target=\"_blank\">"
                                               . "<img src=\"../images/arrow_right.png\" border=\"0\" height=\"10\" width=\"25\">"
                                               . "</a></td>";
               // Reports of the courier                                
               // $fieldArray["cr_id_report"] = "";
               $dspColArray["cr_id_report"] =  "<td align=\"center\"__BGCOL__><a href=\"../groupware/report.php?rpObjType=cr&g_cr_id=" . ec($row["cr_id"]) . "\" target=\"_blank\">"
                                               . "<img src=\"../images/arrow_right.png\" border=\"0\" height=\"10\" width=\"25\">"
                                               . "</a></td>";
               // Edit the courier
               // $fieldArray["cmp_id_edit"] = "";
               $dspColArray["cmp_id_edit"] = "<td align=\"center\"__BGCOL__><a href=\"../admin/courier_special.php?companyId=" . ec($row["cmp_id"]) . $wordingWrapperObjecttypemode . "\" target=\"_blank\">"
                                             . "<img src=\"../images/arrow_right.png\" border=\"0\" height=\"10\" width=\"25\">" 
                                             . "</a></td>";

               // Number of documents associated to the courier
               $fieldArray["cr_num_of_documents"] = ($row["cr_num_of_documents"] != "" && $row["cr_num_of_documents"] != "0" ? $row["cr_num_of_documents"] : "0"); 
               $dspColArray["cr_num_of_documents"] = "<td align=\"center\"__BGCOL__>"
                                                     . "<a href=\"../import/data_transfer.php?objType=cr&objId=" . ec($row["cr_id"]) . "\" target=\"_blank\">" . ($row["cr_num_of_documents"] != "" && $row["cr_num_of_documents"] != "0" ? "[" . $row["cr_num_of_documents"] . "]" : "&nbsp;") . "</a>"
                                                     . "</td>";

               // Number of documents associated to the vehicle
               $fieldArray["crvh_num_of_documents"] = ($row["crvh_num_of_documents"] != "" && $row["crvh_num_of_documents"] != "0" ?  $row["crvh_num_of_documents"] : "0");
               $dspColArray["crvh_num_of_documents"] = "<td align=\"center\"__BGCOL__>"
                                                       . "<a href=\"../import/data_transfer.php?objType=crvh&objId=" . ec($row["crvh_id"]) . "\" target=\"_blank\">" . ($row["crvh_num_of_documents"] != "" && $row["crvh_num_of_documents"] != "0" ? "[" . $row["crvh_num_of_documents"] . "]" : "&nbsp;") . "</a>"
                                                       . "</td>";
            endif;

            // 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)) :
   	               if ($lineToggler == 0) : $lineToggler = 1; else : $lineToggler = 0; endif;
                   $cellColor = getListColor($rowCounter, $lineToggler);
                   $dspColArray[$tmpKeys[$j]] = str_replace("__BGCOL__", " bgcolor=\"" . $cellColor ."\"", $dspColArray[$tmpKeys[$j]]);
                   $tableOfRows .= $dspColArray[$tmpKeys[$j]];
                   
                   // File export
                   if ($fileOutput == "1" && $titleArray[$tmpKeys[$j]] != "") :
                      $outArr2File[$rowCounter][] .= mcStr($fieldArray[$tmpKeys[$j]]);
                   endif;
                endif;
            }
            // Add potential extra cols
            if ($f_pda_infos == "1") :
               if ($lineToggler == 0) : $lineToggler = 1; else : $lineToggler = 0; endif;
               $cellColor = getListColor($rowCounter, $lineToggler);
               $dspColArray["cr_pda_infos"] = str_replace("__BGCOL__", " bgcolor=\"" . $cellColor ."\"", $dspColArray["cr_pda_infos"]);
               $tableOfRows .= $dspColArray["cr_pda_infos"];

               // File export
               if ($fileOutput == "1" && $titleArray["cr_pda_infos"] != "") :
                  $outArr2File[$rowCounter][] .= mcStr($fieldArray["cr_pda_infos"]);
               endif;
            endif;

            $tableOfRows .= "</tr>";
      endwhile;
      $result->free();

      // Optional output to file
      $f_secretFileName = "";
      if ($fileOutput == "1") :

         // Remove old files
         foreach (glob($tempPath . "*.cr_list.csv") as $fileNameToBoRemoved) {
             if ((time() - filemtime($fileNameToBoRemoved)) > 60) :
                if (file_exists($fileNameToBoRemoved)) :
                   unlink($fileNameToBoRemoved);
                endif;
             endif;
         }
      
         // Convert matrix to CSV
         $outputLinesArray = convertMatrixToCsv($outArr2File);
         $outputLinesArrayLen = count($outputLinesArray);
         if ($outputLinesArrayLen > 0) :
      
            $f_fileName = "COURIERLIST_" . date("YmdHis");
            $f_secretFileName = md5($f_fileName) . ".cr_list.csv";
      
            for ($i = 0; $i < $outputLinesArrayLen; $i++) :                   
                writeToFile($tempPath . $f_secretFileName, $outputLinesArray[$i]);   // write statistic data
            endfor;
         else :
            $statusMessage = "Es wurden keine Daten gefunden! Eine Datei wurde deshalb nicht generiert!";
            $fileOutput = "0";
         endif;
      endif;
            
   else :
     $statusMessage .= getLngt("Bei Eingabe weniger als 2 Zeichen in mindestens einem Feld erfolgt keine Suche!");
   endif;            
endif;   

// Output
$jsOut = "";
// Set relation to calling parent field
if ($f_mode == "0") :
   $jsOut .= "function setParentGeneric(crParentId,crParentValue) {\n";
   $jsOut .= "  opener.document.forms[0]." . $genJsParName . ".value = crParentValue;\n";
   $jsOut .= "  this.close();\n";
   $jsOut .= "};\n";
endif;
// Set relation between courier and subcontractor
if ($f_mode == "1") :
   $jsOut .= "function setParent(crParentId,crParentValue) {\n";
   $jsOut .= "  opener.document.forms[0].f_cr_id_parent.value = crParentId;\n";
   if (CR_SID_STATUS == "1") :
      $jsOut .= "  opener.document.forms[0].f_cr_sid_parent.value = crParentValue;\n";
   else :
      $jsOut .= "  opener.document.forms[0].f_cr_eid_parent.value = crParentValue;\n";
   endif;
   $jsOut .= "  this.close();\n";
   $jsOut .= "};\n";
endif;
// Set relation between customer and courier (blocked/favoured)
if ($f_mode == "2") :
   $jsOut .= "function setCustomerRelation(crId,crEid,crSid,crFirstname,crName,crPhone,crEmail) {\n";
   $jsOut .= "  opener.document.forms[0].g_cr_id.value = crId;\n";
   $jsOut .= "  opener.document.forms[0].g_cr_eid.value = crEid;\n";
   $jsOut .= "  opener.document.forms[0].g_cr_sid.value = crSid;\n";
   $jsOut .= "  opener.document.forms[0].g_usr_firstname.value = crFirstname;\n";
   $jsOut .= "  opener.document.forms[0].g_usr_name.value = crName;\n";
   $jsOut .= "  opener.document.forms[0].g_usr_phone.value = crPhone;\n";
   $jsOut .= "  opener.document.forms[0].g_usr_email.value = crEmail;\n";
   $jsOut .= "  this.close();\n";
   $jsOut .= "};\n";
endif;
// Set single courier (statistics, etc.)
if ($f_mode == "3") :
   $jsOut .= "function setSingleCourier(crId,crEid) {\n";
   $jsOut .= "  opener.document.forms[0].g_cr_id.value = crId;\n";
   $jsOut .= "  opener.document.forms[0].g_cr_eid.value = crEid;\n";
   $jsOut .= "  this.close();\n";
   $jsOut .= "};\n";
endif;

// Link to enter a new customer or to switch "visibility output"
$wordingWrapperObjecttypemode = "";
if ($objecttypemode != "") :
   $wordingWrapperObjecttypemode = "?objecttypemode=" . ec($objecttypemode);
endif;
$headerOps = "&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;";
$headerOps .= "<a href=\"../admin/courier_special.php" . $wordingWrapperObjecttypemode . "\" target=\"_blank\">" . getLngt("Neuer " . wrapPhrase("Transporteur", $objecttypemode)) . "</a>";
$headerOps .= "&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;";
$headerOps .= getLngt("Nur Unternehmer anzeigen") . "&nbsp;<input type=\"checkbox\" name=\"f_contractors_only[]\" value=\"1\" " . ($f_contractors_only == "1" ? "checked" : "") . " tabindex=\"\">";
$headerOps .= "&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;";
$headerOps .= getLngt("VPA-Infos anzeigen") . "&nbsp;<input type=\"checkbox\" name=\"f_pda_infos[]\" value=\"1\" " . ($f_pda_infos == "1" ? "checked" : "") . " tabindex=\"\">";
$headerOps .= "&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;";
$headerOps .= getLngt("Ausgeblendete&nbsp;anzeigen") . "&nbsp;<input type=\"checkbox\" name=\"f_show_invisible[]\" value=\"1\" " . ($f_show_invisible == "1" ? "checked" : "") . " tabindex=\"\">";
if ($empIdRootAdmin == $emp_id) :
   $headerOps .= "&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;";
   $headerOps .= getLngt("Interne&nbsp;anzeigen") . "&nbsp;<input type=\"checkbox\" name=\"f_show_invisible_internal[]\" value=\"1\" " . ($f_show_invisible_internal == "1" ? "checked" : "") . " tabindex=\"\">";
endif;
if ($empHasAccessCrCsvExport) :
   $headerOps .= "&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;";
   $headerOps .= getLngt("CSV-Export") . "&nbsp;<input type=\"checkbox\" name=\"fileOutput[]\" value=\"1\" " . ($fileOutput == "1" ? "checked" : "") . " tabindex=\"\">";
endif;

// Special search fields
$extraSearchFields = "<div style=\"float:left\"><span class=\"f8np1\">&nbsp;" . getLngt("Filter:") . "&nbsp;</span>" .
                     "<input class=\"f8np1\" type=\"text\" name=\"g_filter\" value=\"" . $g_filter . "\" size=\"20\" readonly >&nbsp;</div>" .
                     defineButtonType08("...", "action_filter", "openFilters();", "20", "left", "2");
                     
// print_r($outArr2File); echo "\n\n<br><br>";
?>

<html>

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

<style type="text/css">
  <?php include_once ("../css/phoenix.css.php"); ?>
  <?php include_once ("../css/navigation.css.php"); ?>
  <?php include_once ("../css/mc.css.php"); ?>
</style>

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

<script src="../include/checkFormTags.js" type="text/javascript"></script>

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

function setFocus() {
  document.forms[0].f_cr_sid.focus();
};

function removeCustomer(cmp_id) {
  if (confirm('<?php echo getLngt("Kunde wirklich entfernen?") ?>')) {
     document.forms[0].f_act.value = '<?php echo ec("removeCourier") ?>';
     document.forms[0].companyId.value = cmp_id;
     document.forms[0].submit();
  };
};

function finishPage() {
  document.forms[0].f_act.value='<?php echo ec("search") ?>';
  document.forms[0].submit();  
};

function clearFields() {
	if (document.forms[0].f_cmp_comp) {document.forms[0].f_cmp_comp.value = ''};
	if (document.forms[0].f_cmp_freetext1) {document.forms[0].f_cmp_freetext1.value = ''};
  if (document.forms[0].f_cr_sid) {document.forms[0].f_cr_sid.value = ''};
  if (document.forms[0].f_cr_eid) {document.forms[0].f_cr_eid.value = ''};
  if (document.forms[0].f_usr_name) {document.forms[0].f_usr_name.value = ''};
  if (document.forms[0].f_usr_firstname) {document.forms[0].f_usr_firstname.value = ''};
  if (document.forms[0].f_usr_phone) {document.forms[0].f_usr_phone.value = ''};
  if (document.forms[0].f_usr_email) {document.forms[0].f_usr_email.value = ''};
  if (document.forms[0].f_usr_account) {document.forms[0].f_usr_account.value = ''};
  if (document.forms[0].f_cr_mobile_pda) {document.forms[0].f_cr_mobile_pda.value = ''};
  if (document.forms[0].f_cr_imei) {document.forms[0].f_cr_imei.value = ''};
  if (document.forms[0].f_cr_serialno) {document.forms[0].f_cr_serialno.value = ''};
  <?php if (CR_SID_STATUS == "1") : ?>
    if (document.forms[0].f_cr_vh_sign) {document.forms[0].f_cr_vh_sign.value = ''};
  <?php else : ?>
    if (document.forms[0].f_crvh_vh_sign) {document.forms[0].f_crvh_vh_sign.value = ''};
    if (document.forms[0].f_crvh_comm_no) {document.forms[0].f_crvh_comm_no.value = ''};
    if (document.forms[0].f_crvh_mobile) {document.forms[0].f_crvh_mobile.value = ''};
    if (document.forms[0].f_crvh_prov) {document.forms[0].f_crvh_prov.value = ''};
    if (document.forms[0].f_crvh_remark) {document.forms[0].f_crvh_remark.value = ''};
    if (document.forms[0].f_crvh_freetext_1) {document.forms[0].f_crvh_freetext_1.value = ''};
    if (document.forms[0].f_crvh_measurement) {document.forms[0].f_crvh_measurement.value = ''};
    if (document.forms[0].f_crvh_length) {document.forms[0].f_crvh_length.value = ''};
    if (document.forms[0].f_crvh_width) {document.forms[0].f_crvh_width.value = ''};
    if (document.forms[0].f_crvh_height) {document.forms[0].f_crvh_height.value = ''};
    if (document.forms[0].f_crvh_position) {document.forms[0].f_crvh_position.value = ''};
    if (document.forms[0].f_crvh_payload) {document.forms[0].f_crvh_payload.value = ''};
    if (document.forms[0].f_crvh_env_badge_level) {document.forms[0].f_crvh_env_badge_level.value = ''};
    if (document.forms[0].f_crvh_corporate_identity) {document.forms[0].f_crvh_corporate_identity.value = ''};
    if (document.forms[0].f_crvh_licence_date) {document.forms[0].f_crvh_licence_date.value = ''};
    // if (document.forms[0].f_crvh_drivetype) {document.forms[0].f_crvh_drivetype.value = ''};
    if (document.forms[0].f_crvh_drivetype) {setSelectedValue('f_crvh_drivetype', '0')};
    if (document.forms[0].f_vht_id_inv) {setSelectedValue('f_vht_id_inv', '0')};
    if (document.forms[0].f_crvh_totalweight) {document.forms[0].f_crvh_totalweight.value = ''};
  <?php endif; ?>
  if (document.forms[0].f_ad_street) {document.forms[0].f_ad_street.value = ''};
  if (document.forms[0].f_ad_zipcode) {document.forms[0].f_ad_zipcode.value = ''};
  if (document.forms[0].f_ad_city) {document.forms[0].f_ad_city.value = ''};
  if (document.forms[0].g_filter) {document.forms[0].g_filter.value = ''};
  <?php if ($f_pda_infos == "1") : ?>
    if (document.forms[0].f_cr_pda_infos) {document.forms[0].f_cr_pda_infos.value = ''};
    if (document.forms[0].f_cr_pda_infos_device) {setSelectedValue('f_cr_pda_infos_device', '')};
  <?php endif; ?>
};

function openCourierSpecial(cmpId) {
  var widthPopupWin = 900;
  var heightPopupWin = 750;
  var leftPopupWin = (screen.width / 2) - (widthPopupWin / 2) - 12;
  var topPopupWin = (screen.height / 2) - (heightPopupWin / 2) - 50;
  var popupWin;
  popupWin = window.open("../admin/courier_special.php?companyId=" + cmpId ,"","dependent=yes,width=" + widthPopupWin + ",height=" + heightPopupWin +",left=" + leftPopupWin + ",top=" + topPopupWin + ",scrollbars=yes");
};

function openFilters() {
  var widthPopupWin = 900;
  var heightPopupWin = 700;
  var leftPopupWin = (screen.width / 2) - (widthPopupWin / 2) - 12;
  var topPopupWin = (screen.height / 2) - (heightPopupWin / 2) - 50;
  var popupWin;
  var f_filter_str = document.forms[0].g_filter.value;
  popupWin = window.open("../admin/filter.php?f_crvh_filter_str=" + f_filter_str + "&openerParameter=g_filter","","dependent=yes,width=" + widthPopupWin + ",height=" + heightPopupWin +",left=" + leftPopupWin + ",top=" + topPopupWin + ",scrollbars=yes");
};

function openCourierVehicles(courierId, crvhId) {
  var widthPopupWin = 1100;
  var heightPopupWin = 900;
  var leftPopupWin = (screen.width / 2) - (widthPopupWin / 2) - 12;
  var topPopupWin = (screen.height / 2) - (heightPopupWin / 2) - 50;
  var popupWin;
  popupWin = window.open("../admin/courier_vehicle.php?courierId=" + courierId + "&f_crvh_id=" + crvhId,"","dependent=yes,width=" + widthPopupWin + ",height=" + heightPopupWin +",left=" + leftPopupWin + ",top=" + topPopupWin + ",scrollbars=yes");
};

function downloadFile() {
  var widthPopupWin = 850;
  var heightPopupWin = 600;
  var leftPopupWin = (screen.width / 2) - (widthPopupWin / 2) - 12;
  var topPopupWin = (screen.height / 2) - (heightPopupWin / 2) - 50;
  var popupWin;

  popupWin = window.open("../include/data_download.php?f_path=<?php echo 'EMC2' . substr($tempPath, 2); ?>&f_fileName=<?php echo $f_secretFileName ?>", "","dependent=yes,width=" + widthPopupWin + ",height=" + heightPopupWin +",left=" + leftPopupWin + ",top=" + topPopupWin + ",scrollbars=yes");
};

<?php echo $jsOut ?>
-->
</script>
</head>

<body onLoad="<?php echo $phpCurrentNavigationOnLoad ?>displayStatusMessage();setFocus();<?php if ($fileOutput == "1") : echo "downloadFile();"; endif; ?> ">

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

<?php if ($pageHeadline != "") : ?>
  <div class="mc_page-header" id="mc_page-header">
    <?php echo $pageHeadline ?>
  </div>
<?php endif ?>

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

  <form action="../admin/courier_list.php" method="post">
  <input type="hidden" name="f_act" value="">
  <input type="hidden" name="f_mode" value="<?php echo $f_mode ?>">
  <input type="hidden" name="genJsParName" value="<?php echo $genJsParName ?>">
  <input type="hidden" name="orderClause" value="<?php echo $orderClause ?>">
  <?php echo $phpCurrentNavigationInputHidden ?>
  <input type="hidden" name="deactivateMenu" value="<?php echo ec($deactivateMenu) ?>">
  <input type="hidden" name="objecttypemode" value="<?php echo ec($objecttypemode) ?>">

  <?php echo htmlDivLineSpacer("10px"); ?>

  <!-- Headquarters checkboxes -->
  <?php if (authCheckEmployeeRights($emp_id, "10")) : ?>
    <?php echo getHeadquartersCheckboxes($f_hq_id); ?>
    <?php echo htmlDivLineSpacer("10px", "", "left"); ?>
  <?php endif; ?>

  <div>
    <a href="javascript:document.forms[0].f_act.value='search';document.forms[0].submit();">&nbsp;<?php echo getLngt("Suchen") ?>&nbsp;</a>
    &nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;
    <a href="javascript:clearFields();"><?php echo getLngt("Felder zurücksetzen") ?></a>
    <?php echo $headerOps ?>
  </div>

  <?php echo htmlDivLineSpacer("10px"); ?>
  
  <div>
    <?php echo $extraSearchFields ?>
  </div>

  <?php echo htmlDivLineSpacer("10px", "", "left"); ?>
  
  <div>
    <table class="f8np1" border="0" cellpadding="0">
      <tr>
        <?php echo $tableHeaderSearchFields ?>
      </tr>
      <tr>
        <?php echo $tableHeaderLinks ?>
      </tr>                                
      <?php echo $tableOfRows ?>
    </table>
  </div>

  <?php echo htmlDivLineSpacer("10px"); ?>

  <div>
    <?php echo getLngt("Anzahl Einträge:") ?> <?php echo $numOfRows ?><?php if ($numOfRows == "0" && $f_act == "search" && $statusMessage == "") : echo " " . getLngt("(Keine Einträge gefunden.)"); endif; ?>
  </div>

  </form>
</div>

</body>

</html>
