<style type="text/css">
.body_lightbox {
width:100%;
background-color:#e7e7e7;
position:absolute;
left:280px;
top:95px;
padding-left:5px;
padding-right:5px;
border:#666666 solid 3px;
}
</style>
<div class="body_lightbox" id="lightbox2" style="overflow:auto; width:600px; height:250px; display:none;"></div>
 <?php session_name('CAKEPHP'); session_start();
$PageSecurity = 4;
$PageName = 'Purchase Requisition';
include('includes/DefinePOClass.php');
include('includes/SQL_CommonFunctions.inc');
include('includes/DefineSuppTransClass.php');
include('includes/Fun-ChkOpeningStock.php');

$title = _('Purchase Requisition');
if($_REQUEST['RequisitionNo']!='' && $_REQUEST['Delete']=='1')
{
	$delete_sql1="Delete from materialrequisitions where requisitionno='".$_REQUEST['RequisitionNo']."'";
	$delete_res1=DB_query($delete_sql1,$db);
	$delete_sql2="Delete from requisitionorderdetails where requsitionno='".$_REQUEST['RequisitionNo']."'";
	$delete_res2=DB_query($delete_sql2,$db);
	$delete_sql3="Delete from comprative_detail where RequisitionNo='".$_REQUEST['RequisitionNo']."'";
	$delete_res3=DB_query($delete_sql3,$db);
	$_SESSION['success_msg']="Delete Successfully";	 
	echo "<script>location.href='index.php?route=POModule/MR_SelectOSMaterialRequi&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
}
				if($_REQUEST['Save'])
				{
					  $fstatus=true;
					  if($_REQUEST['site']=='000')
					  {
						  $msgErr[]="Please Select Branch Name"	;
						  $fstatus=false;
					  }
				  
				}
				
