2 // This script is shared by time.tpl, time_edit.tpl (both regular and mobile),
 
   3 // and also by WEB-INF/templates/mobile/timer.tpl.
 
   4 // This creates certain restrictions, such as the form name being "timeRecordForm",
 
   5 // variables such as $client_list, $project_list and others to be assigned in php files
 
   6 // for all pages. Things need to be tested together for all involved files.
 
   8 // We need a few arrays to populate project and task dropdowns.
 
   9 // When client selection changes, the project dropdown must be re-populated with only relevant projects.
 
  10 // When project selection changes, the task dropdown must be repopulated similarly.
 
  12 // project_ids[143] = "325,370,390,400";  // Comma-separated list of project ids for client.
 
  13 // project_names[325] = "Time Tracker";   // Project name.
 
  14 // task_ids[325] = "100,101,302,303,304"; // Comma-separated list ot task ids for project.
 
  15 // task_names[100] = "Coding";            // Task name.
 
  17 // Prepare an array of project ids for clients.
 
  18 var project_ids = new Array();
 
  19 {foreach $client_list as $client}
 
  20   project_ids[{$client.id}] = "{$client.projects}";
 
  22 // Prepare an array of project names.
 
  23 var project_names = new Array();
 
  24 {foreach $project_list as $project}
 
  25   project_names[{$project.id}] = "{$project.name|escape:'javascript'}";
 
  27 // We'll use this array to populate project dropdown when client is not selected.
 
  29 var projects = new Array();
 
  30 {foreach $project_list as $project}
 
  31   projects[idx] = new Array("{$project.id}", "{$project.name|escape:'javascript'}");
 
  35 // Prepare an array of task ids for projects.
 
  36 var task_ids = new Array();
 
  37 {foreach $project_list as $project}
 
  38   task_ids[{$project.id}] = "{$project.tasks}";
 
  40 // Prepare an array of task names.
 
  41 var task_names = new Array();
 
  42 {foreach $task_list as $task}
 
  43   task_names[{$task.id}] = "{$task.name|escape:'javascript'}";
 
  46 // Mandatory top options for project and task dropdowns.
 
  47 var empty_label_project = "{$i18n.dropdown.select|escape:'javascript'}";
 
  48 var empty_label_task = "{$i18n.dropdown.select|escape:'javascript'}";
 
  50 // The fillDropdowns function populates the "project" and "task" dropdown controls
 
  51 // with relevant values.
 
  52 function fillDropdowns() {
 
  53   if(document.body.contains(document.timeRecordForm.client))
 
  54     fillProjectDropdown(document.timeRecordForm.client.value);
 
  56   fillTaskDropdown(document.timeRecordForm.project.value);
 
  59 // The fillProjectDropdown function populates the project combo box with
 
  60 // projects associated with a selected client (client id is passed here as id).
 
  61 function fillProjectDropdown(id) {
 
  62   var str_ids = project_ids[id];
 
  63   var dropdown = document.getElementById("project");
 
  64   // Determine previously selected item.
 
  65   var selected_item = dropdown.options[dropdown.selectedIndex].value;
 
  67   // Remove existing content.
 
  69   var project_reset = true;
 
  70   // Add mandatory top option.
 
  71   dropdown.options[0] = new Option(empty_label_project, '', true);
 
  73   // Populate project dropdown.
 
  75     // If we are here, client is not selected.
 
  76     var len = projects.length;
 
  77     for (var i = 0; i < len; i++) {
 
  78       dropdown.options[i+1] = new Option(projects[i][1], projects[i][0]);
 
  79       if (dropdown.options[i+1].value == selected_item)  {
 
  80         dropdown.options[i+1].selected = true;
 
  81         project_reset = false;
 
  85     var ids = new Array();
 
  86     ids = str_ids.split(",");
 
  89     for (var i = 0; i < len; i++) {
 
  91       dropdown.options[i+1] = new Option(project_names[p_id], p_id);
 
  92       if (dropdown.options[i+1].value == selected_item)  {
 
  93         dropdown.options[i+1].selected = true;
 
  94         project_reset = false;
 
  99   // If project selection was reset - clear the tasks dropdown.
 
 101     dropdown = document.getElementById("task");
 
 103     dropdown.options[0] = new Option(empty_label_task, '', true);
 
 107 // The fillTaskDropdown function populates the task combo box with
 
 108 // tasks associated with a selected project (project id is passed here as id).
 
 109 function fillTaskDropdown(id) {
 
 110   var str_ids = task_ids[id];
 
 112   var dropdown = document.getElementById("task");
 
 113   if (dropdown == null) return; // Nothing to do.
 
 115   // Determine previously selected item.
 
 116   var selected_item = dropdown.options[dropdown.selectedIndex].value;
 
 118   // Remove existing content.
 
 120   // Add mandatory top option.
 
 121   dropdown.options[0] = new Option(empty_label_task, '', true);
 
 123   // Populate the dropdown from the task_names array.
 
 125     var ids = new Array();
 
 126     ids = str_ids.split(",");
 
 127     var len = ids.length;
 
 130     for (var i = 0; i < len; i++) {
 
 132       if (task_names[t_id]) {
 
 133         dropdown.options[idx] = new Option(task_names[t_id], t_id);
 
 138     // If a previously selected item is still in dropdown - select it.
 
 139     if (dropdown.options.length > 0) {
 
 140       for (var i = 0; i < dropdown.options.length; i++) {
 
 141         if (dropdown.options[i].value == selected_item) {
 
 142           dropdown.options[i].selected = true;
 
 147     // Select a task if user is required to do so and there is only one task available.
 
 148     if ({$user->task_required} && dropdown.options.length == 2) { // 2 because of mandatory top option.
 
 149       dropdown.options[1].selected = true;
 
 154 // The formDisable function disables some fields depending on what we have in other fields.
 
 155 function formDisable(formField) {
 
 156   var formFieldValue = eval("document.timeRecordForm." + formField + ".value");
 
 157   var formFieldName = eval("document.timeRecordForm." + formField + ".name");
 
 160   if (((formFieldValue != "") && (formFieldName == "start")) || ((formFieldValue != "") && (formFieldName == "finish"))) {
 
 161     x = eval("document.timeRecordForm.duration");
 
 164     x.style.background = "#e9e9e9";
 
 167   if (((formFieldValue == "") && (formFieldName == "start") && (document.timeRecordForm.finish.value == "")) || ((formFieldValue == "") && (formFieldName == "finish") && (document.timeRecordForm.start.value == ""))) {
 
 168     x = eval("document.timeRecordForm.duration");
 
 171     x.style.background = "white";
 
 174   if ((formFieldValue != "") && (formFieldName == "duration")) {
 
 175     x = eval("document.timeRecordForm.start");
 
 178     x.style.background = "#e9e9e9";
 
 179     x = eval("document.timeRecordForm.finish");
 
 182     x.style.background = "#e9e9e9";
 
 185   if ((formFieldValue == "") && (formFieldName == "duration")) {
 
 186     x = eval("document.timeRecordForm.start");
 
 188     x.style.background = "white";
 
 189     x = eval("document.timeRecordForm.finish");
 
 191     x.style.background = "white";
 
 195 // The setNow function fills a given field with current time.
 
 196 function setNow(formField) {
 
 197   var x = eval("document.timeRecordForm.start");
 
 199   x.style.background = "white";
 
 200   x = eval("document.timeRecordForm.finish");
 
 202   x.style.background = "white";
 
 203   var today = new Date();
 
 204   var time_format = '{$user->time_format}';
 
 205   var obj = eval("document.timeRecordForm." + formField);
 
 206   obj.value = today.strftime(time_format);
 
 207   formDisable(formField);
 
 210 function get_date() {
 
 211   var date = new Date();
 
 212   return date.strftime("%Y-%m-%d");
 
 215 function get_time() {
 
 216   var date = new Date();
 
 217   return date.strftime("%H:%M");