<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>
<script type="text/javascript" language="javascript">
$.noConflict();
$(document).ready(function(){	
		//var cnt=document.getElementById("count").value;
		$('input[name=profile_popup]').click(function(){
		var tt=this.id;
		var tt1=tt.split("_");
		var tt2=tt1[1];
		var site=document.getElementById("site").value;
		
		//var LeaveType=document.getElementById("LeaveType_"+tt2).value;
		$("#PickedItems").dialog({
				title:'Item List',
				modal: true,
				resizable: false,
				width:900,
				height:400,
				closeText: 'fechar',
				draggable: true,
				show: 'fade',
				hide: 'fade',
				dialogClass: 'main-dialog-class',
				buttons: {
					Exit: function()
					{
						$(this).dialog("close");
					}
				}
				
			});
		GetitemListForDirectPO(site);
		});
	});
	
	function GetitemListForDirectPO(str)
{
	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("PickedItems").innerHTML=xmlhttp.responseText;
	}
	}
	xmlhttp.open("GET","popup_item_list.php?site="+str,true);
	xmlhttp.send();
}
function getItemsList(id,code,shade,current,desc,ctr)
{
	var dup='empty';
	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(document.getElementById("row_id_"+ctr).checked==true)
	{
		  
			for(var i=counter; i<itemcount; i++)
			{	
					
				for(var j=0; j<itemcount; j++)
			    {
				if(document.getElementById('itemid_'+j))
					{	
				if(document.getElementById('itemid_'+j).value==id)
				{
					alert("Duplicate Value"); 
					document.getElementById("row_id_"+ctr).checked=false;
					var dup = "dup";
					break;
				}
				}
			}
			
				if(dup=='empty')
				{	
				
				var data= addRow('dataTable');			
				
					if(document.getElementById('itemid_'+i))
					{				
					 
					if(document.getElementById('itemid_'+i).value=='')
					{
					   		
						document.getElementById('itemid_'+i).value = id;
						document.getElementById('itemcode_'+i).value = code;
						document.getElementById('item_name_'+i).value = shade;
						document.getElementById('Unit_'+i).value =desc ;		
				        document.getElementById('stockqty_'+i).value =current ;
					
						break;
					}
					}
					else
					{
						continue;
					}
				}
			}
		
	}
	else
	{
		
		
			for(var i=counter; i<itemcount; i++)
			{
			    if(document.getElementById('itemid_'+i))
				{
				if(document.getElementById('itemid_'+i).value==document.getElementById("row_id_"+ctr).value)
				{
				    document.getElementById('itemid_'+i).value = '';
					document.getElementById('itemcode_'+i).value = '';
					document.getElementById('item_name_'+i).value = '';
					document.getElementById('Unit_'+i).value = '';		
				document.getElementById('stockqty_'+i).value = '';
				}
				}
			}
		}
	
	
	}
	



//function claculateExcise(id,val)
//{
//	$("#div_id").dialog({
//	modal: true,
//	resizable: false,
//	width:300,
//	height:150,
//	closeText: 'fechar',
//	draggable: true,
//	show: 'fade',
//	hide: 'fade',
//	dialogClass: 'main-dialog-class',
//	buttons: {
//		//Save: function()
////		{
////		$("#myform").closest("form").submit();
////		},
//		//Exit: function()
////		{
////			$(this).dialog("close");
////		}
//	}
//	});
//	$(".ui-dialog-titlebar").hide();
//	calculateExcisejq(id,val)
//}
</script>
<?php session_name('CAKEPHP'); session_start();
$PageSecurity = 4;
$PageName = 'Purchase Requisition';
$title = _('Purchase Requisition');
if($_REQUEST['Save']!='')
{
	$prefix_sql=DB_query("Select * from prefix_tbl where prefix_name='".$PageName."'",$db);
	$prefix_row=DB_fetch_array($prefix_sql);
	$prefixf=explode("/",$prefix_row['prefix_code']);
	$prefix=$prefixf[1];
	$financial=substr($_SESSION['YearStart'],-2)."-".substr($_SESSION['YearEnd1'],-2);
	 $sql="Select Reqno from materialrequisitions where sitecode='".$_REQUEST['site']."' 
	and INSTR(`Reqno`, '$financial') > 0 order by Reqno desc limit 0,1";

	$result=DB_query($sql,$db);
	$myrow=DB_fetch_array($result);
	//echo DB_num_rows($result);
	if(DB_num_rows($result)>0)
	{
	//$Reqno=$myrow['Reqno'];
	//$req_part1=substr($Reqno,-4);
	$ExplBill=explode("/",$myrow['Reqno']);
	$maxval = $ExplBill[3];
	//echo $req_part1;
	$req_final=$maxval+1;
	//echo "<br>".$req_final;
	}
	else
	{
	$req_final="1";
	}
	
	$string=$prefix."/".$_REQUEST['site']."/".$financial."/";
	$final_string=$string.str_pad($req_final,4,"0",STR_PAD_LEFT);
	
	$data['sitecode']=$_REQUEST['site'];
	$data['Reqno']=$final_string;
	$data['requition_by']=$_REQUEST['requition_by'];
	$data['prdate']=FormatDateForSQL($_REQUEST['date']);
	$data['category']=$_REQUEST['category'];
	$data['comments']=$_REQUEST['remarks'];
	$data['initiator']=$_SESSION['UserID'];
	$data['CompanyId']=$_SESSION['ACGCompanyId'];
	//echo "<pre>";
	//print_r($data);
	//exit;
	$s->RecordInsert('materialrequisitions',$data);
	$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]."',
	uom='".$_REQUEST['Unit'][$cr]."',
	stock_qty='".$_REQUEST['stock_qty'][$cr]."',
	item_no='".$_REQUEST['item_no'][$cr]."'"; 
	$details_res=DB_query($material_details_sql,$db);
	}
	}
}

