function openAgent()
{
	if ( getCookie( "a_h", document.cookie ) != null )
		return;

	if ( parent )
	{
		if ( parent.agent )
		{
			agent = parent.agent;
			return;
		}
	}

	document.write( "<object width=\"0\" height=\"0\" classid=\"clsid:D45FD31B-5C6E-11D1-9EC1-00C04FD7081F\" id=\"ctlAgent\"></object>" );

	document.write( "<script type=\"text/javascript\" for=\"ctlAgent\" event=\"Hide(CharacterID,Cause)\">" );
	document.write( "tweakAgentFormAndLinkTargets( true );" );
	document.write( "</script>" );

	document.write( "<script type=\"text/javascript\" for=\"ctlAgent\" event=\"Show(CharacterID,Cause)\">" );
	document.write( "tweakAgentFormAndLinkTargets( false );" );
	document.write( "</script>" );

	document.write( "<script type=\"text/javascript\" for=\"ctlAgent\" event=\"RequestComplete(Request)\">" );
	document.write( "if ( agent ) { if ( Request.Status == 0 ) {" );
	document.write( "if ( document.frames[0] ) document.frames[0].doAgentSync(Request); else doAgentSync(Request);" );
	document.write( "agent.Play('RestPose');}}" );
	document.write( "</script>" );

	document.write( "<script type=\"text/javascript\">" );
	document.write( "try {" );
	document.write( "var control = document.getElementById('ctlAgent');" );
	document.write( "if ( control ) {" );
	document.write( "control.Connected = true;" );
	document.write( "if ( control.Characters ) {" );
	document.write( "if ( control.Characters.Load( 'Merlin', 'Merlin.acs' ).Status == 0 ) {" );
	document.write( "agent = control.Characters( 'Merlin' );" );
	document.write( "if ( !agent.Visible ) {" );
	document.write( "var left;" );
	document.write( "var top;" );
	document.write( "if ( window.screenLeft < 240 ) left = window.screenLeft + 300; else left = window.screenLeft;" );
	document.write( "if ( window.screenTop > 75 ) top = window.screenTop - 75; else top = 0;" );
	document.write( "agent.MoveTo( left, top );" );
	document.write( "agent.Show();" );
	document.write( "}" );

	document.write( "document.write( \"</head>\" );" );
	document.write( "document.write( \"<frameset><frame src='\" );" );

	document.write( "var strLocation = window.location.href;" );
	document.write( "document.write( strLocation );" );

	document.write( "if ( strLocation.indexOf( \"?\" ) == -1 )" );
	document.write( "document.write( \"?\" );" );
	document.write( "else " );
	document.write( "document.write( \"&amp;\" );" );

	document.write( "document.write( \"frameset=\" );" );
	document.write( "document.write( Date.parse( new Date() ));" );
	document.write( "document.write( \"'></frameset>\" );" );

	document.write( "} else { document.cookie = 'a_h=' + escape( 'true' ); }" );
	document.write( "} else { document.cookie = 'a_h=' + escape( 'true' ); }" );
	document.write( "} else { document.cookie = 'a_h=' + escape( 'true' ); }" );
	document.write( "} catch ( e ) { document.cookie = 'a_h=' + escape( 'true' ); agent = null; }" );
	document.write( "</script>" );
}

function tweakAgentFormAndLinkTargets( hide )
{
	if ( !agent )
	{
		document.cookie = "a_h=" + escape( "true" );
		return;
	}

	var visibilityCause = agent.VisibilityCause;

	if ( visibilityCause >= 3 && visibilityCause <= 6 )
		return;

	if ( hide )
	{
		document.cookie = "a_h=" + escape( "true" );
	}
	else
	{
		document.cookie = "a_h=";
	}

	if ( !document.frames[0] )
		return;

	var frameDocument = document.frames[0].document;

	tweakTargets( frameDocument.getElementsByTagName( "a" ), hide );
	tweakTargets( frameDocument.getElementsByTagName( "form" ), hide );
}

function tweakTargets( collection, hide )
{
	for ( var iLoop = 0; iLoop < collection.length; iLoop++ )
	{
		var strLink = collection[iLoop].href;

		if ( !strLink )
		{
			strLink = collection[iLoop].action;

			if ( !strLink )
				continue;
		}

		var strTarget = collection[iLoop].target;

		if ( hide || ( strLink.indexOf( "http" ) == 0 && strLink.indexOf( "localhost" ) == -1 && strLink.indexOf( "makeastorybook.com" ) == -1 ))
		{
			if ( strTarget == "" )
				collection[iLoop].target = "_top";
		}
		else
		{
			if ( strTarget == "_top" )
				collection[iLoop].target = "";
		}
	}
}

