<?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");
	
	if(isset($_GET['m_id']) && isset($_GET['r_id']) && isset($_GET['mas_reg_id']) && $_GET['r_id'] != "" && $_GET['mas_reg_id'] != "" && $_GET['m_id'] != "")
	{
		$reg_id = $_GET['r_id'];
		$mas_id = $_GET['m_id'];
		$mas_reg_id = $_GET['mas_reg_id'];
		$class = $_GET['class'];
		//delete a record from workshopmaster_registrant
		$sqlQuerry = "DELETE FROM worshopmaster_registrant WHERE mas_reg_id='$mas_reg_id'";
		$QuerryResult = mysql_query($sqlQuerry) or die("SELECT query failed from worshopmaster_registrant");
		
		//delete this registrant from the registrants table
		$sqlQuerry = "DELETE FROM registrants WHERE reg_id='$reg_id'";
		$QuerryResult = mysql_query($sqlQuerry) or die("SELECT query failed from worshopmaster_registrant");
		
	
		//check if there is a wait list for this class
		$sqlWQuerry = "SELECT * FROM worshopmaster_registrant WHERE master_id='$mas_id' and reg_wait_list='1'";
		$WQuerryResult = mysql_query($sqlWQuerry) or die("SELECT query failed from worshopmaster_registrant");

		$flag = 0;
		while($row = @mysql_fetch_array($WQuerryResult))
		{
			$mas_reg_id = $row['mas_reg_id'];
			$wait_reg_id = $row['reg_id'];
			//update this record so that it is under the actual list
			$sqlUQuerry = "UPDATE worshopmaster_registrant SET reg_wait_list='0' WHERE mas_reg_id='$mas_reg_id'";
			$UQuerryResult = mysql_query($sqlUQuerry) or die("SELECT query failed from workshopmaster_registrant");
			
			$flag = 1;
			break;
		}
		if($flag == 1)
		{
			//selecting all current record for this quarter
			$sqlWorkshopQuerry = "SELECT 
							workshop_detail.w_name, quarters.quarter, workshop_master.time, workshop_master.date, location.location
						FROM
							quarters, workshop_master, location, workshop_detail
						WHERE
							workshop_master.master_id='$mas_id' and
							workshop_detail.w_id=workshop_master.w_id and
							location.loc_id=workshop_master.loc_id and
							quarters.quarter_id=workshop_master.quarter_id
							";
							
			$WorkshopQuerryResult = mysql_query($sqlWorkshopQuerry) or die("SELECT query failed");
			$count = 0;
			while($q_row = @mysql_fetch_array($WorkshopQuerryResult))
			{
				$w_name = $q_row['w_name'];
				$w_time = $q_row['time'];
				$w_date = $q_row['date'];
				$w_loc = $q_row['location'];
				$w_quart = $q_row['quarter'];
			}
			//sending email to the registrant that is added to the actual class list
			$sqlEmail = "SELECT reg_email FROM registrants WHERE reg_id='$wait_reg_id'";
			$EmailResult = mysql_query($sqlEmail) or die("SELECT query failed from registrants");
			$email = "";
			while($row = @mysql_fetch_array($EmailResult))
			{
				$email = $row['reg_email'];
				if($email != "")
				{
					$mail_to = $email;
					$mail_subj = "Workshop registration from Langsam Library";
					$mail_header = "From: ulinstruction@mail.libraries.uc.edu\r\n";
					$mail_header .= "X-Mailer: server\r\n";
					$mail_header .= "Reply To: ulinstruction@mail.libraries.uc.edu\r\n";
					$sendmail_param = "ulinstruction@mail.libraries.uc.edu";
					$mail_text = "You are registered for\nClass:$w_name\nDate: $w_date\nQuarter: $w_quart\nHour: $w_time\nLocation: $w_loc\nPlease call (556-1851) or email us (ulinstruction@mail.libraries.uc.edu)  if you have any questions or changes.  Thank you for your registration.\nLibrary Workshop Series\nUniversity Libraries\nulinstruction@mail.libraries.uc.edu";
					mail("$mail_to","$mail_subj","$mail_text","$mail_header","-f$sendmail_param");
					echo "Sent email to: $email<br />";
				}
			}
		}
		
		echo "Updated workshop successfull. 
			<script type='text/javascript'>
			window.location='http://ul.uc.edu/uclit/protected/workshop/review.php?mas_id=$mas_id&class=$class';
		</script>
		";
	}
	else
	{
		echo "Invalid entry in 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');

	
?>