$_SESSION['success_msg']="Saved Successfully";	
$s->pageLocation("index.php?route=POModule/MR_SelectOSMaterialRequi".$rurl);
}
?>
<div id="PickedItems" style="display:none">
</div>

<div id="div_id" style="display:none"></div>

<div id="div_idEcess" style="display:none"></div>
<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>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div"> 
<h1>Purchase Requisition</h1>	
<?php 
if($_REQUEST['Cancel'])
{

 $update=DB_query("update requisitionorderdetails set edit_status=0 where requsitionno='".$_GET['RequisitionNo']."'",$db);

	 unset($_SESSION['itemlist']);
	 unset($_SESSION['ACGRequisition-ItemList']);

}
if(isset($_POST['Update']))
{

 

 //$sqldata=DB_query("delete  from requisitionorderdetails where edit_status=1 and requsitionno='".$_GET['RequisitionNo']."'",$db);
 
 
 	  $materialrequisitions_sql="Update materialrequisitions set
									comments='".$_REQUEST['remarks']."',
									prdate='".FormatDateForSQL($_REQUEST['date'])."',
									initiator='".$_SESSION['UserID']."',
									sitecode='".$_REQUEST['site']."',
									requition_by='".$_REQUEST['requition_by']."',
									category='".$_REQUEST['category']."',
									Reqno='".$_REQUEST['requisition_no']."'  
									WHERE requisitionno='".$_GET['RequisitionNo']."'";
					
						$update_materialrequisitions_sql=DB_query($materialrequisitions_sql,$db);
	    $del=DB_query("delete from requisitionorderdetails where requsitionno='".$_REQUEST['RequisitionNo']."' ",$db);
	for($i=0;$i<count($_REQUEST['mst_itemid']);$i++)
	{
		if($_REQUEST['mst_itemid'][$i]!='')
		{
			$dataArr['requsitionno']=$_REQUEST['RequisitionNo'];
			$dataArr['mst_itemid']=$_REQUEST['mst_itemid'][$i];
			$dataArr['product_id']=$_REQUEST['item_code'][$i];
			$dataArr['product_description']=$_REQUEST['item_name'][$i];
			$dataArr['stock_qty']=$_REQUEST['stock_qty'][$i];
			$dataArr['requisition_qty']=$_REQUEST['requisition_qty'][$i];
			$dataArr['material_required_by']=FormatDateForSQL($_REQUEST['material_required_by'][$i]);
			$dataArr['uom']=$_REQUEST['Unit'][$i];
			$dataArr['Remarks']=$_REQUEST['textarea'][$i];
			$s->RecordInsert('requisitionorderdetails',$dataArr);//INSERT INTO CHIELD TABLE
		}
	}	 		
$_SESSION['success_msg']="Update Successfully";	
echo "<script>location.href='index.php?route=POModule/MR_SelectOSMaterialRequi$rurl&RequisitionNo=".$_GET['RequisitionNo']."'</script>";
unset($_SESSION['itemlist']);
unset($_SESSION['ACGRequisition-ItemList']);
unset($_SESSION['requisition_no']);
}
  $date1=date('M-Y');
  $date2=date('Y-m-d');
		if (empty($_GET['identifier'])) {
			 $identifier=date('U');
		} else {
			$identifier=$_GET['identifier'];
		}
		$msg='';
		    $reqno = $_GET['RequisitionNo'];
			$sql="SELECT * FROM materialrequisitions WHERE requisitionno='".$reqno."'";
			$result=DB_query($sql,$db);
			$myrow=DB_fetch_array($result);
			$site=$myrow['sitecode'];
			$Reqno=$myrow['Reqno'];
			$prdate =$myrow['prdate'];
			$version=$myrow['version'];
			$revised=$myrow['revised'];
			$initiator=$myrow['initiator'];
			$requisition_site=$myrow['requisition_site'];
			$sitecode=$myrow['sitecode'];
			$dateprinted=$myrow['dateprinted'];
			$requisition_nature=$myrow['requisition_nature'];
			$month_year=$myrow['month_year'];
			$purchase_category =$myrow['purchase_category'];
			$intostocklocationcode=$myrow['intostocklocationcode'];
			$intostocklocation=$myrow['intostocklocation'];
			//$status=$myrow['status'];
			$stat_comment=$myrow['stat_comment'];
			$comments=$myrow['comments'];
			$requition_by=$myrow['requition_by'];
			$category=$myrow['category'];
			
			?>	
