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


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


// Check HTTP-Parameters
getSecHttpVars("1",array("f_act", "customerId", "cscIdRoot", "cscIdActual",
                  "f_srvpaa_mode", "f_del_assoc", "f_jbb_sort", 
                  "g_cs_id", "g_csc_id", "g_csc_name", "initCsSearch", "f_filter", "statusMessage"));

$numOfEditRows = 20;
$numOfInputFieldsPerRows = 10;
$numOfRows = 0;
// Init input fields
$timeToEditHourDefault = "10";
$timeToEditMinuteDefault = "00";

for ($i = 1; $i <= $numOfEditRows; $i++) :
    for ($j = 1; $j <= $numOfInputFieldsPerRows; $j++) :
        getSecHttpVars("1",array("f_field_" . $i . "_" . $j));
    endfor;
endfor;

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

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

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

// Get company data
$g_cmp_id = getFieldValueFromId("customer", "cs_id", $g_cs_id, "cmp_id");
$g_cs_eid = getFieldValueFromId("customer", "cs_id", $g_cs_id, "cs_eid");
$g_cmp_comp = getFieldValueFromId("company", "cmp_id", $g_cmp_id, "cmp_comp");
$g_cmp_comp2 = getFieldValueFromId("company", "cmp_id", $g_cmp_id, "cmp_comp2");

// Filter
$currentYear = getDateTime("year");
$lfdToEditShow = "checked";
$yearToEditShow = "";
$timeToEditShow = "";
if ($f_filter != "") :
   if (!(array_search("lfdToEditShow",$f_filter) === FALSE)) : $lfdToEditShow = "checked"; endif;
   if (!(array_search("yearToEditShow",$f_filter) === FALSE)) : $yearToEditShow = "checked"; endif;
   if (!(array_search("timeToEditShow",$f_filter) === FALSE)) : $timeToEditShow = "checked"; endif;
endif;


// Get $f_jbb_id from $f_jbb_sort and $g_cs_id
function getJbbData($sqlField = "jbb_id") {
   global $db, $PHP_SELF, $f_jbb_sort, $g_cs_id;
   $retVal = "";
   if ($f_jbb_sort != "" && $g_cs_id != "") :
      $retVal = $db->getOne("SELECT " . $sqlField . " FROM jobbatch WHERE cs_id = '" . $g_cs_id . "' AND jbb_sort = '" . $f_jbb_sort . "'");
   endif;
   return $retVal;
}
$f_jbb_id = getJbbData("jbb_id");

// echo "g_cs_id: " . $g_cs_id . "<br>g_cs_eid: " . $g_cs_eid . "<br>f_jbb_id: " . $f_jbb_id . "<br>f_jbb_sort: " . $f_jbb_sort . "<br>";


