/*
  A simple java script library for networked data retrieval
  based directly on code in "Ajax in Action" pp. 195 ff.

  copyright (c) 2005 Neel Smith

   This program 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 2
   of the License, or (at your option) any later version.
   
   This program 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 this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

  Neel Smith, College of the Holy Cross, nsmith@holycross.edu
*/



/**
 * Generic object for managing network data retrieval objects
 */ 
var net=new Object();


// "Constants" for values of XMLHttpRequest status:
/**
 * "Static" value for XMLHttpRequest = uninitialized
 */
net.READY_STATE_UNINITIALIZED=0;
/**
 * "Static" value for XMLHttpRequest = loading in progress
 */
net.READY_STATE_LOADING=1;
net.READY_STATE_LOADED=2;
net.READY_STATE_INTERACTIVE=3;
/**
 * "Static" value for XMLHttpRequest = fully ready
 */
net.READY_STATE_COMPLETE=4;

/**
 * Constructor of structure for submitting and managing XMLHttpRequests.
 * Registers call-back functions, then initiates request with call to <code>loadXMLDoc</code>.
 * @param url A string with the URL to submit (required).
 * @param loadHandler Function that will be registered to handle changes in request status (required).
 * @param loadParams String with any extra parameters to pass along to <code>loadHandler</code> (optional, default = none)
 * @param errorHandler Function that will be registered to handle errors (optional, default = <code>net.urlLoader.defaultError</code>).
 * @param method String with name of HTTP submission method (optional, default = "GET").
 * @param params HTTP request parameters to include (optional, default = none).
 * @param contentType MIME type of request (optional, default = none).
 */
net.urlLoader=function(url,loadHandler,loadParams,errorHandler,method,params,contentType) {
    this.url = url;
    this.req = null;

    this.loadHandler = loadHandler;
    if (errorHandler) {
	this.errorHandler = errorHandler;
    } else {
	this.errorHander =  net.urlLoader.defaultError;
    }

    this.loadXMLDoc(this.url,loadParams,method,params,contentType);
} //urlLoader



/**
 * Submits XMLHttpRequest using call-back functions registered with parent 
 * <code>net.urlLoader</code>
 * @param url A string with the URL to submit (required).
 * @param loadParams String with any extra parameters to pass along to <code>loadHandler</code> (optional, default = none)
 * @param errorHandler Function that will be registered to handle errors (optional, default = <code>net.urlLoader.defaultError</code>).
 * @param method String with name of HTTP submission method (optional, default = "GET").
 * @param params HTTP request parameters to include (optional, default = none).
 * @param contentType MIME type of request (optional, default = ).
 */
net.urlLoader.prototype.loadXMLDoc=function(url,loadparams,method,params,contentType) {
    if (!method) {
	method = "GET";
    }
    if (!contentType && method=="POST") {
	contentType="application/x-www-form-urlencoded";
    }
    this.req=new XMLHttpRequest();
    if (this.req) {
	try {
	    var loader = this;
	    this.req.onreadystatechange=function() {
		loader.onReadyState.call(loader,loadparams);
	    }

	    this.req.open(method,url,true);
	    if (contentType) {
		this.req.setRequestHeader('Content-Type',contentType);
	    }
	    this.req.send(params);
	} catch (e) {
	    //	    this.errorHandler.call(this);
	    alert("Error " + e);
	    throw(e);
	}
    }
} // loadXMLDoc


/**
 * Calls registered call-back function when readyState = complete.
 * @param extras String wtih any additional parameters to 
 * submit to the call-back function.
 */
net.urlLoader.prototype.onReadyState=function(extras) {
    var req= this.req;
    var ready = req.readyState;

    if (ready == net.READY_STATE_COMPLETE) {
	var httpStatus = req.status;
	//	if (httpStatus==200 || httpStatus==0) {
	    this.loadHandler.call(this,extras);
	    //	} else {
	    //	    this.errorHandler.call(this,extras);
	    //	}
    }
} // onReadyState


/**
 * Throws alert if the XMLHttpRequest generated an error.
 */
net.urlLoader.prototype.defaultError=function(extras) {
    alert("ERROR LOADING URL:" + extras);
} //defaultError
