<?php

session_start();
if($HTTP_SESSION_VARS['dbName']==""){
header("location:../index.php");
}


include("config.inc.php");
include("Functions_Purchase.php");


$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
mysql_select_db($HTTP_SESSION_VARS['dbName'],$connection) or die("Could not find DB");

//Read From Login Table
$querylog=" select ifNull(FI_ShowCostPrice,0) As FI_ShowCostPrice ".
          " from ".$HTTP_SESSION_VARS['dbLabel'].".login where user_account='".$HTTP_SESSION_VARS['useraccount']."' and Upper(Login_Schema) = Upper('".$HTTP_SESSION_VARS['dbName']."')  ";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}

	
//Read From Setup Table							
$querysetup= " select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2 from setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}

$querydivtype="Select DivisionTypes_Name From DivisionTypes Where Upper(DivisionTypes_Forms) Like '%AD%' And ifNull(DivisionTypes_Master,0) = 0 Order By DivisionTypes_Name";
$resultdivtype=mysql_query($querydivtype,$connection) or die(mysql_error());




//Group List
$querygroup= " select distinct group_code, group_name ".
			 " from grouping where group_code in (select group_code from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk where user_account='".$HTTP_SESSION_VARS['useraccount']."') ".
			 " order by group_code";
$resultgroup = mysql_query($querygroup,$connection) or die(mysql_error());
$groupexist   = mysql_num_rows($resultgroup);
if ($groupexist == 0)
	$querygroup="select group_code, group_name from grouping order by group_code";
	
$resultgroup=mysql_query($querygroup,$connection) or die(mysql_error());


//Warehouse List
$querywarehouse=" select distinct warehouse.warehouse_code, warehouse_name ".
				" from warehouse where warehouse_code in (select warehouse_code from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk where user_account='".$HTTP_SESSION_VARS['useraccount']."') ".
				" order by warehouse.warehouse_code";
$resultwh = mysql_query($querywarehouse,$connection) or die(mysql_error());
$whexist   = mysql_num_rows($resultwh);
if ($whexist == 0)
	$querywarehouse="select warehouse_code,warehouse_name from warehouse order by warehouse_code";
	
$resultwarehouse=mysql_query($querywarehouse,$connection) or die(mysql_error());


$querydflwh="select default_warehouse_code as defaultwarehouse from ".$HTTP_SESSION_VARS['dbLabel'].".loginstk where user_account='".$HTTP_SESSION_VARS['useraccount']."' and default_warehouse_code != '' ";
$resultdflwh=mysql_query($querydflwh,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultdflwh)){
  extract($rows);
}



print <<<HERE
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="default.css" type="text/css" />
<script type="text/javascript" language="javascript">


function AddDivision(){
	x = document.adjustmentdt;
	
	var vMainMode      = x.mainaction.value;
	var vAdjId    	   = x.PAdjId.value;
	var vAdjDtId  	   = x.PAdjDtId.value;
	var vAdjDtDivId    = x.txtAdjDtDivID.value;
	var vDivisionId    = x.txtDivisionID.value;
	var vDivisionCode  = x.txtDivisionCode.value;
	
	if (vDivisionCode == "") {
		alert("Division code is mandatory.!");
		x.txtDivisionCode.focus();
	}
	else {
		if (vAdjDtDivId == '')	vMode = 'adddivisiondt';	
		else vMode = 'editdivisiondt';
		
		x.action="Form_Adjustment_Actions.php?mode="+vMode+"&mainmode="+vMainMode+"&SAdjId="+vAdjId+"&SAdjDtId="+vAdjDtId+"&SAdjDtDivId="+vAdjDtDivId;
		x.submit();
	
	}
}

function EditDivision(AdjDtDivId, DivisionId, DivisionType, DivisionCode) {
	x = document.adjustmentdt;
	
	x.txtAdjDtDivID.value   = AdjDtDivId;
	x.txtDivisionID.value   = DivisionId;
	x.txtDivisionCode.value = DivisionCode;
	x.txtDivType.value      = DivisionType;
	
	x.submit();
	
}

