<?php

//流程控制
switch ($action)
{   
   //友善列印篩選規劃書
   case "print_voice_table":
      print_voice_table();
      add_counter($action);
      break;   

   //友善列印篩選清單
   case "print_final_list":
      print_final_list();
      add_counter($action);
      break;   

   //友善列印課表
   case "print_final_timetable":
      print_final_timetable();
      add_counter($action);
      break;

   //最後動作，找時機將確定的所選項目寫回資料庫
   case "print_and_update":
      require("header.php");
      print_and_update();
      include("footer.php");
      add_counter($action);
      break;
	
   //輸出固定課表 與 排序後內容
   case "show_output":
      require("header.php");
      show_output();
      include("footer.php");
      add_counter($action);
      break;

   //排序所選課程
   case "sort_suit_course":
      require("header.php");
      sort_suit_course();
      include("footer.php");
      add_counter($action);
      break;

   //列出符合的時段供選擇 並 選定
   case "show_suit_time":
      require("header.php");
      show_suit_time();
      include("footer.php");
      add_counter($action);
      break;

   //秀出固定課表 & 篩選項目選擇
   case "show_timetable_2nd":
      require("header.php");
      show_timetable_2nd();
      include("footer.php");
      add_counter($action);
      break;
	
   //秀出固定課表 & 必修未選資料(警告) & 必修重修項目、擋修項目
   case "show_timetable_1st":
      require("header.php");
      show_timetable_1st();
      include("footer.php");
      add_counter($action);
      break;  
      
   //學生資料確認 & (詢問重修、擋修)
   case "confirm_std_data":
      require("header.php");
      confirm_std_data();
      include("footer.php");
      add_counter($action);
      break;
      
   //預設畫面(輸入學號)
   default:
      require("header.php");      
      input_std_num();
      include("footer.php");
      add_counter("default");
      break;
}

//---------------------------------------------
//預設畫面(輸入學號)
//---------------------------------------------
function input_std_num()
{
   global $PHP_SELF;

?>
<P><CENTER>(篩選服務由本「社團」中原網策會提供，<FONT COLOR=RED SIZE=3>並非中原大學的官方網站</FONT> 大家別搞錯唷 ^_____^ )<p>篩選輔助篩選系統開跑囉^^。 <FONT COLOR=RED SIZE=3> 12/22、12/23、12/25 篩選登記</FONT>，快來看看有哪些課可以選!<BR><BR><A HREF="http://140.135.16.161/voice_system/922選課相關資訊/選課即時最新訊息.htm" TARGET="_BLANK">官方網站最新公告</A></CENTER>
<FORM action=<? echo $PHP_SELF ?> method=post><INPUT type=hidden value=confirm_std_data 
name=action> 
    <center>
    <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="450" height="484">
      <tr>
        <td width="100%" valign="bottom"><div align="right">
  <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="350" id="AutoNumber3" background="title.jpg" height="100%">
    <tr>
      <td width="100%">　<p>　</p>
      <p>　</p>
      <p>　</p>
      <p>　</p>
      <p>　</p>
      <p>　</p>
      <p>　</p>
      <TABLE cellSpacing=0 cellPadding=3 border=0 style="border-collapse: collapse" bordercolor="#111111">
  <TBODY>
  <TR>
    <TD>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 您的學號: </TD>
    <TD valign="bottom">
    <INPUT size=9 name=std_num style="border: 1px ridge #6666FF; text-align:center"></TD>
    </TR></TBODY></TABLE><BR><TABLE id=AutoNumber1 style="BORDER-COLLAPSE: collapse" borderColor=#111111 
cellSpacing=0 cellPadding=5 width=338 border=1>
  <TBODY>
  <TR>
    <TD align=left width="326" bgColor=#ecfff5 style="line-height: 150%">您只要輸入<FONT 
      color=#ff0000><B>學號</B></FONT>，本系統將幫您整理出沒有衝堂、符合時段、性別、興趣的選修課程，讓您可以輕鬆的挑選您要選的課程。</TD></TR></TBODY></TABLE><p><P align=center><INPUT type=submit value=" 確  定 "> <INPUT type=reset value=" 重  設 "></P>
<P></P>
<P><img src="1-1.gif" align="middle" width="94" height="2"><br><img src="1-2.gif" width="2" height="20"><img border="0" src="http://www.zzz.com.tw/cgi-bin/counter/counter.pl?font=digits/neat&amp;inc=cna&amp;show=cna&amp;w=6" width="90" height="20" alt="計數器"><img src="1-3.gif" width="2" height="20"><br><img src="1-4.gif" align="middle" width="94" height="2"><br><img src="05.gif" width="125" height="8"><br></P>
</td>
      </tr>
    </table>
</td>
    </tr>
  </table>
</div>
</P>
</td>
      </tr>
    </table>
    </center>
  </div>
<P>
<P>
<CENTER>注意，您只需輸入學號即可，本系統將<b><font color="#ff0000" size="3">不會</font></b>要求您輸入 
<font color="#c0c0c0"><u>選課密碼</u></font> ，敬請放心！</CENTER></FORM><P><P>
<?php
	hot_rank_show2();
	echo "<HR>";
	hot_rank_show();
	/*
	if(getenv("REMOTE_ADDR")=="210.243.203.27")
	{
		$now = count(file("http://140.135.16.161/voice_system/91-1課程異動表.htm"));
		$host = "localhost";
   		$user = "root";
   		$pass = "";   
   		$database = "cycu";
   		
   		$conn=mysql_connect( $host, $user, $pass );
		$result=mysql_db_query( $database, "select num from counter where name = 'new_info'" , $conn);
		$row = mysql_fetch_row($result);
		if($row[0]!=$now) pass_message("新序號: ". $now . "，課程有異動!! 請處理");
	}
	*/
}

//---------------------------------------------
////學生資料確認 & (詢問重修、擋修)
//---------------------------------------------
function confirm_std_data()
{
	global $PHP_SELF, $std_num;
	
	if(!$std_num) error_message("請輸入學號!!");
   
   	$host = "localhost";
   	$user = "root";
   	$pass = "";   
   	$database = "cycu";
   		
   	$conn=mysql_connect( $host, $user, $pass );
	$result=mysql_db_query( $database, "select * from std912 where std_num = '$std_num'" , $conn);
	$row = @mysql_fetch_row($result);
	
	if(!$row) pass_message("找不到您指定的學號，請手動挑選您的班級!!");
	
	$std_class_next = $row[3];
	/*
	$std_class_next = str_replace("七", "八", $std_class_next);
	$std_class_next = str_replace("六", "七", $std_class_next);
	$std_class_next = str_replace("五", "六", $std_class_next);
	$std_class_next = str_replace("四", "五", $std_class_next);
	$std_class_next = str_replace("三", "四", $std_class_next);
	$std_class_next = str_replace("二", "三", $std_class_next);
	$std_class_next = str_replace("一", "二", $std_class_next);
	*/
	
	
?>
<form method="POST" action="<?php echo $PHP_SELF ?>">
<INPUT TYPE="hidden" NAME="action" VALUE="show_timetable_1st">
<INPUT TYPE="hidden" NAME="std_num" VALUE="<?php echo $row[0] ?>">
<INPUT TYPE="hidden" NAME="std_sex" VALUE="<?php echo $row[2] ?>">
<?
	if($row)
	{
		if($row[2]==1){
			echo "<p align=center>歡迎 <font color=#FF6600>$row[3]</font> 的 <b><font size=3 color=#1E90FF>$row[1]</font></b> 帥哥 使用，";
		}else{
			echo "<p align=center>歡迎 <font color=#FF6600>$row[3]</font> 的 <b><font size=3 color=#FF00FF>$row[1]</font></b> 美女 使用，";	
		}
	}
	else
	{
			echo "<p align=center>歡迎使用本系統，請挑選您的班級！<P>";
	}
/*	
	if($row[5]==0){
		echo "您是 <font color=#FF0000>初次</font> 拜訪本系統唷...</p>";
	}else{
		$row[5]++;
		echo "您是第 <font color=#FF0000>$row[5]</font> 次拜訪本系統唷...</p>";
		echo "<p align=center>上次是在 <font color=#008000>".format_date($row[6])."</font> 從 <font color=#6666FF><b>$row[7]</b></font> 上網一遊。</p>";
	}
*/
if($row)
{
?>
<p align="center">如果您不是<font color="#808080"> <? echo $row[1]; ?></font>，請 
<a href="index.php"><font color="#660033">按這裡</font></a> 回上頁更正。</p>
<?
}
?>
<div align="center">
  <center><table border="1" cellpadding="4" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" bgcolor="#FFFFCC">
  <tr>
    <td width="100%"><table border="0" cellpadding="2" cellspacing="0" style="border-collapse: collapse">
    <tr>
      <td align="right" bgcolor="#FFFFCC"><FONT SIZE=3 COLOR=RED><B>92年第二學期</B>&nbsp;&nbsp;</FONT>我是</td>
      <td bgcolor="#FFFFCC" valign="center">:&nbsp;<select size="1" name="who_open"><?     
      	
	$mysql_server_name = "localhost";
	$mysql_username    = "root";
	$mysql_password    = "";
	$mysql_database    = "cycu";
	
	$conn1=mysql_connect( $mysql_server_name, $mysql_username, $mysql_password );
	//$result=mysql_db_query( $mysql_database, "select DISTINCT who_open from cc922 order by who_open",$conn1);
	$result=mysql_db_query( $mysql_database, "select name from class_code order by name",$conn1);
	$row=mysql_fetch_row($result);	
	
	mysql_data_seek($result,0);
	
	while($row=mysql_fetch_row($result))
	{        
        	if($row[0]=="???"||$row[0]=="共必(日)"||$row[0]=="軍訓(日)"||$row[0]=="軍護(日)"||$row[0]=="校通(日)"||$row[0]=="體育(女)"||$row[0]=="體育興趣")
        	{
        		continue;
        	}
        	elseif($row[0]==$std_class_next)
        	{
        		echo "<option value=$std_class_next selected>$std_class_next</option>";
        	}
        	else
        	{
        		echo "<option value=$row[0]>$row[0]</option>";
        	}
	}               	
	mysql_free_result($result);      	
      	      	
      ?></select></td>
      <td bgcolor="#FFFFCC" align="left">的學生</td>
    </tr>
    <tr>
      <td align="right" bgcolor="#FFFFCC">我有部分科目</td>
      <td bgcolor="#FFFFCC" colspan="2" valign="center">:<input type="checkbox" name="re_study" value="ON">要重修 <input type="checkbox" name="cannot_study" value="ON">有擋修</td>
    </tr>
    <tr>
      <td align="right" bgcolor="#FFFFCC">輔系、雙主修、跨領域</td>
      <td bgcolor="#FFFFCC" colspan="2" valign="center">:&nbsp;<select size="1" name="my_another_major">
      <option value="" selected> (無)</option><?

	$result=mysql_db_query( $mysql_database, "select DISTINCT left(who_open,4) from cc922 order by who_open",$conn1);
	$row=mysql_fetch_row($result);	
	
	mysql_data_seek($result,0);
	
	while($row=mysql_fetch_row($result))
	{        
        	if($row[0]=="共必"||$row[0]=="軍訓"||$row[0]=="軍護"||$row[0]=="校通"||$row[0]=="體育"||$row[0]=="進化"||$row[0]=="進工"||$row[0]=="學前")
        	{
        		continue;
        	}
        	else
        	{
        		echo "<option value=$row[0]>$row[0]</option>";
        	}
	}               	
	mysql_free_result($result);
      	mysql_close($conn1);
      	      	
      ?></select></td>
    </tr></table>
  </td>
  </tr>
</table>
  </center>
</div>
<p align="center">若您的新學期系級不正確，請手動選一下唷!!</p>
<p align="center">若打算重修上學期之前的必修課，請勾<b><font color="#CC33FF">重修</font></b>，若這學期有學科被擋修，也請勾<font color="#CC33FF"><b>擋修</b></font>。</p>
<p align="center">若您有修輔系、雙主修，或是跨領域學程者，請選擇您輔修的科系、所。</p>
<p align="center"><input type="submit" value="產生課表"><input type="reset" value="重新設定"></p>

</form>
<?php
}


