<script type="text/javascript">
    var valuedatatable;
	
	$(document).ready(function(){
        <!-- page script -->
        //$('.datatables').DataTable();
        $('.datetimepicker').datetimepicker({
            viewMode: 'months',
            format: 'DD-MM-YYYY'
        });
				
				
	$('form#voter_form').off('submit').on('submit',function() {
		if ($(this).validationEngine('validate')) {
			var proceed = true;
			
			var noAhli = $.trim(	$('#noAhli').val()	);
			if(noAhli == "") { proceed =  false; $('#noAhli').validationEngine('showPrompt', '<?php echo $this->lang->line('plsVoterID') ?>', 'load','bottomRight',1); }
			if(!proceed) return false;
			
			var userID = Number(	$('#userID').val()	);
			if(!userID) {			
				var userPassword = $.trim(	$('#userPassword').val()	);
				var confirmuserPassword = $.trim(	$('#confirmuserPassword').val()	);
				
				if(userPassword == "") { proceed =  false; $('#userPassword').validationEngine('showPrompt', '<?php echo $this->lang->line('plsPwd') ?>', 'load','bottomRight',1); }
				if(confirmuserPassword == "") { proceed =  false; $('#confirmuserPassword').validationEngine('showPrompt', '<?php echo $this->lang->line('plsPwdConf') ?>', 'load','bottomRight',1); }
				if(confirmuserPassword != userPassword) {
					proceed =  false;
					$('#userPassword').validationEngine('showPrompt', '<?php echo $this->lang->line('pwdConfNotMatch') ?>', 'load','bottomRight',1);
					$('#confirmuserPassword').validationEngine('showPrompt', '<?php echo $this->lang->line('pwdConfNotMatch') ?>', 'load','bottomRight',1);
				}
				if(!proceed) return false;
			}
			
			openLoader();
			return true;
		} else {
			return false;
		}
	});
		
		
		$('#datatable_voterlist').on('search.dt', function() {
    		valuedatatable = $('.dataTables_filter input').val();
    		//alert(valuedatatable);
		});
		
		/*
		$('#datatable_voterlist').dataTable( {
			"aoColumnDefs": [
			  		{ "bSearchable": false, "aTargets": [ 0] },
			  		{ "bSearchable": true, "aTargets": [ 1] },
			  		{ "bSearchable": true, "aTargets": [ 2] },
			  		{ "bSearchable": true, "aTargets": [ 3] },
			   		{ "bSearchable": false, "aTargets": [ 4] },
			    	{ "bSearchable": false, "aTargets": [ 5] }
			] 
		});
		*/
		
		
		$('#datatable_voterlist').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');
				
				this.api().columns(4).every( function () {				
					var column = this;
					var wrap = $('<label />',{ 'class': 'status_select' })
											.html('<span>Status:   </span>')
											.css('margin','0 20px 0 0')
											.prependTo( $filter );
					var select = $('<select id="select_status"><option value="">ALL</option></select>')
												.append( '<option value="Voted">Voted</option>' )
												.append( '<option value="No Vote">No Vote</option>' )
												.addClass('form-control input-sm')
												.appendTo( wrap )
												.on( 'change', function () {
														var val = $.fn.dataTable.util.escapeRegex( $(this).val() );
														column.search( val ? '^'+val+'$' : '', true, false ).draw();
												});
					
				});
			}
		});
	
		$('#voter_modal').on('hidden.bs.modal', function() {
			$(this).find('#noAhli').eq(0).val('');
			$(this).find('#voterName').eq(0).val('');
			$(this).find('#userPassword').eq(0).val('');
			$(this).find('#confirmuserPassword').eq(0).val('');
			$(this).find('#companyName').eq(0).val('');
		});
    });

    
    
	function showDetail(me)
	{
		$("#saveVoter").attr("disabled", true);
		$('#confirmuserPassword, #noAhli').removeAttr( 'style' );

		$('#userID').val("");
		$('#noAhli').val("");
		$('#voterName').val("");
		$('#companyName').val("");
		
		$('#userPassword').val('');
		$('#userPassword').next('.helper').eq(0).hide();
		$('#confirmuserPassword').val('');
		$('#confirmuserPassword').next('.helper').eq(0).hide();
		
    	$('#voter_modal').modal('show');
		
	}
	
	function showDetail2(userID, companyID, companyName)
	{
		
		//alert(companyName);
		$("#saveVoter").attr("disabled", false);
		
		$('#voter_modal').modal('show');
		
		$('#userID').val(userID);
		$('#noAhli').val(	$.trim(	$('#noAhli_' + userID + ' a').html()	)	);
		$('#voterName').val($('#voterName_' + userID).text());
		
		
		$('#userPassword').val('');
		$('#userPassword').next('.helper').eq(0).show();
		$('#confirmuserPassword').val('');
		$('#confirmuserPassword').next('.helper').eq(0).show();
		
			$.ajax({
				type: "POST",
				url: "<?php echo base_url(); ?>voter/getcompany",
				data:{},
				dataType:'json',
				success: function(data){
					if(data.status == 'success'){
						
						var n=data.message.length;
						var i=0;
					
						var opt='';
					
							if(companyID != null || companyName != null){
								opt = opt + "<option value='" + companyID + "'>" + companyName + "</option>";

                                for(i=0;i<n;i++){
                                    if(companyID != data.message[i].companyID)
                                        opt = opt + "<option value='" + data.message[i].companyID + "'>" + data.message[i].companyName + "</option>";
                                }
							}
							else{
								opt = opt + "<option>NA</option>";

                                for(i=0;i<n;i++){
                                    if(companyID != data.message[i].companyID)
                                        opt = opt + "<option value='" + data.message[i].companyID + "'>" + data.message[i].companyName + "</option>";
                                }
							}
							
							
						$('#companyName').empty();
						$('#companyName').append(opt);
						
						
					}
				}
				
			});
		
		
		
	}
	
	
	
	
	function deleteConfirm(me)
	{
		$('#delete_modal').modal('show');
		
		$('#deleteID').val(me);
		
	}
	
	function deleteVoter(){
		
		var userID = $('#deleteID').val();
	
			$.ajax({
				type: "POST",
				url: "<?php echo base_url(); ?>voter/deleteVoter",
				data: {
					'userID' : userID
				},
				dataType:'json',
				success: function(data){
					if(data.status == 'success'){
						
						location.reload();
						
					}
				}
				
			});
	
	}
	
	function printvoter() {
		$('#print_btn').attr('href','javascript:void(0);');
		var status = $.trim($('#select_status').val()).toUpperCase();
				status = (status == 'VOTED') ? 1 : ((status == 'NO VOTE') ? 0 : 99);
		
		var search = $.trim($('#search_input').val());				
				search = (search !== "") ? search.replace(' ','%20') : 'all';
		
		var url = "<?php echo base_url() ?>voter/printVoter/"+status+"/" + search;
		
		$('#print_btn').attr('target','_blank');
		$('#print_btn').attr('href',url);
		return true;
	}

	function newEntity(){
        if( $('#quickAddEntity').is(":visible")){
            $('#quickAddEntity').slideUp();
        } else {
            $('#quickAddEntity').slideDown();
        }
	}

	function saveEntity(){
		var companyName = $('#qEntity').val();
		var btn = $('#qEntityBtn');

		btn.button('loading');

		$.ajax({
			type: "POST",
			url: "<?php echo base_url(); ?>candidate/saveCompany",
			data: {
				'companyName' : companyName
			},
			dataType:'json',
			success: function(data){
				if(data.status == 'success'){
					$('#companyID').append($("<option></option>").attr("value",data.insertedID).text(companyName)); 

					btn.button('reset');
					
					$('#quickAddEntity').slideUp();
				}
			}
		});
	}
	
	function changestatus(){
		
		var status = $('#status option:selected').val();
		//alert(status);
		location.replace("<?php echo base_url()?>voter/voterlist/" + status);
		
		
	}
	
	function checkpassword(){
		var userPassword = $('#userPassword').val()
		var confirmuserPassword = $('#confirmuserPassword').val();
		var userID = $('#userID').val();
		
		if(userID == "" || userID == null){
			if(userPassword == "" || confirmuserPassword == "")
			{
				$("#saveVoter").attr("disabled", true);
			}
			else{
				if(userPassword == confirmuserPassword){
					$("#saveVoter").attr("disabled", false);
                    $('#confirmuserPassword').css("border", "2px solid green");
                    
				}
				else{
					$("#saveVoter").attr("disabled", true);
                    $('#confirmuserPassword').css("border", "2px solid red");
				}
			}
		}
		else{
			if(userPassword == confirmuserPassword){
				$("#saveVoter").attr("disabled", false);
				$('#confirmuserPassword').css("border", "2px solid green");
			}
			else{
				$("#saveVoter").attr("disabled", true);
        $('#confirmuserPassword').css("border", "2px solid red");
			}
		}
		
	}

	function checkVoterID(){
		var voterID = $('#noAhli').val();
		var userID = $('#userID').val();

		if(voterID == "" | voterID == null){
			$("#saveVoter").attr("disabled", true);
		} else {
			$.ajax({
				type: "POST",
				url: "<?php echo base_url(); ?>voter/checkVoterID",
				data: {
					'userID' : userID,
					'noAhli' : voterID
				},
				dataType:'json',
				success: function(data){
					if(data.status == 'success'){
						$('#noAhli').css("border", "2px solid green");
						$("#saveVoter").attr("disabled", false);
					}
					else{
						$('#noAhli').css("border", "2px solid red");
						$("#saveVoter").attr("disabled", true);
					}
				}
			});
		}
	}
	
</script>

<style>
 /*
 	.modal-content{
    	border-top-left-radius: 2em;
		border-top-right-radius: 2em;
		border-bottom-left-radius: 2em;
		border-bottom-right-radius: 2em;
	}
	.modal-header{
		color:#F0F;
	}
	*/
    .input-err{
        border: 2px solid red;
    }
    .input-suc{
        border: 2px solid green;
    }
	
</style>
