<?
	$_REQUEST["job_id_for_timer"] = ($job_id_for_timer) ? $job_id_for_timer : $_REQUEST["id"];
	$is_running = utility::getTableFieldById("timer","is_running","job_id",$_REQUEST["job_id_for_timer"]);
	$disabled = "";
	if($is_running == 1){
		$disabled = "disabled";
	}
?>
<h2 id="timer_value" style="float:left;clear:right;margin-top:0px;"><time>00:00:00</time></h2>
<button id="start" <?=$disabled;?> class="btn btn-info" style="float:left;clear:right;margin-left:5px;">start</button>
<button id="stop" class="btn btn-warning" style="float:left;clear:right;margin-left:5px;">pause</button>
<button id="clear" class="btn btn-danger" style="float:left;clear:right;margin-left:5px;">stop</button>

<script type="text/javascript">
  var h2 = document.getElementsByTagName('h2')[0],
    start = document.getElementById('start'),
    stop = document.getElementById('stop'),
    clear = document.getElementById('clear'),
    seconds = 0, minutes = 0, hours = 0,
    t;

function add() {
    seconds++;
    if (seconds >= 60) {
        seconds = 0;
        minutes++;
        if (minutes >= 60) {
            minutes = 0;
            hours++;
        }
    }

    h2.textContent = (hours ? (hours > 9 ? hours : "0" + hours) : "00") + ":" + (minutes ? (minutes > 9 ? minutes : "0" + minutes) : "00") + ":" + (seconds > 9 ? seconds : "0" + seconds);

    timer();
}
function timer() {
    t = setTimeout(add, 1000);
}
//timer();


/* Start button */
start.onclick = timer;

/* Stop button */
stop.onclick = function(){
    clearTimeout(t);
}

/* Clear button */
clear.onclick = function(){
	var timer_value = $('#timer_value').text();
	if(timer_value != "00:00:00"){
		if(confirm("Do you really want to stop the timer. If yes then a timesheet entry will be save against this job.")){
			var job_id = '<?=$_REQUEST['job_id_for_timer'];?>';
			var form_data   = {'job_id':job_id,'button':'clear','timer_value':timer_value};       
			$.ajax({
				async: false,
				type: "POST",
				url: site_url+"/modules/enquiry_management/index.php?page=ajax_actions&action=timer_time",
				data: form_data,
				success: function(r){
					location.reload();
				}
			});
				
			h2.textContent = "00:00:00";
			seconds = 0; minutes = 0; hours = 0;
			$("#stop").click();
		}else{
			//$("#stop").click();
		}
	}
}
</script>
<script type="text/javascript">
$(function(){
	$("#start").click(function(){
		$(this).prop('disabled', true);
		var job_id = '<?=$_REQUEST['job_id_for_timer'];?>';
		var timer_value = $('#timer_value').text();
		var form_data   = {'job_id':job_id,'button':'start','timer_value':timer_value};       
		$.ajax({
			async: false,
			type: "POST",
			url: site_url+"/modules/enquiry_management/index.php?page=ajax_actions&action=timer_time",
			data: form_data,
			success: function(result){  
				var resultObj = jQuery.parseJSON(result);    
				hours = resultObj.t_time_hours; 
				minutes = resultObj.t_time_mins; 
				seconds = resultObj.t_time_secs;
			}
		});
	});
	
	$("#stop").click(function(){
		$("#start").prop('disabled', false);
		var job_id = '<?=$_REQUEST['job_id_for_timer'];?>';
		var timer_value = $('#timer_value').text();
		if(timer_value != "00:00:00"){
			var form_data   = {'job_id':job_id,'button':'pause','timer_value':timer_value};       
			$.ajax({
				async: false,
				type: "POST",
				url: site_url+"/modules/enquiry_management/index.php?page=ajax_actions&action=timer_time",
				data: form_data,
				success: function(r){    
					//$('#myModalForSendMail').show().html(r);
				}
			});
		}
	});
	
	$(window).load(function(){
		var job_id = '<?=$_REQUEST['job_id_for_timer'];?>';
		var timer_value = $('#timer_value').text();
		//if(timer_value != "00:00:00"){
			var form_data   = {'job_id':job_id,'button':'page_reload'};       
			$.ajax({
				async: false,
				type: "POST",
				url: site_url+"/modules/enquiry_management/index.php?page=ajax_actions&action=timer_time",
				data: form_data,
				success: function(result){    
					//$('#timer_value').show().html(r);
					var resultObj = jQuery.parseJSON(result);           
                    //alert(resultObj.timer_value_new);
					//$('#timer_value').html(resultObj.timer_value_new);
					pause = resultObj.pause; 
					if(pause == 1){
						$('#timer_value').html(resultObj.timer_value_new);
					}else{
						hours = resultObj.t_time_hours; 
						minutes = resultObj.t_time_mins; 
						seconds = resultObj.t_time_secs;
						$("#start").click();
					}
				}
			});
		//}
	});
});
</script>
<?
	/*$timestamp = time()+date("Z");
	echo "<br><br>";
	echo date('Y-m-d H:i:s')."------->".strtotime(date('Y-m-d H:i:s'))."<br>";
	//echo gmdate('Y-m-d H:i:s')."------->".strtotime(gmdate('Y-m-d H:i:s'))."<br>";
	//echo gmdate('Y-m-d H:i:s',$timestamp)."------->".strtotime(gmdate('Y-m-d H:i:s',$timestamp))."<br>";
	date_default_timezone_set('Europe/London');
	$sql = "SELECT * FROM timer WHERE job_id = ".$_REQUEST['job_id_for_timer']." AND is_start = '1' ";
	$rs = mysqli_query($conn,$sql);
	if(mysqli_num_rows($rs) > 0){
		$row = mysqli_fetch_assoc($rs);
		$changed_on = $row["changed_on"];
		echo "<br>";
		echo date('Y-m-d H:i:s')."------->".strtotime(date('Y-m-d H:i:s'))."<br>";
		echo $changed_on."-------------->".strtotime($changed_on)."<br>";
		echo $seconds  = strtotime(date('Y-m-d H:i:s')) - strtotime($changed_on);
	}*/
?>