var popupStatus = 0;
var siteurl='/sites/all/themes/cap/membership_application/signature/';
function wait_timer(ms){
	var dt = new Date();
	dt.setTime(dt.getTime() + ms);
	while (new Date().getTime() < dt.getTime());
}
function filevalidate(){
var App_ID= $(':input[name=Applicant_ID]').val();
var tempfilestatus=false;
	$.ajax({
	type: "POST",
	url: siteurl+"filevalidate.php",
	cache:false,
	async:false,
	data:"applicantID=" +App_ID ,
	success: function(data){
		if(data=='found'){
			tempfilestatus=true;
		}
		else{
			alert('please sign the document using any of the given ways to continue with next steps');

		}
	},
	error:function(xhr, ajaxOptions, thrownError){
		alert('please try again, their may be some problem with your internate connection speed')
	}
	});
	return tempfilestatus;
}
//loading popup with jQuery magic!
function loadPopup(){
//loads popup only if it is disabled
	if(popupStatus==0){
		popupStatus = 1;
		$('.divEmailClose').css('color', '#524A2E');
		$('#container').wrap('<div id="parentpopup" />&nbsp;</div>');
		$("#parentpopup").css({
			"opacity": "0.3"
		});
		$("#parentpopup").fadeIn("slow");
		$("#popupContact").fadeIn("slow");
		$('#parentpopup').css('cursor', 'wait');
		$('html, body').animate({scrollTop:0}, 'slow');
		$('#parentpopup').children("a").click(
			function () {
			return false;
			});
	}
}
//disabling popup with jQuery magic!
function disablePopup(){
	if(popupStatus==1){
		$("#container").unwrap();
		$("#popupContact").fadeOut("slow");
		popupStatus = 0;
		var waiturl=siteurl+'waiting.html';
		$('#Sframe').attr('src',waiturl);
		$('#Sframe').load();
	}
}
function scroltop(){
	$('html, body').animate({scrollTop:0}, 'slow');
}
//centering popup
function centerPopup(){
	//request data for centering
	var leftmargin = ($(window).width()/2)-($("#popupContact").width()/2);
	//centering
	$("#popupContact").css({
		"top": 140,
		"left": 20
	});
	$("#backgroundPopup").css({
		//"height": windowHeight
	});
}
function OpenPopUp(){     
	//centering with css
	centerPopup();
	//load popup
	loadPopup();		
}	
function digicheck(){
$('#digitalcheck').attr('checked', true);
	if($('#uploadcheck').attr('checked', true)){
		$('#uploadcheck').attr('checked', false);   }
		//$('#uploaddoc').hide(1000);
		//$('#acceptdoc').show(1000);
		$('#uploaddoc :input').attr('disabled', true);
		$('#uploaddoc').css('color','#B3B0AB');
		$('#uploaddoc a').css({'color':'#B3B0AB','text-decoration':'none'});		
		$('#uploadlabel').css('color','#B3B0AB');
		$('#uploaddoc :input').css('color','#B3B0AB');
		$('#file_signature').css('color','#B3B0AB');
		if($('#acceptdoc :input').attr('disabled', true)){
			$('#accept').css('color','#000000');
			$('#acceptdoc').css('color','#000000');
			$('#acceptlabel').css('color','#524A2E');			
			$('#acceptdoc :input').removeAttr('disabled');
		}
}
function upcheck(){
	if($('#digitalcheck').attr('checked', true)){
		$('#digitalcheck').attr('checked', false);   
	}
		$('#acceptdoc :input').attr('disabled', true);
		$('#accept').css('color','#B3B0AB');
		$('#acceptdoc').css('color','#B3B0AB');
		$('#acceptlabel').css('color','#B3B0AB');
		$('#uploaddoc').css('color','#524A2E');
		$('#uploaddoc :input').css('color','#524A2E');
		$('#uploaddoc a').css({'color':'#003057','text-decoration': 'underline'});
		$('#uploaddoc').css({'color':'#524A2E','text-decoration': 'none'});	
		$('#uploadlabel').css('color','#524A2E');
		if($('#uploaddoc :input').attr('disabled', true)){
		$('#uploaddoc :input').removeAttr('disabled');
		$('#uploaddoc :input').css('color','#000000');
	}
}
$(document).ready(function(){
	digicheck();		
	$('#digitalcheck').click(function(){
		digicheck();		
	});		  
	$('#uploadcheck').click(function(){
		upcheck();
	});
    $('#accept').click(
		function(){ 
			//code for popup bindiging with current window	
			$("#popupContactClose").click(function(){
				disablePopup();
			});
			//Click out event!
			$("#backgroundPopup").click(function(){
				disablePopup();
			});
			//Press Escape event!
			$(document).keypress(function(e){
				if(e.keyCode==27 && popupStatus==1){
					disablePopup();
				}
			});
			if($(':input[name=PERSONALINFORMATION_Fname]').val()==''){
				alert('please fill name field on step 4 to continue with this step');
			}
			else if($(':input[name=ADDRESSES_PrimaryOfficeCity]').val()==''){
				alert('please fill city field on step 7 to continue with this step');
			}
			else if($(':input[name=ADDRESSES_PrimaryOfficeState]').val()==''){
				alert('please fill state field on step 7 to continue with this step');
			}
			else if($(':input[name=ID_Email_Address]').val()==''){
				alert('please fill email field on step 1 to continue with this step');
			}
			else{
				var User_Email=$(':input[name=ID_Email_Address]').val();
				var User_Applicant_ID=$(':input[name=Applicant_ID]').val();
				var User_Fname=$(':input[name=PERSONALINFORMATION_Fname]').val();
				var User_Mname=$(':input[name=PERSONALINFORMATION_Mname]').val();
				var User_Lname=$(':input[name=PERSONALINFORMATION_Lname]').val();
				User_Mname = User_Mname ? ' ' + User_Mname + ' ' : ' ';
				var User_name = User_Fname + User_Mname + User_Lname;
				var User_City= $(':input[name=ADDRESSES_PrimaryOfficeCity]').val();
				var User_State=$(':input[name=ADDRESSES_PrimaryOfficeState]').val();OpenPopUp();
				//$('#certificate').html('<p>Wait for pdf Creation</p>');
				$.ajax({
				type: "POST",
				url: siteurl+"tcpdf/pdf/pdf.php",
				data:"name=" + User_name +"&email=" + User_Email +"&city=" + User_City+"&state=" + User_State,
				dataType: 'json',
				success: function(data){
					if(data.error=='pdfsucces'){
						$.ajax({
						type: "POST",
						url: siteurl+"signature.php",
						data:"filename=" + data.filename +"&User_Email=" + User_Email +"&User_Applicant_ID=" + User_Applicant_ID,
						success: function(data){
						var frameurl=data;
						$('#Sframe').attr('src',frameurl );
						$('#Sframe').bind('disablepopup', function() {
						$("#container").unwrap();
						$("#popupContact").fadeOut("slow");
						popupStatus = 0; });
					
						$('#Sframe').bind('scrolltop', function() {
						$('html, body').animate({scrollTop:0}, 'slow');  });
					
						$('#Sframe').load();

						},
						error:function(xhr, ajaxOptions, thrownError){
						alert(xhr.status);
						alert(thrownError);
						alert('some error occured while signature creation, please try again');}
						});
					}
					else{
						alert(data.msg);
						}
					},
				error:function(xhr, ajaxOptions, thrownError){
					alert(xhr.status);
                    alert(thrownError);
					alert('some error while pdf creation')
				}
				});    
			}
			
    });
			
})