// Store new jobs
if ($f_act == "storeJobs") :
   if ($g_cs_id != "") :

      // Get data for storing the job
      $currentTime = getDateTime("0");
      $usrId = getFieldValueFromId("employee", "emp_id", $emp_id, "usr_id");
      
      // Check for absolute customer discount
      $csDiscountAbsolute = getFieldValueFromId("customer", "cs_id", $g_cs_id, "cs_discount");

      $rowComplete = array();
      $vhtId = array(); $crId = array(); $crSid = array(); $cscIdPayer = array(); $jbFinishtime = array(); $price = array(); 
      $cmpId = array(); $cmpPostage = array(); $csInvmode = array(); $jbFreetext1 = array(); $txId = array(); $txValue = array(); 
      $txSign = array(); $cmpAdId = array(); $cmpHsno = array(); $cmpComp = array(); $cmpComp2 = array();
      
      // [1.] Iterate all fields and check the row to be complete   
      for ($i = 1; $i <= $numOfEditRows; $i++) :
          $rowComplete[$i] = FALSE;
          
          $vhtId[$i] = getFieldValueFromId("couriervehicle", "crvh_sid", ${("f_field_" . $i . "_10")}, "vht_id");
          $crId[$i] = getFieldValueFromId("couriervehicle", "crvh_sid", ${("f_field_" . $i . "_10")}, "cr_id");
          $crSid[$i] = ${("f_field_" . $i . "_10")};

          // If paying costcenter does not exist then get the root costcenter
          if ($g_csc_id == "") :
             $cscIdPayer[$i] = getFieldValueFromId("customer", "cs_id", $g_cs_id, "csc_id");
          else :
             $cscIdPayer[$i] = $g_csc_id;
          endif;

          if (${("f_field_" . $i . "_4")} == "") : ${("f_field_" . $i . "_4")} = $currentYear; endif;
          if (${("f_field_" . $i . "_5")} == "") : ${("f_field_" . $i . "_5")} = $timeToEditHourDefault; endif;
          if (${("f_field_" . $i . "_6")} == "") : ${("f_field_" . $i . "_6")} = $timeToEditMinuteDefault; endif;          
          $jbFinishtime[$i] = ${("f_field_" . $i . "_4")} . "-" . pad(${("f_field_" . $i . "_3")},2) . "-" . pad(${("f_field_" . $i . "_2")},2) . " " .
                              pad(${("f_field_" . $i . "_5")},2) . ":" . pad(${("f_field_" . $i . "_6")},2) . ":00";

          $lfd[$i] = ${("f_field_" . $i . "_1")};
          if (!is_numeric($lfd[$i])) : $lfd[$i] = ""; endif;
          
          $price[$i] = str_replace (",", ".", ${("f_field_" . $i . "_9")});
          $cmpId[$i] = getFieldValueFromId("customer", "cs_id", $g_cs_id, "cmp_id");
          $cmpPostage[$i] = getFieldValueFromId("company", "cmp_id", $cmpId[$i], "cmp_postage");
          $csInvmode[$i] = getFieldValueFromId("customer", "cs_id", $g_cs_id, "cs_invmode");
          $trCommissionNo[$i] = my_str_check_js(${("f_field_" . $i . "_8")});
          $jbInvoiceText[$i] = my_str_check_js(${("f_field_" . $i . "_7")});
          $jbFreetext1[$i] = $jbInvoiceText[$i];    // . " " . $trCommissionNo[$i];
          $txId[$i] = getFieldValueFromId("company", "cmp_id", $cmpId[$i], "tx_id");
          $txValue[$i] = getFieldValueFromId("tax", "tx_id", $txId[$i], "tx_value");
          $txSign[$i] = getFieldValueFromId("tax", "tx_id", $txId[$i], "tx_sign");
          $cmpAdId[$i] = getFieldValueFromId("company", "cmp_id", $cmpId[$i], "ad_id");
          $cmpHsno[$i] = getFieldValueFromId("company", "cmp_id", $cmpId[$i], "cmp_hsno");
          $cmpComp[$i] = getFieldValueFromId("company", "cmp_id", $cmpId[$i], "cmp_comp");
          $cmpComp2[$i] = getFieldValueFromId("company", "cmp_id", $cmpId[$i], "cmp_comp2");

          if ($vhtId[$i] != "" && $crId[$i] != "" && $crSid[$i] != "" && $cscIdPayer[$i] != "" && $jbFinishtime[$i] != "" && 
              $price[$i] != "" && is_numeric($price[$i]) && $cmpId[$i] != "" && $jbFreetext1[$i] != "" && $cmpAdId[$i] != "" && 
              $cmpComp[$i] != "") :

             $rowComplete[$i] = TRUE;
          endif;
      endfor;

      // Transaction control starts here
      TA("B");

      // [2.] Generate a new list ($f_jbb_id == "") with current timestamp or use an old one
      //      and check if at least one job has to be stored
      if ($f_jbb_sort == "") :
         $atLeastOneJobOk = FALSE;
         $atLeastOneJobNotOk = FALSE;
         for ($i = 1; $i <= $numOfEditRows; $i++) :
             if ($rowComplete[$i]) :
                $atLeastOneJobOk = TRUE;
             else :
                $atLeastOneJobNotOk = TRUE;
             endif;
         endfor;
         if ($atLeastOneJobOk) :

            // Get maximum list number of the special customer from jbb_sort
            $f_jbb_sort = getMaxOfField("jobbatch", "jbb_sort", "cs_id = '" . $g_cs_id . "'");
            if (is_numeric($f_jbb_sort)) :
               ++$f_jbb_sort;   // Increment because of the next free number
             else : 
               $f_jbb_sort = "1";
            endif;

            insertStmt("jobbatch", array("cs_id", $g_cs_id, "jbb_sort", $f_jbb_sort, "jbb_createtime", $currentTime));
            $f_jbb_id = getLastInsertId();    // IMPORTANT to use parameter $f_jbb_id because of loop and following search below... !!!
         endif;
      endif;

      // [3.] Iterate all jobs and insert
      for ($i = 1; $i <= $numOfEditRows; $i++) :

          if ($rowComplete[$i]) :                   

             // Check for absolute customer discount
             /*
             if ($csDiscountAbsolute > 0.00) :
                $price[$i] = ($price[$i] * ((100 - $csDiscountAbsolute) / 100));
             endif;
             */
   
             insertStmt("job", array("hq_id", $hq_id, "vht_id", $vhtId[$i], "csc_id_payer", $cscIdPayer[$i], "jb_payment", "0",
                                     "jb_ordertime", $jbFinishtime[$i], "cr_id", $crId[$i], "cr_sid", $crSid[$i], "cr_id_order", $crId[$i],
                                     "jb_taketime", $jbFinishtime[$i], "jb_status", "2", "jb_autoranking", "0",
                                     "jb_finishtime", $jbFinishtime[$i], "emp_id", $usrId, "jb_fixprice", $price[$i], "jb_totalprice", $price[$i],
                                     "jb_postage", $cmpPostage[$i], "jb_invmode", $csInvmode[$i], "jb_freetext_1", $jbFreetext1[$i],
                                     "jb_sales_tax_rate", $txValue[$i], "jb_sales_tax_rate_sign", $txSign[$i],
                                     "jb_booktime", $currentTime));
             $jbIdNew = getLastInsertId();
             
             insertStmt("tour", array("jb_id", $jbIdNew, "ad_id", $cmpAdId[$i], "tr_sort", "1", "tr_comp", $cmpComp[$i], "tr_comp2", $cmpComp2[$i],
                                      "tr_hsno", $cmpHsno[$i], "csc_id", $cscIdPayer[$i], "tr_status", "1", "tr_signname", "Listenbuchung",
                                      "tr_finishtime", $jbFinishtime[$i], "tr_commission_no", $trCommissionNo[$i]));
   
             insertStmt("tourservice", array("jb_id", $jbIdNew, "csc_id", $cscIdPayer[$i], "tr_sort", "0", 
                                             "srv_id", "0", "trs_srv_name", "Fixpreis", "srvt_id", "0", "trs_srvt_name", "", 
                                             "trs_price", $price[$i], "trs_discount", $csDiscountAbsolute));
             
             // Write logdata into log database
             writeToLogDB("1",$hq_id,$jbIdNew,$usrId,$crId[$i],$crSid[$i],"","");


             
             // Take lfd of the input field and get maximum of the lfd of the special list if nessassary
             $jbblLfdMaxval = $lfd[$i];
             if ($jbblLfdMaxval == "") :
                $jbblLfdMaxval = getMaxOfField("jobbatchlist", "jbbl_lfd", "jbb_id = '" . $f_jbb_id . "'");
                if (is_numeric($jbblLfdMaxval)) :
                   ++$jbblLfdMaxval;   // Increment because of the next free number
                else : 
                   $jbblLfdMaxval = "1";
                endif;
             endif;

             insertStmt("jobbatchlist", array("jbb_id", $f_jbb_id, "jbbl_lfd", $jbblLfdMaxval, "jb_id", $jbIdNew));

             // Generate invoice text
             mk_jb_invtext($jbIdNew, false);
             
             // Reset input fields of the row stored correctly
             for ($j = 1; $j <= $numOfInputFieldsPerRows; $j++) :
                 ${("f_field_" . $i . "_" . $j)} = '';
             endfor;
          endif;
      endfor;

      TA("C");
      TA("E");

      $f_act = "loadSpecialListOfCustomer";
   else :
      $statusMessage = "Sie haben noch keinen Kunden ausgewählt!";
   endif;   
