﻿function CheckAjaxResponsesForFirewallErrors() {
	Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
	function EndRequestHandler(sender, args) {
		if (args.get_error() != undefined) {
			var errorMessage = args.get_error().message;
			var error = getAFWResponse(args.get_response()._xmlHttpRequest.responseText);
			if (error != null) {
				args.set_errorHandled(true);
				PageMethods.NotifyError(error[0], ClientAlert);
			} else {
				args.set_errorHandled(false);
			}
		}
	}
}

function getAFWResponse(input) {
	var regEx = new RegExp(/AFW=[0-9]+/);
	var msg = regEx.exec(input);
	return msg;
}

function ClientAlert(PopupMessageContent) {
	// TODO: Try find an element in the DOM that is designated for AFW errors
	// The BasePage.NotifyError web method will return the localised error message specific to the AFW error
	//alert(PopupMessageContent.Description);

	$('#errorPopupTitle').text(PopupMessageContent.Title);
	$('#errorPopupText').text(PopupMessageContent.Description);
	$('#errorPopupAction').text(PopupMessageContent.Action);
	$('#errorPopupContact').text(PopupMessageContent.Contact);
	loadPopup("#errorPopupBackground", "#errorPopup");
	$("#errorPopupClose").click(function() {
		disablePopup("#errorPopupBackground", "#errorPopup");
	});
}
