(function() {

	/**

	 * SensFRX device initialization

	 */

	var anchors = document.querySelectorAll("a[href*='logout']");

	for(var i=0; i<anchors.length; i++) {

		if(anchors[i].addEventListener){

			anchors[i].addEventListener("click", logoutClickHandler, false);  //Modern browsers

		}else if(anchors[i].attachEvent){

			anchors[i].attachEvent('onclick', logoutClickHandler);            //Old IE

		}

	}

	// ------------------ Start Pixel For Bot ----------------------------------------------------------------------

	var d_id_for_bot = _sensfrx("getRequestString");

	// Function to get an existing cookie value

	function getCookie(cookieName) {

		const name = cookieName + "=";

		const decodedCookie = decodeURIComponent(document.cookie);

		const cookieArray = decodedCookie.split(';');

		for (let i = 0; i < cookieArray.length; i++) {

			let cookie = cookieArray[i].trim();

			if (cookie.indexOf(name) === 0) {

				return cookie.substring(name.length, cookie.length);

			}

		}

		return null;

	}



	// Function to set or update a cookie if needed

	function setOrUpdateCookie(cookieName, cookieValue, daysToExpire) {

		if (getCookie(cookieName) !== cookieValue) {

			let expires = '';

			if (daysToExpire) {

				const date = new Date();

				date.setTime(date.getTime() + (daysToExpire * 24 * 60 * 60 * 1000));

				expires = '; expires=' + date.toUTCString();

			}

			document.cookie = `${cookieName}=${cookieValue}${expires}; path=/`;

		} else {

			// Optional action if the cookie already exists with the same value

		}

	}



	// Splitting the device ID

	const half = Math.ceil(d_id_for_bot.length / 2);



	const device_id_0 = d_id_for_bot.slice(0, half);

	const device_id_1 = d_id_for_bot.slice(half);



	const mid_0 = Math.ceil(device_id_0.length / 2);

	const mid_1 = Math.ceil(device_id_1.length / 2);



	const device_id_0_1 = device_id_0.slice(0, mid_0);

	const device_id_0_2 = device_id_0.slice(mid_0);

	const device_id_1_1 = device_id_1.slice(0, mid_1);

	const device_id_1_2 = device_id_1.slice(mid_1);



	// Set or update cookies

	setOrUpdateCookie('device0', device_id_0_1, 7);

	setOrUpdateCookie('device1', device_id_0_2, 7);

	setOrUpdateCookie('device2', device_id_1_1, 7);

	setOrUpdateCookie('device3', device_id_1_2, 7);

	//----------------------End Pixel for Bot --------------------------------------------------------------------------------

	function logoutClickHandler(e) {

		e.preventDefault();

		var device_id = _sensfrx("getRequestString");

		var href = this.href;

		href+= '&did='+device_id;

		window.location = href;

	}

	var formElem = document.querySelector(".lost_reset_password,form.login");

	if(formElem) {

		if(formElem.addEventListener){

			formElem.addEventListener("click", submitHandler, false);  // Modern browsers

		}else if(formElem.attachEvent){

			formElem.attachEvent('onclick', submitHandler);            // Old IE

		}

	}

	function submitHandler() {

		var device_id = _sensfrx("getRequestString");

		var deviceidelement = document.createElement('input');

		deviceidelement.setAttribute('type', 'hidden');

		deviceidelement.setAttribute('name', 'device_id');

		deviceidelement.setAttribute('value', device_id);

		formElem.appendChild(deviceidelement);

	}

	jQuery(document).on('click', '.woocommerce-cart .remove', function(e) {

		if (typeof _sensfrx === 'function') {

			_sensfrx("postCartAction",'remove_from_cart');

		}

	});

	jQuery(document).on('click', '.wc-block-cart-items', function(e) {

		if (typeof _sensfrx === 'function') {

			_sensfrx("postCartAction",'remove_from_cart');

		}

	});

	jQuery(document.body).on('click', '.add_to_cart_button', function() {

		if (typeof _sensfrx === 'function') {

			_sensfrx("postCartAction",'add_to_cart');

		}

	});

	jQuery(document).ready(function($) {

		$(document).on('click', 'button[name="update_cart"]', function(e) {

			if (typeof _sensfrx === 'function') {

				_sensfrx("postCartAction",'add_to_cart');

			}

		});

	});

})();