endif;


// Show all lists of the customer
$outStoredBatchListsOfCustomer = "";
if ($f_act == "getListsOfCustomer") :
   if ($g_cs_id != "") :
      
      // Only for output get length of the max jbb_sort value for pad()
      $jbbSortPadlLength = strlen(getMaxOfField("jobbatch", "jbb_sort", "cs_id = '" . $g_cs_id . "'"));
      if ($jbbSortPadlLength == "0") : $jbbSortPadlLength = "1"; endif;
   
      // Get lists of the customer
      $sqlquery = "SELECT jbb.jbb_id, jbb.cs_id, jbb.jbb_sort, jbb.jbb_createtime " .
                  " FROM jobbatch AS jbb" . 
                  " WHERE jbb.cs_id = '" . $g_cs_id . "' " .
                  " ORDER BY jbb.jbb_createtime DESC";
      
      $result = $db->query($sqlquery);
      if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());

      while ($row = $result->fetch_assoc()):
            $outStoredBatchListsOfCustomer .= "<a href=\"javascript:finishPage('loadSpecialListOfCustomer','" . $row["jbb_sort"] . "');\">" . 
                                              pad($row["jbb_sort"],$jbbSortPadlLength) . " - " . formatOutput($row["jbb_createtime"],"datetime","3") . "</a><br>";
      endwhile;
      $result->free();

      if ($outStoredBatchListsOfCustomer != "") : 
         $outStoredBatchListsOfCustomer = "<br><br>Listenbestand: <br><br>" . $outStoredBatchListsOfCustomer . "<br><br>";
         
         if ($f_jbb_id != "") :
            $f_act = "loadSpecialListOfCustomer";
         endif;
      else :
         $statusMessage = "Für den Kunden existiert noch keine Liste!";
      endif;
   else :
      $statusMessage = "Sie haben noch keinen Kunden ausgewählt!";
   endif;
