/******************************************************************************
 *  
 *  Copyright 2008-2009 David D. Emory 
 *  [additional contributors append names above]
 *
 *  This file is part of Five Points. See <http://www.fpdev.org> for
 *  additional project information and documentation.
 *  
 *  Five Points is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  Five Points is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 *  
 ******************************************************************************/  

/**
 *  fp_ajax.js
 *
 *  A collection of utility functions for common ajax operations.
 */

// FUNCTION DEFINITIONS

/**
 *  fp_ajaxLoadAsText(..)
 *
 *  A simple utility function for loading a text-based file, indicated by a
 *  URL, passing the text content (if read successfully) to a specified
 *  function. 
 *  
 *  -- parameters --
 *  url (String): the url to be read
 *  fn (Function): reference to the function to be called if successful
 * 
 */
function fp_ajaxLoadAsText(url, fn) {
  var req = false;
  if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (req != undefined) {
    req.onreadystatechange = function() {
      fp_ajaxLoadAsTextResponse(req, fn);
    };
    req.open("GET", url, true);
    req.send("");
  }
}  

/**
 * fp_ajaxLoadAsTextResponse(..)
 * 
 * The function called by fp_ajaxLoadAsText() if/when a response is received.
 * 
 * -- parameters --
 * req (XMLHttpRequest or ActiveXObject): the browser-specific http request object
 * fn (Function): the function to be called with the successfully read content
 */
function fp_ajaxLoadAsTextResponse(req, fn) {
  if (req.readyState == 4) { // only if req is "loaded"
    if (req.status == 200) { // only if "OK"
      fn(req.responseText);
    } else {
      fn("Error:\n"+ req.status + "\n" +req.statusText);
    }
  }
}

/**
 *  fp_ajaxLoadAsXML(..)
 *
 *  Similar to fp_ajaxLoadAsText(), only the repsonse is expected as XML, and
 *  fields may be passed.
 *  
 *  -- parameters --
 *  url (String): the url to be read
 *  fn (Function): reference to the function to be called if successful
 *  fields (String): the fields to send with the http request
 */
function fp_ajaxLoadAsXML(url, fn, fields) {
  var req = false;
  if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (req != undefined) {
    req.onreadystatechange = function() {
      fp_ajaxLoadAsXMLResponse(req, fn);
    };
    req.open("POST", url, true);
    req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    req.send(fields);
  }
}  

/**
 * fp_ajaxLoadAsXMLResponse(..)
 * 
 * The function called by fp_ajaxLoadAsXML() if/when a response is received.
 * 
 * -- parameters --
 * req (XMLHttpRequest or ActiveXObject): the browser-specific http request object
 * fn (Function): the function to be called with the successfully read content
 */
function fp_ajaxLoadAsXMLResponse(req, fn) {

  if (req.readyState == 4) {
    if (req.status == 200) {
      fn(req.responseXML);
    } else {
      alert("Error:\n"+ req.status + "\n" +req.statusText);
    }
  }
}
