﻿if(typeof jQuery=='undefined') 
{
	var headTag = document.getElementsByTagName("head")[0];
	var jqTag = document.createElement('script');
	jqTag.type = 'text/javascript';
	jqTag.src = 'https://www.dein-stellplatz.de/supportchat/public/js/jquery.min.js';
	jqTag.onload = init_profectus_sc;
	headTag.appendChild(jqTag);
}
else
{
	init_profectus_sc();
}

//var onoffline = 'offline';
var chatstatus = '';
var onofflineint = null;
var chatint = null;
var messint = null;
var manuallyclicked = false;
var lang = 'pl';
var esjgke = '12';

var chatgreetings = {
	'de': 'Hi, ich bin NAME - kein Roboter :-)<br />Wie kann ich dir helfen?',
	'en': 'Hi, I\'m NAME - not robot :-)<br />How can I help you?',
	'pl': 'Cześć, jestem NAME - nie robot :-)<br />Jak mogę ci pomóc?'
}

function init_profectus_sc()
{
	$("head").append($("<link rel='stylesheet' href='https://www.dein-stellplatz.de/supportchat/frontend/getcssnew?v=1730201430&lang=pl' type='text/css' media='screen' />"));
	var html = '';
	html += '<div class="supportchatnewouter ">';
		html += '<img src="https://www.dein-stellplatz.de/supportchat/public/img/schliessen-Icon.svg" class="supportchatclose" alt="close" onclick="closesupportchat();" />';
		html += '<div class="supportchatnewonline">';
			html += '<div class="supportchatclosed supportchatclosedonline" onclick="openchatonline();">';
				html += '<img src="https://www.dein-stellplatz.de/supportchat/public/img/Live-Chat-Icon.svg" class="supportchatonlineicon" />';
				html += '<div class="supportchatsmallonline"><div class="supportchatsmallonlineinner">Spersonalizowane. Szybko. Wygodnie.</div></div>';
				html += '<div class="supportchatsmallsub"><div class="supportchatsmallsubinner">Z przyjemnością Ci pomożemy! <div class="supportchatsmallsubperholder"><img src="https://www.dein-stellplatz.de/supportchat/public/img/Chat-Mitarbeiter-alle.webp" alt="Chat-Support" /><span>+2</span></div></div></div>';
			html += '</div>';
			html += '<div class="supportchatopened supportchatopenedonline">';
				html += '<div class="supportchatopenedinner">';
					html += '<div class="supportchatheaderouter">';
						html += '<img src="https://www.dein-stellplatz.de/supportchat/public/img/header-aktiv.svg" alt="aktiv" />';
						html += '<span>Spersonalizowane. Szybko. Wygodnie.</span>';
					html += '</div>';
					html += '<div class="supportchatopeninner">';
						html += '<div class="supportchatmessages"></div>';
						html += '<div class="supportchatsendform">';
							html += '<input type="text" placeholder="wiadomość" id="profectus_sc_chat_message" />';
							html += '<img src="https://www.dein-stellplatz.de/supportchat/public/img/absenden-Icon.svg" alt="send" class="supportchatsend" onclick="profectus_sc_sendmessage()" />';
							html += '<div class="supportchatclear"></div>';
							html += '<div class="supportchatcaptchaclaim">Pokaż nam, że też nie jesteś robotem ;-)</div>';
							html += '<div class="g-recaptcha" data-sitekey="6Lf0EIkpAAAAAFg-12N_4lQhVdeAUB94e99uSJLe"></div>';
						html += '</div>';
					html += '</div>';
				html += '</div>';
			html += '</div>';
		html += '</div>';
		html += '<div class="supportchatnewoffline">';
			html += '<div class="supportchatclosed supportchatclosedoffline" onclick="openchatoffline();">';
				html += '<img src="https://www.dein-stellplatz.de/supportchat/public/img/Live-Chat-Icon-inaktiv.svg" class="supportchatonlineicon" />';
				html += '<div class="supportchatsmallonline"><div class="supportchatsmallonlineinner"><br /></div></div>';
				html += '<div class="supportchatsmallsub"><div class="supportchatsmallsubinner">Z przyjemnością Ci pomożemy!</div></div>';
			html += '</div>';
			html += '<div class="supportchatopened supportchatopenedoffline">';
				html += '<div class="supportchatopenedinner">';
					html += '<div class="supportchatheaderouter"><img src="https://www.dein-stellplatz.de/supportchat/public/img/header-inaktiv.svg" alt="aktiv" /></div>';
					html += '<div class="supportchatopeninner">';
						html += '<div class="supportchatofflinehint">';
							html += '<strong>Jesteśmy obecnie offline.</strong> Zostaw nam wiadomość. Odezwiemy się jak najszybciej.';
						html += '</div>';
						html += '<div class="supportchatofflineform">';
							html += '<input type="text" id="profectus_sc_offline_name" placeholder="Twoje imię*" />';
							html += '<input type="text" id="profectus_sc_offline_mail" placeholder="e-Mail*" />';
							html += '<input type="text" id="profectus_sc_offline_tel" placeholder="numer telefonu" />';
							html += '<textarea id="profectus_sc_offline_message" placeholder="wiadomość"></textarea>';
							html += '<div class="g-recaptcha" data-sitekey="6Lf0EIkpAAAAAFg-12N_4lQhVdeAUB94e99uSJLe"></div>';
							html += '<div class="supportchatsendofflinebtn" onclick="profectus_sc_sendoffline();">wyślij</div>';
						html += '</div>';
					html += '</div>';
				html += '</div>';
			html += '</div>';
		html += '</div>';
	html += '</div>';
	html += '<div class="profectus_sc_infowindow">';
		html += '<div class="profectus_sc_infoinner">';
			html += '<div id="profectus_infomessages"></div>';
			html += '<div class="profectus_sc_infobtn" onclick="profectus_sc_closeinfo();">Okay</div>';
		html += '</div>';
	html += '</div>';
			
			
			
				
	$('body').append(html);
	
	profectus_sc_checkoffonline();
	profectus_sc_checkcookie();
	
}

