function Form_Cookie_Domain() {
	var la_Domain = location.hostname.split('.');

	var lt_Prefix = 'www patient m legacy chartreuse'.split(' ');

	if (lt_Prefix.indexOf(la_Domain[0]) != -1) {
		la_Domain.shift();
	}

	return la_Domain.join('.');
}
function Form_QueryStringToJSON(ps_String) {
	var lo_Object = {};
	ps_String.replace(
		new RegExp("([^?=&]+)(=([^&]*))?", "g"),
		function ($0, $1, $2, $3) {
			try {
				lo_Object[$1] = decodeURIComponent($3);
			}
			catch (e) {
				// console.log(e, ps_String);
				lo_Object[$1] = $3;
			}
		}
	);
	return lo_Object;
}

function Form_JSONToQueryString(po_Object) {
	var la_Result = [];

	for (var ls_Key in po_Object) {
		if (!$.isFunction(po_Object[ls_Key])) {
			la_Result.push(ls_Key + "=" + encodeURIComponent(po_Object[ls_Key]));
		}
	}

	return la_Result.join('&');
}

function Form_Cookie_Set(ps_Cookie, ps_Value, ps_Path) {
	if (!gb_Application_IsProduction) {
		ps_Cookie = 'DEV_' + ps_Cookie;
	}

	var ls_Cookie = ps_Cookie + '=' + encodeURIComponent(ps_Value) + ';';

	if (typeof ps_Path != 'undefined') {
		ls_Cookie = ls_Cookie + 'path=' + ps_Path + ';';
	}

	
	ls_Cookie += 'domain=' + Form_Cookie_Domain() + ';';

	if (location.protocol == 'https:') {
		ls_Cookie += 'secure;';
	}

	ls_Cookie += 'SameSite=Strict;';

	document.cookie = ls_Cookie;

	return;
}

function Form_Cookie_SetKey(ps_Cookie, ps_Key, ps_Value, ps_Path, pd_ExpiryDate) {
	if (ps_Key == '') {
		return Form_Cookie_Set(ps_Cookie, ps_Value, ps_Path, pd_ExpiryDate);
	}

	if (!gb_Application_IsProduction) {
		ps_Cookie = 'DEV_' + ps_Cookie;
	}

	var lo_Cookie = Form_QueryStringToJSON(Form_Cookie_Get(ps_Cookie));

	lo_Cookie[ps_Key] = ps_Value;

	var ls_Cookie = ps_Cookie + '=' + Form_JSONToQueryString(lo_Cookie) + ';';

	if (typeof ps_Path != 'undefined') {
		ls_Cookie = ls_Cookie + 'path=' + ps_Path + ';';
	}

	if (typeof pd_ExpiryDate != 'undefined') {
		ls_Cookie = ls_Cookie + 'expires=' + pd_ExpiryDate.toUTCString() + ';';
	}

	ls_Cookie += 'domain=' + Form_Cookie_Domain() + ';';

	if (location.protocol == 'https:') {
		ls_Cookie += 'secure;';
	}

	ls_Cookie += 'SameSite=Strict;';

	document.cookie = ls_Cookie;
}

function Form_Cookie_Delete(ps_Cookie) {
	if (!gb_Application_IsProduction) {
		ps_Cookie = 'DEV_' + ps_Cookie;
	}

	var ld_ExpireDate = new Date();
	ld_ExpireDate.setTime(ld_ExpireDate.getTime() + -1 * 24 * 3600 * 1000);

	document.cookie = ps_Cookie + '=;expires=' + ld_ExpireDate.toGMTString();
}

function Form_Cookie_Get(ps_Cookie) {

	function escapeRegex(value) {
		return value.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&");
	}

	var ls_Cookie = ps_Cookie;

	if (!gb_Application_IsProduction && ps_Cookie.indexOf('DEV_') == -1) {
		ls_Cookie = 'DEV_' + ps_Cookie;
	}
	
	const regex = new RegExp('(^| )' + escapeRegex(ls_Cookie) + '=([^;]+)');

	const match = document.cookie.match(regex);

	if (match) {
		return match[2]
	}

	return '';
}

function Form_Cookie_GetKey(ps_Cookie, ps_Key) {
	if (ps_Key == '') {
		return Form_Cookie_Get(ps_Cookie);
	}

	if (!gb_Application_IsProduction) {
		ps_Cookie = 'DEV_' + ps_Cookie;
	}

	var lo_Cookies = Object.fromEntries(
		document.cookie.split(';').map(x =>
			[
				decodeURIComponent(x.trim().split('=')[0].trim()),
				x.trim().split('=').length == 2 ?
					decodeURIComponent(x.trim().split('=')[1]) :
					Object.fromEntries(x.trim().split('=').splice(1).join('=').split('&').map(y => y.split('=').map(z => decodeURIComponent(z))))
			])
	);

	var lo_Cookie = lo_Cookies[ps_Cookie];

	if (!lo_Cookie) {
		return '';
	}

	if (!ps_Key) {
		return lo_Cookie;
	}

	return lo_Cookie[ps_Key];
}