///////////////////////////////////////////////////////
// Javascript for personalized actions in website
///////////////////////////////////////////////////////

// public variable for the last object set
var change = 0;

// set the focus on the first item for each page
function setfirst()
{
  // go to input box depending on what screen is showing
  if ($('user')) { $('user').focus(); }
  else if ($('full_name')) { $('full_name').focus(); }
  else if ($('search')) { $('search').focus(); }
  else if ($('oldpass')) { $('oldpass').focus(); }
  else if ($('email')) { $('email').focus(); }
}

// read a file from the server
function setarrived(value)
{
  // only continue if xmlHttp isn't void
  if (xmlHttp)
  {
    // try to connect to the server
    try
    {
      // work out the ajax send-string
      var hold = "ajax/set.php?arrived=" + value;
      if ($('L' + value).value != '') { hold += "&license=" + $('L' + value).value.toUpperCase(); }
      if ($('U' + value).value != '') { hold += "&registration=" + $('U' + value).value.toUpperCase(); }
      // initiate reading a file from the server
      xmlHttp.open("GET", hold, true);
      xmlHttp.onreadystatechange = handlearrived;
      xmlHttp.send(null);
      // and set the last object
      change = value;
    }
    // display the error in case of failure
    catch (e)
    {
      alert("Can't connect to server:\n" + e.toString());
    }
  }
  // return false
  return false;
}

// function called when the state of the HTTP request changes
function handlearrived()
{
  // when readyState is 4, we are ready to read the server response
  if (xmlHttp.readyState == 4)
  {
    // continue only if HTTP status is "OK"
    if (xmlHttp.status == 200)
    {
      try
      {
        // read the message from the server
        var httpResponse = xmlHttp.responseText;
        // put into the td
        $('A' + change).innerHTML = httpResponse;
        // build up a "departed" a-reference
        var dep = "<a href=\"#\" onclick=\"setdeparted(" + change + "); return false;\">Now</a>";
        $('D' + change).innerHTML = dep;
        // change the row background to show that it's in the yard
        $('R' + change).className = 'showme inyard';
        // change the license and registration fields
        $('T' + change).innerHTML = $('L' + change).value.toUpperCase();
        $('V' + change).innerHTML = $('U' + change).value.toUpperCase();
      }
      catch(e)
      {
        // display error message
        alert("Error reading the response: " + e.toString());
      }
    }
    else
    {
      // display status message
      alert("There was a problem retrieving the data:\n" + xmlHttp.statusText);
    }
  }
}

// read a file from the server
function setdeparted(value)
{
  // only continue if xmlHttp isn't void
  if (xmlHttp)
  {
    // try to connect to the server
    try
    {
      // initiate reading a file from the server
      xmlHttp.open("GET", "ajax/set.php?departed=" + value, true);
      xmlHttp.onreadystatechange = handledeparted;
      xmlHttp.send(null);
      // and set the last object
      change = value;
    }
    // display the error in case of failure
    catch (e)
    {
      alert("Can't connect to server:\n" + e.toString());
    }
  }
  // return false
  return false;
}

// function called when the state of the HTTP request changes
function handledeparted()
{
  // when readyState is 4, we are ready to read the server response
  if (xmlHttp.readyState == 4)
  {
    // continue only if HTTP status is "OK"
    if (xmlHttp.status == 200)
    {
      try
      {
        // read the message from the server
        var httpResponse = xmlHttp.responseText;
        // put into the td
        $('D' + change).innerHTML = httpResponse;
        // change the row background to show that it's in the yard
        $('R' + change).className = 'showme outyard';
      }
      catch(e)
      {
        // display error message
        alert("Error reading the response: " + e.toString());
      }
    }
    else
    {
      // display status message
      alert("There was a problem retrieving the data:\n" + xmlHttp.statusText);
    }
  }
}

// read a file from the server
function setaflip(value)
{
  // only continue if xmlHttp isn't void
  if (xmlHttp)
  {
    // try to connect to the server
    try
    {
      // set the last object
      change = value;
      // initiate reading a file from the server
      xmlHttp.open("GET", "ajax/aflip.php?info=" + value, true);
      xmlHttp.onreadystatechange = handleaflip;
      xmlHttp.send(null);
    }
    // display the error in case of failure
    catch (e)
    {
      alert("Can't connect to server:\n" + e.toString());
    }
  }
  // return false
  return false;
}

// function called when the state of the HTTP request changes
function handleaflip()
{
  // when readyState is 4, we are ready to read the server response
  if (xmlHttp.readyState == 4)
  {
    // continue only if HTTP status is "OK"
    if (xmlHttp.status == 200)
    {
      try
      {
        // read the message from the server
        var httpResponse = xmlHttp.responseText;
        // display the internals
        $('a' + change).innerHTML = httpResponse;
      }
      catch(e)
      {
        // display error message
        alert("Error reading the response: " + e.toString());
      }
    }
    else
    {
      // display status message
      alert("There was a problem retrieving the data:\n" + xmlHttp.statusText);
    }
  }
}

// read a file from the server
function setdflip(value)
{
  // only continue if xmlHttp isn't void
  if (xmlHttp)
  {
    // try to connect to the server
    try
    {
      // set the last object
      change = value;
      // initiate reading a file from the server
      xmlHttp.open("GET", "ajax/dflip.php?info=" + value, true);
      xmlHttp.onreadystatechange = handledflip;
      xmlHttp.send(null);
    }
    // display the error in case of failure
    catch (e)
    {
      alert("Can't connect to server:\n" + e.toString());
    }
  }
  // return false
  return false;
}

// function called when the state of the HTTP request changes
function handledflip()
{
  // when readyState is 4, we are ready to read the server response
  if (xmlHttp.readyState == 4)
  {
    // continue only if HTTP status is "OK"
    if (xmlHttp.status == 200)
    {
      try
      {
        // read the message from the server
        var httpResponse = xmlHttp.responseText;
        // display the internals
        $('d' + change).innerHTML = httpResponse;
      }
      catch(e)
      {
        // display error message
        alert("Error reading the response: " + e.toString());
      }
    }
    else
    {
      // display status message
      alert("There was a problem retrieving the data:\n" + xmlHttp.statusText);
    }
  }
}