if($_REQUEST['Save']!='' and $fstatus)
{
    
	$year=$_SESSION['financialYearStart'];
	$year_after=$_SESSION['financialYearEnd'];
	$str=substr($_REQUEST['site'],0,3);
	$prefix_sql=DB_query("Select * from prefix_tbl where prefix_name='".$PageName."'",$db);
	$prefix_row=DB_fetch_array($prefix_sql);
	$prefix=$prefix_row['prefix_code'];
	$x=substr($prefix,6,9);
	$sql="Select Reqno from materialrequisitions where sitecode='".$_REQUEST['site']."' order by requisitionno desc limit 0,1";
	$result=DB_query($sql,$db);
	$myrow=DB_fetch_array($result);
	if(DB_num_rows($result)>0)
	{
	$Reqno=$myrow['Reqno'];
	$req_part1=substr($Reqno,-4);
	//echo $req_part1;
	$req_final=$req_part1+1;
	//echo "<br>".$req_final;
	}
	else
	{
	$req_final="1";
	}
	$string=$str."/".$x."/".$year.$year_after."/";
	$final_string=$string.str_pad($req_final,6,"0",STR_PAD_LEFT);
	//echo $final_string; exit;
	$requisition_sql="Insert into materialrequisitions set
	requisitionno='',
	comments='".$_REQUEST['remarks']."',
	prdate='".FormatDateForSQL($_REQUEST['date'])."',
	initiator='".$_SESSION['UserID']."',
	sitecode='".$_REQUEST['site']."',
	Reqno='".$final_string."' ";
	$requisition_res=DB_query($requisition_sql,$db);
	$last_id=DB_Last_Insert_ID($db,'materialrequisitions','id');
	
if($last_id!='')
{
	for($cr=0;$cr<count($_REQUEST['mst_itemid']);$cr++)
	{
	if($_REQUEST['mst_itemid'][$cr]!='')
	{
	$product_description = htmlspecialchars($_REQUEST['item_name'][$cr], ENT_QUOTES);
	$item_code = htmlspecialchars($_REQUEST['item_code'][$cr], ENT_QUOTES);
	$material_details_sql="insert into requisitionorderdetails set
	id='',											
	requsitionno='".$last_id."',
	mst_itemid='".$_REQUEST['mst_itemid'][$cr]."' ,
	product_id='".$item_code."',
	product_description='".$product_description."',
	make='".$_REQUEST['make'][$cr]."',    
	color='".$_REQUEST['color'][$cr]."',   
	grade='".$_REQUEST['grade'][$cr]."',
	size='".$_REQUEST['size'][$cr]."',
	req_quantity='".$_REQUEST['required_qty'][$cr]."',
	required_qty='".$_REQUEST['required_qty'][$cr]."',
	requisition_qty='".$_REQUEST['requisition_qty'][$cr]."',
	material_required_by='".FormatDateForSQL($_REQUEST['material_required_by'][$cr])."',
	Remarks='".$_REQUEST['remark'][$cr]."',
	stock_qty='".$_REQUEST['stock_qty'][$cr]."',
	item_no='".$_REQUEST['item_no'][$cr]."',
	status='Active'"; 
	$details_res=DB_query($material_details_sql,$db);
	}
	}
}

$_SESSION['success_msg']="Saved Successfully";	
echo "<script>location.href='index.php?route=POModule/MR_SelectOSMaterialRequi&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
}
?>
<script language="javascript" type="text/javascript" src="datetimepicker.js"></script>
<script language="javascript" type="text/javascript">
function CalrequiredQty(val,i)
{
 var ctr1=i.split("_");
 //alert(ctr1[0]);
 if(ctrl[0]==undefined)
 {
var a=	document.getElementById("stock_qty").value;
var res = val-a;
document.getElementById("required_qty").value=res.toFixed(3);
 }
 else
 {
var a=	document.getElementById("stock_qty_"+ctr1[1]).value;
var res = val-a;
document.getElementById("required_qty_"+ctr1[1]).value=res.toFixed(3);
 }
 

 
}
function open_items()
{
	window.open("requisition_items.php?action=add&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>","item_popup_window","height=600,width=500,toolbar=no,menubar=no,dependent=yes,resizable =yes, scrollbars=yes");
}
function generate_reqno(str)
{
	if (str.length==0)
  {
  document.getElementById("reqno").innerHTML="";
  return;
  }
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)
    {
    document.getElementById("reqno").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","get_reqno.php?site="+str,true);
xmlhttp.send();
}
function check_approveqty(str,j)
{
	var req_qty=document.getElementById("req_qty_"+j).value;
	if(str>req_qty)
	{
		alert("Quantity to approve cannot be greater than requsition quantity");
		document.getElementById("app_qty_"+j).value='';
		document.getElementById("app_qty_"+j).focus();
	}
	
}
</script>
<!--<a href="MR_SelectOSMaterialRequi.php">Back>></a>-->
<?php if($_REQUEST['RequisitionNo']=='' && $_REQUEST['act']=='')
{ ?>
<table width="70%">	
<?php if(count($msgErr)>0){?>
   <tr>
   <td colspan="4"><?php
   for($i=0;$i<count($msgErr);$i++)
   {
     echo "<div class='error'>".$msgErr[$i].'</div>';
   }
    ?></td>
   </tr>
   <?php } ?>	
	</table>	
<h2 align="center"><strong>Purchase Requisition    </strong></h2>
<h3 align="center"><font color="#FF0000">Please enter the requisition details first</font></h3>
<form name="RequisitionDetailsform" action="" method="post" onsubmit="return formvalidate()">
<div style="width:100%">
<h3 align="center"><strong>Requisition Details</strong></h3>
<table width="100%" border="1" cellspacing="0" cellpadding="3">
  <tr>
    <th style="width:150px;"><strong>REQUISITION DATE</strong></th>
    <td  style="width:250px;"><input type="text" name="date" id="date" class="datepicker" readonly="true" value="<?php if(isset($_SESSION['date']))echo $_SESSION['date']; else echo date('d/m/Y');?>" onchange="checkReqDate()" style="width:100%"></td>
    <th rowspan="3"  style="width:150px"><strong>REMARKS</strong></th>
    <td rowspan="3"><textarea name="remarks" style="width:100%; height:100px;"><?php echo $_SESSION['remarks'];?></textarea></td>
  </tr>
  <tr>
    <th><strong>SITE</strong></th>
    <td>
<?php if($_SESSION['AssignedSites']!='0')
	  {	  	
 $ss="Select loccode,locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in (".$_SESSION['AssignedSites'].")";
	  	 $site_sql=DB_query($ss,$db);
	  }
	  else
	  {
		$ss="Select loccode,locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."'";
		$site_sql=DB_query($ss,$db);
	  }
	?>
<select name="site" id="site" style="width:100%"><!-- onchange="generate_reqno(this.value)"-->
      <option value="000">---Select---</option>
<?php while($site_row=DB_fetch_array($site_sql)){?>
      <option value="<?php echo $site_row['loccode'];?>"<?php echo ($_SESSION['site']==$site_row['loccode']?'selected':'')?>><?php echo $site_row['locationname'];?></option>
<?php } ?>
</select></td>
  </tr>
  <tr>
    <th><strong>REQUISITION NUMBER </strong></th>
    <td><div id="reqno">
        <input name="requisition_no" type="text" id="requisition_no" value="Auto generated after saving"  style="width:100%" readonly="true" />
      </div></td>
  </tr>
</table>
</div>
<p align="left" style="padding-left:10px;">
 <input type="button" name="Add" value="Add"  onclick="addRow('dataTable')"/>
    <input type="button" class="search"   onclick="deleteRowOrder('dataTable')" value="Delete" />                  
  </p>

<!--<input type="submit" name="item_popup" value="Add Items" id="item_popup"  onclick="open_items();" />
 <input type="submit" name="DeleteRow" value="Delete" onclick="confirm('Are you sure you want to delete?')"/></p> -->
  <div style="overflow:auto; width:950px; height:250px">    
      <h3 align="left"><strong>Item Details</strong></h3>
      <table width="100%" border="1" cellpadding="3" cellspacing="0">
         <tr>
	     <th></th>
         <th width="6%" align="center">ITEM CODE </th>
		 <th></th>
         <th width="8%" align="center">ITEM NAME </th>
         <th width="13%" align="center">MAKE</th>
	     <th width="8%" align="center">GRADE</th>
	     <th width="8%" align="center">COLOR</th>
		 <th width="8%" align="center">SIZE</th>
	     <th width="6%" align="center">UNIT</th>
		 <th width="6%" align="center">Item No</th>
         <th width="8%" align="center">Current Stock </th>
         <th width="10%" align="center">REQUISITION QUANTITY </th>
         <th width="10%" align="center">REQUIRED QUANTITY </th>
         <th width="8%" align="center">MATERIAL REQUIRED DATE </th>
         <th width="20%" align="center">REMARKS</th>
      </tr>
       <tbody id="dataTable">
      <tr bgcolor="#FFFFFF">
    <td><input name="delitem[]" type="checkbox"  id="delitem" /></td>
<td><input type="text" name="item_code[]" id="itemcode" value="" onkeyup="opendata(this.value,this.id)" /> </td>
<td><input type="hidden" name="mst_itemid[]" id="mst_itemid" value="" /></td>
<td><input type="text" name="item_name[]" id="item_name" /> </td>
<td><select  name="Make[<?php echo $i;?>]" id="Make_" style="width:100px">
	<option value="0">Select Grade</option>
	<?php
	$querymake=DB_query("select * from product_manufacture where 1=1" ,$db);
			
	while($que_row=DB_fetch_array($querymake))
	{ 
 	?>
    <option value="<?php echo $que_row['id'];?>"><?php echo  $que_row['manufacturer'];?></option>
    <?php  }?>
	  </select></td>
	  <td><select  name="Grade[<?php echo $i;?>]" id="Grade" style="width:100px">
		<option value="0">Select Grade</option>
      <?php
			 $querymake=DB_query("select * from product_grade where 1=1" ,$db);
			while($que_row=DB_fetch_array($querymake))
			{ 
 		 ?>
      <option value="<?php echo $que_row['id'];?>" <?php echo ($get_row['grade']==$que_row['id'] ?'selected':'')?>><?php echo $que_row['grade'];?></option>
      <?php  }?>
	  </select>
	  </td>
<td><select name="Color[]" style="width:100%">
			<option value="0">Select Color</option>
		<?php
			 $querycolor=DB_query("select * from product_color where 1=1" ,$db);
			while($que_row=DB_fetch_array($querycolor))
			{ 
 		 ?>
      <option value="<?php echo $que_row['id'];?>" ><?php echo  $que_row['color'];?></option>
      <?php  }?></select></td>
<td><select name="size[]" id="size_<?php echo $i;?>" style="width:100%">
		  	<option value="0">Select Size</option>
		    <?php 
		
			$sizeId=DB_query("Select * from size_master where 1",$db);
			while($sizeRow=DB_fetch_array($sizeId))
			{
		?>
		    <option value="<?php echo $sizeRow['id'];?>"><?php echo $sizeRow['name'];?></option>
		    <?php } ?>
		    </select> </td>
<td><input type="text" name="Unit[]" id="Unit" style="width:100%" readonly /></td>
 <td><input type="text" name="item_no[]" id="itemno"   /></td>
   <td><input type="text" name="stock_qty[]" id="stockqty"  style="width:100%" readonly /></td>
  <td align="center"><input name="requisition_qty[]" type="text" id="requisitionqty" style="width:100%" size="14" onblur="CalrequiredQty(this.value,this.id)" /></td>
  <td align="center"><input name="required_qty[]" type="text" id="requiredqty" style="width:100%"/></td>
        <td align="center"><input name="material_required_by[]" id="materialrequiredby" type="text"    
	 onClick="javascript:NewCal(this.id,'ddmmyyyy')" value="<?php  echo date('d/m/Y'); ?>" /></td>
        <td align="center"><textarea name="textarea" cols="10" rows="2" id="textarea"></textarea>
        </td>
      </tr>
   </tbody>
      
      
    </table>
     
  </div>

<input type="hidden" name="hiditemcount" id="hiditemcount" value="" />
  <input type="hidden" name="" value="" id="edit_cnt" />

<p align="center"><input type="submit" name="Save" value="Save" onclick="return validatedata()"><input type="button" name="back" value="Back">
<input type="submit" name="cancel" value="Reset" />	
</p>
</form>


<?php }

