/*********************************************************************/
/*                                                                   */
/* IBM Confidential                                                  */
/*                                                                   */
/* OCO Source Materials                                              */
/*                                                                   */
/* Copyright IBM Corp. 2005, 2007                                    */
/*                                                                   */
/* The source code for this program is not published or otherwise    */
/* divested of its trade secrets, irrespective of what has been      */
/* deposited with the U.S. Copyright Office.                         */
/*                                                                   */
/*********************************************************************/

//-------------------------------------------------------------
//  Quickplace Ajax library
//-------------------------------------------------------------

// Quickplace "AJAX" Object
function QPAjax()
{
	this.Request = QPAjax_Request;
	this.RequestJS = QPAjax_RequestJS;
	this.RequestXML = QPAjax_RequestXML;
	this.SubmitForm = QPAjax_SubmitForm;
	this.Error = QPAjax_Error;	  // Default error handler if none specified
}


// Make an XMLHTTP request;
//
// Required args:
//  url: the request URL
//  sFunc: "success" callback function with returned data as argument.
//
// Optional args:
//  meth: "get" or "post" ("get" is dojo.io.bind's default).
//  eFunc: "error" callback function with error code as argument.
//  type: The MIME type determining format of returned data.
//        Default is "text/plain" (response passed back "as is", as raw text).
//
// For info on dojo.io.bind, see http://dojo.jot.com/Tutorials/HelloWorld
//
function QPAjax_Request(url, sFunc, meth, eFunc, type)
{
	var theMethod = meth || "get";
	var theErrFunc = eFunc || this.Error;
	var theType = type || "text/plain";

	try {
		dojo.io.bind({
			url: url,
			method: theMethod,
			load: function(type, data, evt){sFunc(data)},
			error: function(type, error){theErrFunc(error)},
			mimetype: theType,
			transport: "XMLHTTPTransport"
		});
	}
	catch(e) {
		this.Error(e);
	}
}


// Request data assumed to be javascript code,
// try to evaluate and run the returned javascript.
//
// Example:
// --------
// demo.js on the server contains:
//
// function helloWorld() {
//     alert( "Hello world!\n" +
//         "I'm some javascript." );
//     return "I have returned";
// }
// helloWorld();
//
// Your client-side code:
//
// function showReturnValue( type, evaldObj ) {
//     alert('The script returned ' + evaldObj );
// }
//
// ajax = new QPAjax();
// ajax.RequestJS((demoLocation + 'demo.js'), showReturnValue);
//
function QPAjax_RequestJS(url, sFunc, meth, eFunc)
{
	QPAjax_Request(url, sFunc, meth, eFunc, "text/javascript");
}


// Request XML data,
// try to create an XML document from the returned XML.
// (This data can then be parsed using the JS DOM APIs).
function QPAjax_RequestXML(url, sFunc, meth, eFunc)
{
	QPAjax_Request(url, sFunc, meth, eFunc, "text/xml");
}


// Submit a Form asynchronously
//
// No URL required, because the form's action property is used.
//
// Required args:
//  formId: the id of the <form> tag
//  sFunc: "success" callback function with returned data as argument.
//
// Optional args:
//  eFunc: "error" callback function with error code as argument.
//
function QPAjax_SubmitForm(formId, sFunc, eFunc)
{
	var theErrFunc = eFunc || this.Error;

	try {
		dojo.io.bind({
			load: function(type, data, evt){sFunc(data)},
			error: function(type, error){theErrFunc(error)},
			formNode: document.getElementById(formId),
			mimetype: "text/plain"
		});
	}
	catch(e) {
		this.Error(e);
	}
}

// FIX ME: report error
function QPAjax_Error(error)
{
	alert("QPAjax error!\n" + error.message);
}

