<?php
	include('/var/www/html/uclit/protected/common/login/check.php');
	include('/var/www/html/uclit/protected/common/css_login/css_head2.php');
	include('/var/www/html/uclit/protected/connections/db_work.php');
	mysql_select_db($database_workshop, $sqlConn) or die("Could not select database");
		$m = $_GET['mas_id'];
		$cap = $_GET['cap'];
		$size = $_GET['size'];
		$class = $_GET['class'];
		$mode = $_GET['mode'];
		$class = $_GET['name'];
		
	function display_student()
	{
		$m = $_GET['mas_id'];
		$cap = $_GET['cap'];
		$size = $_GET['size'];
		$class = $_GET['class'];
		$mode = $_GET['mode'];
		include('/var/www/html/uclit/protected/connections/db_work.php'); 
		mysql_select_db($database_workshop, $sqlConn) or die("Could not select database");
		
		$sqlQuerry = "SELECT registrants.reg_id,registrants.reg_name,registrants.reg_email		
					FROM
						worshopmaster_registrant, registrants
					WHERE
						worshopmaster_registrant.master_id='$m' and
						worshopmaster_registrant.reg_wait_list='0' and
						worshopmaster_registrant.reg_id=registrants.reg_id
					ORDER BY
						registrants.reg_name	
						";
		$QuerryResult = mysql_query($sqlQuerry) or die("SELECT query failed from registrants and workshopmaster_registrant");
			$count = 0;
			$count3 = 0;
			$id = array();
			$name = array();
			while($row = @mysql_fetch_array($QuerryResult))
			{
				$id[$count] = $row['reg_id'];
				$name[$count] = $row['reg_name'];
				$email[$count] = $row['reg_email'];
				$count++;
			}
			$flag = 0;
			for($row=0;$row<10;$row++)
			{
				echo "<tr>";
				for($col=0;$col<5;$col++)
				{
					if($count3 < $size && $name[$count3] != "")
					{
						if($mode == "email")	
							echo "<td width='25%'><input type='checkbox' name='namelist[]' value='$id[$count3]' checked/>$name[$count3] ($email[$count3])</td>";
							
						elseif($mode == "feedback")	
							echo "<td width='25%'><input type='checkbox' name='namelist[]' value='$id[$count3]' checked/>$name[$count3] ($email[$count3])</td>";
						else
								echo "<td width='25%'><input type='checkbox' name='namelist[]' value='$id[$count3]' />$name[$count3]</td>";
						$count3++;
					 }
					 else
					 {//stop make checkboxes
					 	$flag = 1;
						break;
					 } 
				}
				 echo "</tr>";
				if($flag == 1)
				{//exit this loop
					break;
				}
			}
	}// end function
	
	function display_waitlist()
	{
		$m = $_GET['mas_id'];
		$cap = $_GET['cap'];
		$size = $_GET['size'];
		$class = $_GET['class'];
		$mode = $_GET['mode'];
		include('/var/www/html/uclit/protected/connections/db_work.php'); 
		mysql_select_db($database_workshop, $sqlConn) or die("Could not select database");
		
		$sqlQuerry = "SELECT registrants.reg_id,registrants.reg_name,registrants.reg_email		
					FROM
						worshopmaster_registrant, registrants
					WHERE
						worshopmaster_registrant.master_id='$m' and
						worshopmaster_registrant.reg_wait_list='1' and
						worshopmaster_registrant.reg_id=registrants.reg_id
					ORDER BY
						registrants.reg_name	
						";
		$QuerryResult = mysql_query($sqlQuerry) or die("SELECT query failed from registrants and workshopmaster_registrant");
			$count = 0;
			$count3 = 0;
			$id = array();
			$name = array();
			while($row = @mysql_fetch_array($QuerryResult))
			{
				$id[$count] = $row['reg_id'];
				$name[$count] = $row['reg_name'];
				$email[$count] = $row['reg_email'];
				$count++;
			}
			$flag = 0;
			for($row=0;$row<10;$row++)
			{
				echo "<tr>";
				for($col=0;$col<5;$col++)
				{
					if($count3 < $size && $name[$count3] != "")
					{
						if($mode == "email")	
							echo "<td width='25%'><input type='checkbox' name='namelist[]' value='$id[$count3]' checked/>$name[$count3] ($email[$count3])</td>";
							
						elseif($mode == "feedback")	
							echo "<td width='25%'><input type='checkbox' name='namelist[]' value='$id[$count3]' checked/>$name[$count3] ($email[$count3])</td>";
						elseif($mode == "waitlist")	
							echo "<td width='25%'><input type='checkbox' name='namelist[]' value='$id[$count3]' checked/>$name[$count3] ($email[$count3])</td>";
						else
								echo "<td width='25%'><input type='checkbox' name='namelist[]' value='$id[$count3]' />$name[$count3]</td>";
						$count3++;
					 }
					 else
					 {//stop make checkboxes
					 	$flag = 1;
						break;
					 } 
				}
				 echo "</tr>";
				if($flag == 1)
				{//exit this loop
					break;
				}
			}
	}// end function
	
	if(isset($_GET['mas_id']) && $_GET['mas_id'] != "")
	{
		
		if($mode == "attendance")
		{
			//echo "attendance, m= $m<br />";
			echo "
				<script type='text/javascript'>
				window.location='http://ul.uc.edu/uclit/protected/workshop/review.php?mas_id=$m&class=$class';
				</script>
			";
		}
		else if($mode == "email")
		{
			//echo "email, m= $m<br />";
			echo "<strong>Email Registrant(s)</strong><br /><hr />";
			echo "
				<form name='attend' method='POST' action='email.php' enctype='multipart/form-data'>
				<table border='0'>
				";
				display_student();			
			echo "
				</table>
				<strong>Subject:</strong><br />
				<input type='text' name='subject' value='' />
				<br /><strong>Body:</strong><br />
				<textarea type='text' name='body' cols='40' rows='10'></textarea>
				<br />
				 <!-- MAX_FILE_SIZE must precede the file input field -->
    			<input type='hidden' name='MAX_FILE_SIZE' value='100000' />
   				 <!-- Name of input element determines name in $_FILES array -->
 				<br /><strong>Attach File:</strong><br />
				<input name='attachment' type='file' id='attachment' size='20 maxlength=100> 
				<br />
				<input type='hidden' name='size' value='$size' />
				<input type='hidden' name='capacity' value='$cap' />
				<input type='hidden' name='mode' value='email' />
				<hr />
				<input type='button' name='getemail' value='Send Email' onClick='CheckContent()'/>
				<input type='reset' name='resetemail' value='Reset' />
				<input type='button' name='back' value='Back' onClick='GoBack()'/>
				</form>
				";			
		}
		else if($mode == "waitlist")
		{
			//echo "email, m= $m<br />";
			echo "<strong>Email Waitlist Form</strong><br /><hr />";
			echo "
				<form name='attend' method='POST' action='email.php'>
				<table border='0'>
				";
				display_waitlist();	
				
				$sqlURL = "Select workshop_master.master_id, workshop_detail.w_survey
From workshop_detail Inner Join workshop_master ON workshop_detail.w_id = workshop_master.w_id
Where '$m' = workshop_master.master_id ";
				$URLResult = mysql_query($sqlURL) or die("SELECT query failed from registrants and 				workshopmaster_registrant");
				while($row = @mysql_fetch_array($URLResult))
				{
				$URL = $row['w_survey'];
				}
				
				
						
			echo "
				</table>
				<strong>Subject:</strong><br />
				<input type='text' name='subject' value='' />
				<br /><strong>Body:</strong><br />
				<textarea type='text' name='body' cols='40' rows='10'></textarea>
				<br />
				<input type='hidden' name='size' value='$size' />
				<input type='hidden' name='capacity' value='$cap' />
				<input type='hidden' name='mode' value='feedback' />
				<hr />
				<input type='button' name='getemail' value='Send Email' onClick='CheckContent()'/>
				<input type='reset' name='resetemail' value='Reset' />
				<input type='button' name='back' value='Back' onClick='GoBack()'/>
				</form>
				";			
		}
		
		
		
		
		else if($mode == "feedback")
		{
			//echo "email, m= $m<br />";
			echo "<strong>Email Registrant(s) Feedback Form</strong><br /><hr />";
			echo "
				<form name='attend' method='POST' action='email.php'>
				<table border='0'>
				";
				display_student();	
				
				$sqlURL = "Select workshop_master.master_id, workshop_detail.w_survey
From workshop_detail Inner Join workshop_master ON workshop_detail.w_id = workshop_master.w_id
Where '$m' = workshop_master.master_id ";
				$URLResult = mysql_query($sqlURL) or die("SELECT query failed from registrants and 				workshopmaster_registrant");
				while($row = @mysql_fetch_array($URLResult))
				{
				$URL = $row['w_survey'];
				}
				
				
						
			echo "
				</table>
				<strong>Subject:</strong><br />
				<input type='text' name='subject' value='' />
				<br /><strong>Body:</strong><br />
				<textarea type='text' name='body' cols='40' rows='10'></textarea>
				<br />
				Attach Feedback URL:<br /> $URL<br />
				<input type='radio' name='survey' value='yes' checked>Yes
				<input type='radio' name='survey' value='no'>No<br> 
			
				
				<input type='hidden' name='url' value='$URL' />
				<input type='hidden' name='size' value='$size' />
				<input type='hidden' name='capacity' value='$cap' />
				<input type='hidden' name='mode' value='feedback' />
				<hr />
				<input type='button' name='getemail' value='Send Email' onClick='CheckContent()'/>
				<input type='reset' name='resetemail' value='Reset' />
				<input type='button' name='back' value='Back' onClick='GoBack()'/>
				</form>
				";			
		}
		else
		{
			echo "Invalid entry to this page<br />
				Please return to <a href='workshop_detail.php'>workshop detail</a>.<br />
			";
		}
	}
	else
	{
		echo "Invalid entry to this page<br />
				Please return to <a href='workshop_detail.php'>workshop detail</a>.<br />
		";
	}
	include('/var/www/html/uclit/protected/common/css_login/css_foot2.php'); 
	
	?>
	<script type="text/javascript">
	function CheckContent()
	{
		var x = document.attend.subject.value;
		var y = document.attend.body.value;
		var msg = "Invalid field(s):\n";
		var count = 0;
		
		if(x == "")
		{
			msg += "Subject\n";
			count++;
		}
		if(y == "")
		{
			msg += "Body\n";
			count++;
		}
		if(count == 0)
		{
			document.attend.submit();
		}	
		else
		{
			window.alert(msg);		
		}	
	}
	function GoBack()
	{
		window.location = "workshop_detail.php";
	}
	</script>