if($_REQUEST['RequisitionNo']!='' && $_REQUEST['Approve']=='1' && $_REQUEST['act']=='')
{
 
if(isset($_REQUEST['dataapprove']))
{
  $update_sqlparent="update materialrequisitions set 
 								grand_total='".$_REQUEST['grand_total']."',
			 					Approved_remarks='".$_REQUEST['remarks']."',
								Approved_by='".$_SESSION['UserID']."'
			 					where requisitionno='".$_REQUEST['reqid']."'";
								//echo "<pre>";
								//print_r($update_sqlparent); exit;
								$update_resultparent=DB_query($update_sqlparent,$db);

 $f=0; //print_r($_REQUEST['count_req']);exit;
 	for($CTR=1;$CTR<=$_REQUEST['count_req'];$CTR++)
	{
		// echo FormatDateForSQL($_REQUEST['material_required_by'][$CTR]); exit;
		if($_REQUEST['app_qty'][$CTR]!='')//status='".$_REQUEST['MRstatus'][$CTR]."'
		{
				   $update_sql="update requisitionorderdetails set 
								material_required_by='".FormatDateForSQL($_REQUEST['material_required_by'][$CTR])."',
								App_quantity='".$_REQUEST['app_qty'][$CTR]."',
								last_po_rate='".$_REQUEST['last_po_rate'][$CTR]."',
								sub_total='".$_REQUEST['sub_total'][$CTR]."',								 
								approved_date='".FormatDateForSQL(date('d/m/Y'))."',
								App_quantity1='".$_REQUEST['app_qty1'][$CTR]."',
								Remarks='".$_REQUEST['remark'][$CTR]."'
								where id='".$_REQUEST['row_id'][$CTR]."'";
 		  $update_result=DB_query($update_sql,$db);
		  $f=1;
		}	 
	}
	if($f==1){
	 	$dataArray['requisitionno']=$_REQUEST['RequisitionNo'];
			$dataArray['status']=1;
			$cond="requisitionno='".$_REQUEST['RequisitionNo']."'";
			$s->RecordEdit('materialrequisitions', $dataArray, $cond);
			}	 
 $_SESSION['success_msg']="Approved Successfully";	
echo "<script>location.href='index.php?route=POModule/MR_SelectOSMaterialRequi&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";  
}

$req_sql=DB_query("Select Reqno from materialrequisitions where requisitionno='".$_REQUEST['RequisitionNo']."'",$db);
$req=DB_fetch_array($req_sql);
?>

<form action="" method="post">
<br>
<br>
<br>
<h2 align="center"><strong>Approve Purchase Requisition </strong></h2>
 
<input type="hidden" name="reqid" value="<?php echo $_REQUEST['RequisitionNo'];?>" />
  <div style="overflow:auto; width:950px; height:250px">
<table width="100%" border="0" cellspacing="1" cellpadding="2">
  <tr>
    <th><strong>ITEM CODE </strong></th>
    <th><strong>ITEM NAME </strong></th>
    <th><strong>MAKE</strong></th>
    <th><strong>GRADE</strong></th>
    <th><strong>COLOR</strong></th>
    <th><strong>UNIT</strong></th>
    <th><strong>TOTAL STOCK </strong></th>
    <th><strong>REQUISITION QUANTITY </strong></th>
	<th><strong>MATERIAL REQUIRED DATE </strong></th>
	<th><strong>QUANTITY TO APPROVE</strong></th>
	<th><strong>LAST PO RATE</strong></th>
	<th><strong>SUB TOTAL</strong></th>
    <th><strong>REMARKS</strong></th>	
	<!--<th><strong>STATUS</strong></th>-->
  </tr>
		<?php 
	   $get_sql="Select * from requisitionorderdetails where requsitionno='".$_REQUEST['RequisitionNo']."'  ";
		$get_result=DB_query($get_sql,$db);
		$i=0;
		while($get_row=DB_fetch_array($get_result))
		{
			$i++;
			//echo "<pre>";
			//print_r($get_row);
			
		if($i%2==0)
		{$bg="#CCCCCC";}
		else
		$bg="#C8BECB";
		
		?>
	  <tr bgcolor="<?php echo $bg;?>">
  	  <td><?php //echo $get_sql?>
	  <input type="hidden" name="row_id[<?php echo $i;?>]" value="<?php echo $get_row['id'];?>" />
	  <?php echo $get_row['product_id'];?><input type="hidden" name="item_code[<?php echo $i;?>]" id="item_code_<?php echo $i;?>" value="<?php echo $get_row['product_id'];?>">
	  
	  <input type="hidden" name="mst_itemid[<?php echo $i;?>]" id="mst_itemid_<?php echo $i;?>" value="<?php echo $get_row['mst_itemid'];?>">
	  </td>      
    <td><?php echo $get_row['product_description'];?><input type="hidden" name="item_name[<?php echo $i;?>]" id="item_name_<?php echo $i;?>" value="<?php echo $get_row['product_description'];?>"></td>
    <td> 
	
	
	<select  name="Make[<?php echo $i;?>]" id="Make_" style="width:100px">
		<option value="0">Select Make</option>
      <?php
			 $querymake=DB_query("select * from product_manufacture where 1=1" ,$db);
			while($que_row=DB_fetch_array($querymake))
			{ 
 		 ?>
      <option value="<?php echo $que_row['id'];?>" <?php echo ($get_row['make']==$que_row['id'] ?'selected':'')?>><?php echo $que_row['manufacturer'];?></option>
      <?php  }?>
	  </select>

  </td>
    <td> 

	<select  name="Grade[<?php echo $i;?>]" id="Grade_" style="width:100px">
		<option value="0">Select Grade</option>
      <?php
			 $querymake=DB_query("select * from product_grade where 1=1" ,$db);
			while($que_row=DB_fetch_array($querymake))
			{ 
 		 ?>
      <option value="<?php echo $que_row['id'];?>" <?php echo ($get_row['grade']==$que_row['id'] ?'selected':'')?>><?php echo $que_row['grade'];?></option>
      <?php  }?>
	  </select>

 </td>
    <td>
	<select  name="Color[<?php echo $i;?>]" id="Color_" style="width:100px">
		<option value="0">Select Color</option>
      <?php
			 $querymake=DB_query("select * from product_color where 1=1" ,$db);
			while($que_row=DB_fetch_array($querymake))
			{ 			
 		 ?>
      <option value="<?php echo $que_row['id'];?>" <?php echo ($get_row['color']==$que_row['id'] ?'selected':'')?>><?php echo $que_row['color'];?></option>
      <?php  }?>
	  </select>
 </td>
    <td><?php 
	$unit=$s->getField('item_master','StockID',$get_row['product_id'],'unit');
	$unitname=$s->getField('unitsofmeasure','unitid',$unit,'unitname');
	echo $unitname;?></td>
    <td><?php echo $get_row['stock_qty'];?><input type="hidden" name="stock_qty[<?php echo $i;?>]" value="<?php echo $get_row['stock_qty'];?>" id="stock_qty_<?php echo $i;?>" readonly="true"></td>
    <td><input type="text" name="req_qty[<?php echo $i;?>]" id="req_qty_<?php echo $i;?>" value="<?php echo $get_row['requisition_qty'];?>" size="10" readonly="true"></td>
	
	 <td align="center">
 <input name="material_required_by[<?php echo $i;?>]" type="text" id="material_required_by_<?php echo $i;?>" style="width:100%" value="<?php echo $new_date= date('d-m-Y', strtotime($get_row['material_required_by'])); ?>"  class="datepicker"/></td>
	 <td><input type="text" name="app_qty[<?php echo $i;?>]" id="app_qty_<?php echo $i;?>" value="<?php echo $get_row['requisition_qty'];?>" size="10" onkeyup="subTotal(this.value,<?php echo $i;?>)" >
	
	<!--onchange="calamt(<?php //echo $i; ?>,this.value)" -->
<!--	 onblur="check_approveqty(this.value,<?php //echo $i;?>)"-->
	<input type="hidden" name="app_qty1[<?php echo $i;?>]" id="app_qty1_<?php echo $i;?>" value="<?php echo $get_row['requisition_qty'];?>" size="10" />
	</td>
	 <td><?php
	 $sql_porate=DB_fetch_array(DB_query("SELECT * FROM purchorderdetails where mst_itemid='".$get_row['mst_itemid']."' ORDER BY id DESC",$db));
	 
	// echo $sql_porate['rate'];?><input type="text" name="last_po_rate[<?php echo $i;?>]" value="<?php echo  $sql_porate['rate'];?>" id="last_po_rate_<?php echo $i;?>" ></td>
	 <?php $sub_total=$get_row['requisition_qty']*$sql_porate['rate']; $tot_subtotal[]=$sub_total;?>
	  <td> <input type="text" name="sub_total[<?php echo $i;?>]" id="sub_total_<?php echo $i;?>" readonly="true"  value="<?php echo $sub_total;?>"></td>
     <td align="center">
	 
	 <textarea name="remark[<?php echo $i;?>]" cols="10" rows="2" id="remark_<?php echo $i;?>" readonly="true"><?php echo $get_row['Remarks'];?></textarea>
	 </td>
	<!--<td><select  name="MRstatus[<?php //echo $i;?>]" id="MRstatus_<?php //echo $i;?>"   style="width:50px">
		 <option value="Active" <?php //echo $MRstatus=='Active'?'selected':''?>  >Active</option>
 		 <option value="Inactive" <?php //echo $MRstatus=='Inactive'?'selected':''?> >Inactive</option>
		  <option value="Declined" <?php //echo $MRstatus=='Declined'?'selected':''?> >Declined</option>
		   <option value="Davation" <?php //echo $MRstatus=='Davation'?'selected':''?> >Davation</option>
		 </select>
	
	</td>-->
  </tr>
  <?php  } ?>
   <input type="hidden" name="count_req" value="<?php echo $i;?>" id="count_req"/>
 
 
</table>
</div>
<br />
<br />

<table width="100%" border="0" cellspacing="1" cellpadding="2" >
 
   <tr><td colspan="4" align="right"><table width="30%" align="right"><tr><th width="35%" align="right"><strong>Grand Total</strong></th>
   <td width="65%" bgcolor="#C8BECB"> <input type="text" name="grand_total" value="<?php echo array_sum($tot_subtotal);?>" id="grand_total" readonly="true" size="25" onfocus="calc_basicamt()"></td></tr></table></td></tr>
<tr>
<th><strong>Remarks </strong></th>
<td width="78%" colspan="2"><textarea name="remarks" cols="85" rows="3" id="remark"></textarea></td>
</tr></table>
<br>
<p align="center"><input type="submit" name="dataapprove" value="Approve" onclick="return validate()"></p>
<!--onclick="return validate()"-->
</form>
<?php 	
}

