/***************
 ** Variables **
 ***************/

var cTactical = new aprsTACTICAL();

/**
 * toggleTACTICAL
 * 
 * Toggle tools functionality.
 * 
 */
function toggleTACTICAL() {
   
  if (!cTactical.isON()) {
    changeById('toolStatusOverlayMessage', 'Tactical Callsign Mode');
    showWindow('tacticalOverlay');
    showControl('controlTACTICAL', 'on');
    windowControl('tacticalOverlay');
    cTactical.ON();
    Drag.init(document.getElementById("tacticalHandle"), document.getElementById("tacticalOverlay"), null, null, null, null, true);
    changeById('miniTACTICAL', '');
  } // if  
  else {
    if (cTactical.isPAUSE())
      changeById('miniTACTICAL',
                 '<img src="/images/mini/off/tactical.png" width="22" height="22" border="0" alt="Tactical List is Paused" />');
    else
      changeById('miniTACTICAL',
                 '<img src="/images/mini/on/tactical.png" width="22" height="22" border="0" alt="Tactical List is Active" />');

    hideWindow('tacticalOverlay');
    showControl('controlTACTICAL', 'off');
    cTactical.OFF();    
  } // else
  
  return;
} // toggleTACTICAL

/**
 * toggleTACTICALPAUSE
 *
 * Toggle tools functionality.
 * 
 */
function toggleTACTICALPAUSE() {

  if (cTactical.Pause())
    showControl('controlTACTICALPAUSE', 'on');
  else
    showControl('controlTACTICALPAUSE', 'off');

  return;
} // toggleTACTICALPAUSE

function aprsTACTICAL() {
  this.on = false;
  this.loading = 0;
  this.pause = false;
} // aprsTACTICAL::Constructor

aprsTACTICAL.prototype.ON = function() {
  if (this.on)
    return;

  this.Update();

  this.on = true;
} // aprsTACTICAL::ON

aprsTACTICAL.prototype.Message = function(callsign) {
  if (!cMessage.isON())
    toggleMESSAGING();

  changeFieldById('aprsMESSAGE:Form:Target', callsign);
} // aprsTACTICAL::Message

aprsTACTICAL.prototype.Pause = function() {

  if (this.pause) {
    this.pause = false;
    this.Refresh();
  } // if
  else
    this.pause = true;

  return this.pause;
} // aprsTACTICAL::ON

aprsTACTICAL.prototype.isPAUSE = function() {
  return this.pause;
} // aprsTACTICAL::isPAUSE

aprsTACTICAL.prototype.isON = function() {
  return this.on;
} // aprsTACTICAL::isON

aprsTACTICAL.prototype.OFF = function() {
  if (!this.on)
    return false;

  this.on = false;
} // aprsTACTICAL::OFF

/**
 * aprsTACTICAL::Update
 *
 * Update the currently displayed map.
 *
 */
aprsTACTICAL.prototype.Update = function(){
  // Load any new stations
  this.Refresh();

  // set the timer
  window.setTimeout('cTactical.Update()', 120000);
} // aprsTACTICAL::Update

/** 
 * aprsTACTICAL::Add
 *
 * Load's a help article into the help window.
 *
 */
aprsTACTICAL.prototype.Add = function() {
  var request = createXMLHttpRequest();
  var me = this;

  if (!loggedIn) {
    changeById('aprsTACTICAL:Error', 'You must be logged in to use this feature.');
    return;
  } // if

  // reset error fields
  changeById('aprsTACTICAL:Error', '');

  /**
   * Default Variables
   *
   * Span through the possible field values
   * and set our variable list accordingly.
   *
   */
  var ids = {
    "c": "aprsTACTICAL:Form:Callsign",
    "t": "aprsTACTICAL:Form:Tactical"
  }

  var dfts = {
    "c": "",
    "t": ""
  }

  var var_list = createURI(document, false, ids, dfts);

  var queryString = "/ajax/tactical/add.php?&h="
                    + seed
                    + var_list;

  disableField('aprsTACTICAL:Form:Add');
  
  request.open("GET", queryString, true);
  request.onreadystatechange = function() {
    if (request.readyState == 4) {
      var xmlDoc = request.responseXML;
 

      var retData = new Array();
      xmlArray("/openaprs/reply", xmlDoc, retData);
      for (i=0; i < retData.length; i++) {
        if (retData[i]["done"] == "yes") {
          changeById('aprsTACTICAL:Error', retData[i]["response"]);
          ClearField('aprsTACTICAL:Form:Callsign');
          ClearField('aprsTACTICAL:Form:Tactical');
        } // if
        else {
          if (retData[i]["field"] != 'aprsTACTICAL:Error')
            changeById('aprsTACTICAL:Error', 'There was an error processing your request.');

          changeById(retData[i]["field"], retData[i]["response"]);
        } // else
      } // for

      enableField('aprsTACTICAL:Form:Add');
      me.Refresh();
    } // if
  
  } // function()
    
  request.send(null);
} // aprsTACTICAL::Add

