<style type="text/css">
    .sambox{
        display:none;
    }
    .amount{
        text-align: right;
    }

    .quantity {
        text-align: center;
    }

    .btn-group button {
        height:30px; }

    #content .wrap #grid_content_1 .row-fluid.scRow .row-fluid.scRow .span12.scCol #grid_block_5 .content.np #listDetails tbody #trInfos td .row-fluid .controls-row .span3 {
        font-size: 11px;
        color: #000;
        font-weight: bold;
    }
    .test {
        font-size: 10px;
    }
    .span3 {
        font-size: 10px;
    }
    tbody {
        font-size: 10px;
    }
    #content .wrap #grid_content_1 .row-fluid.scRow .row-fluid.scRow .span12.scCol #grid_block_6 .content.np #listCharge tbody #trInfosCharge td .row-fluid .controls-row .span3 {
        font-weight: bold;
    }
</style>
<script type="application/javascript">
$(document).ready(function() {
	$('select').select2();
	jQuery('.datepicker2').datepicker({
			dateFormat : 'dd-mm-yy',
			changeMonth : true,
			changeYear : true
			});
    $("table.editable").on("click",".edit",function(){
    	getTotal();
        var eRow   = $(this).parents('tr');
        var eState = $(this).attr('data-state');
        if(eState == null){
            $(this).html('<span class="icon-ok-circle">');
            eState = 1;
        }
        var totalAmount = accounting.unformat($('#totalAmount').val());
        eRow.find('td').each(function(){            
            if(eState == 1){
                var eContent = $(this).html(); 
//				alert(eContent);
//				alert($(this).attr('id'));
				if($(this).attr('id') == 'unitPrice'){     
                	if(eContent.indexOf('<') < 0){
                    	$(this).addClass("editing").html('<input class="tar" type="text" value="' + eContent + '" disabled/>');                    
                	}
				}else if($(this).attr('id') == 'quantitySent'){
                	if(eContent.indexOf('<') < 0){
                    	$(this).addClass("editing").html('<input class="tar" type="text" value="' + eContent + '" onkeyup="calculate_amount(this)"/>');                    
                	}
				}else if($(this).attr('id') == 'amount'){
                	if(eContent.indexOf('<') < 0){
                    	$(this).addClass("editing").html('<input class="tar" type="text" value="' + accounting.formatNumber(eContent) + '" disabled/>');                    
                	}
				}
            }
            if(eState == 2){
                var eContent = $(this).find('input').val();                                
                if(eContent != null && !$(this).hasClass('noCont')){
                    $(this).removeClass("editing").html(accounting.formatNumber(eContent,2));
                    // Here your ajax action after Save button pressed
			        var fldid = $('td:first', $(this).parents('tr')).text(); 
			        var ID = $('td:eq(1)', $(this).parents('tr')).text(); 
					var deliveryID = $("#deliveryID").val();
					var fieldname = $(this).attr('id');
                	var newContent = $(this).val();
					var urls = "<?php echo base_url()?>sotransaction/doDetails_update";
					var factor = eRow.find('#factor').val();
//			 		alert(fieldname);
					postData = {
						'deliveryID' : deliveryID,
						'ID' : ID,
						'fldid' : fldid,
						'fieldname' : fieldname,
						'eContent' : accounting.unformat(eContent),
						'totalAmount': accounting.unformat($('#totalAmount').val())
					};
 					$.ajax({
  						type: "POST",
  						dataType: "json",
						url: urls,
						data: postData ,
	  					async: false,
						success: function(content){
						}
					});
					/* $.ajax({
  						type: "POST",
  						url: "menu_save?id=" + fldid,
  						success: function(){
						alert('Data has been saved...');
					}, 
		});   */    // Here your ajax action after delete confirmed
					
                }
            }
        });
        
        if(eState == 1) 
            $(this).attr('data-state','2');
        else{
            $(this).removeAttr('data-state');
            $(this).html('<span class="i-pen">');
        }
    });
});
function selectCustomer(){
		$.ajax({
			type: "POST",
        	url: '<?php echo base_url();?>sotransaction/customerSearch',
			dataType:"json",
			success: function(content){
				if(content.status == "success") {
					var items = [];
//					items.push('<option value=""><?php echo $this->lang->line('pleaseselect')?></option>');
					for ( var i = 0; i < content.message.length; i++) {
					items.push('<option value="'+content.message[i].ID+'">'
//						+ content.message[i].code + ' [ '+ content.message[i].desc + ' ]</option>');
						+ content.message[i].customerName + ' [ ' + content.message[i].accountCode + ' ]</option>');
					}
//					for (var x = startrows; x < tablerows ; x++) {
 						var rowId = "#customerID";
						jQuery(rowId).empty();
						jQuery(rowId).append(items.join('</br>'));
//					}
				} else {
					$("#error").html('<p>'+content.message+'</p>');
				}
					return false;
			}
		});
}
function refreshLocation(){
 $.ajax({
	type: "POST",
	url: "<?php echo base_url() ?>purchasetransaction/refreshLocation",
	success: function(data) {
	
	   var resul = eval ("(" + data + ")"); 
	   var n=resul.records.length;
	   var i=0;
	   var opt='' +
		'<option></option>';
		for (i=0;i<n;i++)
			opt=opt + '<option value="' + resul.records[i].id +'">' + resul.records[i].location + '</option>';
		$('#location').empty();
		$('#location').append(opt);
	}
   });	
}
function selectSO(obj){
//						alert($("#salesorder option:selected").val());
	var customerID = obj.value;
			postData = {
				customerID : customerID
			};
	if($("#salesorder option:selected").val() == 0){ 
		$.ajax({
			type: "POST",
        	url: '<?php echo base_url();?>sotransaction/SalesOrderSearch',
			dataType:"json",
			data: postData,
			success: function(content){
				if(content.status == "success") {
					var items = [];
					items.push('<option value=""><?php echo $this->lang->line('pleaseselect')?></option>');
					for ( var i = 0; i < content.message.length; i++) {
					items.push('<option value="'+content.message[i].ID+'">'
//						+ content.message[i].code + ' [ '+ content.message[i].desc + ' ]</option>');
						+ content.message[i].OrderNo + '</option>');
					}
//					for (var x = startrows; x < tablerows ; x++) {
 						var rowId = "#salesorder";
						jQuery(rowId).empty();
						jQuery(rowId).append(items.join('</br>'));
//					}
				} else {
					$("#error").html('<p>'+content.message+'</p>');
				}
						$('#currencyID').val(content.message1.currencyID).change();
						$('#exchangeRate').val(parseFloat(content.exchangeRate).toFixed(5));
					return false;
			}
		});
	}
	$("#grid_block_5").show();
	return;
}
function refreshEmployee(){
		$.ajax({
			type: "POST",
        	url: '<?php echo base_url();?>sotransaction/employeeSearch',
			dataType:"json",
			success: function(content){
				if(content.status == "success") {
					var items = [];
//					items.push('<option value=""><?php echo $this->lang->line('pleaseselect')?></option>');
					for ( var i = 0; i < content.message.length; i++) {
					items.push('<option value="'+content.message[i].ID+'">'
//						+ content.message[i].code + ' [ '+ content.message[i].desc + ' ]</option>');
						+ content.message[i].employeeName + '</option>');
					}
//					for (var x = startrows; x < tablerows ; x++) {
 						var rowId = "#salesPerson";
						jQuery(rowId).empty();
						jQuery(rowId).append(items.join('</br>'));
//					}
				} else {
					$("#error").html('<p>'+content.message+'</p>');
				}
					return false;
			}
		});
}
function addRow(){
	/*if($("#bill").val() != ''){
		var tablerows = $("#bill").val();
	}else{*/
		var tablerows = $('#oTable2 tbody tr').length+1;
	//}
    var rowToInsert = '';
    rowToInsert = "<td class='span2'><select id='itemID"+ tablerows +"' style='width: 200px;' onChange='selectDesc(this)'></select></td>";
    rowToInsertUM = "<td id='unitOfMeasureID' class='span1'><input type='text' placeholder='UM' /></td>";
    $("#oTable2 tbody").append(
		"<tr>" +
        "<td class='span1'><input type='text' disabled='disabled' value='"+tablerows+"' name='lineNo' class='span12'/>"+
		rowToInsert +
        "<td class='span4'><input type='text' name='description' class='span12'/>"+
		rowToInsertUM +
        "<td class='span2 tar'><input type='text' name='unitprice' class='span12 tar' onchange='calculate_amount1(this)' onkeyup='calculate_amount1(this)' value='0'/></td>"+
        "<td class='span2 tar'><input type='text' name='qtySent' class='span12 tar'  onkeyup='calculate_amount1(this)' value='0'/></td>"+
        "<td class='span2 tar'><input type='text' name='amount' class='span12 tar' disabled/></td>"+
        "<td class='tac'>" + 
		"<button class='btn-icon btn-mini btn-success-form' title='<?php echo $this->lang->line('save') ?>' onClick='saveDetails(this)'><span class='i-checkmark'></button><button class='btn-icon btn-mini btn-danger-form remove' title='<?php echo $this->lang->line('cancel') ?>' onClick='cancelRow(this)'><span class='i-cross'></button></td>"+
        "</tr>");
	$('select').select2({
		placeholder: 'Please',
	});
  	selectoption(tablerows);
  	$("#bill").val(parseInt(tablerows) + 1);
  	selectUnitmeasure(tablerows);
}
function cancelRow(obj){
    var eRow   = $(obj).parents('tr');
	var totalAmount = $("#totalAmount").val();
	var amount = parseFloat($('td:eq(6)', $(obj).parents('tr')).find("input").val());
	$("#totalAmount").val((parseFloat(totalAmount) - parseFloat(amount)).toFixed(2));
	eRow.remove();
	$("#addLine").show(); 
        getTotal ();
}

