/*=======================================================================
 *
 * lib_courier.js
 *
 * Autor: Carsten Annacker
 *
 =======================================================================*/

/**
 *  Funktionen für die Kurierliste der Auftragserfassung und -disposition
 *
 */

/*
 * getCr_id
 *   params: i
 *   return: cr_id
 *   global: cr_id_order_list (muss von PHP definiert werden)
 *
 * Aus der Liste cr_id_order_list wird die Kuriernummer von Eintrag Nummer i
 * zurückgegeben.
 * Wenn Liste mit PLZ und Zeit, dann muss Kuriernummer aus Liste extrahiert werden
 */

/*
function getPLZ(cr_id_order_list, i)
{
  return cr_id_order_list[i].substring(0, cr_id_order_list[i].indexOf(",&nbsp;"));
}
*/

function getPLZ(cr_id_order_list_i_)
{
  return cr_id_order_list_i_.substring(0, cr_id_order_list_i_.indexOf(",&nbsp;"));
}

function getCr_id(cr_id_order_list, i)
{
  var start = cr_id_order_list[i].indexOf("&nbsp;<i><b>") + "&nbsp;<i><b>".length;
  var cr_id = cr_id_order_list[i].substring(start, cr_id_order_list[i].indexOf("</b>, "));

  if (cr_id.indexOf("span") > -1)
  {
    var start = cr_id.indexOf("\">") + "\">".length;
    cr_id = cr_id.substring(start, cr_id.indexOf("</span>"));
  }
  return cr_id;

//  if (cr_id_order_list.length >= i - 1)
//  {
//    if (cr_id_order_list[i].indexOf("*, &nbsp;") > -1 && cr_id_order_list[i].indexOf(")") > -1)
//    {
//      return cr_id_order_list[i].substr(0, cr_id_order_list[i].indexOf("*, &nbsp;"));
//    } else {
//      if (cr_id_order_list[i].indexOf(", &nbsp;") > -1 && cr_id_order_list[i].indexOf(")") > -1)
//      {
//        return cr_id_order_list[i].substr(0, cr_id_order_list[i].indexOf(", &nbsp;"));
//      } else {
//        return cr_id_order_list[i];
//      }
//   }
//  } else {
//    return "";
//  }
}

function getCr_id2(cr_id_order_list_i_)
{
  var start = cr_id_order_list_i_.indexOf("&nbsp;<i><b>") + "&nbsp;<i><b>".length;
  var cr_id = cr_id_order_list_i_.substring(start, cr_id_order_list_i_.indexOf("</b>, "));
  if (jb_longhaul == "1")
    cr_id = cr_id_order_list_i_.substring(start, cr_id_order_list_i_.indexOf("</b> "));

  if (cr_id.indexOf("span") > -1)
  {
    var start = cr_id.indexOf("\">") + "\">".length;
    cr_id = cr_id.substring(start, cr_id.indexOf("</span>"));
  }
  return cr_id;
}

function getCr_vehicle(cr_id_order_list_i_)
{
  if (jb_longhaul != "1")
    return "";
  var start = cr_id_order_list_i_.indexOf("<td align=center valign=top>") + "<td align=center valign=top>".length;
  start = cr_id_order_list_i_.indexOf("\" value=\"", start) + "\" value=\"".length;

  return cr_id_order_list_i_.substring(start, cr_id_order_list_i_.indexOf("\"></td>", start));
}

function getCr_starting_min(cr_id_order_list_i_)
{
  if (jb_longhaul != "1")
    return "";
  var start = cr_id_order_list_i_.indexOf("<td align=center valign=top>") + "<td align=center valign=top>".length;
  start = cr_id_order_list_i_.indexOf("<td align=center valign=top>", start) + "<td align=center valign=top>".length;

  return cr_id_order_list_i_.substring(start, cr_id_order_list_i_.indexOf("</td>", start));
}

function getCr_starting_km(cr_id_order_list_i_)
{
  if (jb_longhaul != "1")
    return "";
  var start = cr_id_order_list_i_.indexOf("<td align=center valign=top>") + "<td align=center valign=top>".length;
  start = cr_id_order_list_i_.indexOf("<td align=center valign=top>", start) + "<td align=center valign=top>".length;
  start = cr_id_order_list_i_.indexOf("<td align=center valign=top>", start) + "<td align=center valign=top>".length;

  return cr_id_order_list_i_.substring(start, cr_id_order_list_i_.indexOf("</td>", start));
}