function closesupportchat()
{
	$('.supportchatnewouter').addClass('scclosed');
	$('.supportchatnewouter').removeClass('scopen');
	profectus_sc_setcookie('profectus_sc_isopen', '2', 1);
}

function openchatoffline()
{
	$('.supportchatnewouter').removeClass('scclosed');
	$('.supportchatnewouter').addClass('scopen');
	profectus_sc_setcookie('profectus_sc_isopen', '1', 1);
}

function openchatonline()
{
	$('.supportchatnewouter').removeClass('scclosed');
	$('.supportchatnewouter').addClass('scopen');
	profectus_sc_setcookie('profectus_sc_isopen', '1', 1);
	setTimeout(function(){ 
		profectus_sc_sendgreetingmessage(chatgreetings.pl);
	}, 2000);
	
}

function profectus_sc_closechat()
{
	if (confirm('Chat wirklich beenden?'))
	{
		$.ajax({
			url: "https://www.dein-stellplatz.de/supportchat/frontend/closechat",
			jsonp: "callback",
			dataType: "jsonp",
			data: {
				token: profectus_sc_getcookie('profectus_sc_token')
			},
			success: function( response ) {
				$('.profectus_sc_regonline').show();
				$('.profectus_sc_chat').css('display','none');
				clearInterval(chatint);
				chatint = null;
				profectus_sc_setcookie('profectus_sc_token', '', -100);
			}
		});
		
	}
}

function profectus_sc_clearofflineform()
{
	$('#profectus_sc_offline_name').val('');
	$('#profectus_sc_offline_mail').val('');
	$('#profectus_sc_offline_tel').val('');
	$('#profectus_sc_offline_message').val('');
}

function profectus_sc_sendoffline()
{
	var error = '';
	if ($('#profectus_sc_offline_name').val()=='')
	{
		error += '<p class="profectus_sc_error">Proszę wpisać swoje imię!</p>';
	}
	if (!profectus_sc_validate_email($('#profectus_sc_offline_mail').val()))
	{
		error += '<p class="profectus_sc_error">Proszę wpisać aktualny adres e-mail!</p>';
	}
	if(grecaptcha.getResponse(1) == "") 
	{
		error += '<p class="profectus_sc_error">Potwierdź, że nie jesteś robotem!</p>';
	}
	if (error=='')
	{
		$.ajax({
			url: "https://www.dein-stellplatz.de/supportchat/frontend/sendoffline",
			jsonp: "callback",
			dataType: "jsonp",
			data: {
				name: $('#profectus_sc_offline_name').val(),
				email: $('#profectus_sc_offline_mail').val(),
				tel: $('#profectus_sc_offline_tel').val(),
				message: $('#profectus_sc_offline_message').val(),
				fulluri: document.location.href
			},
			success: function( response ) {
								if (response[0]=='success')
				{
					profectus_sc_showinfo('<p>Wielkie dzięki!<br />Skontaktujemy się z Tobą jak najszybciej.</p>');
					profectus_sc_clearofflineform();
				}
				
			}
		});
	}
	else
	{
		profectus_sc_showinfo(error);
	}
}


