
function checkJsonError(json) {
	if (typeof json._error != 'undefined') {
		
		var id = 'err-' + (new Date().getTime());
		$(document.body).append("<div id='" + id + "' style=''></div>");
		var mb = $("#" + id);
		
		var options = {
			modal: true,
			closeOnEscape: false,
			resizable: true,
			stack: true,
			autoOpen: false,
			bgiframe: true,
			position: 'top',
			title: json._error.message,
			zIndex: 100000000,
			maxWidth: 950,
			maxHeight: 625,
			buttons: {}
		};
		
		// Set up any buttons
		if (typeof json._redirect != 'undefined') {
			// This design pattern is known as a closure - useful when you need to construct an anonymous callback function but also pass it arguments.
			options.buttons['Ok'] = function(destination) { 
				return function() { window.location = destination; };
			}(json._redirect);
		} else if (typeof json._reload == 'boolean' && json._reload) {
			options.buttons['Ok'] = function() { window.location.reload(); };
		} else {
			options.buttons['Ok'] = function() { $(this).dialog("close"); };
		}
		
		// Set the dialog body
		if (typeof json._error.fulldetail != 'undefined') {
			mb.html(json._error.fulldetail);
			options['width'] = options.maxWidth;
			options['height'] = options.maxHeight;
			options['title'] = "JSON Error: " + options.title;
		} else {
			mb.html(json._error.detail);
		}
		
		mb.dialog(options);
		mb.dialog("open");
		mb.parent().find('.ui-dialog-titlebar-close').css( { display: 'none' } );
		
		return false;
	} else if (typeof json._redirect != 'undefined') {
		window.location(json._redirect);
	} else if (typeof json._reload == 'boolean' && json._reload) {
		window.location.reload();
	}
	
	return true;
}

// for now just return if there is actually an error message in the json
function checkError ( json ) {
	var isError = false;

	if (typeof json._error != 'undefined') {
		tealeafClientEvent('applicationError', json._error.message);
		//alert(json._error.message + "\n" + json._error.detail);
		isError = true;
	}
	
	return isError;
}

$.ajaxSetup({
	"error": function(XMLHttpRequest, textStatus, errorThrown){
		var data = $.httpData(XMLHttpRequest, 'json');
		try { 
			console.log(data);
		} catch (e) {}
		if (typeof data == 'object' && typeof data._error == 'object') {
			checkJsonError(data);
		} else {
			// If we didn't get back a JSON error response, not really sure how to handle it at this point.
			checkJsonError({
				_error: {
					message: 'An error occurred', 
					detail: 'Please try again.  We do apologize if this error persists and ask that you click on Customer Support or call 1-800-VACCINE for assistance.'
					}, 
				_redirect: "index.cfm"
				});
			// ToDo: maybe append XMLHttpRequest.responseText in a hidden div of the error detail, as it might have more details, 
			// but since on the client side we don't know whether this is PR or not, it shouldn't be shown by default.
		}
	}
});