<form name="RequisitionDetailsform" action="" method="post" onsubmit="return formvalidate()">
<table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr class="row1">
	<td colspan="4" style="font-size:12px; font-weight:bold;">Please enter the requisition details first</td>
</tr>
<tr class="row1">
	<td class="tabledata1"><strong>Branch</strong></td>
	<td>
	<?php 
	//$ss1="Select loccode,locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in ('".$_SESSION['AssignedBranch']."')";
	$ss1="Select loccode,locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode='".$site."'";
	$site_sql=DB_query($ss1,$db);?>
	<select name="site" id="site" style="width:150px;"><!-- onchange="generate_reqno(this.value)"-->
	<?php while($site_row=DB_fetch_array($site_sql)){?>
	<option value="<?php echo $site_row['loccode'];?>" <?php echo ($site==$site_row['loccode']?'selected':'')?>><?php echo $site_row['locationname'];?></option>
	<?php } ?>
	</select></td>
	<td class="tabledata1"><strong>Requisition Number</strong></td>
	<td><div id="reqno">
	 <input name="requisition_no" type="text" id="requisition_no" value="<?php if($_SESSION['requisition_no']!=''){ echo $_SESSION['requisition_no'];} else { echo $Reqno; }?>" size="30" readonly="true" />
	</div></td>
  </tr>
<tr class="row1">
	<td class="tabledata1"><strong>Requisition By</strong></td>
    <td><select name="requition_by" id="requition_by" style="width:150px;">
			<option value="">Select</option>
			<?php $SQLEmp=DB_query("SELECT * FROM empmaster where CompanyId='".$_SESSION['ACGCompanyId']."' order by emp_name",$db);
			while($EmpId=DB_fetch_array($SQLEmp)){?>
			<option value="<?php echo $EmpId['id']; ?>" <?php echo ($requition_by==$EmpId['id']?'selected':'')?>><?php echo $EmpId['emp_name']; ?></option>
			<?php }?>
		</select></td>
 	<td class="tabledata1"><strong>Requisition Date</strong></td>
    <td><input type="text" name="date" id="date" class="datepicker" readonly="true" value=" <?php if(isset($_SESSION['date']))echo $_SESSION['date']; else echo ConvertSqlDate($prdate);?>"></td>
</tr>
<tr class="row1">
	<td class="tabledata1"><strong>Category</strong></td>
	<td colspan="3"><select name="category" id="category" style="width:150px;">
						<option value="">Select</option>
						<option value="PPC" <?php echo ($category=='PPC'?'selected':'')?>>PPC</option>
						<option value="NonPPC" <?php echo ($category=='NonPPC'?'selected':'')?>>Non PPC</option>
						<option value="Service" <?php echo ($category=='Service'?'selected':'')?>>Service</option>
					</select></td>
</tr>
<tr class="row1">
	<td class="tabledata1"><strong>Remarks</strong></th>
    <td colspan="3"><textarea name="remarks" style="width:100%" rows="3"><?php if($_SESSION['remarks']!=''){ echo $_SESSION['remarks'];} else { echo $comments; }?></textarea></td>
</tr>
<tr class="row1">
	<td colspan="4">
	<input type="button" name="profile_popup" value="Pick Items" id="profile_popup" class="search" />
	
    <input type="button" class="search"   onclick="deleteRowOrder('dataTable')" value="Delete" />       
	</td>
