//
//  AJAX functions
//
//  Clio Soleil (clio@PrettyGetter.TV)
//  PrettyGetter Productions (http://PrettyGetter.TV)
//  

//--------------------------------------------------------------------------------------------------------------------------------------------
// the main AJAX function called by JS scripts
function requestAJAX(url, callback, div, return_xml, method)
//--------------------------------------------------------------------------------------------------------------------------------------------
{
  // the majik variable
  var http_request = false;

  // MUST specify a URL to continue!
  if (!url) return false;
  // The callback function is optional, but if explicitly set to "", use default callback function (which just alerts the result)
  if (callback == "") callback = "alertAJAX";
  // The DIV used to display the url's results/output
  if (!div) div = ""; 
  // Assume using the GET method, but POST works too
  if (!method) method = "GET";
    
  // For Mozilla, Safari, etc....
  if (window.XMLHttpRequest)            
    http_request = new XMLHttpRequest();
  // for IE ...
  else if (window.ActiveXObject)
    http_request = new ActiveXObject("Microsoft.XMLHTTP");
  // and for all the rest
  else
  {
    alert("Your browser doesn't support this feature.  :(");
    return false;
  }
  
  // create a callback function for when the URL finally gets read.
  http_request.onreadystatechange = 
    function() 
    {
      // 4 = interactive + data loaded... the only state change we care about
      if (http_request.readyState == 4) 
      {
        // 200 = OK.  Also valid (but not okay) values are 300 (moved), 301 (moved), 404 (not found) , and 500 (sever error)
        if (http_request.status == 200) 
        {
          // if a JS callback function was given, execute it with the proper XML/text data
          if (callback)
          {
            // XML 
            if (return_xml) 
              eval(callback + '("' + div + '", http_request.responseXML)');
            // plain text
            else 
              eval(callback + '("' + div + '", http_request.responseText)');
          }
        } 
        else 
          // this should only be seen by developers and hackers
          alert('There was a problem with the request.(Code: ' + http_request.status + ')');
      }
    }
  // actually open the document
  http_request.open(method, url, true);
  http_request.send(null);
}

//////////////////////
// DEFAULT CALLBACKS
//////////////////////

//--------------------------------------------------------------------------------------------------------------------------------------------
// a simple callback for easy testing; doesn't do anything with the DIV's contents, just alerts the result
function alertAJAX(div, text)
//--------------------------------------------------------------------------------------------------------------------------------------------
{
  alert(div+"\n"+text);
}

//--------------------------------------------------------------------------------------------------------------------------------------------
// for scripts/urls/classes/ajax dlls/etc that return HTML and were given a DIV id, update the contents
function getDataCB(div, text)
//--------------------------------------------------------------------------------------------------------------------------------------------
{
  document.getElementById(div).innerHTML = text;
}

//--------------------------------------------------------------------------------------------------------------------------------------------
// for scripts/urls/classes/ajax dlls/etc that return the HTML needed for a total reload
function getDataReloadCB(div, text)
//--------------------------------------------------------------------------------------------------------------------------------------------
{
  e = _getElementsByName(div, "div");      // get a list of elements with the name of div
  if (e.length >= 1)                       // if we found an item
    e[0].innerHTML = text;                 // replace the first instance's content with the text
}

//--------------------------------------------------------------------------------------------------------------------------------------------
// a more complicated function; gets passed an array of DIVs - sets the first one's contents to the text, and 
// clears the ! not saved icon for the second DIV element in the array.  
// (note that JS is not a strongly typed language so you can specify an array instead of a string) 
function setDataCB(divs, text)
//--------------------------------------------------------------------------------------------------------------------------------------------
{
  div = divs.split(" ");
  getDataCB(div[0], text);
  updateDataCB(div[1] , text);
}

//--------------------------------------------------------------------------------------------------------------------------------------------
// for GUI objects; clears the ! (not saved) icon
function updateDataCB(div, text)
//--------------------------------------------------------------------------------------------------------------------------------------------
{
  if (text == "OK")
    setWarning(div, false);
  
  // clear the saved indicator after 5 seconds
  setTimeout("document.getElementById('"+div+"').innerHTML = ''", 5000);
}

//--------------------------------------------------------------------------------------------------------------------------------------------
// replaces the DIV's contents with a saved/unsaved icon 
function setWarning(div, warn)
//--------------------------------------------------------------------------------------------------------------------------------------------
{
  var y = document.getElementById(div);
  if (warn)
    // indicate the data has not yet been saved
    y.innerHTML = "<img src='/_pgtv/_img/icons/warning-tiny.png' alt='!'>";
  else
    // saved
    y.innerHTML = "<img src='/_pgtv/_img/icons/ok-tiny.png' alt=' '>";
}