if($_REQUEST['act']=='copy' && $_REQUEST['reqid']!='')
{
if($_REQUEST['SaveCopy']!='')
{

	$requisition_sql="Insert into materialrequisitions set
									requisitionno='',
									comments='".$_REQUEST['remarks']."',
									prdate='".FormatDateForSQL($_REQUEST['date'])."',
									initiator='".$_SESSION['UserID']."',
									sitecode='".$_REQUEST['site']."',
									Reqno='".$_REQUEST['requisition_no']."' ";
	$requisition_res=DB_query($requisition_sql,$db);
	$last_id=DB_Last_Insert_ID($db,'materialrequisitions','id');	
	if($last_id!='')
	{
		for($cr=0;$cr<count($_REQUEST['mst_itemid']);$cr++)
		{
		    	if($_REQUEST['mst_itemid'][$cr]!=''){
				$product_description = htmlspecialchars($_REQUEST['item_name'][$cr], ENT_QUOTES);
				$item_code = htmlspecialchars($_REQUEST['item_code'][$cr], ENT_QUOTES);
				 $material_details_sql="insert into requisitionorderdetails set
											id='',											
											requsitionno='".$last_id."',
											mst_itemid='".$_REQUEST['mst_itemid'][$cr]."' ,
											product_id='".$item_code."',
 											product_description='".$product_description."',
											make='".$_REQUEST['Make'][$cr]."',    
											color='".$_REQUEST['Color'][$cr]."',   
											grade='".$_REQUEST['Grade'][$cr]."',
											size='".$_REQUEST['size'][$cr]."',
											gsm='".$_REQUEST['gsm'][$cr]."',
											req_quantity='".$_REQUEST['required_qty'][$cr]."',
											required_qty='".$_REQUEST['required_qty'][$cr]."',
											requisition_qty='".$_REQUEST['requisition_qty'][$cr]."',
											material_required_by='".FormatDateForSQL($_REQUEST['material_required_by'][$cr])."',
											Remarks='".$_REQUEST['remark'][$cr]."',
											stock_qty='".$_REQUEST['stock_qty'][$cr]."',
											item_no='".$_REQUEST['item_no'][$cr]."',
											status='Active'"; 
			
			$details_res=DB_query($material_details_sql,$db); 
			}
		}
	}

 
$_SESSION['success_msg']="Saved Successfully";	
echo "<script>location.href='index.php?route=POModule/MR_SelectOSMaterialRequi&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
}	
	$sql="Select * from materialrequisitions where requisitionno='".$_REQUEST['reqid']."'";
	$result=DB_query($sql,$db);
	$row=DB_fetch_array($result);
$prefix_sql=DB_query("Select * from prefix_tbl where prefix_name='".$PageName."'",$db);
$prefix_row=DB_fetch_array($prefix_sql);
$prefix=$prefix_row['prefix_code'];

$sql2="SELECT Reqno FROM materialrequisitions where sitecode='".$_REQUEST['site']."' ORDER BY Reqno DESC LIMIT 0,1";
$result2=DB_query($sql2,$db);
		if(DB_num_rows($result2)>0)
		{
			$row2=DB_fetch_array($result2);
			$Reqno=$row2['Reqno'];
			$req_part1=substr($Reqno,-4);
			$req_final=$req_part1+1;
		}
				$str=substr($_REQUEST['site'],0,3);
				$x=substr($prefix,6,9);
				$year=$_SESSION['financialYearStart'];
				$year_after=$_SESSION['financialYearEnd'];
				

$string=$str."/".$x."/".$year.$year_after."/";
$final_string=$string.str_pad($req_final,6,"0",STR_PAD_LEFT);

?>

<form name="RequisitionDetailsform" action="" method="post" onsubmit="return formvalidate()">
<h2 align="center"><strong>Purchase Requisition</strong></h2>
<div style="width:950px; margin-left:auto; margin-right:auto;">
<h3 align="center"><strong>Requisition Details</strong></h3>

<table width="100%" border="1" cellspacing="0" cellpadding="3">
  <tr>
    <th><strong>REQUISITION DATE</strong></th>
    <td><input type="text" name="date" id="date" class="datepicker" readonly="true" value="<?php  echo date('d/m/Y');?>" onchange="checkReqDate()" style="width:98%"></td>
    <th rowspan="3"><strong>REMARKS</strong></th>
    <td rowspan="3"><textarea name="remarks" style="width:100%" rows="3"></textarea></td>
  </tr>
  <tr>
    <th><strong>SITE</strong></th>
    <td>
	<?php 
	if($_SESSION['AssignedSites']!='0')
	  {
	  	
	   	  $ss="Select loccode,locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in (".$_SESSION['AssignedSites'].")";
	  	 $site_sql=DB_query($ss,$db);
	  }
	  else
	  {
		  $ss="Select loccode,locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."'";
		$site_sql=DB_query($ss,$db);
	  }
	?>
	<select name="site" id="site" style="width:98%" onchange="generate_reqno(this.value)">
      <option value="000">---Select---</option>
      <?php
	while($site_row=DB_fetch_array($site_sql)){
	?>
      <option value="<?php echo $site_row['loccode'];?>"<?php echo ($_REQUEST['site']==$site_row['loccode']?'selected':'')?>><?php echo $site_row['locationname'];?></option>
      <?php } ?>
    </select></td>
  </tr>
  <tr>
    <th><strong>REQUISITION NUMBER </strong></th>
    <td><div id="reqno">
        <input name="requisition_no" type="text" id="requisition_no" value="<?php echo $final_string;?>" style="width:98%"readonly="true" />
      </div></td>
  </tr>
</table>

</div>
<p align="left">
<input type="button" name="Add" value="Add"  onclick="addRow('dataTable')"/>
<input type="button" class="search"   onclick="deleteRowOrder('dataTable')" value="Delete" /></p>

<div style="width:950px; margin-left:auto; margin-right:auto; overflow:auto;">
<h3 align="left" style="padding-left:10px;"><strong>Item Details</strong></h3>
<table width="100%" border="1" cellpadding="0" cellspacing="3	">
  <tr>
	    <th></th>
        <th width="6%" align="center">ITEM CODE </th>
		<th></th>
        <th width="8%" align="center">ITEM NAME </th>
        <th width="13%" align="center">MAKE</th>	
        <th width="8%" align="center">GRADE</th>	
        <th width="8%" align="center">COLOR</th>		
        <th width="8%" align="center">SIZE</th>
		<th width="6%" align="center">UNIT</th>
	     <th width="6%" align="center">Item No</th>
        <th width="8%" align="center">Current Stock </th>
        <th width="10%" align="center">REQUISITION QUANTITY </th>
        <th width="10%" align="center">REQUIRED QUANTITY </th>
        <th width="8%" align="center">MATERIAL REQUIRED DATE </th>
        <th width="20%" align="center">REMARKS</th>
      </tr>
	<?php 
	 $details="Select * from requisitionorderdetails where requsitionno='".$_REQUEST['reqid']."'";
	$details_res=DB_query($details,$db);
	$k=0;
	while($details_row=DB_fetch_array($details_res))
	{
	extract($details_row);
		$item_sql=DB_query("Select item_name,unit,item_no from item_master where StockID=".$product_id."",$db);
		$item_row=DB_fetch_array($item_sql);
		
		//$_SESSION['ACGRequisition-ItemList_1-copy']=$product_id;
	?>
	 <tr bgcolor="#FFFFFF">
    <td><input name="delitem[]" type="checkbox"  id="delitem_<?php echo $k; ?>" /></td>
<td><input type="text" name="item_code[]" id="itemcode_<?php echo $k; ?>" value="<?php echo $product_id;?>" readonly /> </td>
<td><input type="hidden" name="mst_itemid[]" id="mst_itemid_<?php echo $k; ?>" value="<?php echo $mst_itemid;?>" /></td>
<td><input type="text" name="item_name[]" id="item_name_<?php echo $k; ?>" readonly value="<?php echo $s->getField('item_master','id',$mst_itemid,'item_name');?>" /> </td>
<td><select name="Make[<?php echo $i;?>]" id="Make" style="width:100%">
			<option value="0">Select Make</option>
      <?php
			 $querymake=DB_query("select * from product_manufacture where 1=1" ,$db);
			while($que_row=DB_fetch_array($querymake))
			{ 
 		 ?>
      <option value="<?php echo $que_row['id'];?>" <?php echo ($details_row['make']==$que_row['id'] ?'selected':'')?>><?php echo $que_row['manufacturer'];?></option>
      <?php  }?>
	  </select></td>
<td><select name="Grade[<?php echo $i;?>]" style="width:100%">
			<option value="0">Select Grade</option>
		<?php
			 $querygrade=DB_query("select * from product_grade where 1=1" ,$db);
			while($que_row=DB_fetch_array($querygrade))
			{ 
 		 ?>
      <option value="<?php echo $que_row['id'];?>" <?php echo ($details_row['grade']==$que_row['id']?'selected':'')?>><?php echo  $que_row['grade'];?></option>
      <?php  }?>
	  </select></td>
<td><select name="Color[<?php echo $i;?>]" style="width:100%">
			<option value="0">Select Color</option>
		<?php
			 $querycolor=DB_query("select * from product_color where 1=1" ,$db);
			while($que_row=DB_fetch_array($querycolor))
			{ 
 		 ?>
      <option value="<?php echo $que_row['id'];?>" <?php echo ($details_row['color']==$que_row['id'] ?'selected':'')?>><?php echo  $que_row['color'];?></option>
      <?php  }?></select></td>
<td><select name="size[<?php echo $i;?>]" id="size_<?php echo $i;?>" style="width:100%">
		  	<option value="0">Select Size</option>
		    <?php 
		
			$sizeId=DB_query("Select * from size_master where 1",$db);
			while($sizeRow=DB_fetch_array($sizeId))
			{
		?>
		    <option value="<?php echo $sizeRow['id'];?>"><?php echo $sizeRow['name'];?></option>
		    <?php } ?>
		    </select> </td>
<td><?php
		$grade_query=DB_query("select * from unitsofmeasure where unitid='".$details_row['uom']."'", $db);
		while($grade_row=DB_fetch_array($grade_query))
		 {
		 echo $grade_row['unitname'];?>
		  <?php } ?></td>
		  
 <td><input type="text" name="item_no[]" id="itemno1_<?php echo $k; ?>"  readonly /></td>
    <td> <input type="text" name="stock_qty[]" id="stockqty_<?php echo $k; ?>"  style="width:100%" value="<?php echo $details_row['stock_qty']; ?>"  readonly /></td>
  <td align="center"><input name="requisition_qty[]" type="text" id="requisitionqty_<?php echo $k; ?>" style="width:100%" size="14" onblur="CalrequiredQty(this.value,this.id)" value="<?php echo $details_row['required_qty']; ?>"  readonly/></td>
  <td align="center"><input name="required_qty[]" type="text" id="requiredqty<?php echo $k; ?>" style="width:100%" value="<?php echo $details_row['req_quantity']; ?>" /></td>
        <td align="center"><input name="material_required_by[]" id="materialrequiredby_<?php echo $k; ?>" type="text" class="datepicker"  readonly
	 onchange="checkReqDate1(this.id)"  value="<?php echo ConvertSQLDate($details_row['material_required_by']); ?>" /></td>
        <td align="center"><textarea name="textarea" cols="10" rows="2" id="textarea_<?php echo $k; ?>"><?php echo $details_row['Remarks']; ?></textarea>
        </td>
      </tr>	  	
	  <?php 
	$k++;}
	?>  
	  <tbody id="dataTable">
      <tr bgcolor="#FFFFFF">
    <td><input name="delitem[]" type="checkbox"  id="delitem" /></td>
<td><input type="text" name="item_code[]" id="itemcode" value="" onkeyup="opendata(this.value,this.id)" /> </td>
<td><input type="hidden" name="mst_itemid[]" id="mst_itemid" value="" /></td>
<td><input type="text" name="item_name[]" id="item_name" readonly /> </td>
<td><select name="Make[<?php echo $i;?>]" id="Make" style="width:100%">
			<option value="0">Select Make</option>
      <?php
			 $querymake=DB_query("select * from product_manufacture where 1=1" ,$db);
			while($que_row=DB_fetch_array($querymake))
			{ 
 		 ?>
      <option value="<?php echo $que_row['id'];?>" <?php echo ($details_row['make']==$que_row['id'] ?'selected':'')?>><?php echo $que_row['manufacturer'];?></option>
      <?php  }?>
	  </select></td>
<td><select name="Grade[<?php echo $i;?>]" style="width:100%">
			<option value="0">Select Grade</option>
		<?php
			 $querygrade=DB_query("select * from product_grade where 1=1" ,$db);
			while($que_row=DB_fetch_array($querygrade))
			{ 
 		 ?>
      <option value="<?php echo $que_row['id'];?>" <?php echo ($details_row['grade']==$que_row['id']?'selected':'')?>><?php echo  $que_row['grade'];?></option>
      <?php  }?>
	  </select></td>
<td><select name="Color[<?php echo $i;?>]" style="width:100%">
			<option value="0">Select Color</option>
		<?php
			 $querycolor=DB_query("select * from product_color where 1=1" ,$db);
			while($que_row=DB_fetch_array($querycolor))
			{ 
 		 ?>
      <option value="<?php echo $que_row['id'];?>" <?php echo ($details_row['color']==$que_row['id'] ?'selected':'')?>><?php echo  $que_row['color'];?></option>
      <?php  }?></select></td>
<td><select name="size[<?php echo $i;?>]" id="size_<?php echo $i;?>" style="width:100%">
		  	<option value="0">Select Size</option>
		    <?php 
		
			$sizeId=DB_query("Select * from size_master where 1",$db);
			while($sizeRow=DB_fetch_array($sizeId))
			{
		?>
		    <option value="<?php echo $sizeRow['id'];?>"><?php echo $sizeRow['name'];?></option>
		    <?php } ?>
		    </select> </td>
<td><input type="text" name="Unit[]" id="Unit" style="width:100%" readonly /></td>
 <td><input type="text" name="item_no[]" id="itemno"  readonly /></td>
    <td> <input type="text" name="stock_qty[]" id="stockqty"  style="width:100%"/></td>
  <td align="center"><input name="requisition_qty[]" type="text" id="requisitionqty" style="width:100%" size="14" onblur="CalrequiredQty(this.value,this.id)" /></td>
  <td align="center"><input name="required_qty[]" type="text" id="required_qty" style="width:100%"/></td>
        <td align="center"><input name="material_required_by[]" id="materialrequiredby" type="text" class="datepicker"  
	 onchange="checkReqDate1(this.id)" /></td>
        <td align="center"><textarea name="textarea" cols="10" rows="2" id="textarea"></textarea>
        </td>
      </tr>
   </tbody>  
</table>

<input type="hidden" name="hiditemcount" id="hiditemcount" value="" />
<input type="hidden" name="" value="" id="edit_cnt" />

</div>
<p align="center"><input type="submit" name="SaveCopy" value="Save" onclick="return validatedata()"><input type="button" name="back" value="Back">
<input type="submit" name="cancel" value="Reset" />	
</p>

</form>
 <?php } ?>