function touchCookie( strName )
{
	var cookie = getCookie( "AGENT", document.cookie );
	var value;

	if ( cookie == null )
	{
		value = 0;
		cookie = strName + "=0";
	}
	else
	{
		cookie = unescape( cookie[1] );

		var indexOfStart = cookie.indexOf( strName + "=" );

		if ( indexOfStart != -1 )
		{
			var indexOfEnd = cookie.indexOf( ";", indexOfStart );

			if ( indexOfEnd != -1 )
			{
				value = cookie.substring( indexOfStart + ( strName.length + 1 ), indexOfEnd );
				cookie = cookie.substring( 0, indexOfStart ) + cookie.substring( indexOfEnd + 1 );
			}
			else
			{
				value = cookie.substring( indexOfStart + ( strName.length + 1 ));
				cookie = cookie.substring( 0, indexOfStart - 1 );
			}
		}
		else
		{
			value = -1;
		}

		value++;
		cookie += ";" + strName + "=" + value;
	}

	document.cookie = "AGENT=" + escape( cookie );

	return value;
}

function agentGestureAt( id, messageOnScreen, messageScroll, p_window )
{
	if ( !p_window )
		p_window = window;

	var actual_agent = p_window.agent;

	if ( !actual_agent )
		return;

	var actual_document = p_window.document;
	var element = actual_document.getElementById( id );

	if ( !element )
		return;

	var clientWidth = element.clientWidth;
	var clientHeight = element.clientHeight;
	var offsetLeft = -actual_document.body.scrollLeft;
	var offsetTop = -actual_document.body.scrollTop;

	while( element )
	{
		offsetLeft += element.offsetLeft;
		offsetTop += element.offsetTop;
		element = element.offsetParent;
	}

	if ( !messageScroll )
		messageScroll = messageOnScreen;

	if ( offsetTop + clientHeight < 0 )
	{
		if ( offsetLeft < 0 )
		{
			messageOnScreen = "Scroll up and to the left, " + messageScroll;
		}
		else if ( offsetLeft > ( actual_document.body.clientWidth - clientWidth ))
		{
			messageOnScreen = "Scroll up and to the right, " + messageScroll;
		}
		else
		{
			messageOnScreen = "Scroll up, " + messageScroll;
		}
	}
	else if ( offsetTop > actual_document.body.clientHeight )
	{
		if ( offsetLeft < 0 )
		{
			messageOnScreen = "Scroll down and to the left, " + messageScroll;
		}
		else if ( offsetLeft > ( actual_document.body.clientWidth - clientWidth ))
		{
			messageOnScreen = "Scroll down and to the right, " + messageScroll;
		}
		else
		{
			messageOnScreen = "Scroll down, " + messageScroll;
		}
	}
	else
	{
		offsetLeft += p_window.screenLeft;
		offsetTop += p_window.screenTop;

		var agentWidth = actual_agent.Width;
		var agentHeight = actual_agent.Height;

		var moveToLeft;
		var moveToTop;

		if ( offsetTop > agentHeight )
		{
			moveToLeft = offsetLeft + ( clientWidth / 2 ) - ( agentWidth / 2 );
			moveToTop = offsetTop - agentHeight;
		}
		else if ( offsetLeft > agentWidth )
		{
			moveToLeft = offsetLeft - agentWidth;
			moveToTop = offsetTop + ( clientHeight / 2 ) - ( agentHeight / 2 );
		}
		else
		{
			moveToLeft = offsetLeft + clientWidth;
			moveToTop = offsetTop + ( clientHeight / 2 ) - ( agentHeight / 2 );
		}

		var diffX = actual_agent.Left - moveToLeft;
		var diffY = actual_agent.Top - moveToTop;

		if ( diffX < -10 || diffX > 10 || diffY < -10 || diffY > 10 )
		{
			actual_agent.Hide();
			actual_agent.MoveTo( moveToLeft, moveToTop );
			actual_agent.Show();
		}
	}

	actual_agent.GestureAt( offsetLeft + ( clientWidth / 2 ), offsetTop + ( clientHeight / 2 ));
	actual_agent.Speak( messageOnScreen );
	actual_agent.Play( "RestPose" );
}

