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

  // Check HTTP-Parameters
  getSecHttpVars("1",array("f_act", "retMode", "objType", "searchValue", "fields", "hqAccess", "wrap_html"));
  
  if ($retMode != "") :
     header("Content-Type: text/html; charset=ISO-8859-1\n");
  endif;


  function wrap_html($aStr) {
  	global $wrap_html;
  	if ($wrap_html == "1") :
  	   // $aStr = my_str_check_html($aStr);
       $aStr = str_replace("%u20AC", '&euro;', $aStr);
    endif;
    return $aStr;
  }


  $retValue = "";

  if ($searchValue != "") : 
  
     $searchValue = urldecode($searchValue);
     $fields = urldecode($fields);
     
     global $hq_id;
     
     $sqlStmt = "";
     $htmlOut = "";

     // "JB"
     if ($objType == "jb") :
        if (is_numeric($searchValue)) :
           $sqlStmt .= "
               SELECT jb.jb_id, jb.cr_sid, tr.tr_id, tr.tr_commission_no
                 FROM 
                   job AS jb,
                   tour AS tr
                 WHERE 
                   jb.jb_id = '" . $searchValue . "' AND
                   jb.hq_id = '" . $hq_id . "' AND
                   jb.jb_id = tr.jb_id AND
                   tr.tr_sort = '1'";
        endif;

        $categoryTitle = getLngt("AUFTRÄGE");
        $titleArray = array(getLngt("Auftrag") . "&nbsp;", getLngt("Kommissionsnummer") . "&nbsp;");
        $fieldArray = array("jb_id", "tr_commission_no");
        $aligns = "r,l";
        $alignArray = spliti(",",$aligns);
        $alignTitles = "left";
        $widths = "80,250";
        $widthArray = spliti(",",$widths);
        $summationField = "";
        $postParserField = "";
        $rowLinkType = "jb";
        $rowLinkField = "jb_id";
        $mode = "1";             // Output from DB-RESULT
        $sortDBField = "";       // Used in following include-file for sorting per column;
     endif;

     // "TR"
     if ($objType == "tr") :
        $sqlStmt .= "
            SELECT jb.jb_id AS obj_id, jb.cr_sid, tr.tr_id, tr.tr_commission_no AS cmp_comp, '' AS cmp_comp2, '' AS usr_name, '' AS usr_firstname
              FROM 
                tour AS tr,
                job AS jb
              WHERE 
                tr.tr_commission_no = '" . $searchValue . "' AND
                tr.tr_sort = '1' AND
                jb.jb_id = tr.jb_id AND 
                jb.hq_id = '3'";

        // $categoryTitle = getLngt("AUFTRÄGE");
        $titleArray = array(getLngt("Auftrag") . "&nbsp;", getLngt("Kommissionsnummer") . "&nbsp;");
        $fieldArray = array("jb_id", "tr_commission_no");
        $aligns = "r,l";
        $alignArray = spliti(",",$aligns);
        $alignTitles = "left";
        $widths = "80,100";
        $widthArray = spliti(",",$widths);
        $summationField = "";
        $postParserField = "";
        $mode = "1";
        $sortDBField = "";
     endif;

     // CS
     if ($objType == "cs") :
        $sqlStmt .= "
            SELECT cs.cs_id, cs.cs_eid, cmp.cmp_id, cmp.cmp_comp, CONCAT(usr.usr_name,', ',usr.usr_firstname) AS usr_data
              FROM 
                customer AS cs,
                company AS cmp,
                employee AS emp,
                user AS usr
              WHERE 
                cs.cmp_id = cmp.cmp_id AND
                cmp.cmp_archived = '0' AND
                cs.cs_admin = emp.emp_id AND
                emp.usr_id = usr.usr_id AND
                cs.hq_id = '" . $hq_id . "' AND
                (
                  cs.cs_eid LIKE '%" . $searchValue . "%' OR
                  cmp.cmp_comp LIKE '%" . $searchValue . "%' OR
                  cmp.cmp_match LIKE '%" . $searchValue . "%' OR
                  usr.usr_name LIKE '%" . $searchValue . "%' OR
                  usr.usr_firstname LIKE '%" . $searchValue . "%'
                )
              ORDER BY cmp.cmp_comp";

           $categoryTitle = getLngt("KUNDEN");
           $titleArray = array(getLngt("EID") . "&nbsp;", getLngt("Firma") . "&nbsp;", getLngt("Name, Vorname") . "&nbsp;");
           $fieldArray = array("cs_eid", "cmp_comp", "usr_data");
           $aligns = "l,l,l";
           $alignArray = spliti(",",$aligns);
           $alignTitles = "left";
           $widths = "80,350,250";
           $widthArray = spliti(",",$widths);
           $summationField = "";
           $postParserField = "";
           $postParserField2 = "";
           $rowLinkType = "cs";
           $rowLinkField = "cmp_id";
           $mode = "1";
           $sortDBField = "";
     endif;

     // CR
     if ($objType == "cr") :
        $sqlStmt .= "
            SELECT cr.cr_id, cr.cr_eid, cmp.cmp_id, cmp.cmp_comp, CONCAT(usr.usr_name,', ',usr.usr_firstname) AS usr_data
              FROM 
                courier AS cr,
                company AS cmp,
                user AS usr
              WHERE 
                cr.cmp_id = cmp.cmp_id AND
                cmp.cmp_archived = '0' AND
                cr.usr_id = usr.usr_id AND
                cr.hq_id = '" . $hq_id . "' AND
                (
                  cr.cr_eid LIKE '%" . $searchValue . "%' OR
                  cmp.cmp_comp LIKE '%" . $searchValue . "%' OR
                  cmp.cmp_match LIKE '%" . $searchValue . "%' OR
                  usr.usr_name LIKE '%" . $searchValue . "%' OR
                  usr.usr_firstname LIKE '%" . $searchValue . "%'
                )
              ORDER BY usr_data";

           $categoryTitle = getLngt("TRANSPORTEURE");
           $titleArray = array(getLngt("EID") . "&nbsp;", getLngt("Firma") . "&nbsp;", getLngt("Name, Vorname") . "&nbsp;");
           $fieldArray = array("cr_eid", "cmp_comp", "usr_data");
           $aligns = "l,l,l";
           $alignArray = spliti(",",$aligns);
           $alignTitles = "left";
           $widths = "80,350,250";
           $widthArray = spliti(",",$widths);
           $summationField = "";
           $postParserField = "";
           $rowLinkType = "cr";
           $rowLinkField = "cmp_id";
           $mode = "1";
           $sortDBField = "";
     endif;

     // CRVH
     if ($objType == "crvh") :
        $sqlStmt .= "
            SELECT crvh.crvh_id, crvh.crvh_sid, cr.cr_id, cr.cr_eid, CONCAT(usr.usr_name,', ',usr.usr_firstname) AS usr_data
              FROM 
                couriervehicle AS crvh,
                courier AS cr,
                user AS usr
              WHERE
                crvh.cr_id = cr.cr_id AND
                cr.hq_id = '" . $hq_id . "' AND
                (
                  crvh.crvh_sid LIKE '%" . $searchValue . "%'
                ) AND
                cr.usr_id = usr.usr_id
              ORDER BY crvh_sid";

           $categoryTitle = getLngt("FAHRZEUGE");
           $titleArray = array(getLngt("SID") . "&nbsp;", getLngt("EID") . "&nbsp;", getLngt("Name, Vorname") . "&nbsp;");
           $fieldArray = array("crvh_sid", "cr_eid", "usr_data");
           $aligns = "l,l,l";
           $alignArray = spliti(",",$aligns);
           $alignTitles = "left";
           $widths = "80,350,250";
           $widthArray = spliti(",",$widths);
           $summationField = "";
           $postParserField = "";
           $rowLinkType = "crvh";
           $rowLinkField = "cr_id";
           $mode = "1";
           $sortDBField = "";          
     endif;

     // Get result
     if ($sqlStmt != "") :
        $result = $db->query($sqlStmt);
        if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());

        // Display result list
        include ("../include/inc_list_defineoutput.inc.php");

        // Post parsing if necessary
        if ($postParserField != "") :
           // $tableBody = substituteTagContent($tableBody, "<postparser>", "</postparser>", $substitutionString);
        endif;

        if ($rowCounter > 0) :
           $categoryTitle = "<span style=\"width:100%; height:15px; color:green;\">" . $categoryTitle . "</span>";
           $htmlOut = $categoryTitle . "</br>" . "<table>" . $tableHeader . $tableBody . "</table>";
        endif;
     endif;

     $htmlOut = preg_replace("/\w*?$searchValue\w*/i", "<span style=\"background-color:yellow\">$0</span>", $htmlOut);

     $retValue = my_char_conversion($htmlOut);
     $retValue = my_str_check_js($retValue);
  endif;

  echo "searchResult = '" . $retValue . "';\n";  
?>