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

require_once("HTML/IT.php");
include_once("../include/caglobal.inc.php");
include_once("../include/global.inc.php");
include_once("../include/services_func.inc.php");

$hq_id = $HTTP_SESSION_VARS['hq_id'];

list ($zipcode1, $zipcode2, $zipcode3, $zipcode4, $zipcode5, $zipcode6, $vht_id) =
  getHttpVars(array("zipcode1", "zipcode2", "zipcode3", "zipcode4", "zipcode5", "zipcode6", "vht_id"));
//if ($zipcode1 == "" || $zipcode2 == "" || $vht_id == "") myDie ("$PHP_SELF: Parameter fehlt!");

// Der Normalfall
$cs_invmode_default = -1;
// Besonderheit in Berlin: Zuerst Bereichs-Preise ermitteln ...
if ($hq_id == 3):
  $cs_invmode_default = 2;
endif;

$grundpreis = 0;
list($tmpGrundpreis, $plzpreis1) = getPrice($zipcode1, $zipcode2, $cs_invmode_default);
if ($tmpGrundpreis > 0) $grundpreis = $tmpGrundpreis;
list($tmpGrundpreis, $plzpreis2) = getPrice($zipcode2, $zipcode3, $cs_invmode_default);
if ($tmpGrundpreis > 0) $grundpreis = $tmpGrundpreis;
list($tmpGrundpreis, $plzpreis3) = getPrice($zipcode3, $zipcode4, $cs_invmode_default);
if ($tmpGrundpreis > 0) $grundpreis = $tmpGrundpreis;
list($tmpGrundpreis, $plzpreis4) = getPrice($zipcode4, $zipcode5, $cs_invmode_default);
if ($tmpGrundpreis > 0) $grundpreis = $tmpGrundpreis;
list($tmpGrundpreis, $plzpreis5) = getPrice($zipcode5, $zipcode6, $cs_invmode_default);
if ($tmpGrundpreis > 0) $grundpreis = $tmpGrundpreis;
 
$fullprice = $grundpreis + $plzpreis1 + $plzpreis2 + $plzpreis3 + $plzpreis4 + $plzpreis5;
$valign = "center";

$area_names1 = "";
$area_names2 = "";
$area_names3 = "";
$area_names4 = "";
$area_names5 = "";

$area_price1 = "";
$area_price2 = "";
$area_price3 = "";
$area_price4 = "";
$area_price5 = "";

$area_fullprice = "";

// ... dann PLZ-Preise ermitteln!
if ($hq_id == 3):
  $valign = "bottom";
  
  $area_name1 = getAreaName($zipcode1);
  $area_name2 = getAreaName($zipcode2);
  $area_name3 = getAreaName($zipcode3);
  $area_name4 = getAreaName($zipcode4);
  $area_name5 = getAreaName($zipcode5);
  $area_name6 = getAreaName($zipcode6);

  $area_names1 = "<span style=\"color:red\">" . "VON&nbsp;$area_name1&nbsp;NACH&nbsp;$area_name2" . "</span>";
  $area_names2 = "<span style=\"color:red\">" . "VON&nbsp;$area_name2&nbsp;NACH&nbsp;$area_name3" . "</span>";
  $area_names3 = "<span style=\"color:red\">" . "VON&nbsp;$area_name3&nbsp;NACH&nbsp;$area_name4" . "</span>";
  $area_names4 = "<span style=\"color:red\">" . "VON&nbsp;$area_name4&nbsp;NACH&nbsp;$area_name5" . "</span>";
  $area_names5 = "<span style=\"color:red\">" . "VON&nbsp;$area_name5&nbsp;NACH&nbsp;$area_name6" . "</span>";

  list($tmpGrundpreis, $area_price1) = getPrice($zipcode1, $zipcode2, 1);
  list($tmpGrundpreis, $area_price2) = getPrice($zipcode2, $zipcode3, 1);
  list($tmpGrundpreis, $area_price3) = getPrice($zipcode3, $zipcode4, 1);
  list($tmpGrundpreis, $area_price4) = getPrice($zipcode4, $zipcode5, 1);
  list($tmpGrundpreis, $area_price5) = getPrice($zipcode5, $zipcode6, 1);
  $area_fullprice = $grundpreis + $area_price1 + $area_price2 + $area_price3 + $area_price4 + $area_price5;
endif;