function DeleteDivision(AdjDtDivId, DivisionType, DivisionCode) {
	x = document.adjustmentdt;
	
	var vMainMode = x.mainaction.value;
	var vAdjId    = x.PAdjId.value;
	var vAdjDtId  = x.PAdjDtId.value;
	var vDelMsg   = "Are you sure you want to Delete "+DivisionType+" ("+DivisionCode+") ?";
	   
	if (confirm ( vDelMsg ))
	{
		x.action="Form_Adjustment_Actions.php?mode=deletedivisiondt&mainmode="+vMainMode+"&SAdjId="+vAdjId+"&SAdjDtId="+vAdjDtId+"&SAdjDtDivId="+AdjDtDivId;
		x.submit();
	}

}

function ClearDivision() {
	x = document.adjustmentdt;
	
	x.txtAdjDtDivID.value = '';
	x.txtDivisionID.value = '';
	x.txtDivisionCode.value = '';
	x.txtDivType.value = '';
	
	x.submit();

}


function showlistDivision(str) {
	x = document.adjustmentdt;
	
	vDivisionType = x.txtDivType.value;
	
	if (vDivisionType == "") {
		alert("Division Type is mandatory.!");
		x.txtDivType.focus();
	} else {
		window.open('List_FrmDivisions.php?frm=5&item='+str+'&divtype='+vDivisionType,'Divisions','toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=no,width=700,height=450,left=100,top=20');
	}
	
}

function kd(e)
{
	x=document.adjustmentdt;
    var intKey = (window.Event) ? e.which : e.keyCode;
    if (intKey == 13) { //enter key
        x.btnSubmitRecord.click();
		return false;
    }
    return true;
}

</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Detail - Add Divisions</title>


<script type="text/javascript" src="dropdown.js"></script>
<link rel="stylesheet" href="default.css" type="text/css" />
<link rel="stylesheet" href="short.css" type="text/css" />
<link rel="stylesheet" href="dropdown.css" type="text/css" />
<style type="text/css">