function saveDetails(obj){
    var eRow   = $(obj).parents('tr');
	var deliveryID = $("#deliveryID").val();
	var customerID = $("#customerID option:selected").val();
	var itemID = $('td:eq(1) option:selected', $(obj).parents('tr')).val();
	var lineNo = $('td:eq(0)', $(obj).parents('tr')).find("input").val();
	var description = $('td:eq(2)', $(obj).parents('tr')).find("input").val();
	var standUM = $('td:eq(3) select', $(obj).parents('tr')).select2().find(':selected').data('baseum');
	var umID = $('td:eq(3) option:selected', $(obj).parents('tr')).val();
	var factor = $('td:eq(3) select', $(obj).parents('tr')).select2().find(':selected').data('factor');
    var unitm = $('td:eq(3) select', $(obj).parents('tr')).select2().find(':selected').data('unitm');
	var unitPrice1 = $('td:eq(4)', $(obj).parents('tr')).find("input").val();
	var qtySent1 = $('td:eq(5)', $(obj).parents('tr')).find("input").val();	

	/*if(factor != 0){
        var unitPrice = unitPrice1/factor;
        var qtySent = (qtySent1/unitm) * factor;
    }else{
        var qtySent = qtySent1;
        var unitPrice = unitPrice1;
    }*/


	var amount = accounting.unformat($('td:eq(6)', $(obj).parents('tr')).find("input").val());

	postData = {
		'ID' : deliveryID,
		'lineNo' : lineNo,
		'itemID' : itemID,
		'description' : description,
		'standUM' : standUM,
		'umID' : umID,
		'unitPrice' : unitPrice1,
		'qtySent' : qtySent1,
		'amount' : amount,
		'totalAmount': accounting.unformat($('#totalAmount').val())
		};
                $(obj).attr("disabled","disabled");
	$.ajax({
		type: "POST",
		dataType: "json",
		url: "<?php echo base_url()?>sotransaction/doDetails_save",
		data: postData ,
		async: false,
		success: function(content){
			if(content.status == "success") {
				eRow.remove();
				$("#oTable2 tbody").append(
				"<tr><td style='display:none'>"+content.details.ID+"</td>" +
				"<td>"+content.details.lineNo+"</td>" +
				"<td>"+content.details.itemCode+"</td>" +
				"<td>"+description+"</td>" +
				"<td class='tac'>"+content.details.code+"</td>" +
				"<td class='tar'>"+accounting.formatNumber(unitPrice1,2)+"</td>" +
				"<td class='tar'>"+qtySent1+"</td>" +
				"<td class='tar'>"+accounting.formatNumber(amount,2)+"</td>" +
				"<td class='tac'><button class='btn-icon btn-mini btn-danger-form remove' title='<?php echo $this->lang->line('remove') ?>' onClick='saveRemove(this)'><span class='i-trashcan'></button></td>" +
		        "</tr>");
		        if(content.message == 'Updated'){
					alertbox("Delivery Order updated successfully",1);
		        }
	//			$("#deliveryID").val(content.ID);
	//			location.reload();

				getTotal();
			}
		}
	});  
}
function selectoption(line){
		$.ajax({
			type: "POST",
        	url: '<?php echo base_url();?>sotransaction/itemSearch',
			dataType:"json",
			success: function(content){
				if(content.status == "success") {
					var items = [];
					items.push('<option value=""><?php echo $this->lang->line('pleaseselect')?></option>');
					for ( var i = 0; i < content.message.length; i++) {
					items.push('<option value="'+content.message[i].ID+'">'
						+ content.message[i].itemCode + ' [ '+ content.message[i].name + ' ]</option>');
					}
//					for (var x = startrows; x < tablerows ; x++) {
 						var rowId = "#itemID"+line;
						jQuery(rowId).empty();
						jQuery(rowId).append(items.join('</br>'));
//					}
				} else {
					$("#error").html('<p>'+content.message+'</p>');
				}
					return false;
			}
		});
}

