<script>
$(document).ready(function(){
	$('select').select2();
	jQuery('.datepicker2').datepicker({
			dateFormat : 'dd-mm-yy',
			changeMonth : true,
			changeYear : true
			});
    $("table.editable").on("click",".edit",function(){
        var eRow   = $(this).parents('tr');
        var eState = $(this).attr('data-state');
        if(eState == null){
            $(this).html('<span class="icon-ok-circle">');
			$(this).find('id').show();
            eState = 1;
        }
        
        eRow.find('td').each(function(){            
            if(eState == 1){
                var eContent = $(this).html(); 
//				alert(eContent);
//				alert($(this).attr('id'));
				if($(this).attr('id') == 'rateConv'){     
                	if(eContent.indexOf('<') < 0){
                    	$(this).addClass("editing").html('<input class="tar" type="text" value="' + eContent + '"/>');
                	}
				}else if($(this).attr('id') == 'startDate' || $(this).attr('id') == 'endDate'){
                	if(eContent.indexOf('<') < 0){
                    	$(this).addClass("editing").html('<input class="datepicker2 financhk" type="text" value="' + eContent + '"/>');                    
						$(".datepicker2").datepicker({
			dateFormat : 'dd-mm-yy',
			changeMonth : true,
			changeYear : true
							});                    
                	}
				}
            }
            if(eState == 2){
                var eContent = $(this).find('input').val();                                
                if(eContent != null){
                    $(this).removeClass("editing").html(eContent);
                    // Here your ajax action after Save button pressed
			        var fldid = $('td:first', $(this).parents('tr')).text(); 
					var fieldname = $(this).attr('id');
                	var newContent = $(this).val();
					var urls = "<?php echo base_url()?>glsetting/currencyConv_update";
//			 		alert(fieldname);
					postData = {
						'fldid' : fldid,
						'fieldname' : fieldname,
						'eContent' : eContent,
					};
 					$.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 changeRate(){
 	$("#oTable tbody tr").remove(); 
	var currFrom = $("#currencyFrom option:selected").val();
	var currTo = $("#currencyTo option:selected").val();
	if(currFrom == '' || currTo == ''){
	}else{
		postData = {
			currFrom : currFrom,
			currTo : currTo
		};
		$.ajax({
			type: "POST",
        	url: '<?php echo base_url();?>glsetting/currencyConvSearch',
			dataType:"json",
			data: postData,
			success: function(content){
				if(content.status == "success") {
					for ( var i = 0; i < content.details.length; i++) {
    						$("#oTable tbody").append(
							"<tr><td style='display:none'>"+content.details[i].ID+"</td>" +
        					"<td class='tac' id='startDate'>"+(content.details[i].startDate).substr(8, 2) + 
							"-" + (content.details[i].startDate).substr(5, 2) +
							"-" + (content.details[i].startDate).substr(0, 4) + "</td>" +
        					"<td class='tac' id='endDate'>"+(content.details[i].endDate).substr(8, 2) + 
							"-" + (content.details[i].endDate).substr(5, 2) +
							"-" + (content.details[i].endDate).substr(0, 4) + "</td>" +
							"<td class='tar' id='rateConv'>"+accounting.formatNumber(content.details[i].rateConv, 5)+"</td>" +
							"<td class='tac'><button class='btn-icon btn-mini btn-warning-form edit' title='<?php echo $this->lang->line('edit');?>'><span class='i-pen'></button>" +
					        "</tr>");
					}
				}
			}
		});
	}
		
}
function addRow(){
	var currFrom = $("#currencyFrom option:selected").val();
	var currTo = $("#currencyTo option:selected").val();
	var tablerows = $("#bill").val();
    var rowToInsert = '';
	if(currFrom == '' || currTo == ''){
		return false;
	}
    $("#oTable tbody").append(
		"<tr>" +
        "<td  style='display:none'></td>"+
        "<td class='span4 tac'><input class='datepicker2' type='text' class='span12'/></td>"+
        "<td class='span4 tac'><input class='datepicker2' type='text' class='span12'/></td>"+
        "<td class='span2 tar'><input type='text' class='span12 tar' value='0'/></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-3'></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',
	});
  	$("#bill").val(parseInt(tablerows) + 1);
	$(".datepicker2").datepicker({
		dateFormat : 'dd-mm-yy',
		changeMonth : true,
		changeYear : true
	});                    
	$("#addLine").hide(); 
}
function cancelRow(obj){
    var eRow   = $(obj).parents('tr');
	eRow.remove();
	$("#addLine").show(); 
}
function saveDetails(obj){
    var eRow   = $(obj).parents('tr');
	var currencyFrom = $("#currencyFrom option:selected").val();
	var currencyTo = $("#currencyTo option:selected").val();
	if(currencyFrom == '' || currencyTo == ''){
		return false;
	}
	var fromDate = $('td:eq(1)', $(obj).parents('tr')).find("input").val();
	var toDate = $('td:eq(2)', $(obj).parents('tr')).find("input").val();
	var convRate = $('td:eq(3)', $(obj).parents('tr')).find("input").val();
	postData = {
		currencyFrom : currencyFrom,
		currencyTo : currencyTo,
		startDate : fromDate,
		endDate : toDate,
		rateConv : convRate,
	};
	$.ajax({
		type: "POST",
		url: '<?php echo base_url();?>glsetting/currencyConv_insert',
		dataType:"json",
		data: postData,
		async: false,
		success: function(content){
			if(content.status == "success") {
				eRow.remove();
    			$("#oTable tbody").append(
				"<tr><td style='display:none'>"+content.details+"</td>" +
        		"<td class='tac' id='startDate'>"+ fromDate + "</td>" +
        		"<td class='tac' id='endDate'>"+ toDate + "</td>" +
				"<td class='tar' id='rateConv'>"+accounting.formatNumber(convRate, 5)+"</td>" +
				"<td class='tac'><button class='btn-icon btn-mini btn-warning-form edit' title='<?php echo $this->lang->line('edit');?>'><span class='i-pen'></button>" +
					        "</tr>");
				$("#addLine").show(); 
				alert(content.message);
			}
		}
	});
}
</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()."GeneralLedger" ?>'> <?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">
			<div class="wrap">    
				<div class="row-fluid">
					<div class="span12">
						<div class="block">
							<div class="head">
								<h2><?php echo $this->lang->line('searchParam') ?></h2>
								<ul class="buttons">
										<li><a href="#" class="block_toggle"><span class="i-arrow-down-3"></span></a></li>
								</ul>
								<div class="side fr">
									<a class="btn-form btn-success-form" href="" onClick="do_print()"><?php echo $this->lang->line('print') ?></a>
								</div>
							</div><!-- head -->
							<div class="content np">    
								<div class="controls-row"> <!-- row 1 -->
									<div class="span2"><?php echo $this->lang->line('currencyFrom') ?></div>
										<div class="span10">
										<?php
										$preselcurrency = element('compCurrency', $this->session->userdata('logged_in'));
										$options[NULL] = $this->lang->line('pleaseselect');
										foreach($currency as $row){
											$options[$row->fldid] = $row->fldcurr_code.' [ '.$row->fldcurr_desc.' ]';
										}
										$js='class="span5" id="currencyFrom" onChange="changeRate()"';
											echo form_dropdown('currencyFrom', $options, $preselcurrency, $js);
										?>       
									</div>                
								</div><!-- End Row 1 -->
								<div class="controls-row"> <!-- row 1 -->
									<div class="span2"><?php echo $this->lang->line('currencyTo') ?></div>
									<div class="span10">
										<?php
										$preselcurrency2 = '';
										$option = '';
										$options[NULL] = $this->lang->line('pleaseselect');
										foreach($currency as $row){
											$options[$row->fldid] = $row->fldcurr_code.' [ '.$row->fldcurr_desc.' ]';
										}
										$js='class="span5" id="currencyTo" onChange="changeRate()"';
											echo form_dropdown('currencyTo', $options, $preselcurrency2, $js);
										?>       
										<?php 								
										$data = array(
											'name'=>'bill', 
											'id'=>'bill', 
											'value'=> 1,
											'type' => 'hidden'); 
												echo form_input($data); 
										?>
									</div>                
								</div><!-- End Row 1 -->
							</div><!-- content -->
						</div><!-- block -->
					</div><!-- span12 -->
				</div><!-- row-fluid -->
				<div class="row-fluid">                            
					<div class="span12">
						<div class="block" id="detailsTable">
							<div class="head">
								<h2><?php echo $this->lang->line('currConversionDet') ?></h2>
								<ul class="buttons">
									<li><a href="#" class="block_toggle"><span class="i-arrow-down-3"></span></a></li>
								</ul>
								<div class="side fr" >
									<a class="btn-icon btn-warning-form" title="Add New" onclick="addRow()"><span class="i-plus-2"></span></a>
								</div>                                        
							</div><!-- head -->
							<div class="content np">
							<table cellpadding="0" cellspacing="0" width="100%" id="oTable" class="editable">
								<thead>
									<tr>
										<th style="display:none"></th>
										<th class="tac"><?php echo $this->lang->line('dateFrom')?></th>
										<th class="tac"><?php echo $this->lang->line('dateTo')?></th>
										<th class="tar"><?php echo $this->lang->line('rate')?></th>
										<th class="span1 tac"><?php echo $this->lang->line('action')?></th>
									</tr>
								</thead>
								<tbody></tbody>
							</table>                                         
							</div><!-- content -->
						</div><!-- detailsTable -->
					</div><!-- span12 -->
				</div><!-- row-fluid -->
			</div>
		</div>
	</div>
</div>