//------------------------------------------------------------
////秀出固定課表 & 必修未選資料(警告) & 必修重修項目、擋修項目
//------------------------------------------------------------
function show_timetable_1st()
{
   global $PHP_SELF, $std_num, $std_sex, $who_open, $re_study, $cannot_study, $my_another_major;
   
   if($my_another_major==substr($who_open,0,4)) error_message("哪有輔系跟自己的系一樣滴? 您嘛幫幫忙  >_____<|| ");
?>
<form method="POST" action="<?php echo $PHP_SELF ?>">
<INPUT TYPE="hidden" NAME="action" VALUE="show_timetable_2nd">
<INPUT TYPE="hidden" NAME="std_num" VALUE="<?php echo $std_num ?>">
<INPUT TYPE="hidden" NAME="std_sex" VALUE="<?php echo $std_sex ?>">
<INPUT TYPE="hidden" NAME="who_open" VALUE="<?php echo $who_open ?>">
<INPUT TYPE="hidden" NAME="my_another_major" VALUE="<?php echo $my_another_major ?>">
<?
	//統計使用者
	
	$host = "localhost";
	$user = "root";
	$pass = "";
	$database = "cycu";
	$ip_addr = getenv("REMOTE_ADDR");
	$conn=mysql_connect( $host, $user, $pass );
	$query = "UPDATE std912 SET total_login = total_login + 1 , last_ip = '$ip_addr' WHERE std_num = '$std_num'";
	mysql_select_db($database,$conn);
	$result = mysql_query($query);
	mysql_close($conn);	
	

	if($std_sex=="1"){
		$sex_limit="2";
	}else{
		$sex_limit="1";
	}
	
	$mysql_server_name = "localhost";
	$mysql_username    = "root";
	$mysql_password    = "";
	$mysql_database    = "cycu";
	
	$conn1=mysql_connect( $mysql_server_name, $mysql_username, $mysql_password );
	
	//"-----------------------系上必修課(已選)---------------------------<br>";
	$result=mysql_db_query( $mysql_database, "select course_name,teacher,time_a,time_b,time_c,course_code from cc922 where who_open = '$who_open' and checked = '1' and course_need = '1' and course_sex <> '$sex_limit'",$conn1);
	@$row=mysql_fetch_row($result);
	
	if($row){
			
		mysql_data_seek($result,0);
		while($row=mysql_fetch_row($result))
		{        
       			$input[$row[2]] = "<font color=red>$row[0]</font><br>$row[1]<br><font color=#008000><B>($row[5])</B></FONT>";
       			if($row[3]) $input[$row[3]] = "<font color=red>$row[0]</font><br>$row[1]<br><font color=#008000><B>($row[5])</B></FONT>";
       			if($row[4]) $input[$row[4]] = "<font color=red>$row[0]</font><br>$row[1]<br><font color=#008000><B>($row[5])</B></FONT>";
       			$original_need[$row[5]] = $row[2];
       			if($row[3]) $original_need[$row[5]] = $original_need[$row[5]] . " " .$row[3];
       			if($row[4]) $original_need[$row[5]] = $original_need[$row[5]] . " " . $row[4];
		}
		
		echo "<P><center>";
		fill_timetable($input);
		echo "<P></center>"; 	
	}else{
		
		include("time_table.php");
			
	}        
	
	
	@mysql_free_result($result);
	if($original_need)
	{
		reset($original_need);
		foreach($original_need as $need_key => $need_value)
		{
			echo "<INPUT TYPE=\"hidden\" NAME=\"origin[$need_key]\" VALUE=\"$need_value\">\n";
		}
	}
	
	//echo "-----------------------系上必修課(未選)---------------------------<br>";
	
	$result=mysql_db_query( $mysql_database, "select * from cc922 where who_open = '$who_open' and checked <> '1' and course_need = '1' and course_sex <> '$sex_limit'",$conn1);
	@$row=mysql_fetch_row($result);
	
	if(!$row){
		echo "<p align=\"center\">(您沒有其他未選的系上必修課程)</p>\n";
		echo "<hr noshade color=\"#111111\" width=\"90%\" size=\"1\">\n";
	}else{
?>
    <p align="center">以下是貴系所開之<font size="3" color="#FF0000">必修課程</font>，但尚未列入您的課表</p>
    <div align="center">
      <center>
    <table bgColor="dodgerblue" style="border-collapse: collapse" bordercolor="#111111" cellpadding="2" cellspacing="0" border="1">
      <tr bgColor="skyblue">
        <td noWrap bgcolor="#CCFFFF">開課系所</td>
        <td bgcolor="#CCFFFF">類別</td>
        <td noWrap bgcolor="#CCFFFF">語音代碼</td>
        <td noWrap bgcolor="#CCFFFF">課程代碼</td>
        <td noWrap bgcolor="#CCFFFF">課程名稱</td>
        <td bgcolor="#CCFFFF">性質</td>
        <td bgcolor="#CCFFFF">學分</td>
        <td noWrap bgcolor="#CCFFFF">教師</td>
        <td noWrap bgcolor="#CCFFFF">時間A</td>
        <td noWrap bgcolor="#CCFFFF">時間B</td>
        <td noWrap bgcolor="#CCFFFF">時間C</td>
        <td noWrap bgcolor="#CCFFFF">備註</td>
        <td noWrap bgcolor="#CCFFFF">篩選</td>
        <td noWrap bgcolor="#66FF99"><font size="3">我要</font></td>
      </tr>
<?		mysql_data_seek($result,0);
		while($row=mysql_fetch_row($result))
		{
?>
      <tr bgColor="lightyellow">
        <td noWrap><?php echo $row[3]; ?></td>
        <td noWrap><?php echo $row[22]; ?></td>
        <td noWrap><?php echo $row[4]; ?></td>
        <td noWrap><?php echo $row[5]; ?></td>
        <td noWrap><?php echo $row[6]; ?></td>
        <td noWrap><?php if($row[8]==0){echo"半";}elseif($row[8]==1){echo"全";}?></td>
        <td noWrap><?php echo $row[10]; ?></td>
        <td noWrap><?php echo $row[11]; ?></td>
        <td noWrap><?php echo $row[13]; ?></td>
        <td noWrap><?php echo $row[15]; ?></td>
        <td noWrap><?php echo $row[17]; ?></td>
        <td noWrap><?php echo $row[19]; ?></td>
        <td noWrap><?php echo $row[18]; ?></td>
        <td noWrap align="center" bgcolor="#CCFFCC">
        <input type="checkbox" name="add_need[<? echo $row[5]; ?>]" value="<?
	$time_total = $row[13];
	if($row[15]) $time_total = $time_total . " " . $row[15];
	if($row[17]) $time_total = $time_total . " " . $row[17]; 
	echo $time_total; ?>"></td>
      </tr>
<?
		}
?>
    </table>
      </center>
</div>
    <p align="center">如果您已經確定哪堂課會排入課表，請選取<b><font color="#008000" size="3"> 我要 
    </font></b>。<p align="center">如果不確定，請點選此處「<a href="http://cna.ax.com.tw/yours.htm" target="blank">搶先版查詢</a>」查詢(網策會提供^^)，或是學校的「<a href="http://wwwsrv.cc.cycu.edu.tw/andy/StudCourse/StudCourse.htm" target="blank">正式連結</a>」</p>

<hr noshade color="#111111" width="90%" size="1">
<?	
	}         	
	
	@mysql_free_result($result);

if($re_study=="ON"){
	//echo "-------------------------系上重修可選課程-----------------------------------<br>";
	
	$std_class_before = $who_open;
	$std_class_found = 0;
	$list_count=0;
	
	//-----------
	$change_check = $std_class_before;
	$std_class_before = str_replace("八", "七", $std_class_before);
	if($change_check!=$std_class_before){
	
		$result=mysql_db_query( $mysql_database, "select * from cc922 where who_open = '$std_class_before' and course_need = '1' and course_sex <> '$sex_limit'",$conn1);
		@$row=mysql_fetch_row($result);			
			
		if($row){		
			mysql_data_seek($result,0);
			while($row=mysql_fetch_row($result))
			{        
				if($row[8]==0){$x1 = "半";}elseif($row[8]==1){$x1 ="全";}
				$time_total = $row[13];
				if($row[15]) $time_total = "$time_total $row[15]";
				if($row[17]) $time_total = $time_total . " " . $row[17];
       				$restudy_list[$list_count]="<tr bgColor=lightyellow><td noWrap>$row[3]</td><td noWrap>$row[22]</td><td noWrap>$row[4]</td><td noWrap>$row[5]</td><td noWrap>$row[6]</td><td noWrap>$x1</td><td noWrap>$row[10]</td><td noWrap>$row[11]</td><td noWrap>$row[13]</td><td noWrap>$row[15]</td><td noWrap>$row[17]</td><td noWrap>$row[19]</td><td noWrap>$row[18]</td><td noWrap align=center bgcolor=#CCFFCC><input type=checkbox name=add_need[$row[5]] value=\"$time_total\"></td></tr>";
       				$list_count++;
			}
			$std_class_found = 1;
		}
		@mysql_free_result($result);
	}
	//-----------	
	$change_check = $std_class_before;
	$std_class_before = str_replace("七", "六", $std_class_before);
	if($change_check!=$std_class_before){
	
		$result=mysql_db_query( $mysql_database, "select * from cc922 where who_open = '$std_class_before' and course_need = '1' and course_sex <> '$sex_limit'",$conn1);
		@$row=mysql_fetch_row($result);			
			
		if($row){		
			mysql_data_seek($result,0);
			while($row=mysql_fetch_row($result))
			{        
				if($row[8]==0){$x1 = "半";}elseif($row[8]==1){$x1 ="全";}
				$time_total = $row[13];
				if($row[15]) $time_total = $time_total . " " . $row[15];
				if($row[17]) $time_total = $time_total . " " . $row[17];
       				$restudy_list[$list_count]="<tr bgColor=lightyellow><td noWrap>$row[3]</td><td noWrap>$row[22]</td><td noWrap>$row[4]</td><td noWrap>$row[5]</td><td noWrap>$row[6]</td><td noWrap>$x1</td><td noWrap>$row[10]</td><td noWrap>$row[11]</td><td noWrap>$row[13]</td><td noWrap>$row[15]</td><td noWrap>$row[17]</td><td noWrap>$row[19]</td><td noWrap>$row[18]</td><td noWrap align=center bgcolor=#CCFFCC><input type=checkbox name=add_need[$row[5]] value=\"$time_total\"></td></tr>";     
       				$list_count++;
			}
			$std_class_found = 1;
		}
		@mysql_free_result($result);
	}
	//-----------	
	$change_check = $std_class_before;
	$std_class_before = str_replace("六", "五", $std_class_before);
	if($change_check!=$std_class_before){
	
		$result=mysql_db_query( $mysql_database, "select * from cc922 where who_open = '$std_class_before' and course_need = '1' and course_sex <> '$sex_limit'",$conn1);
		@$row=mysql_fetch_row($result);			
			
		if($row){		
			mysql_data_seek($result,0);
			while($row=mysql_fetch_row($result))
			{        
				if($row[8]==0){$x1 = "半";}elseif($row[8]==1){$x1 ="全";}
				$time_total = $row[13];
				if($row[15]) $time_total = $time_total . " " . $row[15];
				if($row[17]) $time_total = $time_total . " " . $row[17];
       				$restudy_list[$list_count]="<tr bgColor=lightyellow><td noWrap>$row[3]</td><td noWrap>$row[22]</td><td noWrap>$row[4]</td><td noWrap>$row[5]</td><td noWrap>$row[6]</td><td noWrap>$x1</td><td noWrap>$row[10]</td><td noWrap>$row[11]</td><td noWrap>$row[13]</td><td noWrap>$row[15]</td><td noWrap>$row[17]</td><td noWrap>$row[19]</td><td noWrap>$row[18]</td><td noWrap align=center bgcolor=#CCFFCC><input type=checkbox name=add_need[$row[5]] value=\"$time_total\"></td></tr>";     
       				$list_count++;
			}
			$std_class_found = 1;
		}
		@mysql_free_result($result);
	}
	//-----------	
	$change_check = $std_class_before;
	$std_class_before = str_replace("五", "四", $std_class_before);
	if($change_check!=$std_class_before){			
		
		$query = substr($std_class_before,0,strlen($std_class_before)-2) . "%";
				
		$result=mysql_db_query( $mysql_database, "select * from cc922 where who_open like '$query' and course_need = '1' and course_sex <> '$sex_limit'",$conn1);
		@$row=mysql_fetch_row($result);			
			
		if($row){		
			mysql_data_seek($result,0);
			while($row=mysql_fetch_row($result))
			{        
				if($row[8]==0){$x1 = "半";}elseif($row[8]==1){$x1 ="全";}
				$time_total = $row[13];
				if($row[15]) $time_total = $time_total . " " . $row[15];
				if($row[17]) $time_total = $time_total . " " . $row[17];
       				$restudy_list[$list_count]="<tr bgColor=lightyellow><td noWrap>$row[3]</td><td noWrap>$row[22]</td><td noWrap>$row[4]</td><td noWrap>$row[5]</td><td noWrap>$row[6]</td><td noWrap>$x1</td><td noWrap>$row[10]</td><td noWrap>$row[11]</td><td noWrap>$row[13]</td><td noWrap>$row[15]</td><td noWrap>$row[17]</td><td noWrap>$row[19]</td><td noWrap>$row[18]</td><td noWrap align=center bgcolor=#CCFFCC><input type=checkbox name=add_need[$row[5]] value=\"$time_total\"></td></tr>";     
       				$list_count++;
			}
			$std_class_found = 1;
		}
		@mysql_free_result($result);
	}
	//-----------	
	$change_check = $std_class_before;
	$std_class_before = str_replace("四", "三", $std_class_before);
	if($change_check!=$std_class_before){
		
		$query = substr($std_class_before,0,strlen($std_class_before)-2) . "%";
				
		$result=mysql_db_query( $mysql_database, "select * from cc922 where who_open like '$query' and course_need = '1' and course_sex <> '$sex_limit'",$conn1);
		@$row=mysql_fetch_row($result);			
			
		if($row){		
			mysql_data_seek($result,0);
			while($row=mysql_fetch_row($result))
			{        
				if($row[8]==0){$x1 = "半";}elseif($row[8]==1){$x1 ="全";}
				$time_total = $row[13];
				if($row[15]) $time_total = $time_total . " " . $row[15];
				if($row[17]) $time_total = $time_total . " " . $row[17];
       				$restudy_list[$list_count]="<tr bgColor=lightyellow><td noWrap>$row[3]</td><td noWrap>$row[22]</td><td noWrap>$row[4]</td><td noWrap>$row[5]</td><td noWrap>$row[6]</td><td noWrap>$x1</td><td noWrap>$row[10]</td><td noWrap>$row[11]</td><td noWrap>$row[13]</td><td noWrap>$row[15]</td><td noWrap>$row[17]</td><td noWrap>$row[19]</td><td noWrap>$row[18]</td><td noWrap align=center bgcolor=#CCFFCC><input type=checkbox name=add_need[$row[5]] value=\"$time_total\"></td></tr>";     
       				$list_count++;
			}
			$std_class_found = 1;
		}
		@mysql_free_result($result);
			
				
	}
	//-----------	
	$change_check = $std_class_before;
	$std_class_before = str_replace("三", "二", $std_class_before);
	if($change_check!=$std_class_before){
		
		$query = substr($std_class_before,0,strlen($std_class_before)-2) . "%";
				
		$result=mysql_db_query( $mysql_database, "select * from cc922 where who_open like '$query' and course_need = '1' and course_sex <> '$sex_limit'",$conn1);
		@$row=mysql_fetch_row($result);			
			
		if($row){		
			mysql_data_seek($result,0);
			while($row=mysql_fetch_row($result))
			{        
				if($row[8]==0){$x1 = "半";}elseif($row[8]==1){$x1 ="全";}
				$time_total = $row[13];
				if($row[15]) $time_total = $time_total . " " . $row[15];
				if($row[17]) $time_total = $time_total . " " . $row[17];
       				$restudy_list[$list_count]="<tr bgColor=lightyellow><td noWrap>$row[3]</td><td noWrap>$row[22]</td><td noWrap>$row[4]</td><td noWrap>$row[5]</td><td noWrap>$row[6]</td><td noWrap>$x1</td><td noWrap>$row[10]</td><td noWrap>$row[11]</td><td noWrap>$row[13]</td><td noWrap>$row[15]</td><td noWrap>$row[17]</td><td noWrap>$row[19]</td><td noWrap>$row[18]</td><td noWrap align=center bgcolor=#CCFFCC><input type=checkbox name=add_need[$row[5]] value=\"$time_total\"></td></tr>";     
       				$list_count++;
			}
			$std_class_found = 1;
		}
		@mysql_free_result($result);
			
				
	}
	//-----------	
	$change_check = $std_class_before;
	$std_class_before = str_replace("二", "一", $std_class_before);
	if($change_check!=$std_class_before){
		
		$query = substr($std_class_before,0,strlen($std_class_before)-2) . "%";
				
		$result=mysql_db_query( $mysql_database, "select * from cc922 where who_open like '$query' and course_need = '1' and course_sex <> '$sex_limit'",$conn1);
		@$row=mysql_fetch_row($result);			
			
		if($row){		
			mysql_data_seek($result,0);
			while($row=mysql_fetch_row($result))
			{        
				if($row[8]==0){$x1 = "半";}elseif($row[8]==1){$x1 ="全";}
				$time_total = $row[13];
				if($row[15]) $time_total = $time_total . " " . $row[15];
				if($row[17]) $time_total = $time_total . " " . $row[17];
       				$restudy_list[$list_count]="<tr bgColor=lightyellow><td noWrap>$row[3]</td><td noWrap>$row[22]</td><td noWrap>$row[4]</td><td noWrap>$row[5]</td><td noWrap>$row[6]</td><td noWrap>$x1</td><td noWrap>$row[10]</td><td noWrap>$row[11]</td><td noWrap>$row[13]</td><td noWrap>$row[15]</td><td noWrap>$row[17]</td><td noWrap>$row[19]</td><td noWrap>$row[18]</td><td noWrap align=center bgcolor=#CCFFCC><input type=checkbox name=add_need[$row[5]] value=\"$time_total\"></td></tr>";     
       				$list_count++;
			}
			$std_class_found = 1;
		}
		@mysql_free_result($result);
			
				
	}
	
	if($std_class_found==0){
		echo "<p align=\"center\">(您沒有可以重修的課程)</p>\n";
		echo "<hr noshade color=\"#111111\" width=\"90%\" size=\"1\">\n";
	}else{
?>
 <p align="center">以下是<font size="3" color="#FF0000"> 重修課程 </font>清單，如有符合需求請點選<b><font color="#008000" size="3"> 
我要 </font></b>。<P>
    <div align="center">
      <center>
    <table bgColor="dodgerblue" style="border-collapse: collapse" bordercolor="#111111" cellpadding="2" cellspacing="0" border="1">
      <tr bgColor="skyblue">
        <td noWrap bgcolor="#CCFFFF">開課系所</td>
        <td bgcolor="#CCFFFF">類別</td>
        <td noWrap bgcolor="#CCFFFF">語音代碼</td>
        <td noWrap bgcolor="#CCFFFF">課程代碼</td>
        <td noWrap bgcolor="#CCFFFF">課程名稱</td>
        <td bgcolor="#CCFFFF">性質</td>
        <td bgcolor="#CCFFFF">學分</td>
        <td noWrap bgcolor="#CCFFFF">教師</td>
        <td noWrap bgcolor="#CCFFFF">時間A</td>
        <td noWrap bgcolor="#CCFFFF">時間B</td>
        <td noWrap bgcolor="#CCFFFF">時間C</td>
        <td noWrap bgcolor="#CCFFFF">備註</td>
        <td noWrap bgcolor="#CCFFFF">篩選</td>
        <td noWrap bgcolor="#66FF99"><font size="3">我要</font></td>
      </tr>
<?
	foreach($restudy_list as $xxxxx) echo "$xxxxx\n";
?>
    </table>
    </center>
</div>
<p align="center">系統會替您檢查是否衝堂，衝堂課程一律被忽略。</p>
<hr noshade color="#111111" width="90%" size="1">
<?
}
}	
if($cannot_study=="ON"){
	//echo "--------------------------系上必修課(擋修選擇用)-------------------------------<br>";
	
	$result=mysql_db_query( $mysql_database, "select * from cc922 where who_open = '$who_open' and course_need = '1' and course_sex <> '$sex_limit'",$conn1);		

	@$row=mysql_fetch_row($result);
	
	if(!$row){
		echo "<p align=\"center\">(您沒有被擋修的課程)</p>\n";
		echo "<hr noshade color=\"#111111\" width=\"90%\" size=\"1\">\n";
	}else{
?>
<p align="center">以下是<font size="3" color="#FF0000"> 擋修課程 </font>清單，如有符合被擋修科目請點選<b><font color="#008000" size="3"> 
擋修 </font></b>。</p>
    <div align="center">
      <center>
    <table bgColor="dodgerblue" style="border-collapse: collapse" bordercolor="#111111" cellpadding="2" cellspacing="0" border="1">
      <tr bgColor="skyblue">
        <td noWrap bgcolor="#CCFFFF">開課系所</td>
        <td bgcolor="#CCFFFF">類別</td>
        <td noWrap bgcolor="#CCFFFF">語音代碼</td>
        <td noWrap bgcolor="#CCFFFF">課程代碼</td>
        <td noWrap bgcolor="#CCFFFF">課程名稱</td>
        <td bgcolor="#CCFFFF">性質</td>
        <td bgcolor="#CCFFFF">學分</td>
        <td noWrap bgcolor="#CCFFFF">教師</td>
        <td noWrap bgcolor="#CCFFFF">時間A</td>
        <td noWrap bgcolor="#CCFFFF">時間B</td>
        <td noWrap bgcolor="#CCFFFF">時間C</td>
        <td noWrap bgcolor="#CCFFFF">備註</td>
        <td noWrap bgcolor="#CCFFFF">篩選</td>
        <td noWrap bgcolor="#FF0000"><font size="3" color="#FFFFFF">擋修</font></td>
      </tr>
<?		mysql_data_seek($result,0);
		while($row=mysql_fetch_row($result))
		{
?>
      <tr bgColor="lightyellow">
        <td noWrap><?php echo $row[3]; ?></td>
        <td noWrap><?php echo $row[22]; ?></td>
        <td noWrap><?php echo $row[4]; ?></td>
        <td noWrap><?php echo $row[5]; ?></td>
        <td noWrap><?php echo $row[6]; ?></td>
        <td noWrap><?php if($row[8]==0){echo"半";}elseif($row[8]==1){echo"全";}?></td>
        <td noWrap><?php echo $row[10]; ?></td>
        <td noWrap><?php echo $row[11]; ?></td>
        <td noWrap><?php echo $row[13]; ?></td>
        <td noWrap><?php echo $row[15]; ?></td>
        <td noWrap><?php echo $row[17]; ?></td>
        <td noWrap><?php echo $row[19]; ?></td>
        <td noWrap><?php echo $row[18]; ?></td>
        <td noWrap align="center" bgcolor="#FF5050">
        <input type="checkbox" name="remove_need[<?php echo $row[5]; ?>]" value="<?
	$time_total = $row[13];
	if($row[15]) $time_total = $time_total . " " . $row[15];
	if($row[17]) $time_total = $time_total . " " . $row[17]; 
	echo $time_total; ?>"></td>
      </tr>
<?
	}
?>
    </table>
    </center>
</div>
<p align="center">被擋修課程，將會從課表上被移除，讓您空出時段選擇其他課程。</p>
<hr noshade color="#111111" width="90%" size="1">
<?
	}
	@mysql_free_result($result);

}

//處理輔系資料
if($my_another_major)
{
	
	//echo "--------------------------輔系必修課-------------------------------<br>";
	
	$result=mysql_db_query( $mysql_database, "select * from cc922 where who_open like '$my_another_major" . "%" . "' and course_need = '1' and course_type = '一般' and (course_name <> '國文' AND course_name <> '英文' AND course_name <> '勞動服務' AND course_name <> '英聽') and course_sex <> '$sex_limit' order by who_open",$conn1);

	@$row=mysql_fetch_row($result);
	
	if(!$row){
		echo "<p align=\"center\">(您沒有可選的輔系必修課程)</p>\n";
		echo "<hr noshade color=\"#111111\" width=\"90%\" size=\"1\">\n";
	}else{
?>
<p align="center">以下是輔修<font size="3" color="#FF0000"> <? echo $my_another_major?>系(所) </font>的必修課程清單，如有符合的科目請點選<b><font color="#008000" size="3"> 
我要 </font></b>。</p>
    <div align="center">
      <center>
    <table bgColor="dodgerblue" style="border-collapse: collapse" bordercolor="#111111" cellpadding="2" cellspacing="0" border="1">
      <tr bgColor="skyblue">
        <td noWrap bgcolor="#CCFFFF">開課系所</td>
        <td bgcolor="#CCFFFF">類別</td>
        <td noWrap bgcolor="#CCFFFF">語音代碼</td>
        <td noWrap bgcolor="#CCFFFF">課程代碼</td>
        <td noWrap bgcolor="#CCFFFF">課程名稱</td>
        <td bgcolor="#CCFFFF">性質</td>
        <td bgcolor="#CCFFFF">學分</td>
        <td noWrap bgcolor="#CCFFFF">教師</td>
        <td noWrap bgcolor="#CCFFFF">時間A</td>
        <td noWrap bgcolor="#CCFFFF">時間B</td>
        <td noWrap bgcolor="#CCFFFF">時間C</td>
        <td noWrap bgcolor="#CCFFFF">備註</td>
        <td noWrap bgcolor="#CCFFFF">篩選</td>
        <td noWrap bgcolor="#66FF99"><font size="3">我要</font></td>
      </tr>
<?		mysql_data_seek($result,0);
		while($row=mysql_fetch_row($result))
		{
?>
      <tr bgColor="lightyellow">
        <td noWrap><?php echo $row[3]; ?></td>
        <td noWrap><?php echo $row[22]; ?></td>
        <td noWrap><?php echo $row[4]; ?></td>
        <td noWrap><?php echo $row[5]; ?></td>
        <td noWrap><?php echo $row[6]; ?></td>
        <td noWrap><?php if($row[8]==0){echo"半";}elseif($row[8]==1){echo"全";}?></td>
        <td noWrap><?php echo $row[10]; ?></td>
        <td noWrap><?php echo $row[11]; ?></td>
        <td noWrap><?php echo $row[13]; ?></td>
        <td noWrap><?php echo $row[15]; ?></td>
        <td noWrap><?php echo $row[17]; ?></td>
        <td noWrap><?php echo $row[19]; ?></td>
        <td noWrap><?php echo $row[18]; ?></td>
        <td noWrap align="center" bgcolor="#CCFFCC">
        <input type="checkbox" name="add_need[<?php echo $row[5]; ?>]" value="<?
	$time_total = $row[13];
	if($row[15]) $time_total = $time_total . " " . $row[15];
	if($row[17]) $time_total = $time_total . " " . $row[17]; 
	echo $time_total; ?>"></td>
      </tr>
<?
	}
?>
    </table>
    </center>
</div>
<p align="center">以上輔系修課資格，請務必確認清楚再排入，以確保自己權益。</p>
<hr noshade color="#111111" width="90%" size="1">
<?
	}
	@mysql_free_result($result);
	$www = substr($who_open,0,6);
	//echo "--------------------------輔系調整課程-------------------------------<br>";
	
	$result=mysql_db_query( $mysql_database, "select * from cc922 where ( who_open like '$www" . "%" . "' and who_open <> '$who_open') and course_need = '1' and course_type = '一般' and (course_name <> '國文' AND course_name <> '英文' AND course_name <> '勞動服務' AND course_name <> '英聽') and course_sex <> '$sex_limit' order by who_open",$conn1);

	@$row=mysql_fetch_row($result);
	
	if($row){
?>
<p align="center">以下是輔修衝堂<font size="3" color="#FF0000"> 「換班」 </font>至其他班的必修課，如有符合的科目請點選<b><font color="#008000" size="3"> 
我要 </font></b>。</p>
    <div align="center">
      <center>
    <table bgColor="dodgerblue" style="border-collapse: collapse" bordercolor="#111111" cellpadding="2" cellspacing="0" border="1">
      <tr bgColor="skyblue">
        <td noWrap bgcolor="#CCFFFF">開課系所</td>
        <td bgcolor="#CCFFFF">類別</td>
        <td noWrap bgcolor="#CCFFFF">語音代碼</td>
        <td noWrap bgcolor="#CCFFFF">課程代碼</td>
        <td noWrap bgcolor="#CCFFFF">課程名稱</td>
        <td bgcolor="#CCFFFF">性質</td>
        <td bgcolor="#CCFFFF">學分</td>
        <td noWrap bgcolor="#CCFFFF">教師</td>
        <td noWrap bgcolor="#CCFFFF">時間A</td>
        <td noWrap bgcolor="#CCFFFF">時間B</td>
        <td noWrap bgcolor="#CCFFFF">時間C</td>
        <td noWrap bgcolor="#CCFFFF">備註</td>
        <td noWrap bgcolor="#CCFFFF">篩選</td>
        <td noWrap bgcolor="#66FF99"><font size="3">我要</font></td>
      </tr>
<?		mysql_data_seek($result,0);
		while($row=mysql_fetch_row($result))
		{
?>
      <tr bgColor="lightyellow">
        <td noWrap><?php echo $row[3]; ?></td>
        <td noWrap><?php echo $row[22]; ?></td>
        <td noWrap><?php echo $row[4]; ?></td>
        <td noWrap><?php echo $row[5]; ?></td>
        <td noWrap><?php echo $row[6]; ?></td>
        <td noWrap><?php if($row[8]==0){echo"半";}elseif($row[8]==1){echo"全";}?></td>
        <td noWrap><?php echo $row[10]; ?></td>
        <td noWrap><?php echo $row[11]; ?></td>
        <td noWrap><?php echo $row[13]; ?></td>
        <td noWrap><?php echo $row[15]; ?></td>
        <td noWrap><?php echo $row[17]; ?></td>
        <td noWrap><?php echo $row[19]; ?></td>
        <td noWrap><?php echo $row[18]; ?></td>
        <td noWrap align="center" bgcolor="#CCFFCC">
        <input type="checkbox" name="add_need[<?php echo $row[5]; ?>]" value="<?
	$time_total = $row[13];
	if($row[15]) $time_total = $time_total . " " . $row[15];
	if($row[17]) $time_total = $time_total . " " . $row[17]; 
	echo $time_total; ?>"></td>
      </tr>
<?
	}
	}
	@mysql_free_result($result);
?>
    </table>
    </center>
</div><P>
<?
	//echo "--------------------------退本系做輔系調整課程-------------------------------<br>";
	
	$result=mysql_db_query( $mysql_database, "select * from cc922 where who_open = '$who_open' and course_need = '1' and course_sex <> '$sex_limit'",$conn1);

	@$row=mysql_fetch_row($result);
	
	if($row){
?>
<p align="center">以下是您目前班級<font size="3" color="#FF0000"> <? echo $who_open?> </font>的必修課，請配合上面的調整勾選要退的課。</p>
    <div align="center">
      <center>
    <table bgColor="dodgerblue" style="border-collapse: collapse" bordercolor="#111111" cellpadding="2" cellspacing="0" border="1">
      <tr bgColor="skyblue">
        <td noWrap bgcolor="#CCFFFF">開課系所</td>
        <td bgcolor="#CCFFFF">類別</td>
        <td noWrap bgcolor="#CCFFFF">語音代碼</td>
        <td noWrap bgcolor="#CCFFFF">課程代碼</td>
        <td noWrap bgcolor="#CCFFFF">課程名稱</td>
        <td bgcolor="#CCFFFF">性質</td>
        <td bgcolor="#CCFFFF">學分</td>
        <td noWrap bgcolor="#CCFFFF">教師</td>
        <td noWrap bgcolor="#CCFFFF">時間A</td>
        <td noWrap bgcolor="#CCFFFF">時間B</td>
        <td noWrap bgcolor="#CCFFFF">時間C</td>
        <td noWrap bgcolor="#CCFFFF">備註</td>
        <td noWrap bgcolor="#CCFFFF">篩選</td>
        <td noWrap bgcolor="#FF0000"><font size="3" color="#FFFFFF">退掉</font></td>
      </tr>
<?		mysql_data_seek($result,0);
		while($row=mysql_fetch_row($result))
		{
?>
      <tr bgColor="lightyellow">
        <td noWrap><?php echo $row[3]; ?></td>
        <td noWrap><?php echo $row[22]; ?></td>
        <td noWrap><?php echo $row[4]; ?></td>
        <td noWrap><?php echo $row[5]; ?></td>
        <td noWrap><?php echo $row[6]; ?></td>
        <td noWrap><?php if($row[8]==0){echo"半";}elseif($row[8]==1){echo"全";}?></td>
        <td noWrap><?php echo $row[10]; ?></td>
        <td noWrap><?php echo $row[11]; ?></td>
        <td noWrap><?php echo $row[13]; ?></td>
        <td noWrap><?php echo $row[15]; ?></td>
        <td noWrap><?php echo $row[17]; ?></td>
        <td noWrap><?php echo $row[19]; ?></td>
        <td noWrap><?php echo $row[18]; ?></td>
        <td noWrap align="center" bgcolor="#FF5050">
        <input type="checkbox" name="remove_need[<?php echo $row[5]; ?>]" value="<?
	$time_total = $row[13];
	if($row[15]) $time_total = $time_total . " " . $row[15];
	if($row[17]) $time_total = $time_total . " " . $row[17]; 
	echo $time_total; ?>"></td>
      </tr>
<?
	}
?>
    </table>
    </center>
</div>
<?
	}
	@mysql_free_result($result);

?>
<p align="center">因為您修輔系、雙主修或是跨領域學程的關係，所以有些課程必須換班上課。</p>
<p align="center">請您務必成對勾選，一加一退，課程才會正確排入。</p>
<hr noshade color="#111111" width="90%" size="1">
<?

	
}
//結束輔系處理

?>
<p align="center">系統自訂功能<font size="3" color="#FF0000"> 手動加選</font>，提供您處理例外狀況</p>
      <center>
      <table id="AutoNumber1" style="BORDER-COLLAPSE: collapse" borderColor="#111111" cellSpacing="0" cellPadding="7" border="1" bgcolor="#FFFFE0">
        <tr>
          <td width="100%">
          <table style="BORDER-COLLAPSE: collapse" borderColor="#111111" cellSpacing="0" cellPadding="2" border="0">
            <tr>
              <td align="middle" nowrap><b><font color="#008080" size="3">課程代碼 :</font></b></td>
              <td vAlign="top" align="left" nowrap>
              <input size="45" name="force_add"></td>
            </tr>
            <tr>
              <td align="middle" colspan="2" style="line-height: 150%" nowrap>
              請填入課程代碼(如 MH042A)，並以<b><font color="#008000" size="3"> 
    </font><font color="#0000FF" size="3">逗號</font><font color="#008000" size="3"> 
    </font></b>將多筆資料分隔開。</td>
            </tr>
          </table>
          </td>
        </tr>
      </table>
      </center>
    <p align="center">手動加選的必需是<b><font color="#000080"> 未列於上面清單中 </font></b>的<font color="#000080"><b>
    </b></font><b><font color="#008000" size="3">必修課程</font></b>，除衝堂外，系統將幫您無條件加入</p>
<hr noshade color="#111111" width="90%" size="1">
<p align="center">系統自訂功能<font size="3" color="#008000"> 手動退選</font>，提供您處理例外狀況</p>
      <center>
      <table id="AutoNumber1" style="BORDER-COLLAPSE: collapse" borderColor="#111111" cellSpacing="0" cellPadding="7" border="1" bgcolor="#FFFFE0">
        <tr>
          <td width="100%">
          <table style="BORDER-COLLAPSE: collapse" borderColor="#111111" cellSpacing="0" cellPadding="2" border="0">
            <tr>
              <td align="middle" nowrap><b><font color="#008080" size="3">課程代碼 :</font></b></td>
              <td vAlign="top" align="left" nowrap>
              <input size="45" name="force_remove"></td>
            </tr>
            <tr>
              <td align="middle" colspan="2" style="line-height: 150%" nowrap>
              請填入課程代碼(如 MH042A)，並以<b><font color="#008000" size="3"> 
    </font><font color="#0000FF" size="3">逗號</font><font color="#008000" size="3"> 
    </font></b>將多筆資料分隔開。</td>
            </tr>
          </table>
          </td>
        </tr>
      </table>
      </center>
    <p align="center">手動退選的必需是<b><font color="#ff0000"> 列於上面清單中 </font></b>的<font color="#000080"><b>
    </b></font><b><font color="#008000" size="3">必修課程</font></b>，例如不是您要的課程，系統可將幫您無條件去除</p>
<hr noshade color="#111111" width="90%" size="1">
<p align="center"><input type="submit" value="送出處理"><input type="reset" value="重新設定"></p>
</form>
<?php
}

