  <script type="text/javascript" src="calendar1.js"></script>
  <br>
  <table border=1 cellspacing=1 cellpadding=2 width=760 style="border: 1px inset;">
  <form action="controllers/inputController.php" method="post" name="invoiceform" id="invoiceform">
  <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="invoice_actions" id="invoice_actions" type="hidden" value="">
  <input name="invoice_pkeys" id="invoice_pkeys" type="hidden" value="">
  <input name="invoice_nos" id="invoice_nos" type="hidden" value="">
  <input name="invoice_dates" id="invoice_dates" type="hidden" value="">
  <input name="invoice_items" id="invoice_items" type="hidden" value="">
  <input name="invoice_quantities" id="invoice_quantities" type="hidden" value="">
  <input name="invoice_rates" id="invoice_rates" type="hidden" value="">
  <input name="invoice_debitnotes" id="invoice_debitnotes" 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>PO Amount</td>
  <td valign=top width=305 class=formvalue><?=number_format($poamount,2);?>&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><?=unconverttounixslash($po_data['po_date']);?>&nbsp;</td>
  </tr>
  </table>

  <? $icount=0; ?>

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

    <? $icount++; ?>

    <? // get item total in dos
    $quantityreceived=$mydoModel->getquantity_received($pubitemid,$myitem['item_subsubtradepkey']);
    $mroamount=($myitem['item_quantity']*$myitem['item_rate']);
    ?>

    <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=120 class=listheader>Qty</td>
    <td valign=top width=100 class=listheader>Rate</td>
    <td valign=top width=120 class=listheader>Amount</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=120 class=listcontentnumeric style="cursor: pointer;"><?=number_format($myitem['item_quantity'],4);?></td>
    <td valign=top width=100 class=listcontentnumeric style="cursor: pointer;"><?=number_format($myitem['item_rate'],4);?></td>
    <td valign=top width=120 class=listcontentnumeric style="cursor: pointer;"><?=number_format($mroamount,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>
   

    <br>
	
	<!--
	//////////////////////////////////////////////////////////////////////////////////
	-->
    <table border=1 cellspacing=1 cellpadding=2 width=920 style="border: 1px inset;">
    <input name="invoice_mroamount-<?=$icount;?>" id="invoice_mroamount-<?=$icount;?>" type="hidden" value="<?=$mroamount;?>">
    <input name="invoice_received-<?=$icount;?>" id="invoice_received-<?=$icount;?>" type="hidden" value="<?=$quantityreceived;?>">
    <tr valign=top>
    <td valign=top width=40 class=formlabel>No</td>
    <td valign=top width=140 class=formlabel>Invoice No</td>
    <td valign=top width=200 class=formlabel>Invoice Date</td>
    <td valign=top width=120 class=formlabel>Qty</td>
    <td valign=top width=100 class=formlabel>Rate</td>
    <td valign=top width=120 class=formlabel>Amount</td>
    <td valign=top width=40 class=formlabel>&nbsp;</td>
    <td valign=top width=120 class=formlabel>Debit Note</td>
    <td valign=top width=40 class=formlabel>&nbsp;</td>
    </tr>

    <?
    $inv_items = $myinvoiceModel->getlist_invoicebypoitem($pubitemid, $supplier_data['supplier_pkey'], $myitem['item_subsubtradepkey']);
    $invtotal = 0;
    $invmax = 0;
    ?>

    <? for ($invcount=1;$invcount<=20;$invcount++) { ?>

      <? $invindex = $invcount-1; ?>
      <? $spanstyle = "none"; ?>

      <input name="invoice_prevtotal-<?=$icount;?>-<?=$invcount;?>" id="invoice_prevtotal-<?=$icount;?>-<?=$invcount;?>" type="hidden" value="<?=$invtotal;?>">

      <?
      if ($inv_items[$invindex]['item_pkey']!="") {
        $spanstyle = "block";
        $invmax = $invcount;
        $invamount = ($inv_items[$invindex]['item_quantity']*$inv_items[$invindex]['item_rate']);
        $invtotal += $invamount;
        //$invdebitnote = $invtotal - $mroamount;
        $invdebitnote = 0;
      } else {
        $invamount = 0;
        $invdebitnote = 0;
      }
      ?>

      <tr id="invspan-<?=$icount;?>-<?=$invcount;?>" valign=top style="display: <?=$spanstyle;?>;">
      <input name="vis-<?=$icount;?>-<?=$invcount;?>" id="vis-<?=$icount;?>-<?=$invcount;?>" type="hidden" value="<?=$spanstyle;?>">
      <td valign=top width=40 class=listnumber><?=$invcount;?></td>
      <td valign=top width=140 class=listcontent>
      <input name="invoice_action-<?=$icount;?>-<?=$invcount;?>" id="invoice_action-<?=$icount;?>-<?=$invcount;?>" type="hidden" value="1">
      <input name="invoice_item-<?=$icount;?>-<?=$invcount;?>" id="invoice_item-<?=$icount;?>-<?=$invcount;?>" type="hidden" value="<?=$myitem['item_pkey'];?>">
      <input name="invoice_pkey-<?=$icount;?>-<?=$invcount;?>" id="invoice_pkey-<?=$icount;?>-<?=$invcount;?>" type="hidden" value="<?=$inv_items[$invindex]['item_pkey'];?>">
      <input name="invoice_no-<?=$icount;?>-<?=$invcount;?>" id="invoice_no-<?=$icount;?>-<?=$invcount;?>" type="text" value="<?=$inv_items[$invindex]['item_invoiceno'];?>" maxlength=40 style="width: 120px;">
      </td>
      <td valign=top width=200 class=listcontent>
      <input name="invoice_date-<?=$icount;?>-<?=$invcount;?>" id="invoice_date-<?=$icount;?>-<?=$invcount;?>" type="text" value="<?=unconverttounixslash($inv_items[$invindex]['item_invoicedate']);?>" maxlength=10 style="width: 90px;" onKeypress="dateOnly();">
      <a href="javascript:cal<?=$icount;?><?=$invcount;?>.popup();"><img src="images/icon/cal.gif" width="16" height="16" border="0" alt="Click to select date"></a>
      </td>
      <td valign=top width=120 class=listcontentnumeric>
      <input name="invoice_quantity-<?=$icount;?>-<?=$invcount;?>" id="invoice_quantity-<?=$icount;?>-<?=$invcount;?>" type="text" value="<?=number_format($inv_items[$invindex]['item_quantity'],4);?>" maxlength=21 style="width: 110px; text-align: right;" onKeypress="decimalOnly();" onBlur="calcAmount(<?=$icount;?>,<?=$invcount;?>);">
      </td>
      <td valign=top width=100 class=listcontentnumeric>
      <input name="invoice_rate-<?=$icount;?>-<?=$invcount;?>" id="invoice_rate-<?=$icount;?>-<?=$invcount;?>" type="text" value="<?=number_format($inv_items[$invindex]['item_rate'],2);?>" maxlength=21 style="width: 90px; text-align: right;" onKeypress="decimalOnly();" onBlur="calcAmount(<?=$icount;?>,<?=$invcount;?>);">
      </td>
      <td valign=top width=120 class=listcontentnumeric>
      <input name="invoice_amount-<?=$icount;?>-<?=$invcount;?>" id="invoice_amount-<?=$icount;?>-<?=$invcount;?>" type="text" value="<?=number_format($invamount,2);?>" maxlength=21 readonly style="width: 110px; text-align: right;">
      </td>
      <td valign=top width=40 class=listcontent>&nbsp;</td>
      <td valign=top width=120 class=listcontentnumeric>
      <input name="invoice_debitnote-<?=$icount;?>-<?=$invcount;?>" id="invoice_debitnote-<?=$icount;?>-<?=$invcount;?>" type="text" value="<?=number_format($invdebitnote,2);?>" maxlength=21 readonly style="width: 110px;">
      </td>
      <td valign=top width=40 class=listcontent>
      <? // if ($inv_items[$invindex]['item_pkey']!="") { ?>
        <img src="gfx/options/b_drop.png" title="Remove" alt="Remove" style="cursor: pointer;" onClick="removeItem(<?=$icount;?>,<?=$invcount;?>);">
      <? // } ?>
      &nbsp;
      </td>
      </tr>

    <? } ?>

    <input name="invoice_total-<?=$icount;?>" id="invoice_total-<?=$icount;?>" type="hidden" value="<?=$invtotal;?>">
    </table>

    <button name="additem-<?=$icount;?>" id="additem-<?=$icount;?>" type="button" onClick="addItem(<?=$icount;?>);"><img src="gfx/options/b_add.png" class="actionbutton">&nbsp;Add Invoice</button>

    </div>

  <? } ?>

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


  <script type="text/javascript">
    <? for($i=1;$i<=$icount;$i++) { ?>
      <? for($j=1;$j<=20;$j++) { ?>
        var cal<?=$i;?><?=$j;?> = new calendar1(document.forms['invoiceform'].elements['invoice_date-<?=$i;?>-<?=$j;?>']);
        cal<?=$i;?><?=$j;?>.year_scroll = true;
        cal<?=$i;?><?=$j;?>.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 addItem(icount) {
    var isel = 1;
    for (i=1;i<=20;i++) {
      var objvis = document.getElementById('vis-'+icount+'-'+i);
      if (objvis.value=="none") {
        isel = i;
        objvis.value = "block";
        if (i==20) {
          var objbutton = document.getElementById('additem-'+icount);
          objbutton.disabled = true;
        }
        break;
      }
    }
    var objtr = document.getElementById('invspan-'+icount+'-'+isel);
    objtr.style.cssText = "display: block;";
  }

  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;
      len -=3;
    }
    return val.substr(0,len)+result+dec;
  }

  function calcAmount(icount,invno) {
    nextno = invno+1;
    var objmrototal = document.getElementById('invoice_mroamount-'+icount);
    var objprevtotal = document.getElementById('invoice_prevtotal-'+icount+'-'+invno);
    var objnexttotal = document.getElementById('invoice_prevtotal-'+icount+'-'+nextno);
    var objquantity = document.getElementById('invoice_quantity-'+icount+'-'+invno);
    var objrate = document.getElementById('invoice_rate-'+icount+'-'+invno);
    var objamount = document.getElementById('invoice_amount-'+icount+'-'+invno);
    var objdebitnote = document.getElementById('invoice_debitnote-'+icount+'-'+invno);
    var mymrototal = objmrototal.value;
    var mytotal = objprevtotal.value;
    var myquantity = objquantity.value
    var myrate = objrate.value;
    var myamount = objamount.value;
    var mydebitnote = objdebitnote.value;
    if (myquantity!="") {
      mymrototal = mymrototal.replace(/,/g,"");
      mytotal = mytotal.replace(/,/g,"");
      myquantity = myquantity.replace(/,/g,"");
      myrate = myrate.replace(/,/g,"");
      mymrototal = parseFloat(mymrototal)
      mytotal = parseFloat(mytotal)
      myquantity = parseFloat(myquantity)
      myrate = parseFloat(myrate)
      var mynewamount = myquantity * myrate;
      var mynewtotal = mytotal + mynewamount;
      var mynewdebitnote = mynewtotal - mymrototal;
      //alert(mytotal);
      //alert(mynewtotal);
      //alert(mymrototal);
      objnexttotal.value = mynewtotal;
      myamount = mynewamount.toFixed(2);
      objamount.value = TS(myamount);
      if (mydebitnote>0) {
        mydebitnote = mynewdebitnote.toFixed(2);
        objdebitnote.value = TS(mydebitnote);
      } else {
        mynewdebitnote = 0;
        mydebitnote = mynewdebitnote.toFixed(2);
        objdebitnote.value = TS(mydebitnote);
      }
    } else {
      objamount.value = "";
      objdebitnote.value = "";
    }
  }

  function removeItem(itemno,invno) {
    //var objinvoicepkey = document.getElementById('invoice_pkey-'+itemno+'-'+invno);
    var objinvoiceno = document.getElementById('invoice_no-'+itemno+'-'+invno);
    var objinvoicedate = document.getElementById('invoice_date-'+itemno+'-'+invno);
    var objinvoicequantity = document.getElementById('invoice_quantity-'+itemno+'-'+invno);
    var objinvoicerate = document.getElementById('invoice_rate-'+itemno+'-'+invno);
    var objinvoiceamount = document.getElementById('invoice_amount-'+itemno+'-'+invno);
    var objinvoicedebitnote = document.getElementById('invoice_debitnote-'+itemno+'-'+invno);
    //objinvoicepkey.value="";
    objinvoiceno.value="";
    objinvoicedate.value="";
    objinvoicequantity.value="";
    objinvoicerate.value="";
    objinvoiceamount.value="";
    objinvoicedebitnote.value="";
    // move up
    // NOTE: cannot do shift items up because it will interfere with deletions
    /*
    var k=0;
    for (j=invno;j<=19;j++) {
      var objinvoiceno = document.getElementById('invoice_no-'+itemno+'-'+j);
      if (objinvoiceno!=null) {
	k=j+1;
        var objinvoicedate = document.getElementById('invoice_date-'+itemno+'-'+j);
        var objinvoicequantity = document.getElementById('invoice_quantity-'+itemno+'-'+j);
        var objinvoicerate = document.getElementById('invoice_rate-'+itemno+'-'+j);
        var objinvoiceamount = document.getElementById('invoice_amount-'+itemno+'-'+j);
        var objinvoicedebitnote = document.getElementById('invoice_debitnote-'+itemno+'-'+j);
        //objinvoicepkey.value="";
        objinvoiceno.value="";
        objinvoicedate.value="";
        objinvoicequantity.value="";
        objinvoicerate.value="";
        objinvoiceamount.value="";
        objinvoicedebitnote.value="";
      }
    }
    */
  }

  function validateForm() {
    return true;
  }

  function submitForm() {
    var invoiceactions="";
    var invoicepkeys="";
    var invoicenos="";
    var invoicedates="";
    var invoiceitems="";
    var invoicequantities="";
    var invoicerates="";
    var invoicedebitnotes="";
    var objinvoiceactions = document.getElementById('invoice_actions');
    var objinvoicepkeys = document.getElementById('invoice_pkeys');
    var objinvoicenos = document.getElementById('invoice_nos');
    var objinvoicedates = document.getElementById('invoice_dates');
    var objinvoiceitems = document.getElementById('invoice_items');
    var objinvoicequantities = document.getElementById('invoice_quantities');
    var objinvoicerates = document.getElementById('invoice_rates');
    var objinvoicedebitnotes = document.getElementById('invoice_debitnotes');
    for (i=1;i<=100;i++) {
     for (j=1;j<=20;j++) {
      var objinvoiceno = document.getElementById('invoice_no-'+i+'-'+j);
      if (objinvoiceno!=null) {
        //if (objinvoiceno.value!="") {
          invoicenos = invoicenos+"|"+objinvoiceno.value;
          var objinvoiceaction = document.getElementById('invoice_action-'+i+'-'+j);
          invoiceactions = invoiceactions+"|"+objinvoiceaction.value;
          var objinvoicepkey = document.getElementById('invoice_pkey-'+i+'-'+j);
          invoicepkeys = invoicepkeys+"|"+objinvoicepkey.value;
          var objinvoicedate = document.getElementById('invoice_date-'+i+'-'+j);
          invoicedates = invoicedates+"|"+objinvoicedate.value;
          var objinvoiceitem = document.getElementById('invoice_item-'+i+'-'+j);
          invoiceitems = invoiceitems+"|"+objinvoiceitem.value;
          var objinvoiceqty = document.getElementById('invoice_quantity-'+i+'-'+j);
          invoicequantities = invoicequantities+"|"+objinvoiceqty.value;
          var objinvoicerate = document.getElementById('invoice_rate-'+i+'-'+j);
          invoicerates = invoicerates+"|"+objinvoicerate.value;
          var objinvoicedebitnote = document.getElementById('invoice_debitnote-'+i+'-'+j);
          invoicedebitnotes = invoicedebitnotes+"|"+objinvoicedebitnote.value;
        //}
      }
     }
    }
    objinvoiceactions.value = invoiceactions.substr(1);
    objinvoicepkeys.value = invoicepkeys.substr(1);
    objinvoicenos.value = invoicenos.substr(1);
    objinvoicedates.value = invoicedates.substr(1);
    objinvoiceitems.value = invoiceitems.substr(1);
    objinvoicequantities.value = invoicequantities.substr(1);
    objinvoicerates.value = invoicerates.substr(1);
    objinvoicedebitnotes.value = invoicedebitnotes.substr(1);
    return true;
  }

  </script>
