<?php
	$cnt = 0;
	foreach($_SESSION['sysuser'] as $key => $item){
		if($item['app_name'] == "webmaster"){
			$ptn_id = $_SESSION['sysuser'][$cnt]['position_id'];
			$user_role = $_SESSION['sysuser'][$cnt]['role_id'];
		}
		$cnt++;
	}
	/*==============================*/
//require_once("../template/inside_template.php");
//require_once ('../library/configs.php');
//require_once ('../library/function.php');
//connectdb();
//selectdb();

/*
 * function select list when modifify 
 */

function selListEdit($filId, $filName, $table, $name)
{
  //declare variable 
  global $link, $row_ict;
  selectStaff();
  $sql = "SELECT $filId, $filName FROM $table";
  $result = mysql_query($sql, $link);
  echo "<select name='$name' class='selectOption'>\n";
  while ($exp = mysql_fetch_array($result, MYSQL_ASSOC)) {
	if ($exp[$filId] == $funid) {
	  echo "<option value=\"$exp[$filId]\" selected=\"selected\" >".htmlspecialchars($exp[$filName])."</option>\n";
	} else {
	  echo "<option value=\"$exp[$filId]\">".htmlspecialchars($exp[$filName])."</option>\n";
	}
  }
  echo "</select >\n";	
}

/*
 * function select list of gender
 */	
function selListsex($name) 
{
  //declare variable 
  global $link;
  //select list status
  echo "<select name='$name' class='selectOption' >\n";
  echo "<option value = ''>--&#3776;&#3749;&#3767;&#3757;&#3713;&#3776;&#3742;&#3732;--</option>";
  $gender = array("&#3722;&#3762;&#3725;" => "&#3722;&#3762;&#3725;", "&#3725;&#3764;&#3719;" => "&#3725;&#3764;&#3719;");
  foreach ($gender as $name => $value)                
  if (isset($txtGender) && $txtGender == $name) {
	echo "<option value = '$name' class = 'inputtext' selected>$value</option>";
  } 
  else {
	echo "<option value = '$name' class = 'inputtext' >$value</option>";
  }	
	echo "</select >\n";
}

function editListsex($name) 
{
  //declare variable 
  global $link, $row_ict;
  //select list status
  echo "<select name='$name' class='selectOption' id =\"sex\" onchange=\"checkEmptyOnly('sex', 'errSex', 'chkSex')\" >\n";
  echo "<option value = '' >--&#3776;&#3749;&#3767;&#3757;&#3713;&#3776;&#3742;&#3732;--</option>";
  $gender = array("&#3722;&#3762;&#3725;" => "&#3722;&#3762;&#3725;", "&#3725;&#3764;&#3719;" => "&#3725;&#3764;&#3719;");
  foreach ($gender as $name => $value)                
  if ($row_ict['sex'] == $name) {
	echo "<option value = '$name' class = 'inputtext' selected/>$value</option>";
  } else {
	  echo "<option value = '$name' class = 'inputtext' />$value</option>";
  }
  echo "</select >\n";
}

/*
 * function select list for modify status
 */	
function editListstatus($name) 
{
  //declare variable 
  global $link, $row_ict;
  //select list status
  echo "<select name='$name' class='selectOption' id = \"status\" onchange=\"checkEmptyOnly('status', 'errSta', 'chkSta')\" >\n";
  echo "<option value = ''>--&#3776;&#3749;&#3767;&#3757;&#3713;&#3754;&#3760;&#3734;&#3762;&#3737;&#3760;--</option>";
  $status = array('&#3778;&#3754;&#3732;' => '&#3778;&#3754;&#3732;', '&#3777;&#3733;&#3784;&#3719;&#3719;&#3762;&#3737;' => '&#3777;&#3733;&#3784;&#3719;&#3719;&#3762;&#3737;', '&#3746;&#3784;&#3762;&#3758;&#3785;&#3762;&#3719;' => '&#3746;&#3784;&#3762;&#3758;&#3785;&#3762;&#3719;', '&#3776;&#3739;&#3761;&#3737;&#3805;&#3785;&#3762;&#3725;' => '&#3776;&#3739;&#3761;&#3737;&#3805;&#3785;&#3762;&#3725;');
  foreach ($status as $name => $value)                
  if ($row_ict['status'] == $name) {
	echo "<option value = '$name' class = 'inputtext' selected/>$value</option>";
  } else {
	  echo "<option value = '$name' class = 'inputtext' />$value</option>";
  }
  echo "</select >\n";
}