//---------------------------------------------
//秀出固定課表 & 篩選項目選擇
//---------------------------------------------
function show_timetable_2nd()
{
   global $PHP_SELF, $std_num, $std_sex, $add_need, $remove_need, $origin, $who_open,$force_add,$my_another_major,$force_remove;
   
   	/*
   	foreach($origin as $keyx =>$valuex)	echo "$keyx   $valuex <br>";
   	echo "<P>";
   	foreach($add_need as $keyx =>$valuex)	echo "$keyx   $valuex <br>";
   	echo "<P>";
   	foreach($remove_need as $keyx =>$valuex)	echo "$keyx   $valuex <br>";
   	echo "<P>";
	reset($origin);
	*/
	if($force_remove)
	{		
		if(strlen($force_remove)==6)
		{
			reset($origin);
			while(list($xkey,$xvalue)=each($origin))
			{
				if($force_remove==$xkey) $origin[$xkey]="X";			
			}
		}
		else
		{
			$force_remove_array = explode(",", $force_remove);
			
			reset($origin);
			while(list($xkey,$xvalue)=each($origin))
			{
				for($p=0;$p<sizeof($force_remove_array);$p++)
				{
					if($force_remove_array[$p]==$xkey) $origin[$xkey]="X";
				}				
			}			
		}	
	}
		
	if($remove_need)
	{
		reset($origin);
		while(list($xkey,$xvalue)=each($origin))
		{
			if($remove_need[$xkey]==$origin[$xkey]) $origin[$xkey]="X";			
		}
	}
		
	if($origin)
	{
		reset($origin);
		while(list($xkey,$xvalue)=each($origin))
		{
			if($xvalue!="X") $origin_array[$xkey]=$xvalue;
		}
	}
	
	if($origin_array) {
		reset($origin_array);
		$origin = $origin_array;
	}
	
	if($force_add)
	{
		$force_add_array = course_str_compare($force_add,",");
		
		if($force_add_array)
		{
			reset($force_add_array);
			$force_add_array = compare_me($force_add_array);			
			if($force_add_array)
			{
				@reset($force_add_array);
				$force_add_array = compare_us($origin,$force_add_array);
				
				if($force_add_array)
				{
					@reset($force_add_array);
					foreach($force_add_array as $yykey => $yyvalue)
					{
						$origin[$yykey] = $yyvalue;
					}
				}
			}
		}
		else
		{
			error_message("您手動加選的課程代號有問題，請重新檢查");
		}
	}	

	if($add_need)
	{
		$xx_num = count($add_need);
		$after_test = compare_me($add_need);
		if(count($after_test)==0) pass_message("沒有再加入任何課程，因為所有課程互相衝堂被忽略掉了!!");
	}
   	if(count($after_test)!=0)
   	{
   		$new_items = compare_us($origin,$after_test);
   		if(count($new_items)==0)
   		{
   			pass_message("沒有再加入任何課程，因為所有課程跟必修衝堂被忽略掉了!!");
   		}
   		elseif(count($new_items)<count($after_test))
   		{
   			pass_message("只有加入部分課程，因為有些課程跟必修衝堂被忽略掉了!!");
   		}
   		else
   		{
   			pass_message("加入成功!!");
   		}
   	}
   	
   	if($new_items)
   	{
   		reset($new_items);
   		while(list($xkey,$xvalue)=each($new_items))
		{
			$origin[$xkey]=$xvalue;
		}
	}
	   	
	$your_major = substr($who_open,0,4);
	$your_level = substr($who_open,4,2);	
?>
<CENTER>您已修 <FONT SIZE=3 COLOR=RED><? echo get_credit($origin) ?></FONT> 學分囉</CENTER><BR>
<form method="POST" action="<?php echo $PHP_SELF ?>">
<INPUT TYPE="hidden" NAME="action" VALUE="show_suit_time">
<INPUT TYPE="hidden" NAME="std_num" VALUE="<?php echo $std_num ?>">
<INPUT TYPE="hidden" NAME="std_sex" VALUE="<?php echo $std_sex ?>">
<INPUT TYPE="hidden" NAME="your_major" VALUE="<?php echo $your_major ?>">
<INPUT TYPE="hidden" NAME="your_level" VALUE="<?php echo $your_level ?>">
<INPUT TYPE="hidden" NAME="my_another_major" VALUE="<?php echo $my_another_major ?>">
<?
	echo "<P><CENTER>";
	if($origin)
	{		
		fill_timetable(get_course_time($origin));
	}
	else
	{		
		include("time_table.php");	        
	}
?><P><hr noshade color="#000000" width="90%" size="1"><br><font color="#008000">請利用以下的表格，來篩選出適合您時段的選修課程</font><p><table border="1" cellpadding="7" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" id="AutoNumber1">
  <tr>
    <td width="100%"><table border="0" cellpadding="2" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111">
        <tr>
          <td align="center" width="54">選修興趣 :</td>
          <td align="left" valign="top">
          <input type="checkbox" name="interest_type3[]" value="歷史">歷史
          <input type="checkbox" name="interest_type4[]" value="法政">法政
          <input type="checkbox" name="interest_type1[]" value="宗哲">宗哲
          <input type="checkbox" name="interest_type1[]" value="人哲">人哲
          <input type="checkbox" name="interest_type2[]" value="天">天
          <input type="checkbox" name="interest_type2[]" value="人">人
          <input type="checkbox" name="interest_type2[]" value="物">物
          <input type="checkbox" name="interest_type2[]" value="我">我
          <input type="checkbox" name="interest_type6[]" value="體育">體育</td>
        </tr>
        <tr>
          <td align="center" width="54">　</td>
          <td align="left" valign="top">
          <input type="checkbox" name="interest_type7[]" value="英聽">英聽
	<?if($std_sex=="1"){
	?>
          <input type="checkbox" name="interest_type8[]" value="軍訓">軍訓
        <?
        }else{
        ?>
          <input type="checkbox" name="interest_type8[]" value="軍護">軍護
        <? } 
        ?><input type="checkbox" name="interest_type8[]" value="<? echo $your_major ?>"><font color="#000080"><? echo $your_major ?>系選修</font>
        <?
        if($my_another_major) echo "<input type=\"checkbox\" name=\"interest_type8[]\" value=\"$my_another_major\"><font color=\"#000080\">$my_another_major" . "系選修課</font>";
        ?>
        <input type="checkbox" name="interest_type8[]" value="一般">其他系選修
        <input type="checkbox" name="interest_type8[]" value="國文">國文
        <input type="checkbox" name="interest_type8[]" value="英文">英文
        </td>
        </tr>
        <tr>
          <td align="center" width="54">排除時段 :</td>
          <td align="left" valign="top">
          <input type="text" name="noclass_time" size="62"></td>
        </tr>
        <tr>
          <td align="center" width="54">　</td>
          <td align="left" valign="top">
          請根據 1-78, 2-CD 等方式填寫，將不排課時段標示上去，請用逗號隔開)</td>
        </tr>
<?
	/*
        <tr>
          <td align="center" width="54">排除教師 :</td>
          <td align="left" valign="top">
          <input type="text" name="unlike_teacher" size="62"></td>
        </tr>
        <tr>
          <td align="center" width="54">　</td>
          <td align="left" valign="top">
          (請輸入您要排除的授課教師姓名，可只輸入部分姓名，請用逗號隔開)</td>
        </tr>
        */
?>
        </table>
      <p>其他資訊 : <a href="https://csys.cycu.edu.tw/" target="_blank">選課系統</a>　<a href="http://etouch.cycu.edu.tw/course/coursedept.htm" target="_blank">開課資料</a>　<a href="http://140.135.16.161/voice_system/board.htm" target="_blank">選課資訊</a>　<a href="http://wwwsrv.cc.cycu.edu.tw/andy/Employee/employee.htm" target="_blank">教師姓名</a></p>
      </td>
    </tr>
  </table></td>
  </tr>
</table>
<? 	if($origin)
	{
		reset($origin);
		while(list($key,$value)=each($origin))
		{
			echo"<input type=\"hidden\" name=\"origin[$key]\" value=\"$value\">\n";
		}
	}
?>
<p align="center"><input type="submit" value="進行篩選"><input type="reset" value="重新設定"></p>
</form>
<?php
}

//---------------------------------------------
//列出符合的時段供選擇 並 選定
//---------------------------------------------
function show_suit_time()
{
	global $PHP_SELF, $std_num, $std_sex, $origin, $interest_type1, $interest_type2, $interest_type3, $interest_type4, $interest_type5, $interest_type6, $interest_type7, $interest_type8, $noclass_time, $unlike_teacher, $your_major, $your_level,$my_another_major;
	
	$check_your_choose = 0;
	if($interest_type1) $check_your_choose = 1;
	if($interest_type2) $check_your_choose = 1;
	if($interest_type3) $check_your_choose = 1;
	if($interest_type4) $check_your_choose = 1;
	if($interest_type5) $check_your_choose = 1;
	if($interest_type6) $check_your_choose = 1;
	if($interest_type7) $check_your_choose = 1;
	if($interest_type8) $check_your_choose = 1;
		
	if($check_your_choose == 0) error_message("請至少勾選一項您感興趣的課程類別!");
	
	if($std_sex=="1")
	{
		$sex_limit="2";
	}
	else
	{
		$sex_limit="1";
	}
	
	if($noclass_time)
	{
		if(!ereg("(,?[1-6]-[1-8A-H]+)+",$noclass_time)) error_message("您不排課的時段填寫有誤，請修正!");
		if(strstr($noclass_time,","))
		{
			$noclass_time = str_replace(","," ",strtoupper(trim($noclass_time)));
		}
		
		$very_happy["(另有安排)"] = $noclass_time;		
    		$very_good = compare_us($origin,$very_happy);
    		if(!$very_good)
    		{
    			error_message("您不排課的時段跟必修課程衝突，請修正!");
    		}
    		else
    		{
			$origin["(另有安排)"] = $noclass_time;
			reset($origin);
		}		
   	}  	

?>
<form method="POST" action="<?php echo $PHP_SELF ?>">
<INPUT TYPE="hidden" NAME="action" VALUE="sort_suit_course">
<INPUT TYPE="hidden" NAME="std_num" VALUE="<?php echo $std_num ?>">
<INPUT TYPE="hidden" NAME="std_sex" VALUE="<?php echo $std_sex ?>">
<? 	if($origin)
	{
		reset($origin);
		while(list($key,$value)=each($origin))
		{
			echo"<input type=\"hidden\" name=\"origin[$key]\" value=\"$value\">\n";
		}
	}
	
	if($interest_type1) show_your_choose_type($sex_limit,$origin,$interest_type1,"1",$your_major,$your_level,$my_another_major);
	if($interest_type2) show_your_choose_type($sex_limit,$origin,$interest_type2,"2",$your_major,$your_level,$my_another_major);
	if($interest_type3) show_your_choose_type($sex_limit,$origin,$interest_type3,"3",$your_major,$your_level,$my_another_major);
	if($interest_type4) show_your_choose_type($sex_limit,$origin,$interest_type4,"4",$your_major,$your_level,$my_another_major);
	if($interest_type5) show_your_choose_type($sex_limit,$origin,$interest_type5,"5",$your_major,$your_level,$my_another_major);
	if($interest_type6) show_your_choose_type($sex_limit,$origin,$interest_type6,"6",$your_major,$your_level,$my_another_major);
	if($interest_type7) show_your_choose_type($sex_limit,$origin,$interest_type7,"7",$your_major,$your_level,$my_another_major);
	if($interest_type8) show_your_choose_type($sex_limit,$origin,$interest_type8,"8",$your_major,$your_level,$my_another_major);
?>
<p align="center"><font color="#CC0099">(本系統即將替您輸出整理所選的課程，並提供您排定選課時的優先順序)</font><br>
<p align="center">小秘訣 : 「各大類別」儘量挑選不要超過 <font color="red" size="3">10</font> 
筆的課程，以免您排得頭昏眼花，很難選擇唷&nbsp; ^_____^</p>
<p align="center"><input type="submit" value="開始整理"><input type="reset" value="重新設定"></p>
</form>
<?php
}