<script language="javascript" type="text/javascript">
function deactivate(str)
{
	document.getElementById("mst_itemid_"+str).value='';
	document.getElementById("active_"+str).style.display='block';
	alert('Item removed');
}
function activate(str)
{
	document.getElementById("mst_itemid_"+str).value=document.getElementById("temp_item_id_"+str).value;
	document.getElementById("active_"+str).style.display='none';
}
function open_copy_items()
{
	window.open("requisition_items.php?action=copy&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&reqid=<?php echo $_REQUEST['reqid'];?>","item_popup_window","height=600,width=500,toolbar=no,menubar=no,dependent=yes,resizable =yes, scrollbars=yes");
}
function checkReqDate()
{

var curr_date2='';	
var curr_month2='';
var d = new Date();

//alert(d.format("m/dd/yy"));
var curr_date = d.getDate();


var curr_month1 = d.getMonth();

var curr_year = d.getFullYear();
var curr_month=curr_month1+1;
var t=curr_month;
var d=curr_date;
if(t!=10 || t!=11 ||t!=12 )
{
 curr_month2="0" + curr_month;	
}
else
{
 curr_month2=curr_month;	
}
if(d==1 || d==2 || d==3 || d==4 || d==5 || d==6 || d==7 || d==8 || d==9 )
{
 curr_date2="0" + curr_date;		
}
else
{
 curr_date2=curr_date;		
}


var datedat=(curr_date2 + "/" + curr_month2 
+ "/" + curr_year);

var date1=document.getElementById("date").value;
var str1 = date1;
var str2 = datedat;
    var dt1  = parseInt(str1.substring(0,2),10);
    var mon1 = parseInt(str1.substring(3,5),10);
    var yr1  = parseInt(str1.substring(6,10),10);
    var dt2  = parseInt(str2.substring(0,2),10);
    var mon2 = parseInt(str2.substring(3,5),10);
    var yr2  = parseInt(str2.substring(6,10),10);
    var date1 = new Date(yr1, mon1, dt1);
    var date2 = new Date(yr2, mon2, dt2);
    if(date2 > date1)
    {
        alert(" Requisition Date Can't Be Less than Current Date");
        return false;
    }
    else
    {
        return true;
        
    } 




}