/*
 * function select list of status
 */	
function selListstatus($name) 
{
  //declare variable 
  global $link;
  //select list status
  echo "<select name='$name' class='selectOption'>\n";
  echo "<option value = ''>--&#3776;&#3749;&#3767;&#3757;&#3713;&#3754;&#3760;&#3734;&#3762;&#3737;&#3760;--</option>";
  $status = array('&#3778;&#3754;&#3732;' => '&#3778;&#3754;&#3732;', '&#3777;&#3733;&#3784;&#3719;&#3719;&#3762;&#3737;' => '&#3777;&#3733;&#3784;&#3719;&#3719;&#3762;&#3737;', '&#3746;&#3784;&#3762;&#3758;&#3785;&#3762;&#3719;' => '&#3746;&#3784;&#3762;&#3758;&#3785;&#3762;&#3719;', '&#3776;&#3739;&#3761;&#3737;&#3805;&#3785;&#3762;&#3725;' => '&#3776;&#3739;&#3761;&#3737;&#3805;&#3785;&#3762;&#3725;');
  foreach ($status as $name => $value)                
  if (isset($txtStatus) && $txtStatus == $name) {
	echo "<option value = '$name' class = 'inputtext' selected>$value</option>";
  } 
  else {
	echo "<option value = '$name' class = 'inputtext' >$value</option>";
  }
	echo "</select >\n";
}

/*
 * use for get auto id from select max id 
 */
function maxId() 
{
  global $ict, $row_ict;
  /*
  ict("SELECT max(emp_id) FROM emp_personal");
  $xid		= $row_ict['max(emp_id)'];
  $xid 		+= 1;
  $autoid 	= $xid;
  return $autoid;
  */
}

/*
 *function add new Staff 
 */

