<?php
  include_once ("../include/global.inc.php");
  include_once ("../include/inc_calendar.inc.php");
  include_once ("../groupware/calendar.php");
 
  // Check HTTP-Parameters
  getSecHttpVars("1",array("f_act", "customerId", "cscIdRoot", "cscIdCurrent", "statusMessage", "deactivateMenu", "grpId",
                           "mode", "apId", "f_selUsrId", "f_day", "f_month", "f_year", "f_day_to", "f_month_to", "f_year_to"));

  if ($mode != "") :
     header("Content-Type: text/html; charset=ISO-8859-1\n");
  endif;

// echo "alert('" . $mode . "' + '" . $apId . "');";


  if ($mode == "1") :
     // Get appointment data
     if ($apId != "") :

        $sqlStmt = "SELECT ap.ap_id, ap.ap_text, ap.ap_confirmed, ap.ap_execdate, ap.ap_enddate,"
                   . "     cmp.cmp_id, cmp.cmp_comp, cmp.cmp_comp2, cs.cs_id, cs.cs_eid,"
                   . "     pt.pt_cmp_comp, pt.pt_cmp_comp2, pt.pt_cs_eid, pt.pt_id,"
                   . "     ap.usr_id, usr.usr_firstname, usr.usr_name, ap.ap_participants,"
                   . "     ap.ap_category_1, ap.ap_category_2, ap.ap_category_3, ap.ap_category_4"
                   . " FROM phoenix_group.appointment AS ap" 
                   . "      LEFT JOIN phoenix.customer AS cs ON ap.cs_id = cs.cs_id"
                   . "      LEFT JOIN phoenix.company AS cmp ON cs.cmp_id = cmp.cmp_id"
                   . "      LEFT JOIN phoenix.user AS usr ON ap.usr_id = usr.usr_id"
                   . "      LEFT JOIN phoenix.prospect AS pt ON ap.pt_id = pt.pt_id"
                   . " WHERE  ap.ap_id = '" . $apId . "'";

        $result = $db->query($sqlStmt);
        if (DB::isError($result)) die ("$PHP_SELF: <br>$sqlStmt<br>" . $result->getMessage());
        $count = 0;
        while ($row = $result->fetch_assoc()):
              $tmpId = $row["ap_id"];
              $tmpText = $row["ap_text"];
                $tmpText = my_str_check_js($tmpText);
              $tmpExecDateTime = $row["ap_execdate"];
                $tmpExecDate = substr($tmpExecDateTime,8,2) . "." . substr($tmpExecDateTime,5,2) . "." . substr($tmpExecDateTime,0,4);
                $tmpExecTime = substr($tmpExecDateTime,11,5);
                $tmpExecYear = substr($tmpExecDateTime,0,4);
                $tmpExecMonth = substr($tmpExecDateTime,5,2);
                $tmpExecDay = substr($tmpExecDateTime,8,2);
                $tmpExecHour = substr($tmpExecDateTime,11,2);
                $tmpExecMinute = substr($tmpExecDateTime,14,2);
              $tmpCmpId = trim($row["cmp_id"]);
              $tmpCmpComp = trim($row["cmp_comp"]);
              $tmpCmpComp2 = trim($row["cmp_comp2"]);
              $tmpCsId = $row["cs_id"];
              $tmpCsEid = $row["cs_eid"];
              $tmpEndDateTime = $row["ap_enddate"];
                $tmpEndDate = substr($tmpEndDateTime,8,2) . "." . substr($tmpEndDateTime,5,2) . "." . substr($tmpEndDateTime,0,4);
                $tmpEndTime = substr($tmpEndDateTime,11,5);
                $tmpEndYear = substr($tmpEndDateTime,0,4);
                $tmpEndMonth = substr($tmpEndDateTime,5,2);
                $tmpEndDay = substr($tmpEndDateTime,8,2);
                $tmpEndHour = substr($tmpEndDateTime,11,2);
                $tmpEndMinute = substr($tmpEndDateTime,14,2);
              $tmpUsrId = trim($row["usr_id"]);
              $tmpUsrFirstname = trim($row["usr_firstname"]);
              $tmpUsrName = trim($row["usr_name"]);
              $tmpApCategory1 = $row["ap_category_1"];
                if ($tmpApCategory1 == "") : $tmpApCategory1 = "1"; endif;
              $tmpApCategory2 = $row["ap_category_2"];
                if ($tmpApCategory2 == "") : $tmpApCategory2 = "1"; endif;
              $tmpApCategory3 = $row["ap_category_3"];
                if ($tmpApCategory3 == "") : $tmpApCategory3 = "1"; endif;
              $tmpApCategory4 = $row["ap_category_4"];
                if ($tmpApCategory4 == "") : $tmpApCategory4 = "0"; endif;
              $tmpParticipants = $row["ap_participants"];
                $tmpParticipants = substr($tmpParticipants,1,-1);     // Remove commata from db-field
              $tmpConfirmed = trim($row["ap_confirmed"]);
                $tmpConfirmed = substr($tmpConfirmed,1,-1);           // Remove commata from db-field
              $tmpPtCmpComp = trim($row["pt_cmp_comp"]);
              $tmpPtCmpComp2 = trim($row["pt_cmp_comp2"]);
              $tmpPtCsEid = $row["pt_cs_eid"];
              $tmpPtId = $row["pt_id"];              
              $count++;
        endwhile;
        $result->free();

        if ($count > 0) :

           // Begin date fields
           echo "setSelectedValue('f_day', " . $tmpExecDay . ");\n";
           echo "setSelectedValue('f_month', " . $tmpExecMonth . ");\n";
           echo "setSelectedValue('f_year', " . $tmpExecYear . ");\n";
           echo "setSelectedValue('f_hour', " . $tmpExecHour . ");\n";
           echo "setSelectedValue('f_minute', " . $tmpExecMinute . ");\n";

           // End date fields
           echo "setSelectedValue('f_day_to', " . $tmpEndDay . ");\n";
           echo "setSelectedValue('f_month_to', " . $tmpEndMonth . ");\n";
           echo "setSelectedValue('f_year_to', " . $tmpEndYear . ");\n";
           echo "setSelectedValue('f_hour_to', " . $tmpEndHour . ");\n";
           echo "setSelectedValue('f_minute_to', " . $tmpEndMinute . ");\n";

           // Categories
           echo "setSelectedValue('f_ap_cat_1', " . $tmpApCategory1 . ");\n";
           echo "setSelectedValue('f_ap_cat_2', " . $tmpApCategory2 . ");\n";
           echo "setSelectedValue('f_ap_cat_3', " . $tmpApCategory3 . ");\n";
           echo "setSelectedValue('f_ap_cat_4', " . $tmpApCategory4 . ");\n";

           // Participants
           if ($tmpParticipants != "") : 
              $tmpParticipants = spliti(",", $tmpParticipants);   // It has to be an array
              $tmpParticipantsLen = count($tmpParticipants);

              echo "participantsLen = document.getElementsByName('f_usr_id[]').length;";
              echo "for (var i=0; i<participantsLen; i++) {";
              echo "    document.getElementsByName('f_usr_id[]')[i].checked = false;\n";
              echo "};\n";

              for ($k = 0; $k < $tmpParticipantsLen; $k++) :
                   echo "for (var i=0; i<participantsLen; i++) {";
                   echo "    if (document.getElementsByName('f_usr_id[]')[i].value == '" . $tmpParticipants[$k] . "') {\n";
                   echo "       document.getElementsByName('f_usr_id[]')[i].checked = true;\n";
                   echo "    };\n";
                   echo "};\n";
              endfor;
           endif;

           // Relations
           echo "document.forms[0].g_cs_eid.value = '" . $tmpCsEid . "';\n";
           // echo "document.forms[0].g_pt_cs_eid.value = '" . $tmpPtCsEid . "';\n";

           // Text
           echo "document.forms[0].f_text.value = '" . $tmpText . "';\n";

           // Toggle to edit mode
           echo "toggleDisplay('1');";
        endif;
     endif;

  elseif ($mode == "2") :
     // Get number of appointments
     if ($f_selUsrId != "") :
        if ($f_day != "" && $f_month != "" && $f_year != "") :
           if ($f_day_to == "") : $f_day_to = $f_day; endif;
           if ($f_month_to == "") : $f_month_to = $f_month; endif;
           if ($f_year_to == "") : $f_year_to = $f_year; endif;
           $tmpAppointments = getAppointmentsFromDB($f_year, $f_month, $f_day, $f_year_to, $f_month_to, $f_day_to);
           $retValue = count($tmpAppointments);
           echo "retValue = '" . $retValue . "';";
        endif;
     endif;

  elseif ($mode == "3") :
     // Get participants by group
     $retValue = "";
     $usrIdArray = getColVectorFromDB2ArrayByClause("employee", "usr_id", "emp_group LIKE '%," . $grpId . ",%'", "", "", "");
     $usrIdArrayLen = count($usrIdArray);
     if ($usrIdArrayLen > 0) :
        $retValue = implode(",",$usrIdArray);
     endif;
     echo "retArray = Array(" . $retValue . ");";
     

  elseif ($mode == "4") :
  
     // ...

  endif;
?>  