/** 
 * aprsTACTICAL::Remove
 *
 * Load's a help article into the help window.
 *
 */
aprsTACTICAL.prototype.Remove = function(callsign) {
  var request = createXMLHttpRequest();
  var me = this;

  if (!loggedIn) {
    changeById('aprsTACTICAL:Error', 'You must be logged in to use this feature.');
    return;
  } // if

  // reset error fields
  changeById('aprsTACTICAL:Error', '');

  var queryString = "/ajax/tactical/remove.php?&h="
                    + seed
                    + "&c="
                    + encodeURIComponent(callsign);

  disableField('aprsTACTICAL:Form:Add');

  request.open("GET", queryString, true);
  request.onreadystatechange = function() {
    if (request.readyState == 4) {
      var xmlDoc = request.responseXML;
 

      var retData = new Array();
      xmlArray("/openaprs/reply", xmlDoc, retData);
      for (i=0; i < retData.length; i++) {
        if (retData[i]["done"] == "yes")
          changeById('aprsTACTICAL:Error', retData[i]["response"]);
        else {
          if (retData[i]["field"] != 'aprsTACTICAL:Error')
            changeById('aprsTACTICAL:Error', 'There was an error processing your request.');

          changeById(retData[i]["field"], retData[i]["response"]);
        } // else
      } // for

      enableField('aprsTACTICAL:Form:Add');
      me.Refresh();
    } // if
  
  } // function()
    
  request.send(null);
} // aprsTACTICAL::Remove

/**
 * aprsTACTICAL::Refresh
 *
 * Redraws the current list of stations.
 *
 */
aprsTACTICAL.prototype.Refresh = function() {
  var request = createXMLHttpRequest();
  var now = new Date();
  var me = this;

  // reset error messages
  //changeById('aprsTACTICAL:Error', '');

  if (!loggedIn) {
    clearRows('tacticalTable');
    changeById('aprsTACTICAL:Error', 'You must be logged in to use this feature.');
    return;
  } // if

  // pause check must be before loading or it will never
  // unpause.
  if (this.pause) {
    changeById('tacticalSTATUS', 'Paused');
    return;
  } // if

  if (this.loading)
    return;
  else
    this.loading = now.getTime();

  var queryString = "/ajax/tactical/view.php?h="
                    + seed;

  changeById('tacticalSTATUS', 'Updating');

  // ### PROFILING: START
  var start_date = new Date();

  request.open("GET", queryString, true);
  request.onreadystatechange = function() {

    if (request.readyState == 4) {
      var xmlDoc = request.responseXML;
      var numTactical = 0;

      var retData = new Array();
      xmlArray("/openaprs/callsigns/tactical", xmlDoc, retData);

      clearRows('tacticalTable');

      for (i=0; i < retData.length; i++) {  
        var chatArray = [];

        var aCell = new cellStruct;
        aCell.text = retData[i]["image"];
        chatArray.push(aCell);

        var aCell = new cellStruct;
        aCell.text = retData[i]["link"] + "<br />" + retData[i]["tactical"];
        chatArray.push(aCell);

        var aCell = new cellStruct;
        aCell.text = retData[i]["remove"];
        chatArray.push(aCell);

        addRow2('tacticalTable', chatArray.reverse());

      } // for

      var numTactical = retData.length;

      // ### PROFILING: END
      var end_date = new Date();
      var diff = parseFloat((end_date.getTime()/1000) - (start_date.getTime()/1000));

      logById('logContainer', 'logTable', 'aprsTACTICAL: UPDATED (xml:'+diff.toRound(3)+' seconds)');
      changeById("tacticalSTATS", numTactical +" tactical (" + diff.toRound(3) +" secs)");
      changeById('tacticalSTATUS', 'Idle');
      me.loading = 0;
    } // if
  } // function()

  request.send(null);
} // aprsTACTICAL::Refresh

