/*
##########################################################################
© versicherungsmarkt.de gmbh
Version 4[VEMA], 23.11.2018
##########################################################################
*/
var confirm_form_url	=	"https://landingpage.vmproduct.de/globcont/confirm_form.php?m=eller&src=vema_alias&p=bar-rad-ebike";
function init_linkconfirm(elem) {
	//console.log("LinkConfirm (c) by www.versicherungsmarkt.de");
	if(typeof(elem)=="object") {
		var tgt = elem;
	}
	else {
		var tgt = $("BODY");
	}
	$(tgt).find('a.confirm_extern').on("click", function(e) {
		e.preventDefault();
		var href = $(this).attr('href');
		var target = $(this).attr('target');
		// console.log(confirm_form_url);
		// console.log(href);
		// console.log(target);
		$.ajax({
			url: confirm_form_url,
			dataType: "jsonp",
			jsonpCallback: 'callback',
			type: 'GET',
			success: function (confirm_form) {
				// console.log(confirm_form);
				if(confirm_form.html!="") {
					$("body").prepend("
");
					$("#confirm_link_overlay").on("click", function(event) {
						if(event.target==this) {
							linkconfirm_close();
						}
					});
					$("body #confirm_link_extern form").on("submit", function(e) {
						e.preventDefault;
						if(check_user_confirm($(this))===true) {
							linkconfirm_close();
							if(typeof(target)=="undefined" || target=="_top") {
								document.location.href=href;
							}
							else {
								window.open(href);							
							}
						}
						else {
							$('#confirm_link_extern').addClass("error");
							setTimeout(function() {
							  $("#confirm_link_extern").removeClass('error');
							}, 1000);
							//alert("Bitte bestätigen Sie die angezeigten Punkte!");
						}
						return false;
					});
				}
			}
		});
	});
	$(tgt).find('iframe.confirm_extern').each(function(layindex) {
		var iframe=$(this);
		var width=iframe.outerWidth();
		var height=iframe.outerHeight();
		//Verschiebungen berücksichtigen
		var position=iframe.css("position");
		var left=iframe.css("left");
		var right=iframe.css("right");
		var top=iframe.css("top");
		var bottom=iframe.css("bottom");
		var margin=iframe.css("margin");
		var transform=iframe.css("transform");
		iframe.addClass("unconfirmed");
		$.ajax({
			url: confirm_form_url,
			dataType: "jsonp",
			jsonpCallback: 'callback_iframe'+layindex,
			type: 'GET',
			success: function (confirm_form) {
				if(confirm_form.html!="") {
					var lay_id="confirm_ext_"+layindex;
					var temp=""+confirm_form.html+"
";
					$(temp).insertAfter(iframe);
					$("#"+lay_id+" a[href='javascript:linkconfirm_close()']").remove();
					$("#"+lay_id).find("form").on("submit", function(e) {
						e.preventDefault;
						if(check_user_confirm($(this))===true) {
							iframeconfirm_close(lay_id, true);
						}
						else {
							$("#"+lay_id).addClass("error");
							setTimeout(function() {
							  $("#"+lay_id).removeClass('error');
							}, 1000);
							//alert("Bitte bestätigen Sie die angezeigten Punkte!");
						}
						return false;
					});
					//Styles vom iframe auf DIV anwenden
					$("#"+lay_id).css("width", width+"px");
					$("#"+lay_id).css("height", height+"px");
					if(position!="static") {
						$("#"+lay_id).css("position", position);						
					}
					$("#"+lay_id).css("left", left);
					$("#"+lay_id).css("right", right);
					$("#"+lay_id).css("top", top);
					$("#"+lay_id).css("bottom", bottom);
					$("#"+lay_id).css("margin", margin);
					$("#"+lay_id).css("transform", transform);
					
					//IDs korrigieren
					$("#"+lay_id).find("input").each(function() {
						id=$(this).attr("id");
						if(typeof(id)!=="undefined") {
							$(this).attr("id", id+""+layindex);
						}
					});
					$("#"+lay_id).find("label").each(function() {
						temp=$(this).attr("for");
						if(typeof(temp)!=="undefined") {
							$(this).attr("for", temp+""+layindex);
						}
					});
					
					//Schließen-Link anpassen
					$("#"+lay_id).find("form > a:last-child").attr('href', 'javascript:iframeconfirm_close(\''+lay_id+'\')');
				}
			}
		});
	});
	$(tgt).find('.confirm_intern').each(function(layindex) {
		var locked_layer=$(this);
		//console.log(locked_layer);
		var pos=$(this).css("position");
		$(this).addClass("linkconfirm_overflow");
		$(this).css("overflow", "hidden");
		if(pos=="static") {
			$(this).css("position", "relative");
			var classname="absolute";
		}
		if($(this).prop('tagName')=="BODY") {
			$("html, body").scrollTop(0,0);
		}
		
		$.ajax({
			url: confirm_form_url,
			dataType: "jsonp",
			jsonpCallback: 'callback_intern'+layindex,
			type: 'GET',
			success: function (confirm_form) {
				if(confirm_form.html!="") {
					$(locked_layer).prepend("");
					$(locked_layer).find("#confirm_link_extern a[href='javascript:linkconfirm_close()']").remove();
					$(locked_layer).find("#confirm_link_extern form").on("submit", function(e) {
						e.preventDefault;
						if(check_user_confirm($(this))===true) {
							linkconfirm_close();
						}
						else {
							$('#confirm_link_extern').addClass("error");
							setTimeout(function() {
							  $("#confirm_link_extern").removeClass('error');
							}, 1000);
							//alert("Bitte bestätigen Sie die angezeigten Punkte!");
						}
						return false;
					});
				}
			}
		});
	});
}
$(document).ready(function() {
	init_linkconfirm();
});
function check_user_confirm(f) {
	var ok=true;
	$(f).find("input[type=checkbox]:not(:checked)").each(function() {
		ok=false;
	});
	return ok;
}
function linkconfirm_close() {
	$("#confirm_link_extern").remove();
	$("#confirm_link_overlay").remove();
	$(".linkconfirm_overflow").css("overflow", "inherit");
	$(".linkconfirm_overflow").removeClass("linkconfirm_overflow");	
}
function iframeconfirm_close(lay_id, ok) {
	if(ok===true) {
		$("#"+lay_id).prev("iframe").removeClass("unconfirmed");		
		$("#"+lay_id).prev("iframe").removeClass("disabled");		
		$("#"+lay_id).remove();
	}
	else {
		//$("#"+lay_id).prev("iframe").addClass("disabled");
		$("#"+lay_id).addClass('declined');
	}
}
/*
##########################################################################
########## Versionshistorie:
##########################################################################
+ Version 1: Lokale Implementierung
+ Version 2: Zentrale Implementierung
+ Version 3: Ausweitung auf iFrames
+ Version 4: Gestalterische Verbesserungen / Implementierung confirm_intern
*/