var IBM = IBM || {};
(function () {
}).apply(IBM);
(function(webapi, $){
function safeAjax(ajaxOptions) {
var deferredAjax = $.Deferred();
shell.getTokenDeferred().done(function (token) {
// add headers for AJAX
if (!ajaxOptions.headers) {
$.extend(ajaxOptions, {
headers: {
"__RequestVerificationToken": token
}
});
} else {
ajaxOptions.headers["__RequestVerificationToken"] = token;
}
$.ajax(ajaxOptions)
.done(function(data, textStatus, jqXHR) {
validateLoginSession(data, textStatus, jqXHR, deferredAjax.resolve);
}).fail(deferredAjax.reject); //AJAX
}).fail(function () {
deferredAjax.rejectWith(this, arguments); // on token failure pass the token AJAX and args
});
return deferredAjax.promise();
}
webapi.safeAjax = safeAjax;
})(window.webapi = window.webapi || {}, jQuery)
var API_OS_UK_APIKEY = 0;
IBM.Forms = (function () {
return {
setVisible: function (fieldName, visible) {
if (visible) {
$('#' + fieldName).parent().parent().parent().show();
}
else {
$('#' + fieldName).parent().parent().parent().hide();
}
},
setSectionVisible: function (name, visible) {
///
///Sets the section with the name provided to be visible or not, depending on the value passed in for ‘visible’.
///
///The name of the field.
///The value to apply to the field.
if (typeof name === 'string') {
name = [name];
}
for (var a = 0; a <= name.length; a++) {
if (visible) {
$('table [name="' + name[a] + '"]').show();
$('.section[data-name="' + name[a] + '"]').show();
$('.section[data-name="' + name[a] + '"]').prev('.section-title').show();
$('table[data-name="' + name[a] + '"]').parent().show();
} else {
$('table [name="' + name[a] + '"]').hide();
$('.section[data-name="' + name[a] + '"]').hide();
$('.section[data-name="' + name[a] + '"]').prev('.section-title').hide();
$('table[data-name="' + name[a] + '"]').parent().hide();
}
}
},
setRequired: function (name, isRequired, failOnFalse, errorMsg) {
if ($('#' + name).length < 1) {
return;
}
if (typeof (Page_Validators) === 'undefined') return;
var exists = $('#RequiredFieldValidator' + name).length > 0;
errorMsg = errorMsg === null || errorMsg === '' || typeof (errorMsg) === 'undefined' ? $('label[for="' + name + '"], label[id="' + name + '_label"]').text() : errorMsg;
if (isRequired) {
//Does validator exist already?
if (!exists) {
var validator = document.createElement('span');
validator.style.display = "none";
validator.id = 'RequiredFieldValidator' + name;
validator.controltovalidate = name;
validator.errormessage = errorMsg + ' is a required field.';
validator.evaluationfunction = function () {
return !(getValue(name) === null ||
getValue(name) === 'undefined' ||
getValue(name) === '' ||
!getValue(name) && failOnFalse &&
($('#' + name).hasClass('checkbox') || $('#' + name).hasClass('boolean-radio')));
};
Page_Validators.push(validator);
$('label[for="' + name + '"], label[id="' + name + '_label"]').parent().find('.validators').append($(validator));
}
//Hook up event to validate
$('label[for="' + name + '"], label[id="' + name + '_label"]').parent().addClass('required');
ValidatorEnable($('#RequiredFieldValidator' + name)[0], true);
} else {
if (exists) {
$('label[for="' + name + '"], label[id="' + name + '_label"]').parent().removeClass('required');
ValidatorEnable($('#RequiredFieldValidator' + name)[0], false);
}
}
function getValue (name) {
if ($('#' + name).length === 0) {
return '';
}
if ($('#' + name).attr('type') === 'checkbox') {
return $('#' + name).prop('checked');
} else if ($('#' + name).hasClass('boolean-radio')) {
return $('#' + name + '_1').prop('checked');
} else if ($('#' + name).hasClass('boolean-dropdown')) {
return $('#' + name + ' option:selected').val() === "1";
} else if ($('#' + name).hasClass('picklist vertical') || $('#' + name).hasClass('picklist horizontal')) {
return $('input[id*="' + name + '"]:checked').length > 0 ? $('input[id*="' + name + '"]:checked').val() : null;
} else {
return $('#' + name).val();
}
}
}
}
})();
IBM.RecentActivity = (function () {
return {
showSummary: function (containerName, id) {
var container = $('#' + containerName);
if (container.hasClass("ipssmd-trackit-viewmore-holder-closed")) {
var url = window.location.href;
if(url.endsWith("/myaccount") || url.endsWith("/myaccount/"))
{
var detailsUrl = "details?id=";
}
else{
//var detailsUrl = "myaccount/details?id=";
var detailsUrl = "details?id=";
}
$('#' + containerName).load(detailsUrl + id, function () {
container.removeClass("ipssmd-trackit-viewmore-holder-closed");
container.addClass("ipssmd-trackit-viewmore-holder");
});
}
else {
container.html('');
container.removeClass("ipssmd-trackit-viewmore-holder");
container.addClass("ipssmd-trackit-viewmore-holder-closed");
}
}
}
})();
IBM.Portal = IBM.Portal || function () {
return {
setLookupAsAddressOOB: function (addressLookupId) {
var addressLookupSelector = '#' + addressLookupId;
// Get text from content snippets
var searchMessage = '{{ snippets["IBM/Address/SearchMessage"] }}';
var noResultsAlert = '{{ snippets["IBM/Address/NoResultsMessage"] }}';
var noResultsInitialAlert = '{{ snippets["IBM/Address/InitialAlertMessage"] }}';
var searchTitle = '{{ snippets["IBM/Address/ModalTitle"] }}';
var instructions = '{{ snippets["IBM/Address/ModalInstructionMessage"] }}';
var addressSavingError = '{{ snippets["IBM/Address/ErrorSaving"] }}';
var addressSaveInProgressMessage = '{{ snippets["IBM/Address/SaveInProgressMessage"] }}';
// When the input box is clicked, the modal is opened (by emulating a click on the search button)
$(addressLookupSelector + '_name').click(function () {
$(addressLookupSelector).parent().find('.fa.fa-search').click();
});
// Set UI text
$('#' + addressLookupId + '_lookupmodal').find('.query.form-control').attr('data-original-title', searchMessage);
$('#' + addressLookupId + '_lookupmodal').find('.query.form-control').attr('aria-label', searchMessage);
$('#' + addressLookupId + '_lookupmodal > div.entity-lookup > section > .modal-dialog > .modal-content > .modal-header > h1.modal-title').text(searchTitle);
// Set alert at the bottom
$('#' + addressLookupId + '_lookupmodal').find('.view-empty.message > div.alert').text(noResultsInitialAlert);
// Reset alert at bottom to original text when a search has been done
$('#' + addressLookupId + '_lookupmodal').find('[title="Search Results"].btn').click(function () {
$('#' + addressLookupId + '_lookupmodal').find('.view-empty.message > div.alert').text(noResultsAlert);
});
// Make search box full width
$('#' + addressLookupId + '_lookupmodal').find('div.input-group.pull-left.view-search.entitylist-search').attr('style', 'width:auto;');
// Remove sorting link
$('#' + addressLookupId + '_lookupmodal').find('a[role="button"][aria-label="Full address"]').parent().append('Address');
$('#' + addressLookupId + '_lookupmodal').find('a[role="button"][aria-label="Full address"]').remove();
$('#' + addressLookupId + '_lookupmodal').find('.pull-right.toolbar-actions').prepend(instructions);
// Clear alert below lookup field
function clearNotification(id) {
if ($('#' + id).length > 0) {
$('#' + id).remove();
}
}
// Add alert below lookup field
function addNotification(id, message, type, afterElement) {
if ($('#' + id).length == 0) {
afterElement.append('
' + message + '<\/div>');
}
}
// When an address has been selected
$(addressLookupSelector).change(function (changeEvent) {
if ($(addressLookupSelector).val() == "" || $(addressLookupSelector).val() == null) return;
addNotification('savingAddress', addressSaveInProgressMessage, 'warning', $(addressLookupSelector).parent().parent());
// Call OData service to create address
$.ajax({
url: '/_odata/CreateAddress?$filter=ibm_lookupid%20eq%20%27' + $(addressLookupSelector).val() + '%27',
type: 'GET',
success: function (data) {
$(addressLookupSelector).val(data.value[0].ibm_addressid);
clearNotification('addressError');
clearNotification('savingAddress');
},
error: function (XMLHttpRequest, status, error) {
addNotification('addressError', addressSavingError, 'danger', $(addressLookupSelector).parent().parent());
clearNotification('savingAddress');
}
});
});
},
setLookupAsAddress: function (name, manualSection, onAddressSelected,onManualSectionShown) {
var outOfAreaAddresses = null;
function findAddressFromAPI(_address, _name, _manualSection ){
outOfAreaAddresses = null;
var url = 'https://api.os.uk/search/places/v1/find?maxresults=100&query=' + _address + '&dataset=LPI';
var apiKey = '&key=' + API_OS_UK_APIKEY;
$.getJSON(url + apiKey, function(data){
if (data.results && data.results.length > 0){
outOfAreaAddresses = data.results;
handlResultAPIFoundAddress(_name, _manualSection);
}else {
showManualAddressSection(_name, _manualSection);
if (onManualSectionShown != null) {
onManualSectionShown(true);
}
}
}).fail(function (){
showManualAddressSection(_name, _manualSection);
if (onManualSectionShown != null) {
onManualSectionShown(true);
}
});
}
function findPostCodeFromAPI(_postCode, _name, _manualSection ){
$.getJSON("/_api/ibm_settings?$select=ibm_name,ibm_textvalue&$filter=ibm_name eq 'API.OS.UK.APIKEY'&$top=1", function (data){
outOfAreaAddresses = null;
var url = 'https://api.os.uk/search/places/v1/postcode?postcode=' + _postCode + '&dataset=LPI';
API_OS_UK_APIKEY = data.value[0].ibm_textvalue;
var apiKey = '&key=' + API_OS_UK_APIKEY;
$.getJSON(url + apiKey, function(data){
if (data.results && data.results.length > 0){
outOfAreaAddresses = data.results;
handlResultAPIFoundAddress(_name, _manualSection);
}else {
findAddressFromAPI(_postCode, _name, _manualSection);
}
}).fail(function (){
findAddressFromAPI(_postCode, _name, _manualSection);
});
}).fail(function (){
console.log('fail settings')
});
}
function handlResultAPIFoundAddress(_name, _manualSection){
var html = '';
html = '
';
for (var ctr = 0; ctr < outOfAreaAddresses.length; ctr++) {
if(outOfAreaAddresses[ctr].LPI.ADDRESS != null){
var fullAddress = outOfAreaAddresses[ctr].LPI.ADDRESS.toLowerCase();
fullAddress = fullAddress.substring(0, fullAddress.length - 8) + fullAddress.slice(-8).toUpperCase();
html += '
';
}
}
$('#llpgSelect' + _name).append(html);
$('#llpgSelect' + _name).show();
$('#llpgWait' + _name).hide();
$('#llpgSelect' + _name).off('change');
$('#llpgSelect' + _name).change(function () {
var value = $('#llpgSelect' + _name + ' option:selected').val();
var LPI = outOfAreaAddresses[value].LPI;
$.getJSON("/_api/ibm_addresses?$select=ibm_addressid&$filter=ibm_uprn eq '"+ LPI.UPRN +"'&$top=1", function (data){
if(data.value.length == 0){
var _lsc = '';
switch (LPI.LOGICAL_STATUS_CODE){
case '1':
_lsc = 863320000;
break;
case '3':
_lsc = 863320001;
break;
case '5':
_lsc = 863320002;
break;
case '6':
_lsc = 863320003;
break;
case '7':
_lsc = 863320004;
break;
case '8':
_lsc = 863320005;
break;
case '9':
_lsc = 863320006;
break;
}
var record = {};
record.ibm_llpglogicalstatus = _lsc; // Choice
record.ibm_postcode = LPI.POSTCODE_LOCATOR; // Text
record.ibm_posttown = LPI.TOWN_NAME; // Text
record.ibm_town = LPI.TOWN_NAME; // Text
record.ibm_uprn = LPI.UPRN; // Text
record.cwc_isosplaceaddress = true;
record.ibm_localcustodiancode = LPI.LOCAL_CUSTODIAN_CODE;
record.ibm_streetname = LPI.STREET_DESCRIPTION;
record.ibm_easting = LPI.X_COORDINATE;
record.ibm_northing = LPI.Y_COORDINATE;
record.ibm_lookupid = LPI.UPRN;
if(LPI.LOGICAL_STATUS_CODE != undefined){
if(LPI.LOGICAL_STATUS_CODE == 3)
record.cwc_alternateaddress = LPI.ADDRESS;
if(LPI.LOGICAL_STATUS_CODE == 1)
record.ibm_fulladdress = LPI.ADDRESS;
}
if (LPI.PAO_END_NUMBER != undefined)
record.ibm_paoendnumber = LPI.PAO_END_NUMBER;
if (LPI.PAO_END_SUFFIX != undefined)
record.ibm_paoendsuffix = LPI.PAO_END_SUFFIX;
if (LPI.PAO_START_NUMBER != undefined)
record.ibm_paostartnumber = LPI.PAO_START_NUMBER;
if (LPI.PAO_START_SUFFIX != undefined)
record.ibm_paostartsuffix = LPI.PAO_START_SUFFIX;
if (LPI.PAO_TEXT != undefined)
record.ibm_paotext = LPI.PAO_TEXT;
// record.ibm_paon = 'N/A';
// record.ibm_parentaddressid = LPI.PARENT_UPRN; this field is lookup
if (LPI.SAO_END_NUMBER != undefined)
record.ibm_saoendnumber = LPI.SAO_END_NUMBER;
if (LPI.SAO_END_SUFFIX != undefined)
record.ibm_saoendsuffix = LPI.SAO_END_SUFFIX;
if (LPI.SAO_START_NUMBER != undefined)
record.ibm_saostartnumber = LPI.SAO_START_NUMBER;
if (LPI.SAO_START_SUFFIX != undefined)
record.ibm_saostartsuffix = LPI.SAO_START_SUFFIX;
if (LPI.SAO_TEXT != undefined)
record.ibm_saotext = LPI.SAO_TEXT;
// record.ibm_saon = 'N/A'
// record.ibm_usrn = LPI.USRN;
// record.ibm_wardcode = 'N/A';
// record.ibm_wardname = 'N/A';
// THOROUGHFARE_NAME --> street
if(LPI.LOCAL_CUSTODIAN_CODE == '4635')
record.ibm_isinarea = true; // Boolean
else
record.ibm_isinarea = false; // Boolean
var _addressSplit = LPI.ADDRESS.split(',');
$.each(_addressSplit, function (key, value){
if(parseInt(_addressSplit[0]) == _addressSplit[0]){
if (key == 0)
record.ibm_paon = _addressSplit[0];
if (key == 1)
record.ibm_line1 = _addressSplit[0] + ' ' + value.trim();
else if (key == 2)
record.ibm_line2 = value.trim();
else if (key == 3)
record.ibm_line3 = value.trim();
else if (key == 4)
record.ibm_line4 = value.trim();
else if (key == 5)
record.ibm_line5 = value.trim();
else if (key == 6)
record.ibm_line6 = value.trim();
else if (key == 7)
record.ibm_line7 = value.trim();
}else {
if(key == 0)
record.ibm_line1 = value.trim();
else if(key == 1)
record.ibm_line2 = value.trim();
else if(key == 2)
record.ibm_line3 = value.trim();
else if(key == 3)
record.ibm_line4 = value.trim();
else if(key == 4)
record.ibm_line5 = value.trim();
else if(key == 5)
record.ibm_line6 = value.trim();
else if(key == 6)
record.ibm_line7 = value.trim();
}
});
webapi.safeAjax({
type: "POST",
contentType: "application/json",
url: "/_api/ibm_addresses",
data: JSON.stringify(record),
success: function (data, textStatus, xhr) {
var newId = xhr.getResponseHeader("entityid");
if (newId != null && newId != "" && newId != "none") {
var isInArea = $('#llpgSelect' + _name + ' option:selected').data('isinarea');
$('#' + _name).val(newId);
$('#' + _name + "_entityname").val("ibm_address");
$('#' + _name + "_name").val($('#llpgSelect' + _name + ' option:selected').text());
$('#llpg' + _name).val($('#llpgSelect' + _name + ' option:selected').text());
$('#llpg' + _name).css("text-transform", "capitalize");
$('#llpgSelect' + _name + ' option').remove();
$('#llpgSelect' + _name).hide();
if (onAddressSelected != null) {
onAddressSelected({
SelectedOption: {
Text: $('#llpg' + _name).val(),
Value: $('#' + _name).val(),
IsInArea: isInArea
}
});
}
$('#' + _name).trigger("change");
if (_manualSection != null && _manualSection != "") {
IBM.Forms.setSectionVisible(_manualSection, false);
if (onManualSectionShown != null) {
onManualSectionShown(false);
}
}
}
else {
$('#llpgSelect' + _name + ' option').remove();
$('#llpgSelect' + _name).hide();
showManualAddressSection(_name, _manualSection);
if (onManualSectionShown != null) {
onManualSectionShown(true);
}
}
},
error: function (xhr, textStatus, errorThrown) {
console.log(xhr);
},
fail: function( jqXHR, textStatus, errorThrown ){
console.log('Fail Ajax');
}
});
}else{
var findId = data.value[0].ibm_addressid;
var isInArea = $('#llpgSelect' + _name + ' option:selected').data('isinarea');
$('#' + _name).val(findId);
$('#' + _name + "_entityname").val("ibm_address");
$('#' + _name + "_name").val($('#llpgSelect' + _name + ' option:selected').text());
$('#llpg' + _name).val($('#llpgSelect' + _name + ' option:selected').text());
$('#llpg' + _name).css("text-transform", "capitalize");
$('#llpgSelect' + _name + ' option').remove();
$('#llpgSelect' + _name).hide();
if (onAddressSelected != null) {
onAddressSelected({
SelectedOption: {
Text: $('#llpg' + _name).val(),
Value: $('#' + _name).val(),
IsInArea: isInArea
}
});
}
$('#' + _name).trigger("change");
if (_manualSection != null && _manualSection != "") {
IBM.Forms.setSectionVisible(_manualSection, false);
if (onManualSectionShown != null) {
onManualSectionShown(false);
}
}
}
}).fail(function (){
console.log('fail settings');
});
});
}
function showManualAddressSection(name,manualSection) {
$('#llpgWait' + name).hide();
$('#llpgNone' + name).show();
$('#llpg' + name).val('');
$('#' + name).val('');
$(addressLookupSelector + "_name").val('');
if (manualSection != null && manualSection != "") {
IBM.Forms.setSectionVisible(manualSection, true);
IBM.Portal.getSystemAddress(name);
}
}
var addressLookupSelector = '#' + name;
$(addressLookupSelector + '_name').parent().hide();
$(addressLookupSelector + '_name').parent().css('visibility', 'hidden');
if(manualSection != null && manualSection != "")
IBM.Forms.setSectionVisible(manualSection, false);
var html = '';
html +=
'
' +
'
' +
'