function profectus_sc_sendmessage()
{
	var error = '';
	if ($('#profectus_sc_chat_message').val()=='')
	{
		error += '<p class="profectus_sc_error">Proszę wprowadzić swoją wiadomość czat!</p>';
	}
	var recaptcha = $("#g-recaptcha-response").val();
	if(grecaptcha.getResponse(0) == "") 
	{
		error += '<p class="profectus_sc_error">Potwierdź, że nie jesteś robotem!</p>';
	}
	if (error=='')
	{
		overridefrom = '';
		if (currentcustomer!=false)
		{
			overridefrom = currentcustomer.cunumber+' | '+currentcustomer.cuemail;
		}
		$.ajax({
			url: "https://www.dein-stellplatz.de/supportchat/frontend/sendmessage",
			jsonp: "callback",
			dataType: "jsonp",
			data: {
				token: profectus_sc_getcookie('profectus_sc_token'),
				message: $('#profectus_sc_chat_message').val(),
				fulluri: document.location.href,
				overridefrom: overridefrom
			},
			success: function( response ) {
				$('#profectus_sc_chat_message').val('');
				profectus_sc_getmessages();
				profectus_sc_setcookie('profectus_sc_token', profectus_sc_getcookie('profectus_sc_token'), 1);
			}
		});
	}
	else
	{
		profectus_sc_showinfo(error);
	}
}

function profectus_sc_sendspecialmessage(message)
{
	
	$.ajax({
		url: "https://www.dein-stellplatz.de/supportchat/frontend/sendspecialmessage",
		jsonp: "callback",
		dataType: "jsonp",
		data: {
			token: profectus_sc_getcookie('profectus_sc_token'),
			message: message,
			fulluri: document.location.href
		},
		success: function( response ) {
			$('#profectus_sc_chat_message').val('');
			$('.profectus_sc_messages').html('');
			profectus_sc_getmessages();
			profectus_sc_setcookie('profectus_sc_token', profectus_sc_getcookie('profectus_sc_token'), 1);
			profectus_sc_setcookie('profectus_sc_isopen', '1', 1);
			$('body').addClass('profectus_sc_openbody');
			supportchatsetopenstatus();
		}
	});
	
}

function profectus_sc_sendgreetingmessage(message)
{
	if ($('.profectus_sc_greeting').length==0 && chatstatus=='online')
	{
		$.ajax({
			url: "https://www.dein-stellplatz.de/supportchat/frontend/sendgreetingmessage",
			jsonp: "callback",
			dataType: "jsonp",
			data: {
				token: profectus_sc_getcookie('profectus_sc_token'),
				message: message,
				fulluri: document.location.href
			},
			success: function( response ) {
				$('#profectus_sc_chat_message').val('');
				$('.profectus_sc_messages').html('');
				profectus_sc_getmessages();
				profectus_sc_setcookie('profectus_sc_token', profectus_sc_getcookie('profectus_sc_token'), 1);
				profectus_sc_setcookie('profectus_sc_isopen', '1', 1);
				$('.profectus_sc_outer').addClass('profectus_sc_open');
				$('body').addClass('profectus_sc_openbody');
			}
		});
	}
}

function profectus_sc_regonline()
{
	var error = '';
	/*
	if ($('#profectus_sc_online_name').val()=='')
	{
		error += '<p class="profectus_sc_error">Proszę wpisać swoje imię!</p>';
	}
	
	if (!profectus_sc_validate_email($('#profectus_sc_online_mail').val()))
	{
		error += '<p class="profectus_sc_error">Proszę wpisać aktualny adres e-mail!</p>';
	}
	*/
	if (error=='')
	{
		$.ajax({
			url: "https://www.dein-stellplatz.de/supportchat/frontend/reqonline",
			jsonp: "callback",
			dataType: "jsonp",
			data: {
				name: '',
				email: '',
				tel: '',
				domain: document.domain
			},
			success: function( response ) {
				if (response[0]=='success')
				{
					profectus_sc_setcookie('profectus_sc_token', response[1], 1);
					profectus_sc_startchat();
				}
			}
		});
	}
	else
	{
		profectus_sc_showinfo(error);
	}
}

function supportchatsetopenstatus()
{
	var currentclosestate = profectus_sc_getcookie('profectus_sc_isopen');
	console.log(currentclosestate);
	if (currentclosestate=='2')
	{
		closesupportchat();
	}
	else if (currentclosestate=='1')
	{
		if (chatstatus == 'offline')
		{
			openchatoffline();
		}
		else if (chatstatus == 'online')
		{
			openchatonline();
		}
	}
}

function profectus_sc_checkcookie()
{
	var currenttoken = profectus_sc_getcookie('profectus_sc_token');
	if (currenttoken!='')
	{
		profectus_sc_startchat();
	}
	else
	{
		profectus_sc_regonline();
	}
	supportchatsetopenstatus();
}

