<?php

session_start();
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");


$querylog="SELECT FU_DataTransfer as loginaccess 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");




print <<< HERE
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=windows-1256" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 

<link rel="stylesheet" type="text/css" media="all" href="js/grid/UI_DataGrid_V1.css">
<script type="text/javascript" src="js/grid/UI_DataGrid.js"></script>
<script type="text/javascript" src="js/grid/jquery-1.9.1.js"></script>

<link rel="stylesheet" type="text/css" media="all" href="css-v2/style.css">
<script type="text/javascript" language="javascript">


function TransferData(PAction) {
	var x=document.form1;
	var vTransferOptions = document.getElementsByName('TransferOptions[]');
	var vReceiveArray = ["25","26","27","28"];
	
	var optionValues = [];
	for (var i=0;i < vTransferOptions.length;i++) {
	
		if ((PAction == 'S' && vReceiveArray.indexOf(vTransferOptions[i].value) == -1) || (PAction == 'R' && vReceiveArray.indexOf(vTransferOptions[i].value) > -1)) {
			if (vTransferOptions[i].checked){
				optionValues.push(vTransferOptions[i].value);
			}
		}	
	}
	var vals = optionValues.join(",");
	
	var vSelectedRows = x.SelectedRows.value;
		
	if (vals == "") {
		alert("Please select one or more option(s).");
	} 
	else {
		if (document.getElementById('btnSend')) x.btnSend.disabled = true;
		if (document.getElementById('btnReceive')) x.btnReceive.disabled = true;
			
		x.action =  "Form_DataTransfer_Actions.php?DataOptions="+vals+"&SelectedRows="+vSelectedRows;
		x.submit();
		
		document.getElementById("imgDiv").style.display="block";
	}

}

function ShowData(Option) {
	var xmlhttp = getXMLHttpRequest();	
	var getdate = new Date();  //Used to prevent caching during ajax call
  
	
	document.getElementById("imgDiv").style.display="block";
	
	if(xmlhttp) {
		xmlhttp.onreadystatechange=function()
		{
			if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
				var data = $.parseJSON(xmlhttp.responseText);
				var msg = data[0].message;
					
				if (msg != "") {
					document.getElementById("imgDiv").style.display="none";
					alert(decodeURIComponent(msg));
				} else {
					document.getElementById("imgDiv").style.display="none";
					document.getElementById("DivData").innerHTML=data[0].layout;
					SelectRecord();					
				}
			}
		}
			
		xmlhttp.open("GET","Form_DataTransfer_Actions.php?ActionMode=ShowData&TableOption="+Option);
		xmlhttp.send();
			
	}

}

function CheckIfSelected() {
	var CheckName, vSelectedRows;
	var x, y;
	
	x = document.production;
	
	vSelectedRows = 0;
	for (var i=1; i <= x.ChkCounter.value; i++)
	{
		CheckName='R'+i;
		y = document.getElementById(CheckName);
		
		if (y.checked)
			vSelectedRows = vSelectedRows + 1;
		
	}
	
	return vSelectedRows;
	
}



function SelectRecord() {
	var CheckName, vSelectedRows;
	var x, y;
	
	x = document.form1;
	
	vSelectedRows = '';
	for (var i=1; i <= x.ChkCounter.value; i++)
	{
		CheckName='R'+i;
		y = document.getElementById(CheckName);
		
		if (y.checked==true)
			vSelectedRows = vSelectedRows + ',' + y.value;
		
	}
	
	vSelectedRows = vSelectedRows.substr(1);
	x.SelectedRows.value = vSelectedRows;
}

function ShowHideDetails(TransCode) {
	vSlideDivId = '#displaydetails'+TransCode;
	$(vSlideDivId).slideToggle();
}

function ExportData(Option) {
	x = document.form1;
	var vSelectedRows = x.SelectedRows.value;
	
	str = 'Exp_DataTransfer.php?TableOption='+Option+'&SelectedRows='+vSelectedRows;
	window.open(str);
			   
}

</script>
<title>Transfer Data</title>
<style type="text/css">


.style5 {color: #5151FF; font-size: 10px;}


div#imgDiv {
	font-size: 12px; 
    font-weight:bold; 
	color: #FF0000 ;
	text-align: center; 
	margin-top:10px;
	display:none;
}
</style>

</head>
<body>
<form id='form1' name='form1' method='post' action=''>
<input type='hidden' name='actionmode' value='$actionmode'>
<input type='hidden' name='mainaction' value='$mainaction'>
<input type='hidden' name='message' value='$message' size='200'>
<input type='hidden' name='SelectedRows' value='$SelectedRows'>
HERE;


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'>DATA TRANSFER</span></td>
				<td valign='middle' style='height:26px;'>&nbsp;</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='30'  height='34'>&nbsp;</td>
				<td valign='middle' height='34' width='720px' align='left'><span class='message'>".htmlspecialchars($message, ENT_QUOTES)."</span></td>";
echo"		</tr></table>";
echo"</td></tr>";