function unitofmesure(obj,id){
    $.ajax({
        type: "POST",
        url: '<?php echo base_url(); ?>aptransaction/getUmList',
        dataType:"JSON",
        data: {
            option :'sale',
            itemID : $("#"+id).select2('val'),
            datePrice : $("#effdate").val(),
            customerID : $("#customerID").select2('val')
        },
        success: function(data){
            console.log('um here');
            var option = '<option value="'+data['items'][0].unitOfMeasureID+'"  data-baseum="'+data['items'][0].unitOfMeasureID+'" data-unitm="0" data-factor="0">'+data['items'][0].umCode+'</option>';
            if(data['umConv']){
                for(i = 0; i<data['umConv'].length; i++){
                    option += '<option value="'+data['umConv'][i].from_um+'" data-baseum="'+data['umConv'][i].to_um+'" data-unitm="'+data['umConv'][i].unitm+'" data-factor="'+data['umConv'][i].factor+'">'+data['umConv'][i].fromCode+'</option>';
                }
            }
            $('#unitOfMeasureID',$(obj).parents('tr')).text('').append('<select class="umIDlist span12">'+
                                    option+
                                    '</select>');
            $('#unitOfMeasureID select',$(obj).parents('tr')).select2();
        }
    });
}

function selectDesc(obj){
    var eRow   = $(this).parents('tr');
	var ID = obj.value;
	var customerID = $("#customerID option:selected").val();
	var currencyID = $("#currencyID option:selected").val();
	var effdate = $("#effdate").val();
	postData = {
		ID : ID,
		customerID : customerID,
		currencyID : currencyID,
		effdate : effdate
	};
	
		$.ajax({
			type: "POST",
        	url: '<?php echo base_url();?>sotransaction/itemSearch2',
			dataType:"json",
			data: postData,
	  				async: false,
			success: function(content){
				if(content.status == "success") {
					$('td:eq(2)', $(obj).parents('tr')).find("input").val(content.message.description);
 					$('td:eq(4)', $(obj).parents('tr')).find("input").val(accounting.formatNumber(content.price,2));
 					$('td:eq(3)', $(obj).parents('tr')).find("select").val(content.message.unitOfMeasureID).change();
// 					$('#unitOfMeasureID').val(content.message.unitOfMeasureID).change();
					unitofmesure(obj,$('td:eq(1)', $(obj).parents('tr')).find("select").attr('id'));
				}
			}
		});
}
function selectUnitmeasure(line){
		$.ajax({
			type: "POST",
        	url: '<?php echo base_url();?>sotransaction/umSearch',
			dataType:"json",
			success: function(content){
				if(content.status == "success") {
					var items = [];
//					items.push('<option value=""><?php echo $this->lang->line('pleaseselect')?></option>');
					for ( var i = 0; i < content.message.length; i++) {
					items.push('<option value="'+content.message[i].fldid+'">'
//						+ content.message[i].code + ' [ '+ content.message[i].desc + ' ]</option>');
						+ content.message[i].code + '</option>');
					}
//					for (var x = startrows; x < tablerows ; x++) {
 						var rowId = "#unitOfMeasureID"+line;
						jQuery(rowId).empty();
						jQuery(rowId).append(items.join('</br>'));
//					}
				} else {
					$("#error").html('<p>'+content.message+'</p>');
				}
					return false;
			}
		});
}