function getCr_current_location(cr_id_order_list_i_)
{
  if (jb_longhaul != "1")
    return "";
  var start = cr_id_order_list_i_.indexOf("<td align=center valign=top>") + "<td align=center valign=top>".length;
  start = cr_id_order_list_i_.indexOf("<td align=center valign=top>", start) + "<td align=center valign=top>".length;
  start = cr_id_order_list_i_.indexOf("<td align=center valign=top>", start) + "<td align=center valign=top>".length;
  start = cr_id_order_list_i_.indexOf("<td align=center valign=top>", start) + "<td align=center valign=top>".length;

  return cr_id_order_list_i_.substring(start, cr_id_order_list_i_.indexOf("</td>", start));
}

function getCur_cr_homezipcode(cr_id_order_list_i_)
{
  if (jb_longhaul != "1")
    return "";
  var start = cr_id_order_list_i_.indexOf("<td align=center valign=top>") + "<td align=center valign=top>".length;
  start = cr_id_order_list_i_.indexOf("<td align=center valign=top>", start) + "<td align=center valign=top>".length;
  start = cr_id_order_list_i_.indexOf("<td align=center valign=top>", start) + "<td align=center valign=top>".length;
  start = cr_id_order_list_i_.indexOf("<td align=center valign=top>", start) + "<td align=center valign=top>".length;
  start = cr_id_order_list_i_.indexOf("<td align=center valign=top>", start) + "<td align=center valign=top>".length;

  return cr_id_order_list_i_.substring(start, cr_id_order_list_i_.indexOf("</td>", start));
}

function getRound_km(cr_id_order_list_i_)
{
  if (jb_longhaul != "1")
    return "";
  var start = cr_id_order_list_i_.indexOf("<td align=center valign=top>") + "<td align=center valign=top>".length;
  start = cr_id_order_list_i_.indexOf("<td align=center valign=top>", start) + "<td align=center valign=top>".length;
  start = cr_id_order_list_i_.indexOf("<td align=center valign=top>", start) + "<td align=center valign=top>".length;
  start = cr_id_order_list_i_.indexOf("<td align=center valign=top>", start) + "<td align=center valign=top>".length;
  start = cr_id_order_list_i_.indexOf("<td align=center valign=top>", start) + "<td align=center valign=top>".length;
  start = cr_id_order_list_i_.indexOf("<td align=center valign=top>", start) + "<td align=center valign=top>".length;

  return cr_id_order_list_i_.substring(start, cr_id_order_list_i_.indexOf("</td>", start));
}

function getCr_price(cr_id_order_list_i_)
{
//console.log("getCr_price");
  if (jb_longhaul != "1")
    return "";
//  var start = cr_id_order_list_i_.lastIndexOf("<td align=center valign=top>") + "<td align=center valign=top>".length;
  var start = cr_id_order_list_i_.indexOf("TSZ)\n\">") + "TSZ)\n\">".length;
//console.log("start = ", start);
  var end_correct = 0;
  if (cr_id_order_list_i_.substring(start, start + "<span style=\"color:red\">".length) == "<span style=\"color:red\">") {
    start += "<span style=\"color:red\">".length;
    end_correct = "</span>".length;
  }
//  var cr_price = cr_id_order_list_i_.substring(start, cr_id_order_list_i_.indexOf("<input type=\"hidden\" name=\"jb_cr_price_one_way_") - end_correct);
//console.log("start = ", start);
  var cr_price = cr_id_order_list_i_.substring(start, cr_id_order_list_i_.indexOf("</div><td align=center valign=top>") - end_correct);
  return cr_price.replace(/,/,".");
}

function getCr_price_one_way(cr_id_order_list_i_)
{
  if (jb_longhaul != "1")
    return "";

  var start = cr_id_order_list_i_.indexOf("\" value=\"") + "\" value=\"".length;
  start = cr_id_order_list_i_.indexOf("\" value=\"", start) + "\" value=\"".length;
  return cr_id_order_list_i_.substring(start, cr_id_order_list_i_.indexOf("\"><input type=\"hidden\" name=\"jb_cr_price_one_way_sub_"));
}