function checkReqDate1(i)
{

var j=i.split("_");

if(j[1]==undefined)
{
var date1=document.getElementById("materialrequiredby").value;
}
else
{
var k=j[1];	
var date1=document.getElementById("materialrequiredby_"+k).value;
}
       
       var collection1 =   date1.split("/");
             
       var DobDate   = collection1[0];
       var DobMonth = collection1[1];       
       var DobYear   = collection1[2];     
	   
	   
	   var d = new Date();        
              
       var SysDate   = d.getDate();
       var SysMonth = d.getMonth()+1;
       var SysYear   = d.getFullYear();       
        
       if(DobYear < SysYear)
       {
         alert(" Requisition Date Can't Be Less than Current Date");
		
		
		 
         return false;      
       }
       else if((DobYear == SysYear) && (DobMonth < SysMonth))
       {
        alert(" Requisition Date Can't Be Less than Current Date");
		
		
         return false; 
       }
       
        else if((DobYear == SysYear) && (DobMonth == SysMonth) && (DobDate < SysDate))
       {
        alert(" Requisition Date Can't Be Less than Current Date");
		
		
         return false;
       }
      
         return true;


	


}

function validatedata()
{
var date=document.getElementById("date").value;

var from_date= document.getElementById('date').value; 


	    
var curr_date2='';	
var curr_month2='';
var d = new Date();


//alert(d.format("m/dd/yy"));
var curr_date = d.getDate();



var curr_month1 = d.getMonth();


var curr_year = d.getFullYear();
var curr_month=curr_month1+1;


var t=curr_month;
var d=curr_date;
if(t!=10 || t!=11 ||t!=12 )
{
 curr_month2="0" + curr_month;	
}
else
{
 curr_month2=curr_month;	
}
if(d==1 || d==2 || d==3 || d==4 || d==5 || d==6 || d==7 || d==8 || d==9 )
{
 curr_date2="0" + curr_date;		
}
else
{
 curr_date2=curr_date;		
}


var datedat=(curr_date2 + "/" + curr_month2 
+ "/" + curr_year);


var date1=Date.parse(date);
var datedat1=Date.parse(datedat);


if(date1 < datedat1)
		{
			alert("Requisition Date Can't Be Less than Current Date");
			document.getElementById("date").focus();
			return false;
		}
	
if(document.getElementById("site").value=='000')
{
	alert("Please Select The Site First");
	return false;
}

		
		
var count=document.getElementById("hiditemcount").value;	
	//if(count=='')
//	{
//	alert("Must Select Items");
//	return false;	
//	}
if(count==''){

if(document.getElementById("mst_itemid").value=='')
{
	alert("Please Select Items First");
	return false;
}

if(document.getElementById("requisitionqty").value=='')
{
	alert("Please Select Requisition Qty");
	return false;
}
if(document.getElementById("requiredqty").value=='')
{
	alert("Please Select Required Qty");
	return false;
}
if(document.getElementById("materialrequiredby").value=='')
{
	alert("Please Select Date");
	return false;
}
}
else
{
	for(var i=1;i<=count;i++)
	{
	
	if(document.getElementById("mst_itemid_"+i).value=='')
	{
	alert("Please Select Items First");
	return false;
	}
	
	
	if(document.getElementById("requisitionqty_"+i).value=='' || document.getElementById("requisitionqty_"+i).value==0)
	{
	alert("Please Enter Requisition Quantity");
	document.getElementById("requisitionqty_"+i).focus();
	return false;	
	}	
	
	
	if(document.getElementById("requiredqty_"+i).value=='' || document.getElementById("requiredqty_"+i).value==0)
	{
	alert("Please Enter Reqired Quantity");
	document.getElementById("requiredqty_"+i).focus();
	return false;	
	}	
	
	
	if(document.getElementById("materialrequiredby_"+i).value<datedat)
	{
		alert("Material Required Date Can't Be Less than Current Date");
			document.getElementById("materialrequiredby_"+i).focus();
			return false;
		
	}
	
	}	return true;
	
	}
	
}
function validate()
{
	
	var counter=document.getElementById("count_req").value;
	
	for(var x=1;x<=counter;x++)
	{
	
		var approve=document.getElementById("app_qty_"+x).value;
		
		if(approve=='')
		{
			alert("Please enter quantity to be approved for all items");
			return false;
		}
	}
	
}