function shipMethodSelect(){
 $.ajax({
	type: "POST",
	url: "<?php echo base_url() ?>setting/refreshShippingMethod",
	success: function(data) {
	
	   var resul = eval ("(" + data + ")"); 
	   var n=resul.records.length;
	   var i=0;
	   var opt='' +
		'<option></option>';
		for (i=0;i<n;i++)
			opt=opt + '<option value="' + resul.records[i].id +'">' + resul.records[i].name + '</option>';
		$('#shippingmethod').empty();
		$('#shippingmethod').append(opt);
	}
   });		
}
function calculate_amount(obj){
	var totalAmount = accounting.unformat($("#totalAmount").val());
	var previousAmount = accounting.unformat($('td:eq(10)', $(obj).parents('tr')).find("input").val());
//	var totalShiped = parseFloat($('td:eq(6)', $(obj).parents('tr')).text()) - parseFloat($('td:eq(7)', $(obj).parents('tr')).text());
//	var qtysent = $('td:eq(7)', $(obj).parents('tr')).find("input").val();
//	if(qtysent > totalShiped){
//		alert("<?php echo $this->lang->line('message9') ?>");
//	}
	var amount = accounting.unformat($('td:eq(9)', $(obj).parents('tr')).find("input").val()) * accounting.unformat($('td:eq(7)', $(obj).parents('tr')).find("input").val());
	$('td:eq(10)', $(obj).parents('tr')).find("input").val(formatNumber(amount,2));
	$("#totalAmount").val(accounting.formatNumber(accounting.unformat(totalAmount) + accounting.unformat(amount) - accounting.unformat(previousAmount),2));
}
function calculate_amount1(obj){
	var totalAmount = $("#totalAmount").val();
	var amount = accounting.unformat($('td:eq(5)', $(obj).parents('tr')).find("input").val()) * accounting.unformat($('td:eq(4)', $(obj).parents('tr')).find("input").val());
	//$("#totalAmount").val(accounting.formatNumber((accounting.unformat(totalAmount) + accounting.unformat(amount)),2));
	$('td:eq(6)', $(obj).parents('tr')).find("input").val(formatNumber(amount,2));
        //getTotal ();
}
function removeLine(obj){
	var totalAmount = $("#totalAmount").val();
	var amount = accounting.unformat($('td:eq(7)', $(obj).parents('tr')).find("input").val());
	$("#totalAmount").val(accounting.formatNumber((accounting.unformat(totalAmount) - accounting.unformat(amount)),2));
    var eRow   = $(obj).parents('tr');
	eRow.remove();
        getTotal ();
}

 function getTotal(){
            var n = $("#oTable > tbody > tr").length-1;
            var total = 0;
            if($('#amount').hasClass('editing')){
            	total = total + accounting.unformat($('#amount input').val());
            }
            for (i = 0; i <= n; i++) {
             total = total + accounting.unformat($('#oTable > tbody > tr:eq(' + i + ') > td:eq(10)').text());
            }

            var n =$("#oTable2 > tbody > tr").length;
            var total2 =0;
            for (i=0; i<=n; i++) {
            	total2= total2 + accounting.unformat($('#oTable2 > tbody > tr:eq(' + i + ') > td:eq(7)').text()); 
            }
            console.log(total+"<->"+total2+'<->'+(total+total2));
            $("#totalAmount").val(accounting.formatNumber((total + total2),2));
    }

  function saveRemove(obj){
    var eRow   = $(obj).parents('tr');deliveryID
	var deliveryID = $("#deliveryID").val();
	var totalAmount = accounting.unformat($("#totalAmount").val());
	var lineAmount = accounting.unformat($('td:eq(7)', $(obj).parents('tr')).text());
	var Amount = accounting.unformat(totalAmount) - accounting.unformat(lineAmount);
	var ID = $('td:eq(0)', $(obj).parents('tr')).text();
				postData = {
					ID : ID,
					deliveryID : deliveryID,
					totalAmount : Amount,
					lineAmount : lineAmount
				};
                                $(obj).attr("disabled","disabled");
				$.ajax({
					type: "POST",
        			url: '<?php echo base_url();?>sotransaction/doDetails_delete',
					dataType:"json",
					data: postData,
	  				async: false,
					success: function(content){
						if(content.status == "success") {
							//alert(content.message);
							$("#totalAmount").val(accounting.formatNumber(Amount,2));
							eRow.remove();
                            alertbox("Row has been removed",1);
						}
					}
				}); 
}