endif;


// Show all jobs of a selected list of the customer
$outCurrentListOfCustomer = "";
if ($f_act == "loadSpecialListOfCustomer") :
   if ($g_cs_id != "") :   
      if ($f_jbb_id != "") :

         $sqlquery = "SELECT jbbl.jbbl_lfd, jb.jb_id, jb.jb_export_time, jb.jb_finishtime, jb.jb_freetext_1, jb.cr_sid, tr.tr_commission_no, csc.csc_name,"
                      . " " . $priceFormular . " AS price"
                      . " FROM jobbatch AS jbb, jobbatchlist AS jbbl, tour AS tr, job AS jb LEFT JOIN tourservice AS trs ON trs.jb_id = jb.jb_id, costcenter AS csc"
                      . " WHERE jbb.jbb_id = '" . $f_jbb_id . "' AND"
                      . "       jbb.jbb_id = jbbl.jbb_id         AND"
                      . "       jbbl.jb_id = jb.jb_id            AND"
                      . "       jb.jb_id = tr.jb_id              AND"
                      . "       tr.tr_sort = '1'                 AND"
                      . "       jb.csc_id_payer = csc.csc_id        "
                      . " GROUP BY trs.jb_id"
                      . " ORDER BY jbbl.jbbl_lfd";

         $result = $db->query($sqlquery);
         if (DB::isError($result)) die ("$PHP_SELF: " . $result->getMessage());
   
         $out = "";
         while ($row = $result->fetch_assoc()):
               $numOfRows++;

               $out .= "<tr class=\"f10np1\">";
               $out .= "<td align=\"center\"> ". $row["jbbl_lfd"] . "</td> ";
               
               $tmpDateTime = formatOutput($row["jb_finishtime"],"datetime","3");
               $out .= "<td align=\"center\"> ". substr($tmpDateTime,0,8) . "</td>";
               $out .= "<td align=\"center\"> ". substr($tmpDateTime,11,5) . "</td>";
               $out .= "<td> ". $row["jb_freetext_1"] . "<br>[Kst.: " . $row["csc_name"] . "]</td> ";
               $out .= "<td> ". $row["tr_commission_no"] . "</td> ";
               $out .= "<td align=\"right\"> ". formatOutput($row["price"],"float_rounded_2") . "&nbsp;</td> ";
               $out .= "<td align=\"center\"> ". $row["cr_sid"] . "</td> ";
               $out .= "<td align=\"center\">";
               if ($row["jb_export_time"] == "" || $row["jb_export_time"] == "0000-00-00 00:00:00") :
                   $out .= "<a href=\"../jobs/job_edit.php?jb_id=" . ec($row[jb_id]) . "\" target=\"_blank\">" . $row[jb_id] . "</a>";
               else :
                   $out .= "Exportiert";
               endif;
               $out .= "</td>";
               $out .= "</tr>\n";
         endwhile;
         $result->free();
         
         $outCurrentListOfCustomer = $f_jbb_sort . " - " . formatOutput(getJbbData("jbb_createtime"),3);
      else :
         $statusMessage = "Es wurde entweder keine Liste gewählt ODER Eingaben sind für die Anlage eine neue Liste nicht korrekt!";
      endif;      
   else :
      $statusMessage = "Sie haben noch keinen Kunden ausgewählt!";
   endif;
