{stored_object name=$blah|cat:'u':$user->id type="newcomp" from="newcomp" assign="newcomp"}

{if $smarty.request.confirmReset}{stored_object name=$blah|cat:'u':$user->id type="signup" destroy="true"}{/if}

{include_php file="shared_handle:newcomp.createInvitation"}

<h1 class="pageHead2">Create a Competition: Step 2</h1>

<h2>Create Your Invitation Letter:</h2>

<form name="compStartForm" method="post">
<input type="hidden" name="pg" value="{toc_next}">
<input type="hidden" name="no_expire" value="true">
<input type="hidden" id="letterType" name="newcomp[letterType]" value="">

<script language="JavaScript">

var examples = Array();
{foreach from=$examples key="key" item="example"}
examples[{$key}] = '{eval var=$example.body|addslashes|replace:"\r":""|replace:"\n":"\\n"}';
{/foreach}

{literal}

function toggleType(type) {
	if (type == 'customLetter') {
		document.getElementById('customLetter').style.display = 'block';
		document.getElementById('madlib').style.display = 'none';
		document.getElementById('letterType').value = 'customLetter';
		document.getElementById('madlibsElement').selectedIndex = 0;
	} else {
		document.getElementById('customLetter').style.display = 'none';
		document.getElementById('madlib').style.display = 'block';
		document.getElementById('letterType').value = 'madlib';
	}
}

function setExample(key) {
	toggleType('customLetter');
	var ta = document.getElementById('customLetterElement');

	if (!ta.value || confirm('Are you sure you want to overwrite your custom letter with this example?')) {
		ta.value = examples[key];
	}
}

function setMadlib(key) {
	var obj;
	key--;
	toggleType('madlib');	
	for (var x = 0; x < {/literal}{$madlibs|@count}{literal}; x++) {
		obj = document.getElementById('madlib'+x).style.display = (x != key) ? 'none' : 'block';
	}
}
{/literal}

</script>


<table align="center">
<tr>
	<td><div>
		<u>Examples:</u>
		<div style="margin:0px 10px 0px 10px;">
			{foreach from=$examples key="key" item="example"}
				<a href="javascript://" onclick="setExample({$key});">{$example.title}</a><br />
			{foreachelse}
				Sorry, there are no examples for this combination of competition type and invitation letter type.
			{/foreach}
		</div>
	</div></td>
	<td align="center">
		<h2>{$newcomp.invitationType|niceText} Letter</h2>
		
		<table style="border:1px solid #AAAAAA;background-color:#CCCCCC;" width="350">
		<tr>
			<td><div>
				<!-- <a href="javascript://" onclick="toggleType('madlib');">Use a MadLib!</a> -->

				{if $madlibs|@count}
					<select id="madlibsElement" name="newcomp[selectedMadlib]" onchange="setMadlib(this.selectedIndex);">
						<option value="">(Select a MadLib)</option>
					{foreach from=$madlibs item="madlib"}
						<option value="{$madlib.id}">{$madlib.title}</option>
					{/foreach}
					</select>
				{else}
					No Madlibs Available!
				{/if}
				&nbsp; | &nbsp; 
				<a href="javascript://" onclick="toggleType('customLetter');">Create My Own</a>
			</div></td>
		</tr>
		<tr>
			<td align="left">
				<div id="madlib" align="left" style="padding:8px;border:1px solid #CCCCCC;background-color:#FFFFFF;display:none;">
					{foreach from=$madlibs key="key" item="madlib"}
						<div id="madlib{$key}" style="display:none;">{eval var=$madlib.body|replace:"\n":"<br />"}</div>
					{/foreach}
					</div>

				<div id="customLetter" style="display:block;">
					<textarea id="customLetterElement" name="newcomp[customLetter]" rows="20" cols="40" class="textarea"></textarea></div>
			</td>
		</tr>
		</table>
	</td>
	<td>
	</td>
</tr>
</table>

{stored_object_fill_form object=$newcomp name="newcomp"}

</form>