function printSalesDOedit(){
	//var selDateFrom = $('#selDateFrom').val();
	var deliveryID = $("#deliveryID").val();
	window.open("<?php echo base_url();?>sotransaction/SalesDO_print/"+deliveryID);
}
</script>
<div id="content"> 
<div class="wrap">
	<div class="head">
		<div class="info">
			<h1><?php echo element('compName', $this->session->userdata('logged_in')) ?> [ 
				<?php echo element('compNo', $this->session->userdata('logged_in')) ?> ]</h1>
			<ul class="breadcrumb">
            	<li><a href='<?php echo base_url()."home" ?>'><?php echo $this->lang->line('dashboard') ?></a> <span class="divider">-</span></li>
                <li><a href='<?php echo base_url()."so/home" ?>'> <?php echo $this->lang->line('module') ?></a> <span class="divider">-</span></li>
                <li class="active"><?php echo $this->lang->line('title') ?></li>
            </ul>
		</div>
		<div class="search">
			<form action="<?php echo base_url() ?>admin/search" method="post">
				<input name="search_text" type="text" placeholder="search..."/>                                
            	<button type="submit"><span class="i-magnifier"></span></button>
			</form>
		</div>        
                                        
	</div><!-- head --> 
<div class="content" id="grid_content_1">
	<div class="row-fluid">
		<div class="block">
		<div class="head">
			<h2><?php echo $this->lang->line('title') ?></h2>
			<ul class="buttons">
                <li><a target="_blank" onClick="printSalesDOedit()" href="javascript:" title="updatePosting" onclick="">Print</a></li>
            </ul>
		</div>
		</div>
	</div>
      	<div class="block">
      	<div class="content">
	<div class="row-fluid scRow">                            
	  <div class="span6 scCol">
		<div class="block ns" id="grid_block_1">
			<div class="controls-row"> <!-- row 1 -->
            	<div class="span3"><?php echo $this->lang->line('customer') ?></div>
              	<div class="span9">
					<?php
							$preselcust = $headerDo->customerID;
							$options[] = $this->lang->line('pleaseselect');
							if($customers){
							foreach($customers as $row){
								$options[$row->ID] = $row->customerName.' [ '.$row->accountCode.' ]';
							}
							}
							$js='class="span12" id="customerID" disabled';
							echo form_dropdown('customerID', $options, $preselcust, $js);
						?>     
            	</div>                
         	</div><!-- End Row 1 -->
                	<div class="controls-row"><!-- Row 4-->
                    	<div class="span3"><?php echo $this->lang->line('orderNo');?></div>
                   		<div class="span9">
                            	<?php 								
								$data = array(
									'name'=>'formNo', 
									'id'=>'formNo', 
									'disabled'=>'disabled', 
									'class'=>'span12',); 
								echo form_input($data, $headerDo->formNo); ?>
                            	<?php 								
								$data = array(
									'name'=>'deliveryToID', 
									'id'=>'deliveryToID', 
									'type'=>'hidden',); 
								echo form_input($data, $headerDo->deliverToID); ?>
                  		</div>
                 	</div><!-- End Row 4-->
                  <div class="controls-row"><!-- Row 3 -->
                    	<div class="span3"><?php echo $this->lang->line('effective_date');?></div>
                    <div class="span9">
                    	<div class="input-prepend span12">
                        	<span class="add-on span2"><i class="i-calendar"></i></span>
                            	<?php 								
								$data = array(
									'name'=>'deliveryID', 
									'id'=>'deliveryID', 
									'type'=>'hidden',); 
								echo form_input($data, $headerDo->ID); ?>
                            	<?php 								
								$data = array(
									'type'=>'text',
									'name'=>'effdate', 
									'id'=>'effdate', 
									'class'=>'span10 datepicker2 financhk',); 
								echo form_input($data, date("d-m-Y", strtotime($headerDo->deliveryOrderDate))); ?>
                        </div>                                                                                                
                     </div>
                    </div><!-- End Row 3 -->
			<div class="controls-row"> <!-- row 1 -->
            	<div class="span3"><?php echo $this->lang->line('location') ?></div>
              	<div class="span9">
					<?php
							$presellocation = $headerDo->locationID;
							$options = '';
							$options[] = $this->lang->line('pleaseselect');
							if($location){
							foreach($location as $row){
								$options[$row->fldid] = $row->code . ' | ' . $row->desc;
							}
							}
							$js='class="span12" id="location"';
							echo form_dropdown('location', $options, $presellocation, $js);
						?>    
            	</div>                
         	</div><!-- End Row 1 -->
                
                <div class="controls-row"> <!-- row 1 -->
                    <div class="span3"><?php echo $this->lang->line('shipto') ?></div>
                    <div class="span9">
                    <?php
							$data = array(
									'name'=>'address', 
									'id'=>'address', 
									'class'=>'span12',);
							$js = 'style="height:100px"'; 
							echo form_textarea($data, $headerDo->toAddress, $js); ?>  
                    </div>
                </div>
                
                
        </div><!--grid_block_1 -->
      </div><!--span6 scCol -->
	  <div class="span6 scCol">
		<div class="block ns" id="grid_block_1">
			<div class="controls-row"> <!-- row 1 -->
            	<div class="span3"><?php echo $this->lang->line('salesorder') ?></div>
              	<div class="span9">
					<?php
							$preselso = $headerDo->salesOrderID;
							$options = '';
							$options[] = $this->lang->line('pleaseselect');
							if($salesorder){
							foreach($salesorder as $row){
								$options[$row->ID] = $row->OrderNo;
							}
							}
							$js='class="span12" id="salesorder" disabled';
							echo form_dropdown('salesorder', $options, $preselso, $js);
						?>       
            	</div>                
         	</div><!-- End Row 1 -->
			<div class="controls-row"> <!-- row 1 -->
            	<div class="span3"><?php echo $this->lang->line('currency') ?></div>
              	<div class="span9">
					<?php
							$preselcurrency = $headerDo->currencyID;
							$options[] = $this->lang->line('pleaseselect');
							if($currency){
							foreach($currency as $row){
								$options[$row->fldid] = $row->fldcurr_code.' [ '.$row->fldcurr_desc.' ]';
							}
							}
							$js='class="span12" id="currencyID" disabled';
							echo form_dropdown('currencyID', $options, $preselcurrency, $js);
						?>       
            	</div>                
         	</div><!-- End Row 1 -->
                	<div class="controls-row"><!-- Row 4-->
                    	<div class="span3"><?php echo form_label($this->lang->line('exchangerate'),'exchangeRate');?></div>
                   		<div class="span9">
                            	<?php 								
								$data = array(
									'name'=>'exchangeRate', 
									'id'=>'exchangeRate', 
									'class'=>'span12 tar',); 
								echo form_input($data, number_format($headerDo->exchangeRate,5)); ?>
                  		</div>
                 	</div><!-- End Row 4-->
			<div class="controls-row"> <!-- row 1 -->
            	<div class="span3"><?php echo $this->lang->line('salesperson') ?></div>
              	<div class="span9">
					<?php
							$preselperson = $headerDo->employeeID;
							$options = '';
							$options[] = $this->lang->line('pleaseselect');
							if($employees){
							foreach($employees as $row){
								$options[$row->ID] = $row->employeeName;
							}
							}
							$js='class="span12" id="salesPerson"';
							echo form_dropdown('salesPerson', $options, $preselperson, $js);
						?>    
            	</div>                
         	</div><!-- End Row 1 -->
                
                <div class="controls-row"><!-- Row 4-->
                    	<div class="span3"><?php echo form_label($this->lang->line('memo'),'memo');?></div>
                   		<div class="span9">
                                    
                                    <?php 	
                                    $data = array('name'=>'memo', 
									'id'=>'memo', 
									'class'=>'span12');
							$js = 'style="height:100px"'; 
							echo form_textarea($data, $headerDo->memo, $js); ?>
                            	<?php 								
								$data = array(
									'name'=>'bill', 
									'id'=>'bill', 
									'value'=> 1,
									'type' => 'hidden'); 
								echo form_input($data); ?>
                                </div>
                 	</div><!-- End Row 4-->
               
        </div><!--grid_block_1 -->
      </div><!--span6 scCol -->
    </div><!--row-fluid scRow -->
	
    </div><!--row-fluid scRow -->
    </div><!--row-fluid scRow -->
	<div class="row-fluid scRow">                            
	  <div class="span12 scCol">
		<div class="block" id="grid_block_4">
	       <div class="head">
           	<h2><?php echo $this->lang->line('outstanding')?></h2>
                    	<ul class="buttons">
                            <li><a href="#" class="block_toggle" title="Close"><span class="i-arrow-down-3"></span></a></li>
                        </ul>                                        
          </div><!-- head -->
          <div class="content np">
   			<table id="oTable" name="oTable" cellpadding="0" cellspacing="0" width="100%" class="editable">
         		<thead>
             	<tr>
                	<th style='display:none'></th>
                	<th style='display:none'></th>
            		<th>#</th>
            		<th><?php echo $this->lang->line('itemCode')?></th>
            		<th><?php echo $this->lang->line('description')?></th>
            		<th class="tac"><?php echo $this->lang->line('daterequired')?></th>
            		<th class="tac"><?php echo $this->lang->line('um')?></th>
            		<th class="tar"><?php echo $this->lang->line('unitPrice')?></th>
            		<th class="tar"><?php echo $this->lang->line('qtyOrd')?></th>
            		<th class="tar"><?php echo $this->lang->line('qtySend')?></th>
            		<th class="tar"><?php echo $this->lang->line('amount')?></th>
            		<th class="tac"><?php echo $this->lang->line('action')?></th>
              	</tr>
        		</thead>
                <tbody>
			<?php 
                $totalAmount = 0;
				if($datatbls1){
					/*print_r($datatbls1);
					exit();*/
				foreach($datatbls1 as $datatbl):
                                    $totalAmount +=  $datatbl->shipAmount;
				?>
            	<tr>
                	<td id="doID" style="display:none" ><?php echo $datatbl->doID; ?></td>
                	<td id="soID" style="display:none" ><?php echo $datatbl->ID; ?></td>
                	<td><?php echo $datatbl->lineNo?></td>
                	<td><?php echo $datatbl->itemCode?></td>
                    <td><?php echo $datatbl->description ?></td>
                	<td id="requiredDate" class="tac"><?php echo date("d-m-Y", strtotime($datatbl->requiredDate)) ?></td>
                	<td id="code" class="tac"><?php echo $datatbl->code?></td>
                	<td id="unitPrice" class="tar"><?php echo number_format(($datatbl->price != 0)? $datatbl->price : $datatbl->unitPrice ,2); ?></td>
                	<td id="quantityOrder" class="tar"><?php echo $datatbl->quantityOrder; ?></td>
                	<td id="quantitySent" class="tar"><?php echo $datatbl->quantitySent; ?></td>
                	<?php /*<td id="quantitySent" class="tar"><?php echo $datatbl->quantitySent; ?><input type="hidden" id="factor" value="<?php echo $datatbl->factor; ?>" /></td> */?>
                	<td id="amount" class="tar"><?php echo number_format($datatbl->shipAmount,2); ?></td>
 					<td id="action" class="tac"><?php
							$js = 'class="btn-icon btn-mini btn-warning-form edit" title="'.$this->lang->line('edit').'"  onClick=""';
							echo form_button('edit','<span class="i-pen">',$js);
						?></td>
					<td class="noCont" style="display:none"><input type="hidden" id="factor" value="<?php echo $datatbl->factor; ?>" /></td>
               </tr>
            <?php endforeach; }?>
                </tbody>
     		</table>
          </div><!--content -->
        </div><!--grid_block_4 -->
      </div><!--span6 scCol -->
    </div><!--row-fluid scRow -->
	<div class="row-fluid scRow">                            
	  <div class="span12 scCol">
		<div class="block" id="grid_block_5">
	       <div class="head">
           	<h2><?php echo $this->lang->line('withoutso')?></h2>
                    	<ul class="buttons">
                            <li><a href="#" class="block_toggle" title="Close"><span class="i-arrow-down-3"></span></a></li>
                        </ul>                                        
          </div><!-- head -->
          <div class="content np">
   			<table id="oTable2" cellpadding="0" cellspacing="0" width="100%">
         		<thead>
             	<tr>
                	<td style="display:none" ></td>
            		<th>#</th>
            		<th><?php echo $this->lang->line('itemCode')?></th>
            		<th><?php echo $this->lang->line('itemdescription')?></th>
            		<th><?php echo $this->lang->line('um')?></th>
            		<th class="tar"><?php echo $this->lang->line('unitPrice')?></th>
            		<th class="tar"><?php echo $this->lang->line('qtySend')?></th>
            		<th class="tar"><?php echo $this->lang->line('amount')?></th>
            		<th class="tac"><?php echo $this->lang->line('action')?></th>
              	</tr>
        		</thead>
                <tbody>
			<?php $bil = 0; 
				if($datatbls){
					//print_r($datatbls);
					//exit();
				foreach($datatbls as $datatbl):
                                     $totalAmount +=  $datatbl->amount;
                                    $bil++; 
                                ?>
            	<tr>
                	<td style="display:none" ><?php echo $datatbl->ID; ?></td>
                	<td><?php echo $bil; ?></td>
                	<td><?php echo $datatbl->itemCode?></td>
                    <td><?php echo $datatbl->description ?></td>
                	<td class="tac"><?php echo $datatbl->code?></td>
                	<td class="tar"><?php echo ($datatbl->factor != '')?number_format($datatbl->unitPrice*$datatbl->factor,2):number_format($datatbl->unitPrice,2); ?></td>
                	<td class="tar"><?php echo ($datatbl->factor != '')?$datatbl->quantitySent/$datatbl->factor:$datatbl->quantitySent; ?></td>
                	<td class="tar"><?php echo number_format($datatbl->amount,2); ?></td>
 					<td class="tac"><?php
							$js = 'class="btn-icon btn-mini btn-danger-form remove" title="'.$this->lang->line('delete').'"  onClick="saveRemove(this)"';
							echo form_button('remove','<span class="i-trashcan">',$js);
						?></td>
               </tr>
            <?php endforeach; }?>
                </tbody>
     		</table>
          </div><!--content -->
            <div class="footer">
            	<div class="side fl">
                	<div class="btn-group">
            				<button class="btn-icon btn-mini btn-warning-form" onClick="addRow()" id="addLine">
                				<span class="i-plus"></span> <?php echo $this->lang->line('addnewline')?></button>
                    </div>
                </div>
            </div>                                    
        </div><!--grid_block_5 -->
      </div><!--span6 scCol -->
    </div><!--row-fluid scRow -->
	<div class="row-fluid scRow">                            
	  <div class="span12 scCol">
		<div class="block" id="grid_block_3">
            	<div class="head">
                	<h2><?php echo $this->lang->line('memoandshipto') ?></h2>
                    	<ul class="buttons">
                            <li><a href="#" class="block_toggle" title="Close"><span class="i-arrow-down-3"></span></a></li>
                        </ul>                                        
                </div>
          <div class="content">
			<div class="controls-row"> <!-- row 1 -->
            	<div class="span2"><?php echo $this->lang->line('shipMethod') ?></div>
              	<div class="span4">
					<?php
							$preselshipmethod = $headerDo->shippingMethodID;
							$options = '';
							$options[] = $this->lang->line('pleaseselect');
							foreach($shippingMethod as $row){
								$options[$row->ID] = $row->code . ' [ ' . $row->name . ' ]';
							}
							$js='class="span8" id="shippingmethod"';
							echo form_dropdown('shippingmethod', $options, $preselshipmethod, $js);
						?>       
                	<div class="btn-group">
						<button class="btn-icon btn-small dropdown-toggle" data-toggle="dropdown">
                		<span class="i-arrow-down-2"></span>
                		</button>
						<ul class="dropdown-menu">
      					<!-- dropdown menu links -->
      						<li><a target="_blank" href="<?php echo base_url() ?>setting/masterCodeSetup"  id="btnNewShipMethod">
                    			<i class="icon-plus-sign"></i> <?php echo $this->lang->line('newShipMethod')?></a></li>
                                <li><a href="#" onclick="shipMethodSelect()"><i class="icon-refresh"></i> <?php echo $this->lang->line('refreshlist')?></a></li>
						</ul>
         			</div><!-- btn group -->
            	</div>                
                    	<div class="span2 tar"><?php echo $this->lang->line('totalAmount');?></div>
                   		<div class="span4">
                            	<?php 								
								$data = array(
									'name'=>'totalAmount', 
									'id'=>'totalAmount', 
									'disabled'=>'disabled', 
									'class'=>'span12 tar',); 
								echo form_input($data, number_format( $totalAmount, 2)); ?>
                  		</div>
                 	</div><!-- End Row 4-->
          </div><!--content -->
        </div><!--grid_block_1 -->
      </div><!--span12 scCol -->
    </div><!--row-fluid scRow -->
</div><!--grid_content_1 -->
</div><!--wrap -->
</div><!--content -->
