

var pcmsBot = (function PcmsBotIIFE() {
    var _cookie_bot_name = 'pcms_cookie_bot_2fee';
    var _cookieDialog_id = 'pcmsCookieDialog';
    var _dialog_endpoint = 'cookie/dialog/';
    var _preferences_endpoint = 'cookie/preferences/';
    var _base_url = 'https://pcmsforms.aia.gr/';
    var _api_key = 'bf295781-3d44-4726-85b5-0e531fd38dd2';
    var _cookie_life_duration = 180;
    var _cookie_version = '25d8e9f2-b400-42b9-9104-15b56da889ca';
    var _cookies_list = [{"Category":"41494120-5043-4d53-0004-000000000001","Title":"__utmt"},{"Category":"41494120-5043-4d53-0004-000000000001","Title":"f5_cspm"},{"Category":"41494120-5043-4d53-0004-000000000001","Title":"TS01d19f79"},{"Category":"41494120-5043-4d53-0004-000000000001","Title":"__utmz"},{"Category":"41494120-5043-4d53-0004-000000000001","Title":"__utm.gif"},{"Category":"41494120-5043-4d53-0004-000000000001","Title":"XSRF-TOKEN"},{"Category":"41494120-5043-4d53-0004-000000000001","Title":"laravel_session"},{"Category":"41494120-5043-4d53-0004-000000000003","Title":"IDE"},{"Category":"41494120-5043-4d53-0004-000000000001","Title":"pcms_cookie_bot_2fee"},{"Category":"41494120-5043-4d53-0004-000000000001","Title":"TS#"},{"Category":"41494120-5043-4d53-0004-000000000001","Title":"cp_total_cart_items"},{"Category":"41494120-5043-4d53-0004-000000000001","Title":"f5avrbbbbbbbbbbbbbbbb"},{"Category":"41494120-5043-4d53-0004-000000000001","Title":"f5avr1252634108aaaaaaaaaaaaaaaa"},{"Category":"41494120-5043-4d53-0004-000000000001","Title":"_SessionId"},{"Category":"41494120-5043-4d53-0004-000000000001","Title":"voteNO_cookie"},{"Category":"41494120-5043-4d53-0004-000000000001","Title":"aia_visited_notification_bubble"},{"Category":"41494120-5043-4d53-0004-000000000001","Title":"__utmb"},{"Category":"41494120-5043-4d53-0004-000000000001","Title":"cp_sessionTime"},{"Category":"41494120-5043-4d53-0004-000000000001","Title":"__utma"},{"Category":"41494120-5043-4d53-0004-000000000001","Title":"cpab"},{"Category":"41494120-5043-4d53-0004-000000000001","Title":"History.store"},{"Category":"41494120-5043-4d53-0004-000000000003","Title":"handlers/ClickImpressions.ashx"},{"Category":"41494120-5043-4d53-0004-000000000001","Title":"__utmc"},{"Category":"41494120-5043-4d53-0004-000000000001","Title":"aia_visitedNaN"},{"Category":"41494120-5043-4d53-0004-000000000001","Title":"bi"},{"Category":"41494120-5043-4d53-0004-000000000001","Title":"voteYES_cookie"},{"Category":"41494120-5043-4d53-0004-000000000001","Title":"cp_total_cart_value"},{"Category":"41494120-5043-4d53-0004-000000000001","Title":"_gid"},{"Category":"41494120-5043-4d53-0004-000000000001","Title":"track"},{"Category":"41494120-5043-4d53-0004-000000000003","Title":"test_cookie"}];
    var _langUrlParam = '?lang=' + 'en';
    var _allowCookieChoices;

    function init() {

    if (_api_key == null || _api_key == '') {
    console.log('PcmsBotIIFE api_key error');
    }

    if (!String.prototype.startsWith) {
    String.prototype.startsWith = function(searchString, position) {
    return this.substr(position || 0, searchString.length) === searchString;
    };
    }

    /**
    * Εάν υπάρχει το cookie_bot cookie, ελέγχουμε εάν έχουμε την ίδια έκδοση.
    * Εάν η εκδοση είναι διαφορετική το διαγράφουμε απο τον browser
    */
    if (getCookie(_cookie_bot_name) != "") {
    var _cooSettings = JSON.parse(atob(getCookie(_cookie_bot_name)));
    if (_cooSettings.CookieVersion != _cookie_version) {
    console.log('PcmsBotIIFE cookie wrong version, it will be deleted');
    deleteCookie(_cookie_bot_name);
    _showCookieBotDialog();
    }
    }

    if (getCookie(_cookie_bot_name) == "") {
    console.log('PcmsBotIIFE showCookieBotDialog...');
    _showCookieBotDialog();
    } else {
    console.log('PcmsBotIIFE cookie settings find');
    }
    }

    function deleteCookie(cname) {
    document.cookie = cname + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
    }

    function 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 getCookie(cname) {
    var name = cname + "=";
    var decodedCookie = decodeURIComponent(document.cookie);
    var ca = decodedCookie.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 createCORSRequest(method, url) {
    console.log('uel=' + url);


    var xhr = new XMLHttpRequest();
    if ("withCredentials" in xhr) {
    // Check if the XMLHttpRequest object has a "withCredentials" property.
    // "withCredentials" only exists on XMLHTTPRequest2 objects.
    xhr.open(method, url, true);
    } else if (typeof XDomainRequest != "undefined") {
    // Otherwise, check if XDomainRequest.
    // XDomainRequest only exists in IE, and is IE's way of making CORS requests.
    xhr = new XDomainRequest();
    xhr.open(method, url);
    } else {
    // Otherwise, CORS is not supported by the browser.
    xhr = null;
    }
    return xhr;
    }

    function removeCookieBotDialog() {
    var element = document.getElementById(_cookieDialog_id);
    element.parentNode.removeChild(element);
    }

    function _showCookieBotDialog() {
    //Δείξε το dialog:
    var xhr = createCORSRequest('GET', (_base_url + _dialog_endpoint + _api_key + _langUrlParam));

    if (!xhr) {
    console.log('PcmsBotIIFE: CORS not supported');
    return;
    }
    xhr.onload = function() {
    if (xhr.status >= 200 && xhr.status < 300) {
    var wrapper = document.createElement('div');
    wrapper.setAttribute('id', _cookieDialog_id);
    wrapper.innerHTML = this.responseText;
    document.getElementsByTagName('body')[0].appendChild(wrapper);

    var cookie = getCookie(_cookie_bot_name);
    if (cookie != "") {
    _cookieValue = atob(cookie);
    var CookiesPreferences = JSON.parse(_cookieValue);
    if(typeof CookiesPreferences !== 'undefined'){

    for (var i = 0; i < CookiesPreferences.Choices.length; i++) {
    if (CookiesPreferences.Choices[i].HasConsented) {
    $("#chk_" + CookiesPreferences.Choices[i].Category).prop('checked', true);

    }
    }
    }
    }

    }
    };
    xhr.onerror = function() {
    console.log('PcmsBotIIFE: there was an error making the request (_showCookieBotDialog)');
    };
    xhr.send();
    }

    function _onDeclineAndProceed() {
    var dialog = document.getElementById(_cookieDialog_id);
    if (dialog !== null) {
    var botPost = {};
    botPost.choices = [];
    botPost.contracts = [];
    var inputs = dialog.getElementsByTagName("input");
    for (var i = 0; i < inputs.length; i++) {
    if (inputs[i].id.startsWith("chk_")) {
    var serviceId = inputs[i].id.substring(4);
    var isChecked = inputs[i].value == "true" ? true : false;
    botPost.choices.push({
    Category: serviceId,
    HasConsented: isChecked
    });
    } else if (inputs[i].id.startsWith("contract_id_")) {
    botPost.contracts.push(inputs[i].id);
    }

    }
    _SaveChoicesAndClose(botPost);
    }
    }

    function _onAcceptAllAndProceed() {
    var dialog = document.getElementById(_cookieDialog_id);
    if (dialog !== null) {
    var botPost = {};
    botPost.choices = [];
    botPost.contracts = [];
    var inputs = dialog.getElementsByTagName("input");
    for (var i = 0; i < inputs.length; i++) {
    if (inputs[i].id.startsWith("chk_")) {
    var serviceId = inputs[i].id.substring(4);
    var isChecked = true;
    botPost.choices.push({
    Category: serviceId,
    HasConsented: isChecked
    });
    } else if (inputs[i].id.startsWith("contract_id_")) {
    botPost.contracts.push(inputs[i].id);
    }
    }
    _SaveChoicesAndClose(botPost);
    }
    }

    function _OnAgreeAndProceed() {
    var dialog = document.getElementById(_cookieDialog_id);
    if (dialog !== null) {
    var botPost = {};
    botPost.choices = [];
    botPost.contracts = [];
    var inputs = dialog.getElementsByTagName("input");
    for (var i = 0; i < inputs.length; i++) {
    if (inputs[i].id.startsWith("chk_")) {
    var serviceId = inputs[i].id.substring(4);
    var isChecked = inputs[i].checked;
    botPost.choices.push({
    Category: serviceId,
    HasConsented: isChecked
    });
    } else if (inputs[i].id.startsWith("contract_id_")) {
    botPost.contracts.push(inputs[i].id);
    }
    }
    _SaveChoicesAndClose(botPost);
    }
    }

    function _OnSaveAndProceed() {
    var dialog = document.getElementById(_cookieDialog_id);
    if (dialog !== null) {
    var botPost = {};
    botPost.choices = [];
    botPost.contracts = [];
    var inputs = dialog.getElementsByTagName("input");
    for (var i = 0; i < inputs.length; i++) {
    if (inputs[i].id.startsWith("chk_")) {
    var serviceId = inputs[i].id.substring(4);
    var isChecked = inputs[i].checked;
    botPost.choices.push({
    Category: serviceId,
    HasConsented: isChecked
    });
    } else if (inputs[i].id.startsWith("contract_id_")) {
    botPost.contracts.push(inputs[i].id);
    }
    }
    _SaveChoicesAndClose(botPost);
    }
    }



    function _OnflipContainer() {
    var element = document.getElementById("pcms.flipper");
    var x = document.getElementById("pcms.more-links");

    if (x.style.display === "block") {
    x.style.display = "block";
    } else {
    x.style.display = "block";
    }
    element.classList.toggle("flipped");
    document.getElementById('pcms.flipper').scrollIntoView(true);
    }
    function _tabLinksPopUp(evt, cityName) {
    var i, tabcontent, tablinks;
    tabcontent = document.getElementsByClassName("tabcontent");
    for (i = 0; i < tabcontent.length; i++) {
    tabcontent[i].style.display = "none";
    }
    tablinks = document.getElementsByClassName("tab-item");
    for (i = 0; i < tablinks.length; i++) {
    tablinks[i].className = tablinks[i].className.replace(" active", "");
    }
    document.getElementById(cityName).style.display = "block";
    document.getElementById(cityName).style.color = "#f7b750";
    evt.target.parentElement.className += " active";
    }
    function _CookiePolicy() {
    var popup = document.getElementById("myPopup");
    popup.classList.toggle("show");
    }

    function _OnflipContainerDefault() {
    console.log('_OnflipContainerDefault');
    var element = document.getElementById("pcms.flipper");

    element.classList.toggle("flipped");
    }

    function _ClosePopUp() {
    var dialog = document.getElementById(_cookieDialog_id);
    if (dialog !== null) {
    var botPost = {};
    botPost.choices = [];
    botPost.contracts = [];
    var inputs = dialog.getElementsByTagName("input");
    for (var i = 0; i < inputs.length; i++) {
    if (inputs[i].id.startsWith("chk_")) {
    var serviceId = inputs[i].id.substring(4);
    var isChecked = false;
    if (serviceId == '41494120-5043-4d53-0004-000000000001'){
    isChecked = true;
    }
    botPost.choices.push({
    Category: serviceId,
    HasConsented: isChecked
    });
    } else if (inputs[i].id.startsWith("contract_id_")) {
    botPost.contracts.push(inputs[i].id);
    }
    }
    _SaveChoicesAndClose(botPost);
    }
    }


    function _SaveChoicesAndClose(consents) {
    //Αποθήκευσε το consents
    var xhr = createCORSRequest('POST', (_base_url + _preferences_endpoint + _api_key));
    if (!xhr) {
    console.log('PcmsBotIIFE: CORS not supported');
    return;
    }
    xhr.onload = function() {
    if (xhr.status >= 200 && xhr.status < 300) {
    setCookie(_cookie_bot_name, this.responseText, _cookie_life_duration);
    removeCookieBotDialog();
    }
    };
    xhr.onerror = function() {
    console.log('PcmsBotIIFE: there was an error making the request. (_SaveChoicesAndClose)');
    removeCookieBotDialog();
    };
    xhr.setRequestHeader("content-type", "application/json;charset=UTF-8");
    xhr.send(JSON.stringify(consents));
    }

    init();

    return {
    getCookieName: function() {
    return _cookie_bot_name;
    },
    getDialogId: function() {
    return _cookieDialog_id;
    },
    getBaseUrl: function() {
    return _base_url;
    },
    onDeclineAndProceed: function() {
    _onDeclineAndProceed();
    },
    onAcceptAllAndProceed: function() {
    _onAcceptAllAndProceed();
    },
    onAgreeAndProceed: function() {
    _OnAgreeAndProceed();
    },
    onViewCookieSettings: function() {
    _OnViewCookieSettings();
    },
    onSaveAndProceed: function() {
    _OnSaveAndProceed();
    },
    onFlipContainer: function() {
    _OnflipContainer();
    },
    CookiePolicy: function() {
    _CookiePolicy();
    },
    ClosePopUp: function() {
    _ClosePopUp();
    },
    tabLinksPopUp: function(event,city) {
    _tabLinksPopUp(event,city);
    },
    onFlipContainerDefault: function() {
    _OnflipContainerDefault();
    },
    onShowCookieBotDialog: function() {
    _showCookieBotDialog();
    },
    onShowMandatoryAlert: function(message) {
    alert(message);
    },
    getCookieSettings: function() {
    var cookie = getCookie(_cookie_bot_name);
    if (cookie != "") {
    _cookieValue = atob(cookie);
    return JSON.parse(_cookieValue).CookieValue;
    }

    return null;
    },
    debugCookie: function() {
    var cookie = getCookie(_cookie_bot_name);
    if (cookie != "") {
    var _cooSettings = JSON.parse(atob(cookie));
    console.log('CookieValue = ' + _cooSettings.CookieValue);
    console.log('CookieVersion = ' + _cooSettings.CookieVersion);
    console.log('CookieCreationDT = ' + _cooSettings.CookieCreationDT);
    for (var i = 0; i < _cooSettings.Choices.length; i++) {
    var choice = _cooSettings.Choices[i];
    //console.log('Category=' + choice.Category + ', HasConsented=' + choice.HasConsented + ', DeveloperName=' + choice.DeveloperName);
    }
    } else {
    console.log('Cookie does not exist');
    }
    },
    allow: function allowCookieType(developername) {
    if (_allowCookieChoices === undefined) {
    var cookie = getCookie(_cookie_bot_name);
    if (cookie != "") {
    var _cookieValue = atob(cookie);
    _allowCookieChoices = JSON.parse(_cookieValue).Choices;
    }
    }

    if (_allowCookieChoices != undefined) {
    CookiesPreferences = _allowCookieChoices;

    for (var i = 0; i < _allowCookieChoices.length; i++) {
    if (_allowCookieChoices[i].DeveloperName == developername) {
    return _allowCookieChoices[i].HasConsented;
    }
    }
    }
    return false;
    }
    }
    })();