function addStaff()
{
	//Variable connect database
	global $link, $row_ict;
	//Picture information
  if (isset($_POST['FormStaff']) && $_POST['FormStaff'] == "FormAddStaff") {
	$File_name = $_POST['stPic'];
    if (isset($_FILES['stPic']) && $_FILES['stPic']['name'] != "") {
      if (!defined('UPLOADDIR')) define('UPLOADDIR', (dirname(__FILE__) ."/../images//staff") );
	    //checks if file uploaded
	    if (is_uploaded_file($_FILES['stPic']['tmp_name'])) {
		  $File_tmpname = $_FILES["stPic"]["tmp_name"];
		  $pinfo = pathinfo($_FILES['stPic']['name']);
		  $File_extension = $pinfo['extension'];
	  	  $File_name = $_POST['nameSt_e']."_".$_POST['txtidCode'].".".$File_extension;
		  $File_type = $_FILES["stPic"]["type"];
		  $File_size = $_FILES["stPic"]["size"];
		  move_uploaded_file($File_tmpname, (UPLOADDIR . "//" . $File_name));
	    }
	}

	//get value from another textbox
	$id = conditionEscape($_POST['txtid']);
	$idCode = conditionEscape($_POST['txtidCode']);
	$fname = conditionEscape($_POST['nameSt']);
	$lname = conditionEscape($_POST['SurnameSt']);
	$fnameE = conditionEscape($_POST['nameSt_e']);
	$lnameE = conditionEscape($_POST['SurnameSt_e']);
	$status = conditionEscape($_POST['status']);
	$sex = conditionEscape($_POST['SEX']);
	$edu = conditionEscape($_POST['eduLevel']);
	$eduP = conditionEscape($_POST['eduPlace']);
	$desc = conditionEscape($_POST['descrip']);
	$posit = conditionEscape($_POST['position']);
	$child = conditionEscape($_POST['child']);
	$resp = conditionEscape($_POST['respon']);
	$hire = conditionEscape($_POST['hire_date']);
	$birth = conditionEscape($_POST['birth_date']);
	$calCode = conditionEscape($_POST['calCode']);
	$BAdd = conditionEscape($_POST['B_address']);
	$CAdd = conditionEscape($_POST['C_address']);
	$tribe = conditionEscape($_POST['txttribe']);
	$relig = conditionEscape($_POST['txtrelig']);
	$tel = conditionEscape($_POST['telSt']);	
	$email = conditionEscape($_POST['emailSt']);
	$Pdate = conditionEscape($_POST['party_date']);
	$Ydate = conditionEscape($_POST['young_date']);
	$Ldate = conditionEscape($_POST['labor_date']);
	$DPdate = conditionEscape($_POST['de_party_date']);
	$Wdate = conditionEscape($_POST['w_date']);
	$Rdate = conditionEscape($_POST['rec_date']);
	$OldSpec = conditionEscape($_POST['oldSpec']);
	$hobby = conditionEscape($_POST['hobbSt']);
	$frt = conditionEscape($_POST['frtSt']);
	$peID = conditionEscape($_POST['ident']);
	$passID = conditionEscape($_POST['passp']);
	$remark = conditionEscape($_POST['remark']);
	$statusP = conditionEscape($_POST['status_per']);
	$statusBdate = conditionEscape($_POST['status_birthdate']);
	$statusWP = conditionEscape($_POST['status_w_place']);
	$statusW = conditionEscape($_POST['status_w']);
	
	$sql = sprintf("INSERT INTO emp (id_emp, code_emp, name, last_name, name_e, last_name_e, birth_date, sex, born_add, address, tribe, religion, tel, status, hobby, free_t, pe_id, pass_id, hire_date, remark, edu, edu_place, description, respon, cal_code, email_address, position, party_date, young_date, labor_date, deputy_party_date, women_date, old_specialist, record_date, status_per, status_birthdate, status_w_place, status_w, child_n, staff_pict)
	VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
		GetSQLValueString($id, "int"),
		GetSQLValueString($idCode, "text"),
		GetSQLValueString($fname, "text"),
		GetSQLValueString($lname, "text"),
		GetSQLValueString($fnameE, "text"),
		GetSQLValueString($lnameE, "text"),
		GetSQLValueString($birth, "date"),
		GetSQLValueString($sex, "text"),
		GetSQLValueString($BAdd, "text"),
		GetSQLValueString($CAdd, "text"),
		GetSQLValueString($tribe, "text"),
		GetSQLValueString($relig, "text"),
		GetSQLValueString($tel, "text"),
		GetSQLValueString($status, "text"),
		GetSQLValueString($hobby, "text"), 
		GetSQLValueString($frt, "text"),
		GetSQLValueString($peID, "text"),
		GetSQLValueString($passID, "text"),
		GetSQLValueString($hire, "date"),
		GetSQLValueString($remark, "text"),
		GetSQLValueString($edu, "text"),
		GetSQLValueString($eduP, "text"),
		GetSQLValueString($desc, "text"),
		GetSQLValueString($resp, "text"),
		GetSQLValueString($calCode, "text"),
		GetSQLValueString($email, "text"),
		GetSQLValueString($posit, "text"),
		GetSQLValueString($Pdate, "date"),
		GetSQLValueString($Ydate, "date"),
		GetSQLValueString($Ldate, "date"),
		GetSQLValueString($DPdate, "date"),
		GetSQLValueString($Wdate, "date"),
		GetSQLValueString($OldSpec, "text"),
		GetSQLValueString($Rdate, "date"),
		GetSQLValueString($statusP, "text"),
		GetSQLValueString($statusBdate, "date"),
		
		GetSQLValueString($statusWP, "text"),
		GetSQLValueString($statusW, "text"),
		GetSQLValueString($child, "int"),
		GetSQLValueString($File_name, "text"));

	$Result = mysql_query($sql, $link) ;

	if ($Result) {
	    $_SESSION['s'] = "success"; 
		$_SESSION['st'] = "t";
		header('Location: index.php?view=view');		
	} else {
		$_SESSION['s'] = "fail";
		$_SESSION['st'] = "f";
		header('Location: index.php?view=add&stid='.$id);	
	}
	@mysql_close($link);
  }
}