</tr>
<tr class="row1">
	<td colspan="4" style="font-size:12px; font-weight:bold;">Item Details</td>
</tr>
</table>
<div style="overflow:auto; width:100%;">    
<table border="1" cellpadding="3" cellspacing="0" width="100%" style="border:1px solid #333333;">
<tr bgcolor="#d4d4d4">
	     <td class="tabledata"><strong>Select</strong></td>
         <td class="tabledata"><strong>Item Code</strong></td>
		 <td class="tabledata"><strong></strong></td>
         <td class="tabledata"><strong>Item Name</strong></td>
	     <td class="tabledata"><strong>UOM</strong></td>		 
         <td class="tabledata"><strong>Current Stock</strong></td>
         <td class="tabledata"><strong>Requisition Quantity</strong></td>
         <td class="tabledata"><strong>Material Required Date</strong></td>
         <td class="tabledata"><strong>Remarks</strong></td>
</tr>
<tbody id="dataTable">
 <?php
  $k=0;
  $sql1=DB_query("select * from requisitionorderdetails where requsitionno='".$reqno."'",$db);
  while($row=DB_fetch_array($sql1))
  {
   ?>
<tr class="rowdata">
<td><input name="delitem[]" type="checkbox"  id="delitem" /></td>
<td><input type="text" name="item_code[]" id="itemcode_<?php echo $k; ?>"  style="width:100%" value="<?php echo $row['product_id']; ?>" readonly/></td>
<!--onkeyup="opendata(this.value,this.id)"-->
<td><input type="hidden" name="mst_itemid[]" id="itemid_<?php echo $k; ?>" value="<?php echo $row['mst_itemid']; ?>" /></td>
	<td><input type="text" name="item_name[]" id="item_name_<?php echo $k; ?>" style="width:100%" value="<?php echo $s->getField('item_master','id',$row['mst_itemid'],'item_name');?>" readonly/></td>
	<td><input type="text" name="Unit[]" id="Unit_<?php echo $k; ?>" value="<?php echo $row['uom']; ?>" style="width:100%" readonly /></td>
	<td><input type="text" name="stock_qty[]" id="stockqty_<?php echo $k; ?>"  value="<?php echo $row['stock_qty']; ?>" style="width:100%" readonly /></td>
	<td><input name="requisition_qty[]" type="text" id="requisitionqty_<?php echo $k; ?>" style="width:100%" onblur="CalrequiredQty(this.value,this.id)" value="<?php echo $row['requisition_qty']; ?>" <?php if($row['ApprovalStatus']=='3'){?>readonly <?php }?>/></td>
	<td><input name="material_required_by[]" id="materialrequiredby_<?php echo $k; ?>" type="text" style="width:100%" class="datepicker" value="<?php echo ConvertSqlDate($row['material_required_by']); ?>"/></td>
	<td><textarea name="textarea[]" style="width:100%" id="remark_<?php echo $k; ?>"><?php echo $row['Remarks'];?></textarea> </td>
</tr><?php $k++; }  ?>
</tbody>
</table>
</div>
<input type="text" name="hiditemcount" id="hiditemcount" value="<?php echo $k; ?>" />
  <input type="hidden" name="edit_cnt" id="edit_cnt" value="<?php echo $k; ?>" />

<table width="100%" cellpadding="3" cellspacing="0">
<tr class="row1">
	<td style="text-align:center;" colspan="15">
	<input type="submit" name="Update" value="Update" class="search" onclick="return validatedata();" />
              <input type="submit" name="Cancel" class="search" value="Cancel" />
	</td>
</tr>
</table>
</form>
</div>
</div>

<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");
		document.getElementById("date").value='';
		document.getElementById("date").focus();
        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");
	document.getElementById("site").focus();
	return false;
}


if(document.getElementById("date").value=='')
{
	alert("Please Select The Requisition Date First");
	document.getElementById("date").focus();
	return false;
}

if(document.getElementById("requition_by").value=='')
{
	alert("Please Select The Requisition By First");
	document.getElementById("requition_by").focus();
	return false;
}


if(document.getElementById("category").value=='')
{
	alert("Please Select The Category First");
	document.getElementById("category").focus();
	return false;
}
		
		
var count=document.getElementById("hiditemcount").value;	
	//if(count=='')
