// Helper functions for FST webserver
//
// 2004-01-10 OWES

// ReLoad simply reloads the page, after stripping any CI commands
//
function ReLoad() {
  var page;

  page = MyStrip();
  window.location.href = page;
}

// CI sends a simple command to the web server
// Parameters:
//   CIString
function CI(CIString) {
  var found;
  var page;

  page = MyStrip();
  // Now add modify command to URL and reload
  window.location.href = page + "?ci:" + CIString;
}

// CISend sends a modify command to the web server
// Parameters:
//   Operand: for example MW0
//   Value
function CISend(Operand,Value) {
  var found;
  var page;

  // Verify variable value
  if ( CheckValue(Value) == false )
  {
    return;
  }

  page = MyStrip();
  // Now add modify command to URL and reload
  window.location.href = page + "?ci:" + "M" + Operand + "=" + Value;
}

// CISend2 sends 2 modify command to the web server
// Parameters:
//   Operand1: for example MW0
//   Value1
//   Operand2: for example MW1
//   Value2
function CISend2(Operand1,Value1,Operand2,Value2) {
  var found;
  var page;

  // Verify variable value1
  if ( CheckValue(Value1) == false )
  {
    return;
  }
  // Verify variable value2
  if ( CheckValue(Value2) == false )
  {
    return;
  }

  page = MyStrip();
  // Now add modify command to URL and reload
  window.location.href = page + "?ci:" + "M" + Operand1 + "=" + Value1  + ";M" + Operand2 + "=" + Value2;
}

// CISendStr sends a modify string command to the web server
// Parameters:
//   Operand: for example 3
//   Value
function CISendStr(Operand,Value) {
  var found;
  var page;

  page = MyStrip();
  // Now add modify command to URL and reload
  window.location.href = page + "?ci:" + "!3M" + Operand + "=" + Value;
}

// CISendStr sends a modify string command to the web server
// Parameters:
//   Operand: for example 3
//   Value
function CISetAndSendStr(Operand,Value,StringNumber,StringText) {
  var found;
  var page;

  // Verify variable value
  if ( CheckValue(Value) == false )
  {
    return;
  }

  page = MyStrip();
  // Now add modify command to URL and reload
  window.location.href = page + "?ci:" + "M" + Operand + "=" + Value + ";!3M" + StringNumber + "=" + Stringtext;
}

function MyStrip() {
  var found;
  var href;

  // Strip previous modify command from URL
  href = this.location.href;
  found = href.indexOf("?");
  if ( found > 0 ) {
    href = href.substring(0, found);
  }
  return href;
}

function CheckValue(Value)
{
  if ( isNaN(Value) == true ) 
  {
    alert("Value (" + Value + ") is not a number");
    return false;
  }

  // Verify variable value
  if ( (Value < -65535) || (Value > 65535) ) 
  {
    alert("Allowed range for value is -65535 to 65535");
    return false;
  }
  return true;
}

// Returns a text string containing the current date in the format 2004-01-10  (ISO standard)
function stringDate() 
{
  date = new Date();
  with (date)
  {
    y = getFullYear();
    m = getMonth() + 1;
    d = getDate();
  }

  m = (m < 10) ? "0" + m : m;
  d = (d < 10) ? "0" + d : d;
  text = y + "-" + m + "-" + d;

  return(text);
}    

// Returns a text string containing the current time in the format 09:06:45
function stringTime() 
{
  time = new Date();
  with (date)
  {
    h = getHours();
    m = getMinutes();
    s = getSeconds();
  }

  h = (h < 10) ? "0" + h : h;
  m = (m < 10) ? "0" + m : m;
  s = (s < 10) ? "0" + s : s;

  text = h + ":" + m + ":" + s;

  return(text);
}