endif;


// **********************
// * Define edit fields *
// **********************

// Get maximum of the lfd of the special list
$jbblLfdMaxval = 0;
if ($g_cs_id != "" && $f_jbb_id != "") :
   $jbblLfdMaxval = getMaxOfField("jobbatchlist", "jbbl_lfd", "jbb_id = '" . $f_jbb_id . "'");
endif;

$outEditFields = "";
for ($i = 1; $i <= $numOfEditRows; $i++) :

    $outEditFields .= "<tr>";
    
    ${("f_field_" . $i . "_1")} = $jbblLfdMaxval + $i;
    $outEditFields .= "<td valign=\"top\"><input type=\"text\" name=\"f_field_" . $i . "_1\" value=\"" . ${("f_field_" . $i . "_1")} . "\" size=\"3\" onBlur=\"checkDate(this,'lfd','" . $i . "','1')\" " . ($lfdToEditShow == "" ? "disabled" : "") . "></td>";
    
    $outEditFields .= "<td valign=\"top\">";
    $outEditFields .= "<input type=\"text\" name=\"f_field_" . $i . "_2\" value=\"" . ${("f_field_" . $i . "_2")} . "\" size=\"2\" maxlength=\"2\" onBlur=\"checkDate(this,'day','" . $i . "','2')\">";
    $outEditFields .= "<input type=\"text\" name=\"f_field_" . $i . "_3\" value=\"" . ${("f_field_" . $i . "_3")} . "\" size=\"2\" maxlength=\"2\" onBlur=\"checkDate(this,'month','" . $i . "','3')\">";

    // Preset of the year
    if (${("f_field_" . $i . "_4")} == "") : ${("f_field_" . $i . "_4")} = $currentYear; endif;
    $outEditFields .= "<input type=\"text\" name=\"f_field_" . $i . "_4\" value=\"" . ${("f_field_" . $i . "_4")} . "\" size=\"4\" maxlength=\"4\" onBlur=\"checkDate(this,'year','" . $i . "','4')\" " . ($yearToEditShow == "" ? "disabled" : "") . ">";
    
    $outEditFields .= "</td>";
    $outEditFields .= "<td valign=\"top\">";
    
    // Preset of the hour
    if (${("f_field_" . $i . "_5")} == "") : ${("f_field_" . $i . "_5")} = $timeToEditHourDefault; endif;
    $outEditFields .= "<input type=\"text\" name=\"f_field_" . $i . "_5\" value=\"" . ${("f_field_" . $i . "_5")} . "\" size=\"2\" maxlength=\"2\" onBlur=\"checkDate(this,'hour','" . $i . "','5')\" " . ($timeToEditShow == "" ? "disabled" : "") . ">";
    
    // Preset of the hour
    if (${("f_field_" . $i . "_6")} == "") : ${("f_field_" . $i . "_6")} = $timeToEditMinuteDefault; endif;
    $outEditFields .= "<input type=\"text\" name=\"f_field_" . $i . "_6\" value=\"" . ${("f_field_" . $i . "_6")} . "\" size=\"2\" maxlength=\"2\" onBlur=\"checkDate(this,'minute','" . $i . "','6')\" " . ($timeToEditShow == "" ? "disabled" : "") . ">";
    
    $outEditFields .= "</td>";
    $outEditFields .= "<td valign=\"top\"><textarea name=\"f_field_" . $i . "_7\" cols=\"45\" rows=\"2\">" . ${("f_field_" . $i . "_7")} . "</textarea><br><br></td>";
    $outEditFields .= "<td valign=\"top\"><input type=\"text\" name=\"f_field_" . $i . "_8\" value=\"" . ${("f_field_" . $i . "_8")} . "\" size=\"10\"></td>";
    $outEditFields .= "<td valign=\"top\"><input type=\"text\" name=\"f_field_" . $i . "_9\" value=\"" . ${("f_field_" . $i . "_9")} . "\" size=\"7\"></td>";
    $outEditFields .= "<td valign=\"top\"><input type=\"text\" name=\"f_field_" . $i . "_10\" value=\"" . ${("f_field_" . $i . "_10")} . "\" size=\"10\"></td>";
    $outEditFields .= "<td valign=\"top\"><a href=\"javascript:clearJobFields('" . $i . "');\">Zurücksetzen</a></td>";
    $outEditFields .= "</tr>";
