var _buttons    = mywindow.findChild("_buttons");
var _oldCountry = mywindow.findChild("_oldCountry");
var _newCountry = mywindow.findChild("_newCountry");
var _params;

function set(params)
{
  _params = params;
  var qry = toolbox.executeQuery(
      "SELECT MIN(id), country, country AS code "
    + "  FROM ("
    + "  SELECT addr_id AS id, addr_country AS country"
    + "    FROM addr"
    + "   WHERE addr_country != ''"
    + "     AND addr_country NOT IN (SELECT country_name"
    + "                                FROM country"
    + "                               WHERE country_name IS NOT NULL)"
    + "  UNION ALL"
    + "  SELECT cmhead_id, cmhead_billtocountry"
    + "    FROM cmhead"
    + "   WHERE cmhead_billtocountry != ''"
    + "     AND cmhead_billtocountry NOT IN (SELECT country_name"
    + "                                        FROM country"
    + "                                       WHERE country_name IS NOT NULL)"
    + '     <? if not exists("showAll") ?> AND (NOT cmhead_posted) <? endif ?>'
    + "  UNION ALL"
    + "  SELECT cmhead_id, cmhead_shipto_country"
    + "    FROM cmhead"
    + "    WHERE cmhead_shipto_country != ''"
    + "      AND cmhead_shipto_country NOT IN (SELECT country_name"
    + "                                          FROM country"
    + "                                         WHERE country_name IS NOT NULL)"
    + '      <? if not exists("showAll") ?> AND (NOT cmhead_posted) <? endif ?>'
    + "  UNION ALL"
    + "  SELECT invchead_id, invchead_billto_country"
    + "    FROM invchead"
    + "   WHERE invchead_billto_country != ''"
    + "     AND invchead_billto_country NOT IN (SELECT country_name"
    + "                                           FROM country"
    + "                                          WHERE country_name IS NOT NULL)"
    + '     <? if not exists("showAll") ?> AND (NOT invchead_posted) <? endif ?>'
    + "  UNION ALL"
    + "  SELECT invchead_id, invchead_shipto_country"
    + "    FROM invchead"
    + "   WHERE invchead_shipto_country != ''"
    + "     AND invchead_shipto_country NOT IN (SELECT country_name"
    + "                                           FROM country"
    + "                                          WHERE country_name IS NOT NULL)"
    + '     <? if not exists("showAll") ?> AND (NOT invchead_posted) <? endif ?>'
    + "  UNION ALL"
    + "  SELECT quhead_id, quhead_billtocountry"
    + "    FROM quhead"
    + "   WHERE quhead_billtocountry != ''"
    + "     AND quhead_billtocountry NOT IN (SELECT country_name"
    + "                                        FROM country"
    + "                                       WHERE country_name IS NOT NULL)"
    + '     <? if not exists("showAll") ?> AND (quhead_expire>=CURRENT_DATE) <? endif ?>'
    + "  UNION ALL"
    + "  SELECT quhead_id, quhead_shiptocountry"
    + "    FROM quhead"
    + "   WHERE quhead_shiptocountry != ''"
    + "     AND quhead_shiptocountry NOT IN (SELECT country_name"
    + "                                        FROM country"
    + "                                       WHERE country_name IS NOT NULL)"
    + '     <? if not exists("showAll") ?> AND (quhead_expire>=CURRENT_DATE) <? endif ?>'
    + "  UNION ALL"
    + "  SELECT cohead_id, cohead_billtocountry"
    + "    FROM cohead"
    + "   WHERE cohead_billtocountry != ''"
    + "     AND cohead_billtocountry NOT IN (SELECT country_name"
    + "                                        FROM country"
    + "                                       WHERE country_name IS NOT NULL)"
    + '    <? if not exists("showAll") ?>'
    + "     AND cohead_id IN (SELECT coitem_cohead_id"
    + "                         FROM coitem"
    + "                        WHERE COALESCE(coitem_status, 'O')='O')"
    + '    <? endif ?>'
    + "  UNION ALL"
    + "  SELECT cohead_id, cohead_shiptocountry"
    + "    FROM cohead"
    + "   WHERE cohead_shiptocountry != ''"
    + "     AND cohead_shiptocountry NOT IN (SELECT country_name"
    + "                                       FROM country"
    + "                                      WHERE country_name IS NOT NULL)"
    + '    <? if not exists("showAll") ?>'
    + "     AND cohead_id IN (SELECT coitem_cohead_id"
    + "                         FROM coitem"
    + "                        WHERE COALESCE(coitem_status, 'O')='O')"
    + '    <? endif ?>'
    + ") AS dummy "
    + "GROUP BY country "
    + "ORDER BY country;",

                                 params);
  _oldCountry.populate(qry);
  if (qry.lastError().type != 0)
    toolbox.messageBox("warning", mywindow, qsTr("Database Error"),
                       qry.lastError().text);
}