echo 
  "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n" .
  "<html lang=\"de\">\n" .
  "<head>\n" .
  "<title>Mi2ju: Preisanfrage</title>\n" .
  "<link rel=\"stylesheet\" type=\"text/css\" href=\"../css/phoenix.css\">\n" .
  "<script type=\"text/javascript\">\n" .
  "<!" . "--\n" .
  "var curZipcode = \"\";\n" .
  "\n" .
  "function searchAddress() {\n" .
  "\n" .
  "/*  switch (curZipcode)\n" .
  " {\n" .
  "    case 1:\n" .
  "      f_zipcode = document.forms[0].zipcode1.value;\n" .
  "      break;\n" .
  "    case 2:\n" .
  "      f_zipcode = document.forms[0].zipcode2.value;\n" .
  "      break;\n" .
  "    case 3:\n" .
  "      f_zipcode = document.forms[0].zipcode3.value;\n" .
  "      break;\n" .
  "    case 4:\n" .
  "      f_zipcode = document.forms[0].zipcode4.value;\n" .
  "      break;\n" .
  "    case 5:\n" .
  "      f_zipcode = document.forms[0].zipcode5.value;\n" .
  "      break;\n" .
  "    case 6:\n" .
  "      f_zipcode = document.forms[0].zipcode6.value;\n" .
  "      break;\n" .
  "    default:\n" .
  "      f_zipcode = \"\";\n" .
  "      break;\n" .
  "  } */ \n" .
  "  if (curZipcode == \"\") curZipcode = 'price_query1';\n" .
  "  var widthPopupWin = 800;\n" .
  "  var heightPopupWin = 600;\n" .
  "  var leftPopupWin = (screen.width / 2) - (widthPopupWin / 2) - 12;\n" .
  "  var topPopupWin = (screen.height / 2) - (heightPopupWin / 2) - 50;\n" .
  "  var popupWin;\n" .
  "  popupWin = window.open(\"../admin/ad_list.php?f_act=search&f_zipcode=\" /* + f_zipcode */ + \"&tourno=\" + curZipcode, \"\",\"dependent=yes,width=\" + widthPopupWin + \",height=\" + heightPopupWin +\",left=\" + leftPopupWin + \",top=\" + topPopupWin + \",scrollbars=yes,resizable=yes,status=no\");\n" .
  "}\n" .
  "\n" .
  "function doFocus(){\n" .
  "  document.forms[0].zipcode1.focus();\n" .
  "  if (document.forms[0].zipcode6.value == \"\")\n" .
  "    document.forms[0].zipcode6.focus();\n" .
  "  if (document.forms[0].zipcode5.value == \"\")\n" .
  "    document.forms[0].zipcode5.focus();\n" .
  "  if (document.forms[0].zipcode4.value == \"\")\n" .
  "    document.forms[0].zipcode4.focus();\n" .
  "  if (document.forms[0].zipcode3.value == \"\")\n" .
  "    document.forms[0].zipcode3.focus();\n" .
  "  if (document.forms[0].zipcode2.value == \"\")\n" .
  "    document.forms[0].zipcode2.focus();\n" .
  "  if (document.forms[0].zipcode1.value == \"\")\n" .
  "    document.forms[0].zipcode1.focus();\n" .
  "}\n" .
  "\n" .
  "function doDelete(){\n" .
  "  self.location.href=\"../jobs/price_query.php?zipcode1=&zipcode2=&vht_id=" . getFieldValueFromId("headquarters", "hq_id", "$hq_id", "hq_vht_default") . "\";\n" .
  "}\n" .
  "\n" .
  "function updatePrice(){\n" .
  "  if (document.forms[0].zipcode1.value != '' && document.forms[0].zipcode2.value != '')\n" .
  "    self.location.href=\"../jobs/price_query.php?zipcode1=\" + document.forms[0].zipcode1.value + \"&zipcode2=\" + document.forms[0].zipcode2.value + \"&zipcode3=\" + document.forms[0].zipcode3.value + \"&zipcode4=\" + document.forms[0].zipcode4.value + \"&zipcode5=\" + document.forms[0].zipcode5.value + \"&zipcode6=\" + document.forms[0].zipcode6.value + \"&vht_id=\" + document.forms[0].vht_id.value;\n" .
  "}\n" .
  "\n" .
  "//--" . ">\n" .
  "</script>\n" .
  "</head>\n" .
  "<body onload=\"doFocus()\"\n>" .
  "  <table border=0 align=center width=90% cellspacing=0 cellpadding=0 vspace=0 hspace=0>\n" .
  "    <tr>\n" .
  "<form>\n" .
  "      <td width=70% align=right valign=center>\n" .
  "  VON PLZ&nbsp;<input name=\"zipcode1\" type=\"text\" value=\"" . $zipcode1 . "\" size=\"5\" maxlength=\"5\" class=\"input\"\n" .
  "    onchange=\"updatePrice()\" onFocus=\"curZipcode='price_query1'\">\n" .
  "  NACH PLZ&nbsp;<input name=\"zipcode2\" type=\"text\" value=\"" . $zipcode2 . "\" size=\"5\" maxlength=\"5\" class=\"input\"\n" .
  "    onchange=\"updatePrice()\" onFocus=\"curZipcode='price_query2'\">\n" .
  "      <br>$area_names1<br></td><td width=30% align=right valign=$valign>\n" .
  ($hq_id == 3 ? formatPrice($area_price1) . "<br>" : "") .
  "<span style=\"color:red\">" . formatPrice($plzpreis1) . "</span><br>" .

  "      </td></tr><tr><td align=right valign=center>\n" .
  "  VON PLZ&nbsp;<input name=\"zipcode2_disabled\" type=\"text\" value=\"" . $zipcode2 . "\" size=\"5\" maxlength=\"5\" class=\"input\"\n" .
  "    disabled>\n" .
  "  NACH PLZ&nbsp;<input name=\"zipcode3\" type=\"text\" value=\"" . $zipcode3 . "\" size=\"5\" maxlength=\"5\" class=\"input\"\n" .
  "    onchange=\"updatePrice()\" onFocus=\"curZipcode='price_query3'\">\n" .
  "      <br>$area_names2<br></td><td align=right valign=$valign>\n" .
  ($hq_id == 3 ? formatPrice($area_price2) . "<br>" : "") .
  "<span style=\"color:red\">" . formatPrice($plzpreis2) . "</span><br>" .

  "      </td></tr><tr><td align=right valign=center>\n" .
  "  VON PLZ&nbsp;<input name=\"zipcode3_disabled\" type=\"text\" value=\"" . $zipcode3 . "\" size=\"5\" maxlength=\"5\" class=\"input\"\n" .
  "    disabled>\n" .
  "  NACH PLZ&nbsp;<input name=\"zipcode4\" type=\"text\" value=\"" . $zipcode4 . "\" size=\"5\" maxlength=\"5\" class=\"input\"\n" .
  "    onchange=\"updatePrice()\" onFocus=\"curZipcode='price_query4'\">\n" .
  "      <br>$area_names3<br></td><td align=right valign=$valign>\n" .
  ($hq_id == 3 ? formatPrice($area_price3) . "<br>" : "") .
  "<span style=\"color:red\">" .  formatPrice($plzpreis3) . "</span><br>" .

  "      </td></tr><tr><td align=right valign=center>\n" .
  "  VON PLZ&nbsp;<input name=\"zipcode4_disabled\" type=\"text\" value=\"" . $zipcode4 . "\" size=\"5\" maxlength=\"5\" class=\"input\"\n" .
  "    disabled>\n" .
  "  NACH PLZ&nbsp;<input name=\"zipcode5\" type=\"text\" value=\"" . $zipcode5 . "\" size=\"5\" maxlength=\"5\" class=\"input\"\n" .
  "    onchange=\"updatePrice()\" onFocus=\"curZipcode='price_query5'\">\n" .
  "      <br>$area_names4<br></td><td align=right valign=$valign>\n" .
  ($hq_id == 3 ? formatPrice($area_price4) . "<br>" : "") .
  "<span style=\"color:red\">" .  formatPrice($plzpreis4) . "</span><br>" .

  "      </td></tr><tr><td align=right valign=center>\n" .
  "  VON PLZ&nbsp;<input name=\"zipcode5_disabled\" type=\"text\" value=\"" . $zipcode5 . "\" size=\"5\" maxlength=\"5\" class=\"input\"\n" .
  "    disabled>\n" .
  "  NACH PLZ&nbsp;<input name=\"zipcode6\" type=\"text\" value=\"" . $zipcode6 . "\" size=\"5\" maxlength=\"5\" class=\"input\"\n" .
  "    onchange=\"updatePrice()\" onFocus=\"curZipcode='price_query6'\">\n" .
  "      <br>$area_names5<br></td><td align=right valign=$valign>\n" .
  ($hq_id == 3 ? formatPrice($area_price5) . "<br>" : "") .
  "<span style=\"color:red\">" . formatPrice($plzpreis5) . "</span><br>" .

  "      </td></tr><tr><td align=right valign=center>\n" .
  "  MIT FAHRZEUG&nbsp;" .
  "  <select name=\"vht_id\" class=\"input\" onchange=\"updatePrice()\">\n" .
  addOptionsFromTable("metatype", "mt_sort", "mt_value", "mt_sort", "mt_type = 'vehicletype' AND mt_sort > 0", $vht_id) .
  "  </select>\n" .
  "      <br><br></td><td align=right valign=$valign>\n" .
  ($hq_id == 3 ? formatPrice($grundpreis) . "<br>" : "") .
  "<span style=\"color:red\">" . formatPrice($grundpreis) . "</span><br>" .

  "      </td></tr><tr><td align=right valign=center>\n" .  
  "<b>Gesamtpreis: </b>" .
  "      <br><br></td><td align=right valign=center>\n" .
  ($hq_id == 3 ? "<b>" . formatPrice($area_fullprice) . "</b><br>" : "") .
  "<span style=\"color:red\"><b>" . formatPrice($fullprice) . "</b></span><br>" .
  
  "      </td></tr><tr><td colspan=2 align=center valign=top>\n" .
  "<br>" .
  "<input type=\"button\" value=\"Schließen\" onClick=\"self.close()\" accesskey=\"s\">&nbsp;\n" .
  "<input type=\"button\" value=\"Rücksetzen\" onClick=\"doDelete()\" accesskey=\"r\">&nbsp;\n" .
  "<input type=\"button\" name=\"AdressButton\" value=\"Suche PLZ\" title=\"Suche PLZ\" onclick=\"searchAddress();\">" .
  "      </td>\n" .
  "    </tr>\n" .
  "  </table>\n" .
  "</form>\n" .
  "</body>\n" .
  "</html>\n";

