// Form General Query
//
// - value of dropdown is a code but want displayed value available too
// - to submit the displayed value from a dropdown in a hidden field
function getSelectedValue( selectField, textField )
{
	try
	{
		var ix = selectField.selectedIndex;

		var textInSelectField = selectField[ix].text;

		textField.value = textInSelectField;
	}
	catch(err)
	{
		handleError(err);
	}
}

// Error Handler
//
// Used in catch statements to report exceptions nicely to users
function handleError(err)
{
	var errMsg = "";
	
	errMsg += "An error has occured in the website. Sorry for any inconvenience caused.\n";
	errMsg += "\n";
	errMsg += "Please report the information below to us:\n";
	errMsg += "= URL: " + window.location.href + "\n";
	errMsg += "= Details: " + err + "\n";
	
	alert(errMsg);
}


function reportException(ex)
{
	msg = 'Internal Error:\n\n';
	msg = msg + ex + '\n\n';
	msg = msg + 'Please report this to the website administrator.';
	
	alert(msg);
}



// getObjectById
//
// Cross browser friendly function to get an object by id
function getObjectById(id)
{
	var object = null;
	if (document.layers)
	{
		object = document.layers[id];
	}
	else if (document.all)
	{
		object = document.all[id];
	}
	else if (document.getElementById)
	{
		object = document.getElementById(id);
	}
	return object;
}

// TODO: REMOVE
function getObjectByIdX(id)
{
	alert('getObjectByIdX '+id);
	return getObjectById(id);
}

function pad( str, padStr, length )
{
    while (str.length < length)
	{
        str = padStr + str;
	}
    return str;
}

traceMsgs = new Array();
traceMsgsCount = 0;

function trace( msg )
{
	// collect msgs
	msg = pad(String(traceMsgsCount),'0',4) + ':' + msg;
	traceMsgs.push( msg );
	traceMsgsCount++;

	// display this one
	traceOutputDiv = getObjectById('traceJS');
	if ( null != traceOutputDiv )
	{	
		msgIx = traceMsgsCount-1;
		msgCount = 0;
		sofar = '';
		while ( msgIx >= 0 && msgCount < 100)
		{
			sofar = sofar+traceMsgs[msgIx]+'<br/>';
			msgIx--;
			msgCount++;
		}
		traceOutputDiv.innerHTML = sofar;
	}
	else
	{
//		alert('Msg: '+msg);
	}
}


function showTrace()
{
	showTraceDiv('traceJS');
	showTraceDiv('tracePHP_0');
	showTraceDiv('tracePHP_1');
	showTraceDiv('tracePHP_2');
	showTraceDiv('tracePHP_3');
	showTraceDiv('tracePadder');
}

function showTraceDiv(id)
{
	traceOutput = getObjectById(id);
	if ( null != traceOutput )
	{
		display = traceOutput.style.display;
		if ( display == '' )
		{
			traceOutput.style.display = 'none';
		}
		else if ( display == 'none' )
		{
			traceOutput.style.display = 'block';
		}
		else if ( display == 'block' )
		{
			traceOutput.style.display = 'none';
		}
	}
}


function aniBar(barId, count)
{
	var bar = document.getElementById( barId );

	var width = parseInt(bar.style.width);
	var upto = bar.attributes.upto.value;

	var left = upto - width;
	if ( left > 20 )
	{
		bar.style.width = (width+5)+'px';
	}
	else if ( left > 10 )
	{
		bar.style.width = (width+3)+'px';
	}
	else if ( left > 3 )
	{
		bar.style.width = (width+2)+'px';
	}
	else
	{
		bar.style.width = (width+1)+'px';
	}
	
	
//	alert( w+','+upto );
	if ( width < parseInt(upto) )
	{
		var cmd = 'aniBar("' + barId + '",' + count + ')';
		var t = setTimeout(cmd, 25);
	}
}