endfor;
?>

<html>

<head>
<title>votian: Auftragsstapelverarbeitung</title>
<link rel="stylesheet" type="text/css" href="../css/phoenix.css"> 

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

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

function displayStatusMessage() {
 if (statusMessage != "") {
    alert(statusMessage);
 }
};

function clearJobFields(line) {
  for (var i = 1; i <= <?php echo $numOfInputFieldsPerRows ?>; i++) {
      eval('document.forms[0].f_field_' + line + '_' + i).value = '';
  };
};

function clearFields() {
  for (var j = 1; j <= <?php echo $numOfEditRows ?>; j++) {
      clearJobFields(j);
      eval('document.forms[0].f_field_' + j + '_1').value = j;
      eval('document.forms[0].f_field_' + j + '_4').value = <?php echo $currentYear ?>;
      eval('document.forms[0].f_field_' + j + '_5').value = <?php echo $timeToEditHourDefault ?>;
      eval('document.forms[0].f_field_' + j + '_6').value = <?php echo $timeToEditMinuteDefault ?>;
  }
  document.forms[0].f_act.value = '';
  document.forms[0].f_jbb_sort.value = '';
}

function searchCs() {
  searchCsCscGeneric('111000000000111');
}

function finishPage(mode,jbb_sort) {
  if (mode == 'storeJobs') {
     if (document.forms[0].g_cs_id.value != '') {
        if (confirm('Möchten Sie die eingetragenen Neuaufträge speichern?')) {
     	   document.forms[0].f_act.value = mode;
           document.forms[0].submit();
        }
     } else {
       alert('Sie müssen zuvor bitte einen Kunden wählen!');
     }
  }
  if (mode == 'getListsOfCustomer') {
     // if (confirm('Möchten Sie die Listen einsehen?')) {
     	document.forms[0].f_act.value = mode;
        document.forms[0].submit();
     // }
  }
  if (mode == 'loadSpecialListOfCustomer') {
     if (document.forms[0].g_cs_id.value != '') {
        if (confirm('Möchten Sie die Liste laden?')) {
     	   document.forms[0].f_act.value = mode;
     	   document.forms[0].f_jbb_sort.value = jbb_sort;
           document.forms[0].submit();
        }
     } else {
       alert('Sie müssen zuvor bitte einen Kunden wählen!');
     }
  }    
  if (mode == 'clearPage') {
     if (confirm('Möchten Sie die komplette Seite zurücksetzen?')) {     	
        document.location.href = "jb_edit_batch.php";
     }
  }
  if (mode == 'clearPageAndCustomerSearch') {
     if (document.forms[0].g_cs_id.value != '' && document.forms[0].f_jbb_sort.value != '') {
        if (confirm('Möchten Sie einen neuen Kunden wählen? Die Seiteninformationen gehen verloren!')) {
           document.location.href = "jb_edit_batch.php?initCsSearch=1";
        }
     } else {
     	clearFields();
     	searchCs();
     };
  }
  if (mode == 'newCustomerList') {
     if (document.forms[0].g_cs_id.value != '' && document.forms[0].f_jbb_sort.value != '') {
        if (confirm('Möchten Sie eine neue Liste beginnen?')) {
           clearFields();
           document.forms[0].submit();
        }
     } else {     	
     	if (document.forms[0].g_cs_id.value == '') {
           alert('Es ist kein Kunde ausgewählt!');
        } else {
           clearFields();
        }
     }   
  }  
};