function getPrice($zipcode1, $zipcode2, $cs_invmode = -1)
{
  global $db, $hq_id, $vht_id;
  
  if (strlen($zipcode1) == 5 && strlen($zipcode2) == 5 && is_numeric($zipcode1) && is_numeric($zipcode2)):
    if ($cs_invmode > -1):
      $cs_id = $cs_invmode * -1;
    else:
      $cs_id = 0;
    endif;
    // $cs_id decides about invmode
    // if 0, then default invmode from hq_id is taken
    // for Berlin, $cs_id directly takes invmode in negative form (in order to distinguish from ordinary cs_id, that always are postive)
    $today = getdate();
    $jb_ordertime = $today['year'] . "-" . $today['mon'] . "-" . $today['mday'] . " "
      . $today['hours'] . ":" . $today['minutes'] . ":" . $today['seconds'];
    // Grundpreis
    // Servicetype-Name (Fahrrad etc.)
    $srvt_name = $db->getOne(
      "SELECT mt_value FROM metatype WHERE mt_sort = '$vht_id' AND mt_type = 'vehicletype'");
    list ($grundpreis, $fd, $customer_specific) =
      saveServiceCosts("Grundpreis", $srvt_name, $hq_id, $cs_id, 0 /* $jb_id */, 0, $jb_ordertime, 0 /* count */, 1 /* $costsplit_count */, 1 /* $getPriceOnly */, false /* $jb_cash */, "0" /* $csc_id_payer */, 0 /* $jb_storno */, false /* zipcode1 */, false /* zipcode2 */, false);
    //$fullPrice += $fp * ((100-$fd)/100);
    //$discount_sign = ($fd > 0 || $customer_specific || $discount_sign == "*" ? "*" : "");
    // PLZ-Preis
    list ($plzpreis, $fd, $customer_specific) =
      saveServiceCosts($zipcode1, $zipcode2, $hq_id, $cs_id, 0 /* $jb_id */, 0, $jb_ordertime, 0 /* count */, 1 /* $costsplit_count */, 1 /* $getPriceOnly */, false /* $jb_cash */, "0" /* $csc_id_payer */, 0 /* $jb_storno */, false /* zipcode1 */, false /* zipcode2 */, true);
    //$fullPrice += $fp * ((100-$fd)/100);
    //$discount_sign = ($fd > 0 || $customer_specific || $discount_sign == "*" ? "*" : "");
  else:
    $zipcode1 = "";
    $zipcode2 = "";
    $grundpreis = 0;
    $plzpreis = 0;
  endif;
  
  return array($grundpreis, $plzpreis);
}

function formatPrice($thePrice)
{
  if (strlen($thePrice) > 0):
    return str_replace(".", ",", sprintf("%01.2f", $thePrice)) . " EUR";
  else:
    return "";
  endif;
}

?>
