// This object can be updated to reference the names of your address fields
var outputFields = {
line_1: ‘[name=”item_meta[1507]”]’,
line_2: ‘[name=”item_meta[1508]”]’,
post_town: ‘[name=”item_meta[1509]”]’,
postcode: ‘[name=”item_meta[1510]”]’,
};
var optInId = “frm_field_1511_container”;
var line1 = document.getElementById(‘frm_field_1507_container’);
var line2 = document.getElementById(‘frm_field_1508_container’);
var posttown = document.getElementById(‘frm_field_1509_container’);
var postcode = document.getElementById(‘frm_field_1510_container’);
// Include your Account API Key here
var apiKey = “ak_kskddyb2msShMlNtzPQcuRAA9hYuS”;
// This is the geofence for Holborn
var geofence = [
[-0.1231, 51.5305],
[-0.124, 51.53],
[-0.1249, 51.5296],
[-0.1269, 51.5288],
[-0.1309, 51.5273],
[-0.1344, 51.5261],
[-0.1362, 51.5256],
[-0.1373, 51.5253],
[-0.138, 51.5249],
[-0.1373, 51.5238],
[-0.1365, 51.5228],
[-0.1344, 51.5207],
[-0.1304, 51.5164],
[-0.1286, 51.5167],
[-0.1266, 51.517],
[-0.1249, 51.5171],
[-0.1226, 51.5172],
[-0.1204, 51.5176],
[-0.1195, 51.5164],
[-0.1185, 51.515],
[-0.1146, 51.5159],
[-0.1139, 51.5147],
[-0.1114, 51.5153],
[-0.1125, 51.5167],
[-0.1138, 51.5182],
[-0.1132, 51.5183],
[-0.1124, 51.5183],
[-0.1118, 51.5182],
[-0.1112, 51.5182],
[-0.1114, 51.5184],
[-0.1115, 51.5187],
[-0.1117, 51.519],
[-0.1119, 51.5193],
[-0.113, 51.5212],
[-0.1146, 51.5232],
[-0.117, 51.5253],
[-0.1186, 51.5275],
[-0.1206, 51.5303],
[-0.1218, 51.5306],
[-0.1224, 51.5306],
[-0.1231, 51.5305],
];
var toForm = function (target) {
return jQuery(target).parents(“form”)[0];
}
var enableBtn = function (target) {
var btn = toForm(target).querySelector(‘button.frm_button_submit[type=”submit”]’);
if (!btn) return;
btn.disabled = false;
btn.removeAttribute(“style”);
}
var disableBtn = function (target) {
var btn = toForm(target).querySelector(‘button.frm_button_submit[type=”submit”]’);
if (!btn) return;
btn.disabled = true;
btn.style.background = “gray”;
}
var disableOptIn = function () {
var radio = document.getElementById(optInId);
if (!radio) return;
radio.style.display = “none”;
}
var enableOptIn = function () {
var radio = document.getElementById(optInId);
if (!radio) return;
radio.style.display = “”;
}
var hideOutputFields = function () {
if (!line1) return;
line1.style.display = “none”;
if (!line2) return;
line2.style.display = “none”;
if (!posttown) return;
posttown.style.display = “none”;
if (!postcode) return;
postcode.style.display = “none”;
}
var showOutputFields = function () {
if (!line1) return;
line1.style.display = “”;
if (!line2) return;
line2.style.display = “”;
if (!posttown) return;
posttown.style.display = “”;
if (!postcode) return;
postcode.style.display = “”;
}
var debounce = function (func) {
var timer;
return function () {
clearTimeout(timer);
timer = setTimeout(function () {
func();
}, 800);
};
};
var inside = function (point, vs) {
// ray-casting algorithm based on
// https://wrf.ecse.rpi.edu/Research/Short_Notes/pnpoly.html/pnpoly.html
var x = point[0],
y = point[1];
var inside = false;
for (var i = 0, j = vs.length – 1; i y != yj > y && x < ((xj – xi) * (y – yi)) / (yj – yi) + xi;
if (intersect) inside = !inside;
}
return inside;
};
var withinLondon =
“
Your postcode indicates you live outside the main boundary for registration at St Philips Medical Centre, however, you may register with us as an Out of Area Patient.
” +
“
Registering at this practice as an “out of area” patient.
” +
“
As you live outside the Practice area (catchment area), we are not required to provide you with home visits.
” +
“
You may, on occasion, develop an urgent illness or injury at home which means that attending the GP surgery as normal would not be appropriate.
” +
“
If you require a GP, please contact this practice in the first instance. If we determine you need access to services local to where you live, we may ask you to call NHS 111.
” +
“
In these circumstances, NHS 111 will direct you to the local service that has been established by NHS England for patients such as you. This local service could be a GP practice near to where you live, the local walk-in or urgent care centre, A&E, or minor injuries unit.
” +
“
The local services will then decide if you can attend for an urgent face-to-face appointment with a healthcare professional, or if a home visit is needed, which will be based on your individual circumstances.
” +
“
If this is in the out-of-hours period when the surgery is normally closed between 8.30 and 18.30 am weekdays and during weekends – NHS 111 will direct you to the local out-of-hours provider.
“;
var outsideLondon =
“
Your postcode indicates that you live outside London.
” +
“
Please register with a GP closer to your home location.
” +
‘
You can find your nearest practice on this website, simply enter your postcode to get a list of local doctors surgeries: www.nhs.uk/service-search/find-a-GP
‘ +
“
Thank you.
“;
function init() {
var target = document.querySelector(outputFields.line_1);
if (!target) return;
if (target.type === “hidden”) return;
var validateLocation = function (addresses) {
var address = addresses[0];
if (inside([address.longitude, address.latitude], geofence)) {
enableBtn(target);
disableOptIn();
return message(“”);
}
if (address.post_town.toLowerCase() === “london”){
enableBtn(target);
enableOptIn();
return message(withinLondon);
}
disableBtn(target);
disableOptIn();
return message(outsideLondon);
};
var msgBox;
var message = function (msg, t) {
var className = t || “frm_error_style”; // Can be “frm_success_style”
if (!msgBox) {
msgBox = document.createElement(“div”);
msgBox.style.display = “none”;
target.parentElement.parentElement.appendChild(msgBox);
}
if (msg.length > 0) {
msgBox.className = className;
msgBox.innerHTML = msg;
msgBox.style.display = “”;
return;
}
msgBox.style.display = “none”;
};
var isHallsOfResidence = function () {
return target.parentElement.style.display === “none”;
};
var insertPostcodeFields = function (target) {
var container = document.createElement(“div”);
container.className =
“frm_form_field form-field frm_required_field frm_top_container”;
container.style.display = “none”;
var label = document.createElement(“label”);
label.className = “frm_primary_label”;
label.innerText = “Search your Postcode”;
var button = document.createElement(“button”);
button.innerText = “Lookup Postcode”;
button.style.marginTop = “0.5em”;
var input = document.createElement(“input”);
input.type = “text”;
var context = document.createElement(“div”);
context.style.marginTop = “0.5em”;
container.appendChild(label);
container.appendChild(input);
container.appendChild(button);
container.appendChild(context);
var parent = target.parentNode.parentNode;
parent.insertBefore(container, target.parentNode);
return {
container: container,
label: label,
input: input,
button: button,
context: context,
};
};
const fields = insertPostcodeFields(target);
IdealPostcodes.PostcodeLookup.setup({
apiKey: apiKey,
context: fields.context,
button: fields.button,
input: fields.input,
outputFields: outputFields,
onAddressesRetrieved: validateLocation,
});
if (target.parentElement.style.display !== “none”)
fields.container.style.display = “”;
var o = new MutationObserver(function () {
fields.container.style.display = target.parentElement.style.display;
});
o.observe(target, { attributes: true });
// Watch postcode field in case Halls of Residence selected
var postcodeInput = document.querySelector(outputFields.postcode);
if (postcodeInput) postcodeInput.addEventListener(
“keyup”,
debounce(function () {
if (!isHallsOfResidence()) return;
var postcode = postcodeInput.value.trim();
if (postcode.length === 0) return;
var client = new IdealPostcodes.Client({ api_key: apiKey });
IdealPostcodes.lookupPostcode({
client: client,
postcode: postcode,
}).then(function (result) {
if (result.length === 0)
return message(“Your postcode could not be found”);
validateLocation(result);
});
})
);
}
document.addEventListener(“DOMContentLoaded”, init);