<?
  session_start();
  
	include("class.php");
	include("topic.php");
	@include("article.php");
	// session_start();
	
	if (!$_SESSION['user'])
		header("Location: index.php");
		
	$classes = getActiveClasses(true, $_SESSION['user']);
	$duplicate = false;
	
	if ($_POST['selClass']) {
		$title = $_POST['articlename'];
		$author = $_POST['author1'];
		$author2 = $_POST['author2'];
		$source = $_POST['source'];
		$date = $_POST['month']."/".$_POST['day']."/".$_POST['year'];
		$url = $_POST['url'];
		$language = $_POST['language'];
		$summary = $_POST['summary'];
		if (!get_magic_quotes_gpc()) {
			$title = addslashes($title);
			$author = addslashes($author);
			$author2 = addslashes($author2);
			$source = addslashes($source);
			$url = addslashes($url);
			$language = addslashes($language);
			$summary = addslashes($summary);
		}
		$artid = addArticle($_SESSION['user'], $_POST['selClass'], $title, $author, $author2, $source, $date, $url, $language, $summary);
		$position = $_POST['selPosition'];
		$max = count(getReasons($position, false));
		for ($i = 0; $i < $max; $i++) {
			if ($_POST['reasons'.$position.'_'.$i])
				associateReason($artid, $_POST['reasons'.$position.'_'.$i]);
		}
		if (isset($_POST['reasons'.$position.'_other1'])) {
			if (!get_magic_quotes_gpc())
				$_POST['reasons'.$position.'_other1text'] = addslashes($_POST['reasons'.$position.'_other1text']);
			$reasonid = addReason($position, $_POST['reasons'.$position.'_other1text'], true);
			associateReason($artid, $reasonid);
		}
		if (isset($_POST['reasons'.$position.'_other2'])) {
			if (!get_magic_quotes_gpc())
				$_POST['reasons'.$position.'_other2text'] = addslashes($_POST['reasons'.$position.'_other2text']);
			$reasonid = addReason($position, $_POST['reasons'.$position.'_other2text'], true);
			associateReason($artid, $reasonid);
		}
		$article = getArticle($artid);
		if ($article['Duplicate'] == 1)
			$duplicate = true;
		$ready = true;	
	}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Article Submission</title>

<script>
<!--
	function changeClass(classid) {
		oldclass = document.getElementById('selClass').value;
		oldtopic = document.getElementById('selTopic').value;
		oldposition = document.getElementById('selPosition').value;
		document.getElementById('nopositions').style.display = 'none';
		document.getElementById('topics'+oldclass+'div').style.display = 'none';
		document.getElementById('positions'+oldtopic+'div').style.display = 'none';
		if (oldposition > 0)
			document.getElementById('reasons'+oldposition+'div').style.display = 'none';
		document.getElementById('topics'+classid+'div').style.display = 'block';
		document.getElementById('selClass').value = classid;
		topic = document.getElementById('topics'+classid);
		topicid = topic.options[topic.selectedIndex].value;
		document.getElementById('positions'+topicid+'div').style.display = 'block'; 
		document.getElementById('selTopic').value = topicid;
		var positionid = null;
		var i = 1;
		while ((position = document.getElementById('positions'+topicid+'_'+i)) != null) {
			if (position.checked) {
				positionid = position.value;
				break;
			}
			else
				i++;
		}
		if (!positionid) {
			document.getElementById('nopositions').style.display = 'block';
			positionid = -1;
		}
		else
			document.getElementById('reasons'+positionid+'div').style.display = 'block';
		document.getElementById('selPosition').value = positionid;
	}
	
	function changeTopic(topicid) {
		oldtopic = document.getElementById('selTopic').value;
		oldposition = document.getElementById('selPosition').value;
		document.getElementById('nopositions').style.display = 'none';
		document.getElementById('positions'+oldtopic+'div').style.display = 'none';
		if (oldposition > 0)
			document.getElementById('reasons'+oldposition+'div').style.display = 'none';
		document.getElementById('positions'+topicid+'div').style.display = 'block'; 
		document.getElementById('selTopic').value = topicid;
		var positionid = null;
		var i = 1;
		while ((position = document.getElementById('positions'+topicid+'_'+i)) != null) {
			if (position.checked) {
				positionid = position.value;
				break;
			}
			else
				i++;
		}
		if (!positionid) {
			document.getElementById('nopositions').style.display = 'block';
			positionid = -1;
		}
		else
			document.getElementById('reasons'+positionid+'div').style.display = 'block';
		document.getElementById('selPosition').value = positionid;
	}
	
	function changePosition(positionid) {
		oldposition = document.getElementById('selPosition').value;
		if (oldposition > 0)
			document.getElementById('reasons'+oldposition+'div').style.display = 'none';
		document.getElementById('reasons'+positionid+'div').style.display = 'block';
		document.getElementById('selPosition').value = positionid;			
	}
	
	function checkSubmit() {
		title = document.getElementById('articlename').value == '' ||
				document.getElementById('articlename').value == null;
		author = document.getElementById('author1').value == '';
		source = document.getElementById('source').value == '';
		date = (''+document.getElementById('month').value+document.getElementById('day').value+
				document.getElementById('year').value) == '';
		summary = document.getElementById('summary').value == '';
		curPos = document.getElementById('selPosition').value;
		i = 0;
		blank = true;
		while ((check = document.getElementById('reasons'+curPos+'_'+i)) != null) {
			if (check.checked) {
				blank = false;
				break;
			}
			i++;
		}
		if (document.getElementById('reasons'+curPos+'_other1').checked &&
			document.getElementById('reasons'+curPos+'_other1text').value != '')
			blank = false;
		if (document.getElementById('reasons'+curPos+'_other2').checked &&
			document.getElementById('reasons'+curPos+'_other2text').value != '')
			blank = false;
		if (title || author || source || date || summary || blank) {
			document.getElementById('missing').style.display = 'block';
			return false;
		}
		return true;
	}
	
	function goBack() {
		window.location = 'index.php';
	}