function form_submit( submit_url )
{
	if ( document.getElementById( "link_submitter_url" ))
	{
		scene_selectAll();

		document.getElementById( "link_submitter_url" ).value = submit_url;
		document.getElementById( "link_submitter_form" ).submit();

		return false;
	}

	return true;
}

function template_free()
{
	window.open( "sample.do?sample=" + document.getElementById( "template" ).selectedIndex, "_blank", "resizeable=yes,scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,copyhistory=no" );

	return false;
}

function character_edit()
{
	select = document.getElementById( "character" );

	if ( select.selectedIndex == - 1 )
	{
		alert( 'There must be at least one character in the story before you can use the \'Edit selected character\' button.\n\nPlease use the \'New character\' button to create one' );
		return false;
	}

	return true;
}

function scene_edit( numberOfScenes )
{
	select = document.getElementById( "scene" );
	selectedIndex = select.selectedIndex;

	if ( selectedIndex == - 1 )
	{
		alert( 'There must be at least one scene in the story before you can use the \'Edit selected scene\' button.\n\nPlease use the \'New scene\' button to create one' );
		return false;
	}

	if ( numberOfScenes > 0 && selectedIndex != 0 )
	{
		alert( 'There are ' + numberOfScenes + ' scenes in the story.\n\nPlease start reading them from the first one, so that you can read them in order' );
		return false;
	}

	return true;
}

function scene_selectAll()
{
	select = document.getElementById( "scene" );

	if ( select )
	{
		select.style.visibility = "hidden";
		options = select.options;

		if ( options )
		{
			for ( var iLoop = 0; iLoop < options.length; iLoop++ )
			{
				if ( options[ iLoop ].selected )
				{
					document.getElementById( "scene_selected" ).value = options[ iLoop ].value;
					continue;
				}

				options[ iLoop ].selected = true;
			}
		}
	}
}

function scene_moveUp()
{
	// For each option in the list (except
	// the first, and unless the first is selected)...

	options = document.getElementById( "scene" );

	if ( options.length == 0 )
	{
		alert( 'There must be at least one scene in the story before you can use the \'Move up\' button.\n\nPlease use the \'New scene\' button to create one' );
		return false;
	}

	if ( options[ 0 ].selected )
	{
		alert( 'The selected scene is already the first scene in the story. It cannot be moved up' );
		return false;
	}

	for ( var iLoop = 1; iLoop < options.length; iLoop++ )
	{
		option = options[ iLoop ];

		// ...if it's not selected, move on...

		if ( !option.selected )
			continue;

		// ...otherwise, move it up

		tempText = options[ iLoop - 1 ].text;
		tempValue = options[ iLoop - 1 ].value;
		tempSelected = options[ iLoop - 1 ].selected;
		options[ iLoop - 1 ].text = option.text;
		options[ iLoop - 1 ].value = option.value;
		options[ iLoop - 1 ].selected = option.selected;
		options[ iLoop ].text = tempText;
		options[ iLoop ].value = tempValue;
		options[ iLoop ].selected = tempSelected;
	}

	return false;
}

function scene_moveDown()
{
	// For each option in the list (except
	// the last, and unless the last is selected)...

	options = document.getElementById( "scene" );

	if ( options.length == 0 )
	{
		alert( 'There must be at least one scene in the story before you can use the \'Move down\' button.\n\nPlease use the \'New scene\' button to create one' );
		return false;
	}

	if ( options[ options.length - 1 ].selected )
	{
		alert( 'The selected scene is already the last scene in the story. It cannot be moved down' );
		return false;
	}

	for ( var iLoop = options.length - 2; iLoop >= 0; iLoop-- )
	{
		option = options[ iLoop ];

		// ...if it's not selected, move on...

		if ( !option.selected )
			continue;

		// ...otherwise, move it down

		tempText = options[ iLoop + 1 ].text;
		tempValue = options[ iLoop + 1 ].value;
		tempSelected = options[ iLoop + 1 ].selected;
		options[ iLoop + 1 ].text = option.text;
		options[ iLoop + 1 ].value = option.value;
		options[ iLoop + 1 ].selected = option.selected;
		options[ iLoop ].text = tempText;
		options[ iLoop ].value = tempValue;
		options[ iLoop ].selected = tempSelected;
	}

	return false;
}

