<?php

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

include("config.inc.php");
include("functions_Items.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);
}


$actionmode = $_POST["actionmode"]; if ($actionmode == ""){$actionmode = $_GET["actionmode"];}   
$mainaction = $_POST["mainaction"]; if ($mainaction == ""){$mainaction = $_GET["mainaction"];}   


$querysetup= " select ifnull(Project_FromItems,0) as Project_FromItems from setup ";

$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}
if (empty($mainaction))	$mainaction = 'insert';

$message = ereg_replace("'"," ",$message);

print <<<HERE
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<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="css-v2/style.css">

<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 type="text/css" href="js/grid/scripts/jquery-ui.css" rel="stylesheet">
<script type="text/javascript" src="js/grid/scripts/jquery-ui.min.js"></script>

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

function addField(){
	var itemcol = document.getElementById('itemcol').value;
	var chk_seq = 0;
	if(document.getElementById('chk_seq').checked)
		chk_seq = 1;
	
	if( itemcol == ''){
		alert('Please Select a Field First!');
		return 0;
	}
	else {
		
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttp.onreadystatechange=function()
		{
			if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{ 	
				var data = xmlhttp.responseText.split('[BRK]');
				document.getElementById('showCol').innerHTML=data[1];
				document.getElementById('addFieldDiv').innerHTML=data[2];	
			}
		}
		xmlhttp.open("GET","Form_LoadFromExcel_Action.php?mode=addColumn&itemcol="+itemcol+"&chk_seq="+chk_seq,true);
		xmlhttp.send();
		
	}
}

function removeCol(index){
	var chk_seq = 0;
	if(document.getElementById('chk_seq').checked)
		chk_seq = 1;
	
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{ 	
			var data = xmlhttp.responseText.split('[BRK]');
			document.getElementById('showCol').innerHTML=data[1];
			document.getElementById('addFieldDiv').innerHTML=data[2];	
		}
	}
	xmlhttp.open("GET","Form_LoadFromExcel_Action.php?mode=removeCol&itemRemove="+index+"&chk_seq="+chk_seq,true);
	xmlhttp.send();
}

function ClearPage(){
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{ 	
			var data = xmlhttp.responseText.split('[BRK]');
			document.getElementById('showCol').innerHTML=data[1];
			document.getElementById('addFieldDiv').innerHTML=data[2];
			document.loadExcel.message.value='';	
			document.loadExcel.action = "Form_LoadFromExcel.php";
			document.loadExcel.submit();
		}
	}
	xmlhttp.open("GET","Form_LoadFromExcel_Action.php?mode=clearPage",true);
	xmlhttp.send();
}

function ImportExcel(){
	if(document.getElementById('colnumber').value == 0 || document.getElementById('colnumber').value == ''){
		alert('No Fields for the Columns were Selected!');
		return 0;
	} else {
		if(document.getElementById('Project_FromItems').value==1){
			if(confirm('Create Project For Item?')) var vproject = 1;
			else  var vproject = 0;
		} else var vproject = 1;
		document.loadExcel.action = "Form_LoadFromExcel_Action.php?mode=ImportExcel&vproject="+vproject;
		document.loadExcel.submit();
	}
}

function goBack(){
	window.open("Form_items.php", '_self');
}
function setSeq(){
	if(document.getElementById("chk_seq").checked)
		document.getElementById("chk_seq").value=1;
	else document.getElementById("chk_seq").value=0;
}
</script>

<title>Import From Excel</title>

<style>
#addButton {
	color: black;
    font-size: 16px;
    background-color: darkgray;
    width: 30px;
    height: 20px;
    border-radius: 10px;
}

.colStyle {
	font-size: 15px;
    font-weight: bold;
    font-family: monospace;
	cursor: pointer;
}
</style>
</head>
<body >
<form name='loadExcel' method='post' action=''   enctype='multipart/form-data'>
<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='Project_FromItems' id='Project_FromItems' value='$Project_FromItems' size='200'>

HERE;

//$message='';

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'><input type='hidden' id='txtSaDiscount_Temp'  name='txtSaDiscount_Temp' value='$txtSaDiscount_Temp'>
			<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'>Import From Excel</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='30'  height='34'>&nbsp;</td>
				<td valign='middle' height='34' width='720px' align='left'>";

echo" <button type='button' id='btnClear' name='btnClear' onclick='ClearPage();return false;' title='Clear Page'><span><span>Clear Page</span></span></button>";
echo" <button type='button' id='btnBack' name='btnBack' onclick='goBack();return false;' title='Back'><span><span>Back</span></span></button>";
	

echo"	</td></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'>";	
   
print <<<HERE

		<table border="0" style='table-layout:fixed'>
		  <col width=216>
		  <col width=216>
		  <col width=216>
		  <col width=216>
		  <col width=216>
		

HERE;

echo "<tr height='30px'><td colspan='5'>";
echo"<table border=0><tr><td>
		<button type='button' id='btnImp' name='btnImp' onclick=\"ImportExcel();return false;\" title='Import From Excel'><span><span>Import from Excel</span></span></button>
		<input type='file'  name='image'    id='image' style='width:200px;' accept='file/xls'>
		</td>
	</tr></table>";
echo "</td></tr>";
if($chk_seq == '') $chk_seq = 0;
//$_SESSION['Cols'] = '';
echo "<tr height='30px'><td colspan='5'><div id='addFieldDiv'>";
addColumns($chk_seq);
echo " </div></td></tr>";

echo "<tr><td colspan='5'><div id='showCol'>";

showColumns($tempCol, $removedCol);
echo "</div></td></tr>";		
echo"   </table></td></tr>"; 
 echo"   </table></td></tr><tr height='30'><td align='center' id='content-bottom' ></td></tr>";  
echo"   </table></td></tr>"; 

echo" </table>  ";//end of last table menu
	
print <<<HERE
</form>
</body>

HERE;

?>