function profectus_sc_startchat()
{
	$('.profectus_sc_regonline').hide();
	$('.profectus_sc_chat').css('display','block');
	profectus_sc_getmessages();
	//chatint = setInterval(profectus_sc_getmessages, 5000);
	
}

function profectus_sc_showinfo(content)
{
	$('#profectus_infomessages').html(content);
	$('.profectus_sc_infowindow').fadeIn();
}

function profectus_sc_closeinfo()
{
	$('.profectus_sc_infowindow').fadeOut();
}

function profectus_sc_checkoffonline()
{
	$.ajax({
		url: "https://www.dein-stellplatz.de/supportchat/frontend/checkonoffline",
		jsonp: "callback",
		dataType: "jsonp",
		success: function( response ) {
			var onoffline = String(response[0]);
			chatstatus = onoffline;
			
			if (onoffline == 'offline')
			{
				$('.supportchatnewonline').hide();
				$('.supportchatnewoffline').show();
				$('body').addClass('profectuschatoffline');
			}
			else if (onoffline == 'online')
			{
				$('.supportchatnewonline').show();
				$('.supportchatnewoffline').hide();
				$('body').addClass('profectuschatonline');
			}
			supportchatsetopenstatus();
		}
	});

}

function profectus_sc_getmessages()
{
	$.ajax({
		url: "https://www.dein-stellplatz.de/supportchat/frontend/getmessages?reverse=1",
		jsonp: "callback",
		dataType: "jsonp",
		data: {
				token: profectus_sc_getcookie('profectus_sc_token')
		},
		success: function( response ) {
			var html = '';
			$.each( response, function( key, value ) {
				//console.log(key);
				if (key=='closed' || key=='deleted')
				{
					clearInterval(chatint);
					chatint = null;
					$('.profectus_sc_generalmessage').remove();
					profectus_sc_setcookie('profectus_sc_token', '', -100);
					profectus_sc_checkcookie();
					return;
				}
				
				from = 'user';
				if (value.fulluri=='BACKEND')
				{
					from = 'backend'
				}
				else if (value.specialmessage==0)
				{
					if (chatint == null)
					{
						chatint = setInterval(profectus_sc_getmessages, 5000);
					}
				}
				
				if ($('.messageid-'+value.messageid).length==0)
				{
					if (value.specialmessage==0)
					{
						html += '<div class="profectus_sc_message profectus_sc_generalmessage profectus_sc_usermessage'+value.usermessage+' user-'+from+' messageid-'+value.messageid+'">';
							
							html += '<div class="message">'+nl2br(value.message,true)+'</div>';
							if (value.usermessage==0)
							{
								var img = 'Mitarbeiter-1-im-Chat.webp';
								if (value.sendby=='Monika')
								{
									img = 'Mitarbeiter-2-im-Chat.webp'; 
								}
								html += '<img src="https://www.dein-stellplatz.de/supportchat/public/img/'+img+'" class="chatprofilepic" alt="Support" />';
							}
						html += '</div>';
					}
					else if (value.specialmessage==1)
					{
						html += '<div class="profectus_sc_specialmessage messageid-'+value.messageid+'">';
							html += value.message;
						html += '</div>';
					}
					else if (value.specialmessage==2)
					{
						html += '<div class="profectus_sc_message profectus_sc_greeting user-'+from+' profectus_sc_usermessage'+value.usermessage+' messageid-'+value.messageid+'">';
							html += '<div class="message">'+nl2br(value.message,true)+'</div>';
							if (value.usermessage==0)
							{
								var img = 'Mitarbeiter-1-im-Chat.webp';
								if (value.sendby=='Monika')
								{
									img = 'Mitarbeiter-2-im-Chat.webp'; 
								}
								html += '<img src="https://www.dein-stellplatz.de/supportchat/public/img/'+img+'" class="chatprofilepic" alt="Support" />';
							}
						html += '</div>';
					}
				}
			});
			$('.supportchatmessages').append(html);
			if (html!='')
			{
				$(".supportchatmessages").animate({ scrollTop: $('.supportchatmessages').prop("scrollHeight")}, 1000);
			}
		}
	});

}

function profectus_sc_validate_email(email) { 
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
}

function profectus_sc_setcookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = cname + "=" + cvalue + "; " + expires + "; path=/";
}

function profectus_sc_getcookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1);
        if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
    }
    return "";
}

function nl2br(str, is_xhtml) {
  var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br ' + '/>' : '<br>'; // Adjust comment to avoid issue on phpjs.org display

  return (str + '')
    .replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + breakTag + '$2');
}


