/*
 *+------------------------------------------------------------------------+
 *| Licensed Materials - Property of IBM
 *| BI and PM: prmt
 *| (C) Copyright IBM Corp. 2002, 2013
 *|
 *| US Government Users Restricted Rights - Use, duplication or
 *| disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 *|
 *+------------------------------------------------------------------------+
*/


/*
	CSelectDataSourceSignon.js

	This script is used to allow users to select a connection and signon for a datasource
*/

//Constructor to create a select datasource signon component

//Constructor to create a checkboxlist component
//	oDataSourceConnection: the form control used to for the data source connection value
//	oDataSourceSignon: the form control used for the data source signon value
//	oUsername: the form control used for the user name
//	oPassword: the form control used for the password
//	oSubmit: the form control to submit selections to the server
//	oUseSignonDefault: form control used to set whether choosing signons should be the default
//	sSubmitType: 'default' will submit as a standard form
//				'XML' will convert the submission to XML and submit
//	bSignonForCube: set to true if this signon is for a password protected cube (credentials are differents in this case). Default = false
//  bOKButtonPromptTypeIsNext this is used to determine whether this should do a next or a finish to conform with the onClick of the OKButton of the defaultpagefooter
function CSelectDataSourceSignon(oDataSourceConnection, oDataSourceSignon, oUsername, oPassword, oSubmit, oUseSignonDefault, sSubmitType, oRadioList, oImgTest, bSignonForCube, sCVId, sPromptId, bOKButtonPromptTypeIsNext)
{
	this.setCVId(sCVId);
	this.m_oDataSourceConnection = oDataSourceConnection;
	this.m_oDataSourceSignon = oDataSourceSignon;
	this.m_oUsername = oUsername;
	this.m_oPassword = oPassword;
	this.m_oSubmit = oSubmit;
	this.m_bValid = false;
	this.m_sSubmitType = sSubmitType;
	this.m_bUseSignon = true;
	this.m_bOKButtonPromptTypeIsNext = (bOKButtonPromptTypeIsNext === true);
	this.m_bSignonForCube = (bSignonForCube === true);
	if (typeof oRadioList != K_PRMT_sUNDEFINED) {
		this.m_oRadioList = oRadioList;
	}
	else {
		this.m_oConnectionRadioList = null;
	}

	if (typeof oImgTest != K_PRMT_sUNDEFINED) {
		this.m_oImgTest = oImgTest;
	}
	else {
		this.m_oImgTest = null;
	}
	this.m_sPromptId = sPromptId;

	//skin folder
	this.m_sSkin = (typeof getPromptSkin != K_PRMT_sUNDEFINED ? getPromptSkin() : K_PRMT_sDEFAULTSKIN);

	if (this.m_oImgTest != null)
	{
		this.m_oImgErrorFalse= new Image();
		this.m_oImgErrorFalse.src = this.m_sSkin + "/prompting/images/error_timed_small_off.gif";
		this.m_oImgErrorTrue = new Image();
		this.m_oImgErrorTrue.src = this.m_sSkin + "/prompting/images/error_timed_small.gif";
	}

	//use a User Id and Password or a Signon
	//default use Signon = 'true';
	if (oUseSignonDefault)
	{
		if (oUseSignonDefault.value == 'false')
		{
			this.m_bUseSignon = false;
		}
	}

	// Event handlers
	PRMTUtils.f_addEvent( this.m_oUsername, "keypress", this.keyPress.bind(this) );
	PRMTUtils.f_addEvent( this.m_oPassword, "keypress", this.keyPress.bind(this) );

	this.checkData();
}

CSelectDataSourceSignon.prototype = new CPromptControl();

//render the validated state
function CSelectDataSourceSignon_checkPass()
{
	if ((this.m_oImgTest != null) && (this.m_oImgTest.src != this.m_oImgErrorFalse.src))
	{
		this.m_oImgTest.src = this.m_oImgErrorFalse.src;
	}

	this.notify(gPASS, this);
}

//render the validation error state
function CSelectDataSourceSignon_checkFail()
{
	if (this.m_oImgTest != null)
	{
		this.m_oImgTest.src = this.m_oImgErrorTrue.src + '?' + Math.random();
	}

	this.notify(gFAIL, this);
}

//validate the input into the control
function CSelectDataSourceSignon_checkData()
{
	this.m_bValid = true;
	if (this.m_oRadioList != null)
	{
		this.m_bValid = false;
		if (this.m_oRadioList.checked == true) {
			this.m_bValid = true;
		}
		else
		{
			for (var i = 0; i < this.m_oRadioList.length; i++)
			{
				if (this.m_oRadioList[i].checked) {
					this.m_bValid = true;
				}
			}
		}
	}

	if (this.m_bValid) {
		this.checkPass();
	}
	else {
		this.checkFail();
	}

	this.notify((this.m_bValid ? gPASS : gFAIL), this);

	return this.m_bValid;
}

function CSelectDataSourceSignon_preProcess()
{
	if (this.m_sSubmitType == K_PRMT_sXML && this.m_oSubmit)
	{
		this.m_oSubmit.value = '<selectChoices><selectOption useValue="' + sXmlEncode(this.sGetValue()) + '" selected="true"/></selectChoices>';
	}
}