function checkDate(checkObj,mode,i,j) {
  var resetValue = checkDateFields(checkObj,mode);
  if (resetValue) {
     eval('document.forms[0].f_field_' + i + '_' + j).value = '';
     eval('document.forms[0].f_field_' + i + '_' + j).focus();
  }
}

function setFieldEditStatus(checkObj,mode) {

  var doContinue = true;
  if (checkObj.checked == false) {  
     doContinue = false;
     if (confirm('Achtung: Beim Deaktivieren werden alle Einträge der Spalte zurückgesetzt! Fortfahren?')) {
     	doContinue = true;
     } else {
     	checkObj.checked = true;
     }
  }

  if (doContinue) {
     for (var i = 1; i <= <?php echo $numOfEditRows ?>; i++) {
         if (mode == 'lfd') {
            if (checkObj.checked == true) {
               eval('document.forms[0].f_field_' + i + '_1').disabled = false;
            } else {            
               eval('document.forms[0].f_field_' + i + '_1').disabled = true;
            }
         }
         if (mode == 'year') {
            if (checkObj.checked == true) {
               eval('document.forms[0].f_field_' + i + '_4').disabled = false;
            } else {            
               eval('document.forms[0].f_field_' + i + '_4').disabled = true;
               eval('document.forms[0].f_field_' + i + '_4').value = <?php echo $currentYear ?>;
            }
         }
         if (mode == 'time') {
            if (checkObj.checked == true) {
               eval('document.forms[0].f_field_' + i + '_5').disabled = false;
               eval('document.forms[0].f_field_' + i + '_6').disabled = false;
            } else {
               eval('document.forms[0].f_field_' + i + '_5').disabled = true;
               eval('document.forms[0].f_field_' + i + '_6').disabled = true;
               eval('document.forms[0].f_field_' + i + '_5').value = <?php echo $timeToEditHourDefault ?>;
               eval('document.forms[0].f_field_' + i + '_6').value = <?php echo $timeToEditMinuteDefault ?>;
            }
         }
     }
  }
}

function showPageStatus() {
  alert('g_cs_id: ' + document.forms[0].g_cs_id.value);
  alert('g_csc_id: ' + document.forms[0].g_csc_id.value);
  alert('g_csc_name: ' + document.forms[0].g_csc_name.value);
  alert('f_jbb_sort: ' + document.forms[0].f_jbb_sort.value);
}
-->
</script>
</head>

