﻿var LinkWarner = (function() 
{
	var _excludedDomains = [
		"greatmidwestbank.mortgagewebcenter.com",
		"web1.secureinternetbank.com"
	];

	var _attachWarnings = function()
	{
		$("a").each(function(idx, value)
		{
			if (_isExternal(value.href))
			{
				$(value).click(function()
				{
					return _confirmLeaving();
				});
			}
		});
	};

	var _isExternal = function(url)
	{
		var domainMatch = false;

		var link = new LinkUri(url);
		var scheme = link.scheme.toLowerCase();
		if (scheme === "mailto")
		{
			domainMatch = true;
		}
		else
		{
			var hostname = link.hostname.toLowerCase();
			$.each(_excludedDomains, function(idx, value)
			{
				if (value === hostname)
				{
					domainMatch = true;
				}
			});
			if (!domainMatch && hostname == _getCurrentHostname().toLowerCase())
			{
				domainMatch = true;
			}
		}

		return !domainMatch;
	};

	var _getCurrentHostname = function()
	{
		return document.location.hostname;
	};

	var _confirmLeaving = function()
	{
		return confirm("You are now leaving Great Midwest Bank’s Website. The site you are going to is not owned or controlled by us. To remain at our site, click Cancel. To leave our site for the link you selected, click OK. Thank you for visiting Great Midwest Bank’s Website.");
	};

	return {
		init: _attachWarnings
	};
} ());

var LinkUri = function(uri)
{ 
	uri = uri || "";

	// Set up defaults
	this.href = "";
	this.userinfo = "";
	this.host = "";
	this.hostname = "";
	this.pathname = "";
	this.port = "";
	this.scheme = "";
	this.search = "";
	this.hash = "";

	if (uri.length > 0)
	{
		this.parse(uri);
	}
};
LinkUri.prototype.href = "";
LinkUri.prototype.userinfo = "";
LinkUri.prototype.host = "";
LinkUri.prototype.hostname = "";
LinkUri.prototype.pathname = "";
LinkUri.prototype.port = "";
LinkUri.prototype.scheme = "";
LinkUri.prototype.search = "";
LinkUri.prototype.hash = "";
LinkUri.prototype.parse = function(uri)
{
	var isUrl = false;

	this.href = uri;

	// Get the scheme
	if (uri.indexOf(":") > 0)
	{
		this.scheme = uri.substring(0, uri.indexOf(":"));

		uri = uri.substring(uri.indexOf(":") + 1);
		if (uri.substring(0, 2) == "//")
		{
			isUrl = true;
			uri = uri.substring(2);
		}
	}

	// Get the user info, hostname and port
	var pathStart = uri.indexOf("/");
	var hostDetails = uri.substring(0, pathStart);
	var userInfo = hostDetails.split("@");
	if (userInfo.length > 1)
	{
		this.userinfo = userInfo[0];
		this.host = userInfo[1];
	}
	else
	{
		this.host = hostDetails;
	}

	var hostSplit = this.host.split(":");
	if (hostSplit.length > 0)
	{
		this.hostname = hostSplit[0];
		if (hostSplit.length > 1)
		{
			this.port = hostSplit[1];
		}
	}

	uri = uri.substring(pathStart);

	// Now start from the end, see if there's a hash on this url
	var hashStart = uri.indexOf("#");
	if (hashStart > 0)
	{
		this.hash = uri.substring(hashStart);
		uri = uri.substring(0, uri.length - this.hash.length);
	}

	var searchStart = uri.indexOf("?");
	if (searchStart > 0)
	{
		this.search = uri.substring(searchStart);
		uri = uri.substring(0, uri.length - this.search.length);
	}

	this.pathname = uri;
};
