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

$querycurrency="select Currency_code from currency order by currency_code";
$resultcurrency=mysql_query($querycurrency,$connection) or die(mysql_error());


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


function SubmitRecord() {
	x = document.chargesitems;
	
	var result = CheckFields();
	var vChgId      = x.PChgId.value;
	var vChgItemId  = x.txtChgItemId.value;
	var vMainMode   = x.mainaction.value;
	var vMode       = x.actionmode.value;
	
	if (vChgItemId == '')
		vMode = 'additem';	
	else
		vMode = 'edititem';	
	   
	if (result == 1) {
		x.action="Form_Charges_Actions.php?mode="+vMode+"&mainmode="+vMainMode+"&SChgId="+vChgId+"&SChgItemId="+vChgItemId;
		x.submit();
		
	}	
}

function ChangeLocation() {
	x = document.chargesitems;
	var vMode = x.actionmode.value;
	var vMsg  = x.message.value;
	
	if ((vMode == 'additem' || vMode == 'edititem') && vMsg == "") {
		opener.document.charges.actionmode.value = x.actionmode.value;
		opener.document.charges.mainaction.value = x.mainaction.value;
		opener.document.charges.txtChgId.value   = x.PChgId.value;
		opener.document.charges.submit();
			
		self.close();
	}
}


function CheckFields(){
  
    x = document.chargesitems;
	
	var vmode 	   = x.actionmode.value;
	var vVolume    = x.txtVolume.value;
	var vAmount    = x.txtAmount.value;
	var vPercent   = x.txtPercent.value;
	
	
	if (vAmount == '') vAmount = 0;
	vAmount = parseFloat(vAmount);
		
	if (vVolume == '') vVolume = 0;
	vVolume = parseFloat(vVolume);
	
	if (vPercent == '') vPercent = 0;
	vPercent = parseFloat(vPercent);
	
		
	if ((vAmount*1 != vAmount/1 || vAmount < 0)) {
		alert('Amount should be a NUMERIC and POSITIVE value ');
		x.txtAmount.focus();
		return 0;
	}
	else if ((vVolume*1 != vVolume/1) || (vVolume < 0) || (vVolume > 100)) {
		alert('Volume should be a NUMERIC value and BETWEEN 0 and 100 !');
		x.txtVolume.focus();
		return 0;
	}
	else if ((vPercent*1 != vPercent/1) || (vPercent < 0) || (vPercent > 100)) {
		alert('Percentage should be a NUMERIC value and BETWEEN 0 and 100 !');
		x.txtPercent.focus();
		return 0;
	}

	
	else
	return 1 ;	
}