/*function alert add new staff*/
function errmsgst()
{
	$errq = "";
	//if (isset($_GET['st'])) {
	if (isset($_SESSION['st']) && $_SESSION['st'] == "t") {
		$errq = "<span style = \"font-family: 'Times New Roman, Saysettha OT'; color: #006600; font-size: 16px; font-weight: bold\">Add new Staff successful!</span>";
		unset($_SESSION['st']);
	} else if (isset($_SESSION['st']) && $_SESSION['st'] == "f"){
		$errq = "<span style = \"font-family: 'Times New Roman, Saysettha OT'; color: #FF0000; font-size: 16px; font-weight: bold;\">Add new Staff failed!</span>";
		unset($_SESSION['st']);
	} else {
		unset($_SESSION['st']);
		$errq = "";
	}	
	return $errq;
}

/*function alert Update staff*/
function errmsgstUp()
{
	$errq = "";
	if (isset($_SESSION['st']) && $_SESSION['st'] == "t") {
		$errq = "<span style = \"font-family: 'Times New Roman, Saysettha OT'; color: #006600; font-size: 16px; font-weight: bold\">Update Staff successful!</span>";
		unset($_SESSION['st']);
	} else if (isset($_SESSION['st']) && $_SESSION['st'] == "f"){
		$errq = "<span style = \"font-family: 'Times New Roman, Saysettha OT'; color: #FF0000; font-size: 16px; font-weight: bold;\">Update Staff failed!</span>";
		unset($_SESSION['st']);
	} else {
		unset($_SESSION['st']);
		$errq = "";
	}	
	return $errq;
}

/*
* function check user for edit information
*/
/*function chUser(){
	if($_SESSION['sessuderid'] == $_SESSION['sess_id']){
		echo "Sorry, you cannot edit because this is not your information.";
		exit;
	}
}*/

/*
 * function edit staff
 */
