/*********************************************************************/
/*                                                                   */
/* IBM Confidential                                                  */
/*                                                                   */
/* OCO Source Materials                                              */
/*                                                                   */
/* Copyright IBM Corp. 2005, 2010                                    */
/*                                                                   */
/* 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".
//  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).
//
// 
//
function QPAjax_Request(url, sFunc, meth, eFunc, type)
{
	var theMethod = meth || "get";
	var theErrFunc = eFunc || this.Error;
	var theType=returnSubType(type);

	try {
		if (theMethod=="get") {
			dojo.xhrGet ({
				url: url,
				handleAs: (typeof(theType)=="undefined" ? "text" : theType),
				load: function(data){sFunc(data)},
				error: function(error){theErrFunc(error)}
			});
		}
		else {
			dojo.xhrPost ({
				url: url,
				handleAs: (typeof(theType)=="undefined" ? "text" : theType),
				load: function(data){sFunc(data)},
				error: function(error){theErrFunc(error)}
			});
			}
	}
	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, "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, "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.xhrPost ({
			form: formId,
			load: function(data){sFunc(data)},
			error: function(error){theErrFunc(error)}
		});
	}
	catch(e) {
		this.Error(e);
	}
}

// FIX ME: report error
function QPAjax_Error(error)
{
	alert(QuickrLocaleUtil.getStringResource("AJAX.ERROR") + error.message);
}

// Returns the subtype 
// 
//Dojo.xhr requires handleAs to be the subtype only (ex: "text", "xml")
function returnSubType(mType){
	
	if (mType == "text/plain") 
		return "text";
	
	else if (typeof(mType) == "undefined") 
		return;
		
	else 
		return (mType.substring(mType.indexOf("/") + 1, mType.length));

}