//	{
//	alert("Must Select Items");
//	return false;	
//	}
if(count==''){

	if(document.getElementById("itemcode").value=='')
	{
		alert("Please Select Items First");
		document.getElementById("itemcode").focus();
		return false;
	}
}else if(count=='1'){
	if(document.getElementById("requisitionqty").value=='' || document.getElementById("requisitionqty").value=='0')
	{
		alert("Please Select Requisition Qty");
		document.getElementById("requisitionqty").focus();
		return false;
	}

	if(document.getElementById("materialrequiredby").value=='')
	{
		alert("Please Select Date");
		document.getElementById("materialrequiredby").focus();
		return false;
	}
}

/*if(document.getElementById("requisitionqty").value=='' || document.getElementById("requisitionqty").value=='0')
{
	alert("Please Select Requisition 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("itemcode_"+i).value=='')
	{
	alert("Please Select Items First");
	return false;
	}*/
	if(document.getElementById("itemcode_"+i).value!=''){
	
	
		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("materialrequiredby_"+i).value=='')
		{
			alert("Please Select Material Required Date");
			document.getElementById("materialrequiredby_"+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;
			
		}
	 }else{
	    return true;
	 }
	
	}	
	
	}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 hidedata()
{
document.getElementById("lightbox2").style.display='none';
}

function addRow(tableID) 
{
	if(document)
	var table = document.getElementById(tableID);
	var rowCount = table.rows.length;
	var rowCount = table.rows.length;
	if(document.getElementById("hiditemcount").value=='')
	r =rowCount+1;
	else
	{
	var t=document.getElementById("edit_cnt").value;
	var t1=document.getElementById("hiditemcount").value;
	r=parseInt(t1);
	r1=parseInt(t1)+1;
	}
	var row = table.insertRow(rowCount);
	document.getElementById("hiditemcount").value=r1;
	document.getElementById("edit_cnt").value=r1;
	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;
	//alert(newcell.childNodes[0].type);
	switch(newcell.childNodes[0].type) 
	{
	case "text":
	newcell.childNodes[0].value = "";
	var nod_id = newcell.childNodes[0].id.split("_");
	if(!isNaN(nod_id[1]))
	newcell.childNodes[0].id = nod_id[0]+'_'+r;
	else
	newcell.childNodes[0].id = nod_id[0]+'_'+nod_id[1]+'_'+r;
	break;		
	case "hidden":
	newcell.childNodes[0].value = "";
	var nod_id = newcell.childNodes[0].id.split("_");
	if(!isNaN(nod_id[1]))
	newcell.childNodes[0].id = nod_id[0]+'_'+r;
	else
	newcell.childNodes[0].id = nod_id[0]+'_'+nod_id[1]+'_'+r;
	break;
	case "button":
	var nod_id = newcell.childNodes[0].id.split("_");
	if(!isNaN(nod_id[1]))
	newcell.childNodes[0].id = nod_id[0]+'_'+r;
	else
	newcell.childNodes[0].id = nod_id[0]+'_'+nod_id[1]+'_'+r;
	break;
	case "checkbox":
	newcell.childNodes[0].checked = false;
	var nod_id = newcell.childNodes[0].id.split("_");
	if(!isNaN(nod_id[1]))
	newcell.childNodes[0].id = nod_id[0]+'_'+r;
	else
	newcell.childNodes[0].id = nod_id[0]+'_'+nod_id[1]+'_'+r;
	break;
	case "select-one":
	newcell.childNodes[0].selectedIndex = 0;
	var nod_id = newcell.childNodes[0].id.split("_");
	if(!isNaN(nod_id[1]))
	newcell.childNodes[0].id = nod_id[0]+'_'+r;
	else
	newcell.childNodes[0].id = nod_id[0]+'_'+nod_id[1]+'_'+r;
	break;	
	}
		//document.getElementById("stock_perunit_"+rowCount).value=1;
	}
		//document.getElementById(countid).innerHTML=rowCount;

	}

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=(rowCount)-1;		}
	catch(e) 
	{
	alert(e);
	}
}
function opendata1(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?site3="+str+"&data="+id+"&site1="+sitecode+"&date="+date,true);
  xmlhttp.send();	
}

function getdata1(str)
{ 
	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)
	{
	//var data=xmlhttp.responseText.split("__");	 
	//document.getElementById("ItemListDiv").innerHTML=data[0];
	//document.getElementById("subcategory1").innerHTML=data[0];
	//alert(xmlhttp.responseText);
	document.getElementById("ItemListDiv").innerHTML=xmlhttp.responseText;
	}
	}
	 var site=document.getElementById("site").value;
	xmlhttp.open("GET","ajax_item_list_srch.php?keywd="+str+"&site="+site,true);
	xmlhttp.send();
	//xmlhttp.open("GET","getSort.php?s="+str+"&cat="+cat,true);
}	


</script>


