<?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'];

print <<<HERE
<html>
<head>
<title>List of Serial Numbers</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 searchserial = document.getElementById('searchserial').value;
	
	var str = "List_SerialNo_Actions.php?actionmode=movenext&searchserial="+searchserial+"&start="+start+"&pagenumber="+pagenumber;
	//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_serial').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 searchserial = document.getElementById('searchserial').value;
	
	var str = "List_SerialNo_Actions.php?actionmode=moveprev&searchserial="+searchserial+"&start="+start+"&pagenumber="+pagenumber;
	//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_serial').innerHTML = data[0];
			 }
        };
       xhttp.open("GET", str, true);
       xhttp.send();
}
function searchSerial(){
	var searchserial = document.getElementById('searchserial').value;
	var str = "List_SerialNo_Actions.php?actionmode=searchserial&searchserial="+searchserial;
	var xhttp = new XMLHttpRequest();
         xhttp.onreadystatechange = function() {
         if (xhttp.readyState == 4 && xhttp.status == 200) {
			 var data = xhttp.responseText.split('[BRK]');
			 document.getElementById('list_serial').innerHTML = data[0];
			 }
        };
       xhttp.open("GET", str, true);
       xhttp.send();
}
function clearPage(){
	document.location.href = "List_SerialNo.php";
}
function selectSerial(v){
	var frm = document.getElementById('frm').value;
	if(frm == 1){
		opener.document.getElementById('Serial_Number').value = v;
		opener.document.getElementById('Serial_Number').focus();
		var str = "List_SerialNo_Actions.php?actionmode=getinvoice&serial_number="+v;
		//alert(str);exit;
	    var xhttp = new XMLHttpRequest();
         xhttp.onreadystatechange = function() {
         if (xhttp.readyState == 4 && xhttp.status == 200) {
			 var data = xhttp.responseText.split('[BRK]');
			// alert(data[0]);exit;
			 opener.document.getElementById('Invoice_Number').value = data[0];
			 opener.document.getElementById('Invoice_Date').value = data[1];
			window.close();
			 }
        };
       xhttp.open("GET", str, true);
       xhttp.send();
		//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){
			var rmadt_id = document.getElementById('rmadt_id').value;
			opener.document.getElementById('Serial_Number_'+rmadt_id).value = v;
		opener.document.getElementById('Serial_Number_'+rmadt_id).focus();
		var str = "List_SerialNo_Actions.php?actionmode=getinvoice&serial_number="+v;
		//alert(str);exit;
	    var xhttp = new XMLHttpRequest();
         xhttp.onreadystatechange = function() {
         if (xhttp.readyState == 4 && xhttp.status == 200) {
			 var data = xhttp.responseText.split('[BRK]');
			// alert(data[0]);exit;
			 opener.document.getElementById('Invoice_Number_'+rmadt_id).value = data[0];
			 opener.document.getElementById('Invoice_Date_'+rmadt_id).value = data[1];
			window.close();
			 }
        };
       xhttp.open("GET", str, true);
       xhttp.send();
		//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'>Serial Numbers</th>
</tr>
<tr>
<th width=138 style='text-align: left; font-size: 15px; font-family: Arial; color: #999999; background: #dddddd; padding: 6px'>Serial Number:</th>
<th width=550 style='text-align: left; background: #dddddd; '><input type='text' id='searchserial' 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=\"searchSerial()\">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>
</table>
<table width=700 style='margin: auto'>
<tr>
<th width=50% style='text-align: left; font-size: 17px; font-family: Arial; color: #999999; background: #dddddd; padding: 6px'>Serial No.</th>
<th width=50%  style='text-align: left; font-size: 17px; font-family: Arial; color: #999999; background: #dddddd; padding: 6px'>Invoice Ref. #</th>
</tr>";
echo "<input type='hidden' id='frm' value='$frm' />";
echo "<input type='hidden' id='rmadt_id' value='$rmadt_id' />";
echo"<tr>
<th colspan=2 id='list_serial'>";
drawListSerialNo($connection);
echo"</th>
</tr>
</table>";
print <<<HERE
</body>
</html>
HERE;
?>