  <script type="text/javascript" src="calendar1.js"></script>
  
   <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.11/themes/base/jquery-ui.css" type="text/css" media="all" />
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" type="text/javascript"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.11/jquery-ui.min.js" type="text/javascript"></script>
<script>
<?php
$xx = unconverttounixslash($po_data['po_date']);
$tahun = substr($xx, 6, 8);
$bulan = substr($xx, 3, 2);
$hari = substr($xx, 0,2);
$dtp = $tahun.",".$bulan.",".$hari;
?>
function dateM($c){
$(function() {
var date = $('#datepicker-'+$c).datepicker({ 
dateFormat: 'dd/mm/yy', 
defaultDate: -1,
minDate: new Date(<?php echo $dtp;?>),
maxDate: new Date()
});
  //$( "#datepicker" ).datepicker({ minDate: 0 });
	
}); }
</script>
  
  <br>
  <table border=1 cellspacing=1 cellpadding=2 width=760 style="border: 1px inset;">
  <form action="controllers/inputController.php" method="post" name="doform" id="doform">
  <input name="job" id="job" type="hidden" value="">
  <input name="po_pkey" id="po_pkey" type="hidden" value="<?=$po_data['po_pkey'];?>">
  <input name="po_projectpkey" id="po_projectpkey" type="hidden" value="<?=$pubprojectpkey;?>">
  <input name="po_supplierpkey" id="po_supplierpkey" type="hidden" value="<?=$supplier_data['supplier_pkey'];?>">
  <input name="do_actions" id="do_actions" type="hidden" value="">
  <input name="do_pkeys" id="do_pkeys" type="hidden" value="">
  <input name="do_nos" id="do_nos" type="hidden" value="">
  <input name="do_dates" id="do_dates" type="hidden" value="">
  <input name="do_items" id="do_items" type="hidden" value="">
  <input name="do_quantities" id="do_quantities" type="hidden" value="">
  <input name="do_remarks" id="do_remarks" type="hidden" value="">
  <tr valign=top>
  <td valign=top width=150 class=formlabel>Supplier Name</td>
  <td valign=top width=305 class=formvalue><?=$supplier_data['supplier_name'];?>&nbsp;</td>
  <td valign=top width=150 class=formlabel>Expected Delivery</td>
  <td valign=top width=305 class=formvalue><?=unconverttounixslash($mro_data['mro_deliverydate']);?>&nbsp;</td>
  </tr>
  <tr valign=top>
  <td valign=top width=150 class=formlabel>PO No</td>
  <td valign=top width=305 class=formvalue><?=poidstr($po_data['po_no'],$po_data['po_revno']);?>&nbsp;</td>
  <td valign=top width=150 class=formlabel>PO Date</td>
  <td valign=top width=305 class=formvalue>
  <?php echo unconverttounixslash($po_data['po_date']);
 // echo "<br />".Date('d/m/y');
 // echo "<br />". $dtp;

  ?>
  &nbsp;</td>
  </tr>
  <tr valign=top>
  <td valign=top width=150 class=formlabel>MRO No</td>
  <td valign=top width=305 class=formvalue><?=mroidstr($mro_data['mro_id']);?>&nbsp;</td>
  <td valign=top width=150 class=formlabel>RFQ No</td>
  <td valign=top width=305 class=formvalue><?=itqidstr($itq_data['itq_rfqno']);?>&nbsp;</td>
  </tr>
  </table>

  <? $icount=0; ?>

  <? foreach($po_items as $myitem) { ?>

    <? $icount++; ?>
		<? $spanstyle = "none"; //apit ?>
    <? // get item total in dos
    $quantityreceived=$mydoModel->getquantity_received($pubitemid,$myitem['item_subsubtradepkey']);
    ?>

    <br>
    <table border=1 cellspacing=1 cellpadding=2 width=760 style="border: 1px inset;">
    <tr valign=top>
    <td valign=top width=40 class=listheader>No</td>
    <td valign=top width=340 class=listheader>Item</td>
    <td valign=top width=100 class=listheader>Unit</td>
    <td valign=top width=120 class=listheader>Qty</td>
    <td valign=top width=120 class=listheader>Qty Received</td>
    <td valign=top width=40 class=listheader>&nbsp;</td>
    </tr>
    <tr valign=top>
	
    <span style="cursor: pointer;" onClick="toggleDiv(<?=$icount;?>)"> 
    <td valign=top width=40 class=listnumber style="cursor: pointer;"><?=$icount;?></td>
    <td valign=top width=340 class=listcontent style="cursor: pointer;"><?=unescape($myitem['item_description']);?></td>
    <td valign=top width=100 class=listcontent style="cursor: pointer;"><?=$myitem['item_unit'];?></td>
    <td valign=top width=120 class=listcontentnumeric style="cursor: pointer;"><?=number_format($myitem['item_quantity'],4);?></td>
    <td valign=top width=120 class=listcontentnumeric style="cursor: pointer;"><?=number_format($quantityreceived,4);?></td>
    <td valign=top width=40 class=listcontent style="cursor: pointer;">&nbsp;</td>
    </span> 
    </tr>
    </table>

    <div id="div-<?=$icount;?>" style="display: none;">
    <input name="tag-<?=$icount;?>" id="tag-<?=$icount;?>" type="hidden" value="0">

    <br>
    <table border=1 cellspacing=1 cellpadding=2 width=760 style="border: 1px inset;">
    <tr valign=top>
    <td valign=top width=40 class=listsubheader>No</td>
    <td valign=top width=100 class=listsubheader>DO No</td>
   <!--  <td valign=top width=340 class=listsubheader>Remarks</td> -->
    <td valign=top width=120 class=listsubheader>Date Received</td>
    <td valign=top width=120 class=listsubheader>Qty</td>
    <td valign=top width=40 class=listsubheader>&nbsp;</td>
    </tr>

    <? // get dos
    $do_items = $mydoModel->getlist_dobypoitem($pubitemid, $myitem['item_subsubtradepkey']);
    $dcount=0;
    $dtotal=0;
    ?>

    <? foreach($do_items as $mydoitem) { ?>
		
				
      <?
			/*  apit */
      if ($mydoitem['item_pkey']!="") {
        $spanstyle = "block";
      } else {
				$spanstyle = "none";
      }
			/* apit e */
      ?>

      <? $dcount++; ?>
      <? $dtotal += $mydoitem['item_quantity']; ?>

      <tr valign=top>
			<input name="vis-<?=$icount;?>-<?=$dcount;?>" id="vis-<?=$icount;?>-<?=$dcount;?>" type="hidden" value="<?=$spanstyle;?>">
      <input name="item_pkey-<?=$icount;?>-<?=$dcount;?>" id="item_pkey-<?=$icount;?>-<?=$dcount;?>" type="hidden" value="<?=$mydoitem['item_pkey'];?>">
      <input name="item_dono-<?=$icount;?>-<?=$dcount;?>" id="item_dono-<?=$icount;?>-<?=$dcount;?>" type="hidden" value="<?=$mydoitem['item_dono'];?>">
      <input name="item_date-<?=$icount;?>-<?=$dcount;?>" id="item_date-<?=$icount;?>-<?=$dcount;?>" type="hidden" value="<?=unconverttounixslash($mydoitem['item_dodate']);?>">
      <input name="item_qty-<?=$icount;?>-<?=$dcount;?>" id="item_qty-<?=$icount;?>-<?=$dcount;?>" type="hidden" value="<?=$mydoitem['item_quantity'];?>">
      <input name="item_remarks-<?=$icount;?>-<?=$dcount;?>" id="item_remarks-<?=$icount;?>-<?=$dcount;?>" type="hidden" value="<?=$mydoitem['item_remarks'];?>">
      <td valign=top width=40 class=listnumber><?=$dcount;?></td>
      <td valign=top width=100 class=listcontent><?=$mydoitem['item_dono'];?></td>
    <!--  <td valign=top width=340 class=listcontent><?=$mydoitem['item_remarks'];?></td> -->
      <td valign=top width=120 class=listcontent><?=unconverttounixslash($mydoitem['item_dodate']);?></td>
      <td valign=top width=120 class=listcontentnumeric><?=number_format($mydoitem['item_quantity'],4);?></td>
      <td valign=top width=40 class=listcontent>
      <? if (($pubuseraccess['access_do_edit']==1)||($pubuserrole>=2)) { ?>
        <img src="gfx/options/b_edit.png" alt="" title="Edit" style="cursor: pointer;" onClick="editItem(<?=$icount;?>,<?=$dcount;?>)">
        <img src="gfx/options/b_drop.png" alt="" title="Delete" style="cursor: pointer;" onClick="deleteItem(<?=$icount;?>,<?=$dcount;?>)">
      <? } else { ?>
        &nbsp;
      <? } ?>
      </td>

      </tr>

    <? } ?>

    </table>

    <br>
    <table border=1 cellspacing=1 cellpadding=2 width=760 style="border: 1px inset;">
    <input name="do_action-<?=$icount;?>" id="do_action-<?=$icount;?>" type="hidden" value="1">
    <input name="do_item-<?=$icount;?>" id="do_item-<?=$icount;?>" type="hidden" value="<?=$myitem['item_pkey'];?>">
    <input name="do_total-<?=$icount;?>" id="do_total-<?=$icount;?>" type="hidden" value="<?=$myitem['item_quantity'];?>">
    <input name="do_received-<?=$icount;?>" id="do_received-<?=$icount;?>" type="hidden" value="<?=$quantityreceived;?>">
    <input name="do_pkey-<?=$icount;?>" id="do_pkey-<?=$icount;?>" type="hidden" value="">
    <input name="do_initqty-<?=$icount;?>" id="do_initqty-<?=$icount;?>" type="hidden" value="">
    
	<tr valign=top>
    <td valign=top width=40 class=listsubheader>No</td>
    <td valign=top width=100 class=listsubheader>DO No</td>
    <td valign=top width=340 class=listsubheader>Date Receive</td>
    <td valign=top width=120 class=listsubheader>Quantity</td>
    <td valign=top width=120 class=listsubheader>Balance Not Delivered</td>
    
    </tr>
	<td valign=top width=40 class=formlabel><span id="do_span-<?=$icount;?>"></span></td>
	
    <td valign=top width=100 class=formlabel><input name="do_no-<?=$icount;?>" id="do_no-<?=$icount;?>" type="text" value="" maxlength=40 style="width: 180px;">
    <img src="gfx/options/b_drop.png" title="Cancel" alt="Cancel" style="cursor: pointer;" onClick="cancelItem(<?=$icount;?>);"></td>
    
	<td valign=top width=100 class=formlabel>
	<!--
	<input name="do_date-<?=$icount;?>" id="do_date-<?=$icount;?>" type="text" value="" maxlength=10 style="width: 90px;" onKeypress="dateOnly();">
    <a href="javascript:cal<?=$icount;?>.popup();"><img src="images/icon/cal.gif" width="16" height="16" border="0" alt="Click to select date">
	-->
	<input name="do_date-<?=$icount;?>" id="datepicker-<?=$icount;?>" type="text" value="" maxlength=10 style="width: 90px;" onmousedown="dateM('<?=$icount;?>'); return false">	
	<!-- yus edit date button end -->
	</td>
    
	<td valign=top width=120 class=formlabel><input name="do_quantity-<?=$icount;?>" id="do_quantity-<?=$icount;?>" type="text" value="" maxlength=21 style="width: 180px;" onKeypress="decimalOnly();" onBlur="calcBalance(<?=$icount;?>);"></td>
    
	<td valign=top width=120 class=formlabel><input name="do_balance-<?=$icount;?>" id="do_balance-<?=$icount;?>" type="text" value="" readonly maxlength=21 style="width: 180px; background: #f0f0f0;"></td>
    
	<!-- hiden-->
	<input name="do_remarks-<?=$icount;?>" id="do_remarks-<?=$icount;?>" type="hidden" value="" style="width: 360px;">
	
	</tr>
	
	
    </table>

    </div>

  <? } ?>

  <table border=1 cellspacing=1 cellpadding=2 width=760 style="border: 1px inset;">
  <!--
  <input name="job" id="job" type="hidden" value="">
   -->
  </form>
  </table>

  <!--<button name="closepo" id="closepo" type="button" onClick="po_button_closepo('<?=$po_data['po_pkey'];?>');">&nbsp;<img src="gfx/options/b_add.png" class="actionbutton">&nbsp;&nbsp;Close PO&nbsp;</button>-->

  <script type="text/javascript">
    <? for($i=1;$i<=$icount;$i++) { ?>
      var cal<?=$i;?> = new calendar1(document.forms['doform'].elements['do_date-<?=$i;?>']);
      cal<?=$i;?>.year_scroll = true;
      cal<?=$i;?>.time_comp = false;
    <? } ?>
  </script>

  <script type="text/javascript">

  function toggleDiv(divno) {
    var objdiv = document.getElementById('div-'+divno);
    var objtag = document.getElementById('tag-'+divno);
    if (objtag.value=="0") {
      objdiv.style.cssText="display: block;";
      objtag.value="1";
    } else {
      objdiv.style.cssText="display: none;";
      objtag.value="0";
    }
  }

  /*
  function TS(v) {
    var val = v.toString();
    var result = "";
    var len = val.length;
    var dec = val.substr(len-3);
    val = val.substr(0,len-3);
    len -=3;
    while (len > 3){
     // result = ","+val.substr(len-3,3)+result;
	  result = ","+val.substr(len-3,3)+result;
      len -=3;
    }
    return val.substr(0,len)+result+dec;
  }
  */
  // testing