<body onLoad="displayStatusMessage(); <?php if ($initCsSearch == "1") : echo "searchCs();" ; endif; ?>">
  <form action="jb_edit_batch.php" method="post">
  <input type="hidden" name="f_act" value="">  
  <input type="hidden" name="customerId" value="<?php echo $customerId ?>">
  <input type="hidden" name="cscIdRoot" value="<?php echo $cscIdRoot ?>">
  <input type="hidden" name="cscIdActual" value="<?php echo $cscIdActual ?>">  
  <input type="hidden" name="g_cs_id" value="<?php echo $g_cs_id ?>">
  <input type="hidden" name="f_jbb_sort" value="<?php echo $f_jbb_sort ?>">
  <input type="hidden" name="g_csc_id" value="<?php echo $g_csc_id ?>">  

  <table border="0">
    <tr>      
      <td>
        <table border="0">
          <tr>
            <td class="f12bp1_red">              
              Liste der Firma:&nbsp;&nbsp;
              <input type="text" name="g_cmp_comp" value="<?php echo $g_cmp_comp ?>" size="30" readonly >
              <input type="text" name="g_cmp_comp2" value="<?php echo $g_cmp_comp2 ?>" size="30" readonly >
              <input type="text" name="g_cs_eid" value="<?php echo $g_cs_eid ?>" size="10" readonly >
              <input type="text" name="g_csc_name" value="<?php echo $g_csc_name ?>" size="25" readonly >
              &nbsp;&nbsp;
              <input type="button" <?php echo $htmlClass01 ?> value="Kunde suchen" onClick="finishPage('clearPageAndCustomerSearch');">
<!-- <input type="button" value="!!!" onClick="showPageStatus();"> -->
              <br><br>
            </td>
          </tr>
          <tr>
            <td>
              <!-- Search, reset and store -->
              <br><br>
              <a href="javascript:finishPage('clearPage');">Seite komplett zurücksetzen</a>
              &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
              <a href="javascript:finishPage('getListsOfCustomer');">Listen des Kunden</a>
              &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
              <a href="javascript:finishPage('newCustomerList');">Neue Liste des Kunden beginnen</a>
              &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
              &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
              <input type="button" name="action" value="Neuaufträge speichern" onClick="javascript:finishPage('storeJobs');">
              
              <!-- 
              <a href="javascript:document.forms[0].f_act.value='search';document.forms[0].submit();">Suchen</a>
              &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
              <a href="javascript:clearJobFields('0');">Suchfelder zurücksetzen</a>
              &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
              -->              

              <br><br>

              <?php echo $outStoredBatchListsOfCustomer ?>

              <br><br>

              <span class="f10bp1_red">
                Aktuelle Liste: &nbsp;&nbsp;&nbsp;<?php echo $outCurrentListOfCustomer ?>
              </span>
              &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
              &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
              <!--
              <input type="checkbox" name="f_filter[]" value="lfdToEditShow" onClick="javascript:setFieldEditStatus(this,'lfd');" <?php echo $lfdToEditShow ?>> Spalte Lfd aktiviert
              &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
              -->
              <input type="checkbox" name="f_filter[]" value="yearToEditShow" onClick="javascript:setFieldEditStatus(this,'year');" <?php echo $yearToEditShow ?>> Spalte Jahreszahl aktiviert
              &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
              <input type="checkbox" name="f_filter[]" value="timeToEditShow" onClick="javascript:setFieldEditStatus(this,'time');" <?php echo $timeToEditShow ?>> Spalten Uhrzeit aktiviert
              <br><br>


              <!-- Input fields for new jobs-->
              <table class="f8np1" border="1" cellpadding="0">
                <tr>
                  <td>Lfd</td> <td>Datum</td> <td>Uhrzeit</td> <td>Auftrag / Tourenverlauf</td> <td>Kst./Referenz</td> <td>Betrag</td> <td>Fahrzeug</td>
                  <!--
                  <td><a href="javascript:document.forms[0].orderClause.value='ad_street, srvpaa.srvpaa_hsno_from';document.forms[0].f_act.value='search';document.forms[0].submit();">Straße</a></td>
                  <td>&nbsp;</td>
                  -->
                </tr>              
              
                <?php echo $outEditFields ?>

                <!-- Stored jobs -->
                <?php echo $out ?>
              </table>
              
              <!--
              <br><br>
              Anzahl Einträge: <?php echo $numOfRows ?><?php if ($numOfRows == "0" && $f_act == "search" && $statusMessage == "") : echo " (Keine Einträge gefunden.)"; endif; ?>              
              -->
            </td>
          </tr>        
        </table>
      </td>      
  </table>
  
</body>

</html>