function editStaff() 
{ 
  //Variable connect database
  global $link, $globals; 
	
  $qState 	= "";
	
  //get value from another textbox
	$id = conditionEscape($_POST['txtid']);
	$idCode = conditionEscape($_POST['txtidCode']);
	$fname = conditionEscape($_POST['nameSt']);
	$lname = conditionEscape($_POST['SurnameSt']);
	$fnameE = conditionEscape($_POST['nameSt_e']);
	$lnameE = conditionEscape($_POST['SurnameSt_e']);
	$status = conditionEscape($_POST['status']);
	$sex = conditionEscape($_POST['SEX']);
	$edu = conditionEscape($_POST['eduLevel']);
	$eduP = conditionEscape($_POST['eduPlace']);
	$desc = conditionEscape($_POST['descrip']);
	$posit = conditionEscape($_POST['position']);
	$child = conditionEscape($_POST['child']);
	$resp = conditionEscape($_POST['respon']);
	$hire = conditionEscape($_POST['hire_date']);
	$birth = conditionEscape($_POST['birth_date']);
	$calCode = conditionEscape($_POST['calCode']);
	$BAdd = conditionEscape($_POST['B_address']);
	$CAdd = conditionEscape($_POST['C_address']);
	$tribe = conditionEscape($_POST['txttribe']);
	$relig = conditionEscape($_POST['txtrelig']);
	$tel = conditionEscape($_POST['telSt']);	
	$email = conditionEscape($_POST['emailSt']);
	$Pdate = conditionEscape($_POST['party_date']);
	$Ydate = conditionEscape($_POST['young_date']);
	$Ldate = conditionEscape($_POST['labor_date']);
	$DPdate = conditionEscape($_POST['de_party_date']);
	$Wdate = conditionEscape($_POST['w_date']);
	$Rdate = conditionEscape($_POST['rec_date']);
	$OldSpec = conditionEscape($_POST['oldSpec']);
	$hobby = conditionEscape($_POST['hobbSt']);
	$frt = conditionEscape($_POST['frtSt']);
	$peID = conditionEscape($_POST['ident']);
	$passID = conditionEscape($_POST['passp']);
	$remark = conditionEscape($_POST['remark']);
	$statusP = conditionEscape($_POST['status_per']);
	$statusBdate = conditionEscape($_POST['status_birthdate']);
	$statusWP = conditionEscape($_POST['status_w_place']);
	$statusW = conditionEscape($_POST['status_w']);
	
	//if page is exist it will show on the URL
	if (isset($page)) {
		$num = "&page=".$page;
	}
	//query insert into database
	if (isset($_POST['frmAddSt']) && $_POST['frmAddSt'] == "AddSt") {
	  //Picture information
		$File_name = $_POST['stPic'];
	  if (isset($_FILES['stPicUp']) && $_FILES['stPicUp']['name'] != "") {
		
		if (!defined('UPLOADDIR')) {
		  define('UPLOADDIR', (dirname(__FILE__) ."/../images//staff"));
		}
		//checks if file uploaded
		if (is_uploaded_file($_FILES['stPicUp']['tmp_name'])) {
		  $File_tmpname = $_FILES['stPicUp']['tmp_name'];
		  $pinfo = pathinfo($_FILES['stPicUp']['name']);
		  $File_extension = $pinfo['extension'];
		  $File_name = $_POST['nameSt_e']."_".$_POST['txtidCode'].".".$File_extension;
		  $File_type = $_FILES["stPicUp"]["type"];
		  $File_size = $_FILES["stPicUp"]["size"];
		  move_uploaded_file($File_tmpname, (UPLOADDIR . "//" . $File_name));
		}
		
/*
	  $sql 	= sprintf("UPDATE tbl_expert SET firstname = %s, lastname = %s, dob = %s, sex = %s, status = %s, nation_id = %s, passport_no = %s, issue_date = %s,
	  exp_date = %s, place_issue = %s, edu_id = %s, address = %s, specialist = %s, email = %s, tel = %s, mobile = %s, fax = %s, visa_no = %s, visa_issuedate = %s, visa_expdate = %s,
	  visa_approve = %s, idcard = %s, issue_idcard = %s, exp_idcard = %s, approve_idcard = %s, expert_picture = %s, start_date = %s, end_date = %s, expert_status = %s WHERE expert_id = %s",
*/	  
	  $sql 	= sprintf("UPDATE emp SET code_emp = %s, name = %s, last_name = %s, name_e = %s, last_name_e = %s, birth_date = %s, sex = %s, born_add = %s, address = %s, tribe = %s, religion = %s, tel = %s, status = %s, hobby = %s, free_t = %s, pe_id = %s, pass_id = %s, hire_date = %s, remark = %s, edu = %s, edu_place = %s, description = %s, respon = %s, cal_code = %s, email_address = %s, position = %s, party_date = %s, young_date = %s, labor_date = %s, deputy_party_date = %s, women_date = %s, old_specialist = %s, record_date = %s, status_per = %s, status_birthdate = %s, status_w_place = %s, status_w = %s, child_n = %s, staff_pict = %s WHERE id_emp = %s",
		GetSQLValueString($idCode, "text"),
		GetSQLValueString($fname, "text"),
		GetSQLValueString($lname, "text"),
		GetSQLValueString($fnameE, "text"),
		GetSQLValueString($lnameE, "text"),
		GetSQLValueString($birth, "date"),
		GetSQLValueString($sex, "text"),
		GetSQLValueString($BAdd, "text"),
		GetSQLValueString($CAdd, "text"),
		GetSQLValueString($tribe, "text"),
		GetSQLValueString($relig, "text"),
		GetSQLValueString($tel, "text"),
		GetSQLValueString($status, "text"),
		GetSQLValueString($hobby, "text"), 
		GetSQLValueString($frt, "text"),
		GetSQLValueString($peID, "text"),
		GetSQLValueString($passID, "text"),
		GetSQLValueString($hire, "date"),
		GetSQLValueString($remark, "text"),
		GetSQLValueString($edu, "text"),
		GetSQLValueString($eduP, "text"),
		GetSQLValueString($desc, "text"),
		GetSQLValueString($resp, "text"),
		GetSQLValueString($calCode, "text"),
		GetSQLValueString($email, "text"),
		GetSQLValueString($posit, "text"),
		GetSQLValueString($Pdate, "date"),
		GetSQLValueString($Ydate, "date"),
		GetSQLValueString($Ldate, "date"),
		GetSQLValueString($DPdate, "date"),
		GetSQLValueString($Wdate, "date"),
		GetSQLValueString($OldSpec, "text"),
		GetSQLValueString($Rdate, "date"),
		GetSQLValueString($statusP, "text"),
		GetSQLValueString($statusBdate, "date"),
		
		GetSQLValueString($statusWP, "text"),
		GetSQLValueString($statusW, "text"),
		GetSQLValueString($child, "int"),
		GetSQLValueString($File_name, "text"),
		GetSQLValueString($id, "int"));	
		} else {
			$sql 	= sprintf("UPDATE emp SET code_emp = %s, name = %s, last_name = %s, name_e = %s, last_name_e = %s, birth_date = %s, sex = %s, born_add = %s, address = %s, tribe = %s, religion = %s, tel = %s, status = %s, hobby = %s, free_t = %s, pe_id = %s, pass_id = %s, hire_date = %s, remark = %s, edu = %s, edu_place = %s, description = %s, respon = %s, cal_code = %s, email_address = %s, position = %s, party_date = %s, young_date = %s, labor_date = %s, deputy_party_date = %s, women_date = %s, old_specialist = %s, record_date = %s, status_per = %s, status_birthdate = %s, status_w_place = %s, status_w = %s, child_n = %s WHERE id_emp = %s",
			GetSQLValueString($idCode, "text"),
			GetSQLValueString($fname, "text"),
			GetSQLValueString($lname, "text"),
			GetSQLValueString($fnameE, "text"),
			GetSQLValueString($lnameE, "text"),
			GetSQLValueString($birth, "date"),
			GetSQLValueString($sex, "text"),
			GetSQLValueString($BAdd, "text"),
			GetSQLValueString($CAdd, "text"),
			GetSQLValueString($tribe, "text"),
			GetSQLValueString($relig, "text"),
			GetSQLValueString($tel, "text"),
			GetSQLValueString($status, "text"),
			GetSQLValueString($hobby, "text"), 
			GetSQLValueString($frt, "text"),
			GetSQLValueString($peID, "text"),
			GetSQLValueString($passID, "text"),
			GetSQLValueString($hire, "date"),
			GetSQLValueString($remark, "text"),
			GetSQLValueString($edu, "text"),
			GetSQLValueString($eduP, "text"),
			GetSQLValueString($desc, "text"),
			GetSQLValueString($resp, "text"),
			GetSQLValueString($calCode, "text"),
			GetSQLValueString($email, "text"),
			GetSQLValueString($posit, "text"),
			GetSQLValueString($Pdate, "date"),
			GetSQLValueString($Ydate, "date"),
			GetSQLValueString($Ldate, "date"),
			GetSQLValueString($DPdate, "date"),
			GetSQLValueString($Wdate, "date"),
			GetSQLValueString($OldSpec, "text"),
			GetSQLValueString($Rdate, "date"),
			GetSQLValueString($statusP, "text"),
			GetSQLValueString($statusBdate, "date"),
			
			GetSQLValueString($statusWP, "text"),
			GetSQLValueString($statusW, "text"),
			GetSQLValueString($child, "int"),
			GetSQLValueString($id, "int"));	
		}
	  $Result 	= mysql_query($sql, $link) or die(mysql_error());
			
	  //if query successed it will go to page index and if page number is exist it will link to page number in page edit
	  if ($Result) {
	    $_SESSION['s'] = "success"; 
		$_SESSION['st'] = "t";
		header('Location: index.php?view=view');			
	  } else {
	    $_SESSION['s'] = "success"; 
		$_SESSION['st'] = "f";
		header('Location: index.php?view=edit&stid='.$id.'&code='.$idCode.'&'.$num);			
	  }
	}	
}