function getCr_price_one_way_sub(cr_id_order_list_i_)
{
  if (jb_longhaul != "1")
    return "";

  var start = cr_id_order_list_i_.indexOf("\" value=\"") + "\" value=\"".length;
  start = cr_id_order_list_i_.indexOf("\" value=\"", start) + "\" value=\"".length;
  start = cr_id_order_list_i_.indexOf("\" value=\"", start) + "\" value=\"".length;
  return cr_id_order_list_i_.substring(start, cr_id_order_list_i_.indexOf("\"><input type=\"hidden\" name=\"cr_fuel_markup_"));
}

function getCr_fuel_markup(cr_id_order_list_i_)
{
  if (jb_longhaul != "1")
    return "";

  var start = cr_id_order_list_i_.lastIndexOf("\" value=\"") + "\" value=\"".length;
  return cr_id_order_list_i_.substring(start, cr_id_order_list_i_.lastIndexOf("\"></td><td>&nbsp;"));
}

function is_blocked(cr_id_order_list_i_)
{
  if (typeof(cr_id_order_list_blocked)!='undefined'&&typeof(MASK_COURIER_BLOCK_BLOCKED)!='undefined') {
    if(MASK_COURIER_BLOCK_BLOCKED=="1") {
      cr_id = getCr_id2(cr_id_order_list_i_);
      for (var i = 0; i < cr_id_order_list_blocked.length; i++)
        if (cr_id_order_list_blocked[i] == cr_id)
          return true;
    }
  }
  return false;
}
var refresh_done = false;


function is_in_cr_id_order_list_customer(cr_sid)
{
//alert("'" + cr_sid + "' '" + cr_id_order_list_customer.length + "'");
  for (var j = 0; j < cr_id_order_list_customer.length; j++)
  {
    if (cr_id_order_list_customer[j] == cr_sid) {
//alert("'" + cr_sid + "' '" + cr_id_order_list_customer.length + "'");
      return true;
    }
  }
  return false;
}

/*
 * update_cr_id_list
 *   params: jb_id, ad_zipcode
 *   return:
 *   global: cr_id_order_list
 *
 * Update der Kurierliste in cr_id_order_list per Server-Request, wenn auto_refresh = 0
 * (d.h. automatischer Refresh ausgeschaltet).
 * Wird durch glob_defs.inc.php gesteuert, PHP (job_options.php und jb_list.php) setzt
 * in die Variable auto_refresh den Wert aus MASK_AUTO_REFRESH.
 */

function update_cr_id_list(jb_id, ad_zipcode, hq_id_string)
{
  var php_params = "jb_id=" + jb_id + "&zipcode=" + ad_zipcode + "&hq_id_string=" + hq_id_string;
  // Wenn jb_id nicht angegeben, dann handelt es sich um einen Aufruf aus der Auftragserfassung heraus
  if (jb_id == '')
    php_params = "zipcode=" + getJobList(0, jl_ad_zipcode);
  tempWin = mkPopupWin(
    "function startReload()\n" +
    "{\n" +
    "  checkDone();\n" +
    "  self.location.href=\"../jobs/get_couriers.php?" + php_params + "\";\n" +
    "}\n" +
    "\n" +
    "//--" + ">\n" +
    "<\/script>\n" +
    "<\/head>\n" +
    "<body onLoad=\"startReload()\">\n" +
    "  <table border=0 align=center height=100% cellspacing=0 cellpadding=0 vspace=0 hspace=0>\n" +
    "    <tr>\n" +
    "      <td align=center valign=center>\n" +
    "        <b>Hole Daten...</b>\n" +
    "      <\/td>\n" +
    "    <\/tr>\n" +
    "  <\/table>\n" +
    "<\/body>\n" +
    "<\/html>\n", 0, 0);
  refresh_done = true;
}

function compareFn7(a, b) {
//console.log(getCr_vehicle(a), getCr_vehicle(b));
  if (getCr_vehicle(a) < getCr_vehicle(b)) {
    return -1;
  } else if (getCr_vehicle(a) > getCr_vehicle(b)) {
    return 1;
  }
  return 0;
}

