<script type="text/javascript">
    var valuedatatable;
	var orderexists = false;
	$(document).ready(function(){
		
		$('#position_form').off('submit').on('submit', function() {			
			var name = $.trim(	$('#positionName').val()	);
			if(name == "") {
				$('#positionName').parents('.form-group').eq(0).addClass('has-error');
				return false;
			}
			
			var elected = $.trim(	$('#elected').val()	);
			if(elected == "") {
				$('#elected').parents('.form-group').eq(0).addClass('has-error');
				return false;
			}
			
			$('#ordering').parents('.form-group').eq(0).removeClass('has-error');
			$('#orderingexists').addClass('hidden');
			if (orderexists) {
				$('#ordering').parents('.form-group').eq(0).addClass('has-error');
				$('#orderingexists').removeClass('hidden');
				return false;
			}
		});
		
<!-- page script -->
        //$('.datatables').DataTable();
        $('.datetimepicker').datetimepicker({
            viewMode: 'months',
            format: 'DD-MM-YYYY'
        });
		
		
		$('#datatable_positionlist').on('search.dt', function() {
    		valuedatatable = $('.dataTables_filter input').val();
    		//alert(valuedatatable);
		});
		/*
		$('#datatable_positionlist').dataTable( {
			"aoColumnDefs": [
			  		{ "bSearchable": false, "aTargets": [ 0] },
			  		{ "bSearchable": true, "aTargets": [ 1] },
			  		{ "bSearchable": true, "aTargets": [ 2] },
			  		{ "bSearchable": true, "aTargets": [ 3] },
			   		{ "bSearchable": false, "aTargets": [ 4] }
			] 
		});
		*/
		
		$('#datatable_positionlist').DataTable( {
			"pageLength": 25,
			initComplete: function () {	
				var $table = $(this);			
				var $filter = $table.parents('.dataTables_wrapper').find('.dataTables_filter');
				
				$filter.find('input[type="search"]').attr('id','search_input');
			}
		});
	
		

    });
    
	function showDetail(me)
	{
		
		$('#positionID').val("");
		$('#positionName').val("");
		$('#elected').val("");
		$('#ordering').val("");
		
		$('#ordering').parents('.form-group').eq(0).removeClass('has-error');
		$('#orderingexists').addClass('hidden');
		orderexists = false;
		
    	$('#position_modal').modal('show');
		
	}
	
	function showDetail2(positionID, positionName, elected, ordering)
	{
		
		$('#position_modal').modal('show');
		
		$('#positionID').val(positionID);
		$('#positionName').val(positionName);
		$('#elected').val(elected);
		$('#ordering').val(ordering);
		
		$('#ordering').parents('.form-group').eq(0).removeClass('has-error');
		$('#orderingexists').addClass('hidden');
		orderexists = false;
		
	}
	
	
	
	
	function deleteConfirm(positionID)
	{
		$('#deleteID').val(positionID);
		$('#delete_modal').modal('show');
		
	}
	
	function deletePosition(){
	
		var positionID = $('#deleteID').val();
		//if (confirm("Are you sure?")) {
        
			$.ajax({
				type: "POST",
				url: "<?php echo base_url(); ?>position/deletePosition",
				data: {
					'positionID' : positionID
				},
				dataType:'json',
				success: function(data){
					if(data.status == 'success'){
						
						location.reload();
						
					}
				}
			 });
		
		 //}
    	//return false;
	
	}
	
	function checkOrdering(me) {
		var data = {
			positionID : Number($('#positionID').val()),
			order : Number($(me).val())
		};
		
		$(me).parents('.form-group').eq(0).removeClass('has-error');
		$('#orderingexists').addClass('hidden');
		
		$.post('<?php echo base_url(); ?>position/checkorderingajax', data, function(r) {
			if ($.trim(r) && $.trim(	r.toUpperCase()	) == "FAIL") {
				$(me).parents('.form-group').eq(0).addClass('has-error');
				$('#orderingexists').removeClass('hidden');
				orderexists = true;
			} else {
				$(me).parents('.form-group').eq(0).removeClass('has-error');
				$('#orderingexists').addClass('hidden');
				orderexists = false;
			}
		});
	}
	
	function printposition(){			
		$('#print_btn').attr('href','javascript:void(0);');
		
		var search = $.trim($('#search_input').val());				
				search = (search !== "") ? search.replace(' ','%20') : 'all';
		
		var url = "<?php echo base_url() ?>position/printPosition/"+search;
		
		$('#print_btn').attr('target','_blank');
		$('#print_btn').attr('href',url);
		return true;
		
	}
	
	function isNumberKey(evt){
    	var charCode = (evt.which) ? evt.which : event.keyCode
    	if (charCode > 31 && (charCode < 48 || charCode > 57)){
			return false;
		}
		else{
			return true;
		}
	}
	
</script>
