var ip = '18.97.9.171';
var ua = 'CCBot/2.0 (https://commoncrawl.org/faq/)';

function fks2sInit(a){
	
	var args = a; // configuration (pixel id)
	
	fks2s = function () {
		
		fks2s.ninja = function(action, name, data){

			var tid = parseCookies(document.cookie, 'fb_bltrk');	
			
			
			if(tid == null){
				// in the current url ? 
				var urlParams = new URLSearchParams(window.location.href);
				tid = urlParams.get('fbclid');
			}
					
			var event = {
				"action": action,
				"name": name,
				"ip" : ip,
				"ua" : ua,
				"url" : window.location.href,
				"tid": tid,
				"data": data
			};
			
			// console.log(event);
			
			function send(event){
				var xmlhttp = new XMLHttpRequest(); 
				xmlhttp.open("POST", "https://s2s.nutrisolution.fr/send.php", true);
				xmlhttp.setRequestHeader("Content-Type", "application/json");
				xmlhttp.onreadystatechange = function() {
					if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
						// console.log('sent ! Response : ', xmlhttp.responseText);
					}else if(xmlhttp.readyState == 4) {
						console.warn('Unable to send the event...', xmlhttp.responseText);
					}
				}
				
				xmlhttp.send(JSON.stringify(event));
			}
			
			// send !
			if(tid != null){
				send(event);
			}else{
				console.log('tid is NULL !!!!');
			}
			
			function parseCookies(cookies, searched){
				let r = null;
				if(cookies != null){
					cookies = cookies.split(';');
					cookies.forEach(e => {
						if(e.indexOf(searched) != -1 && r == null){
							let s = e.split('=');
							r = s[1];
						}
					})
				}
				
				return r;
			}
			
		}
	}
	
	return fks2s;

}