function compareFn2(a, b) {
//console.log(getCr_starting_min(a), getCr_starting_min(b));
  if (Number(getCr_starting_min(a)) < Number(getCr_starting_min(b))) {
    return -1;
  } else if (Number(getCr_starting_min(a)) > Number(getCr_starting_min(b))) {
    return 1;
  }
  return 0;
}

function compareFn3(a, b) {
//console.log(getCr_starting_km(a), getCr_starting_km(b));
  if (Number(getCr_starting_km(a)) < Number(getCr_starting_km(b))) {
    return -1;
  } else if (Number(getCr_starting_km(a)) > Number(getCr_starting_km(b))) {
    return 1;
  }
  return 0;
}

function compareFn4(a, b) {
//console.log(getCr_current_location(a), getCr_current_location(b));
  if (getCr_current_location(a) < getCr_current_location(b)) {
    return -1;
  } else if (getCr_current_location(a) > getCr_current_location(b)) {
    return 1;
  }
  return 0;
}

function compareFn5(a, b) {
//console.log(getCur_cr_homezipcode(a), getCur_cr_homezipcode(b));
  if (getCur_cr_homezipcode(a) < getCur_cr_homezipcode(b)) {
    return -1;
  } else if (getCur_cr_homezipcode(a) > getCur_cr_homezipcode(b)) {
    return 1;
  }
  return 0;
}

function compareFn6(a, b) {
//console.log(getRound_km(a), getRound_km(b));
  if (Number(getRound_km(a)) < Number(getRound_km(b))) {
    return -1;
  } else if (Number(getRound_km(a)) > Number(getRound_km(b))) {
    return 1;
  }
  return 0;
}

function compareFn1(a, b) {
//console.log("compareFn1");
//console.log(getCr_price(a), getCr_price(b));
  if (Number(getCr_price(a)) < Number(getCr_price(b))) {
    return -1;
  } else if (Number(getCr_price(a)) > Number(getCr_price(b))) {
    return 1;
  }
  return 0;
}

function cr_id_order_list_sort(cr_id_order_list_no, s)
{
  var cur_sort = [];
  var cur_i = 0;

  if (cr_id_order_list_no == 1)
    cr_id_order_list_length = cr_id_order_list.length;
  if (cr_id_order_list_no == 2)
    cr_id_order_list_length = cr_id_order_list2.length;

  for (i = 0; i < cr_id_order_list_length; i++)
  {
    if (cr_id_order_list_no == 1)
      cr_id_order_list_i_ = cr_id_order_list[i];
    if (cr_id_order_list_no == 2)
      cr_id_order_list_i_ = cr_id_order_list2[i];
//console.log(cr_id_order_list_i_);
    if (cr_id_order_list_i_.indexOf("<b>Heimat-PLZ</b>") > -1 /* || i == cr_id_order_list_length - 1 */)
    {
//console.log(cr_id_order_list_i_);
//console.log(cur_sort);
	  if (cur_sort.length > 0) {
        switch(Number(s)) {
          case 1:
            cur_sort.sort(compareFn1);
            break;
          case 2:
            cur_sort.sort(compareFn2);
            break;
          case 3:
            cur_sort.sort(compareFn3);
            break;
          case 4:
            cur_sort.sort(compareFn4);
            break;
          case 5:
            cur_sort.sort(compareFn5);
            break;
          case 6:
            cur_sort.sort(compareFn6);
            break;
          case 7:
            cur_sort.sort(compareFn7);
            break;
          default:
            cur_sort.sort(compareFn1);
        }
        // Reverse sort order ?
        var reverse = false;
        if ((cr_id_order_list_no == 1 && cr_id_order_list[cur_i+0] == cur_sort[0] && cr_id_order_list[cur_i+cur_sort.length-1] == cur_sort[cur_sort.length-1]) ||
            (cr_id_order_list_no == 2 && cr_id_order_list2[cur_i+0] == cur_sort[0] && cr_id_order_list2[cur_i+cur_sort.length-1] == cur_sort[cur_sort.length-1]))
          reverse = true;
        for (j = 0; j < cur_sort.length; j++) {
          if (cr_id_order_list_no == 1)
            cr_id_order_list[cur_i+j] = (!reverse ? cur_sort[j] : cur_sort[cur_sort.length-1-j]);
          if (cr_id_order_list_no == 2)
            cr_id_order_list2[cur_i+j] = (!reverse ? cur_sort[j] : cur_sort[cur_sort.length-1-j]);
        }
	  }
      cur_sort = [];
      cur_i = i + 1;
    } else {
	  if (cr_id_order_list_i_.substr(0, 1) != "*" && cr_id_order_list_i_.substr(0, 1) != "#")
        cur_sort[cur_sort.length] = cr_id_order_list_i_;
    }
  }
  if (cur_sort.length > 0) {
    switch(Number(s)) {
      case 1:
        cur_sort.sort(compareFn1);
        break;
      case 2:
        cur_sort.sort(compareFn2);
        break;
      case 3:
        cur_sort.sort(compareFn3);
        break;
      case 4:
        cur_sort.sort(compareFn4);
        break;
      case 5:
        cur_sort.sort(compareFn5);
        break;
      case 6:
        cur_sort.sort(compareFn6);
        break;
      case 7:
        cur_sort.sort(compareFn7);
        break;
      default:
        cur_sort.sort(compareFn1);
    }
    // Reverse sort order ?
    var reverse = false;
    if ((cr_id_order_list_no == 1 && cr_id_order_list[cur_i+0] == cur_sort[0] && cr_id_order_list[cur_i+cur_sort.length-1] == cur_sort[cur_sort.length-1]) ||
        (cr_id_order_list_no == 2 && cr_id_order_list2[cur_i+0] == cur_sort[0] && cr_id_order_list2[cur_i+cur_sort.length-1] == cur_sort[cur_sort.length-1]))
      reverse = true;
    for (j = 0; j < cur_sort.length; j++) {
      if (cr_id_order_list_no == 1)
        cr_id_order_list[cur_i+j] = (!reverse ? cur_sort[j] : cur_sort[cur_sort.length-1-j]);
      if (cr_id_order_list_no == 2)
        cr_id_order_list2[cur_i+j] = (!reverse ? cur_sort[j] : cur_sort[cur_sort.length-1-j]);
    }
  }
}