//---------------------------------------------
//排序所選課程
//---------------------------------------------
function sort_suit_course()
{
   global $PHP_SELF, $std_num ,$std_sex, $add_need1, $add_need2, $add_need3, $add_need4, $add_need5, $add_need6, $add_need7, $add_need8, $origin;
   
   $check_your_choose = 0;
   if($add_need1) $check_your_choose = 1;
   if($add_need2) $check_your_choose = 1;
   if($add_need3) $check_your_choose = 1;
   if($add_need4) $check_your_choose = 1;
   if($add_need5) $check_your_choose = 1;
   if($add_need6) $check_your_choose = 1;
   if($add_need7) $check_your_choose = 1;
   if($add_need8) $check_your_choose = 1;
		
	if($check_your_choose == 0) error_message("請至少勾選一科以上的志願!");
?>
<form method="POST" action="<?php echo $PHP_SELF ?>">
<INPUT TYPE="hidden" NAME="action" VALUE="show_output">
<INPUT TYPE="hidden" NAME="std_num" VALUE="<?php echo $std_num ?>">
<INPUT TYPE="hidden" NAME="std_sex" VALUE="<?php echo $std_sex ?>">
<INPUT TYPE="hidden" NAME="my_sort1_num" VALUE="<? echo count($add_need1) ?>">
<INPUT TYPE="hidden" NAME="my_sort2_num" VALUE="<? echo count($add_need2) ?>">
<INPUT TYPE="hidden" NAME="my_sort3_num" VALUE="<? echo count($add_need3) ?>">
<INPUT TYPE="hidden" NAME="my_sort4_num" VALUE="<? echo count($add_need4) ?>">
<INPUT TYPE="hidden" NAME="my_sort5_num" VALUE="<? echo count($add_need5) ?>">
<INPUT TYPE="hidden" NAME="my_sort6_num" VALUE="<? echo count($add_need6) ?>">
<INPUT TYPE="hidden" NAME="my_sort7_num" VALUE="<? echo count($add_need7) ?>">
<INPUT TYPE="hidden" NAME="my_sort8_num" VALUE="<? echo count($add_need8) ?>">
<? 	if($origin)
	{
		reset($origin);
		while(list($key,$value)=each($origin))
		{
			echo"<input type=\"hidden\" name=\"origin[$key]\" value=\"$value\">\n";
		}
	}

	if($add_need1)
	{
?>
<p align="center"><font color="#CC0099" size="3">宗哲、人哲類</font></p>
<p align="center">您選擇的課程如下，共 <font color="#CC0099" size="3"><? echo count($add_need1) ?></font> 門課，<? if(count($add_need1)<10){echo "請排定您的志願順序:";}else{echo "請挑選前九志願:";}?></p>
<?
		output_for_sort($add_need1,"9","1");
	}

	if($add_need2)
	{
?>
<p align="center"><font color="#CC0099" size="3">天、人、物、我類</font></p>
<p align="center">您選擇的課程如下，共 <font color="#CC0099" size="3"><? echo count($add_need2) ?></font> 門課，<? if(count($add_need2)<10){echo "請排定您的志願順序:";}else{echo "請挑選前九志願:";}?></p>
<?
		output_for_sort($add_need2,"9","2");
	}

	if($add_need3)
	{
?>
<p align="center"><font color="#CC0099" size="3">歷史類</font></p>
<p align="center">您選擇的課程如下，共 <font color="#CC0099" size="3"><? echo count($add_need3) ?></font> 門課，<? if(count($add_need3)<10){echo "請排定您的志願順序:";}else{echo "請挑選前九志願:";}?></p>
<?
		output_for_sort($add_need3,"9","3");
	}

	if($add_need4)
	{
?>
<p align="center"><font color="#CC0099" size="3">法政類</font></p>
<p align="center">您選擇的課程如下，共 <font color="#CC0099" size="3"><? echo count($add_need4) ?></font> 門課，<? if(count($add_need4)<10){echo "請排定您的志願順序:";}else{echo "請挑選前九志願:";}?></p>
<?
		output_for_sort($add_need4,"9","4");
	}

	if($add_need5)
	{
?>
<p align="center"><font color="#CC0099" size="3">軍訓類</font></p>
<p align="center">您選擇的課程如下，共 <font color="#CC0099" size="3"><? echo count($add_need5) ?></font> 門課，<? if(count($add_need5)<5){echo "請排定您的志願順序:";}else{echo "請挑選前五志願:";}?></p>
<?
		output_for_sort($add_need5,"9","5");
	}

	if($add_need6)
	{
?>
<p align="center"><font color="#CC0099" size="3">體育類</font></p>
<p align="center">您選擇的課程如下，共 <font color="#CC0099" size="3"><? echo count($add_need6) ?></font> 門課，<? if(count($add_need6)<10){echo "請排定您的志願順序:";}else{echo "請挑選前九志願:";}?></p>
<?
		output_for_sort($add_need6,"9","6");
	}

	if($add_need7)
	{
?>
<p align="center"><font color="#CC0099" size="3">英聽類</font></p>
<p align="center">您選擇的課程如下，共 <font color="#CC0099" size="3"><? echo count($add_need7) ?></font> 門課，<? if(count($add_need7)<10){echo "請排定您的志願順序:";}else{echo "請挑選前九志願:";}?></p>
<?
		output_for_sort($add_need7,"9","7");
	}

	if($add_need8)
	{
?>
<p align="center"><font color="#CC0099" size="3">一般課程類</font></p>
<p align="center">您選擇的課程如下，共 <font color="#CC0099" size="3"><? echo count($add_need8) ?></font> 門課，<? if(count($add_need8)<10){echo "請排定您的志願順序:";}else{echo "請挑選前九志願:";}?></p>
<?
		output_for_sort($add_need8,"9","8");
	}
?>
<center><input type="submit" value="進行排序"><input type="reset" value="重新設定"></center>
</form>
<?php
}


//---------------------------------------------
//輸出固定課表 與 排序後內容
//---------------------------------------------
function show_output()
{
   global $PHP_SELF, $std_num, $std_sex, $origin;
   global $my_sort1, $my_sort2, $my_sort3, $my_sort4, $my_sort5, $my_sort6, $my_sort7, $my_sort8;
   global $my_sort1_num, $my_sort2_num, $my_sort3_num, $my_sort4_num, $my_sort5_num, $my_sort6_num, $my_sort7_num, $my_sort8_num;

if($my_sort1_num){
   if($my_sort1_num>9)
   {	 
   	if(count(array_unique($my_sort1))!=10) error_message("宗哲、人哲部分項目的順序重複或沒選，請在檢查一下     ^_______^");
   }
   else
   {
   	if(count(array_unique($my_sort1))!=$my_sort1_num) error_message("宗哲、人哲部分項目的順序重複或沒選，請在檢查一下     ^_______^");
   }
}

if($my_sort2_num){
   if($my_sort2_num>9)
   {	 
   	if(count(array_unique($my_sort2))!=10) error_message("天、人、物、我部分項目的順序重複或沒選，請在檢查一下     ^_______^");
   }
   else
   {
   	if(count(array_unique($my_sort2))!=$my_sort2_num) error_message("天、人、物、我部分項目的順序重複或沒選，請在檢查一下     ^_______^");
   }
}

if($my_sort3_num){
   if($my_sort3_num>9)
   {	 
   	if(count(array_unique($my_sort3))!=10) error_message("歷史部分項目的順序重複或沒選，請在檢查一下     ^_______^");
   }
   else
   {
   	if(count(array_unique($my_sort3))!=$my_sort3_num) error_message("歷史部分項目的順序重複或沒選，請在檢查一下     ^_______^");
   }
}

if($my_sort4_num){ 
   if($my_sort4_num>9)
   {	 
   	if(count(array_unique($my_sort4))!=10) error_message("法政部分項目的順序重複或沒選，請在檢查一下     ^_______^");
   }
   else
   {
   	if(count(array_unique($my_sort4))!=$my_sort4_num) error_message("法政部分項目的順序重複或沒選，請在檢查一下     ^_______^");
   }
}

if($my_sort5_num){
   if($my_sort5_num>9)
   {	 
   	if(count(array_unique($my_sort5))!=10) error_message("軍訓部分項目的順序重複或沒選，請在檢查一下     ^_______^");
   }
   else
   {
   	if(count(array_unique($my_sort5))!=$my_sort5_num) error_message("軍訓部分項目的順序重複或沒選，請在檢查一下     ^_______^");
   }
}

if($my_sort6_num){
   if($my_sort6_num>9)
   {	 
   	if(count(array_unique($my_sort6))!=10) error_message("體育部分項目的順序重複或沒選，請在檢查一下     ^_______^");
   }
   else
   {
   	if(count(array_unique($my_sort6))!=$my_sort6_num) error_message("體育部分項目的順序重複或沒選，請在檢查一下     ^_______^");
   }
}

if($my_sort7_num){
   if($my_sort7_num>9)
   {	 
   	if(count(array_unique($my_sort7))!=10) error_message("英聽部分項目的順序重複或沒選，請在檢查一下     ^_______^");
   }
   else
   {
   	if(count(array_unique($my_sort7))!=$my_sort7_num) error_message("英聽部分項目的順序重複或沒選，請在檢查一下     ^_______^");
   }
}

if($my_sort8_num){
   if($my_sort8_num>9)
   {	 
   	if(count(array_unique($my_sort8))!=10) error_message("一般類課程項目的順序重複或沒選，請在檢查一下     ^_______^");
   }
   else
   {
   	if(count(array_unique($my_sort8))!=$my_sort8_num) error_message("一般類課程項目的順序重複或沒選，請在檢查一下     ^_______^");
   }
}
?>
<form method="POST" action="<?php echo $PHP_SELF ?>">
<INPUT TYPE="hidden" NAME="action" VALUE="print_and_update">
<?
 	if($origin)
	{
		reset($origin);
		while(list($key,$value)=each($origin))
		{
			echo"<input type=\"hidden\" name=\"origin[$key]\" value=\"$value\">\n";
		}
	}
	
	if($my_sort1)
	{	
		while(list($key,$value)=each($my_sort1))
		{
			if($value!="0") echo"<input type=\"hidden\" name=\"my_sort1[$key]\" value=\"$value\">\n";
		}		
		reset($my_sort1);
	}
	
	if($my_sort2)
	{	
		while(list($key,$value)=each($my_sort2))
		{
			if($value!="0") echo"<input type=\"hidden\" name=\"my_sort2[$key]\" value=\"$value\">\n";
		}		
		reset($my_sort2);
	}
	
	if($my_sort3)
	{	
		while(list($key,$value)=each($my_sort3))
		{
			if($value!="0") echo"<input type=\"hidden\" name=\"my_sort3[$key]\" value=\"$value\">\n";
		}		
		reset($my_sort3);
	}
	
	if($my_sort4)
	{	
		while(list($key,$value)=each($my_sort4))
		{
			if($value!="0") echo"<input type=\"hidden\" name=\"my_sort4[$key]\" value=\"$value\">\n";
		}		
		reset($my_sort4);
	}
	
	if($my_sort5)
	{	
		while(list($key,$value)=each($my_sort5))
		{
			if($value!="0") echo"<input type=\"hidden\" name=\"my_sort5[$key]\" value=\"$value\">\n";
		}		
		reset($my_sort5);
	}
	
	if($my_sort6)
	{	
		while(list($key,$value)=each($my_sort6))
		{
			if($value!="0") echo"<input type=\"hidden\" name=\"my_sort6[$key]\" value=\"$value\">\n";
		}		
		reset($my_sort6);
	}
	
	if($my_sort7)
	{	
		while(list($key,$value)=each($my_sort7))
		{
			if($value!="0") echo"<input type=\"hidden\" name=\"my_sort7[$key]\" value=\"$value\">\n";
		}		
		reset($my_sort7);
	}
	
	if($my_sort8)
	{	
		while(list($key,$value)=each($my_sort8))
		{
			if($value!="0") echo"<input type=\"hidden\" name=\"my_sort8[$key]\" value=\"$value\">\n";
		}		
		reset($my_sort8);
	}
	
?>
<p align="center">根據您所指定的順序整理如下:</p>
<p align="center">本系統將幫您輸出簡易的課程名稱與代碼組合，供您在選課時更加方便^^</p>
<?
	if($my_sort1) $voice1 = final_output($my_sort1,"1");
	if($my_sort2) $voice2 = final_output($my_sort2,"2");
	if($my_sort3) $voice3 = final_output($my_sort3,"3");
	if($my_sort4) $voice4 = final_output($my_sort4,"4");
	if($my_sort5) $voice5 = final_output($my_sort5,"5");
	if($my_sort6) $voice6 = final_output($my_sort6,"6");
	if($my_sort7) $voice7 = final_output($my_sort7,"7");
	if($my_sort8) $voice8 = final_output($my_sort8,"8");	
	
?>
<p align="center">請您勾選您的<font size="3" color="#CC0099"> 篩選清單</font> 要列出之欄位</p>
<div align="center">
  <center>
  <table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" id="AutoNumber8" bgcolor="#E1FFE1">
    <tr>
      <td width="100%">
      <table border="0" cellpadding="4" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber9">
        <tr>
          <td nowrap><b><font size="3" color="#FF0000">建議項目</font></b></td>
          <td nowrap>:&nbsp; </td>
          <td nowrap>
          <input type="checkbox" name="want_field[who_open]" value="who_open" checked>開課系所</td>
          <td nowrap>
          <input type="checkbox" name="want_field[course_type]" value="course_type" checked>課程類別</td>
          <td nowrap>
          <input type="checkbox" name="want_field[voice_code]" value="voice_code" checked>語音代碼 
          </td>
          <td nowrap>
          <input type="checkbox" name="want_field[course_code]" value="course_code" checked>課程代碼 
          </td>
          <td nowrap>
          <input type="checkbox" name="want_field[course_name]" value="course_name" checked>課程名稱 
          </td>
          <td nowrap>
          <input type="checkbox" name="want_field[time_a]" value="time_a,time_b,time_c" checked>上課時間 
          </td>
          <td nowrap>
          <input type="checkbox" name="want_field[credit_point]" value="credit_point" checked>學分 
          </td>
        </tr>
        <tr>
          <td nowrap align="right">次要項目</td>
          <td nowrap>:</td>
          <td nowrap>
          <input type="checkbox" name="want_field[teacher]" value="teacher">授課教師</td>
          <td nowrap>
          <input type="checkbox" name="want_field[room_a]" value="room_a,room_b,room_c">上課教室&nbsp; 
          </td>
          <td nowrap>
          <input type="checkbox" name="want_field[course_session]" value="course_session">課程性質</td>
          <td nowrap>
          <input type="checkbox" name="want_field[memo]" value="memo">備註資料 </td>
          <td nowrap>
          <input type="checkbox" name="want_field[people_limit]" value="people_limit">開課人數&nbsp; 
          </td>
          <td nowrap>
          <input type="checkbox" name="want_field[filter]" value="filter">篩選人數</td>
          <td nowrap>
          <input type="checkbox" name="want_field[respond_unit]" value="respond_unit">權責單位</td>
        </tr>
      </table>
      </td>
    </tr>
  </table>
  </center>
</div>
<center><BR>如果沒有特殊需求，建議您直接使用<font color="#000080" size="3"> 預設值 </font>即可。
<hr noshade color="#111111" width="90%" size="1">
<BR><input type="submit" value="輸出清單">
<input type="reset" value="重新設定"></CENTER>
<?
	if($voice1)
	{	
		while(list($key,$value)=each($voice1))
		{
			echo "<input type=\"hidden\" name=\"voice1[$key]\" value=\"$value\">\n";
		}
	}	

	if($voice2)
	{	
		while(list($key,$value)=each($voice2))
		{
			echo "<input type=\"hidden\" name=\"voice2[$key]\" value=\"$value\">\n";
		}
	}	

	if($voice3)
	{	
		while(list($key,$value)=each($voice3))
		{
			echo "<input type=\"hidden\" name=\"voice3[$key]\" value=\"$value\">\n";
		}
	}	

	if($voice4)
	{	
		while(list($key,$value)=each($voice4))
		{
			echo "<input type=\"hidden\" name=\"voice4[$key]\" value=\"$value\">\n";
		}
	}	

	if($voice5)
	{	
		while(list($key,$value)=each($voice5))
		{
			echo "<input type=\"hidden\" name=\"voice5[$key]\" value=\"$value\">\n";
		}
	}	

	if($voice6)
	{	
		while(list($key,$value)=each($voice6))
		{
			echo "<input type=\"hidden\" name=\"voice6[$key]\" value=\"$value\">\n";
		}
	}	

	if($voice7)
	{	
		while(list($key,$value)=each($voice7))
		{
			echo "<input type=\"hidden\" name=\"voice7[$key]\" value=\"$value\">\n";
		}
	}	

	if($voice8)
	{	
		while(list($key,$value)=each($voice8))
		{
			echo "<input type=\"hidden\" name=\"voice8[$key]\" value=\"$value\">\n";
		}
	}	

?>
</form>
<?php
}


//---------------------------------------------
//最後動作，找時機將確定的所選項目寫回資料庫
//---------------------------------------------
function print_and_update()
{
   global $PHP_SELF, $std_num, $std_sex, $want_field, $origin, $my_sort1, $my_sort2, $my_sort3, $my_sort4, $my_sort5, $my_sort6, $my_sort7, $my_sort8;
   global $voice1,$voice2,$voice3,$voice4,$voice5,$voice6,$voice7,$voice8;
   
   if(!$want_field) error_message("您連一個欄位都沒勾唷，這樣是不行的唷!     ^_______^    ");
   
   if(is_int(strpos($PHP_SELF,"index.php")))
   {
   	if($my_sort1) update_to_sql($my_sort1);
   	if($my_sort2) update_to_sql($my_sort2);
   	if($my_sort3) update_to_sql($my_sort3);
   	if($my_sort4) update_to_sql($my_sort4);
   	if($my_sort5) update_to_sql($my_sort5);
   	if($my_sort6) update_to_sql($my_sort6);
   	if($my_sort7) update_to_sql($my_sort7);
   	if($my_sort8) update_to_sql($my_sort8);
   }
   
   foreach($origin as $ykey => $yvalue)
   {
   	$yvalue =str_replace(" ", "%20",$yvalue);
   	$print_final_timetable_array = $print_final_timetable_array . "&origin[$ykey]=$yvalue";
   }
   $print_final_timetable_array = $PHP_SELF . "?action=print_final_timetable" . $print_final_timetable_array;

   
   //篩選規劃表
   if($voice1)
   {
   	foreach($voice1 as $key => $value)
   	{   		
   		$print_voice_table = $print_voice_table . "&voice1[$key]=$value";
   	}
   }
   if($voice2)
   {
   	foreach($voice2 as $key => $value)
   	{   		
   		$print_voice_table = $print_voice_table . "&voice2[$key]=$value";
   	}
   }
   if($voice3)
   {
   	foreach($voice3 as $key => $value)
   	{   		
   		$print_voice_table = $print_voice_table . "&voice3[$key]=$value";
   	}
   }
   if($voice4)
   {
   	foreach($voice4 as $key => $value)
   	{   		
   		$print_voice_table = $print_voice_table . "&voice4[$key]=$value";
   	}
   }
   if($voice5)
   {
   	foreach($voice5 as $key => $value)
   	{   		
   		$print_voice_table = $print_voice_table . "&voice5[$key]=$value";
   	}
   }
   if($voice6)
   {
   	foreach($voice6 as $key => $value)
   	{   		
   		$print_voice_table = $print_voice_table . "&voice6[$key]=$value";
   	}
   }
   if($voice7)
   {
   	foreach($voice7 as $key => $value)
   	{   		
   		$print_voice_table = $print_voice_table . "&voice7[$key]=$value";
   	}
   }
   if($voice8)
   {
   	foreach($voice8 as $key => $value)
   	{   		
   		$print_voice_table = $print_voice_table . "&voice8[$key]=$value";
   	}
   }
         
    $print_voice_table = $PHP_SELF . "?action=print_voice_table" . $print_voice_table;

   

   foreach($want_field as $key => $value)
   {
   	$value =str_replace(",", "%2C",$value);
   	$print_final_list = $print_final_list . "&want_field[$key]=$value";
   }   
   
   if($my_sort1){foreach($my_sort1 as $xkey => $xvalue) $print_final_list = $print_final_list . "&my_sort1[$xkey]=$xvalue";}
   if($my_sort2){foreach($my_sort2 as $xkey => $xvalue) $print_final_list = $print_final_list . "&my_sort2[$xkey]=$xvalue";}
   if($my_sort3){foreach($my_sort3 as $xkey => $xvalue) $print_final_list = $print_final_list . "&my_sort3[$xkey]=$xvalue";}
   if($my_sort4){foreach($my_sort4 as $xkey => $xvalue) $print_final_list = $print_final_list . "&my_sort4[$xkey]=$xvalue";}
   if($my_sort5){foreach($my_sort5 as $xkey => $xvalue) $print_final_list = $print_final_list . "&my_sort5[$xkey]=$xvalue";}
   if($my_sort6){foreach($my_sort6 as $xkey => $xvalue) $print_final_list = $print_final_list . "&my_sort6[$xkey]=$xvalue";}
   if($my_sort7){foreach($my_sort7 as $xkey => $xvalue) $print_final_list = $print_final_list . "&my_sort7[$xkey]=$xvalue";}
   if($my_sort8){foreach($my_sort8 as $xkey => $xvalue) $print_final_list = $print_final_list . "&my_sort8[$xkey]=$xvalue";}	
      
   $print_final_list = $PHP_SELF . "?action=print_final_list" . $print_final_list;
     
?>
<center>小秘訣 : 如果要列印下面表格或另存新檔，只要按下「新視窗」即可替您個別顯示!<hr noshade color="#111111" width="90%" size="1"></center>
<center><br><font size="3" color="#008000"><b>篩選清單</b></font> (<a href="<? echo $print_final_list; ?>" target="_blank">新視窗</a>)<table border="0" cellpadding="8" cellspacing="0"><tr>
<td width="100%"><center><table border="1" cellpadding="4" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111">
<?
   if($my_sort1) {print_for_you($want_field,$my_sort1,"1");$total_num = $total_num + get_credit($my_sort1);}
   if($my_sort2) {print_for_you($want_field,$my_sort2,"2");$total_num = $total_num + get_credit($my_sort2);}
   if($my_sort3) {print_for_you($want_field,$my_sort3,"3");$total_num = $total_num + get_credit($my_sort3);}
   if($my_sort4) {print_for_you($want_field,$my_sort4,"4");$total_num = $total_num + get_credit($my_sort4);}
   if($my_sort5) {print_for_you($want_field,$my_sort5,"5");$total_num = $total_num + get_credit($my_sort5);}
   if($my_sort6) {print_for_you($want_field,$my_sort6,"6");$total_num = $total_num + get_credit($my_sort6);}
   if($my_sort7) {print_for_you($want_field,$my_sort7,"7");$total_num = $total_num + get_credit($my_sort7);}
   if($my_sort8) {print_for_you($want_field,$my_sort8,"8");$total_num = $total_num + get_credit($my_sort8);}
   
?></table></center></td></tr></table>(共計 <? echo $total_num ?> 學分)</center><center><P><font size="3" color="#008000"><b>篩選規劃表</b></font> (<a href="<? echo $print_voice_table; ?>" target="_blank">新視窗</a>)<P><?

	show_voice_table($voice1,$voice2,$voice3,$voice4,$voice5,$voice6,$voice7,$voice8);

?><center><P><table border="0" cellpadding="8" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111">
    <tr>
      <td width="100%" align="center"><font size="3" color="#0000FF"><b>必修課程表</b></font> (<a href="<? echo $print_final_timetable_array; ?>" target="_blank">新視窗</a>)
      <CENTER>您已修 <FONT SIZE=3 COLOR=RED><? echo get_credit($origin) ?></FONT> 學分囉</CENTER><P><?
   fill_timetable(get_course_time($origin));
?></td>
    </tr>
  </table>
<hr noshade color="#111111" width="90%" size="1"></center>
<p align="center">謝謝您的使用，如果有任何更好的建議，請告訴我們!!</p>
<p align="center">* ^_______________^ *</p>
<p align="center"><a href="index.php">回到首頁</a>　<a target="_blank" href="http://best.ax.com.tw/cgi-bin/forums.cgi?forum=140">留言反映</a>　<a href="mailto:saturn@ax.com.tw?subject=有關選課輔助系統">寫信給我</a>　<a href="telnet://140.135.12.1">來去心站</a></p>
<?php
}