function sSave()
{
  try {
  var params = _params;
  params.oldCountry = _oldCountry.currentText;
  params.newCountry = _newCountry.currentText;

  var qry = toolbox.executeQuery(
      'UPDATE addr SET addr_country = <? value("newCountry") ?>'
    + ' WHERE addr_country = <? value("oldCountry") ?>;', params);
  if (qry.lastError().type != 0)
    throw new Error("Error updating Address: " + qry.lastError().text);

  qry = toolbox.executeQuery(
      'UPDATE cmhead SET cmhead_billtocountry = <? value("newCountry") ?>'
    + ' WHERE cmhead_billtocountry = <? value("oldCountry") ?>'
    + '  <? if not exists("showAll") ?>'
    + "   AND NOT cmhead_posted"
    + "  <? endif ?>", params);
  if (qry.lastError().type != 0)
    throw new Error("Error updating Credit Memo (Bill-To): " + qry.lastError().text);

  qry = toolbox.executeQuery(
      'UPDATE cmhead SET cmhead_shipto_country = <? value("newCountry") ?>'
    + ' WHERE cmhead_shipto_country = <? value("oldCountry") ?>'
    + '  <? if not exists("showAll") ?>'
    + "   AND NOT cmhead_posted"
    + "  <? endif ?>", params);
  if (qry.lastError().type != 0)
    throw new Error("Error updating Credit Memo (Ship-To): " + qry.lastError().text);

  qry = toolbox.executeQuery(
      'UPDATE invchead SET invchead_billto_country = <? value("newCountry") ?>'
    + ' WHERE invchead_billto_country = <? value("oldCountry") ?>'
    + '  <? if not exists("showAll") ?>'
    + "   AND NOT invchead_posted"
    + "  <? endif ?>", params);
  if (qry.lastError().type != 0)
    throw new Error("Error updating Invoice (Bill-To): " + qry.lastError().text);

  qry = toolbox.executeQuery(
      'UPDATE invchead SET invchead_shipto_country = <? value("newCountry") ?>'
    + ' WHERE invchead_shipto_country = <? value("oldCountry") ?>'
    + '  <? if not exists("showAll") ?>'
    + "   AND NOT invchead_posted"
    + "  <? endif ?>", params);
  if (qry.lastError().type != 0)
    throw new Error("Error updating Invoice (Ship-To): " + qry.lastError().text);

  qry = toolbox.executeQuery(
      'UPDATE quhead SET quhead_billtocountry = <? value("newCountry") ?>'
    + ' WHERE quhead_billtocountry = <? value("oldCountry") ?>'
    + '  <? if not exists("showAll") ?>'
    + "   AND quhead_expire >= CURRENT_DATE"
    + "  <? endif ?>", params);
  if (qry.lastError().type != 0)
    throw new Error("Error updating Quote (Bill-To): " + qry.lastError().text);

  qry = toolbox.executeQuery(
      'UPDATE quhead SET quhead_shiptocountry = <? value("newCountry") ?>'
    + ' WHERE quhead_shiptocountry = <? value("oldCountry") ?>'
    + '  <? if not exists("showAll") ?>'
    + "   AND quhead_expire >= CURRENT_DATE"
    + "  <? endif ?>", params);
  if (qry.lastError().type != 0)
    throw new Error("Error updating Quote (Ship-To): " + qry.lastError().text);

  qry = toolbox.executeQuery(
      'UPDATE cohead SET cohead_billtocountry = <? value("newCountry") ?>'
    + ' WHERE cohead_billtocountry = <? value("oldCountry") ?>'
    + '   <? if not exists("showAll") ?>'
    + "   AND cohead_id IN (SELECT coitem_cohead_id"
    + "                     FROM coitem"
    + "                     WHERE COALESCE(coitem_status, 'O')='O')"
    + "   <? endif ?>", params);
  if (qry.lastError().type != 0)
    throw new Error("Error updating Sales Order (Bill-To): " + qry.lastError().text);

  qry = toolbox.executeQuery(
      'UPDATE cohead SET cohead_shiptocountry = <? value("newCountry") ?>'
    + ' WHERE cohead_shiptocountry = <? value("oldCountry") ?>'
    + '   <? if not exists("showAll") ?>'
    + "   AND cohead_id IN (SELECT coitem_cohead_id"
    + "                     FROM coitem"
    + "                     WHERE COALESCE(coitem_status, 'O')='O')"
    + "   <? endif ?>", params);
  if (qry.lastError().type != 0)
    throw new Error("Error updating Sales Order (Ship-To): " + qry.lastError().text);

  }
  catch (e)
  {
     toolbox.messageBox("critical", mywindow,
                        "bulkFixCountry::sSave() exception", e);

  }
  mywindow.close();
}

_buttons.accepted.connect(sSave);
_buttons.rejected.connect(mywindow, "close()");
