function addEvent(elm, evType, fn, useCapture){if(elm.addEventListener){elm.addEventListener(evType, fn, useCapture);return true;}else if (elm.attachEvent){var r = elm.attachEvent('on' + evType, fn);return r;}else{elm['on' + evType] = fn;}}

var appWindow = null;

function closeAppWin(){
	if (appWindow != null && !appWindow.closed) {
			appWindow.close();
	}
}

function popupApplication(e) {	
	
	closeAppWin();
	
	var width = "642";
	var screenY = screen.height;
	var screenX = screen.width;
	var topvar = 0;
	var leftvar = (screenX - width) / 2;
	
	if (screen.height && screen.width) {
		height = screen.height - 80;
	} else {
		height = 520;
	}
	
	if (window.open){
		appWindow = window.open(this.href,'','resizable=no,scrollbars=yes,toolbar=no,menubar=no,location=no,status=yes,top='+topvar+',left='+leftvar+',width='+width+',height='+height+'');
		appWindow.focus();
		//cancel the default link action if pop-up activated
		if (window.event)  {
			window.event.returnValue = false;
			window.event.cancelBubble = true;
		} else if (e) {
			e.stopPropagation();
			e.preventDefault();
		}
	}
}

function findAppPopup() {
	var popups = document.getElementsByTagName("a");
	for (i=0;i<popups.length;i++) { 
		if (popups[i].href.indexOf("https://apply.gomastercard.com.au")!=-1) {
			// attach popup behaviour
			popups[i].onclick = popupApplication;
			// add popup indicator
			/*
			if (popups[i].rel.indexOf("noicon")==-1) {
				//more of the 'no icon' thing in a moment ...
				popups[i].style.backgroundImage = "url(pop-up.gif)";
				popups[i].style.backgroundPosition = "0 center";
				popups[i].style.backgroundRepeat = "no-repeat";
				popups[i].style.paddingLeft = "15px";
   			}*/
  			// add info to title attribute to alert fact that it's a pop-up window
  			popups[i].title = popups[i].title + " [Opens in pop-up window]";
  		}
 	}
}

addEvent(window, "load", findAppPopup, false);