<?php
  include_once ("../include/global.inc.php");
  include_once ("../include/inc_metafield.inc.php");


  // Check HTTP-Parameters
  // Stocks
  getSecHttpVars("1",array("f_act", "customerId", "cscIdRoot", "cscIdCurrent", "statusMessage", "deactivateMenu",
                           "ft", "category", "presetFields", "searchValue"));
  
  if ($ft != "") :
     header("Content-Type: text/html; charset=ISO-8859-1\n");
  endif;
                           
// echo "alert('" . $ft . " | ' + '" . $searchValue . " | ' + '" . $presetFields . " | ' + '" . $category . " ');";

  if ($ft == "co2cs") :
     // Get contact preset with cutomer data
     if ($category != "" && $presetFields != "" && $searchValue != "") :
         $remObjId = $objId;
         $objId = getFieldValueFromId("customer", "cs_eid", $searchValue, "cs_id");
         $presetFields = urldecode($presetFields);
         $presetFieldsArray = getKeyValueArrayFromString($presetFields);
         $tmpKeys = array_keys($presetFieldsArray);
         $tmpKeysLen = count($tmpKeys);
         // Remember the problem if an address has been entered first before selection of the co2cs relation:
         // Should the (address, phone, etc.) data be overwritten...? Presently it always will be overwritten by co2cs selection!
         echo "var overwriteWithPreset = true;\n";
         echo "var atLeastOnePresetFieldIsOccupied = false;\n";
         for ($i = 0; $i < $tmpKeysLen; $i++) :
             // Check for field to be checked is NOT type of "co2cs", because it would be set before associating a new customer
             $tmpFieldType = getFieldValueFromId("metafieldkey", "mtfk_id", $tmpKeys[$i], "mtfk_type");
             if ($tmpFieldType != $ft) :
                echo "if (document.forms[0].f_mtfv_" . $category . "_" . $tmpKeys[$i] . ") {\n";
                echo "   if (trim(document.forms[0].f_mtfv_" . $category . "_" . $tmpKeys[$i] . ".value) != '') {\n";
                echo "      atLeastOnePresetFieldIsOccupied = true;\n";
                echo "   };\n";
                echo "};\n";
             endif;
         endfor;
         echo "if (atLeastOnePresetFieldIsOccupied) {\n";
         echo "   overwriteWithPreset = false;\n";
         echo "   if (confirm('" . getLngt("Möchten Sie die bestehenden Felder mit den neuen Vorbelegungen überschreiben?") . "')) {\n";
         echo "      overwriteWithPreset = true;\n";
         echo "   };\n";
         echo "};\n";
         echo "if (overwriteWithPreset) {\n";
         for ($i = 0; $i < $tmpKeysLen; $i++) :
             // Remember the problem if an address has been entered first before selection of the co2cs relation:
             // Should the (address, phone, etc.) data be overwritten...? Presently it always will be overwritten by co2cs selection!
             echo "   if (document.forms[0].f_mtfv_" . $category . "_" . $tmpKeys[$i] . ") {\n";
             echo "      if (overwriteWithPreset) {\n";
             echo "         document.forms[0].f_mtfv_" . $category . "_" . $tmpKeys[$i] . ".value='" . getFieldValueBySpecialCall($presetFieldsArray[$tmpKeys[$i]]) . "';\n";
             echo "      };\n";
             echo "   };\n";
         endfor;
         echo "};\n";
         $objId = $remObjId;
     endif;

  elseif ($ft == "2") :
     //

  elseif ($ft == "3") :
     //

  endif;
?>  