function subTotal(str,i)
{
		var rate=document.getElementById("last_po_rate_"+i).value;
		if(rate==''){rate=0;}
		var total=parseFloat(str) * parseFloat(rate);
		var approve=document.getElementById("sub_total_"+i).value=total.toFixed(2);
		var count_req=document.getElementById("count_req").value;
		var grand_tot=0;
		for(var k=1;k<=count_req;k++)
		{
			var sub_tot=document.getElementById("sub_total_"+k).value;
			grand_tot=parseFloat(grand_tot)+parseFloat(sub_tot);
		}
		document.getElementById("grand_total").value=grand_tot.toFixed(2);
		
}
function calc_basicamt()
{
	var grand_total=0;
	var cnt=document.getElementById("count_req").value
	for(var j=1;j<=cnt;j++)
	{
		var sub_total=document.getElementById("sub_total_"+j).value;
		if(sub_total==''){sub_total=0;}
		grand_total=parseFloat(grand_total)+parseFloat(sub_total);
	}
	document.getElementById("grand_total").value=grand_total.toFixed(2);
}

function calamt(i,str)
{
	
document.getElementById("app_qty1_"+i).value=str;	
	
}

function opendata(str,id)
{

  var sitecode=document.getElementById("site").value;
  var date=document.getElementById("date").value;
 if (str.length == 0)
    {    	
     document.getElementById("lightbox2").innerHTML="";
     document.getElementById("lightbox2").style.border="0px";
      return;
    }	
	
	if(sitecode=='000')
	{
	alert("Must Select Site");
	document.getElementById("site").focus();
	return;
	}
	else if(date=='')
	{
	alert("Must Select Date");
	document.getElementById("date").focus();
	return;
	}

var xmlhttp;
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)
    {
    document.getElementById("lightbox2").style.display='block';	
	document.getElementById("lightbox2").innerHTML=xmlhttp.responseText;
	
   // document.getElementById("textitem").innerHTML=data[1];
	
    }
  }
  
  var sitecode=document.getElementById("site").value;
  var date=document.getElementById("date").value;


  xmlhttp.open("GET","showpopup_for_requisition.php?site="+str+"&data="+id+"&site1="+sitecode+"&date="+date,true);
  xmlhttp.send();	
}


