﻿var win;
var popUpWindow = false;

var windowName = "popup";
var windowOpt = "toolbar,status,resizable,scrollbars,menubar,location,height=600,width=800";

function doOpen(url)
{	
	// attempt to open the popup
	win = window.open(url, windowName, windowOpt); 
	if (win)
	{
		// popup successfully created
		if(!popUpWindow)
		    win.blur();
	} else {
		// popup was not created.
	    //if( confirm("You have a Popup blocker enabled.") )
	    //    doOpen(url);
	}
	
	return win;
}

//Exit popup code

var Page_ShowPopOnExit=false;
var hostName = document.location.hostname;
var exitURL = "";

function XBrowserAddHandlerPops(target,eventName,handlerName) {
    if ( target.addEventListener ) {
        target.addEventListener(eventName, function(e){target[handlerName](e);}, false);
    } else if ( target.attachEvent ) {
        target.attachEvent("on" + eventName, function(e){target[handlerName](e);});
    } else {
        var originalHandler = target["on" + eventName];
        if ( originalHandler ) {
            target["on" + eventName] = function(e){originalHandler(e);target[handlerName](e);};
        } else {
            target["on" + eventName] = target[handlerName];
        }
    }
}

function ExternalLink() {
	Page_ShowPopOnExit = true;
}

function SiteExit() {       
    if (Page_ShowPopOnExit==true)
        win = window.open(exitURL, windowName, windowOpt); 
}

function LinkConvert()
{
    var href;
	var anchors = document.getElementsByTagName('a');

	for(var y=0; y<anchors.length; y++)
	{
		href = anchors[y].href.toLowerCase();		
		if (href != "" && href.indexOf(hostName) == -1 && href.indexOf("javascript:") == -1)
        {            
            anchors[y].clickhandler=ExternalLink
            XBrowserAddHandlerPops(anchors[y],"click","clickhandler");
        }
	}
}

function HandleOnClose(e) 
{          
   //Currently does not work with Firefox
   var width = GetWidth();
   if (event && event.clientY < 0 && (event.clientX > width && event.clientX < width + 75)) 
        Page_ShowPopOnExit = true;
}

function initPopUpExit(url)
{        
    exitURL = url;
    XBrowserAddHandlerPops(window,"load","LinkConvert");
    XBrowserAddHandlerPops(window,"unload","SiteExit");
    //XBrowserAddHandlerPops(window,"submit","ExternalLink");
    Page_ShowPopOnExit=false;        
}

function GetWidth() 
{
    var myWidth = 0;
    if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        myWidth = window.innerWidth;        
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;        
    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;        
    }
    return myWidth;
}