<?php
	/******************************************************************/
	/*檔名:registration.php															*/
	/*說明:論文作者註冊作業															*/
	/*相關檔案:																			*/
	/*			register_ok.php														*/
	/*			register_enable.php													*/
	/* 		connection.php															*/
	/* 		Smarty.php																*/	
	/* 		function.mail.php														*/
	/* 		html.lib.php															*/
	/* 																					*/			
	/******************************************************************/
	session_start();
	
	require_once 'connection.php';
	
	$query_Recordset = "SELECT SubmitDateBegin,SubmitDateEnd,SubmitExtend FROM ConferenceInfo Where ConferenceID='".$_SESSION['conf_id']."'";
	$Recordset = mysql_query($query_Recordset, $conn) or die(mysql_error());
	$row_Recordset = mysql_fetch_assoc($Recordset);
	
	if (time() >= mktime (0,0,0,intval(substr($row_Recordset['SubmitDateEnd'],5,2)),intval(substr($row_Recordset['SubmitDateEnd'],8,2))+intval($row_Recordset['SubmitExtend'])+1,intval(substr($row_Recordset['SubmitDateEnd'],0,4))))
	{
		header('Location: ./due_date.html');
		exit;
	}
	
	if (isset($_POST['submit']))
	{	// *** User submit the registration form
		$LoginRS_query = "SELECT LoginID FROM UserInfo WHERE LoginID='".$_POST['loginID']."'";
		$LoginRS = mysql_query($LoginRS_query, $GLOBALS['conn']) or die(mysql_error());
		$loginFoundUser = mysql_num_rows($LoginRS);
	
		// If there is a row in the database, the Login ID was found - can not add the requested Login ID
		if($loginFoundUser)
		{	// *** if this Login ID exists
			$message1 = 'The Login ID has existed in the Submission System!!';
			$message2 = 'Please change Login ID!!';
			show_page($message1, $message2);
			
			exit;
		}
		else
		{	// *** if this Login ID doesn't exist			
			$hashCode = crypt($_POST['password']);
			$insertSQL = sprintf("INSERT INTO UserInfo (LoginID,Password,LastName,MiddleName,FirstName,CourteousTitle,JobTitle,Affiliation,Department,Address,City,State,ZipCode,CountryID,Email,Email2,Email3,Phone,Fax,Specialty,UserType,HashCode,Date) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)",
								GetSQLValueString($_POST['loginID'], 'text'),
								GetSQLValueString($_POST['password'], 'text'),
								GetSQLValueString($_POST['lastName'], 'text'),
								GetSQLValueString($_POST['middleName'], 'text'),								
								GetSQLValueString($_POST['firstName'], 'text'),
								GetSQLValueString($_POST['courteousTitle'], 'text'),
								GetSQLValueString($_POST['jobTitle'], 'text'),
								GetSQLValueString($_POST['affiliation'], 'text'),
								GetSQLValueString($_POST['department'], 'text'),
								GetSQLValueString($_POST['address'], 'text'),
								GetSQLValueString($_POST['city'], 'text'),
								GetSQLValueString($_POST['state'], 'text'),
								GetSQLValueString($_POST['zipCode'], 'text'),
								GetSQLValueString($_POST['countryID'], 'text'),
								GetSQLValueString($_POST['email'], 'text'),
								GetSQLValueString($_POST['email2'], 'text'),
								GetSQLValueString($_POST['email3'], 'text'),
								GetSQLValueString($_POST['phone'], 'text'),
								GetSQLValueString($_POST['fax'], 'text'),
								GetSQLValueString($_POST['specialty'], 'text'),
							   GetSQLValueString('Author', 'text'),
							   GetSQLValueString($hashCode, 'text'),
							   GetSQLValueString(date('Y-m-d'), 'date'));
		
			mysql_query($insertSQL, $GLOBALS['conn']) or die(mysql_error());
			
			$serverDir = 'http://'.$_SERVER['SERVER_NAME'].substr($_SERVER['SCRIPT_NAME'],0,strrpos($_SERVER['SCRIPT_NAME'],'/'));
			
			// 系統自動發信: 傳送郵件通知論文作者註冊成功
			require_once 'email_lib.php';
			notice_registration($_POST['lastName'],$_POST['firstName'],$_POST['courteousTitle'],$_POST['loginID'],$_POST['email'],$hashCode,$serverDir);
			
			$context = '<div align="left">Dear '.$_POST['courteousTitle'].' '.$_POST['lastName'].', '.$_POST['firstName'].' '.$_POST['middleName'].' ,<br><br>You have successfully completed the registration process of the Login ID: '.$_POST['loginID'].'.<br><br>Please use this <a href='.$serverDir.'/register_enable.php?loginID='.$_POST['loginID'].'&comeFrom=porgram&hashCode='.$hashCode.'&conf_alias='.$_SESSION['conf_alias'].'><font color=#FF0000>hyperlink</font></a> to enable your Login ID in '.$_SESSION['conf_shortName'].' Paper Submission System.<br></div>';
			header('Location: register_ok.php?context='.urlencode($context));
			exit;
		}
	}
	else
	{
		show_page();
	}

//--------------------------------------------------------------------------------------------------
	function show_page($message1 = "", $message2 = "") 
	{
		require_once 'Smarty.php';
		require_once 'html.lib.php';

		if(!isset($_SESSION['conf_alias']))
		{
			$_SESSION['conf_alias'] = $_GET['conf_alias'];
		
			$query_Recordset = "SELECT ConferenceID,FullName,ShortName,MailBox,ContactEmail,SupportEmail FROM ConferenceInfo Where alias='".$_SESSION['conf_alias']."'";
			$Recordset = mysql_query($query_Recordset, $GLOBALS['conn']) or die(mysql_error());
			$row_Recordset = mysql_fetch_assoc($Recordset);
		
			$_SESSION['conf_id'] = $row_Recordset['ConferenceID'];
			$_SESSION['conf_fullName'] = $row_Recordset['FullName'];
			$_SESSION['conf_shortName'] = $row_Recordset['ShortName'];
			$_SESSION['conf_mailBox'] = $row_Recordset['MailBox'];
			$_SESSION['conf_contactEmail'] = $row_Recordset['ContactEmail'];
			$_SESSION['conf_supportEmail'] = $row_Recordset['SupportEmail'];
		}
		
		$smarty->debugging = FALSE;
		$smarty->assign('message1', $message1);
		$smarty->assign('message2', $message2);
		$smarty->assign('loginIDCheck', Form_Button('CheckButton', 'Check Duplicate Login ID!', "onClick='window.open(\"./loginID_check.php?checkuser=\" + this.form.loginID.value ,\"checkuser\",\"width=240,height=130,status=0,scrollbars=0,resizable=1,menubar=0,toolbar=0,location=0\")'"));
		
	   // find the country of applicant
		$CountryRS_query = 'select CountryID,CountryName from Country';
		$CountryRS = mysql_query($CountryRS_query, $GLOBALS['conn']) or die(mysql_error());
		
		$CountryArray[''] = '-- Please select your Country --';
	   while(List($CountryID,$CountryName) = mysql_fetch_row($CountryRS))
	   {
	      $CountryArray[$CountryID] = $CountryName;
	   }
	   
	   if(isset($_POST['countryID']))
	      $countryID = $_POST['countryID'];
	   else
	      $countryID = '';
	      
		$smarty->assign('country', Form_Select('countryID',$countryID,$CountryArray,''));

		$smarty->display('registration.tpl.html');
	}	
?>