<script type="text/javascript">
    $(document).ready(function(){
<!-- page script -->
        $('.datatables').DataTable();
        $('.datetimepicker').datetimepicker({
            viewMode: 'months',
            format: 'DD-MM-YYYY'
        });

    });
    
	function insertmemberNo()
	{
		
		var noAhli = $('#noAhli').val();
		
    	if(noAhli == ""){
			
			
			var opt = '';
			
				opt = "<p style='font-size:16px; font-family:roboto'><?php echo $this->lang->line('cannotempty'); ?></p>";
				
				$('#message').empty();
				$('#message').append(opt);
				$('#popup_modal').modal('show');
			
		}
		else{
			$.ajax({
				type: "POST",
				url: "<?php echo base_url(); ?>voting/checkvote",
				data: {
					'noAhli' : noAhli
				},
				dataType:'json',
				success: function(data){
					
					var opt = '';
					
					if(data.status == 'failed1'){
						opt = "<p style='font-size:16px; font-family:roboto'>safsafasfasf</p>";
				
						$('#message').empty();
						$('#message').append(opt);
						$('#popup_modal').modal('show');
						
					}
					else if(data.status == 'failed2'){
						opt = "<p style='font-size:16px; font-family:roboto'>fadfad</p>";
				
						$('#message').empty();
						$('#message').append(opt);
						$('#popup_modal').modal('show');
					}
					else{
						location.replace('voting/selectcandidate/1');
					}
					
				}
			 });
		}
		
	}
	
	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 isNumberKey(evt){
    	var charCode = (evt.which) ? evt.which : event.keyCode
    	if (charCode > 31 && (charCode < 48 || charCode > 57)){
			return false;
		}
		else{
			return true;
		}
	}
	
	function backprev(){
		history.back(-1);
	}
	
	function radioVote(){
		
			
			$.ajax({
				type: "POST",
				url: "<?php echo base_url(); ?>voting/getdataby_currentpositionID",
				data: {
						'positionID' : "<?php echo $currentpositionID ?>",
						'projectID' : "<?php echo $projectID ?>"
					},
				dataType:'json',
				success: function(data){
					if(data.status == 'success'){
						
						var n = data.message.length;
						
						var i = 0;
						var count = 0;
						var radiobuttonchecked = Array();
						
						for(i=0;i<n;i++){
							
							var radiovoted = document.getElementById("radiovoted_" + data.message[i].candidateID);

							if(radiovoted.checked){
    						
								radiobuttonchecked[count] = data.message[i].candidateID;
								
								count ++;
								
							}// end if(radiovoted.checked){
							
						}// end for(i=0;i<n;i++){
							
							
							if(count == "<?php echo $elected; ?>"){
								
								  $.ajax({
									type: "POST",
									url: "<?php echo base_url(); ?>voting/saveradiobutton",
									data: {
											'userID' : "<?php echo $this->session->userdata('voterUserID') ?>",
											'positionID' : "<?php echo $currentpositionID ?>",
											'projectID' : "<?php echo $projectID ?>",
											'candidateID' : radiobuttonchecked
										},
									dataType:'json',
									success: function(data){
										if(data.status == 'success'){
											
											var nextPositionID = "<?php echo $positionID ?>";
		
											if(nextPositionID != ""){
												var urls = "<?php echo base_url() ?>voting/selectcandidate/<?php echo $positionID; ?>/<?php echo $nextID; ?>/<?php echo $projectID; ?>";
												window.location = urls;
											}
											else{
												var urls = "<?php echo base_url() ?>voting/votesummary/<?php echo $projectID; ?>";
												window.location = urls;
											}
											
										}
									}
								 });
								 
							}
							else{
								
								var opt = "<p style='font-size:16px; font-family:roboto'><?php echo $this->lang->line('failed3') ?></p>";
								
								$('#popup_modal').modal('show');
								
								$('#message').empty();
								$('#message').append(opt);
							}	
							
						
						
					}// end if(data.status == 'success'){
					
				}// end success: function(data){
				
			 });// end $.ajax({
		
		
		
	}
	
	function checkboxVote(){
		
		
			$.ajax({
				type: "POST",
				url: "<?php echo base_url(); ?>voting/getdataby_currentpositionID",
				data: {
						'positionID' : "<?php echo $currentpositionID ?>",
						'projectID' : "<?php echo $projectID ?>"
					},
				dataType:'json',
				success: function(data){
					if(data.status == 'success'){
						
						var n = data.message.length;
						
						var i = 0;
						var count = 0;
						var checkboxchecked = Array();
						
						for(i=0;i<n;i++){
							
							var checkboxvoted = document.getElementById("checkboxvoted_" + data.message[i].candidateID);

							if(checkboxvoted.checked){
    							
								
								checkboxchecked[count] = data.message[i].candidateID;
								
								count ++;
								
							}// end if(checkboxvoted.checked){
							
						}// end for(i=0;i<n;i++){
							
							
						  if(count == "<?php echo $elected; ?>"){
								
								  $.ajax({
									type: "POST",
									url: "<?php echo base_url(); ?>voting/savecheckbox",
									data: {
											'userID' : "<?php echo $this->session->userdata('voterUserID') ?>",
											'positionID' : "<?php echo $currentpositionID ?>",
											'projectID' : "<?php echo $projectID ?>",
											'candidateID' : checkboxchecked
										},
									dataType:'json',
									success: function(data){
										if(data.status == 'success'){
											
											var nextPositionID = "<?php echo $positionID ?>";
		
											if(nextPositionID != ""){
												var urls = "<?php echo base_url() ?>voting/selectcandidate/<?php echo $positionID; ?>/<?php echo $nextID; ?>/<?php echo $projectID; ?>";
												window.location = urls;
											}
											else{
												var urls = "<?php echo base_url() ?>voting/votesummary/<?php echo $projectID; ?>";
												window.location = urls;
											}
											
										}
									}
								 });
							
							}
							else if(count > "<?php echo $elected; ?>"){
								
								var opt = "<p style='font-size:16px; font-family:roboto'><?php echo $this->lang->line('failed6')." ".$elected." ".$this->lang->line('failed7') ?></p>";
								
								$('#popup_modal').modal('show');
								
								$('#message').empty();
								$('#message').append(opt);
							}
							else if(count == 0){
								
								var opt = "<p style='font-size:16px; font-family:roboto'><?php echo $this->lang->line('failed3') ?></p>";
								
								$('#popup_modal').modal('show');
								
								$('#message').empty();
								$('#message').append(opt);
							}
							else{
								
								var opt = "<p style='font-size:16px; font-family:roboto'><?php echo $this->lang->line('failed4')." ".$elected." ".$this->lang->line('failed8') ?></p>";
								
								$('#popup_modal').modal('show');
								
								$('#message').empty();
								$('#message').append(opt);
							}
							
						
						
					}// end if(data.status == 'success'){
					
				}// end success: function(data){
				
			 });// end $.ajax({
		
		
	}
	
	
	function checktableradiobutton(candidateID){
			
		document.getElementById("radiovoted_" + candidateID).checked = true;
	
	}
	
	function checktablecheckbox(candidateID){
	
		var checkboxvoted = document.getElementById("checkboxvoted_" + candidateID);

		if(checkboxvoted.checked){
			document.getElementById("checkboxvoted_" + candidateID).checked = false;
		}
		else{
			document.getElementById("checkboxvoted_" + candidateID).checked = true;
		}
	
	}
	
	function checktablecheckbox2(candidateID){
	
		
		$.ajax({
				type: "POST",
				url: "<?php echo base_url(); ?>voting/getdataby_currentpositionID",
				data: {
						'positionID' : "<?php echo $currentpositionID ?>",
						'projectID' : "<?php echo $projectID ?>"
					},
				dataType:'json',
				success: function(data){
					if(data.status == 'success'){
						
						var n = data.message.length;
						
						var i = 0;
						var count = 1;
						var checkboxchecked = Array();
						
						for(i=0;i<n;i++){
							
							var checkboxvoted = document.getElementById("checkboxvoted_" + data.message[i].candidateID);

							if(checkboxvoted.checked){
    							
								
								checkboxchecked[count] = data.message[i].candidateID;
								
								count ++;
								
							}// end if(checkboxvoted.checked){
							
						}// end for(i=0;i<n;i++){
							
							
						  	if(count > "<?php echo $elected; ?>"){
								
								var checkboxvoted = document.getElementById("checkboxvoted_" + candidateID);
								
								if(checkboxvoted.checked){
									document.getElementById("checkboxvoted_" + candidateID).checked = false;
								}
								else{
									var opt = "<p style='font-size:16px; font-family:roboto'><?php echo $this->lang->line('failed6')." ".$elected." ".$this->lang->line('failed7') ?></p>";
								
									$('#popup_modal').modal('show');
								
									$('#message').empty();
									$('#message').append(opt);
								}
								
							}
							else{
								
								var checkboxvoted = document.getElementById("checkboxvoted_" + candidateID);
								
								if(checkboxvoted.checked){
									document.getElementById("checkboxvoted_" + candidateID).checked = false;
								}
								else{
									document.getElementById("checkboxvoted_" + candidateID).checked = true;
								}
							}
							
						
						
					}// end if(data.status == 'success'){
					
				}// end success: function(data){
				
			 });// end $.ajax({
		
		
	
	}
	
