var request = false;
var response = "";
	
var myRegex = /<title>Page Not Found<\/title>/i;
var myRegex2 = /<title>The page cannot be found<\/title>/i;
var myRegex3 = /<meta name="WT.cg_s" content="Error">/i;
	
var urlRegex = /^https:/i;
var urlRegex2 = /^http:\/\/usbnet\.int\.usbc\.com/i;
var urlRegex3 = /^http:\/\/usbnetportal\.us\.bank-dns\.com/i;
var urlRegex4 = /^\//i;
var urlRegex5 = /^https:\/\/usbnet2\.int\.usbc\.com/i;
var urlRegex6 = /^http:\/\/usbnet\.int\.usbc\.com\/hr/i;
	
var domainRegex = /usbnet\.int\.usbc\.com/i;
var domainRegex2 = /(usbnet1t|usbnet1u)/i;
var domainRegex3 = /usbnetportal\.us\.bank-dns\.com/i;
var domainRegex4 = /it2usbank\.us\.bank-dns\.com/i;
var domainRegex5 = /uat2\.www\.usbankhr\.com/i;
var domainRegex6 = /www\.usbankhr\.com/i;
var domainRegex7 = /www\.usbank\.com/i;
var domainRegex8 = /\.us\.bank-dns\.com/i;
			
var thisPageDomain = document.domain;
	
function checkLink(someURL, newWindow) {
	if(urlRegex6.test(someURL)){
		// if hard-coded intranet link, 1st check for existence on internet side
		someURL = someURL.replace(urlRegex2, "");
		
		checkForPage(someURL, newWindow);
	}
	else if((domainRegex4.test(thisPageDomain) || domainRegex5.test(thisPageDomain) || domainRegex6.test(thisPageDomain) || domainRegex7.test(thisPageDomain)) && (urlRegex2.test(someURL) || urlRegex3.test(someURL)|| urlRegex5.test(someURL) || domainRegex8.test(someURL))){
		//if on the internet HR site and navigating to a fully qualified URL on the intranet or
		// if URL has us.bank-dns.com in the domain, its behind the firewall and only available if the user is connected to the network
		if(confirm("This link is currently only available on the Intranet. If you have Intranet access, press OK, otherwise press Cancel.")){
			if(newWindow){
				window.open(someURL, '');
			}
			else{
				document.location.href = someURL;
			}
		}
	}
	else if(urlRegex.test(someURL) || domainRegex6.test(someURL) || domainRegex7.test(someURL)){
		//full url secure page, cannot perform GET operation
		//or full URL HR site or US Bank homepage
		if(newWindow){
			window.open(someURL, '');
		}
		else{
			document.location.href = someURL;
		}
	}
	else if((!(domainRegex.test(someURL) || domainRegex2.test(someURL) || domainRegex3.test(someURL) || domainRegex4.test(someURL) || domainRegex5.test(someURL) || domainRegex6.test(someURL) || domainRegex7.test(someURL)) && ! urlRegex4.test(someURL))){
		//if a url has a domain outside of HR sites 
		if(newWindow){
			window.open(someURL, '');
		}
		else{
			document.location.href = someURL;
		}
	}
	else if(urlRegex4.test(someURL) && (domainRegex.test(thisPageDomain) || domainRegex2.test(thisPageDomain) || domainRegex3.test(thisPageDomain))){
		// if the url starts with a forward slash and user is on intranet
		if(newWindow){
			window.open(someURL, '');
		}
		else{
			document.location.href = someURL;
		}
	}	
	else if(urlRegex2.test(someURL) || urlRegex3.test(someURL)){
		//full intranet url
								
		if(domainRegex.test(thisPageDomain) || domainRegex2.test(thisPageDomain) || domainRegex3.test(thisPageDomain)){
			//currently on intranet page
			if(newWindow){
				window.open(someURL, '');
			}
			else{
				document.location.href = someURL;
			}
		}
		else{
			//currently on internet page
			if(confirm("This link is currently only available on the Intranet. If you have Intranet access, press OK, otherwise press Cancel.")){
				if(newWindow){
					window.open(someURL, '');
				}
				else{
					document.location.href = someURL;
				}
			}
		}
	}
	else{
		checkForPage(someURL, newWindow);
	}
}

function checkForPage(someURL, newWindow){
	var bookmarkRegex = /#/i;
	var urlToGet = someURL;
	
	if(bookmarkRegex.test(someURL)){
		var urlArray = someURL.split("#");
		urlToGet = urlArray[0];
	}
		
	//Firefox and Netscape
	if (window.XMLHttpRequest) {
		request = new XMLHttpRequest();
	}
	//IE
	else if (window.ActiveXObject) {
		request = new ActiveXObject("Microsoft.XMLHttp");
	}
		
	if (request) {
		request.open("GET", urlToGet);
		request.onreadystatechange = function() {
			if (request.readyState == 4){
					response = request.responseText.toString();
					
					if(request.status == 200 && ! myRegex.test(response) && ! myRegex2.test(response) && ! myRegex3.test(response)){
						//page is there
						if(newWindow){
							window.open(someURL, '');
						}
						else{
							document.location.href = someURL;
						}
					}
					else{
						//page is not there
						if(confirm("This link is currently only available on the Intranet. If you have Intranet access, press OK, otherwise press Cancel.")){
							if(urlRegex4.test(someURL)){
								//if the url starts with a forward slash
								if(domainRegex5.test(thisPageDomain)){
									// domain is UAT
									someURL = "http://usbnet1u" + someURL;
								}
								else if(domainRegex6.test(thisPageDomain) || domainRegex7.test(thisPageDomain)){
									//domain is one of the production internet domains
									someURL = "http://usbnet.int.usbc.com" + someURL;
								}
								else{
									someURL = "http://usbnet1t" + someURL;
								}
							
								if(newWindow){
									window.open(someURL, '');
								}
								else{
									document.location.href = someURL;
								}
							}
						}
					}													
			}
		}
							
		request.send(null);
	}
}