/*
 * function select Staff from id Staff use for page modify Staff 
 */
function selStaff()
{
	//get stId from page edit
	if (isset($_GET['stid'])) {
		$stId 		= (int)conditionEscape($_GET['stid']);
	}
	//if stId is not exist it direct to page index
	if (!isset($stId) && $stId != "") {
		header("Location: index.php");
	} else {
		$sql = "SELECT * FROM emp WHERE id_emp='".$stId."'";
		ict($sql);
	}	
}
/*
 * function select staff from id staff use for modify staff
 */
function selEdtst() 
{
	global $link, $row_ict;
  //get staff id from page edit
  if (isset($_GET['stid'])) {
	$stId		= (int)conditionEscape($_GET['stid']);
  }
  //if staff id is not exist it direct to page index
  if (!isset($stId) && $stId != "") {
	header("Location: index.php");
  } else {
	$sql = "SELECT * FROM emp WHERE id_emp='".$stId."'";
	ict($sql);
  }	
  $result 	= mysql_query($sql, $link) or die(mysql_error());
  return $result;
}


/*
 * function select staff table
 */
function selectStaff() 
{ 
  //Variable connect database
  global $link;
	
  //get value from text box search
  if (isset($_GET['schstr'])){
	$search	= conditionEscape($_GET['schstr']);
  }
  //if textbox search got value 
  if (isset($search) && $search != "") {
	$sql = "SELECT * FROM emp WHERE name LIKE '$search%' OR name_e LIKE '$search%'";
  } else {
	$sql = "SELECT * FROM emp";
  }
	
  $rowsPerPage = 10;
  $result		 = mysql_query(getPagingQuery($sql, $rowsPerPage)) or die(mysql_error());
  return $result;
}