//將選課陣列塞入課表
function fill_timetable($course_array){
	
$for_replace = array(              
              "0" => "      <td nowrap align=center id=10></td>\n      <td nowrap align=center id=20></td>\n      <td nowrap align=center id=30></td>\n      <td nowrap align=center id=40></td>\n      <td nowrap align=center id=50></td>\n      <td nowrap align=center id=60></td>\n",
              "A" => "      <td nowrap align=center id=1A></td>\n      <td nowrap align=center id=2A></td>\n      <td nowrap align=center id=3A></td>\n      <td nowrap align=center id=4A></td>\n      <td nowrap align=center id=5A></td>\n      <td nowrap align=center id=6A></td>\n",
              "1" => "      <td nowrap align=center id=11></td>\n      <td nowrap align=center id=21></td>\n      <td nowrap align=center id=31></td>\n      <td nowrap align=center id=41></td>\n      <td nowrap align=center id=51></td>\n      <td nowrap align=center id=61></td>\n",
              "2" => "      <td nowrap align=center id=12></td>\n      <td nowrap align=center id=22></td>\n      <td nowrap align=center id=32></td>\n      <td nowrap align=center id=42></td>\n      <td nowrap align=center id=52></td>\n      <td nowrap align=center id=62></td>\n",
              "3" => "      <td nowrap align=center id=13></td>\n      <td nowrap align=center id=23></td>\n      <td nowrap align=center id=33></td>\n      <td nowrap align=center id=43></td>\n      <td nowrap align=center id=53></td>\n      <td nowrap align=center id=63></td>\n",
              "4" => "      <td nowrap align=center id=14></td>\n      <td nowrap align=center id=24></td>\n      <td nowrap align=center id=34></td>\n      <td nowrap align=center id=44></td>\n      <td nowrap align=center id=54></td>\n      <td nowrap align=center id=64></td>\n",
              "B" => "      <td nowrap align=center id=1B></td>\n      <td nowrap align=center id=2B></td>\n      <td nowrap align=center id=3B></td>\n      <td nowrap align=center id=4B></td>\n      <td nowrap align=center id=5B></td>\n      <td nowrap align=center id=6B></td>\n",
              "5" => "      <td nowrap align=center id=15></td>\n      <td nowrap align=center id=25></td>\n      <td nowrap align=center id=35></td>\n      <td nowrap align=center id=45></td>\n      <td nowrap align=center id=55></td>\n      <td nowrap align=center id=65></td>\n",
              "6" => "      <td nowrap align=center id=16></td>\n      <td nowrap align=center id=26></td>\n      <td nowrap align=center id=36></td>\n      <td nowrap align=center id=46></td>\n      <td nowrap align=center id=56></td>\n      <td nowrap align=center id=66></td>\n",
              "7" => "      <td nowrap align=center id=17></td>\n      <td nowrap align=center id=27></td>\n      <td nowrap align=center id=37></td>\n      <td nowrap align=center id=47></td>\n      <td nowrap align=center id=57></td>\n      <td nowrap align=center id=67></td>\n",
              "8" => "      <td nowrap align=center id=18></td>\n      <td nowrap align=center id=28></td>\n      <td nowrap align=center id=38></td>\n      <td nowrap align=center id=48></td>\n      <td nowrap align=center id=58></td>\n      <td nowrap align=center id=68></td>\n",
              "C" => "      <td nowrap align=center id=1C></td>\n      <td nowrap align=center id=2C></td>\n      <td nowrap align=center id=3C></td>\n      <td nowrap align=center id=4C></td>\n      <td nowrap align=center id=5C></td>\n      <td nowrap align=center id=6C></td>\n",
              "D" => "      <td nowrap align=center id=1D></td>\n      <td nowrap align=center id=2D></td>\n      <td nowrap align=center id=3D></td>\n      <td nowrap align=center id=4D></td>\n      <td nowrap align=center id=5D></td>\n      <td nowrap align=center id=6D></td>\n",
              "E" => "      <td nowrap align=center id=1E></td>\n      <td nowrap align=center id=2E></td>\n      <td nowrap align=center id=3E></td>\n      <td nowrap align=center id=4E></td>\n      <td nowrap align=center id=5E></td>\n      <td nowrap align=center id=6E></td>\n",
              "F" => "      <td nowrap align=center id=1F></td>\n      <td nowrap align=center id=2F></td>\n      <td nowrap align=center id=3F></td>\n      <td nowrap align=center id=4F></td>\n      <td nowrap align=center id=5F></td>\n      <td nowrap align=center id=6F></td>\n",
              "G" => "      <td nowrap align=center id=1G></td>\n      <td nowrap align=center id=2G></td>\n      <td nowrap align=center id=3G></td>\n      <td nowrap align=center id=4G></td>\n      <td nowrap align=center id=5G></td>\n      <td nowrap align=center id=6G></td>\n",
              "H" => "      <td nowrap align=center id=1H></td>\n      <td nowrap align=center id=2H></td>\n      <td nowrap align=center id=3H></td>\n      <td nowrap align=center id=4H></td>\n      <td nowrap align=center id=5H></td>\n      <td nowrap align=center id=6H></td>\n"
            );
	
	while(list($course_key, $course_value) = each($course_array))
	{
		$block_num = strlen($course_key);
		if($block_num==3)
		{
			$find = "id=" . substr($course_key,0,1) . substr($course_key,2,1) . "><";
			$repl = "id=" . substr($course_key,0,1) . substr($course_key,2,1) . ">" . $course_value ."<";
			$key_at = substr($course_key,2,1);
			$for_replace[$key_at] =str_replace($find,$repl,$for_replace[$key_at]);
			//$key_use = substr($course_key,0,1) . substr($course_key,2,1);
			//$hide_post[$key_use] = "  <INPUT TYPE=\"hidden\" NAME=\"fixed_time[]\" VALUE=\"$key_use\">";
		}
		else
		{
			for($z=2;$z<$block_num;$z++)
			{		
				$find_array[$z] = "id=" . substr($course_key,0,1) . substr($course_key,$z,1) . "><";
				$repl_array[$z] = "id=" . substr($course_key,0,1) . substr($course_key,$z,1) . ">" . $course_value ."<";
				$key_at = substr($course_key,$z,1);
				$for_replace[$key_at] =str_replace($find_array[$z],$repl_array[$z],$for_replace[$key_at]);
				//$key_use = substr($course_key,0,1) . substr($course_key,$z,1);
				//$hide_post[$key_use] = "  <INPUT TYPE=\"hidden\" NAME=\"fixed_time[]\" VALUE=\"$key_use\">";
			}
		}		
	
	}

	include("table_first.php");
	$a = 1;
	while(list($course_key,$course_value)=each($for_replace))
	{
		echo $course_value . "\n";
		$target_file = $a . ".php";
		@include($target_file);
		$a++;
	}
	
	//foreach($hide_post as $valuex)	echo "\n$valuex";
}

//傳回沒有衝堂的課程資料
function compare_me($course_array)
{	
	$course_arrayX = $course_array;
	while(list($course_key, $course_value) = each($course_array))
	{	
		if($course_array[$course_key]==$get_out[$course_key]) continue;
		reset($course_arrayX);
		while(list($course_keyX, $course_valueX) = each($course_arrayX))
		{
			if($course_key==$course_keyX) continue;			
			if(strpos($course_value," ")==false&&strpos($course_valueX," ")==false)
			{		
				$block_num1 = strlen($course_value);
				$block_num2 = strlen($course_valueX);
					
				if($block_num1==3&&$block_num2==3)
				{
					$time_at1 = substr($course_value,0,1) . substr($course_value,2,1);
					$time_at2 = substr($course_valueX,0,1) . substr($course_valueX,2,1);					
					
					if($time_at1==$time_at2)
					{
						$get_out[$course_key]=$course_value;						
					}				
				}
				elseif($block_num1==3&&$block_num2!=3)
				{
					$time_at1 = substr($course_value,0,1) . substr($course_value,2,1);
					
					for($z=2;$z<$block_num2;$z++)
					{							
						$time_at2 = substr($course_valueX,0,1) . substr($course_valueX,$z,1);
						
						if($time_at1==$time_at2)
						{
							$get_out[$course_key]=$course_value;
							break;							
						}				
					}
				}
				elseif($block_num1!=3&&$block_num2==3)
				{
					$time_at2 = substr($course_valueX,0,1) . substr($course_valueX,2,1);
					
					for($z=2;$z<$block_num1;$z++)
					{		
						$time_at1 = substr($course_value,0,1) . substr($course_value,$z,1);						
						
						if($time_at1==$time_at2)
						{
							$get_out[$course_key]=$course_value;
							break;							
						}									
					}
				}
				elseif($block_num1!=3&&$block_num2!=3)
				{
					for($z=2;$z<$block_num1;$z++)
					{							
						$time_at1 = substr($course_value,0,1) . substr($course_value,$z,1);							
						for($y=2;$y<$block_num2;$y++)
						{								
							$time_at2 = substr($course_valueX,0,1) . substr($course_valueX,$y,1);
						
							if($time_at1==$time_at2)
							{
								$get_out[$course_key]=$course_value;
								break;							
							}		
						}			
					}
				}
			}
			elseif(strpos($course_value," ")!=false&&strpos($course_valueX," ")==false)
			{
				$course_value_array = explode(" ", $course_value);
				for($b=0;$b<sizeof($course_value_array);$b++)
				{									
				
					$block_num1 = strlen($course_value_array[$b]);
					$block_num2 = strlen($course_valueX);
						
					if($block_num1==3&&$block_num2==3)
					{
						$time_at1 = substr($course_value_array[$b],0,1) . substr($course_value_array[$b],2,1);
						$time_at2 = substr($course_valueX,0,1) . substr($course_valueX,2,1);					
						
						if($time_at1==$time_at2)
						{
							$get_out[$course_key]=$course_value;						
						}				
					}
					elseif($block_num1==3&&$block_num2!=3)
					{
						$time_at1 = substr($course_value_array[$b],0,1) . substr($course_value_array[$b],2,1);
						
						for($z=2;$z<$block_num2;$z++)
						{							
							$time_at2 = substr($course_valueX,0,1) . substr($course_valueX,$z,1);
							
							if($time_at1==$time_at2)
							{
								$get_out[$course_key]=$course_value;
								break;							
							}				
						}
					}
					elseif($block_num1!=3&&$block_num2==3)
					{
						$time_at2 = substr($course_valueX,0,1) . substr($course_valueX,2,1);
						
						for($z=2;$z<$block_num1;$z++)
						{		
							$time_at1 = substr($course_value_array[$b],0,1) . substr($course_value_array[$b],$z,1);						
						
							if($time_at1==$time_at2)
							{
								$get_out[$course_key]=$course_value;
								break;							
							}									
						}
					}
					elseif($block_num1!=3&&$block_num2!=3)
					{
						for($z=2;$z<$block_num1;$z++)
						{							
							$time_at1 = substr($course_value_array[$b],0,1) . substr($course_value_array[$b],$z,1);							
							for($y=2;$y<$block_num2;$y++)
							{								
								$time_at2 = substr($course_valueX,0,1) . substr($course_valueX,$y,1);
							
								if($time_at1==$time_at2)
								{
									$get_out[$course_key]=$course_value;
									break;							
								}		
							}			
						}
					}					
				}
			}
			elseif(strpos($course_value," ")==false&&strpos($course_valueX," ")!=false)
			{
				$course_valueX_array = explode(" ", $course_valueX);
				for($b=0;$b<sizeof($course_valueX_array);$b++)
				{									
				
					$block_num1 = strlen($course_value);
					$block_num2 = strlen($course_valueX_array[$b]);
						
					if($block_num1==3&&$block_num2==3)
					{
						$time_at1 = substr($course_value,0,1) . substr($course_value,2,1);
						$time_at2 = substr($course_valueX_array[$b],0,1) . substr($course_valueX_array[$b],2,1);					
						
						if($time_at1==$time_at2)
						{
							$get_out[$course_key]=$course_value;						
						}				
					}
					elseif($block_num1==3&&$block_num2!=3)
					{
						$time_at1 = substr($course_value,0,1) . substr($course_value,2,1);
						
						for($z=2;$z<$block_num2;$z++)
						{							
							$time_at2 = substr($course_valueX_array[$b],0,1) . substr($course_valueX_array[$b],$z,1);
							
							if($time_at1==$time_at2)
							{
								$get_out[$course_key]=$course_value;
								break;							
							}				
						}
					}
					elseif($block_num1!=3&&$block_num2==3)
					{
						$time_at2 = substr($course_valueX_array[$b],0,1) . substr($course_valueX_array[$b],2,1);
						
						for($z=2;$z<$block_num1;$z++)
						{		
							$time_at1 = substr($course_value,0,1) . substr($course_value,$z,1);						
						
							if($time_at1==$time_at2)
							{
								$get_out[$course_key]=$course_value;
								break;							
							}									
						}
					}
					elseif($block_num1!=3&&$block_num2!=3)
					{
						for($z=2;$z<$block_num1;$z++)
						{							
							$time_at1 = substr($course_value,0,1) . substr($course_value,$z,1);							
							for($y=2;$y<$block_num2;$y++)
							{								
								$time_at2 = substr($course_valueX_array[$b],0,1) . substr($course_valueX_array[$b],$y,1);
							
								if($time_at1==$time_at2)
								{
									$get_out[$course_key]=$course_value;
									break;							
								}		
							}			
						}
					}					
				}
			}
			elseif(strpos($course_value," ")!=false&&strpos($course_valueX," ")!=false)
			{
				$course_value_array = explode(" ", $course_value);
				$course_valueX_array = explode(" ", $course_valueX);
				
				for($b=0;$b<sizeof($course_value_array);$b++)
				{
					for($c=0;$c<sizeof($course_valueX_array);$c++)
					{				
						$block_num1 = strlen($course_value_array[$b]);
						$block_num2 = strlen($course_valueX_array[$c]);
						
						if($block_num1==3&&$block_num2==3)
						{
							$time_at1 = substr($course_value_array[$b],0,1) . substr($course_value_array[$b],2,1);
							$time_at2 = substr($course_valueX_array[$c],0,1) . substr($course_valueX_array[$c],2,1);					
						
							if($time_at1==$time_at2)
							{
								$get_out[$course_key]=$course_value;						
							}				
						}
						elseif($block_num1==3&&$block_num2!=3)
						{
							$time_at1 = substr($course_value_array[$b],0,1) . substr($course_value_array[$b],2,1);
						
							for($z=2;$z<$block_num2;$z++)
							{							
								$time_at2 = substr($course_valueX_array[$c],0,1) . substr($course_valueX_array[$c],$z,1);
								
								if($time_at1==$time_at2)
								{
									$get_out[$course_key]=$course_value;
									break;							
								}				
							}
						}
						elseif($block_num1!=3&&$block_num2==3)
						{
							$time_at2 = substr($course_valueX_array[$c],0,1) . substr($course_valueX_array[$c],2,1);
						
							for($z=2;$z<$block_num1;$z++)
							{		
								$time_at1 = substr($course_value_array[$b],0,1) . substr($course_value_array[$b],$z,1);						
							
								if($time_at1==$time_at2)
								{
									$get_out[$course_key]=$course_value;
									break;							
								}									
							}
						}
						elseif($block_num1!=3&&$block_num2!=3)
						{
							for($z=2;$z<$block_num1;$z++)
							{							
								$time_at1 = substr($course_value_array[$b],0,1) . substr($course_value_array[$b],$z,1);							
								for($y=2;$y<$block_num2;$y++)
								{								
									$time_at2 = substr($course_valueX_array[$c],0,1) . substr($course_valueX_array[$c],$y,1);
								
									if($time_at1==$time_at2)
									{
										$get_out[$course_key]=$course_value;
										break;							
									}		
								}			
							}
						}					
					}
				}
			}	
		}			
	}
	
	@reset($course_array);
	if($get_out)
	{
		while(list($mykey,$myvalue)=each($course_array))
		{
			if(!in_array($myvalue,$get_out)) $final_array[$mykey]=$myvalue;
		}
	}
	else
	{
		$final_array = $course_array;
	}
	return $final_array;
}

