<?php

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


include("config.inc.php");

$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 FS_TicketVisitApp as loginaccess, user_account, Language from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' 	and Upper(Login_Schema) = 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");

$query_navigator = "SELECT LANGUAGE,
       ClearPage,
       Excel,
       Save,
       Print,
       Addd,
       SEARCH,
       Pending,
       CopyJV,
       PaymentRequests,
       ExportExcel
FROM ".$_SESSION['dbLabel'].".mainform_label
WHERE LANGUAGE = '".$Language."'";
$result_navigator = mysql_query($query_navigator, $connection);
$row_navigator    = mysql_fetch_array($result_navigator);
extract($row_navigator);

	
//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($mainmode=='')  		$mainmode='insert';
if ($startrange=='') 	$startrange = 0;
if ($perpage == '' ) 	$perpage = $config['trowlimit'][0];
if($tvStatus=='')  		$tvStatus='PENDING';
if($tvClosed=='')  		$tvClosed= 0;
if($tvApproved=='')  	$tvApproved= 0;

print <<<HERE

<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 

<script type="text/javascript" language="javascript">

function SearchVisit(){
	window.open('Form_TicketVisitSearch.php','Ticket Visit', 'width=1000,height=600');	
}

function SubmitRecord(){
	var x = document.ticketvisit;
	if (x.tvDate.value == ''){
		alert('Date is Empty!');
		x.tvDate.focus();
	} 
	else if (x.tvprojectCode.value == ''){
		alert('Branch is Empty!');
		x.tvprojectCode.focus();
	} 
	else {
		x.action = 'Form_TicketVisit_Action.php';
		x.submit();
	}
}

function editRecord(probid){
	var x = document.ticketvisit;
	x.tvId.value = probid;
	x.mainmode.value = 'edit';
	x.submit();
}

function deleteRecord(probid){
	if(confirm('Are you sure you want to delete this Record?')){
		var x = document.ticketvisit;
		x.tvId.value = probid;
		x.mainmode.value = 'delete';
		x.action = 'Form_TicketVisit_Action.php';
		x.submit();
	}
}

function ClearPage(){
	var x = document.ticketvisit;
	if (confirm ( "Are you sure you want to Clear Page?"))
	{
		x.mainmode.value 		= '';		
		x.message.value 		= '';		
		x.tvId.value 			= '';		
		x.tvDate.value 			= '';		
		x.tvprojectCode.value 	= '';		
		x.tvprojectDesc.value 	= '';		
		x.tvSupTeam.value 		= '';		
		x.tvSupTeamId.value 	= '';		
		x.tvRemark.value 		= '';		
		x.tvClosed.value 		= '';		
		x.tvStatus.value 		= '';		
		x.tvContactPerson.value = '';		
		x.tvApproved.value 		= '';		
		x.image.value 			= '';		
		x.tvType.value 			= '';		
			
		
		x.action='Form_TicketVisit.php';
		x.submit();
	}	
}