//var whenString = "<br>";
var lfdNr = 1;

function mk_cr_id_list_content(cr_id_order_list_no, jb_id, ad_zipcode, s)
{
  var lfdNrStr = "";
  var content = "";
  var cr_id_order_list_length;
  var customer_courier_list = false;

  if (typeof (cs_jbedit_cr) != 'undefined')
  {
    if (cs_jbedit_cr == 1)
    {
      customer_courier_list = true;
    }
  }
  if (jb_longhaul == "1")
    cr_id_order_list_sort(cr_id_order_list_no, s);
  if (cr_id_order_list_no == 1)
    cr_id_order_list_length = cr_id_order_list.length;
  if (cr_id_order_list_no == 2)
    cr_id_order_list_length = cr_id_order_list2.length;
  if (cr_id_order_list_length == 0)
    content = "[Kein&nbsp;Eintrag&nbsp;vorhanden]";

  var curPLZ = "***START***";
  var spacer = "<tr><td colspan=10><img src=\"../images/empty.gif\" width=\"1\" height=\"4\" alt=\" \"></td></tr>";
  var cr_id_order_list_i_ = "";
  for (i = 0; i < cr_id_order_list_length; i++)
  {
    curBestAnchorStart = "";
    curBestAnchorEnd = "";
    curSelectedStart = "";
    curSelectedEnd = "";
    if (cr_id_order_list_no == 1)
      cr_id_order_list_i_ = cr_id_order_list[i];
    if (cr_id_order_list_no == 2)
      cr_id_order_list_i_ = cr_id_order_list2[i];
    // Überschrift
    if (cr_id_order_list_i_.substr(0, 1) == "#")
    {
      content = content + spacer + spacer +
        "<tr><td align=left valign=center colspan=10>" +
        cr_id_order_list_i_.substr(1) +
        "<hr>\n" +
        "<\/td><\/tr>";
    } else {
    // Uhrzeit/Aktualisieren
//      if (cr_id_order_list_i_.substr(0, 1) == "~")
//      {
//        content = content + spacer + spacer +
//          "<tr><td align=right valign=center>" +
//          cr_id_order_list_i_.substr(1) +
//          "<\/td><\/tr>";
//      } else {
        // die Gruppennamen herausfiltern
        if (cr_id_order_list_i_.substr(0, 1) == "*")
        {
          content = content + spacer + spacer +
            "<tr><td align=left valign=center " + (jb_longhaul != "1" ? "colspan=10>" : ">") + 
            cr_id_order_list_i_.substr(1) +
            "<\/td><\/tr>";
        } else {
          if (!customer_courier_list || (customer_courier_list && is_in_cr_id_order_list_customer(getCr_id2(cr_id_order_list_i_))))
          {
            if (curPLZ != getPLZ(cr_id_order_list_i_) && jb_id != "")
            {
//            if (curPLZ != "***START***")
                content = content + spacer;
              curPLZ = getPLZ(cr_id_order_list_i_);
            }
            var jb_id_param = "";
            if (jb_id != '' && ad_zipcode != '')
              jb_id_param = "'" + jb_id + "',";
            lfdNrStr = lfdNr.toString();
            if (lfdNr < 10)
              lfdNrStr = "0" + lfdNrStr;
            lfdNr++;
            var cr_id_order_list_i_temp = "";
            cr_id_order_list_i_temp = cr_id_order_list_i_.replace(/\*/,"");
            if(customer_courier_list){
              cr_id_order_list_i_temp = cr_id_order_list_i_temp.replace(/<a href="javascript:popupWindow\('\.\.\/admin\/jb_detail\.php\?job_id=[0-9]+','Auftrag','scrollbars=yes,width=800,height=500'\);">/g,"");
//cr_id_order_list_i_temp = cr_id_order_list_i_temp.replace(/<\/span><\/i><\/a>/,"</span></i>");
            }
            content = content + curBestAnchorStart +
              "<tr><td align=left valign=center" + (jb_longhaul != "1" ? " colspan=10>" + lfdNrStr + ". &nbsp;" + 
              "<a href=\"../locating/locating.php?f_cr_sid=" + getCr_id2(cr_id_order_list_i_) + "\" target=\"_blank\">" + "O" + "<\/a>" : ">") +
              "&nbsp;&nbsp;&nbsp;" +
              (is_blocked(cr_id_order_list_i_) ? "" : 
              "<a href=\"javascript:setCourier("+jb_id_param+"'"+getCr_id2(cr_id_order_list_i_)+"','"+
              getCr_price_one_way(cr_id_order_list_i_)+"','"+getCr_price_one_way_sub(cr_id_order_list_i_)+"','"+getCr_fuel_markup(cr_id_order_list_i_)+"');\">")+
              curBestAnchorStart + curSelectedStart + cr_id_order_list_i_temp + curSelectedEnd + curBestAnchorEnd + "<\/a>" +
              "\n" +
              "<\/td><\/tr>";
//alert(cr_id_order_list_i_);
//&nbsp;MK,&nbsp;13:02&nbsp;-&nbsp;<i><b>B9999A</b>, Großer Bus<i></i>
//&nbsp;</td><td align=center valign=top>Großer Bus</td><td align=center valign=top>262</td><td align=center valign=top>423</td><td align=center valign=top>13597 Berlin, Gewerbehof 17</td><td align=center valign=top>22525</td><td align=center valign=top>1093</td><td align=center valign=top>190,54

//alert(curBestAnchorStart +
//              "<tr><td align=left valign=center" + (jb_longhaul != "1" ? "colspan=10>" + lfdNrStr + ". &nbsp;" + 
//              "<a href=\"../locating/locating.php?f_cr_sid=" + getCr_id2(cr_id_order_list_i_) + "\" target=\"_blank\">" + "O" + "<\/a>" : ">") +
//              "&nbsp;&nbsp;&nbsp;" +
//              (is_blocked(cr_id_order_list_i_) ? "" : 
//              "<a href=\"javascript:setCourier("+jb_id_param+"'"+getCr_id2(cr_id_order_list_i_)+"');\">")+
//              curBestAnchorStart + curSelectedStart + cr_id_order_list_i_temp + curSelectedEnd + curBestAnchorEnd + lfdNrStr + "honk<\/a>" +
//              "\n" +
//              "<\/td><\/tr>");

          }
        }
      }
    }
//  }
  content = content + spacer + spacer;
  return content;
}