function pageLink() 
{
  //get value from text box search
  if (isset($_GET['schstr'])){
	$search		 = conditionEscape($_GET['schstr']);
	$queryString = "schstr=".$search;
  } else {
	$queryString = "";
  }
  //if textbox search got value 
  if (isset($search) && $search != "") {
	$sql  = "SELECT id_emp as 'stID', name, last_name, name_e, last_name_e, birth_date, tel, email_address, position, code_emp FROM emp WHERE name LIKE '$search%' OR name_e LIKE '$search%'";		
  } else {
	$sql = "SELECT id_emp as 'stID', name, last_name, name_e, last_name_e, birth_date, tel, email_address, position, code_emp FROM emp";
  }
	
  $rowsPerPage = 10;
  $pagingLink	 = getPagingLink($sql, $rowsPerPage, $queryString);  //call function getPagingLink from file function.php	
  return $pagingLink;
}

function selectStaffDet()
{ 
	//Variable connect database
	global $link, $row_ict;
	
	//get value from text box search
	if (isset($_GET['schstr'])){
		$search		= conditionEscape($_GET['schstr']);
	}
	
	//if textbox search got value 
	if (isset($search) && $search != "") {
		$sql = "SELECT DISTINCT id_emp as 'stID', code_emp, name, last_name, name_e, last_name_e, birth_date, sex, born_add, address, tribe, religion, tel, status, hobby, free_t, pe_id, pass_id, hire_date, remark, edu, edu_place, description, respon, cal_code, email_address, position, party_date, young_date, labor_date, deputy_party_date, women_date, old_specialist, record_date, status_per, status_birthdate, status_w_place, status_w, child_n, staff_pict FROM `emp` WHERE name LIKE '$search%' OR name_e LIKE '$search%' OR code_emp LIKE '$search%'";
	} else {
		$sql = "SELECT DISTINCT id_emp as 'stID', code_emp, name, last_name, name_e, last_name_e, birth_date, sex, born_add, address, tribe, religion, tel, status, hobby, free_t, pe_id, pass_id, hire_date, remark, edu, edu_place, description, respon, cal_code, email_address, position, party_date, young_date, labor_date, deputy_party_date, women_date, old_specialist, record_date, status_per, status_birthdate, status_w_place, status_w, child_n, staff_pict FROM `emp`";
	}

	$rowsPerPage = 10;
	$result		 = mysql_query(getPagingQuery($sql, $rowsPerPage)) or die(mysql_error());
	return $result;		
}