function showlistProject(){
	var vProjectCode = encodeURIComponent(document.getElementById('tvprojectCode').value);
	window.open('List_SorProject.php?frm=44&item=&PProject_Code='+vProjectCode,'mywindow','width=850,height=550,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}


function ShowlistSupportUser(){
	var tsSupTeam = encodeURIComponent(document.getElementById('tvSupTeam').value);
	window.open('List_SupportUsers.php?frm=2&PUser='+tsSupTeam,'Users','width=850,height=550,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}


function setChecked(vField){
	if (document.getElementById(vField).checked)
		document.getElementById(vField).value = 1;
	else 
		document.getElementById(vField).value = 0;
}

function SetNextPage(startrange,perpage){
	document.ticketvisit.action='Form_TicketVisit.php?PRef=&startrange='+startrange+'&perpage='+perpage;
	document.ticketvisit.submit();
}

function SetPrvPage(startrange,perpage){
	document.ticketvisit.action='Form_TicketVisit.php?PRef=&startrange='+startrange+'&perpage='+perpage;
	document.ticketvisit.submit();
}

function set_limit(){

	perpage=document.ticketvisit.perpage.value;
	document.ticketvisit.action='Form_TicketVisit.php?perpage='+perpage+'&startrange=0';
	document.ticketvisit.submit();
}

</script>

<title>Visit</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; }
.style12 {
	font-size: 9px;
	color: #999999;
	font-weight: bold;
}
.style15 {color: #5151FF; font-size: 10px;}
.style16 {
	font-size: 16px;
	color: #FF0000;
	font-weight: bold;
	font-family: "Times New Roman", Times, serif;
}
.style17 {font-size: 9px}
.style25 {color: #999999; font-weight: bold; font-size: 10px; }
.style26 {color: #FF0000}
.style27 {color: #999999 ;font-size: 10px;}
.style28 {color: #FFFFFF ;font-size: 10px;}

</style>
<link type="text/css" rel="stylesheet" href="dhtmlgoodies_calendar.css?random=20051112" media="screen"></LINK>
<SCRIPT type="text/javascript" src="dhtmlgoodies_calendar.js?random=20060118"></script>
<link rel="stylesheet" type="text/css" media="all" href="css-v2/style.css">
</head>
<body >
<form name='ticketvisit' method='post' enctype='multipart/form-data' >
<input type='hidden' name='mainmode' value='$mainmode'>
<input type=hidden name='message' value='$message'>
<input type=hidden name='tvId' value='$tvId'>

HERE;

if ($mainmode == 'edit' && $tvId != ''){
	$queryEdit = "select date_format(ticketVisit_date,'%d/%m/%Y'), ticket_visit.project_code, project_name, ticketVisit_userId, username, ticketVisit_remark, ticketVisit_status,
				 ticketVisit_contactPerson, ifnull(ticketVisit_closed,0), ifnull(ticketVisit_approved,0), ticketVisit_document, ticketVisit_type
				 from ticket_visit
				 left join user_support on user_id = ticket_visit.ticketVisit_userId 
				 left join project on project.project_code = ticket_visit.project_code 
				 where ticketVisit_id = $tvId "; //echo $queryEdit;
	$resEdit = mysql_query($queryEdit, $connection);
	$rowEdit = mysql_fetch_row($resEdit);
	$tvDate 		= $rowEdit[0];
	$tvprojectCode 	= $rowEdit[1];
	$tvprojectDesc 	= $rowEdit[2];
	$tvSupTeamId 	= $rowEdit[3];
	$tvSupTeam 		= $rowEdit[4];
	$tvRemark 		= $rowEdit[5];
	$tvStatus 		= $rowEdit[6];
	$tvContactPerson= $rowEdit[7];
	$tvClosed 		= $rowEdit[8];
	$tvApproved 	= $rowEdit[9];
	$image			= $rowEdit[10];
	$tvType			= $rowEdit[11];
}

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' style='text-transform: uppercase'>Visit</span></td>
				<td class='message' valign='middle' style='height:26px;'>".htmlspecialchars($message, ENT_QUOTES)."</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>$ClearPage</span></span></button>";


echo"		<button type='button' id='btnPost' name='btnPost' onclick=\"SubmitRecord();return false;\" title='Save Changes'><span><span>$Save</span></span></button>";
echo"		<button type='button' id='btnSearch' name='btnSearch' onclick=\"SearchVisit();return false;\" title='Search Visit'><span><span>$SEARCH</span></span></button>";

echo"   </td><td align='left' width='240px'></td><td align='left'><span class='style1'><b>Results/page</b> <select name='perpage' id='perpage'  onchange=set_limit();>";
	foreach ($config['trowlimit'] as $value) {
		echo "<option value='$value' ".($value==$perpage?'selected':'')."  >$value</option>";
	}
		
echo "</select></td></tr>";
echo"	</table>";
echo"</td></tr>";

echo"<tr><td id='content-body'>";		
echo"<table border='0'  align='center' valign='top' width=100% class='tablecontent'>
	<col width=65%>";
		
	echo"   <tr bgcolor='#F7F7F7'><td valign='top'  align='left'>";	  
echo"<table border='0' style='table-layout:fixed' align='left' valign='top' width='1100px'>
      <col width=120><col width=300><col width=120><col width=200><col width=180><col width=180>";

//1st row	  
echo"<tr bgcolor='#F7F7F7' height='25'>
     <td valign='center' align='left' ><span class='style1'>DateTime<span class='style26'>*</span></span>";
	echo"</td>";
	echo"<td ><input type='text' name='tvDate' id='tvDate' value='$tvDate' style='font-size:12px; width:150px; height:20px;' maxlength='10' />
	        <a href='#' onclick=\"displayCalendar(document.ticketvisit.tvDate,'dd/mm/yyyy',this);  return false;\" name='anchor1' id='anchor1'><img src='./img/cal.gif' border='0' align='center'></a>
		</td>";
	
echo"<td><span class='style1'>Branch<span class='style26'>*</span></span></td>
	<td colspan='2'><input type='text' name='tvprojectCode' id='tvprojectCode' value='$tvprojectCode' style='font-size:12px; width:150px; height:20px;' maxlength='20' />
		<input type='text' name='tvprojectDesc' id='tvprojectDesc' value='$tvprojectDesc' style='font-size:12px; width:200px; height:20px;' />
		<img src='img/btnopen.jpg' weight='15' height='20' onclick=\"showlistProject();\" title='Select Branch' onmouseover=\"style.cursor='hand';\">
	</td>
	<td></td>
	</tr>";


//2nd row
echo"<tr bgcolor='#F7F7F7' height='25'>
    <td><span class='style1'>Support Team</span></td>
	<td><input type='text' name='tvSupTeam' id='tvSupTeam' value='$tvSupTeam' style='font-size:12px; width:150px; height:20px;' maxlength='20' />
		<input type='hidden' name='tvSupTeamId' id='tvSupTeamId' value='$tvSupTeamId'>
		<img src='img/btnopen.jpg' weight='15' height='20' onclick=\"ShowlistSupportUser();\" title='Select Support' onmouseover=\"style.cursor='hand';\">	
	</td>";
	
echo"<td><span class='style1'>Remark</span></td>
	<td colspan='2'><textarea name='tvRemark' id='tvRemark' row='1' cols='50'>".htmlspecialchars($tvRemark, ENT_QUOTES)."</textarea>		
	</td>
	<td><span class='style1'>Closed</span><input type='checkbox' name='tvClosed' id='tvClosed' value='$tvClosed' onclick=\"setChecked('tvClosed');\" ".($tvClosed==1?'checked':'')."></td></tr>";	

//4th row
echo"<tr bgcolor='#F7F7F7' height='25'>
     <td valign='center' align='left' ><span class='style1'>Ticket Status</span>";
	echo"</td>";
	echo"<td ><span class='style1'>$tvStatus</span><input type='hidden' name='tvStatus' id='tvStatus' value='$tvStatus' ></td>";
	
echo"<td><span class='style1'>Contact Person</span></td>
	<td colspan='2'><input type='text' name='tvContactPerson' id='tvContactPerson' value='".htmlspecialchars($tvContactPerson, ENT_QUOTES)."'  style='width:380px;'>		
	</td>
	<td><span class='style1'>Approved ".($tvApproved == 0?'&#10006;':'&#10004;')."</span><input type='hidden' name='tvApproved' id='tvApproved' value='$tvApproved' ></td>
	</tr>";	


	
//5th row	
echo"<tr bgcolor='#F7F7F7' height='25'>
     <td valign='center' align='left' ><span class='style1'>Visit Type</span>";
	echo"</td>";
	echo"<td ><select name='tvType' id='tvType' style='font-size:12px; width:150px; height:20px;' >
				<option value='' ".($tvType == ''?'selected':'')."></option>
				<option value='1' ".($tvType == '1'?'selected':'').">Maintenance</option>
				<option value='2' ".($tvType == '2'?'selected':'').">Ticket</option>
			</select>
		</td>";
echo"<td><span class='style1'>Upload Document</span></td>
	<td><input type='file' name='image' id='image' style='width:200px;' >
	</td>
	<td><span class='style1'></span></td>
	</tr>";		
echo"<tr height='20'><td colspan='5'>&nbsp;</td></tr>";

echo"<tr height='20'><td colspan='5' align='center'></td></tr>";

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

echo"<table style='width:100%' border='0' cellspacing=0>
	<col width=6%><col width=11%><col width=16%><col width=13%><col width=15%><col width=30%><col width=9%>";				
echo"<tr height='30px' style='background-color:darkgray;'> 
	 <td>&nbsp;</td>
	 <td align='left' ><span class='style1'><b>Visit Date</b></td>
	 <td align='left' ><span class='style1'><b>Branch</b></td>
	 <td align='left' ><span class='style1'><b>Support Team</b></td>
	 <td align='left' ><span class='style1'><b>Contact Person</b></td>
	 <td align='left' ><span class='style1'><b>Remark</b></td>
	 <td align='left' ><span class='style1'><b>Closed</b></td>
	</tr>";
	

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

echo "<tr><td colspan='3' valign='top'><div style='width:100%;height:400px;overflow-x:hidden;overflow-y:scroll;'>";
echo"<table style='width:100%' border='0' cellspacing=0>
	<col width=3%><col width=3%><col width=12%><col width=17%><col width=13%><col width=15%><col width=30%><col width=7%>";		

$queryAll = "select ticketVisit_id, date_format(ticketVisit_date,'%d-%m-%Y'), project_code, username, ticketVisit_contactPerson,  ticketVisit_remark, ifnull(ticketVisit_closed,0)
			from ticket_visit
			left join user_support on user_id = ticketVisit_userId
			order by ticketVisit_date desc ";
$resAll = mysql_query();
$resAll= mysql_query($queryAll,$connection) or die(mysql_error());
$totalrows = mysql_num_rows($resAll);
	
$queryAll .= " limit ".$startrange.",".$perpage;		

$resAll=mysql_query($queryAll,$connection) or die(mysql_error());

while($rows=mysql_fetch_array($resAll)){
	$ct+=1;
	$stylefont='style15';
	if($ct%2==0)
		$Color="style='background-color:".$grid_color1."'";
	else			
		$Color="style='background-color:".$grid_color2."'";	
			
	if($tpId==$rows[0]){
		
		$Color="style='background-color:#1e87a5'";
		$stylefont='style28';
	}
	
	echo"<tr height='15px' $Color> 
		 <td align='center'><img border='0' src='img/button_edit.png' width='9' height='10' title='Edit Detail' onmouseover=\"this.style.cursor='pointer'\" OnClick=\"editRecord('$rows[0]')\"></td>
		 <td align='center'><img border='0' src='img/c_delete.png' width='9' height='10' title='Delete Detail' onmouseover=\"this.style.cursor='pointer'\" OnClick=\"deleteRecord('$rows[0]')\"></td>
		 <td align='left' ><span class='$stylefont'>$rows[1]</td>
		 <td align='left' ><span class='$stylefont'>$rows[2]</td>
		 <td align='left' ><span class='$stylefont'>$rows[3]</td>
		 <td align='left' ><span class='$stylefont'>$rows[4]</td>
		 <td align='left' ><span class='$stylefont'>$rows[5]</td>
		 <td align='left' ><span class='$stylefont'>".($rows[6]== 0?'&#10006;':'&#10004;')."</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.jpg' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPrvPage('".$prvstartrange."','".$perpage."')>";
		}
		else
			echo "&nbsp;";
		
		echo"</td><td  align='center'>";
		
		
		if ($startrange <= $totalrows){
			$startrange = $startrange + $perpage;
			if  ($startrange <= $totalrows)
				echo"<img src='img/arrowright.jpg' title='Next' onMouseOver=\"style.cursor='hand'\" onclick=SetNextPage('".$startrange."','".$perpage."')>";
		} else
			echo "&nbsp;";
			
		echo"</td></tr>";
echo "</table></td></tr>";		
  
echo" <tr height='30'><td align='center' id='content-bottom' ></td></tr>";
include("footer.php");	

print <<< HERE
      
	  </table>
HERE;


print <<<HERE
</form>
</body>

HERE;

?>