<?php

session_start();
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
if($_SESSION['dbName']==""){
header("location:../index.php");
}


include("config.inc.php");

$title = "Document Type Setup";
$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
mysql_select_db($_SESSION['dbName'],$connection) or die("Could not find DB");

if(	$_SESSION['encodingmode'] == 'utf8' || $_SESSION['encodingmode'] == 'utf-8'){
	mysql_query("SET NAMES 'utf8'"); 
	mysql_query('SET CHARACTER SET utf8'); 
	mysql_set_charset('utf8', $connection);
}

$querylog="SELECT FU_DocumentTypes as loginaccess from ".$_SESSION['dbLabel'].".ListUserDir 
		  where Usercode='".$_SESSION['useraccount']."' and upper(concat(CompanyName,'_',Diryear)) = Upper('".$_SESSION['dbName']."')	";
	$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($resultlog)){
		extract($rows);
	}

if ($loginaccess=='' or $loginaccess==0)
	header("location:menu.php");


	
//Read From Setup Table							
$querysetup= " select grid_color1, grid_color2,maxperpage from setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}

if ($startrange=='') $startrange = 0;
if ($perpage=='')	$perpage = 20;

if($mainmode=='') $mainmode='insert';

print <<<HERE
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 
<title>$title</title>

<style type="text/css">