var excluded = " (excluded)";

function scene_include()
{
	// For each option in the list...

	options = document.getElementById( "scene" );

	numberSelected = 0;
	numberChanged = 0;

	for ( var iLoop = options.length - 1; iLoop >= 0; iLoop-- )
	{
		option = options[ iLoop ];

		// ...if it's not selected, move on...

		if ( !option.selected )
			continue;

		numberSelected++;

		// ...if it's already included, move on...

		valueOption = option.value;

		if ( !scene_isExcluded( valueOption ))
			continue;

		numberChanged++;

		// ...otherwise, include it

		option.value = valueOption.substring( 0, valueOption.length - excluded.length );
		option.text = option.text.substring( 0, option.text.length - excluded.length );
		option.selected = true;
	}

	if ( numberChanged == 0 )
	{
		switch( numberSelected )
		{
			case 0:		alert( 'There must be at least one scene in the story before you can use the \'Include\' button.\n\nPlease use the \'New scene\' button to create one' );
						break;

			case 1:		alert( 'The selected scene is already included in the story. Use the \'Exclude\' button if you want to exclude it' );
						break;

			default:	alert( 'The selected scenes are already included in the story. Use the \'Exclude\' button if you want to exclude them' );
						break;
		}
	}

	return false;
}

function scene_exclude()
{
	// For each option in the list...

	options = document.getElementById( "scene" );

	numberSelected = 0;
	numberChanged = 0;

	for ( var iLoop = options.length - 1; iLoop >= 0; iLoop-- )
	{
		option = options[ iLoop ];

		// ...if it's not selected, move on...

		if ( !option.selected )
			continue;

		numberSelected++;

		// ...if it's already excluded, move on...

		valueOption = option.value;

		if ( scene_isExcluded( valueOption ))
			continue;

		// ...otherwise, exclude it

		numberChanged++;

		option.value = valueOption + excluded;
		option.text += excluded;
		option.selected = true;
	}

	if ( numberChanged == 0 )
	{
		switch( numberSelected )
		{
			case 0:		alert( 'There must be at least one scene in the story before you can use the \'Exclude\' button.\n\nPlease use the \'New scene\' button to create one' );
						break;

			case 1:		alert( 'The selected scene is already excluded from the story. Use the \'Include\' button if you want to include it' );
						break;

			default:	alert( 'The selected scenes are already excluded from the story. Use the \'Include\' button if you want to to include them' );
						break;
		}
	}

	return false;
}

function scene_isExcluded( valueOption )
{
	lengthOptionValue = valueOption.length;

	if ( lengthOptionValue > excluded.length )
	{
		valueEnd = valueOption.substring( lengthOptionValue - excluded.length, lengthOptionValue );

		if ( valueEnd == excluded )
			return true;
	}

	return false;
}

function step_checkIsEmpty( option_preamble, option_name )
{
	combo = document.getElementById( option_preamble + option_name );

	if ( combo.options[ combo.selectedIndex ].value.length == 0 )
	{
		alert( "You must choose something from the '" + option_name + "' drop-down box!" );
		return false;
	}

	return true;
}

function step_changed()
{
	document.getElementById( 'changed' ).value = 'true';

	if ( agent && touchCookie( "j0" ) == 0 )
	{
		agent.Stop();
		agentGestureAt( "update", "Click the Update button to see the effect of your change, or...", "then click the Update button to see the effect of your change, or..." );
		agent.Play( "Think" );
		agent.Speak( "...change \\Emp\\several things at once, and then click Update." );
		agent.Play( "RestPose" );
	}
}

function step_changeAlt( option_name, image_name )
{
	image = document.getElementById( image_name );
	combo = document.getElementById( option_name );
	value = combo.options[ combo.selectedIndex ].text;

	if ( value.length == 0 )
	{
		image.alt = "";
		image.style.visibility = "hidden";
		return;
	}

	image.alt = "Edit " + value;
	image.style.visibility = "visible";
}

var color_picker = null;
var decal_picker = null;
var uploader = null;
var storybook_loader = null;

