<?php

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

include("config.inc.php");
include("function.inc.php");


$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass'],true,65536) 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");

$mode 			= $_POST['mode'];
$docName 		= $_POST['docName'];
$docDescription = $_POST['docDescription'];
$docParentId	= $_POST['docParentId'];
$docid			= $_POST['docid'];

if ($docParentId == '') $docParentId = "NULL";
$mode = strrev($mode).'doc';

if ($mode == 'insertdoc' && $docid == ''){
	$query = "insert into DocumentType_Setup (DTS_CreationDate, DTS_Name, DTS_Description, DTS_ParentId) values 
				(now(), ".GetSQLValueString($docName,'text').", ".GetSQLValueString($docDescription,'text').", ".$docParentId.") ";
	$result = mysql_query($query, $connection);
	$message = mysql_error();
}
if ($mode == 'editdoc' && $docid != ''){
	$query = "update DocumentType_Setup set DTS_Name = ".GetSQLValueString($docName,'text').", 
					DTS_Description = ".GetSQLValueString($docDescription,'text').", 
					DTS_ParentId = ".$docParentId."
			where DTS_Id = ".GetSQLValueString($docid,'int')." ";
	$result = mysql_query($query, $connection);
	$message = mysql_error();
}
if ($mode == 'deletedoc' && $docid != ''){
	$query = "delete from DocumentType_Setup where DTS_Id = ".GetSQLValueString($docid,'int')." ";
	$result = mysql_query($query, $connection);
	$message = mysql_error();
}

echo "<form name='docAction' method='POST' action='Form_DocumentTypes.php' >
		<input type=hidden name='message' value='".htmlspecialchars($message,ENT_QUOTES)."'> 
	</form>";
print <<<HERE
	<script>
	document.docAction.submit();
	</script>

HERE;

?>
		           