<?php
	require_once ($_SERVER['DOCUMENT_ROOT']."/inc/pgsql.inc");
	
	if(getenv("REQUEST_METHOD") != "POST")
	{
?>	
<html>
<head>
	<title></title>
	<script language='javascript'>
		function chkForm()
		{
			frm = document.info;
			if(frm.group_id.value == '')
			{
				alert('그룹아이디를 입력하세요!');
				return;
			}
			if(frm.group_name.value == '')
			{
				alert('그룹명을 입력하세요!');
				return;
			}
			if(frm.group_password.value == '' || frm.group_password_confirm.value == '')
			{
				alert('비밀번호를 입력하세요!');
				return;
			}
			if(frm.group_name.value != frm.group_password_confirm.value)
			{
				alert('비밀번호를 확인 하십시오!');
				return;
			}
			frm.submit();
		}
	</script>		
</head>
<body>			
<form name='info' method='POST' action='./add_group.html'>
	<table>
		<tr>
			<td>&nbsp;그룹ID</td>
			<td><input name='group_id' maxlength='20'></td>
		</tr>
		<tr>
			<td>&nbsp;그룹명</td>
			<td><input name='group_name' maxlength='20'></td>
		</tr>
		<tr>
			<td>&nbsp;패스워드</td>
			<td><input type="password" name='group_password' maxlength='20'></td>
		</tr>
		<tr>
			<td>&nbsp;패스워드확인</td>
			<td><input type="password" name='group_password_confirm' maxlength='20'></td>
		</tr>
		<tr>
			<td colspan='2'>				
				<div align='right'><a href='javascript:history.back(-1)'>뒤로</a>&nbsp;<a href='javascript:chkForm();'>추가</a></div>
				</td>
		</tr>
	</table>		
</form>			
</body>
</html>
<?php			
	}else if(getenv("REQUEST_METHOD") == "POST")
	{					
		$group_id = trim($_POST['group_id']);
		$group_name = trim($_POST['group_name']);
		$group_password = trim($_POST['group_password']);
		$group_password_confirm = trim($_POST['group_password_confirm']);
		
		if($group_id != "" && $group_name != "" && ($group_password == $group_password_confirm))
		{			
			$objConn = new DB_Sql;
			$enCoding = "set client_encoding = 'uhc'";
			$objConn->query($enCoding);	
			
			$strSql  = "SELECT COUNT(group_seq) AS cnt ";
			$strSql .= "FROM t_group ";
			$strSql .= "WHERE group_id = '".$group_id."' AND deleted_yn = 'N' ";
			
			$objConn->query($strSql);
			if($objConn->nf() != 0)
			{
				$objConn->next_record();
				$cnt = $objConn->f("cnt");
				if($cnt == 0)
				{
					$strSql  = "INSERT INTO t_group(group_id, group_password, group_name, deleted_yn, ";
					$strSql .= "					reg_datetime, update_datetime) ";		
					$strSql .= "VALUES ('".$group_id."', '".$group_password."', '".$group_name."', 'N', now(), now())";	
					
					$objConn->query($strSql);
					echo "<script language='javascript'>\n";
					echo "	alert('등록되었습니다.');\n";
					echo "	location.href = './group_list.html';\n";
					echo "</script>";
					
				}else
				{
					echo "<script language='javascript'>\n";
					echo "	alert('그룹이 존재합니다.');\n";
					echo "	history.back(-1);\n";
					echo "</script>";			
					die;									
				}
			}else
			{
				echo "<script language='javascript'>\n";
				echo "	alert('잘못된 접근입니다.');\n";
				echo "	history.back(-1);\n";
				echo "</script>";			
				die;				
			}
		}else
		{
			echo "<script language='javascript'>\n";
			echo "	alert('잘못된 접근입니다.');\n";
			echo "	history.back(-1);\n";
			echo "</script>";			
			die;
		}		
	}
	die;
			
	/*		
			
			
			
			
			
			$group_seq 	 		= trim($_POST['group']);
			$member_name 		= trim($_POST['member_name']);
			$member_cellphone 	= trim(str_replace("-", "", $_POST['member_cellphone']));
			
			$strSql  = "SELECT COUNT(group_seq) AS cnt ";
			$strSql .= "FROM t_group_member ";
			$strSql .= "WHERE group_seq = ".$group_seq." AND member_cellphone = '".$member_cellphone."'";
			
			$objConn->query($strSql);
			if($objConn->nf() != 0)
			{
				$objConn->next_record();
				$cnt = $objConn->f("cnt");	
				
				if($cnt == 0)
				{
					$strSql  = "INSERT INTO t_group_member(group_seq, member_name, member_cellphone, reg_datetime, update_datetime) ";		
					$strSql .= "VALUES (".$group_seq.", '".$member_name."', '".$member_cellphone."', now(), now())";	
					
					$objConn->query($strSql);
					echo "<script language='javascript'>\n";
					echo "	alert('등록되었습니다.');\n";
					echo "	location.href = './group_list.html';\n";
					echo "</script>";
				}else
				{
					echo "<script language='javascript'>\n";
					echo "	alert('이미 등록되었습니다.');\n";
					echo "	history.back(-1);\n";
					echo "</script>";
				}				
			}					
		}	
	}else
	{
		echo "<script language='javascript'>\n";
		echo "	alert('잘못된 접근입니다..');\n";
		echo "	history.back(-1);\n";
		echo "</script>";			
		die;		
	}
	*/
?>