body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
}
body {
	margin-top: 2px;
	margin-left: 1px;
}
a:link {
	text-decoration: none;
	color: #0000FF;
}
a:visited {
	text-decoration: none;
	color: #0000FF;
}
a:hover {
	text-decoration: none;
	color: #0000FF;
}
a:active {
	text-decoration: none;
	color: #0000FF;
}
.style11 {color: #999999; font-weight: bold; }
.style12 {
	font-size: 9px;
	color: #999999;
	font-weight: bold;
}
.style15 {color: #5151FF; font-size: 10px;}
.style16 {
	font-size: 16px;
	color: #FF0000;
	font-weight: bold;
	font-family: "Times New Roman", Times, serif;
}
.style17 {font-size: 9px}
.style25 {color: #999999; font-weight: bold; font-size: 10px; }
.style26 {color: #FF0000}
.style27 {color: #999999 ;font-size: 10px;}
.style28 {color: #FFFFFF ;font-size: 10px;}

</style>


</head>
<body>
<form name='adjustmentdt' method='post'>
<input type='hidden' name='message' value='$message'>
<input type='hidden' name='mainaction' value='$mainaction'>
<input type='hidden' name='actionmode' value='$actionmode'>
<input type='hidden' name='AdjustType' value='$AdjustType'>
<input type='hidden' name='PAdjId' value='$PAdjId'>
<input type='hidden' name='PAdjDtId' value='$PAdjDtId'>

HERE;


$style= "style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'";

	  
echo"	<table border='0' style='table-layout:fixed'>
		  <col width=20>
		  <col width=20>
		  <col width=100>
		  <col width=130>
		  <col width=250>";

echo"	<tr bgcolor='#F7F7F7' height='25'>
		    <th colspan='5'><span class='style3'>Add Divisions </span></th>
		</tr>
		<tr bgcolor='#F7F7F7' height='25'>	
			<td colspan='5' align='center'><font size='1' color='#FF0000' face='Verdana'><b>".$message."</b></font></td>
		</tr>";
		  
echo"	<tr bgcolor='#F7F7F7'>";
echo"		<td align='left' colspan='2'><span class='style11'>&nbsp;</span></td>";
echo"		<td align='left' colspan='3'> <span class='style11'>
			<span class='style11'><select name='txtDivType' id='txtDivType' style='width:100px;'>";
			echo"<option></option>";
			while($divtype=mysql_fetch_row($resultdivtype)){
				echo"<option ";
				if ($txtDivType==$divtype[0])
					echo " selected ";
				echo" value='$divtype[0]'>$divtype[0] </option>";
			}
echo"		</select></span>";
echo"		<input type='hidden' name='txtAdjDtDivID' id='txtAdjDtDivID' value='$txtAdjDtDivID'> ";
echo"		<input type='hidden' name='txtDivisionID' id='txtDivisionID' value='$txtDivisionID'> ";
echo"		<input type='text' name='txtDivisionCode' id='txtDivisionCode' value='$txtDivisionCode' style='width:150px;' maxlength='15' readonly /></span>
				<label><span class='style11'><img src='img/btnopen.jpg' title=' Select Division ' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\" onClick='showlistDivision(1); '/></span></label>";
echo"		<input type='button' id='btnAddDivision' name='btnAddDivision' value='Submit'  onclick=\"AddDivision();\" $style title='Add Division'>";
echo"		<input type='button' id='btnClearDivision' name='btnClearDivision' value='Clear'  onclick=\"ClearDivision();\" $style title='Clear Division'>";
echo"   	</td>";
echo"	</tr>";
		  
echo"	<tr>";
echo"  		<td bgcolor='#F7F7F7' align='left' colspan='3'><span class='style27'>&nbsp;</span></td>";
echo"  		<td bgcolor='#F7F7F7' align='left'><span class='style27'>Code</span></td>";
echo"  		<td bgcolor='#F7F7F7' align='left'><span class='style27'>Name</span></td>";		  
echo"	</tr>";

		
$sub_query = " Select AdjustmentDtDivisions_Id, AdjustmentDtDivisions.Divisions_Id, DivisionTypes_Name, Divisions_Code, Divisions_Name  ".
			 " From Temp_AdjustmentDtDivisions As AdjustmentDtDivisions ".
			 " Inner Join Divisions on AdjustmentDtDivisions.Divisions_Id = Divisions.Divisions_Id ".
			 " Inner Join DivisionTypes on Divisions.DivisionTypes_Id = DivisionTypes.DivisionTypes_Id ".
			 " Where AdjustmentDtDivisions.AdjustmentDt_Id = '".$PAdjDtId."' ";
$sub_result=mysql_query($sub_query,$connection) or die(mysql_error());	
$sub_totalrows = mysql_num_rows($sub_result);
//echo $sub_query;
	
while($sub_rows=mysql_fetch_array($sub_result)){
	$ct+=1;

	if($ct%2==0)
		$Color="style='background-color:".$grid_color1."'";
	else			
		$Color="style='background-color:".$grid_color2."'";	
						
					
	echo "<tr ".$Color." >";
	echo "	<td align='center' valign='center'><span class='style15'><img src='img/button_edit.png'  onMouseOver=\"style.cursor='hand'\" onclick=\"EditDivision('".$sub_rows[0]."','".$sub_rows[1]."','".htmlentities($sub_rows[2])."','".htmlentities($sub_rows[3])."')\" title='Edit Record'  ></span></td>";
	echo "	<td align='center' valign='center'><span class='style15'><img src='img/c_delete.png'  onMouseOver=\"style.cursor='hand'\" onclick=\"DeleteDivision('".$sub_rows[0]."','".htmlentities($sub_rows[2])."','".htmlentities($sub_rows[3])."')\" title='Delete Record'  ></span></td>";
	echo "	<td align='left'><span class='style15'>".$sub_rows[2]."</span></td>";
	echo "	<td align='left'><span class='style15'>".$sub_rows[3]."</span></td>";
	echo "	<td align='left'><span class='style15'>".$sub_rows[4]."</span></td>";
	echo "</tr>";
									
}
		
echo"</table>";

	

print <<<HERE
</form>
</body>

HERE;

?>