function CSelectDataSourceSignon_sGetPersistValue()
{
	var v_sResult = K_PRMT_sEMPTY;
	var v_oPersistCheckBox = $( "oPersist" + this.m_sPromptId );
	if (v_oPersistCheckBox && v_oPersistCheckBox.checked) {
		v_sResult += ' persist="true"';
	}

	return v_sResult;
}

//return the selection in the following XML format:
//
// <credential>
//	  <dataSourceConnection>...searchPath to connection...</dataSourceConnection>
//	  <dataSourceSignon>...searchPath to signon...</dataSourceSignon>
//	  <username> ... </username>
//	  <password> ... </password>
//	</credential>
function CSelectDataSourceSignon_sGetValue()
{
	var sCredential = K_PRMT_sEMPTY;
	var v_sPersist = this.sGetPersistValue();
	sCredential += "<credential" + v_sPersist + ">";

	//create credential elements

	//data source connection
	var v_oConnectionName = K_PRMT_sEMPTY;
	var v_oConnectionNameElem = $( "oConnectionName" + this.m_sPromptId );
	if (v_sPersist.length > 0 && v_oConnectionNameElem && v_oConnectionNameElem.value) {
		v_oConnectionName = ' name="' + v_oConnectionNameElem.value +'"';
	}
	sCredential += "<dataSourceConnection" + v_oConnectionName + ">";
	if (this.m_oDataSourceConnection != null)
	{
		sCredential += this.m_oDataSourceConnection.value;
	}

	sCredential += "</dataSourceConnection>";

	if (this.m_bUseSignon == true)
	{
		//data source signon
		sCredential += "<dataSourceSignon>";
		if (this.m_oDataSourceSignon != null)
		{
			sCredential += this.m_oDataSourceSignon.value;
		}
		sCredential += "</dataSourceSignon>";

		//user name
		sCredential += "<username/>";

		//password
		sCredential += "<password/>";
	}
	else
	{
		//data source signon
		sCredential += "<dataSourceSignon/>";

		//user name
		sCredential += "<username>";
		if (this.m_oUsername != null)
		{
			sCredential += this.m_oUsername.value;
		}
		sCredential += "</username>";

		if (this.m_bSignonForCube) {
			//cubePassword
			sCredential += "<cubePassword>";
			if (this.m_oPassword != null)
			{
				sCredential += sXmlEncode(this.m_oPassword.value);
			}
			sCredential += "</cubePassword>";
		}
		else {
			//password
			sCredential += "<password>";
			if (this.m_oPassword != null)
			{
				sCredential += sXmlEncode(this.m_oPassword.value);
			}
			sCredential += "</password>";
		}
	}

	sCredential += "</credential>";

	return sCredential;
}

function CSelectDataSourceSignon_setDataSourceConnection(s)
{
	this.m_oDataSourceConnection.value = s;
}

function CSelectDataSourceSignon_setDataSourceSignon(s)
{
	this.m_oDataSourceSignon.value = s;
}

function CSelectDataSourceSignon_setbUseSignon(sUseSignon)
{
	if (sUseSignon == 'true')
	{
		this.m_bUseSignon = true;
	}
	else if (sUseSignon == 'false')
	{
		this.m_bUseSignon = false;
	}
	else
	{
		this.m_bUseSignon = sUseSignon;
	}
}

function CSelectDataSourceSignon_keyPress()
{
	var v_oEvt = ( arguments && arguments.length ? arguments[arguments.length-1] : null );
	if ( v_oEvt )
	{
		var keyCode = (v_oEvt.keyCode) ? v_oEvt.keyCode : v_oEvt.which;
		if ( keyCode == '13' )
		{
			var oCV = this.getCV();
			if ( oCV && (typeof oCV.canSubmitPrompt == K_PRMT_sFUNCTION) && oCV.canSubmitPrompt() && (typeof oCV.promptAction == K_PRMT_sFUNCTION))
			{
				// If instructed to do so submit the form using next action
				if (this.m_bOKButtonPromptTypeIsNext == true)
				{
					oCV.promptAction(K_ACTION_NEXT);
				}
				else  // do the old thing cause it doesn't have a next button
				{
					oCV.promptAction(K_ACTION_FINISH);
				}
			}

			return PRMTUtils.F_StopEvent( v_oEvt );
		}
	}

	return true;
}

//Prototypes to assign methods to new instances of the object
CSelectDataSourceSignon.prototype.preProcess = CSelectDataSourceSignon_preProcess;
CSelectDataSourceSignon.prototype.checkPass = CSelectDataSourceSignon_checkPass;
CSelectDataSourceSignon.prototype.checkFail = CSelectDataSourceSignon_checkFail;
CSelectDataSourceSignon.prototype.checkData = CSelectDataSourceSignon_checkData;
CSelectDataSourceSignon.prototype.keyPress = CSelectDataSourceSignon_keyPress;
CSelectDataSourceSignon.prototype.sGetPersistValue = CSelectDataSourceSignon_sGetPersistValue;
CSelectDataSourceSignon.prototype.sGetValue = CSelectDataSourceSignon_sGetValue;
CSelectDataSourceSignon.prototype.setDataSourceConnection = CSelectDataSourceSignon_setDataSourceConnection;
CSelectDataSourceSignon.prototype.setDataSourceSignon = CSelectDataSourceSignon_setDataSourceSignon;
CSelectDataSourceSignon.prototype.setbUseSignon = CSelectDataSourceSignon_setbUseSignon;