// <SCRIPT>



function setCommand(command, arg)
{
	var f = document.forms[0];
	if(f.pageSubmitted && f.debug==null)
		return false;

	f.cmd.value = command;
	f.param.value = arg;
	return true;
}

function doSubmit()
{
	var f = document.forms[0];
	if(f.pageSubmitted && f.debug==null)
		return;

	f.submit();
	f.pageSubmitted = true;
	return;
}

function cancelledSubmit()
{
	document.forms[0].pageSubmitted=true;
}

function send(command, arg)
{
	if(setCommand(command, arg))
	{
		var f = document.forms[0];
		f.submit();
		f.pageSubmitted = true;
	}
}

function sendDlgCmd(f, command, arg)
{
	if(f.pageSubmitted && f.debug==null)
		return false;

	f.cmd.value = command;
	f.param.value = arg;
	f.submit();
	f.pageSubmitted = true;
}

function sendDelay(delay, command, arg)
{
	var cmd = "send('"+command+"'";
	if(arg != null)
		cmd += ",'"+arg+"'";
	cmd += ")";
	setTimeout(cmd, delay);
}

function onSubmit()
{
	var f = document.forms[0];
	if(defButton != null)
		if(f.cmd.value.length == 0)
			return setCommand(defButton);

	if(f.pageSubmitted && f.debug==null)
		return false;
	f.pageSubmitted = true;
	return true;
}

function toggle(id, path)
{
	send(id+"_toggle", path);
	event.cancelBubble = true;
}

function leaf()
{
	event.cancelBubble = true;
}

function delCol(table, row)
{
	cmd(table+"delcol", row);
}

function setFocus(name)
{
	var field = document.forms[0][name];
	if(field==null && document.getElementsByName!=null)
		field = document.getElementsByName(name)[0];
	if(field == null)
		return;
	
	if(field.className=='radio')
	{
		var grp = document.forms[0][field.name];
		if(grp==null && document.getElementsByName!=null)
			grp = document.getElementsByName(field.name);
			
		for( var loop=0; loop<grp.length; loop++ )
			if(grp[loop].checked)
				field = grp[loop];
	}
		
	field.focus();
}

function openDlg(name, url, modal, width, height)
{
	if(modal)
	{
		window.showModalDialog(url, name,
			'dialogLeft:50px;dialogTop:30px;dialogWidth:' + width + 'px;dialogHeight:' + height + 'px;status:0;resizeable:1' );
	}
	else
	{
		var picker = window.open(url, name,
			'width=' + width + ',height=' + height + ',scrollbars=1,toolbar=0,status=0,resizable=1' );
		if(picker.opener != null)
			picker.moveTo(50, 30);
	}
}

function openNewBrowserWindow(url)
{
	newwindow = window.open(url, "newwin");
	newwindow.focus();
}

function initCodePicker( id, codeType, elmLevel, param, field, modal )
{
	var url = 'HTMLCompiler?app=com.coda.common.esi.apps.CodePickerApp';
	url+= '&id=' + id;
	url+= '&init=' + codeType;
	url+= '&field=' + field;
	url+= '&parentapp=' + document.forms[0].app.value;
	if (document.forms[0]['state']!=null)
		url+= '&parentappstate='+ document.forms[0].state.value;
	if(param != null)
		url+= '&param=' + escapeURLParam(param);

	if( codeType == 'Element' || codeType == 'AccountSummaryElement' || codeType == 'UmbrellaElement' )
	{
		if( isNaN(parseInt(elmLevel)) )
			elmLevel = document.getElementsByName(elmLevel)[0].value;
		url+= '&elmLevel=' + elmLevel;
		openDlg('bld', url, modal, 352, 535);
	}
	else
	{
		openDlg('bld', url, modal, 352, 450);
	}
}

function initAccFinder( field, wildCards, modal, icDestination )
{
	initAccFinderIC( field, wildCards, modal, icDestination, "" );
}

function initAccFinderIC( field, wildCards, modal, icDestination, icDestinationCode )
{
	var editField = document.forms[0][field];
	var accCode = editField.value;
	var pApp = document.forms[0].app.value;
	var url = 'HTMLCompiler?app=com.coda.common.esi.apps.AccFinderApp&init=' + field + '&code=' + accCode +
					'&parentapp=' + pApp + '&wildcards=' + wildCards + '&icDestination=' + icDestination
					+ '&icDestinationCode=' + icDestinationCode;
	var pState = document.forms[0].state;
	
	if(pState != null)
		url += '&parentappstate=' + pState.value;
		
	if(icDestination && icDestinationCode == "")
		openDlg('accFinder', url, modal, 432, 370);
	else		
		openDlg('accFinder', url, modal, 361, 323);
}