body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
}
body {
	margin-top: 2px;
	margin-left: 1px;
}
a:link {
	text-decoration: none;
	color: #0000FF;
}
a:visited {
	text-decoration: none;
	color: #0000FF;
}
a:hover {
	text-decoration: none;
	color: #0000FF;
}
a:active {
	text-decoration: none;
	color: #0000FF;
}
.style1 {color: #999999; font-weight: bold; }
.style15 {color: #5151FF; font-size: 10px;}
.style26 {color: #FF0000}
.style27 {color: #999999 ;font-size: 10px;}
.style28 {color: #FFFFFF ;font-size: 10px;}

</style>

<LINK REL="stylesheet" TYPE="text/css" MEDIA="all" HREF="tabs.css">
<link rel="stylesheet" type="text/css" media="all" href="css-v2/style.css">

</head>
<body >
<form name='doctype' method='post' action=''>

HERE;

echo "
<input type=hidden name='mode' value='".htmlspecialchars($mode,ENT_QUOTES)."'>
<input type=hidden name='docid' value='".htmlspecialchars($docid,ENT_QUOTES)."'>
<input type=hidden name='startrange' value='".htmlspecialchars($startrange,ENT_QUOTES)."'>
<input type=hidden name='message' value='".htmlspecialchars($message,ENT_QUOTES)."'> ";

echo"<table cellpadding='0' cellspacing='0' border='0'  align='center' class='layout-width'>";
include_once("header.php");
echo" <tr><td  valign='top'>";

include_once("sidemenu.php");
echo"  </td></tr>";


echo" <tr style='height:26px;'>
		<td  valign='top' id='content-top'>
			<table border='0' cellpadding='0' cellspacing='0' style='table-layout:fixed; height:26px;' class='layout-width'>
			<tr style='height:26px;'>
				<td style='width:38px; height:26px;'>&nbsp;</td>
				<td valign='middle' style='width:300px; height:26px;'><span class='title'>".strtoupper($title)."</span></td>
				<td class='message' valign='middle' style='height:26px;'>".$message."</td>
			</tr></table>
		</td>
	  </tr>";
	  

echo"<tr>
		<td  height='34'  valign='top' id='content-top2'>
			<table border='0' cellpadding='0' cellspacing='0' class='layout-width'>
			<tr>
				<td width='38'  height='34'>&nbsp;</td>
				<td valign='middle' height='34' width='720px'>";
echo"		<button type='button' id='btnClear' name='btnClear' onclick='ClearPage(); return false;' title='Clear Page'><span><span>Clear</span></span></button>";
echo"		<button type='button' id='btnPost' name='btnPost' onclick='SubmitRecord(); return false;' title='Save Changes' $Insmode><span><span>Save</span></span></button>";
		
echo"   </td><td align='left' width='250px'> 
	<input type='text' name='txtKeyword' value='$txtKeyword' style='width:105px;' onKeyPress='return submitenter(this,event)'>
	<button type='button' id='btnSearch' name='btnSearch' onclick='set_limit(); return false;' title='Search Area'><span><span>Find</span></span></button>
<button type='button' id='btnClear' name='btnClear' onclick='ClearKeyword(); return false;' title='Clear Keyword'><span><span>Clear</span></span></button>";
     echo"</td><td align='left'><span class='style1'><b>Results/page</b></span>
	 <span class='style1'><select name='perpage' id='perpage' value='$perpage' onchange=set_limit();>";
	 
	
		
	for($i=10;$i<=$maxperpage;$i=$i+10){
		if($i==$perpage)
			echo "<option value='$i'  selected>$i</option>";
		else echo "<option value='$i'>$i</option>";
		
	} 
	 echo"</td></tr></tr>";
echo"	</table>";
echo"</td></tr>";

if ($mode == 'edit' && $docid != ''){
	$query = "select DTS_Name, DTS_Description, DTS_ParentId from DocumentType_Setup where DTS_Id = $docid limit 1";
	$result = mysql_query($query, $connection);
	$row = mysql_fetch_row($result);
	$docName 		= $row[0];
	$docDescription = $row[1];
	$docParentId	= $row[2];
}

echo"<tr><td id='content-body'>";				
echo"<table border='0'  align='center' valign='top'  class='tablecontent' >
	<col width=64%>";

echo"   <tr bgcolor='#F7F7F7'  ><td valign='top' align='left'>";	 

echo" <table border='0' style='table-layout:fixed' align='left' valign='top' >
      <col width=120><col width=450>";
	  
echo"<tr bgcolor='#F7F7F7' height='25'>
		<td valign='center' align='left' ><span class='style1'>Type Name<span class='style26'>*</span></span></td>
		<td><input type='text' name='docName' id='docName' value='".htmlspecialchars($docName,ENT_QUOTES)."' style='font-size:12px; width:200px; height:18px;' maxlength='80'/></td>
	</tr>";

echo"<tr bgcolor='#F7F7F7' height='25'>
		<td valign='center' align='left' ><span class='style1'>Description</span></td>
		<td><input type='text' name='docDescription' id='docDescription' value='".htmlspecialchars($docDescription,ENT_QUOTES)."' style='font-size:12px; width:200px; height:18px;' maxlength='256'/></td>
	</tr>";

echo"<tr bgcolor='#F7F7F7' height='25'>
		<td valign='center' align='left' ><span class='style1'>Parent Type Name</span></td>
		<td><select name='docParentId' id='docParentId' style='font-size:12px; width:200px; height:18px;'>";
	$queryParent = "select DTS_Id, DTS_Name from DocumentType_Setup where if ('$docid'='', 1=1, DTS_Id != '$docid') ";	
	$resParent = mysql_query($queryParent, $connection);

	echo "<option value='".htmlspecialchars('',ENT_QUOTES)."' ".($docParentId == ''?selected:'')." ></option>";
	while ($rowParent = mysql_fetch_array($resParent)){
		echo "<option value='".htmlspecialchars($rowParent[0],ENT_QUOTES)."' ".($docParentId == $rowParent[0]?selected:'')." >$rowParent[1]</option>";
	}
echo "	</select></td>
	</tr>";


 
echo"<tr height=15px><td colspan=3></td></tr>";


echo"</table></td></tr><tr><td  valign='top'>";

// Richard
echo"<table style='width:1050px;' border='0' cellspacing=0 cellpadding=0>
	<tr><td id='' style='width:1050px;height:15px;' colspan='3'></td>
	</tr>
	<tr bgcolor='#F7F7F7' > <td  valign='top' colspan=3 id='' style='width:1050px;'>";

echo"<table style='width:1050px; position:relative; left:15px;' border='0' cellspacing=0 >
	<col width=50><col width=350><col width=300><col width=350>

	 <tr style='background-color:darkgray;' height='30px' >
	 <td>&nbsp;</td>
	 <td align='center' ><span class='style1'><b>Type Name</b></span></td>
	 <td align='center' ><span class='style1'><b>Description</b></span></td> 
	 <td align='center' ><span class='style1'><b>Parent Type Name</b></span></td>
	 </tr>";
	
	 
	echo"<tr><td  colspan=4 ><div style='overflow-y:scroll;height:400px;width:1060px;border:0px solid #000000;'>
	<table  border='0' width='1050px'>
	<col width=25><col width=25><col width=350><col width=300><col width=350>";
	
	$querysearch=" select orgSetup.DTS_Id, orgSetup.DTS_Name, orgSetup.DTS_Description, parentSetup.DTS_Name from DocumentType_Setup as orgSetup
				  left join DocumentType_Setup as parentSetup on parentSetup.DTS_Id = orgSetup.DTS_ParentId ";
	$querysearch .=" where (orgSetup.DTS_Name like"."('%".$txtKeyword."%') or orgSetup.DTS_Description like"."('%".$txtKeyword."%')) 
					order by orgSetup.DTS_Id, parentSetup.DTS_Id ";

	$resultsearch=mysql_query($querysearch,$connection) or die(mysql_error());
	$totalrows = mysql_num_rows($resultsearch);
	
		
	$querysearch = $querysearch." limit ".$startrange.",".$perpage;
	

	$resultsearch=mysql_query($querysearch,$connection) or die(mysql_error());	
	while($rows=mysql_fetch_array($resultsearch)){
		$ct+=1;
		$stylefont='style15';
		
		if($ct%2==0)
			$Color="style='background-color:".$grid_color1."'";
		else			
			$Color="style='background-color:".$grid_color2."'";	
				
		if($mode=='edit' && $docid==$rows[0]){
			
			$Color="style='background-color:#1e87a5'";
			$stylefont='style28';
	}	
		
	echo "<tr $Color height='20px'>	
			<td align=center ><img border='0' src='img/c_delete.png' width='10' height='11' title='Delete Detail' onmouseover=\"this.style.cursor='pointer'\" OnClick=\"deleteRecord('$rows[0]');\" ></td>
			<td align=center ><img border='0' src='img/button_edit.png' width='10' height='11' title='Edit Detail' onmouseover=\"this.style.cursor='pointer'\" OnClick=\"editRecord('$rows[0]');\" ></td>
			<td ><span class='$stylefont' >$rows[1]</span></td>
			<td ><span class='$stylefont'>$rows[2]</span></td>
			<td ><span class='$stylefont'>$rows[3]</span></td>
		</tr>";
}		
	
echo"</table></div></td></tr>";
echo"<tr  ><td >&nbsp;</td><td align='center' >";
		
		$diff= $startrange - $perpage;
		
		if (($diff) >= 0 ){
			$prvstartrange = $startrange - $perpage;
			echo"<img src='img/arrowleft.png' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPagination('".$prvstartrange."','".$perpage."')>";
		}
		else
			echo "&nbsp;";
		
		echo"</td><td  align='center' colspan=2>";
		
		
		if ($startrange <= $totalrows){
			$startrange = $startrange + $perpage;
			if  ($startrange <= $totalrows)
				echo"<img src='img/arrowright.png' title='Next' onMouseOver=\"style.cursor='hand'\" onclick=SetPagination('".$startrange."','".$perpage."')>";
		} else
			echo "&nbsp;";
			
		echo"</td></tr>";
		
		

echo"</table></td></tr><tr>
					<td id='' style='width:380px;height:20px;' colspan='3'></td>
				</tr></table></td></tr>";			

echo" </table></td></tr>";

echo" <tr height='30'><td align='center' id='content-bottom' ></td></tr>";
include("footer.php");	
echo" </table>  ";//end of last table menu



print <<<HERE
</form>
<script>
function SubmitRecord(){
	if ( document.doctype.docName.value == ''){
		alert('Name Should not be Empty!');
		return 0;
	}
	if (document.doctype.docid.value == '')
		document.doctype.mode.value = 'tresni';
	else 	
		document.doctype.mode.value = 'tide';
	
	document.doctype.action="Form_DocumentTypes_Action.php";		
	document.doctype.submit();
}

function editRecord(docId){
	document.doctype.docid.value = docId;
	document.doctype.mode.value = 'edit';
	document.doctype.submit();
}

function ClearPage(){
	document.doctype.docid.value 			= '';
	document.doctype.mode.value 			= '';
	document.doctype.message.value 			= '';
	document.doctype.docName.value 			= '';
	document.doctype.docDescription.value 	= '';
	document.doctype.docParentId.value 		= '';
	document.doctype.txtKeyword.value 		= '';
	document.doctype.perpage.value 			= '';
	document.doctype.action = 'Form_DocumentTypes.php';
	document.doctype.submit();
}

function deleteRecord(docId){
	document.doctype.docid.value = docId;
	if (docId != ''){
		document.doctype.mode.value = 'eteled';
		document.doctype.action="Form_DocumentTypes_Action.php";		
		document.doctype.submit();
	}
}


function SetPagination(startrange,perpage){
	document.doctype.startrange.value = startrange;
	document.doctype.perpage.value = perpage;
	document.doctype.submit();
}


function set_limit(){
	document.doctype.startrange.value = 0;
	document.doctype.submit();
}


function ClearKeyword(){
	document.doctype.txtKeyword.value='';
	document.doctype.startrange.value = 0;
	document.doctype.submit();
}

function submitenter(myfield,e){
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {
	document.doctype.startrange.value = 0;
	document.doctype.submit();
  
   return false;
   }
else
   return true;
}

</script>
</body>

HERE;

?>
