var statusDelay=5000;
var timecheck=1;
var initialDelay=8000;
var page='';
var initial=0;


$(document).ready(function(){
	
	$("form[name='buyAccess']").submit(function(){
		
		var profile_id = $("select[name='paymentType']").val();
		$("input[name='profile_id']").val(profile_id);

		var product_id = $("#pp_"+profile_id).val();
		$("input[name='product_id']").val(product_id);

		
	});

	var access_code = $("input[name='access_code']").val();
	var access_time = $("input[name='access_time']").val();
	$("#access_time_"+access_code).countdown({until: access_time, compact: true, format: 'HMS', description: ''});
	$("#access_time_"+access_code).countdown('pause');

	$("input[name='BTN_start["+access_code+"]']").click(function(){
		acStart(access_code);
	});
	
	$("select[name='paymentType']").change(function(){
		
		var pp=this.value;
		$("select.products").hide();
		$("#pp_"+pp).show();
		
	});
	
	$(".showSignup").click(function(){
		$(".divAccountLogin").hide();
		$(".divPasswordForget").hide();
		$(".divAccountRecover").hide();
		$(".divAccountCreate").show();	
	});
	
	$(".showLogin").click(function(){
		$(".divPasswordForget").hide();
		$(".divAccountCreate").hide();	
		$(".divAccountRecover").hide();	
		$(".divAccountLogin").show();
	});
	
		
	$(".showForget").click(function(){
		$(".divAccountLogin").hide();
		$(".divAccountCreate").hide();
		$(".divAccountRecover").hide();
		$(".divPasswordForget").show();	
	});
		
	$(".showRecover").click(function(){
		$(".divAccountLogin").hide();
		$(".divAccountCreate").hide();
		$(".divPasswordForget").hide();
		$(".divAccountRecover").show();	
	});
	
	$('.'+accountScreen).click();

});


function acStart(access_code)
{
	var onceTrue = 0;
	initial = 1;
	$("#access_time_"+access_code).countdown('resume');
	$("input[name='BTN_start["+access_code+"]']").hide();
	$("input[name='BTN_stop["+access_code+"]']").show();
	
	setTimeout(function(){
		getPaymentStatus(access_code);
	},5000);

	return true;
}

function acStop(access_code)
{
	$("input[name='BTN_stop["+access_code+"]']").hide();
	$("input[name='BTN_start["+access_code+"]']").show();
	$("#access_time_"+access_code).countdown('pause');
	initial = 0;
	stopAccess(access_code);
	
}


function getPaymentStatus(access_code) 
{
	if (access_code == '' || access_code == undefined)  
	{
		return false;
	}

	
	if (initial == 0) return false;
	
	$.ajax({
		dataType:'jsonp',jsonp: 'jsonp_callback',
		data:'mode=multisessionCheck&type=jsonp&access_code='+ access_code +'&r='+Math.random(),
		url: getProtocol() + '//api.pay.nl/',
		success: function (data) 
		{
			var valid = data.valid;
			
			if (valid == 'true')
			{
				onceTrue = 1;
				statusDelay=5000;
				setTimeout(function(){
					getPaymentStatus(access_code)
				},statusDelay);
			}
			else if (onceTrue == 1)
			{
				$("input[name='BTN_stop["+access_code+"]']").hide();
				$("input[name='BTN_start["+access_code+"]']").show();
				$("#access_time_"+access_code).countdown('pause');
				alert ('Your access has been blocked / De toegang is geblokkeerd');
			}
		}
	});
}

function getProtocol(){
  return window.location.protocol;
}


function stopAccess(access_code) 
{
	if (access_code == '' || access_code == undefined)
	{
		return false;
	}

	$.ajax({
		// dataType:'json',
		dataType:'jsonp',jsonp: 'jsonp_callback',
		data:'mode=multisessionStop&type=jsonp&access_code='+ access_code +'&r='+Math.random(),
		url: getProtocol() + '//api.pay.nl/',
		success: function (data) {

			$("input[name='BTN_stop["+access_code+"]']").hide();
			$("input[name='BTN_start["+access_code+"]']").show();
			$("#access_time_"+access_code).countdown('pause');
			
		}
	});
}
