/*
 *+------------------------------------------------------------------------+
 *| 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.
 *|
 *+------------------------------------------------------------------------+
 */

function ServerRequest(sNamespaceId)
{
	this.m_namespaceId = sNamespaceId;
	this.m_oDispatcher = new CDispatcher();
	this.m_oParams = {};
	this.m_sGateway = "";
	this.m_sAction = "";
	this.m_fCallback = null;
	this.m_bPageRequest = false;
	this.m_request = null;
}

function ServerRequest_setUsePageRequest(bUsePageRequest)
{
	this.m_bPageRequest = bUsePageRequest;
};

function ServerRequest_getUsePageRequest()
{
	return this.m_bPageRequest;
};

function ServerRequest_getNamespace()
{
	return this.m_namespaceId;
}

function ServerRequest_cancel()
{
	if(this.m_request != null)
	{
		var requestToCancel = this.m_request;
		this.clearRequest();
		if(this.m_bPageRequest == false)
		{
			this.m_oDispatcher.cancelRequest(requestToCancel);
		}

		return true;
	}

	return false;
}

function ServerRequest_execute()
{
	if(this.m_request == null)
	{
		if(this.m_bPageRequest == true)
		{
			this.m_request = new Object();
			var formRequest = this.buildRequestForm();
			formRequest.submit();
		}
		else
		{
			this.m_oParams["cv.catchLogOnFault"] = "true";
			this.m_oParams["cv.responseFormat"] = "data";
			var aParams = new Array();
			for (var idxParam in this.m_oParams)
			{
				aParams = aParams.concat(idxParam + "=" + this.m_oParams[idxParam])
			}
			this.m_request = this.m_oDispatcher.createRequest(this.m_sGateway, aParams.join("&"), this);
			this.m_request.setResponseType("HTTP");
			this.m_oDispatcher.dispatchRequest(this.m_request);
		}

		return true;
	}

	return false;
}

function ServerRequest_buildRequestForm()
{
	var requestForm = document.createElement("form");

	requestForm.setAttribute("id", "requestForm");
	requestForm.setAttribute("name", "requestForm");
	requestForm.setAttribute("method", "post");
	requestForm.setAttribute("action", this.m_sGateway);
	requestForm.style.display = "none";

	document.body.appendChild(requestForm);

	for (var idxParam in this.m_oParams)
	{
		requestForm.appendChild(this.createHiddenFormField(idxParam, this.m_oParams[idxParam]));
	}

	return requestForm;
}

function ServerRequest_createHiddenFormField(name, value)
{
	var formField = document.createElement("input");
	formField.setAttribute("type", "hidden");
	formField.setAttribute("name", name);
	formField.setAttribute("id", name);
	formField.setAttribute("value", value);
	return(formField);
}

function ServerRequest_addArgument(sArgumentId, sArgument)
{
	this.m_oParams[sArgumentId] = sArgument;
}

function ServerRequest_setRequestURL(sGateway)
{
	this.m_sGateway = sGateway;
}

function ServerRequest_setCallback(fCallback)
{
	this.m_fCallback = fCallback;
}

function ServerRequest_setAction(sAction)
{
	this.m_sAction = sAction;
}

function ServerRequest_clearRequest()
{
	this.m_oParams = null;
	this.m_oParams = {};
	this.m_sGateway = "";
	this.m_sAction = "";
	this.m_fCallback = null;
	this.m_request = null;
}

function ServerRequest_generateRequestParams(oParams, envParams)
{
	if(typeof oParams != "undefined" && oParams != null)
	{
		oParams["cv.ignoreState"] = "true";
		oParams["b_action"] = "cognosViewer";
		oParams["cv.id"] = this.getNamespace();
	}
}

function ServerRequest_processResponse(response)
{
	if(this.m_request != null)
	{
		if (this.m_fCallback && typeof this.m_fCallback.processResponse == "function")
		{
			this.m_fCallback.processResponse(response);
		}
		else if (typeof this.m_fCallback == "function")
		{
			try
			{
				this.m_fCallback(response);
			}
			catch (e)
			{
			}
		}
		else if (typeof this.m_fCallback == "string")
		{
			try
			{
				eval(this.m_fCallback);
			}
			catch (e){}
		}

		this.clearRequest();
	}
}


ServerRequest.prototype.execute = ServerRequest_execute;
ServerRequest.prototype.buildRequestForm = ServerRequest_buildRequestForm;
ServerRequest.prototype.createHiddenFormField = ServerRequest_createHiddenFormField;
ServerRequest.prototype.setUsePageRequest = ServerRequest_setUsePageRequest;
ServerRequest.prototype.getUsePageRequest = ServerRequest_getUsePageRequest;
ServerRequest.prototype.addArgument = ServerRequest_addArgument;
ServerRequest.prototype.setRequestURL = ServerRequest_setRequestURL;
ServerRequest.prototype.setCallback = ServerRequest_setCallback;
ServerRequest.prototype.setAction = ServerRequest_setAction;
ServerRequest.prototype.clearRequest = ServerRequest_clearRequest;
ServerRequest.prototype.generateRequestParams = ServerRequest_generateRequestParams;
ServerRequest.prototype.processResponse = ServerRequest_processResponse;
ServerRequest.prototype.cancel = ServerRequest_cancel;
ServerRequest.prototype.getNamespace = ServerRequest_getNamespace;