//跟固定課表比較，傳回沒有衝堂的課程資料
function compare_us($course_array,$new_array)
{
	$course_arrayX = $new_array;
	while(list($course_key, $course_value) = each($course_array))
	{	
		reset($course_arrayX);
		while(list($course_keyX, $course_valueX) = each($course_arrayX))
		{
			if($course_key==$course_keyX) continue;			
			if(strpos($course_value," ")==false&&strpos($course_valueX," ")==false)
			{		
				$block_num1 = strlen($course_value);
				$block_num2 = strlen($course_valueX);
					
				if($block_num1==3&&$block_num2==3)
				{
					$time_at1 = substr($course_value,0,1) . substr($course_value,2,1);
					$time_at2 = substr($course_valueX,0,1) . substr($course_valueX,2,1);					
					
					if($time_at1==$time_at2)
					{
						$get_out[$course_keyX]="X";
					}				
				}
				elseif($block_num1==3&&$block_num2!=3)
				{
					$time_at1 = substr($course_value,0,1) . substr($course_value,2,1);
					
					for($z=2;$z<$block_num2;$z++)
					{							
						$time_at2 = substr($course_valueX,0,1) . substr($course_valueX,$z,1);
						
						if($time_at1==$time_at2)
						{
							$get_out[$course_keyX]="X";
							break;							
						}				
					}
				}
				elseif($block_num1!=3&&$block_num2==3)
				{
					$time_at2 = substr($course_valueX,0,1) . substr($course_valueX,2,1);
					
					for($z=2;$z<$block_num1;$z++)
					{		
						$time_at1 = substr($course_value,0,1) . substr($course_value,$z,1);						
						
						if($time_at1==$time_at2)
						{
							$get_out[$course_keyX]="X";
							break;							
						}									
					}
				}
				elseif($block_num1!=3&&$block_num2!=3)
				{
					for($z=2;$z<$block_num1;$z++)
					{							
						$time_at1 = substr($course_value,0,1) . substr($course_value,$z,1);							
						for($y=2;$y<$block_num2;$y++)
						{								
							$time_at2 = substr($course_valueX,0,1) . substr($course_valueX,$y,1);
						
							if($time_at1==$time_at2)
							{
								$get_out[$course_keyX]="X";
								break;							
							}		
						}			
					}
				}
			}
			elseif(strpos($course_value," ")!=false&&strpos($course_valueX," ")==false)
			{
				$course_value_array = explode(" ", $course_value);
				for($b=0;$b<sizeof($course_value_array);$b++)
				{									
				
					$block_num1 = strlen($course_value_array[$b]);
					$block_num2 = strlen($course_valueX);
						
					if($block_num1==3&&$block_num2==3)
					{
						$time_at1 = substr($course_value_array[$b],0,1) . substr($course_value_array[$b],2,1);
						$time_at2 = substr($course_valueX,0,1) . substr($course_valueX,2,1);					
						
						if($time_at1==$time_at2)
						{
							$get_out[$course_keyX]="X";						
						}				
					}
					elseif($block_num1==3&&$block_num2!=3)
					{
						$time_at1 = substr($course_value_array[$b],0,1) . substr($course_value_array[$b],2,1);
						
						for($z=2;$z<$block_num2;$z++)
						{							
							$time_at2 = substr($course_valueX,0,1) . substr($course_valueX,$z,1);
							
							if($time_at1==$time_at2)
							{
								$get_out[$course_keyX]="X";
								break;							
							}				
						}
					}
					elseif($block_num1!=3&&$block_num2==3)
					{
						$time_at2 = substr($course_valueX,0,1) . substr($course_valueX,2,1);
						
						for($z=2;$z<$block_num1;$z++)
						{		
							$time_at1 = substr($course_value_array[$b],0,1) . substr($course_value_array[$b],$z,1);						
						
							if($time_at1==$time_at2)
							{
								$get_out[$course_keyX]="X";
								break;							
							}									
						}
					}
					elseif($block_num1!=3&&$block_num2!=3)
					{
						for($z=2;$z<$block_num1;$z++)
						{							
							$time_at1 = substr($course_value_array[$b],0,1) . substr($course_value_array[$b],$z,1);							
							for($y=2;$y<$block_num2;$y++)
							{								
								$time_at2 = substr($course_valueX,0,1) . substr($course_valueX,$y,1);
							
								if($time_at1==$time_at2)
								{
									$get_out[$course_keyX]="X";
									break;							
								}		
							}			
						}
					}					
				}
			}
			elseif(strpos($course_value," ")==false&&strpos($course_valueX," ")!=false)
			{
				$course_valueX_array = explode(" ", $course_valueX);
				for($b=0;$b<sizeof($course_valueX_array);$b++)
				{									
				
					$block_num1 = strlen($course_value);
					$block_num2 = strlen($course_valueX_array[$b]);
						
					if($block_num1==3&&$block_num2==3)
					{
						$time_at1 = substr($course_value,0,1) . substr($course_value,2,1);
						$time_at2 = substr($course_valueX_array[$b],0,1) . substr($course_valueX_array[$b],2,1);					
						
						if($time_at1==$time_at2)
						{
							$get_out[$course_keyX]="X";						
						}				
					}
					elseif($block_num1==3&&$block_num2!=3)
					{
						$time_at1 = substr($course_value,0,1) . substr($course_value,2,1);
						
						for($z=2;$z<$block_num2;$z++)
						{							
							$time_at2 = substr($course_valueX_array[$b],0,1) . substr($course_valueX_array[$b],$z,1);
							
							if($time_at1==$time_at2)
							{
								$get_out[$course_keyX]="X";
								break;							
							}				
						}
					}
					elseif($block_num1!=3&&$block_num2==3)
					{
						$time_at2 = substr($course_valueX_array[$b],0,1) . substr($course_valueX_array[$b],2,1);
						
						for($z=2;$z<$block_num1;$z++)
						{		
							$time_at1 = substr($course_value,0,1) . substr($course_value,$z,1);						
						
							if($time_at1==$time_at2)
							{
								$get_out[$course_keyX]="X";
								break;							
							}									
						}
					}
					elseif($block_num1!=3&&$block_num2!=3)
					{
						for($z=2;$z<$block_num1;$z++)
						{							
							$time_at1 = substr($course_value,0,1) . substr($course_value,$z,1);							
							for($y=2;$y<$block_num2;$y++)
							{								
								$time_at2 = substr($course_valueX_array[$b],0,1) . substr($course_valueX_array[$b],$y,1);
							
								if($time_at1==$time_at2)
								{
									$get_out[$course_keyX]="X";
									break;							
								}		
							}			
						}
					}					
				}
			}
			elseif(strpos($course_value," ")!=false&&strpos($course_valueX," ")!=false)
			{
				$course_value_array = explode(" ", $course_value);
				$course_valueX_array = explode(" ", $course_valueX);
				
				for($b=0;$b<sizeof($course_value_array);$b++)
				{
					for($c=0;$c<sizeof($course_valueX_array);$c++)
					{				
						$block_num1 = strlen($course_value_array[$b]);
						$block_num2 = strlen($course_valueX_array[$c]);
						
						if($block_num1==3&&$block_num2==3)
						{
							$time_at1 = substr($course_value_array[$b],0,1) . substr($course_value_array[$b],2,1);
							$time_at2 = substr($course_valueX_array[$c],0,1) . substr($course_valueX_array[$c],2,1);					
						
							if($time_at1==$time_at2)
							{
								$get_out[$course_keyX]="X";						
							}				
						}
						elseif($block_num1==3&&$block_num2!=3)
						{
							$time_at1 = substr($course_value_array[$b],0,1) . substr($course_value_array[$b],2,1);
						
							for($z=2;$z<$block_num2;$z++)
							{							
								$time_at2 = substr($course_valueX_array[$c],0,1) . substr($course_valueX_array[$c],$z,1);
								
								if($time_at1==$time_at2)
								{
									$get_out[$course_keyX]="X";
									break;							
								}				
							}
						}
						elseif($block_num1!=3&&$block_num2==3)
						{
							$time_at2 = substr($course_valueX_array[$c],0,1) . substr($course_valueX_array[$c],2,1);
						
							for($z=2;$z<$block_num1;$z++)
							{		
								$time_at1 = substr($course_value_array[$b],0,1) . substr($course_value_array[$b],$z,1);						
							
								if($time_at1==$time_at2)
								{
									$get_out[$course_keyX]="X";
									break;							
								}									
							}
						}
						elseif($block_num1!=3&&$block_num2!=3)
						{
							for($z=2;$z<$block_num1;$z++)
							{							
								$time_at1 = substr($course_value_array[$b],0,1) . substr($course_value_array[$b],$z,1);							
								for($y=2;$y<$block_num2;$y++)
								{								
									$time_at2 = substr($course_valueX_array[$c],0,1) . substr($course_valueX_array[$c],$y,1);
								
									if($time_at1==$time_at2)
									{
										$get_out[$course_keyX]="X";
										break;							
									}		
								}			
							}
						}					
					}
				}
			}	
		}			
	}
	
	reset($new_array);
	if($get_out)	
	{
		reset($get_out);
		while(list($xkey,$xvalue)=each($new_array))
		{
			if($get_out[$xkey]=="X") $new_array[$xkey]="X";
		}
	}
	
	reset($new_array);
	while(list($xkey,$xvalue)=each($new_array))
	{
		if($xvalue!="X") $final_array[$xkey]=$xvalue;
	}
	
	return $final_array;

}

//傳入課程代號"字串"，分割符號，輸出可供 XXXX[課程代號]="1-23 4-56 7-89" 類型的陣列
function course_str_compare($string,$char)
{
	$mysql_server_name = "localhost";
	$mysql_username    = "root";
	$mysql_password    = "";
	$mysql_database    = "cycu";
	
	$conn1=mysql_connect( $mysql_server_name, $mysql_username, $mysql_password);
				
	if(strstr($string,$char))
	{
		$temp_array = explode($char,$string);
		foreach($temp_array as $value)
		{
			$result=mysql_db_query($mysql_database, "select time_a,time_b,time_c from cc922 where course_code = '$value' LIMIT 0, 1",$conn1);
			@$row=mysql_fetch_row($result);
	
			if($row)
			{				
				mysql_data_seek($result,0);
				while($row=mysql_fetch_row($result))
				{        
					$output[$value] = $row[0];
					if($row[1]) $output[$value] = $output[$value] . " " . $row[1];
					if($row[2]) $output[$value] = $output[$value] . " " . $row[2];
       				}
			}
			@mysql_free_result($result);
		}	
		
	}
	else
	{
		$result=mysql_db_query($mysql_database, "select time_a,time_b,time_c from cc922 where course_code = '$string' LIMIT 0, 1",$conn1);
		@$row=mysql_fetch_row($result);
	
		if($row)
		{				
			mysql_data_seek($result,0);
			while($row=mysql_fetch_row($result))
			{        
				$output[$string] = $row[0];
				if($row[1]) $output[$string] = $output[$value] . " " . $row[1];
				if($row[2]) $output[$string] = $output[$value] . " " . $row[2];
       			}
		}			
			
		@mysql_free_result($result);		
	}	
	return $output;
}

//傳入課程代號，輸出可供填入課表的時段、說明陣列
function get_course_time($course_code)
{

	$mysql_server_name = "localhost";
	$mysql_username    = "root";
	$mysql_password    = "";
	$mysql_database    = "cycu";
	
	$conn1=mysql_connect( $mysql_server_name, $mysql_username, $mysql_password);	
	
	while(list($xkey,$xvalue)=each($course_code))
	{
		if($xkey=="(另有安排)")
		{
			if(strstr($xvalue," "))
			{
				$temp_array = explode(" ",$xvalue);
				foreach($temp_array as $vvalue)
				{

					$output[$vvalue]=$xkey;	
				}
			}
			else
			{
				$output[$xvalue]=$xkey;
			}
			continue;
		}		
		$result=mysql_db_query($mysql_database, "select course_name,teacher from cc922 where course_code = '$xkey' LIMIT 0, 1",$conn1);
		@$row=mysql_fetch_row($result);
	
		if($row)
		{				
			mysql_data_seek($result,0);
			while($row=mysql_fetch_row($result))
			{        
				if(strpos($xvalue," ")!=false)
				{
					$temp_array = explode(" ", $xvalue);
					$output[$temp_array[0]] = "<font color=red>$row[0]</font><br>$row[1]";
	       				$output[$temp_array[1]] = "<font color=red>$row[0]</font><br>$row[1]";
       					if($temp_array[2]) $output[$temp_array[2]] = "<font color=red>$row[0]</font><br>$row[1]";
				}
				else
				{
					$output[$xvalue] = "<font color=red>$row[0]</font><br>$row[1]";
       				}
			}			
		}	
		@mysql_free_result($result);
	}
	
	return $output;
}

//傳入課程類別名稱，直接輸出該類別時段符合的課程
function get_detail_course($course_type,$course_code,$x_num)
{

	$mysql_server_name = "localhost";
	$mysql_username    = "root";
	$mysql_password    = "";
	$mysql_database    = "cycu";
	
	$conn1=mysql_connect( $mysql_server_name, $mysql_username, $mysql_password);
	?>
    	<p align="center">以下的課程屬於<font size="3" color="#FF0000"><?php echo $course_type; ?>類課程</font>，請依興趣勾選想要的科目</p>
    	<center>
	<table bgColor="dodgerblue" style="border-collapse: collapse" bordercolor="#111111" cellpadding="2" cellspacing="0" border="1">
      	<tr bgColor="skyblue"><td noWrap bgcolor="#CCFFFF">開課系所</td><td bgcolor="#CCFFFF">類別</td><td noWrap bgcolor="#CCFFFF">語音代碼</td><td noWrap bgcolor="#CCFFFF">課程代碼</td><td noWrap bgcolor="#CCFFFF">課程名稱</td><td bgcolor="#CCFFFF">性質</td><td bgcolor="#CCFFFF">學分</td><td noWrap bgcolor="#CCFFFF">教師</td><td noWrap bgcolor="#CCFFFF">時間A</td><td noWrap bgcolor="#CCFFFF">時間B</td><td noWrap bgcolor="#CCFFFF">時間C</td><td noWrap bgcolor="#CCFFFF">備註</td><td noWrap bgcolor="#CCFFFF">篩選</td><td noWrap bgcolor="#CCFFFF" align="center">人氣</td><td noWrap bgcolor="#CCFFFF" align="center">修課人數</td><td noWrap bgcolor="#66FF99"><font size="3">我要</font></td></tr>      		
	<?		
	while(list($xkey,$xvalue)=each($course_code))
	{
		$result=mysql_db_query( $mysql_database, "select * from cc922 where course_code = '$xkey' LIMIT 0, 1",$conn1);
		@$row=mysql_fetch_row($result);
		if($row){
			mysql_data_seek($result,0);
			while($row=mysql_fetch_row($result))
			{
				if($row[8]==0)
				{
					$some = "半";
				}
				else
				{
					$some = "全";
				}
				
				$time_total = $row[13];
				if($row[15]) $time_total = $time_total . " " . $row[15];
				if($row[17]) $time_total = $time_total . " " . $row[17];
				
				echo "<tr bgColor=\"lightyellow\">";
        			echo "<td noWrap>$row[3]</td>";
        			echo "<td noWrap>$row[22]</td>";
        			echo "<td noWrap>$row[4]</td>";
        			echo "<td noWrap>$row[5]</td>";
	        		echo "<td noWrap>$row[6]</td>";
        			echo "<td noWrap>$some</td>";
	        		echo "<td noWrap>$row[10]</td>";
		        	echo "<td noWrap>$row[11]</td>";
		        	echo "<td noWrap>$row[13]</td>";
		        	echo "<td noWrap>$row[15]</td>";
		        	echo "<td noWrap>$row[17]</td>";
		        	echo "<td noWrap>$row[19]</td>";
        			echo "<td noWrap><FONT COLOR=BLUE>$row[18]</td>";
        			echo "<td noWrap align=center><FONT COLOR=#008000>$row[23]</td>";
        			echo "<td noWrap align=center><FONT COLOR=RED>$row[24]</td>";
		        	echo "<td noWrap align=center bgcolor=#CCFFCC>";
		        	echo "<input type=\"checkbox\" name=\"add_need" . $x_num . "[$row[5]]\" value=\"$time_total\"></td></tr>\n";			

			}
		}
		@mysql_free_result($result);
	}
	?>
	</table></center>
	<p align="center">只要勾選<b><font color="#008000" size="3"> 我要 </font></b>，就可以將項目納入您的篩選志願中。</p>
	<hr noshade color="#111111" width="90%" size="1">
<?	
}

//傳入課程、時間陣列，直接輸出課程清單提供排序
function output_for_sort($course_data,$x_num,$y_num)
{

	$mysql_server_name = "localhost";
	$mysql_username    = "root";
	$mysql_password    = "";
	$mysql_database    = "cycu";
	
	$conn1=mysql_connect( $mysql_server_name, $mysql_username, $mysql_password);	
	if(count($course_data)>$x_num)$temp_string = "<option value=\"0\">---</option>";
	for($i=1;$i<=count($course_data);$i++)
	{	
		$temp_string = $temp_string . "<option>$i</option>";
		if($i==$x_num) break;
	}	
	?>    	
    	<center>
	<table bgColor="dodgerblue" style="border-collapse: collapse" bordercolor="#111111" cellpadding="2" cellspacing="0" border="1">
      	<tr bgColor="skyblue"><td noWrap bgcolor="#CCFFFF">開課系所</td><td bgcolor="#CCFFFF">類別</td><td noWrap bgcolor="#CCFFFF">語音代碼</td><td noWrap bgcolor="#CCFFFF">課程代碼</td><td noWrap bgcolor="#CCFFFF">課程名稱</td><td bgcolor="#CCFFFF">性質</td><td bgcolor="#CCFFFF">學分</td><td noWrap bgcolor="#CCFFFF">教師</td><td noWrap bgcolor="#CCFFFF">時間A</td><td noWrap bgcolor="#CCFFFF">時間B</td><td noWrap bgcolor="#CCFFFF">時間C</td><td noWrap bgcolor="#CCFFFF">備註</td><td noWrap bgcolor="#CCFFFF">篩選</td><td noWrap bgcolor="#66FF99"><font size="3">順序</font></td></tr>      		
	<?
	
	while(list($xkey,$xvalue)=each($course_data))
	{		
		$result=mysql_db_query( $mysql_database, "select * from cc922 where course_code = '$xkey' LIMIT 0, 1",$conn1);
		@$row=mysql_fetch_row($result);
		if($row){
			mysql_data_seek($result,0);
			while($row=mysql_fetch_row($result))
			{
				if($row[8]==0)
				{
					$some = "半";
				}
				else
				{
					$some = "全";
				}
				
				$time_total = $row[13];
				if($row[15]) $time_total = $time_total . " " . $row[15];
				if($row[17]) $time_total = $time_total . " " . $row[17];
				
				echo "<tr bgColor=\"lightyellow\">";
        			echo "<td noWrap>$row[3]</td>";
        			echo "<td noWrap>$row[22]</td>";
        			echo "<td noWrap>$row[4]</td>";
        			echo "<td noWrap>$row[5]</td>";
	        		echo "<td noWrap>$row[6]</td>";
        			echo "<td noWrap>$some</td>";
	        		echo "<td noWrap>$row[10]</td>";
		        	echo "<td noWrap>$row[11]</td>";
		        	echo "<td noWrap>$row[13]</td>";
		        	echo "<td noWrap>$row[15]</td>";
		        	echo "<td noWrap>$row[17]</td>";
		        	echo "<td noWrap>$row[19]</td>";
        			echo "<td noWrap>$row[18]</td>";
		        	echo "<td noWrap align=center bgcolor=#CCFFCC>";
		        	echo "<select size=\"1\" name=\"my_sort" . $y_num . "[$xkey]\">$temp_string</select></td></tr>\n";
			}
		}
		@mysql_free_result($result);
	}
	?>
	</table></center>
	<p align="center">只要您調整右側<b><font color="#008000" size="3"> 順序 </font></b>選單，就可以依志願順序輸出清單。</p>
	<hr noshade color="#111111" width="90%" size="1">
<?	
}