function step_openColorPicker( color, default_color )
{
	_width = 470;
	_height = 387;
	_top = ( screen.height - _height ) / 2;
	_left = ( screen.width - _width ) / 2;
	color_picker = window.open( "color_picker.htm?color=" + color + "&default_color=" + default_color, "color_picker", "top=" + _top + ",left=" + _left + ",width=" + _width + ",height=" + _height + ",resizeable=no,scrollbars=no,toolbar=no,location=no,directories=no,status=no,menubar=no,copyhistory=no" );

	return false;
}

function step_openDecalPicker( text, color, font )
{
	_width = 300;
	_height = 305;
	_top = ( screen.height - _height ) / 2;
	_left = ( screen.width - _width ) / 2;
	decal_picker = window.open( "decal_picker.jsp?text=" + text + "&color=" + color + "&font=" + font, "decal_picker", "top=" + _top + ",left=" + _left + ",width=" + _width + ",height=" + _height + ",resizeable=no,scrollbars=no,toolbar=no,location=no,directories=no,status=no,menubar=no,copyhistory=no" );

	return false;
}

function step_openUploader()
{
	_width = 300;
	_height = 110;
	_top = ( screen.height - _height ) / 2;
	_left = ( screen.width - _width ) / 2;
	uploader = window.open( "uploader.jsp", "uploader", "top=" + _top + ",left=" + _left + ",width=" + _width + ",height=" + _height + ",resizeable=no,scrollbars=no,toolbar=no,location=no,directories=no,status=no,menubar=no,copyhistory=no" );

	return false;
}

function step_openStorybookLoader()
{
	select = document.getElementById( "template" );

	if ( select.value != "" )
		return true;

	_width = 300;
	_height = 110;
	_top = ( screen.height - _height ) / 2;
	_left = ( screen.width - _width ) / 2;
	storybook_loader = window.open( "storybook_loader.jsp", "uploader", "top=" + _top + ",left=" + _left + ",width=" + _width + ",height=" + _height + ",resizeable=no,scrollbars=no,toolbar=no,location=no,directories=no,status=no,menubar=no,copyhistory=no" );

	return false;
}

function step_closeStorybookLoader()
{
	if ( storybook_loader != null && !storybook_loader.closed )
	{
		storybook_loader.close();
		storybook_loader = null;
	}
}

function step_closeColorPickerAndDecalPicker()
{
	step_closeColorPicker();

	if ( decal_picker != null && !decal_picker.closed )
	{
		decal_picker.close();
		decal_picker = null;
	}
}

function step_closeColorPickerAndUploader()
{
	step_closeColorPicker();

	if ( uploader != null && !uploader.closed )
	{
		uploader.close();
		uploader = null;
	}
}

function step_closeColorPicker()
{
	if ( color_picker != null && !color_picker.closed )
	{
		color_picker.close();
		color_picker = null;
	}
}

function step_imagemap_submit( ref, option, state )
{
	document.getElementById( "imagemap_ref" ).value = ref;
	document.getElementById( "imagemap_option" ).value = option;
	document.getElementById( "imagemap_state" ).value = state;

	document.getElementById( "link_submitter_form" ).submit();

	return false;
}

function color_picker_load()
{
	var doc;

	if ( opener )
		doc = opener.document
	else
		doc = parent.opener.document;

	combo = doc.getElementById( picker_param( "color" ));
	value = combo.options[ combo.selectedIndex ].value;
	red = 0;
	green = 0;
	blue = 0;
	transparent = 255;

	indexOfComma = value.indexOf( "," );

	if ( indexOfComma == -1 )
	{
		value = picker_param( "default_color" );
		indexOfComma = value.indexOf( "," );
	}

	if ( indexOfComma != -1 )
	{
		red = value.substring( 0, indexOfComma );

		indexOfLastComma = indexOfComma + 1;
		indexOfComma = value.indexOf( ",", indexOfLastComma );

		if ( indexOfComma != -1 )
		{
			green = value.substring( indexOfLastComma, indexOfComma );

			indexOfLastComma = indexOfComma + 1;
			indexOfComma = value.indexOf( ",", indexOfLastComma );

			if ( indexOfComma != -1 )
			{
				blue = value.substring( indexOfLastComma, indexOfComma );

				indexOfLastComma = indexOfComma + 1;
				transparent = value.substring( indexOfLastComma );
			}
			else
			{
				blue = value.substring( indexOfLastComma );
			}
		}
	}

	color_picker_setSample( red, green, blue, transparent );
}

