﻿var AVctrl = "AVL1_";
var Cmd_AVL = "";
var Historical = false;
var ZoomToID = "";
var RRint = 0;

//Start.
function StartAVL() {
   if (!Historical) {
      HideStart();
      ShowStop();
      var obj = $get(AVctrl + "Refresh_dropdown");
      var rate = parseInt(obj.value) * 1000;
      RRint = window.setInterval('SubmitAVL();', rate);
   }
   SubmitAVL();
}
//Stop.
function StopAVL() {
   ShowStart();
   HideStop();
   window.clearInterval(RRint);
   RRint = 0;
}
//Clear.
function ClearAVL() {
   StopAVL();
   Cmd_AVL = "Clear";
   SubmitAVL();
   Cmd_AVL = "";
}
//Restart.
function RestartAVL() {
   if (RRint != 0) {
      StopAVL();
      StartAVL();
   }
}
//Zoom to.
function ZoomToGPS(id) {
   ZoomToID = id;
   SubmitAVL();
}
//Set tracking mode.
function SetTrkMode() {
   var dt = $get("DateTimePanel");
   var lp = $get("LivePanel");
   var obj = $get(AVctrl + "TrkMode_dropdown");

   if (obj.value == "Historical") {
      Historical = true;
      StopAVL();
      dt.style.display = "block";
      lp.style.display = "none";
   } else {
      Historical = false;
      dt.style.display = "none";
      lp.style.display = "block";
   }
}
//Set override.
function SetOverride(id) {
   var obj = $get("OvPanel");
   obj.style.display = "block";

   obj = $get("OvVehicle");
   obj.innerHTML = id;

   obj = $get("Ov_checkbox");
   obj.checked = true;
   StopAVL();

   Cmd_AVL = "SetOverride";
   StartAVL();
   Cmd_AVL = "";
}
//Show start.
function ShowStart() {
   var obj = $get("Start_div");
   obj.style.display = "inline";
}
//Hide start.
function HideStart() {
   var obj = $get("Start_div");
   obj.style.display = "none";
}
//Show stop.
function ShowStop() {
   var obj = $get("Stop_div");
   obj.style.display = "inline";
   obj = $get(AVctrl + "Stop_link");
   obj.style.backgroundColor = "LightPink";
}
//Hide Stop.
function HideStop() {
   var obj = $get("Stop_div");
   obj.style.display = "none";
}
//Save names to process.
function setCheckedNames(inE) {
   inE = inE || window.event;
   var e = inE.srcElement || inE.target;

   if (e.tagName == "INPUT" && e.type == "checkbox") {
      var nl = $get(AVctrl + "NameList_Hid");
      var name = e.title + "|";

      //Remove existing name.
      if (nl.value.indexOf(name) != -1) {
         nl.value = nl.value.replace(name, "");
      }
      //Add new name
      if (e.checked) {
         nl.value += name;
      }
   }
}
//Load hidden fields.
function LoadHidGPS() {
   var obj, hid;

   hid = $get(AVctrl + "Postback_hid");
   hid.value = "";

   obj = $get("Ov_checkbox");
   hid.value += obj.checked + "|";

   obj = $get("OvVehicle");
   hid.value += obj.innerHTML + "|";

   obj = $get(AVctrl + "TrkMode_dropdown");
   hid.value += obj.value + "|";
   
   obj = $get("ShowIDs_checkbox");
   hid.value += obj.checked + "|";

   obj = $get("AutoPan_checkbox");
   hid.value += obj.checked + "|";

   obj = $get(AVctrl + "Symbol_dropdown");
   hid.value += obj.value + "|";

   obj = $get("AVL1_FromDate_I");
   if (obj) {
      hid.value += obj.value + "|";
   } else {
      hid.value += "|";
   }
    
   obj = $get("FromTime");
   hid.value += obj.value + "|";
  

   obj = $get("FromAmPm");
   hid.value += obj.value + "|";

   obj = $get("AVL1_ToDate_I");
   if (obj) {
      hid.value += obj.value + "|";
   } else {
      hid.value += "|";
   }

   obj = $get("ToTime");
   hid.value += obj.value + "|";

   obj = $get("ToAmPm");
   hid.value += obj.value + "|";

   hid.value += ZoomToID + "|";
   ZoomToID = "";

   obj = $get("Inactive_checkbox");
   hid.value += obj.checked + "|";

   hid.value += Cmd_AVL + "|";
}
//Send to server.
function SubmitAVL() {
   var nl = $get(AVctrl + "NameList_Hid");
   var ov = $get("Ov_checkbox");
   if (nl.value == "" && (!ov.checked)) {
      alert("Please select a vehicle");
      return;
   }
   LoadHidGPS();
   showBusy("AVL");
   var obj = $get(AVctrl + "Callback1");
   obj.click();
}