//傳入數值、課程代號，直接輸出排序後的課程清單
function final_output($course_data,$x_num)
{
	$mysql_server_name = "localhost";
	$mysql_username    = "root";
	$mysql_password    = "";
	$mysql_database    = "cycu";
	
	$conn1=mysql_connect( $mysql_server_name, $mysql_username, $mysql_password);	
	natsort($course_data);
	reset($course_data);
	?>    	
    	<center><P>
    	<font size=3 color=red><b><?
    	
    	switch($x_num)
    	{
    		case "1":
    			echo "宗哲、人哲";
    			break;
    		case "2":
    			echo "天、人、物、我";
    			break;
       		case "3":
    			echo "歷史";
    			break;
    		case "4":
    			echo "法政";
    			break;
    		case "5":
    			echo "軍訓";
    			break;
    		case "6":
    			echo "體育";
    			break;
    		case "7":
    			echo "英聽";
    			break;
    		case "8":
    			echo "一般";
    			break;    			
    	}
    	?></b></font> (共計 <? echo get_credit($course_data) ?> 學分)<br><br>
	<table bgColor="dodgerblue" style="border-collapse: collapse" bordercolor="#111111" cellpadding="3" cellspacing="0" border="1">
      	<tr bgColor="skyblue"><td noWrap bgcolor="#CCFFFF">開課系所</td><td bgcolor="#CCFFFF">類別</td><td noWrap bgcolor="#CCFFFF">語音代碼</td><td noWrap bgcolor="#CCFFFF">課程代碼</td><td noWrap bgcolor="#CCFFFF">課程名稱</td><td bgcolor="#CCFFFF">性質</td><td bgcolor="#CCFFFF">學分</td><td noWrap bgcolor="#CCFFFF">教師</td><td noWrap bgcolor="#CCFFFF">時間A</td><td noWrap bgcolor="#CCFFFF">時間B</td><td noWrap bgcolor="#CCFFFF">時間C</td><td noWrap bgcolor="#CCFFFF">備註</td><td noWrap bgcolor="#CCFFFF">篩選</td></tr>      		
	<?
	
	while(list($xkey,$xvalue)=each($course_data))
	{	
		if($xvalue=="0") continue;
		$result=mysql_db_query( $mysql_database, "select * from cc922 where course_code = '$xkey' LIMIT 0, 1",$conn1);
		@$row=mysql_fetch_row($result);
		if($row)
		{
			mysql_data_seek($result,0);
			while($row=mysql_fetch_row($result))
			{
				if($row[8]==0)
				{
					$some = "半";
				}
				else
				{
					$some = "全";
				}
				
				$time_total = $row[13];
				if($row[15]) $time_total = $time_total . " " . $row[15];
				if($row[17]) $time_total = $time_total . " " . $row[17];
				
				echo "<tr bgColor=\"lightyellow\">";
        			echo "<td noWrap>$row[3]</td>";
        			echo "<td noWrap>$row[22]</td>";
        			echo "<td noWrap>$row[4]</td>";
        			echo "<td noWrap>$row[5]</td>";
	        		echo "<td noWrap>$row[6]</td>";
        			echo "<td noWrap>$some</td>";
	        		echo "<td noWrap>$row[10]</td>";
		        	echo "<td noWrap>$row[11]</td>";
		        	echo "<td noWrap>$row[13]</td>";
		        	echo "<td noWrap>$row[15]</td>";
		        	echo "<td noWrap>$row[17]</td>";
		        	echo "<td noWrap>$row[19]</td>";
        			echo "<td noWrap>$row[18]</td>";		        	
		        	echo "</tr>\n";
		        	$voice_array[$row[4]] = $xvalue;
			}
		}
		@mysql_free_result($result);
	}
	?>
	</table></center>
<?	
	return $voice_array;
}

//傳入需要的欄位陣列、排序後的清單資料，直接輸出使用者要的課程清單
function print_for_you($want_field,$course_data,$x_num)
{
	$mysql_server_name = "localhost";
	$mysql_username    = "root";
	$mysql_password    = "";
	$mysql_database    = "cycu";	
	
	$conn1=mysql_connect( $mysql_server_name, $mysql_username, $mysql_password);	
	natsort($course_data);
	reset($course_data);

    	switch($x_num)
    	{
    		case "1":
    			$x_num = "宗哲、人哲";
    			break;
    		case "2":
    			$x_num = "天、人、物、我";
    			break;
       		case "3":
    			$x_num = "歷史";
    			break;
    		case "4":
    			$x_num = "法政";
    			break;
    		case "5":
    			$x_num = "軍訓";
    			break;
    		case "6":
    			$x_num = "體育";
    			break;
    		case "7":
    			$x_num = "英聽";
    			break;
    		case "8":
    			$x_num = "一般類課程";
    			break;    			
    	}

	$num = count($want_field);
	echo "<tr><td nowrap align=\"center\" bgcolor=\"#E6F2FF\" colspan=\"$num\"><font size=\"3\"><b>$x_num</b></font></td>\n";
	if($want_field[who_open]) echo "<tr><td nowrap align=\"center\">開課系所</td>\n";
	if($want_field[course_type]) echo "<td nowrap align=\"center\">課程類別</td>\n";
	if($want_field[voice_code]) echo "<td nowrap align=\"center\">語音代碼</td>\n";
	if($want_field[course_code]) echo "<td nowrap align=\"center\">課程代碼</td>\n";
	if($want_field[course_name]) echo "<td nowrap align=\"center\">課程名稱</td>\n";
	if($want_field[time_a]) echo "<td nowrap align=\"center\">上課時間</td>\n";
	if($want_field[credit_point]) echo "<td nowrap align=\"center\">學分</td>\n";
	if($want_field[teacher]) echo "<td nowrap align=\"center\">教師</td>\n";
	if($want_field[room_a]) echo "<td nowrap align=\"center\">教室</td>\n";
	if($want_field[course_session]) echo "<td nowrap align=\"center\">性質</td>\n";
	if($want_field[memo]) echo "<td nowrap align=\"center\">備註</td>\n";
	if($want_field[people_limit]) echo "<td nowrap align=\"center\">開課人數</td>\n";
	if($want_field[filter]) echo "<td nowrap align=\"center\">篩選</td>\n";
	if($want_field[respond_unit]) echo "<td nowrap align=\"center\">權責單位</td></tr>\n";
	
	while(list($xkey,)=each($course_data))
	{		
		$result=mysql_db_query( $mysql_database, "select * from cc922 where course_code = '$xkey' LIMIT 0, 1",$conn1);
		@$row=mysql_fetch_row($result);
		if($row){
			mysql_data_seek($result,0);
			while($row=mysql_fetch_row($result))
			{
				if($row[8]==0)
				{
					$some = "半學期";
				}
				else
				{
					$some = "全學期";
				}
				
				$time_total = $row[13];
				if($row[15]) $time_total = $time_total . "、" . $row[15];
				if($row[17]) $time_total = $time_total . "、" . $row[17];
				
				$room_total = $row[12];
				if($row[14]) $room_total = $room_total . "、" . $row[14];
				if($row[16]) $room_total = $room_total . "、" . $row[16];

				if($want_field[who_open]) echo "<tr><td nowrap align=\"center\">$row[3]</td>\n";
				if($want_field[course_type]) echo "<td nowrap align=\"center\">$row[22]</td>\n";
				if($want_field[voice_code]) echo "<td nowrap align=\"center\">$row[4]</td>\n";
				if($want_field[course_code]) echo "<td nowrap align=\"center\">$row[5]</td>\n";
				if($want_field[course_name]) echo "<td nowrap align=\"center\">$row[6]</td>\n";
				if($want_field[time_a]) echo "<td nowrap align=\"center\">$time_total</td>\n";
				if($want_field[credit_point]) echo "<td nowrap align=\"center\">$row[10]</td>\n";
				if($want_field[teacher]) echo "<td nowrap align=\"center\">$row[11]</td>\n";
				if($want_field[room_a]) echo "<td nowrap align=\"center\">$room_total</td>\n";
				if($want_field[course_session]) echo "<td nowrap align=\"center\">$some</td>\n";
				if($want_field[memo]) echo "<td nowrap align=\"center\">$row[19]</td>\n";
				if($want_field[people_limit]) echo "<td nowrap align=\"center\">$row[20]</td>\n";
				if($want_field[filter]) echo "<td nowrap align=\"center\">$row[18]</td>\n";
				if($want_field[respond_unit]) echo "<td nowrap align=\"center\">$row[21]</td></tr>\n";
			}
		}
		@mysql_free_result($result);
	}

}


//傳入有興趣的選課類別，直接印出可以勾選的項目
function show_your_choose_type($sex_limit,$origin,$interest_type,$x_num,$your_major,$your_level,$my_another_major)
{
	$mysql_server_name = "localhost";
	$mysql_username    = "root";
	$mysql_password    = "";
	$mysql_database    = "cycu";
	
	$order_by = "ORDER BY real_people_num DESC";
	
	$conn1=mysql_connect( $mysql_server_name, $mysql_username, $mysql_password );
	
	while(list(,$value)=each($interest_type))
	{	
		unset($alltime_array);
		if($value=="一般")
		{
			$result=mysql_db_query( $mysql_database, "select course_code, time_a, time_b, time_c, who_open, cross_a from cc922 where course_type = '$value' and course_sex <> '$sex_limit' and cross_b = '1' $order_by",$conn1);
			$sql_type = "A";
		}
		elseif($value=="國文")
		{
			$result=mysql_db_query( $mysql_database, "select course_code, time_a, time_b, time_c, who_open, cross_a from cc922 where course_name = '國文(一)' $order_by",$conn1);
			$sql_type = "A";
		}
		elseif($value=="英文")
		{
			$result=mysql_db_query( $mysql_database, "select course_code, time_a, time_b, time_c, who_open, cross_a from cc922 where course_name = '英文(一)' $order_by",$conn1);
			$sql_type = "A";
		}
		elseif($value=="英聽")
		{
			$result=mysql_db_query( $mysql_database, "select course_code, time_a, time_b, time_c, who_open, cross_a from cc922 where course_name = '英語聽講(一)' $order_by",$conn1);
			$sql_type = "A";
		}
		elseif($value==$your_major)
		{		
			$result=mysql_db_query( $mysql_database, "select course_code, time_a, time_b, time_c, who_open, cross_a from cc922 where course_type = '一般' and who_open like '" . $your_major . "%' and course_sex <> '$sex_limit' and course_need = '0' $order_by",$conn1);			
			$sql_type = "B";
		}
		elseif($value==$my_another_major)
		{	
			$result=mysql_db_query( $mysql_database, "select course_code, time_a, time_b, time_c from cc922 where course_type = '一般' and who_open like '" . $my_another_major . "%' and course_sex <> '$sex_limit' and course_need = '0' $order_by",$conn1);
			$sql_type = "C";
		}
		else
		{
			$result=mysql_db_query( $mysql_database, "select course_code, time_a, time_b, time_c from cc922 where course_type = '$value' and course_sex <> '$sex_limit' $order_by",$conn1);
			$sql_type = "C";
		}
		
		@$row=mysql_fetch_row($result);
		
		mysql_data_seek($result,0);
		while($row=mysql_fetch_row($result))
		{
			if($sql_type == "C")
			{
				$alltime_array[$row[0]] = $row[1];
				if($row[2]) $alltime_array[$row[0]] = $alltime_array[$row[0]] . " " . $row[2];
				if($row[3]) $alltime_array[$row[0]] = $alltime_array[$row[0]] . " " . $row[3];
			}
			else
			{
				$course_level = substr($row[4],4,2);
				//同科系的課程
				if($sql_type=="B")
				{					
					if($your_level=='碩')
					{
						//碩選碩
						if($course_level=='碩')
						{
							$alltime_array[$row[0]] = $row[1];
							if($row[2]) $alltime_array[$row[0]] = $alltime_array[$row[0]] . " " . $row[2];
							if($row[3]) $alltime_array[$row[0]] = $alltime_array[$row[0]] . " " . $row[3];
						}
						//可跨部
						elseif($course_level!='碩'&&$row[5]=='1')
						{
							$alltime_array[$row[0]] = $row[1];
							if($row[2]) $alltime_array[$row[0]] = $alltime_array[$row[0]] . " " . $row[2];
							if($row[3]) $alltime_array[$row[0]] = $alltime_array[$row[0]] . " " . $row[3];
						}						
					}
					elseif($your_level=='博')
					{
						//博選博
						if($course_level=='博')
						{
							$alltime_array[$row[0]] = $row[1];
							if($row[2]) $alltime_array[$row[0]] = $alltime_array[$row[0]] . " " . $row[2];
							if($row[3]) $alltime_array[$row[0]] = $alltime_array[$row[0]] . " " . $row[3];
						}
						//可跨部
						elseif($course_level!='博'&&$row[5]=='1')
						{
							$alltime_array[$row[0]] = $row[1];
							if($row[2]) $alltime_array[$row[0]] = $alltime_array[$row[0]] . " " . $row[2];
							if($row[3]) $alltime_array[$row[0]] = $alltime_array[$row[0]] . " " . $row[3];
						}						
					}
					//大學部
					elseif($your_level=='一'||$your_level=='二'||$your_level=='三'||$your_level=='四'||$your_level=='五')
					{
						if($course_level=='一'||$course_level=='二'||$course_level=='三'||$course_level=='四'||$course_level=='五')
						{
							$alltime_array[$row[0]] = $row[1];
							if($row[2]) $alltime_array[$row[0]] = $alltime_array[$row[0]] . " " . $row[2];
							if($row[3]) $alltime_array[$row[0]] = $alltime_array[$row[0]] . " " . $row[3];
						}
						elseif($course_level=='博'&&$row[5]=='1')
						{
							$alltime_array[$row[0]] = $row[1];
							if($row[2]) $alltime_array[$row[0]] = $alltime_array[$row[0]] . " " . $row[2];
							if($row[3]) $alltime_array[$row[0]] = $alltime_array[$row[0]] . " " . $row[3];
						}	
						elseif($course_level=='碩'&&$row[5]=='1')
						{
							$alltime_array[$row[0]] = $row[1];
							if($row[2]) $alltime_array[$row[0]] = $alltime_array[$row[0]] . " " . $row[2];
							if($row[3]) $alltime_array[$row[0]] = $alltime_array[$row[0]] . " " . $row[3];
						}
					}					
				}
				//其他系的課程 判斷跨部
				elseif($sql_type=="A")
				{
					if($your_major!=substr($row[4],0,4))
					{
						if($your_level=='碩')
						{
							//碩選碩
							if($course_level=='碩')
							{
								$alltime_array[$row[0]] = $row[1];
								if($row[2]) $alltime_array[$row[0]] = $alltime_array[$row[0]] . " " . $row[2];
								if($row[3]) $alltime_array[$row[0]] = $alltime_array[$row[0]] . " " . $row[3];
							}
							//可跨部
							elseif($course_level!='碩'&&$row[5]=='1')
							{
								$alltime_array[$row[0]] = $row[1];
								if($row[2]) $alltime_array[$row[0]] = $alltime_array[$row[0]] . " " . $row[2];
								if($row[3]) $alltime_array[$row[0]] = $alltime_array[$row[0]] . " " . $row[3];
							}						
						}
						elseif($your_level=='博')
						{
							//博選博
							if($course_level=='博')
							{
								$alltime_array[$row[0]] = $row[1];
								if($row[2]) $alltime_array[$row[0]] = $alltime_array[$row[0]] . " " . $row[2];
								if($row[3]) $alltime_array[$row[0]] = $alltime_array[$row[0]] . " " . $row[3];
							}
							//可跨部
							elseif($course_level!='博'&&$row[5]=='1')
							{
								$alltime_array[$row[0]] = $row[1];
								if($row[2]) $alltime_array[$row[0]] = $alltime_array[$row[0]] . " " . $row[2];
								if($row[3]) $alltime_array[$row[0]] = $alltime_array[$row[0]] . " " . $row[3];
							}						
						}
						//大學部
						elseif($your_level=='一'||$your_level=='二'||$your_level=='三'||$your_level=='四'||$your_level=='五'||$your_level=='業'||$your_level=='工'||$your_level=='學')
						{
							if($course_level=='一'||$course_level=='二'||$course_level=='三'||$course_level=='四'||$course_level=='五'||$course_level=='業'||$course_level=='工'||$course_level=='學')
							{
								$alltime_array[$row[0]] = $row[1];
								if($row[2]) $alltime_array[$row[0]] = $alltime_array[$row[0]] . " " . $row[2];
								if($row[3]) $alltime_array[$row[0]] = $alltime_array[$row[0]] . " " . $row[3];
							}
							elseif($course_level!='博'&&$row[5]=='1')
							{
								$alltime_array[$row[0]] = $row[1];
								if($row[2]) $alltime_array[$row[0]] = $alltime_array[$row[0]] . " " . $row[2];
								if($row[3]) $alltime_array[$row[0]] = $alltime_array[$row[0]] . " " . $row[3];
							}	
							elseif($course_level!='博'&&$row[5]=='1')
							{
								$alltime_array[$row[0]] = $row[1];
								if($row[2]) $alltime_array[$row[0]] = $alltime_array[$row[0]] . " " . $row[2];
								if($row[3]) $alltime_array[$row[0]] = $alltime_array[$row[0]] . " " . $row[3];
							}
						}
					}
				}
			}
		}
		@mysql_free_result($result);
	
		$can_go_class = compare_us($origin,$alltime_array);
	
		if($can_go_class)
		{
			get_detail_course($value,$can_go_class,$x_num);
		}
		else
		{
			echo "<p align=\"center\">(本課程目前沒有符合您時段的課)</p>\n";
			echo "<hr noshade color=\"#111111\" width=\"90%\" size=\"1\">\n";
		}
	
	}
	mysql_close($conn1);
}

//將被選的清單加入 sql 作為統計
function update_to_sql($course_data)
{
	natsort($course_data);
	reset($course_data);

	$host = "localhost";
	$user = "root";
	$pass = "";
	$database = "cycu";
	$conn=mysql_connect( $host, $user, $pass );

	while(list($xkey,)=each($course_data))
	{	
		$query = "UPDATE cc922 SET total = total + 1 WHERE course_code = '$xkey'";
		@mysql_select_db($database);
		$result = mysql_query($query);
		@mysql_free_result($result);
	}
	mysql_close($conn);
}


//熱門選課看板!!
function hot_rank_show()
{
	$all_type[0] = "歷史";
	$all_type[1] = "法政";
	$all_type[2] = "宗哲";
	$all_type[3] = "人哲";
	$all_type[4] = "天";
	$all_type[5] = "人";
	$all_type[6] = "物";
	$all_type[7] = "我";
	$all_type[8] = "體育";
	$all_type[9] = "軍訓";
	$all_type[10] = "英聽";

	$host = "localhost";
	$user = "root";
	$pass = "";
	$database = "cycu";
	$conna=mysql_connect( $host, $user, $pass );
	
	while(list(,$value)=each($all_type))
	{	
		$result=mysql_db_query($database, "select course_code,course_name,teacher,total,voice_code,time_a,time_b,time_c from cc922 where course_type = '$value' and total <> '0' order by total DESC LIMIT 0, 10",$conna);
		$row=mysql_fetch_row($result);
		if($row)
		{
			?>
			<center><b><font color="#9933FF" size="3"><? echo $value ?>類 熱門課程(本站統計)<br></font></b><br>
			<TABLE style="border-collapse: collapse" bordercolor="#111111" cellpadding="4" cellspacing="0" border="1">
			<TR><TD bgcolor="#CCFFFF" nowrap>語音代碼</TD><TD bgcolor="#CCFFFF" nowrap>課程代碼</TD><TD bgcolor="#CCFFFF" nowrap>課程名稱</TD><TD bgcolor="#CCFFFF" nowrap>老師</TD><TD bgcolor="#CCFFFF" nowrap>時間</TD><TD bgcolor="#CCFFFF" nowrap>長條圖</TD><TD bgcolor="#CCFFFF" nowrap>人氣指數</TD></TR>
			<?
		
			mysql_data_seek($result,0);
			$i=1;
			while($row=mysql_fetch_row($result))
			{
				//$width = $row[3];
				//if($width>200) $width = ($width * 0.7);
				$width = ($row[3] * 0.5);
				$time_all = $row[5];
				if($row[6]) $time_all = $time_all . "、" .$row[6];
				if($row[7]) $time_all = $time_all . "、" .$row[7];
				echo "<TR><TD bgcolor=\"#FFFFE0\" align=\"center\" nowrap>$row[4]</TD><TD bgcolor=\"#FFFFE0\" align=\"center\" nowrap>$row[0]</TD><TD bgcolor=\"#FFFFE0\" nowrap>$row[1]</TD><TD bgcolor=\"#FFFFE0\" align=\"center\" nowrap>$row[2]</TD><TD bgcolor=\"#FFFFE0\" align=\"center\" nowrap>$time_all</TD><TD bgcolor=\"#FFFFE0\" nowrap><IMG height=10 src=\"bar" . $i . ".gif\" width=\"$width\"></TD><TD align=\"center\" bgcolor=\"#FFFFE0\" nowrap>$row[3]</TD></TR>\n";			
				$i++;
				if($i==6) $i=1;
			}
			echo "</TABLE></center><P>\n";
			@mysql_free_result($result);
		}
	}
	mysql_close($conna);
}
//熱門選課看板!!
function hot_rank_show2()
{
	$all_type[0] = "歷史";
	$all_type[1] = "法政";
	$all_type[2] = "宗哲";
	$all_type[3] = "人哲";
	$all_type[4] = "天";
	$all_type[5] = "人";
	$all_type[6] = "物";
	$all_type[7] = "我";
	$all_type[8] = "體育";
	$all_type[9] = "軍訓";
	$all_type[10] = "英聽";

	$host = "localhost";
	$user = "root";
	$pass = "";
	$database = "cycu";
	$conna=mysql_connect( $host, $user, $pass );
	
	while(list(,$value)=each($all_type))
	{	
		$result=mysql_db_query($database, "select course_code,course_name,teacher,real_people_num,voice_code,time_a,time_b,time_c from cc922_register where course_type = '$value' and real_people_num <> '0' order by real_people_num DESC LIMIT 0, 10",$conna);
		$row=mysql_fetch_row($result);
		if($row)
		{
			?>
			<center><b><font color="#9933FF" size="3"><? echo $value ?>類 熱門課程(校方系統篩選統計)<br></font></b><br>
			<TABLE style="border-collapse: collapse" bordercolor="#111111" cellpadding="4" cellspacing="0" border="1">
			<TR><TD bgcolor="#CCFFFF" nowrap>語音代碼</TD><TD bgcolor="#CCFFFF" nowrap>課程代碼</TD><TD bgcolor="#CCFFFF" nowrap>課程名稱</TD><TD bgcolor="#CCFFFF" nowrap>老師</TD><TD bgcolor="#CCFFFF" nowrap>時間</TD><TD bgcolor="#CCFFFF" nowrap>長條圖</TD><TD bgcolor="#CCFFFF" nowrap>登記人數</TD></TR>
			<?
		
			mysql_data_seek($result,0);
			$i=1;
			while($row=mysql_fetch_row($result))
			{
				//$width = $row[3];				
				$width = ($row[3] * 0.15);
				//if($width>1500) $width = ($width * 0.7);
				$time_all = $row[5];
				if($row[6]) $time_all = $time_all . "、" .$row[6];
				if($row[7]) $time_all = $time_all . "、" .$row[7];
				echo "<TR><TD bgcolor=\"#FFFFE0\" align=\"center\" nowrap>$row[4]</TD><TD bgcolor=\"#FFFFE0\" align=\"center\" nowrap>$row[0]</TD><TD bgcolor=\"#FFFFE0\" nowrap>$row[1]</TD><TD bgcolor=\"#FFFFE0\" align=\"center\" nowrap>$row[2]</TD><TD bgcolor=\"#FFFFE0\" align=\"center\" nowrap>$time_all</TD><TD bgcolor=\"#FFFFE0\" nowrap><IMG height=10 src=\"bar" . $i . ".gif\" width=\"$width\"></TD><TD align=\"center\" bgcolor=\"#FFFFE0\" nowrap>$row[3]</TD></TR>\n";			
				$i++;
				if($i==6) $i=1;
			}
			echo "</TABLE></center><P>\n";
			@mysql_free_result($result);
		}
	}
	mysql_close($conna);
}
//---------------------------------------------
//友善列印課表
//---------------------------------------------
function print_final_timetable()
{
   global $PHP_SELF, $origin;
?>
<html>

<head>
<meta http-equiv="Content-Language" content="zh-tw">
<meta http-equiv="Content-Type" content="text/html; charset=big5">
<title>必修課表 友善列印</title>
<style>
<!--
body         { font-family: 新細明體; font-size: 9pt }
td           { font-family: 新細明體; font-size: 9pt }
input        { font-family: 新細明體; font-size: 9pt }
select       { font-family: 新細明體; font-size: 9pt }
option       { font-size: 9pt; font-family: 新細明體 }
-->
</style>
</head>

<body background="kabe_top.jpg">
<p align="center"><IMG SRC="title.gif"></p> 
   <center><hr noshade color="#111111" width="90%" size="1"><table border="0" cellpadding="8" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111">
    <tr>
      <td width="100%" align="center"><font size="3" color="#0000FF"><b>必修課程表</b></font><P><?
   fill_timetable(get_course_time($origin));
?></td>
    </tr>
  </table>
<hr noshade color="#111111" width="90%" size="1"><br>
2002 (c) Campus Network Association</p>
</body>
</html>
<?
}