// -->
</script>

</head>

<? include("header.php"); ?>
<span style='color:#FF3300; display:<? if ($ready) echo("block"); else echo("none"); ?>' >
Your article has been submitted successfully. You may enter another or click Home to stop.</span>
<span id='duplicate' style='color:#FF3300; display:<? if ($duplicate) echo("block"); else echo("none"); ?>'>
The article you have submitted may be a duplicate of an article already in the database and has been
 flagged for inspection by your instructor.</span>
<h3>Article Submission</h3>
<form id='submittal' name='submittal' onsubmit='return checkSubmit()' method='POST'>
<table>
	<tr>
		<td align='right' style='font-weight:bold'>Select Class:</td>
		<td align='left'>
		<select id='classlist' name='classlist' 
				onchange='changeClass(this.options[this.selectedIndex].value)'>
		<?
			foreach($classes as $c) {
				if (count(getTopics($c['ID'])) > 0)
					echo("<option value='".$c['ID']."'>".$c['CourseName']."</option>");
			}		
		?>
		</select>
		</td>
	</tr>
	<tr>
		<td align='right' style='font-weight:bold'>Select Topic:</td>
		<td align='left'>
		<?
			$first = true;
			foreach($classes as $c) {
				if (count(getTopics($c['ID'])) > 0) {
					$topics = getTopics($c['ID']);
					echo("<div id='topics".$c['ID']."div' style='display:");
					if ($first) {
						echo("block'>");
						$first = false;
						$selclass = $c['ID'];
						$seltopic = $topics[0]['ID'];
					}
					else
						echo("none'>");
					echo("<select id='topics".$c['ID']."' name='topics".$c['ID']."' ");
					echo("onchange='changeTopic(this.options[this.selectedIndex].value)'>");
					$first2 = true;
					foreach($topics as $t) {
						if (count(getPositions($t['ID'])) > 0) {
							echo("<option value='".$t['ID']."'");
							if ($first2) {
								echo("SELECTED");
								$first2 = false;
							}
							echo(">".$t['Topic']."</option>");
						}
					}
					echo("</select></div>");
				}
			}
			echo("<input type='hidden' id='selClass' name='selClass' value='$selclass' />");
			echo("<input type='hidden' id='selTopic' name='selTopic' value='$seltopic'>");
		?>
		</td>
	</tr>
	<tr><td><br /></td></tr>
	<tr>
		<td align='right' style='font-weight:bold'>Title of Article:</td>
		<td align='left'><input type='text' id='articlename' name='articlename' value='' size='50' />
			<span style='color:#FF3300'>*</span></td>
	</tr>
	<tr>
		<td align='right' style='font-weight:bold'>Author:</td>
		<td align='left'><input type='text' id='author1' name='author1' value='' size='30'  />
			<span style='color:#FF3300'>*</span></td>
		<td align='left'>(Enter as Lastname, Firstname)</td>
	</tr>
	<tr>
		<td align='right' style='font-weight:bold'>Author:</td>
		<td align='left'><input type='text' id='author2' name='author2' value=' ' size='30'  /></td>
		<td align='left'>(for articles with two authors)</td>
	</tr>
	<tr>
		<td align='right' style='font-weight:bold'>Source:</td>
		<td align='left'><input type='text' id='source' name='source' value='' size='40' />
			<span style='color:#FF3300'>*</span></td>
		<td align='left'>(name of newspaper, magazine, etc.)</td>
	</tr>
	<tr>
		<td align='right' style='font-weight:bold'>Date of Article:</td>
		<td align='left'><input type='text' id='month' name='month' maxlength=2 size=2 value=''/> /
						 <input type='text' id='day' name='day' maxlength=2 size=2 value=''/> /
						 <input type='text' id='year' name='year' maxlength=4 size=4 value=''/>
			<span style='color:#FF3300'>*</span></td>
		<td align='left'>(e.g. 2/24/2007)</td>
	</tr>
	<tr>
		<td align='right' style='font-weight:bold'>Stable URL:</td>
		<td align='left'><input type='text' id='url' name='url' value=''/></td>
		<td align='left'>(unique address for this article)</td>
	</tr>
	<tr>
		<td align='right' style='font-weight:bold'>Language:</td>
		<td align='left'><input type='text' id='language' name='language' /></td>
	</tr>
	<tr><td><br /></td></tr>
	<tr>
		<td style='font-weight:bold'>Select position:</td>
	</tr>
	<tr>
		<td colspan=4>
		<span style='color:#FF3300; position:relative; left:20px; display:none' id='nopositions'>
			There are no valid positions currently assigned to this topic.  Please select a different topic.</span>
		<?
			$first = true;
			foreach($classes as $c) {
				$topics = getTopics($c['ID']);
				foreach($topics as $t) {
					$positions = getPositions($t['ID']);
					echo("<div id='positions".$t['ID']."div' style='position:relative; left:20px; display:");
					if ($first) {
						echo("block'>");
						$first = false;
						$selposition = $positions[0]['ID'];
					}
					else
						echo("none'>");
					echo("<table><tr>");
					$first2 = true;
					$j = 1;
					for($i = 0; $i < count($positions); $i++) {
						if ($i % 3 === 0)
							echo("</tr><tr>");
						echo("<td><input type='radio' name='positions".$t['ID']."' value='".$positions[$i]['ID'].
							 "' onclick='changePosition(this.value)' id='positions".$t['ID']."_".$j."' ");
						if ($first2) {
							echo("CHECKED");
							$first2 = false;
						}
						echo(" />".$positions[$i]['Position']." &nbsp; &nbsp;</td>");
						$j++;
					}
					echo("</tr></table></div>");
				}
			}
			echo("<input type='hidden' id='selPosition' name='selPosition' value=$selposition />");
		?>
		</td>
	</tr>
	<tr><td><br /></td></tr>
	<tr>
		<td colspan=2 style='font-weight:bold;'>Select all reasons that apply:<span style='color:#FF3300'>*</span></td>
	</tr>
	<tr>
		<td colspan=4>
		<?
			$first = true;
			foreach($classes as $c) {
				$topics = getTopics($c['ID']);
				foreach($topics as $t) {
					$positions = getPositions($t['ID']);
					foreach($positions as $p) {
						$reasons = getReasons($p['ID'], false);
						echo("<div id='reasons".$p['ID']."div' style='position:relative; left: 20px; display:");
						if ($first) {
							echo("block'>");
							$first = false;
						}
						else
							echo("none'>");
						for ($i = 0; $i < count($reasons); $i++) {
							echo("<input type='checkbox' id='reasons".$p['ID']."_".$i."' name='reasons".$p['ID']."_".$i.
								 "' value='".$reasons[$i]['ID']."'> ".$reasons[$i]['Reason']."<br>");
						}
						if ($p['AllowOther'] == 1) {
							echo("<input type='checkbox' id='reasons".$p['ID']."_other1' name='reasons".$p['ID'].
								 "_other1' value='other1'> Other <input type='text' id='reasons".$p['ID']."_other1text' ".
								 "name='reasons".$p['ID']."_other1text'><br>");
							echo("<input type='checkbox' id='reasons".$p['ID']."_other2' name='reasons".$p['ID'].
								 "_other2' value='other2'> Other <input type='text' id='reasons".$p['ID']."_other2text' ".
								 "name='reasons".$p['ID']."_other2text'><br>");
						}
						echo("</div>");
					}
				}
			}
		?>
		</td>
	</tr>
	<tr><td><br /></td></tr>
	<tr>
		<td colspan=2 style='font-weight:bold'>Summary of argument:<span style='color:#FF3300'>*</span></td>
	</tr>
	<tr>
		<td colspan=3><textarea id='summary' name='summary' cols='60' rows='10' style='position:relative; left:20px'></textarea></td>
	</tr>
	<tr>
		<td align='right'><input type='submit' value='Submit' style='position:relative; left:40px' /> 
			<input type='button' value='Home' onclick='goBack()' style='position:relative; left:40px' /></td>
		<td><span id='missing' style='color:#FF3300; display:none; position:relative; left:40px'>A required field is missing!</span></td>
	</tr>
</table>
</form>
<br />
<? include("footer.php"); ?>
</html>