function color_picker_setSample( red, green, blue, transparent )
{
	if ( transparent )
		color_picker_setTransparent( transparent, false );

	color_picker_setRed( red, false );
	color_picker_setGreen( green, false );
	color_picker_setBlue( blue, true );
}

function color_picker_setRed( red, updateSample )
{
	strRedHex = color_picker_setBar( "Red", red );
	document.getElementById( "Red_bar" ).style.backgroundColor = "#" + strRedHex + "0000";

	if ( updateSample )
		color_picker_updateSample();
}

function color_picker_setGreen( green, updateSample )
{
	strGreenHex = color_picker_setBar( "Green", green );
	document.getElementById( "Green_bar" ).style.backgroundColor = "#00" + strGreenHex + "00";

	if ( updateSample )
		color_picker_updateSample();
}

function color_picker_setBlue( blue, updateSample )
{
	strBlueHex = color_picker_setBar( "Blue", blue );
	document.getElementById( "Blue_bar" ).style.backgroundColor = "#0000" + strBlueHex;

	if ( updateSample )
		color_picker_updateSample();
}

function color_picker_setTransparent( transparent, updateSample )
{
	strBlueHex = color_picker_setBar( "Transparent", transparent );
}

function color_picker_mouseRed( event )
{
	if ( event.button == 0 )
		return;

	offset = event.offsetX;
	color_picker_setRed( offset, true );
}

function color_picker_mouseGreen( event )
{
	if ( event.button == 0 )
		return;

	offset = event.offsetX;
	color_picker_setGreen( offset, true );
}

function color_picker_mouseBlue( event )
{
	if ( event.button == 0 )
		return;

	offset = event.offsetX;
	color_picker_setBlue( offset, true );
}

function color_picker_mouseTransparent( event )
{
	if ( event.button == 0 )
		return;

	offset = event.offsetX;
	color_picker_setTransparent( offset, true );
}

function color_picker_incrementRed( increment )
{
	color_picker_setRed( document.getElementById( "Red_bar" ).width - (-increment), true );
}

function color_picker_incrementGreen( increment )
{
	color_picker_setGreen( document.getElementById( "Green_bar" ).width - (-increment), true );
}

function color_picker_incrementBlue( increment )
{
	color_picker_setBlue( document.getElementById( "Blue_bar" ).width - (-increment), true );
}

function color_picker_incrementTransparent( increment )
{
	color_picker_setTransparent( document.getElementById( "Transparent_bar" ).width - (-increment), true );
}

function color_picker_setBar( bar, value )
{
	value = Math.max( value, 0 );
	value = Math.min( value, 255 );

	strHex = color_picker_convertToHex( value );

	width = Math.max( value, 1 );

	document.getElementById( bar + "_bar" ).style.width = width + "px";
	document.getElementById( bar + "_bar" ).width = width;
	document.getElementById( bar + "_bar" ).innerHTML = value;

	return strHex;
}

function color_picker_updateSample()
{
	red = document.getElementById( "Red_bar" ).width;
	strRedHex = color_picker_convertToHex( red );
	green = document.getElementById( "Green_bar" ).width;
	strGreenHex = color_picker_convertToHex( green );
	blue = document.getElementById( "Blue_bar" ).width;
	strBlueHex = color_picker_convertToHex( blue );

	document.getElementById( "colorSample" ).style.backgroundColor = "#" + strRedHex + strGreenHex + strBlueHex;

	if ( agent && touchCookie( "j1" ) == 1 )
	{
		agent.Stop();
		agentGestureAt( "colorSample", "Your chosen color is displayed here.", "to see your chosen color displayed in the box." );
		agentGestureAt( "use_color", "Click here to use this color.", "and click the Use this color button." );
	}
}

function color_picker_convertToHex( decimal )
{
	strToReturn = "";

	while( strToReturn.length < 2 )
	{
		iPart = decimal % 16;

		switch( iPart )
		{
			case 15:	strToReturn = "F" + strToReturn; break;
			case 14:	strToReturn = "E" + strToReturn; break;
			case 13:	strToReturn = "D" + strToReturn; break;
			case 12:	strToReturn = "C" + strToReturn; break;
			case 11:	strToReturn = "B" + strToReturn; break;
			case 10:	strToReturn = "A" + strToReturn; break;
			default:	strToReturn = iPart + strToReturn; break;
		}

		decimal = Math.floor( decimal / 16 );
	}

	return strToReturn;
}

