<?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");

$rma_id = $_GET['rma_id'];
$rmadt_id = $_GET['rmadt_id'];

print <<<HERE
<html>
<head>
<title>RMA Items Details</title>
<script>
function openListItems(frm){
	var str = "List_SorItems.php?frm="+frm+"&ItemType=All";
	window.open(str, 'myWindow2', 'toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=880,height=500,top=50');
}
function openListItems2(frm, rmadtrpc_id){
	var str = "List_SorItems.php?frm="+frm+"&ItemType=All&rmadtrpc_id="+rmadtrpc_id;
	window.open(str, 'myWindow2', 'toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=880,height=500,top=50');
}
function openListWarehouse(frm){
	var str = "List_warehouses.php?str="+frm;
	window.open(str, 'myWindow3', 'toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=880,height=500,top=50');
}
function openListWarehouse2(frm, rmadtrpc_id){
	var str = "List_warehouses.php?str="+frm+"&rmadtrpc_id="+rmadtrpc_id;
	window.open(str, 'myWindow3', 'toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=880,height=500,top=50');
}
function moveNextField(e, v){
	if(e.keyCode == 13){
		if(v == 1){
			document.getElementById('item_description').focus();
		}
		else
			if(v == 2){
			document.getElementById('serial_number').focus();
		    }
			else
			if(v == 3){
			document.getElementById('warehouse_code').focus();
		    }
			else
			if(v == 4){
			document.getElementById('rmadtrpc_quantity').focus();
		    }
			else
			if(v == 5){
			document.getElementById('rmadtrpc_price').focus();
		    }
			else
				if(v == 6){
					insertRMADt2();
				}
	}
}
function moveNextField2(e, v, rmadtrpc_id){
	if(e.keyCode == 13){
		if(v == 1){
			document.getElementById('item_description_'+rmadtrpc_id).focus();
		}
		else
			if(v == 2){
			document.getElementById('serial_number_'+rmadtrpc_id).focus();
		    }
			else
			if(v == 3){
			document.getElementById('warehouse_code_'+rmadtrpc_id).focus();
		    }
			else
			if(v == 4){
			document.getElementById('rmadtrpc_quantity_'+rmadtrpc_id).focus();
		    }
			else
			if(v == 5){
			document.getElementById('rmadtrpc_price_'+rmadtrpc_id).focus();
		    }
			else
				if(v == 6){
					updateRMADt2(rmadtrpc_id);
				}
	}
}
function deleteRMADt(rmadtrpc_id){
	var rma_id   = document.getElementById('rma_id').value;
	var rmadt_id = document.getElementById('rmadt_id').value; 
	var str = "ListOfRMAdtRPC_Actions.php?actionmode=deletermadt&rma_id="+rma_id+"&rmadt_id="+rmadt_id+"&rmadtrpc_id="+rmadtrpc_id;
	if(confirm("Are you sure?")){
	var xhttp = new XMLHttpRequest();
	//alert(str);exit;
  xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) { 
		var data = xhttp.responseText.split('[BRK]');
		document.getElementById('list_itemdt').innerHTML = data[0];
		}
  };
  xhttp.open("GET", str, true);
  xhttp.send();
	}
}
function searchItem(e){
	if(e.keyCode == 13){
		var item_code = document.getElementById('item_code').value;
		var rma_id = document.getElementById('rma_id').value;
		var rmadtrpc_quantity = document.getElementById('rmadtrpc_quantity').value;
		var str = "ListOfRMAdtRPC_Actions.php?actionmode=searchitem&item_code="+item_code+"&rma_id="+rma_id+"&rmadtrpc_quantity="+rmadtrpc_quantity;
		var xhttp = new XMLHttpRequest();
	//alert(str);exit;
  xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) { 
		var data = xhttp.responseText.split('[BRK]');
		if(data[0] == 0){
			openListItems(16);
		}
		else{
			document.getElementById('item_code').value = data[1];
			document.getElementById('item_description').value = data[2];
			document.getElementById('rmadtrpc_price').value = data[3];
			document.getElementById('rmadtrpc_totalprice').value = data[4];
		}
	}
  };
  xhttp.open("GET", str, true);
  xhttp.send();
	}
}
function searchItem2(e, rmadtrpc_id){
	if(e.keyCode == 13){
		var item_code = document.getElementById('item_code_'+rmadtrpc_id).value;
		var rma_id = document.getElementById('rma_id').value;
		var rmadtrpc_quantity = document.getElementById('rmadtrpc_quantity_'+rmadtrpc_id).value;
		var str = "ListOfRMAdtRPC_Actions.php?actionmode=searchitem&item_code="+item_code+"&rma_id="+rma_id+"&rmadtrpc_quantity="+rmadtrpc_quantity;
		var xhttp = new XMLHttpRequest();
	//alert(str);exit;
  xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) { 
		var data = xhttp.responseText.split('[BRK]');
		if(data[0] == 0){
			openListItems2(17, rmadtrpc_id);
		}
		else{
			document.getElementById('item_code_'+rmadtrpc_id).value = data[1];
			document.getElementById('item_description_'+rmadtrpc_id).value = data[2];
			document.getElementById('rmadtrpc_price_'+rmadtrpc_id).value = data[3];
			document.getElementById('rmadtrpc_totalprice_'+rmadtrpc_id).value = data[4];
		}
	}
  };
  xhttp.open("GET", str, true);
  xhttp.send();
	}
}
function searchWarehouse(e){
	if(e.keyCode == 13){
		var warehouse_code = document.getElementById('warehouse_code').value;
		var xhttp = new XMLHttpRequest();
	//alert(str);exit;
	var str = "ListOfRMAdtRPC_Actions.php?actionmode=searchwarehouse&warehouse_code="+warehouse_code;
  xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) { 
		var data = xhttp.responseText.split('[BRK]');
		if(data[0] == 1){
			document.getElementById('warehouse_code').value = data[1];
		}
		else{
			openListWarehouse(12);
		}
		}
  };
  xhttp.open("GET", str, true);
  xhttp.send();
	}
}
function searchWarehouse2(e, rmadtrpc_id){
	if(e.keyCode == 13){
		var warehouse_code = document.getElementById('warehouse_code_'+rmadtrpc_id).value;
		var xhttp = new XMLHttpRequest();
	//alert(str);exit;
	var str = "ListOfRMAdtRPC_Actions.php?actionmode=searchwarehouse&warehouse_code="+warehouse_code;
  xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) { 
		var data = xhttp.responseText.split('[BRK]');
		if(data[0] == 1){
			document.getElementById('warehouse_code_'+rmadtrpc_id).value = data[1];
		}
		else{
			openListWarehouse2(13, rmadtrpc_id);
		}
		}
  };
  xhttp.open("GET", str, true);
  xhttp.send();
	}
}
function insertRMADt2(){
	var rma_id            = document.getElementById('rma_id').value;
	var rmadt_id          = document.getElementById('rmadt_id').value;
	var item_code         = document.getElementById('item_code').value;
	var serial_number     = document.getElementById('serial_number').value;
	var warehouse_code    = document.getElementById('warehouse_code').value;
	var rmadtrpc_quantity = document.getElementById('rmadtrpc_quantity').value;
	var rmadtrpc_price    = document.getElementById('rmadtrpc_price').value;
	var str = "ListOfRMAdtRPC_Actions.php?actionmode=insertrmadt2&rma_id="+rma_id+"&rmadt_id="+rmadt_id+"&item_code="+item_code+"&serial_number="+serial_number+"&warehouse_code="+warehouse_code+"&rmadtrpc_quantity="+rmadtrpc_quantity+"&rmadtrpc_price="+rmadtrpc_price;
	if(item_code == ""){
		alert("Item is required!");
	}
	else
		if(warehouse_code == ""){
			alert("Warehouse is required!");
		}
		else
			if(rmadtrpc_quantity == ""){
				alert("Quantity is required!");
			}
			else
				if(rmadtrpc_price == "rmadtrpc_price"){
					alert("Price is required1");
				}
				else{
	var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) { 
		var data = xhttp.responseText.split('[BRK]');
		document.getElementById('list_itemdt').innerHTML = data[0];
	}
  };
  xhttp.open("GET", str, true);
  xhttp.send();
				}
		
}
function updateRMADt2(rmadtrpc_id){
	var rma_id            = document.getElementById('rma_id').value;
	var rmadt_id          = document.getElementById('rmadt_id').value;
	var item_code         = document.getElementById('item_code_'+rmadtrpc_id).value;
	var serial_number     = document.getElementById('serial_number_'+rmadtrpc_id).value;
	var warehouse_code    = document.getElementById('warehouse_code_'+rmadtrpc_id).value;
	var rmadtrpc_quantity = document.getElementById('rmadtrpc_quantity_'+rmadtrpc_id).value;
	var rmadtrpc_price    = document.getElementById('rmadtrpc_price_'+rmadtrpc_id).value;
	var str = "ListOfRMAdtRPC_Actions.php?actionmode=updatermadt2&rma_id="+rma_id+"&rmadt_id="+rmadt_id+"&item_code="+item_code+"&serial_number="+serial_number+"&warehouse_code="+warehouse_code+"&rmadtrpc_quantity="+rmadtrpc_quantity+"&rmadtrpc_price="+rmadtrpc_price+"&rmadtrpc_id="+rmadtrpc_id;
	if(item_code == ""){
		alert("Item is required!");
	}
	else
		if(warehouse_code == ""){
			alert("Warehouse is required!");
		}
		else
			if(rmadtrpc_quantity == ""){
				alert("Quantity is required!");
			}
			else
				if(rmadtrpc_price == "rmadtrpc_price"){
					alert("Price is required1");
				}
				else{
	var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) { 
		var data = xhttp.responseText.split('[BRK]');
		document.getElementById('list_itemdt').innerHTML = data[0];
		document.getElementById('item_code').focus();
	}
  };
  xhttp.open("GET", str, true);
  xhttp.send();
				}
}
</script>
</head>
<body>
HERE;
echo "<input type='hidden' id='rma_id' value='$rma_id' />";
echo "<input type='hidden' id='rmadt_id' value='$rmadt_id' />";
$query_item  = "select temp_rmadt.item_code, items.item_description 
                from temp_rmadt inner join items on temp_rmadt.item_code = items.item_code
			    where rmadt_id = '$rmadt_id'";
$result_item = mysql_query($query_item, $connection);
$row_item    = mysql_fetch_array($result_item);
extract($row_item);
echo "<table width=1090>
<tr>
<th style='font-family: Arial; padding: 5px; background: #016a84; color: #ffffff;'>RMA Items Details <br>[Item Code: $item_code ; Item Name: $item_description]</th>
</tr>
<tr>
<th>";
echo"<div id='list_itemdt'>";
drawRMADtRPC($connection, $rma_id, $rmadt_id);
echo"</div>";
echo"</th>
</tr>
</table>";
print <<<HERE
</body>
</html>
HERE;
?>