// Copyright (C) 2008 Cognos Incorporated. All rights reserved.
// Cognos and the Cognos logo are trademarks of Cognos Incorporated.

function init()
{
	<!-- Give focus to the first visible control -->
	var nFormFields = document.pform.elements.length;
	for (var i=0; i < nFormFields; i++) {
		if( document.pform.elements[i].type != 'hidden' ) {
			//Handle a timing error in IE which can prevent setting the focus
			setTimeout('document.pform.elements[' + i + '].focus()',1);
			break;
		}
	}
	
	return true;
}

function processCommand(cmd) {
	switch (cmd) {
		case 'ok'		: doCommandOK(); break;
		case 'cancel'	: doCloseAction('cancel'); break;
		case 'close'	: doCloseAction('close'); break;
		case 'help'		: help(); break;
	}
}
							
function doCloseAction(event)
{
	if (g_PS_externalBack != '') {
		setTimeout("document.location.href = g_PS_externalBack", 100);
	} else if (g_PS_isModal == 'true') {
		<!-- Set the right call back target -->
		if (g_PS_callBackTargetJSVar) {
			g_PS_callBackTargetJSVar.ccModalCallBack(event, null); <!-- TODO: add error details in the call back -->								
		}
	} else if (g_PS_errURL != '') {
		errURLExit();
	} else {
		if (parent && parent.closeErrorPage)
			parent.closeErrorPage();
		else if (window.external && window.external.HasOnClose)
			window.external.OnClose(1);
		else if (window.opener && window.opener.closePSLogin)
			window.opener.closePSLogin();
		else if ( history.length > g_PS_emptyHistoryThreshold )
			history.back();
		else
			window.close();
	}
}									
								
function validate()
{
	<!-- Only care to validate a password change.  If that is the case then do it, otherwise, return true. -->
	if (document.pform.elements[g_PS_sVerifyTextNoEcho]) {
		if (document.pform.elements[g_PS_sVerifyTextNoEcho][0].value != document.pform.elements[g_PS_sVerifyTextNoEcho][1].value) {
			alert(g_PS_msg_PasswordVerifyFailed);
			return false;
		}
	}
	
	return true;
}

function doCommandOK() {
	if (validate()) {		
		document.pform.submit();
		cmdOK_disable();
		return true;
	}
	else {
		return false;
	}
}

function cmdOK_enable()
{
	g_PS_cmdOK_enabled = true;
	document.getElementById('cmdOK').disabled = false;
	document.getElementById('cmdOK').className = "cmdButton";
}

function cmdOK_disable()
{
	g_PS_cmdOK_enabled = false;
	document.getElementById('cmdOK').disabled = true;
	document.getElementById('cmdOK').className = "cmdButtonInactive";
}

function cmdOK_isEnabled()
{
	return g_PS_cmdOK_enabled;
}

function errURLExit()
{
	if (g_PS_errURLTooLong == 'true' && document.errURLForm) {
		document.errURLForm.submit();
	} else {
		location.href = g_PS_errURL;
	}
}
