<?php
/*=======================================================================
 *
 * getTournames.php
 *
 * Autor: Carsten Annacker
 *
 =======================================================================*/

//require_once("../PEAR/HTML/Template/IT.php");

include_once("../include/caglobal.inc.php");
trace_execution_time_start();
include_once("../include/global.inc.php");

list ($csc_id) = getHttpVars(array("csc_id"));

getDb2Connection();     // Try to connect request server because of performance

// gespeicherte Touren darf nicht mehr disabled sein, da durch die Auswahl der Referesh ausgelöst wird!
//$savedTour_disabled = ($savedTour_options == "") ? "disabled" : "";
$savedTour_disabled = "";

// gespeicherte Tournamen der übergebenen Kostenstelle holen
// wenn Kostenstelle Barzahler, dann nur Touren, in denen kein Zahler (in csc_id_payer_cash) eingetragen ist!
if (defined('CSC_ID_PAYER_CASH') && $csc_id == CSC_ID_PAYER_CASH):
  $sqlStmt =
    "SELECT jb_id, jb_tourname FROM job WHERE (csc_id_payer = '$csc_id' AND csc_id_payer_cash = '0') AND LENGTH(jb_tourname) > 0 ORDER BY jb_tourname";
else:
//    "SELECT jb_id, jb_tourname FROM job WHERE (csc_id_payer = '$csc_id' OR (csc_id_payer_cash = '$csc_id' AND csc_id_payer_cash != '0')) AND LENGTH(jb_tourname) > 0 ORDER BY jb_tourname";
  $sqlStmt = "(SELECT jb_id, jb_tourname FROM job WHERE csc_id_payer = '$csc_id' AND LENGTH(jb_tourname) > 0)" .
  " UNION (SELECT jb_id, jb_tourname FROM job WHERE (csc_id_payer_cash = '$csc_id' AND csc_id_payer_cash != '0') AND LENGTH(jb_tourname) > 0) ORDER BY jb_tourname";
endif;
$result = $db2->query($sqlStmt);
if (DB::isError($result))
  reportDie ("$PHP_SELF: <br>$sqlStmt<br>" . $result->getMessage());
//$javascript = "";
$javascript =
  "opener.savedTour_options = '';\n";
$i = 0;
while ($row = $result->fetch_assoc()):
  // Javascript-Code, der die aktuelle neue Option einträgt
//  $javascript .=
//    "var newOption".$row['jb_id']." = new Option('" . htmlentities($row['jb_tourname']) . "', '" . htmlentities($row['jb_id']) . "', false, false);\n";
//  $javascript .=
//    "opener.savedTour_options[" . $i++ . "] = newOption".$row['jb_id'].";\n";
    // wg. einem f***ing Bug im IE funzt der folgende Code net (in Mozillla geht's), deshalb der Umweg oben
//    "opener.parent.job_options.document.tourOptions.savedTour.options[" .
//    "  opener.parent.job_options.document.tourOptions.savedTour.options.length] = newOption".$row['jb_id'].";\n";
  $javascript .= "if (opener.savedTour_options != '') opener.savedTour_options = opener.savedTour_options + '\t';\n";
  $javascript .= "opener.savedTour_options = opener.savedTour_options + '" .
    str_replace('\'', '\\\'', $row['jb_tourname']) . "\t" . $row['jb_id'] . "';\n";
  $i++;
endwhile;
$result->free();
$javascript .=
  "opener.parent.job_options.document.tourOptions.jb_tourname.value = \"\";\n";
$javascript .=
  "opener.savedTour_options_length = $i;\n";

//$javascript .=
//  "alert(".$csc_id.");\n";
// Job-Options Frame initialisieren
$javascript .=
//  "opener.setTimeout('checkJb_tourname_workaround()', 1000);\n" .
  "opener.checkJb_tourname_workaround();\n" .
  "opener.parent.job_options.isCurrentlySubmitting = false;\n" .
  "opener.setTimeout('force_rerender()', 100);\n" .
  "self.close();\n";
//echo $javascript;
// Inhalt des temporären Fensters erzeugen und ausgeben
$output = file_get_contents(GETDATAHTMLTPL);
$output = str_replace("{_javascript_}", $javascript, $output);
writeLog_("../log/job_options_", trace_execution_time_stop() . " '../jobs/get_tournames.php' executed: \$csc_id=$csc_id , \$i=$i");
echo $output;
?>