/*
 * function select info of Staff to show in v_detail_st.php
 */
function viewStaffInfo() 
{
	global $link;
	if (isset($_GET['stid'])) {
		$stId = $_GET['stid'];
		$sql = "SELECT id_emp, code_emp, name, last_name, name_e, last_name_e, birth_date, sex, born_add, address, tribe, religion, tel, status, hobby, free_t, pe_id, pass_id, hire_date, remark, edu, edu_place, description, respon, cal_code, email_address, position, party_date, young_date, labor_date, deputy_party_date, women_date, old_specialist, record_date, status_per, status_birthdate, status_w_place, status_w, child_n, staff_pict 
			FROM emp WHERE id_emp = $stId";
		
	} else {
		$stId = $_GET['stid'];
		$sql = "SELECT id_emp, code_emp, name, last_name, name_e, last_name_e, birth_date, sex, born_add, address, tribe, religion, tel, status, hobby, free_t, pe_id, pass_id, hire_date, remark, edu, edu_place, description, respon, cal_code, email_address, position, party_date, young_date, labor_date, deputy_party_date, women_date, old_specialist, record_date, status_per, status_birthdate, status_w_place, status_w, child_n, staff_pict
			FROM emp WHERE id_emp = $stId";
	}
	
	ict($sql);
	$result 	= mysql_query($sql, $link) or die(mysql_error());
	return $result;
}

/*
 * function select list all Staffs to show in the view page
 */
function listStaff()
{
	//get value from text box search
	if (isset($_GET['schstr'])){
		$search		= conditionEscape($_GET['schstr']);
	}
	
	//if textbox search got value 
	if (isset($search) && $search != "") {
		$sql  = "SELECT id_emp, name, last_name FROM emp WHERE name LIKE '$search%' ";
	} else {
		$sql = "SELECT id_emp, name, last_name FROM emp";
	}

	$rowsPerPage = 10;
	$result		 = mysql_query(getPagingQuery($sql, $rowsPerPage)) or die(mysql_error());
	return $result;	
}
/*----------------- select the position -------------*/
function selPosition(){
	return mysql_query("SELECT * FROM emp_position WHERE isdeleted <> 1 ORDER BY position_id ASC ");
}

?>