//---------------------------------------------
//友善列印篩選清單
//---------------------------------------------
function print_final_list()
{
   global $PHP_SELF, $want_field,$my_sort1,$my_sort2,$my_sort3,$my_sort4,$my_sort5,$my_sort6,$my_sort7,$my_sort8;
?>
<html>

<head>
<meta http-equiv="Content-Language" content="zh-tw">
<meta http-equiv="Content-Type" content="text/html; charset=big5">
<title>篩選清單 友善列印</title>
<style>
<!--
body         { font-family: 新細明體; font-size: 9pt }
td           { font-family: 新細明體; font-size: 9pt }
input        { font-family: 新細明體; font-size: 9pt }
select       { font-family: 新細明體; font-size: 9pt }
option       { font-size: 9pt; font-family: 新細明體 }
-->
</style>
</head>

<body background="kabe_top.jpg">
<p align="center"><IMG SRC="title.gif"></p> 
   <center><hr noshade color="#111111" width="90%" size="1"><table border="0" cellpadding="8" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111">
    <tr>
      <td width="100%" align="center"><font size="3" color="#0000FF"><b>篩選清單</b></font><P>
      <table border="0" cellpadding="8" cellspacing="0"><tr><td width="100%"><center><table border="1" cellpadding="4" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111">
      <?
   if($my_sort1) {print_for_you($want_field,$my_sort1,"1");$total_num = $total_num + get_credit($my_sort1);}
   if($my_sort2) {print_for_you($want_field,$my_sort2,"2");$total_num = $total_num + get_credit($my_sort2);}
   if($my_sort3) {print_for_you($want_field,$my_sort3,"3");$total_num = $total_num + get_credit($my_sort3);}
   if($my_sort4) {print_for_you($want_field,$my_sort4,"4");$total_num = $total_num + get_credit($my_sort4);}
   if($my_sort5) {print_for_you($want_field,$my_sort5,"5");$total_num = $total_num + get_credit($my_sort5);}
   if($my_sort6) {print_for_you($want_field,$my_sort6,"6");$total_num = $total_num + get_credit($my_sort6);}
   if($my_sort7) {print_for_you($want_field,$my_sort7,"7");$total_num = $total_num + get_credit($my_sort7);}
   if($my_sort8) {print_for_you($want_field,$my_sort8,"8");$total_num = $total_num + get_credit($my_sort8);}
?></tr></table><P>(共計 <? echo $total_num ?> 學分)</center></td></tr></table></td></tr></table>
<hr noshade color="#111111" width="90%" size="1"><br>
2002 (c) Campus Network Association</p>
</body>
</html>
<?
}

//友善列印篩選規劃表
function print_voice_table()
{
	global $voice1,$voice2,$voice3,$voice4,$voice5,$voice6,$voice7,$voice8;

	$for_replace = array(
	"A" => "      <td width=\"20%\" align=\"center\" id=\"1\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"2\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"3\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"4\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"5\">　</td>",
	"AA" => "     <td width=\"20%\" align=\"center\" id=\"6\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"7\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"8\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"9\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"10\">　</td>",
	"B" => "      <td width=\"20%\" align=\"center\" id=\"1\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"2\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"3\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"4\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"5\">　</td>",
	"BB" => "     <td width=\"20%\" align=\"center\" id=\"6\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"7\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"8\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"9\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"10\">　</td>",
	"C" => "      <td width=\"20%\" align=\"center\" id=\"1\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"2\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"3\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"4\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"5\">　</td>",
	"CC" => "     <td width=\"20%\" align=\"center\" id=\"6\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"7\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"8\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"9\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"10\">　</td>",
	"D" => "      <td width=\"20%\" align=\"center\" id=\"1\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"2\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"3\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"4\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"5\">　</td>",
	"DD" => "     <td width=\"20%\" align=\"center\" id=\"6\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"7\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"8\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"9\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"10\">　</td>",
	"E" => "      <td width=\"20%\" align=\"center\" id=\"1\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"2\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"3\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"4\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"5\">　</td>",
	"EE" => "     <td width=\"20%\" align=\"center\" id=\"6\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"7\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"8\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"9\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"10\">　</td>",
	"F" => "      <td width=\"20%\" align=\"center\" id=\"1\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"2\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"3\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"4\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"5\">　</td>",
	"FF" => "     <td width=\"20%\" align=\"center\" id=\"6\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"7\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"8\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"9\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"10\">　</td>",
	"G" => "      <td width=\"20%\" align=\"center\" id=\"1\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"2\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"3\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"4\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"5\">　</td>",
	"GG" => "     <td width=\"20%\" align=\"center\" id=\"6\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"7\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"8\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"9\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"10\">　</td>",
	"81" => "          <td width=\"23%\" align=\"center\" id=\"81\">　</td>",
	"86" => "          <td width=\"23%\" align=\"center\" id=\"86\">　</td>",
	"82" => "          <td width=\"23%\" align=\"center\" id=\"82\">　</td>",
	"87" => "          <td width=\"23%\" align=\"center\" id=\"87\">　</td>",
	"83" => "          <td width=\"23%\" align=\"center\" id=\"83\">　</td>",
	"88" => "          <td width=\"23%\" align=\"center\" id=\"88\">　</td>",
	"84" => "          <td width=\"23%\" align=\"center\" id=\"84\">　</td>",
	"89" => "          <td width=\"23%\" align=\"center\" id=\"89\">　</td>",
	"85" => "          <td width=\"23%\" align=\"center\" id=\"85\">　</td>",
	"80" => "          <td width=\"23%\" align=\"center\" id=\"80\">　</td>"
	);
	
	
	if($voice1)
	{
		while(list($course_code, $course_order) = each($voice1))
		{			
			$find = "id=\"" . $course_order . "\">　<";
			$repl = "><font color=red><b>" . $course_code ."</b></font><";
			$for_replace["A"] =str_replace($find,$repl,$for_replace["A"]);
			$for_replace["AA"] =str_replace($find,$repl,$for_replace["AA"]);
		}
	}
	
	if($voice2)
	{
		while(list($course_code, $course_order) = each($voice2))
		{			
			$find = "id=\"" . $course_order . "\">　<";
			$repl = "><font color=red><b>" . $course_code ."</b></font><";
			$for_replace["B"] =str_replace($find,$repl,$for_replace["B"]);
			$for_replace["BB"] =str_replace($find,$repl,$for_replace["BB"]);
		}
	}
	if($voice3)
	{
		while(list($course_code, $course_order) = each($voice3))
		{			
			$find = "id=\"" . $course_order . "\">　<";
			$repl = "><font color=red><b>" . $course_code ."</b></font><";
			$for_replace["C"] =str_replace($find,$repl,$for_replace["C"]);
			$for_replace["CC"] =str_replace($find,$repl,$for_replace["CC"]);
		}
	}
	if($voice4)
	{
		while(list($course_code, $course_order) = each($voice4))
		{			
			$find = "id=\"" . $course_order . "\">　<";
			$repl = "><font color=red><b>" . $course_code ."</b></font><";
			$for_replace["D"] =str_replace($find,$repl,$for_replace["D"]);
			$for_replace["DD"] =str_replace($find,$repl,$for_replace["DD"]);
		}
	}
	if($voice5)
	{
		while(list($course_code, $course_order) = each($voice5))
		{			
			$find = "id=\"" . $course_order . "\">　<";
			$repl = "><font color=red><b>" . $course_code ."</b></font><";
			$for_replace["E"] =str_replace($find,$repl,$for_replace["E"]);
			$for_replace["EE"] =str_replace($find,$repl,$for_replace["EE"]);
		}
	}
	if($voice6)
	{
		while(list($course_code, $course_order) = each($voice6))
		{			
			$find = "id=\"" . $course_order . "\">　<";
			$repl = "><font color=red><b>" . $course_code ."</b></font><";
			$for_replace["F"] =str_replace($find,$repl,$for_replace["F"]);
			$for_replace["FF"] =str_replace($find,$repl,$for_replace["FF"]);
		}
	}
	if($voice7)
	{
		while(list($course_code, $course_order) = each($voice7))
		{			
			$find = "id=\"" . $course_order . "\">　<";
			$repl = "><font color=red><b>" . $course_code ."</b></font><";
			$for_replace["G"] =str_replace($find,$repl,$for_replace["G"]);
			$for_replace["GG"] =str_replace($find,$repl,$for_replace["GG"]);
		}
	}

	if($voice8)
	{
		while(list($course_code, $course_order) = each($voice8))
		{			
			$find = "id=\"8" . $course_order . "\">　<";
			$repl = "><font color=red><b>" . $course_code ."</b></font><";
			$key_at = "8" . $course_order;
			$for_replace[$key_at] =str_replace($find,$repl,$for_replace[$key_at]);
		}
	}

	include("s0.php");
	$a = 1;
	while(list($course_key,$course_value)=each($for_replace))
	{
		echo $course_value . "\n";
		if($a==25)
		{
			echo "        </tr>\n      </table>\n      </td>\n    </tr>\n  </table>\n";
			break;
		}
		$target_file = "s". $a . ".php";
		@include($target_file);
		$a++;
	}
}
//直接顯示篩選規劃表
function show_voice_table($voice1,$voice2,$voice3,$voice4,$voice5,$voice6,$voice7,$voice8)
{

	$for_replace = array(
	"A" => "      <td width=\"20%\" align=\"center\" id=\"1\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"2\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"3\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"4\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"5\">　</td>",
	"AA" => "     <td width=\"20%\" align=\"center\" id=\"6\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"7\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"8\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"9\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"10\">　</td>",
	"B" => "      <td width=\"20%\" align=\"center\" id=\"1\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"2\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"3\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"4\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"5\">　</td>",
	"BB" => "     <td width=\"20%\" align=\"center\" id=\"6\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"7\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"8\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"9\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"10\">　</td>",
	"C" => "      <td width=\"20%\" align=\"center\" id=\"1\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"2\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"3\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"4\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"5\">　</td>",
	"CC" => "     <td width=\"20%\" align=\"center\" id=\"6\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"7\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"8\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"9\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"10\">　</td>",
	"D" => "      <td width=\"20%\" align=\"center\" id=\"1\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"2\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"3\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"4\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"5\">　</td>",
	"DD" => "     <td width=\"20%\" align=\"center\" id=\"6\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"7\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"8\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"9\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"10\">　</td>",
	"E" => "      <td width=\"20%\" align=\"center\" id=\"1\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"2\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"3\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"4\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"5\">　</td>",
	"EE" => "     <td width=\"20%\" align=\"center\" id=\"6\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"7\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"8\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"9\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"10\">　</td>",
	"F" => "      <td width=\"20%\" align=\"center\" id=\"1\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"2\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"3\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"4\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"5\">　</td>",
	"FF" => "     <td width=\"20%\" align=\"center\" id=\"6\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"7\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"8\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"9\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"10\">　</td>",
	"G" => "      <td width=\"20%\" align=\"center\" id=\"1\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"2\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"3\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"4\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"5\">　</td>",
	"GG" => "     <td width=\"20%\" align=\"center\" id=\"6\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"7\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"8\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"9\">　</td>\n      <td width=\"20%\" align=\"center\" id=\"10\">　</td>",
	"81" => "          <td width=\"23%\" align=\"center\" id=\"81\">　</td>",
	"86" => "          <td width=\"23%\" align=\"center\" id=\"86\">　</td>",
	"82" => "          <td width=\"23%\" align=\"center\" id=\"82\">　</td>",
	"87" => "          <td width=\"23%\" align=\"center\" id=\"87\">　</td>",
	"83" => "          <td width=\"23%\" align=\"center\" id=\"83\">　</td>",
	"88" => "          <td width=\"23%\" align=\"center\" id=\"88\">　</td>",
	"84" => "          <td width=\"23%\" align=\"center\" id=\"84\">　</td>",
	"89" => "          <td width=\"23%\" align=\"center\" id=\"89\">　</td>",
	"85" => "          <td width=\"23%\" align=\"center\" id=\"85\">　</td>",
	"80" => "          <td width=\"23%\" align=\"center\" id=\"80\">　</td>"
	);
	
	
	if($voice1)
	{
		while(list($course_code, $course_order) = each($voice1))
		{			
			$find = "id=\"" . $course_order . "\">　<";
			$repl = "><font color=red><b>" . $course_code ."</b></font><";
			$for_replace["A"] =str_replace($find,$repl,$for_replace["A"]);
			$for_replace["AA"] =str_replace($find,$repl,$for_replace["AA"]);
		}
	}
	
	if($voice2)
	{
		while(list($course_code, $course_order) = each($voice2))
		{			
			$find = "id=\"" . $course_order . "\">　<";
			$repl = "><font color=red><b>" . $course_code ."</b></font><";
			$for_replace["B"] =str_replace($find,$repl,$for_replace["B"]);
			$for_replace["BB"] =str_replace($find,$repl,$for_replace["BB"]);
		}
	}
	if($voice3)
	{
		while(list($course_code, $course_order) = each($voice3))
		{			
			$find = "id=\"" . $course_order . "\">　<";
			$repl = "><font color=red><b>" . $course_code ."</b></font><";
			$for_replace["C"] =str_replace($find,$repl,$for_replace["C"]);
			$for_replace["CC"] =str_replace($find,$repl,$for_replace["CC"]);
		}
	}
	if($voice4)
	{
		while(list($course_code, $course_order) = each($voice4))
		{			
			$find = "id=\"" . $course_order . "\">　<";
			$repl = "><font color=red><b>" . $course_code ."</b></font><";
			$for_replace["D"] =str_replace($find,$repl,$for_replace["D"]);
			$for_replace["DD"] =str_replace($find,$repl,$for_replace["DD"]);
		}
	}
	if($voice5)
	{
		while(list($course_code, $course_order) = each($voice5))
		{			
			$find = "id=\"" . $course_order . "\">　<";
			$repl = "><font color=red><b>" . $course_code ."</b></font><";
			$for_replace["E"] =str_replace($find,$repl,$for_replace["E"]);
			$for_replace["EE"] =str_replace($find,$repl,$for_replace["EE"]);
		}
	}
	if($voice6)
	{
		while(list($course_code, $course_order) = each($voice6))
		{			
			$find = "id=\"" . $course_order . "\">　<";
			$repl = "><font color=red><b>" . $course_code ."</b></font><";
			$for_replace["F"] =str_replace($find,$repl,$for_replace["F"]);
			$for_replace["FF"] =str_replace($find,$repl,$for_replace["FF"]);
		}
	}
	if($voice7)
	{
		while(list($course_code, $course_order) = each($voice7))
		{			
			$find = "id=\"" . $course_order . "\">　<";
			$repl = "><font color=red><b>" . $course_code ."</b></font><";
			$for_replace["G"] =str_replace($find,$repl,$for_replace["G"]);
			$for_replace["GG"] =str_replace($find,$repl,$for_replace["GG"]);
		}
	}

	if($voice8)
	{
		while(list($course_code, $course_order) = each($voice8))
		{			
			$find = "id=\"8" . $course_order . "\">　<";
			$repl = "><font color=red><b>" . $course_code ."</b></font><";
			$key_at = "8" . $course_order;
			$for_replace[$key_at] =str_replace($find,$repl,$for_replace[$key_at]);
		}
	}

	include("s00.php");
	$a = 1;
	while(list($course_key,$course_value)=each($for_replace))
	{
		echo $course_value . "\n";
		if($a==25)
		{
			echo "        </tr>\n      </table>\n      </td>\n    </tr>\n  </table>\n";
			break;
		}
		$target_file = "s". $a . ".php";
		@include($target_file);
		$a++;
	}
}

//傳入課程代號，輸出目前所選的學分數
function get_credit($origin)
{
	if(!$origin) $output = 0;
	
	$mysql_server_name = "localhost";
	$mysql_username    = "root";
	$mysql_password    = "";
	$mysql_database    = "cycu";
	
	$conn1=mysql_connect( $mysql_server_name, $mysql_username, $mysql_password);	
	
	while(list($xkey,$xvalue)=@each($origin))
	{
		if($xkey=="(另有安排)") continue;
		if($xvalue=="0") continue;
		if($xvalue=='X') continue;
		$result=mysql_db_query($mysql_database, "select credit_point from cc922 where course_code = '$xkey' LIMIT 0, 1",$conn1);
		@$row=mysql_fetch_row($result);		
		$output = $output + $row[0];			
		@mysql_free_result($result);
	}
	
	if(!$output) $output = 0;
	
	return $output;
}

//寫入頁面計數器
function add_counter($aim){
	
	$host = "localhost";
	$user = "root";
	$pass = "";
	$database = "cycu";
	$conn=mysql_connect( $host, $user, $pass );
	$query = "UPDATE counter SET num = num + 1 WHERE name = '$aim'";
	mysql_select_db($database,$conn);
	$result = mysql_query($query);
	mysql_close($conn);	
}

//日期格式化函式(長)
function format_date($dateshow){

ereg("([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})", $dateshow, $regs) ;
    
    $date_show = $regs[1] . "/" . $regs[2] . "/" . $regs[3] . " " . $regs[4] . ":" . $regs[5] . ":" . $regs[6] ;
    
    return $date_show ;    
}

//錯誤訊息函式
function error_message($msg)
{
   echo "<SCRIPT>alert(\"$msg\");history.go(-1)</SCRIPT>";
   exit;
}

//提示一下使用者
function pass_message($msg)
{
   echo "<SCRIPT>alert(\"$msg\");</SCRIPT>";
}
?>