function kd(e)
{
	x=document.chargesitems;
    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>Charges Items</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;
}
.style12 {
	font-size: 10px;
	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 onLoad='ChangeLocation();' onkeydown='return kd(event)'>
<form name='chargesitems' method='post'>
<input type='hidden' name='mainaction' value='$mainaction'>
<input type='hidden' name='actionmode' value='$actionmode'>
<input type='hidden' name='PChgId' value='$PChgId'>
<input type='hidden' name='PChgItemId' value='$PChgItemId'>
<input type='hidden' name='message' value='$message'>

HERE;



//Edit Charges Detail Mode
if($PChgItemId != ''){
	$query = " Select ChargesPurchases.ChargesPurchases_Id, Purchases.Purchase_Reference, ChargesItems.ChargesItems_ID, ".
			 " PurchasesDt.Item_CodePacked, PurchasesDt.Item_Code, Items.Item_Description, PurchasesDt.Warehouse_Code, ".
			 " ifNull(PurchaseDt_Quantity,0), ChargesItems.ChargesItems_Volume, ChargesItems.Currency_Code, ".
			 " Round(ChargesItems_Amount, ".$DecimalBase."), ChargesItems_AmountB1, ChargesItems_AmountB2, ".
			 " ChargesItems_Perc, Warehouse.Warehouse_Name, ifNull(PurchasesDt.PurchaseDt_Price,0), PurchasesDt.Currency_Code  ".
			 " From Temp_ChargesPurchases As ChargesPurchases ".
			 " Inner Join Purchases on ChargesPurchases.Purchase_Id = Purchases.Purchase_Id ".
			 " Inner Join Temp_ChargesItems As ChargesItems on ChargesItems.ChargesPurchases_Id = ChargesPurchases.ChargesPurchases_Id ".
			 " Inner Join PurchasesDt on ChargesItems.PurchaseDt_Id = PurchasesDt.PurchaseDt_Id ".
			 " Inner Join Items on PurchasesDt.Item_Code = Items.Item_code ".
			 " Inner Join Warehouse on PurchasesDt.Warehouse_Code = Warehouse.Warehouse_Code ".
			 " Where ChargesPurchases.Charges_Id = '".$PChgId."' ".
			 " And ChargesItems.ChargesItems_ID = '".$PChgItemId."' ";
			 
	//echo $query;
	$result=mysql_query($query,$connection) or die(mysql_error());
    $rows=mysql_fetch_array($result);
	
	$txtChgItemId     = $rows[2];
	$txtItemCode      = $rows[4];
	$txtItemDesc      = $rows[5];
	$txtItemWh        = $rows[6];
	$txtWhDesc        = $rows[14];
	$txtItemQty       = $rows[7];
	$txtVolume        = $rows[8];
	$txtCurr          = $rows[9];
	$txtAmount        = $rows[10];
	$txtAmountBase1   = $rows[11];
	$txtAmountBase2   = $rows[12];
	$txtPercent  	  = $rows[13];
	$txtPurPrice  	  = $rows[15];
	$txtPurCurr  	  = $rows[16];
}


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

echo"<table cellpadding='0' cellspacing='0' border='0'  align='center' style='table-layout:fixed' width='560px;'>";
echo"<tr>
		<td id='search-top1' style='height:34px; width:31px;'>&nbsp;</td>
	  	<td id='search-top2' style='height:34px; width:560px' align='center' valign='middle'>
			<table border='0'>
			<tr><td><span class='style3'>Edit Item(s)</span></td></tr>
			</table>
		</td>
		<td id='search-top3' style='height:34px; width:31px;'>&nbsp;</td>
	</tr>";


 echo"<tr  height='25'><td id='search-content1' style='height:34px; width:31px;'>&nbsp;</td>
				<td  align='center' id='search-content2'>
				<button type='button' id='btnSubmit' name='btnSubmit' onclick='SubmitRecord(); return false;' title='Submit Record'><span><span>Submit</span></span></button>
		
		</td><td id='search-content3' style='width:31px;'>&nbsp;</td>
		      </tr>";
			  
echo"<tr><td id='search-content1' style='width:31px;'>&nbsp;</td>
		 <td id='search-content2' >";  	  
echo"   <table border='0' style='table-layout:fixed' align='center' valign='top'>
          <col width=90>
		  <col width=130>
		  <col width=70>
		  <col width=300>";

		  
echo"	 
		   <tr bgcolor='#F7F7F7' height='20'>	
			 <td colspan='4' align='center'><font size='1' color='#FF0000' face='Verdana'><b>".$message."</b></font></td></tr>";
			 
		     
echo"   <tr bgcolor='#F7F7F7' height='25'>
		  <td align='left' ><span class='style12'>Item</span></td><td colspan='3'><b>".$txtItemCode." - ".$txtItemDesc."</b></td>";
echo"   </tr>";	

echo"   <tr bgcolor='#F7F7F7' height='25'><td><span class='style12'>Warehouse </span></td><td colspan='3'>";
echo"       <input type='hidden' name='txtChgItemId' id='txtChgItemId' value='$txtChgItemId'>";
echo"       <input type='hidden' name='txtCurr' id='txtCurr' value='$txtCurr'>";
echo"	  <b>".$txtItemWh." - ".$txtWhDesc."</b></td>";
echo"   </tr>";	

echo"   <tr bgcolor='#F7F7F7' height='25'>
		  <td align='left' ><span class='style12'>Quantity</span></td><td><b>".$txtItemQty."</b></td>
		  <td align='left' ><span class='style12'>Price</span></td><td><b>".number_format($txtPurPrice, $DecimalBase)."  ".$txtPurCurr."</b></td>";
echo"   </tr>";		  
	
echo"   <tr bgcolor='#F7F7F7'><td><span class='style12'>Volume</span></td><td colspan='3'><span class='style12'><label>";
echo"       <input type='text'  name='txtVolume' id='txtVolume' value='$txtVolume' style='width:100px;' maxlength='10' />
			</label></span>
		  </td>";
echo"   </tr>";
			

echo"	  <tr bgcolor='#F7F7F7' height='25'>";
echo"		<td valign='center' align='left' ><span class='style12'>Charges Amt</span></td>";
echo"		<td align='left' colspan='3'><input type='text' name='txtAmount' value='$txtAmount' maxlength='20' style='text-align:right; font-size:13px; width:100px;' >&nbsp;&nbsp;<b>".$txtCurr."</b></td>";
echo"	  </tr>";

echo"	  <tr bgcolor='#F7F7F7' height='25'>";
echo"		<td valign='center' align='left' ><span class='style12'>Charges (%)</span></td>";
echo"		<td align='left' colspan='3'><input type='text' name='txtPercent' value='$txtPercent' maxlength='20' style='text-align:right; font-size:13px; width:100px;' ></td>";
echo"	  </tr>";

 echo "</table></td><td id='search-content3' style='width:31px;'>&nbsp;</td></tr>";

echo"	<tr >
		<td id='search-bottom1' style='height:34px; width:31px;'>&nbsp;</td>
		<td id='search-bottom2' style='height:34px; width:560px;' valign='top'></td>
		<td id='search-bottom3' style='height:34px; width:31px;' valign='top'></td>";
echo" </tr>";
echo"</table>";

	

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

HERE;

?>