function getItemsList1(id,code,shade,netrate,mrprate,rate9,rate10,ctr)
{ 
	var dup='empty';
	var ctr1=ctr.split("_");	
	var i=ctr1[1]
	var itemcount=parseInt(document.getElementById('hiditemcount').value)+1;
	var edit_cnt=document.getElementById('edit_cnt').value;
	if(edit_cnt==''){var counter=1;} else if(edit_cnt>itemcount){var counter=edit_cnt;
	var itemcount=parseFloat(counter)+parseFloat(itemcount);
	} 
	else 
	{
	var counter=edit_cnt;
	}	  
if(ctr1[1]==undefined)
{
	if(document.getElementById('mst_itemid').value=='')
	{
	if(edit_cnt==''){var counter=0;}
	for(var j=0; j<counter; j++)
	{
	if(document.getElementById('mst_itemid_'+j).value==id)
	{
	alert("Duplicate Value"); 
	var dup = "dup";
	break;
	}
	}
	if(dup=='empty'){
	document.getElementById('lightbox2').style.display='none';
	document.getElementById('mst_itemid').value = id;
	document.getElementById('itemcode').value = code;
	document.getElementById('item_name').value = shade;
	document.getElementById('Unit').value = netrate;
	document.getElementById('stockqty').value = mrprate;
	//document.getElementById('Unit1').value=rate9;
	document.getElementById('itemno').value=rate10;
	}
	}
	else
	{
	for(var i=counter; i<=itemcount; i++)
	{	if(edit_cnt==''){var counter=0;}
	for(var j=0; j<counter; j++)
	{
	if(document.getElementById('mst_itemid_'+j).value==id)
	{
	alert("Duplicate Value"); 
	var dup = "dup";
	break;
	}
	}
	if(document.getElementById('mst_itemid').value==id)
	{
	alert("Duplicate Value");
	break;
	}
	if(document.getElementById('mst_itemid_'+i).value==id)
	{
	alert("Duplicate Value"); 
	break;
	}
	if(dup=='empty'){				
	if(document.getElementById('mst_itemid_'+i).value=='')
	{
	document.getElementById('lightbox2').style.display='none';
	document.getElementById('mst_itemid_'+i).value = id;
	document.getElementById('itemcode_'+i).value = code;
	document.getElementById('item_name_'+i).value = shade;
	document.getElementById('Unit_'+i).value = netrate;
	document.getElementById('stockqty_'+i).value = mrprate;
	//document.getElementById('Unit1_'+i).value=rate9;	
	document.getElementById('itemno_'+i).value=rate10;
	break;
	}
	else
	{
	continue;
	}
	}
	}
	}
}
else
{
	if(document.getElementById('mst_itemid').value=='')
	{
	if(edit_cnt==''){var counter=0;}
	for(var j=0; j<counter; j++)
	{
	if(document.getElementById('mst_itemid_'+j).value==id)
	{
	alert("Duplicate Value"); 
	var dup = "dup";
	break;
	}
	}
	if(dup=='empty'){
	document.getElementById('lightbox2').style.display='none';
	document.getElementById('mst_itemid').value = id;
	document.getElementById('itemcode').value = code;
	document.getElementById('item_name').value = shade;
	document.getElementById('Unit').value = netrate;
	document.getElementById('stockqty').value = mrprate;
	//document.getElementById('Unit1').value=rate9;
	document.getElementById('itemno').value=rate10;
	}
	}
	else
	{
	for(var i=counter; i<=itemcount; i++)
	{	if(edit_cnt==''){var counter=0;}
	for(var j=0; j<counter; j++)
	{
	if(document.getElementById('mst_itemid_'+j).value==id)
	{
	alert("Duplicate Value"); 
	var dup = "dup";
	break;
	}
	}
	if(document.getElementById('mst_itemid').value==id)
	{
	alert("Duplicate Value");
	break;
	}
	if(document.getElementById('mst_itemid_'+i).value==id)
	{
	alert("Duplicate Value"); 
	break;
	}
	if(dup=='empty'){				
	if(document.getElementById('mst_itemid_'+i).value=='')
	{
	document.getElementById('lightbox2').style.display='none';
	document.getElementById('mst_itemid_'+i).value = id;
	document.getElementById('itemcode_'+i).value = code;
	document.getElementById('item_name_'+i).value = shade;
	document.getElementById('Unit_'+i).value = netrate;
	document.getElementById('stockqty_'+i).value = mrprate;
	//document.getElementById('Unit1_'+i).value=rate9;	
	document.getElementById('itemno_'+i).value=rate10;
	break;
	}
	else
	{
	continue;
	}
	}
	}
	}
	}
}

function hidedata()
{
document.getElementById("lightbox2").style.display='none';
}

function addRow(tableID) 
{	
	if(document)
	var table = document.getElementById(tableID);
	var rowCount = table.rows.length;
	var row = table.insertRow(rowCount);
	document.getElementById("hiditemcount").value=rowCount;
	var colCount = table.rows[0].cells.length;
	for(var i=0; i<colCount; i++) {	 
	var newcell = row.insertCell(i);
	//newcell.style.background='#e7e7e7';
	//  newcell.className='tabledata';
	newcell.innerHTML = table.rows[0].cells[i].innerHTML;
	switch(newcell.childNodes[0].type) 
	{
	case "text":
	newcell.childNodes[0].value = "";
	var nod_id = newcell.childNodes[0].id;
	newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
	//jQuery("newcell.childNodes[0].id").datepicker({ dateFormat: 'dd/mm/yy',changeMonth: true, changeYear: true,yearRange: '-80:+20'});
	
	break;
	case "hidden":
	newcell.childNodes[0].value = "";
	var nod_id_hidden = newcell.childNodes[0].id;
	newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
	break;
	case "button":
	var nod_id = newcell.childNodes[0].id;
	newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
	break;
	case "checkbox":
	newcell.childNodes[0].checked = false;
	var nod_id_chk = newcell.childNodes[0].id;
	newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
	break;
	case "select-one":
	newcell.childNodes[0].selectedIndex = 0;
	var nod_id = newcell.childNodes[0].id;
	newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+rowCount;
	break;	}
	}
}




function deleteRowOrder(tableID) 
{
	try 
	{
	var table = document.getElementById(tableID);
	//alert(table);
	var rowCount = table.rows.length;
	//alert(rowCount);
	for(var i=0; i<rowCount; i++) 
	{
	var row = table.rows[i];
	var chkbox = row.cells[0].childNodes[0];
	if(null != chkbox && true == chkbox.checked) 
	{
	if(rowCount <= 1) 
	{
	alert("Cannot delete all the rows.");
	break;
	}
	table.deleteRow(i);
	rowCount--;
	i--;
	}
	}
	document.getElementById("hiditemcount").value=parseInt(rowCount)-1;		}
	catch(e) 
	{
	alert(e);
	}
}


</script>