function initValuePicker( app, codeType, field, modal )
{
	var url = 'HTMLCompiler?app=' + app;
	var value = document.getElementsByName(field)[0].value;
	url+= '&field=' + field;
	url+= '&codeType=' + codeType;
	url+= '&value=' + escapeURLParam(value);
	openDlg('bld', url, modal, 450, 300);
}

function onBodyKey(event)
{
	var HELP_KEY = 113;

	if(event.keyCode==HELP_KEY && showHelp!=null)
	{
		showHelp();
		event.cancelBubble = true;
	}
}

function onSetAccCode()
{
	var accCode = "";
	
	if( document.forms[0].destination != null && document.forms[0].destination.disabled == false )
	{
		// Non-code-combo case
		if( document.forms[0].destination.value != "" )
			accCode += document.forms[0].destination.value;
				
		accCode += ".";			
	}
	else if( document.forms[0].destination_text != null && document.forms[0].destination_text.disabled == false )
	{	
		// Code-combo case	
		if( document.forms[0].destination_text.value != "" )
			accCode += document.forms[0].destination_text.value;
			
		accCode += ".";
	}
	
	if( document.forms[0].elm1.value != "" )
		accCode += document.forms[0].elm1.value;

	accCode += ".";
	
	if( document.forms[0].elm2.value != "" )
		accCode += document.forms[0].elm2.value;
		
	accCode += ".";		
	
	if( document.forms[0].elm3.value != "" )
		accCode += document.forms[0].elm3.value;
	
	accCode += ".";
		
	if( document.forms[0].elm4.value != "" )
		accCode += document.forms[0].elm4.value;
	
	accCode += ".";
		
	if( document.forms[0].elm5.value != "" )
		accCode += document.forms[0].elm5.value;
	
	accCode += ".";
		
	if( document.forms[0].elm6.value != "" )
		accCode += document.forms[0].elm6.value;
	
	accCode += ".";
		
	if( document.forms[0].elm7.value != "" )
		accCode += document.forms[0].elm7.value;

	accCode += ".";
	
	if( document.forms[0].elm8.value != "" )
		accCode += document.forms[0].elm8.value;
		
	// Remove trailing . characters		
	for( loop = accCode.length - 1; loop >= 0; loop-- )
	{
		if( accCode.charAt( loop ) == "." )
			accCode = accCode.substring( 0, accCode.lastIndexOf( "." ) );
		else	
			break; // Not more . so we can break out of the loop
	}
	
	returnVal( accCode );
}

function replaceChar(str, oldCh, newStr)
{
	var i;
	var retStr = "";
	for(i=0; i<str.length; ++i)
	{
		var ch = str.charAt(i);
		retStr += ((ch==oldCh) ? newStr : ch);
	}
	return retStr;
}

function escapeURLParam( param )
{
	if(typeof encodeURIComponent == "undefined")
	{
		var escStr = escape(param);
		return replaceChar(escStr, "+", "%2B");
	}
	return encodeURIComponent(param);
}

function explodeDocument( table, row )
{	
	var key = escapeURLParam( document.forms[0].state.value );
	var url = 'HTMLCompiler?app=com.coda.finance.esi.apps.pay.PayDetailsSubApp&state=' + key + '&row=' + row;
	
	window.open( url, 'explodedoc' );
}

function onTargetClick( target, cmd, row, x, y )
{
	var app = escapeURLParam( document.forms[0]["app"].value );
	var state = escapeURLParam( document.forms[0]["state"].value );
	var url = 'HTMLCompiler?app=' + app + '&state=' + state + '&cmd=' + cmd + '&param=' + row;

	// Use code picker defaults
	if( x == null || y == null )
		openDlg( target, url, false, 260, 336 );
	else
		openDlg( target, url, false, x, y );
}

function onDlgCmd( cmd, arg )
{
	// If the second argument is null (or non existant) then send the file name of
	// the state file as the "arg" value. 
	//
	// This was removed in 8.2 but I've put it back as the Actions in Pay stop working
	// if you don't pass in the state.value
	//
	// 07/06/01 - CJL
	if (arg == null)
		sendDlgCmd( opener.document.forms[0], cmd, document.forms[0].state.value );	
	else
		sendDlgCmd( opener.document.forms[0], cmd, arg );

	window.close();	
}