var popupWin = null;

/*
 * mk_cr_id_list
 *   params: jb_id, ad_zipcode
 *   return:
 *   global: cr_id_order_list
 *
 * Erzeugt Popup-Fenster für die Kurierauswahl. Wenn Auto-Refresh ausgeschaltet, dann
 * wird die Liste vorher per Server-Request aktualisiert.
 */

function mk_cr_id_list(jb_id, ad_zipcode, hq_id_string, s)
{
  var widthPopupWin = 1012;
  var heightPopupWin = 700;
  var leftPopupWin = (screen.width / 2) - (widthPopupWin / 2) - 5;
  var topPopupWin = (screen.height / 2) - (heightPopupWin / 2) - 50;
  var content = "";
  var curSelectedStart = "";
  var curSelectedEnd = "";
  var curBest = "";
  var curBestAnchorStart = "";
  var curBestAnchorEnd = "";

  if (typeof(s) == 'undefined')
    var s = 0;

//  whenString = "(Stand: <span style=\"color:red\">" + when + "</span> Uhr)<br><br>\n"
  if (auto_refresh == "0" && jb_id != '' && ad_zipcode != '' && s == '0') {
    if (!refresh_done) {
      update_cr_id_list(jb_id, ad_zipcode, hq_id_string);
      return;
    } else {
      refresh_done = false;
    }
  } else {
    if (s != '0' && typeof(vht_id_str) == 'undefined') {
      update_cr_id_list(jb_id, ad_zipcode, hq_id_string);
      return;
    }
  }

  lfdNr = 1;
  content = content + mk_cr_id_list_content(1, jb_id, ad_zipcode, s);
  if (MASK_COURIER_SORT_BY_OCCUPIED == "1")
  {
    content = content + mk_cr_id_list_content(2, jb_id, ad_zipcode, s);
  }

  setCourierFunction_vht_id_str = "vht_id_str = new Array();\n";
  for (var _cr_sid_ in vht_id_str)
    setCourierFunction_vht_id_str = setCourierFunction_vht_id_str + "vht_id_str[\"" + _cr_sid_ + "\"] = \"" + vht_id_str[_cr_sid_].replace(/\n\n/,"\\n\\n") + "\";\n";
  setCourierFunction =
    "function setCourier(jb_id,cr_sid,jb_cr_price,jb_cr_price_sub,jb_cr_fuel_markup)\n" +
    "{\n" +
    setCourierFunction_vht_id_str +
    "  addmsg = vht_id_str[cr_sid];\n" +
    "  var penalty = 0;\n"+
    "  if (this.document.reset_cr_availabletime_form.reset_cr_availabletime.checked) {\n"+
    "    addmsg = \"\\n(Freimeldezeit von \" + this.document.reset_cr_availabletime_form.cur_cr_sid.value + \" wird aktualisiert)\" +  addmsg;\n"+
    "    penalty = 1;\n"+
    "  }"+
    "  if (confirm(\"Kurier \" + cr_sid + \" dem Auftrag \" + jb_id + \" zuweisen?\" + addmsg)) {\n" +
    "    this.opener.setCourier(jb_id,cr_sid,penalty,jb_cr_price,jb_cr_price_sub,jb_cr_fuel_markup);\n"+
    "    self.close();\n" +
    "  };\n" +
    "};\n";
  // Wenn jb_id nicht angegeben, dann handelt es sich um einen Aufruf aus der Auftragserfassung heraus
  if (jb_id == '' || ad_zipcode == '')
  {
    setCourierFunction =
      "function setCourier(cr_sid)\n" +
      "{\n" +
      "  opener.parent.job_options.document.tourOptions.cr_id_order.value = cr_sid;\n" +
      "  opener.parent.job_options.document.tourOptions.cr_id_order_hidden.value = cr_sid;\n" +
      "  self.close();\n" +
      "  opener.checkCr_id_order();\n" +
      "  opener.parent.job_options.document.forms.tourOptions.cr_id_order.focus();\n" +
      "};\n";
    if (ad_zipcode == 'calculator')
      setCourierFunction =
        "function setCourier(cr_sid)\n" +
        "{\n" +
        "  opener.cur_popupWin.document.forms[0].tmp_add_sid.value = cr_sid;\n" +
        "  self.close();\n" +
        "  opener.cur_popupWin.document.forms[0].tmp_add_sid.focus();\n" +
      "};\n";
  }
  heightPopupWin += 8;
  if (popupWin === null || popupWin.closed)
    popupWin=window.open("about:blank", "",
      "dependent=yes,width=" + widthPopupWin + ",height=" + heightPopupWin +
      ",left=" + leftPopupWin + ",top=" + topPopupWin + ",scrollbars=yes");
  popupWin.document.open();
  popupWin.document.writeln(
    "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n" +
    "<html lang=\"de\">\n" +
    "<head>\n" +
    "<meta http-equiv=\"pragma\" content=\"no-cache\">\n" +
    "<meta http-equiv=\"expires\" content=\"0\">\n" +
    "<title>votian: Auftragserfassung - Liste der Transporteure<\/title>\n"+
    "<link rel=\"stylesheet\" type=\"text/css\" href=\"../css/phoenix.css.php\">\n"+
    "<script type=\"text/javascript\">\n" +
    "<!-" + "-\n" +
    "function reloadMe(s) {\n" +
    "  if (typeof(s) == 'undefined') {\n" +
    "    opener.popupWin = null;\n" +
    "    opener.setTimeout(\"mk_cr_id_list('" + jb_id + "' , '" + ad_zipcode + "' , '" + hq_id_string + "')\",100);\n" +
    "    self.close();\n" +
    "  } else {\n" +
    "    if (typeof(opener.vht_id_str) != 'undefined') {\n" +
    "      opener.mk_cr_id_list('" + jb_id + "' , '" + ad_zipcode + "' , '" + hq_id_string + "' , s);\n" +
    "    } else {\n" +
    "      opener.popupWin = null;\n" +
    "      opener.setTimeout(\"mk_cr_id_list('" + jb_id + "' , '" + ad_zipcode + "' , '" + hq_id_string + "', \" + s + \")\",100);\n" +
    "      self.close();\n" +
    "    }\n" +
    "  }\n" +
    "}\n" +
    "// Opens a new (popup-)window with specified parameters\n" +
    "function popupWindow(url,title,config) {\n" +
    "  popup = window.open(url,title,config);\n" +
    "}\n" +
    "function push_autoranking(url,title,config) {\n" +
    "  if (typeof(this.document.reset_cr_availabletime_form.ignore_fav_only) != 'undefined') {\n" +
    "    if (this.document.reset_cr_availabletime_form.ignore_fav_only.checked) {\n" +
    "      url = url + '&ignore_fav_only=1';\n" +
    "    }\n" +
    "  }\n" +
    "  popup = window.open(url,title,config);\n" +
    "}\n" +
    setCourierFunction +
    "\n" +
    "-" + "->\n" +
    "<\/script>\n" +
    "<\/head>\n" +
    "<body bgcolor=\"#FFFFFA\" leftmargin=\"1\" topmargin=\"1\" marginwidth=\"0\" marginheight=\"0\" link=\"#990000\" vlink=\"#990000\" alink=\"#990000\">\n" +
    "  <table border=0 align=center width=100% cellspacing=0 cellpadding=0 vspace=0 hspace=0>\n" +
//    "<form action=\"\">\n" +
    "    <tr>\n" +
    "      <td align=center valign=center>\n" +
//    "        <br><b>Liste der Transporteure</b><br>" +
    "      &nbsp;<\/td>\n" +
    "    <\/tr>\n" +
    "    <tr>\n" +
    "      <td align=center valign=center>\n" +
    "  <table border=2 align=center width=96% cellspacing=0 cellpadding=0 vspace=0 hspace=0 bgcolor=\"#FFFFFF\">\n" +
    "    <tr>\n" +
    "      <td align=center valign=center>\n" +
    "  <table border=0 align=center width=96% cellspacing=0 cellpadding=0 vspace=0 hspace=0>\n" +
//    whenString +
    content +
    "  <\/table>\n" +
    "      <\/td>\n" +
    "    <\/tr>\n" +
    "  <\/table>\n" +
    "      <\/td>\n" +
    "    <\/tr>\n" +
    "  <\/table>\n" +
//    "<br>\n" +
//    "<center><input type=\"button\" value=\"Schließen\" onClick=\"self.close()\">\n</center>" +
//    "<br>\n" +
//    "<\/form>\n" +
    "<\/body>\n" +
    "<\/html>\n");
  popupWin.document.close();
}