</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;
	}
	
	.everytable{
		 border-radius: 25px;
    	 border: 2px solid #CCC;
    	 padding: 20px;
    	 width: 90%;
    	 height: 90%; 
	}
	
</style>


                <div id="popup_modal" class="modal fade in">
                  <div class="modal-dialog">
                    <div class="modal-content">
                      <div class="modal-header">
                        <h3 class="modal-title"><?php echo $this->lang->line('message') ?></h3>
                      </div>
                      <div class="modal-body" id="message">
                      	<p style="font-size:16px; font-family:roboto"></p>
                      </div>
                      <div class="modal-footer">
                        <button type="button" class="btn btn-warning" data-dismiss="modal"><i class="fa fa-minus-circle"></i> <?php echo $this->lang->line('close') ?></button>
                      </div>
                      
                    </div><!-- /.modal-content -->
                  </div><!-- /.modal-dialog -->
                </div><!-- end addRowModal -->


<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <!--<section class="content-header">
        <h1 style="font-family:roboto">
            <b>&nbsp;</b>
        </h1>
    </section>-->

    <!-- Main content -->
    <section class="content">
        


        <div class="row">
            <div class="col-md-12">
                <!-- Horizontal Form -->

                <!-- general form elements disabled -->
                <div class="box box-info">
                    <div class="box-header with-border" style=" background-color:#00F">
                        <h3  class="box-title">
                            <h4 class="pull-left" style="font-family:roboto; color:#FFF"><b><?php echo $positionName; ?></b></h4>
                        </h3>
                    </div><!-- /.box-header -->
                    
                    
                    <div class="box-body">
                    	<div class="row">
                        	<div class="col-md-12">
                            <?php if($elected > 1){echo "<p style='font-family:roboto; color:#F90; font-size:18px'><b>".$this->lang->line('reminder1').' '.$elected.' '.$this->lang->line('reminder2')."</b></p>";} ?>
                            	<table width="100%" height="100% "border="0">
                            	<?php
									$count=1;
									$limiter = $countcandidate;
									
									foreach($candidateList as $row){
										$i=0;
										if($i < $limiter){
											if($count == 1 && $limiter > 4){
											echo"<tr><td align='center'>";
											}
											else{
											echo"<td align='center'>";
											}
								?>
                                
											
                                                <table class="everytable" onclick="<?php if($elected == 1){echo 'checktableradiobutton('.$row->candidateID.')';}else{echo 'checktablecheckbox2('.$row->candidateID.')';}?>">
                                                    <tr>
                                                        <td rowspan="2" width="10%" align="center">
                                                        <?php
															if($row->elected == 1){	
                                                            	echo "<input id='radiovoted_".$row->candidateID."' name='radiovoted' type='radio' />";
															}
															else{
																echo "<input id='checkboxvoted_".$row->candidateID."' name='checkboxvoted' type='checkbox' onclick='checktablecheckbox(".$row->candidateID.")' />";
															}
														?>
                                                        </td>
                                                        <td height="70%"><img src="<?php echo base_url().'img/candidate/'.$row->candidate_picture; ?>" width="150px" height="150px"/></td>
                                                    </tr>
                                                    <tr>
                                                        <td height="30%"><p style=" font-size:16px"><b><?php echo $row->candidateName; ?></b></p></td>
                                                    </tr>
                                                </table>&nbsp;
                                            
                                    
                                <?php
											if($count < 4){
											echo"</td>";
											}
											else{
											echo"</td></tr>";
											$count = 0;
											}
										}
										else{
											echo"<td align='center'>";
								?>
                                
                                			
                                                <table class="everytable" onclick="<?php if($elected == 1){echo 'checktableradiobutton('.$row->candidateID.')';}else{echo 'checktablecheckbox2('.$row->candidateID.')';}?>">
                                                    <tr>
                                                        <td rowspan="2" width="10%" align="center">
                                                        <?php
															if($row->elected == 1){	
                                                            	echo "<input id='radiovoted_".$row->candidateID."' name='radiovoted' type='radio' />";
															}
															else{
																echo "<input id='checkboxvoted_".$row->candidateID."' name='checkboxvoted' type='checkbox' onclick='checktablecheckbox(".$row->candidateID.")' />";
															}
														?>
                                                        </td>
                                                        <td height="70%"><img src="<?php echo base_url().'img/candidate/'.$row->candidate_picture; ?>" width="150px" height="150px"/></td>
                                                    </tr>
                                                    <tr>
                                                        <td height="30%"><p style=" font-size:16px"><b><?php echo $row->candidateName; ?></b></p></td>
                                                    </tr>
                                                </table>&nbsp;
                                            
                                
								<?php
											echo"</td></tr>";		
										}
										
										$count++;
										$i++;
									}
								?>
                                </table>
                                <br /><button type="button" onclick="<?php if($elected == 1){echo'radioVote()';}else{echo'checkboxVote()';} ?>" class="btn bg-purple btn-lg fad" title="Submit"><?php echo $this->lang->line('submit');?> <i class="fa fa-mail-forward"></i></button><br /><br />
                                <button type="button" onclick="backprev()" class="btn btn-danger btn-lg fad" title="Back"><i class="fa fa-reply"></i> <?php echo $this->lang->line('back');?></button>
                            </div>
          				</div>
                    </div><!-- /.box-body -->
                    
                    
                </div><!-- /.box -->
                
                
            </div>
        </div>


    </section><!-- /.content -->
</div><!-- /.content-wrapper -->
