﻿
/*Note: The load and unload events are implemented in default.aspx.cs
  Bugs: -   When pressing the back or forward button of the browser the exit popup will be triggered
        -   When refreshing the page the exit popup will also be triggered
        -   When pressing enter in address bar will also trigger the exit popup
*/
//// Create namespaces
if (!Creative)
	var Creative = { };
	
if (!Creative.Waad)
	Creative.Waad = { };

Creative.Waad.ExitPopup = {
    Page_ShowPopOnExit: false,
    SiteDomain : "",
    PopupUrl : "",
    
    InitializeExitPopup : function (domain, popupurl){
        Creative.Waad.Events.AddEvent(window,'load',Creative.Waad.ExitPopup.LinkConvert);
        Creative.Waad.Events.AddEvent(window,'unload',Creative.Waad.ExitPopup.SiteExit);
        Creative.Waad.ExitPopup.SiteDomain = domain;
        Creative.Waad.ExitPopup.PopupUrl = popupurl;
        //Set default to show the popup...
        Creative.Waad.ExitPopup.Page_ShowPopOnExit=true;
    },  
    
    LinkConvert: function (){
    
        //add's an eventhandler for the links for specified SiteDomains
	    //if the eventhandler isn't implemented every link click on the website will trigger the exit popup
	    //but we only want the exit popup to be triggered when leaving the website(closing the browser or navigating to another website)
        var href;
	    var links = document.links;
	    var divs = document.getElementsByTagName('div');
	    var h2tag = document.getElementsByTagName('h2');
        
        for(var y=0; y<links.length; y++)
	    {
		    href = links[y].href.toLowerCase();
		    //Checks if the link destination is still in the specified SiteDomains
		    if (!(href.indexOf("http://")!=-1 && href.indexOf(Creative.Waad.ExitPopup.SiteDomain)==-1))
		    {
		        Creative.Waad.Events.AddEvent(links[y],'click',Creative.Waad.ExitPopup.InternalLink);
		    }
	    }
    	
	    //Convert div links which have a onclick event specified
	    for(var x=0; x<divs.length; x++)
	    {
	        if(divs[x].getAttributeNode('onclick')){
	            if(divs[x].getAttributeNode('onclick').nodeValue){
	                href = divs[x].getAttributeNode('onclick').nodeValue.toLowerCase();
	                //Checks if the link destination is still in the specified SiteDomains
	                if (!(href.indexOf("http://")!=-1 && href.indexOf(Creative.Waad.ExitPopup.SiteDomain)==-1))
		            {
		                Creative.Waad.Events.AddEvent(divs[x],'click',Creative.Waad.ExitPopup.InternalLink);
		            }
		        }
	        }
	    }
	    //Convert h2 links which have a onclick event specified
	    for(var x=0; x<h2tag.length; x++)
	    {
	        if(h2tag[x].getAttributeNode('onclick')){
	            if(h2tag[x].getAttributeNode('onclick').nodeValue){
	                href = h2tag[x].getAttributeNode('onclick').nodeValue.toLowerCase();
	                //Checks if the link destination is still in the specified SiteDomains
	                if (!(href.indexOf("http://")!=-1 && href.indexOf(Creative.Waad.ExitPopup.SiteDomain)==-1))
		            {
		                Creative.Waad.Events.AddEvent(h2tag[x],'click',Creative.Waad.ExitPopup.InternalLink);
		            }
		        }
	        }
	    }
    },
    
    InternalLink: function () {
	    Creative.Waad.ExitPopup.Page_ShowPopOnExit = false;
	},


    SiteExit: function () {
        if(Creative.Waad.ExitPopup.Page_ShowPopOnExit==true){
            //ShowExitPopup is implemented in script.ascx
            if (ShowExitPopup() == "True"){
                //Exit popup Url
                Creative.Waad.ExitPopup.OpenWindow(Creative.Waad.ExitPopup.PopupUrl,"exit_popup", "730", "450");
            }
        }
    },
    
    OpenWindow: function (filename,windowname,sWidth,sHeight)
    {
        var mywindow = window.open(filename,windowname,'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=' + sWidth + ',height=' + sHeight)
        if(mywindow != null){
            mywindow.focus();
        }
    }
}

