Files
using.cash/Site/Pages/v5/Public/GeoLookupHelpers.js.include

187 lines
8.0 KiB
Plaintext

<script type="text/javascript" language="javascript">
// This code is common to Swarm/AddPerson and to Signup, so it's placed in a separate file,
// which is server-side-included by both functionalities. It can also be included by
// plugins which wish to save on this particular work, as it's located in the Public
// folder.
$(document).ready(function() {
UpdatePostalPrefix('XX', $('#<%= DropCountries.ClientControlID %>').val());
$('#<%= TextPostal.ClientID %>').on('input', function() {
if (CheckPostalCode()) {
if ($('#<%= TextPostal.ClientID %>').val().length == postalCodeLength) {
$('#<%= TextDateOfBirth.ClientID %>').focus();
}
}
});
$('#<%= TextCity.ClientID %>').on('input', function() {
CheckPostalCity();
});
});
function CheckPostalCity() {
var currentCity = $('#<%= TextCity.ClientID %>').val().toLowerCase();
if (currentCity in cityNameLookup) {
var geoId = cityNameLookup[currentCity];
$('#spanDetectedGeo').text(geographyIdLookup[geoId]);
} else {
$('#spanDetectedGeo').text('');
}
}
function CheckPostalCode() {
var currentPostalCode = $('#<%= TextPostal.ClientID %>').val().toLowerCase();
if (currentPostalCode.length > postalCodeLengthCheck) {
currentPostalCode = currentPostalCode.substring(0, postalCodeLengthCheck);
}
if (currentPostalCode in postalCodeLookup) {
var cityId = postalCodeLookup[currentPostalCode];
$('#<%= TextCity.ClientID %>').attr("disabled", "disabled").val(cityIdLookup[cityId].Name);
$('#spanDetectedGeo').text(geographyIdLookup[cityIdLookup[cityId].GeoId]);
postalCodeIdentified = true;
return true;
} else if (postalCodeIdentified) {
$('#<%= TextCity.ClientID %>').removeAttr("disabled").val('');
$('#spanDetectedGeo').text('');
postalCodeIdentified = false;
}
return false;
}
function AnimatePostalCodeLength() {
if (postalCodeLength > 0) {
var postalTargetLength = postalCodeLength * 15 - 5;
var cityTargetLength = 235 - 15 * postalCodeLength;
$('#<%= TextPostal.ClientID %>').animate({ width: postalTargetLength + "px" });
$('#<%= TextCity.ClientID %>').animate({ width: cityTargetLength + "px" });
if (!postalCodeVisible) {
setTimeout(function() {
$('#<%= TextPostal.ClientID %>').fadeIn(200);
}, 100);
$('#spanLabelPostal').animate({ width: 'toggle' }, 400);
postalCodeVisible = true;
}
} else if (postalCodeVisible) {
// Remove postal code altogether, if visible now
$('#<%= TextPostal.ClientID %>').animate({ width: 0 }, 200).fadeOut(100);
$('#<%= TextCity.ClientID %>').animate({ width: "255px" }, 600);
$('#spanLabelPostal').animate({ width: 'toggle' }, 400);
postalCodeVisible = false;
}
}
function UpdatePostalPrefix(oldValue, newValue) {
$('#spanCountryPrefix').text(newValue);
$('#<%= TextCity.ClientID %>').removeAttr("disabled").val('');
$('#spanDetectedGeo').text('');
// Get postal code data for country. Heavy op; should not be done from mobile
var jsonData = {};
jsonData.countryCode = newValue;
$.ajax({
type: "POST",
url: "/Automation/FieldValidation.aspx/GetPostalCodesCities",
data: $.toJSON(jsonData),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
postalCodeLookup = {};
cityIdLookup = {};
cityNameLookup = {};
geographyIdLookup = {};
postalCodeLength = msg.d.PostalCodeLength;
postalCodeLengthCheck = msg.d.PostalCodeLengthCheck;
$('#<%= TextPostal.ClientID %>').attr('maxlength', postalCodeLength).attr('placeholder', '12345678'.substring(0, postalCodeLength));
msg.d.PostalCodes.forEach(function(element, index, array) {
postalCodeLookup[element.Code.toLowerCase()] = element.CityId;
});
msg.d.CityNames.forEach(function(element, index, array) {
cityIdLookup[element.Id] = {};
cityIdLookup[element.Id].Name = element.Name;
cityIdLookup[element.Id].GeoId = element.GeographyId;
cityNameLookup[element.Name.toLowerCase()] = element.GeographyId;
});
msg.d.Geographies.forEach(function(element, index, array) {
geographyIdLookup[element.Id] = element.Name;
});
AnimatePostalCodeLength();
CheckPostalCode();
},
error: function(msg) {
console.log(msg);
geographyIdLookup = {};
postalCodeLookup = {};
cityIdLookup = {};
cityNameLookup = {};
}
});
}
function ValidateFields() {
var isValid = true;
var dateFieldContents = $('#<%= TextDateOfBirth.ClientID%>').val();
if (dateFieldContents.length > 0) {
var jsonData = {};
jsonData.input = dateFieldContents;
$.ajax({
type: "POST",
url: "/Automation/FieldValidation.aspx/IsDateValid",
data: $.toJSON(jsonData),
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false, // blocks until function returns - race conditions otherwise
success: function (msg) {
if (msg.d != true) {
isValid = false;
$('#<%= TextDateOfBirth.ClientID %>').addClass("data-entry-error");
alertify.error("<asp:Literal runat="server" ID="LiteralErrorDate" />");
$('#<%=this.TextDateOfBirth.ClientID %>').focus();
}
}
});
}
isValid = ValidateTextField('#<%= TextCity.ClientID %>', "<asp:Literal runat="server" ID="LiteralErrorCity" />") && isValid;
isValid = ValidateTextField('#<%= TextStreet1.ClientID %>', "<asp:Literal runat="server" ID="LiteralErrorStreet" />") && isValid;
isValid = ValidateTextField('#<%= TextMail.ClientID %>', "<asp:Literal runat="server" ID="LiteralErrorMail" />") && isValid;
isValid = ValidateTextField('#<%= TextName.ClientID %>', "<asp:Literal runat="server" ID="LiteralErrorName" />") && isValid; // TODO: Actually validate geography?
return isValid;
}
function ValidateTextField(fieldId, message) {
$(fieldId).removeClass("data-entry-error");
if ($(fieldId).val().length == 0) {
alertify.error(message);
$(fieldId).addClass("data-entry-error");
$(fieldId).focus();
return false;
}
return true;
}
var postalCodeLookup = {};
var cityIdLookup = {};
var cityNameLookup = {};
var geographyIdLookup = {};
var postalCodeLength = 0;
var postalCodeLengthCheck = 0;
var postalCodeVisible = true;
var postalCodeIdentified = false;
</script>