function color_picker_apply()
{
	red = document.getElementById( "Red_bar" ).innerHTML;
	green = document.getElementById( "Green_bar" ).innerHTML;
	blue = document.getElementById( "Blue_bar" ).innerHTML;
	transparent = document.getElementById( "Transparent_bar" ).innerHTML;

	var doc;

	if ( opener )
		doc = opener.document
	else
		doc = parent.opener.document;

	combo = doc.getElementById( picker_param( "color" ));
	combo.options[ combo.options.length - 1 ].text = "(mix)";
	combo.options[ combo.options.length - 1 ].value = red + "," + green + "," + blue + "," + transparent;
	combo.options[ combo.options.length - 1 ].styleClass = "step_custom_option";
	combo.options[ combo.options.length - 1 ].selected = true;

	changed = doc.getElementById( "changed" );

	if ( changed )
	{
		changed.value = "true";
	}

	color_picker_close();

	if ( agent && touchCookie( "j2" ) == 0 )
	{
		agent.Stop();
		agentGestureAt( "update", "Click the Update button to see the effect of your new color.", "then click the Update button to see the effect of your new color.", parent.opener );
	}

	return false;
}

function color_picker_close()
{
	if ( parent )
		parent.close();
	else
		window.close();

	color_picker = null;

	return false;
}

function decal_picker_load()
{
	var doc;

	if ( opener )
		doc = opener.document
	else
		doc = parent.opener.document;

	textSrc = doc.getElementById( picker_param( "text" ));
	textDest = document.getElementById( "text" );
	textDest.value = textSrc.value;

	addLatestCustomSymbol();

	color = doc.getElementById( picker_param( "color" ));
	colorUs = document.getElementById( "color" );
	options = colorUs.options;

	bSelected = false;

	for ( var iLoop = 0; iLoop < options.length - 1; iLoop++ )
	{
		option = options[ iLoop ];

		if ( option.value == color.value )
		{
			option.selected = true;
			bSelected = true;
			break;
		}
	}

	if ( !bSelected )
	{
		options[ options.length - 1 ].text = "(mix)";
		options[ options.length - 1 ].value = color.value;
		options[ options.length - 1 ].styleClass = "step_custom_option";
		options[ options.length - 1 ].selected = true;
	}

	font = doc.getElementById( picker_param( "font" ));
	document.getElementById( "font" ).value = font.value;
}

function decal_picker_symbol()
{
	text = document.getElementById( "text" );
	symbol = document.getElementById( "symbol" );
	value = symbol.value;

	if ( value.length > 0 )
	{
		text.value = text.value + "[" + symbol.value + "]";
		text.focus();
	}

	return false;
}

function decal_picker_apply()
{
	var doc;

	if ( opener )
		doc = opener.document
	else
		doc = parent.opener.document;

	textDest = doc.getElementById( picker_param( "text" ));
	textSrc = document.getElementById( "text" );
	textDest.value = textSrc.value;

	color = doc.getElementById( picker_param( "color" ));
	color.value = document.getElementById( "color" ).value;

	font = doc.getElementById( picker_param( "font" ));
	font.value = document.getElementById( "font" ).value;

	doc.getElementById( "changed" ).value = "true";

	decal_picker_close();

	if ( agent && touchCookie( "j3" ) == 0 )
	{
		agent.Stop();
		agentGestureAt( "update", "Click the Update button to see what your design looks like.", "then click the Update button to see what your design looks like.", parent.opener );
	}

	return false;
}

function decal_picker_close()
{
	if ( parent )
		parent.close();
	else
		window.close();

	decal_picker = null;

	return false;
}

function picker_param( name )
{
	search = location.search;
	indexOfStart = search.indexOf( name + "=" );

	if ( indexOfStart != -1 )
	{
		indexOfStart += ( name.length + 1 );
		indexOfEnd = search.indexOf( "&", indexOfStart );

		if ( indexOfEnd == -1 )
			return unescape( search.substring( indexOfStart ));

		return unescape( search.substring( indexOfStart, indexOfEnd ));
	}

	return null;
}

function uploader_close()
{
	if ( parent )
		parent.close();
	else
		window.close();

	uploader = null;

	return false;
}

function storybook_loader_close()
{
	if ( parent )
		parent.close();
	else
		window.close();

	storybook_loader = null;

	return false;
}