function TS(v)
{
  v += '';
  x = v.split('.');
  x1 = x[0];
  x2 = x.length > 1 ? '.' + x[1] : '';
  var rgx = /(\d+)(\d{3})/;
  while (rgx.test(x1)) {
    x1 = x1.replace(rgx, '$1' + ',' + '$2');
  }
  return x1 + x2;
}

  // end testing

  function calcBalance(icount) {
    var objdoaction = document.getElementById('do_action-'+icount);
    var objtotal = document.getElementById('do_total-'+icount);
    var objreceived = document.getElementById('do_received-'+icount);
    var objquantity = document.getElementById('do_quantity-'+icount);
    var objbalance = document.getElementById('do_balance-'+icount);
    var objinitqty = document.getElementById('do_initqty-'+icount);
    var mytotal = objtotal.value;
    var myreceived = objreceived.value;
    var myquantity = objquantity.value
    var mybalance = objbalance.value;
    var myaction = objdoaction.value;
    var myinitqty = objinitqty.value;
    if (myquantity!="") {
      if ((myaction=="1")||(myaction=="2")) {
        mytotal = mytotal.replace(/,/g,"");
        myreceived = myreceived.replace(/,/g,"");
        myquantity = myquantity.replace(/,/g,"");
        mybalance = mybalance.replace(/,/g,"");
        mytotal = parseFloat(mytotal)
        myreceived = parseFloat(myreceived)
        myquantity = parseFloat(myquantity)
        mybalance = parseFloat(mybalance)
        if (myaction=="1") {
          var mynewreceived = myreceived + myquantity;
        } else if (myaction=="2") {
          myinitqty = myinitqty.replace(/,/g,"");
          myinitqty = parseFloat(myinitqty)
          var mynewreceived = (myreceived + myquantity)-myinitqty;
        }
        var mybalance = mytotal - mynewreceived
        mybalance = mybalance.toFixed(4);
        objbalance.value = TS(mybalance);
        myquantity = myquantity.toFixed(4);
        objquantity.value = TS(myquantity);
      }
    } else {
      objbalance.value = "";
    }
  }

  function cancelItem(itemno) {
    var objdoaction = document.getElementById('do_action-'+itemno);
    var objdospan = document.getElementById('do_span-'+itemno);
    var objdopkey = document.getElementById('do_pkey-'+itemno);
    var objdono = document.getElementById('do_no-'+itemno);
    var objdodate = document.getElementById('do_date-'+itemno);
    //var objdoitem = document.getElementById('do_item-'+itemno);
    var objdoquantity = document.getElementById('do_quantity-'+itemno);
    var objdobalance = document.getElementById('do_balance-'+itemno);
    var objdoremarks = document.getElementById('do_remarks-'+itemno);
    objdoaction.value="1";
    objdospan.innerHTML="Add DO";
    objdopkey.value="";
    objdono.value="";
    objdodate.value="";
    //objdoitem.value="";
    objdoquantity.value="";
    objdobalance.value="";
    objdoremarks.value="";
    objdono.readonly=false;
    objdodate.readonly=false;
    objdoquantity.readonly=false;
    objdoremarks.readonly=false;
  }

  function editItem(itemno,dono) {
    var objdoaction = document.getElementById('do_action-'+itemno);
    var objdospan = document.getElementById('do_span-'+itemno);
    var objdopkey = document.getElementById('do_pkey-'+itemno);
    var objdono = document.getElementById('do_no-'+itemno);
    var objdodate = document.getElementById('do_date-'+itemno);
    var objdoquantity = document.getElementById('do_quantity-'+itemno);
    var objdobalance = document.getElementById('do_balance-'+itemno);
    var objdoremarks = document.getElementById('do_remarks-'+itemno);
    var objdoinitqty = document.getElementById('do_initqty-'+itemno);
    // get from do list..
    var objitempkey = document.getElementById('item_pkey-'+itemno+'-'+dono);
    var objitemdono = document.getElementById('item_dono-'+itemno+'-'+dono);
    var objitemdate = document.getElementById('item_date-'+itemno+'-'+dono);
    var objitemqty = document.getElementById('item_qty-'+itemno+'-'+dono);
    var objitemremarks = document.getElementById('item_remarks-'+itemno+'-'+dono);
    objdoaction.value="2";
    objdospan.innerHTML="Edit DO";
    objdopkey.value=objitempkey.value;
    objdono.value=objitemdono.value;
    objdodate.value=objitemdate.value;
    objdoquantity.value=objitemqty.value;
    objdobalance.value="";
    objdoremarks.value=objitemremarks.value;
    objdoinitqty.value=objitemqty.value;
    objdono.readonly=false;
    objdodate.readonly=false;
    objdoquantity.readonly=false;
    objdoremarks.readonly=false;
  }

  function deleteItem(itemno,dono) {
    
	var objdoaction = document.getElementById('do_action-'+itemno);
    var objdospan = document.getElementById('do_span-'+itemno);
    var objdopkey = document.getElementById('do_pkey-'+itemno);
    var objdono = document.getElementById('do_no-'+itemno);
    var objdodate = document.getElementById('do_date-'+itemno);
    var objdoquantity = document.getElementById('do_quantity-'+itemno);
    var objdobalance = document.getElementById('do_balance-'+itemno);
    var objdoremarks = document.getElementById('do_remarks-'+itemno);
    // get from do list..
    var objitempkey = document.getElementById('item_pkey-'+itemno+'-'+dono);
    var objitemdono = document.getElementById('item_dono-'+itemno+'-'+dono);
    var objitemdate = document.getElementById('item_date-'+itemno+'-'+dono);
    var objitemqty = document.getElementById('item_qty-'+itemno+'-'+dono);
    var objitemremarks = document.getElementById('item_remarks-'+itemno+'-'+dono);
    objdoaction.value="3";
    objdospan.innerHTML="Delete DO";
    objdopkey.value=objitempkey.value;
    objdono.value=objitemdono.value;
    objdodate.value=objitemdate.value;
    objdoquantity.value=objitemqty.value;
    objdobalance.value="";
    objdoremarks.value=objitemremarks.value;
    objdono.readonly=true;
    objdodate.readonly=true;
    objdoquantity.readonly=true;
    objdoremarks.readonly=true;
	
    // call delete..
    do_button_deletedos(itemno);
  }

  function validateForm() {
    return true;
  }

  
  function submitForm() {
  
    var doactions="";
    var dopkeys="";
    var donos="";
    var dodates="";
    var doitems="";
    var doquantities="";
    var doremarks="";
    var objdoactions = document.getElementById('do_actions');
    var objdopkeys = document.getElementById('do_pkeys');
    var objdonos = document.getElementById('do_nos');
    var objdodates = document.getElementById('do_dates');
    var objdoitems = document.getElementById('do_items');
    var objdoquantities = document.getElementById('do_quantities');
    var objdoremarks = document.getElementById('do_remarks');

	for (i=1;i<=100;i++) {
      var objdono = document.getElementById('do_no-'+i);
      if (objdono!=null) {
        if (objdono.value!="") {
          donos = donos+"|"+objdono.value;
          var objdoaction = document.getElementById('do_action-'+i);
          doactions = doactions+"|"+objdoaction.value;
          var objdopkey = document.getElementById('do_pkey-'+i);
          dopkeys = dopkeys+"|"+objdopkey.value;
          var objdodate = document.getElementById('do_date-'+i);
          dodates = dodates+"|"+objdodate.value;
          var objdoitem = document.getElementById('do_item-'+i);
          doitems = doitems+"|"+objdoitem.value;
          var objdoqty = document.getElementById('do_quantity-'+i);
          doquantities = doquantities+"|"+objdoqty.value;
          var objdorem = document.getElementById('do_remarks-'+i);
          doremarks = doremarks+"|"+objdorem.value;
        }
      }
    } 
    objdoactions.value = doactions.substr(1);
    objdopkeys.value = dopkeys.substr(1);
    objdonos.value = donos.substr(1);
    objdodates.value = dodates.substr(1);
    objdoitems.value = doitems.substr(1);
    objdoquantities.value = doquantities.substr(1);
    objdoremarks.value = doremarks.substr(1);
    return true;
  }
  
  
  </script>
