<?php
session_start();
if($_SESSION['dbName']==""){
header("location:../index.php");
}
include("config.inc.php");
include("Functions_Trans.php");
//include("Forms_DataGrid.php");
include("Forms_RMA_DataGrid.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");

$frm = $_GET['frm'];
$rmadt_id = $_GET['rmadt_id'];
$type = $_GET['type'];

if($type == 'C') {
	$Label  = "Problem Category"; 
	$Label2 = "Problem";
	$Label3 = "Problem Desc.";
}
else
    if($type == 'A')	{
	$Label  = "Accessories";
	$Label2 = "Accessories";
	$Label3 = "Accessory Desc.";
}
else
    if($type == 'E')	{
	$Label  = "Engineers";
	$Label2 = "Engineers";
	$Label3 = "Engineer Name";
}
else
    if($type == 'P')	{
	$Label  = "Picked Up By";
	$Label2 = "Picked Up By";
	$Label3 = "Picked Up By";
}
print <<<HERE
<html>
<head>
<title>List of Problem Categories</title>
<script>
function moveNext(){
	document.getElementById('start').value = Number(document.getElementById('start').value) + 20;
	document.getElementById('pagenumber').value = Number(document.getElementById('pagenumber').value) + 1;
	var start = document.getElementById('start').value;
	var pagenumber = document.getElementById('pagenumber').value;
	var searchproblem = document.getElementById('searchproblem').value;
	var type = document.getElementById('type').value;
	var str = "List_ProblemCategory_Actions.php?actionmode=movenext&searchproblem="+searchproblem+"&start="+start+"&pagenumber="+pagenumber+"&type="+type;
	//alert(str);
	var xhttp = new XMLHttpRequest();
         xhttp.onreadystatechange = function() {
         if (xhttp.readyState == 4 && xhttp.status == 200) {
			 var data = xhttp.responseText.split('[BRK]');
			 //alert(data[0]);
			 document.getElementById('list_problem').innerHTML = data[0];
			 }
        };
       xhttp.open("GET", str, true);
       xhttp.send();
}
function movePrev(){
	document.getElementById('start').value = Number(document.getElementById('start').value) - 20;
	document.getElementById('pagenumber').value = Number(document.getElementById('pagenumber').value) - 1;
	var start = document.getElementById('start').value;
	var pagenumber = document.getElementById('pagenumber').value;
	var searchproblem = document.getElementById('searchproblem').value;
	var type = document.getElementById('type').value;
	var str = "List_ProblemCategory_Actions.php?actionmode=moveprev&searchproblem="+searchproblem+"&start="+start+"&pagenumber="+pagenumber+"&type="+type;
	//alert(str);
	var xhttp = new XMLHttpRequest();
         xhttp.onreadystatechange = function() {
         if (xhttp.readyState == 4 && xhttp.status == 200) {
			 var data = xhttp.responseText.split('[BRK]');
			 //alert(data[0]);
			 document.getElementById('list_problem').innerHTML = data[0];
			 }
        };
       xhttp.open("GET", str, true);
       xhttp.send();
}
function searchProblem(){
	var searchproblem = document.getElementById('searchproblem').value;
	var type = document.getElementById('type').value;
	var str = "List_ProblemCategory_Actions.php?actionmode=searchproblem&searchproblem="+searchproblem+"&type="+type;
	var xhttp = new XMLHttpRequest();
         xhttp.onreadystatechange = function() {
         if (xhttp.readyState == 4 && xhttp.status == 200) {
			 var data = xhttp.responseText.split('[BRK]');
			 document.getElementById('list_problem').innerHTML = data[0];
			 }
        };
       xhttp.open("GET", str, true);
       xhttp.send();
}
function clearPage(){
	document.location.href = "List_ProblemCategory.php";
}
function selectProblem(v, v2){
	var frm = document.getElementById('frm').value;
	if(frm == 1){
		opener.document.getElementById('RMADT_PROBLEMCATEGORY').value = v;
		opener.document.getElementById('RMADT_PROBLEMCATEGORY').focus();
		window.close();
	}
	else
		if(frm == 2){
			var rmadt_id = document.getElementById('rmadt_id').value;
			opener.document.getElementById('RMADT_PROBLEMCATEGORY_'+rmadt_id).value = v;
		    opener.document.getElementById('RMADT_PROBLEMCATEGORY_'+rmadt_id).focus();
		    window.close();
		}
		else
			if(frm == 3){
				opener.document.getElementById('rmadt_received').value = v;
				window.close();
			}
			else
			if(frm == 4){
				opener.document.getElementById('Engineer_Name').value = v;
				window.close();
			}
			else
			if(frm == 5){
				opener.document.getElementById('textengineer').value = v;
				opener.document.getElementById('textengineer2').value = v2;
				window.close();
			}
			else
			if(frm == 6){
				opener.document.getElementById('RMADt_PickedUpBy').value = v;
				window.close();
			}
			else
			if(frm == 7){
				var rmadt_id = document.getElementById('rmadt_id').value;
				opener.document.getElementById('RMADt_PickedUpBy_'+rmadt_id).value = v;
				window.close();
			}
}
function fillProblem(ProblemId, ProblemDesc){
	var frm = document.getElementById('frm').value;
	if(frm == 3){
		if(document.getElementById(ProblemId).checked){
		if(opener.document.getElementById('rmadt_received').value != ""){
			opener.document.getElementById('rmadt_received').value = opener.document.getElementById('rmadt_received').value + " , ";
		}
		opener.document.getElementById('rmadt_received').value = opener.document.getElementById('rmadt_received').value + ProblemDesc;
	  }
	  else{
		  var rmadt_notes = opener.document.getElementById('rmadt_received').value;
		  
		  rmadt_notes = rmadt_notes.replace(ProblemDesc, "");
		   rmadt_notes = rmadt_notes.replace(" , ", "");
		  opener.document.getElementById('rmadt_received').value = rmadt_notes;
	  }
	}
	else
		if(frm == 4){
			var rmadt_id = document.getElementById('rmadt_id').value;
		if(document.getElementById(ProblemId).checked){
		if(opener.document.getElementById('rmadt_received_'+rmadt_id).value != ""){
			opener.document.getElementById('rmadt_received_'+rmadt_id).value = opener.document.getElementById('rmadt_received_'+rmadt_id).value + " , ";
		}
		opener.document.getElementById('rmadt_received_'+rmadt_id).value = opener.document.getElementById('rmadt_received_'+rmadt_id).value + ProblemDesc;
	  }
	  else{
		  var rmadt_notes = opener.document.getElementById('rmadt_received_'+rmadt_id).value;
		  
		  rmadt_notes = rmadt_notes.replace(ProblemDesc, "");
		   rmadt_notes = rmadt_notes.replace(" , ", "");
		  opener.document.getElementById('rmadt_received_'+rmadt_id).value = rmadt_notes;
	  }
	}
	else
		if(frm == 1){
		if(document.getElementById(ProblemId).checked){
		if(opener.document.getElementById('RMADT_PROBLEMCATEGORY').value != ""){
			opener.document.getElementById('RMADT_PROBLEMCATEGORY').value = opener.document.getElementById('RMADT_PROBLEMCATEGORY').value + " , ";
		}
		opener.document.getElementById('RMADT_PROBLEMCATEGORY').value = opener.document.getElementById('RMADT_PROBLEMCATEGORY').value + ProblemDesc;
	  }
	  else{
		  var RMADT_PROBLEMCATEGORY = opener.document.getElementById('RMADT_PROBLEMCATEGORY').value;
		  
		  RMADT_PROBLEMCATEGORY = RMADT_PROBLEMCATEGORY.replace(ProblemDesc, "");
		   RMADT_PROBLEMCATEGORY = RMADT_PROBLEMCATEGORY.replace(" , ", "");
		  opener.document.getElementById('RMADT_PROBLEMCATEGORY').value = RMADT_PROBLEMCATEGORY;
	  }
	}
	else
		if(frm == 2){
			var rmadt_id = document.getElementById('rmadt_id').value;
		if(document.getElementById(ProblemId).checked){
		if(opener.document.getElementById('RMADT_PROBLEMCATEGORY_'+rmadt_id).value != ""){
			opener.document.getElementById('RMADT_PROBLEMCATEGORY_'+rmadt_id).value = opener.document.getElementById('RMADT_PROBLEMCATEGORY_'+rmadt_id).value + " , ";
		}
		opener.document.getElementById('RMADT_PROBLEMCATEGORY_'+rmadt_id).value = opener.document.getElementById('RMADT_PROBLEMCATEGORY_'+rmadt_id).value + ProblemDesc;
	  }
	  else{
		  var RMADT_PROBLEMCATEGORY = opener.document.getElementById('RMADT_PROBLEMCATEGORY_'+rmadt_id).value;
		  
		  RMADT_PROBLEMCATEGORY = RMADT_PROBLEMCATEGORY.replace(ProblemDesc, "");
		   RMADT_PROBLEMCATEGORY = RMADT_PROBLEMCATEGORY.replace(" , ", "");
		  opener.document.getElementById('RMADT_PROBLEMCATEGORY_'+rmadt_id).value = RMADT_PROBLEMCATEGORY;
	  }
	}
	
}
function closeWindow(){
	var frm = document.getElementById('frm').value;
	if(frm == 3 ){
	opener.document.getElementById('rmadt_received').focus();
	}
	window.close();
}
</script>
</head>
<body>
HERE;
echo "<table width=700 style='margin: auto'>
<tr>
<th colspan=2 style='font-family: Arial; font-size: 17px; background: #dddddd; color: #333333; text-transform: uppercase; padding-top: 3px; padding-bottom: 3px'>$Label</th>
</tr>
<tr>
<th width=138 style='text-align: left; font-size: 15px; font-family: Arial; color: #999999; background: #dddddd; padding: 6px'>Search for $Label2</th>
<th width=550 style='text-align: left; background: #dddddd; '><input type='text' id='searchproblem' style='width: 350px; padding-top: 3px; padding-bottom: 3px' />&nbsp;&nbsp;<button style='font-family: Arial; font-size: 16px; background: #4069a7; color: #FFFFFF; font-weight: bold; cursor: pointer' onclick=\"searchProblem()\">Search</button>&nbsp;<button style='font-family: Arial; font-size: 16px; background: #4069a7; color: #FFFFFF; font-weight: bold; cursor: pointer' onclick=\"clearPage()\">Clear Page</button></th>
</tr>
<tr>
<th colspan=2 style='text-align: left; font-size: 17px; font-family: Arial; color: #999999; background: #dddddd; padding: 6px'>$Label3</th>
</tr>";
echo "<input type='hidden' id='frm' value='$frm' />";
echo "<input type='hidden' id='rmadt_id' value='$rmadt_id' />";
echo "<input type='hidden' id='type' value='$type' />";
echo"<tr>
<th colspan=2 id='list_problem'>";
drawListProblemCategory($connection, '', '', '', $type);
echo"</th>
</tr>
</table>";
echo "<div style='text-align: center'><button style='font-family: Arial; font-size: 16px; background: #4069a7; color: #FFFFFF; font-weight: bold; cursor: pointer' onclick=\"closeWindow()\">Close X</button></div>";
print <<<HERE
</body>
</html>
HERE;
?>