echo"<tr><td id='content-body'>
	<table border='0'  align='center' valign='top' width=100% class='tablecontent'>
	<tr bgcolor='#F7F7F7'><td valign='top' align='left' colspan='10'>
		<table border='0' style='table-layout:fixed'>
		  <col width=150>
		  <col width=150>
		  <col width=50>
		  <col width=150>
		  <col width=200>";	


echo" <tr>";	  
echo"   <td height='19' colspan='2' align='left' bgcolor='#F5F5F5' style='border-bottom:2px solid black;'><span class='style1' style='font-size:14px;'>Send Data</span></td>";
echo"   <td height='19' align='left' bgcolor='#F5F5F5'>&nbsp;</td>";
echo"   <td height='19' colspan='2' align='left' bgcolor='#F5F5F5' style='border-bottom:2px solid black;'><span class='style1' style='font-size:14px;'>Receive Data</span></td>";
echo" </tr>";

echo" <tr>";
echo"   <td valign='top' bgcolor='#F5F5F5'><span class='style1'>";
echo"		<label><input type='checkbox' name='TransferOptions[]' value='1'>Currencies</label><br>";
echo"		<label><input type='checkbox' name='TransferOptions[]' value='2'>Divisions</label><br>";
echo"		<label><input type='checkbox' name='TransferOptions[]' value='3'>Price Lists</label><br>";
echo"		<label><input type='checkbox' name='TransferOptions[]' value='4'>Taxes</label><br>";
echo"		<label><input type='checkbox' name='TransferOptions[]' value='5'>Items</label><br>";
echo"		<label><input type='checkbox' name='TransferOptions[]' value='6'>Clients</label><br>";
echo"	</span></td>";
echo"   <td valign='top' bgcolor='#F5F5F5'><span class='style1'>";
echo"		<label><input type='checkbox' name='TransferOptions[]' value='10'>Vehicles</label><br>";
echo"		<label><input type='checkbox' name='TransferOptions[]' value='11'>Users</label><br>";
echo"		<label><input type='checkbox' name='TransferOptions[]' value='12'><span onclick='ShowData(12); return false;' style='cursor:pointer;'>Vehicles Stock</span></label><br>";
echo"	</span></td>";
echo"   <td valign='top' bgcolor='#F5F5F5'><span class='style1'>&nbsp;</span></td>";
echo"   <td valign='top' bgcolor='#F5F5F5'><span class='style1'>";
echo"		<label><input type='checkbox' name='TransferOptions[]' value='25'><span onclick='ShowData(25); return false;' style='cursor:pointer;'>Orders</span></label><br>";
echo"		<label><input type='checkbox' name='TransferOptions[]' value='26'><span onclick='ShowData(26); return false;' style='cursor:pointer;'>Invoices</span></label><br>";
echo"		<label><input type='checkbox' name='TransferOptions[]' value='27'><span onclick='ShowData(27); return false;' style='cursor:pointer;'>Returns</span></label><br>";
echo"	</span></td>";
echo"   <td valign='top' bgcolor='#F5F5F5'><span class='style1'>";
echo"		<label><input type='checkbox' name='TransferOptions[]' value='28'><span onclick='ShowData(28); return false;' style='cursor:pointer;'>Collections</span></label><br>";
echo"		<label><input type='checkbox' name='TransferOptions[]' value='25'><span onclick='ShowData(125); return false;' style='cursor:pointer;'>Mobile Sorders</span></label><br>";
echo"	</span></td>";
echo" </tr>";

echo" <tr height='30'>";	  
echo"   <td colspan='2' align='center' bgcolor='#F5F5F5'>";
echo" 		<button type='button' id='btnSend' name='btnSend' onclick=\"TransferData('S');return false;\" title='Send Data'><span><span>Send</span></span></button>";
echo"	</td>";
echo"   <td valign='top' bgcolor='#F5F5F5'><span class='style1'>&nbsp;</span></td>";
echo"   <td colspan='2' align='center' bgcolor='#F5F5F5'>";
echo" 		<button type='button' id='btnReceive' name='btnReceive' onclick=\"TransferData('R');return false;\" title='Receive Data'><span><span>Receive</span></span></button>";
echo"	</td>";
echo" </tr>";


echo" <tr height='30'>";
echo"   <td colspan='5' align='center' valign='middle' bgcolor='#F5F5F5'>";
echo "		<div id='imgDiv'><p><image src='img/ajax-loader.gif' id='loader-img' /></p></div>";
echo"	</td>";
echo" </tr>";

echo" <tr>";
echo"   <td colspan='5' align='left' valign='top' bgcolor='#F5F5F5'>";
echo "		<div id='DivData' style='height:230px; width:935px; overflow:auto;'></div>";
echo"	</td>";
echo" </tr>";

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



echo" <tr height='30'><td align='center' id='content-bottom' ></td></tr>";  
echo" </table>  ";//end of last table menu



echo"  
</form>
</body>
</html>";

?>