/*
 *+------------------------------------------------------------------------+
 *| IBM Confidential
 *| OCO Source Materials
 *| BI and PM: viewer
 *| (C) Copyright IBM Corp. 2001, 2009
 *|
 *| US Government Users Restricted Rights - Use, duplication or
 *| disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 *|
 *+------------------------------------------------------------------------+
 */

/*******************************************
		Common Utility Functions
*******************************************/
var GUtil = {};

GUtil.createHiddenForm = function(name, method, viewerId, target) {
	var form = document.getElementById(name);
	if(form) {
		document.body.removeChild(form);
	}

	form = document.createElement("form");
	form.id = name;
	form.name = name;
	form.method = method;
	form.style.display = "none";
	form.action = document.forms["formWarpRequest" + viewerId].action;
	form.target = target + (new Date()).getTime();

	document.body.appendChild(form);
	return form;
};


GUtil.createFormField = function(el, name, value) {
	var input = document.createElement("input");
	input.type = "hidden";
	input.name = name;
	input.value = value;

	el.appendChild(input);
};


/*----------------------------------------------------------
GenerateCallback:
	func:		Function object to use. To call foo.test() pass in foo.test.
	aParams:	Array of parameters to pass to func.
	oContext:	Calling context. If "foo" in foo.test is and instance of "Foo" object, then you need
				to pass the instance as a context. This is this difference between calling Foo.test() and
				foo.test(), where foo = new Foo();

	Returns:	A function object that can be used as a callback, but allows for the passing of stored parameters.

	Example:	var foo = new Foo();
				var func1 = GUtil.generateCallback(alert, ["Hello World"]);
				var func2 = GUtil.generateCallback(window.alert, ["Hi there"], window);

				//alert "Hello World" after 5 seconds.
				setTimeout(func1, 5000);

				//alert "Hi there" after 10 seconds.
				setTimeout(func2, 10000);
------------------------------------------------------------*/
GUtil.generateCallback = function(func, aParams, oContext){
	//normally "this" is probably the window object, but
	//we can pass in a different context object if we need to.
	if (func) {
		var funcContext = oContext || this;
		aParams = (aParams instanceof Array)? aParams : [];
		return (function(){
			try {
				return func.apply(funcContext,aParams);
			} catch ( except ) {
				alert(except.message);
			}
		});
	} else {
		//do nothing
		return (function() {});
	}
};




//TODO: This is a new function call added (perhaps temporarily) to support things like:
//		1. Synchronous XmlHttp calls
//		2. Modified callbacks.
// This should eventually all go through: CCognosViewerRequest


function XmlHttpObject() {
	this.xmlHttp = XmlHttpObject.createRequestObject();
	//call the wait-for-response in the context of this instance
};


XmlHttpObject.prototype.setCallbacks = function(scb, ecb) {
	this.successCallback = scb || null;
	this.errorCallback = ecb || null;
};


XmlHttpObject.createRequestObject = function() {
	var request = null;

	if (window.XMLHttpRequest)		{
        request = new XMLHttpRequest();						// Firefox, Safari, ...
	} else if (window.ActiveXObject)	{
       request = new ActiveXObject("Msxml2.XMLHTTP");		// Internet Explorer
	} else {
		//throw error. exit.
	}
	return request;
};


XmlHttpObject.prototype.waitForXmlHttpResponse = function() {
	//explicit so there's no confusion
	var request = this.xmlHttp;
	if (request.readyState === 4) {
		if(request.status === 200 && this.successCallback) {
			this.successCallback();
			this.successCallback = null;
		} else if (request.errorCallback) {
			this.errorCallback();
			this.errorCallback = null;
		}
	} else {
		//wait...
	}
};


XmlHttpObject.prototype.sendHtmlRequest = function(action, url, params, async) {
	var request = this.xmlHttp;
	request.open(action, url, async);

	//should be after the "open" for best results...
	if (async) {
		request.onreadystatechange = GUtil.generateCallback(this.waitForXmlHttpResponse, [], this);
	}

	request.setRequestHeader("Content-Type", "text/html");
	request.send(params);
};


XmlHttpObject.prototype.getResponseXml = function() {
	return (this.xmlHttp.status === 200) ? this.xmlHttp.responseXML : null;
};

XmlHttpObject.prototype.getResponseText = function() {
	return (this.xmlHttp.status === 200) ? this.xmlHttp.responseText : "";
};

XmlHttpObject.prototype.getStatus = function() {
	return this.xmlHttp.status;
};


