/***********************************************
|A|n|d|y|S|o|w|a|r|d|s|.|c|o|m|
------------------------------------------
* Easy Javascript/PHP Exit Pop up script @ AndySowards.com Developer's Blog (www.andysowards.com)
* This notice MUST stay intact for legal use
* Visit Andy Sowards at http://www.andysowards.com/ for full source code
***********************************************/

//EDIT HERE ONLY

var ExitPopURL = 'http://www.realestatedaytrading.com.php5-5.dfw1-2.websitetestlink.com/opt_in_modal.html?keepThis=true&TB_iframe=true&height=355&width=590'; //This is the URL where your 'exit page' is located.
var AlertBox = "*****************************************************\n\nWait! Stop! Don't Go!\n\nBefore leaving, I have a SPECIAL SURPRISE For You\n\nI will give you my Autographed book for $19.95\n A 30% discount!\n\n WOW!.\n Click Cancel to Claim it NOW!   \n\n*****************************************************"; // This is what the alert() pop up verbage says.
//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

function ShowContent(d) {
if(d.length < 1) { return; }
document.getElementById(d).style.display = "block";
}
function HideContent(d) {
if(d.length < 1) { return; }
document.getElementById(d).style.display = "none";
}
//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupContact").fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupContact").fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	//centering
	$("#popupContact").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	
	//LOADING POPUP
	//Click the button event!
	$("#button").click(function(){
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
	});
				
	//CLOSING POPUP
	//Click the x event!
	$("#popupContactClose").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

});
//DO NOT EDIT BELOW This LINE (Unless of course your Savvy!) ------------------------------



window.onload = function(){
	// this is where we start our journey...
	createExitPop();
}// end function onunload

function ajaxGET(divId, page, effect) 
{ 
     var xmlHttp; 
     try 
    { 
         // Firefox, Opera 8.0+, Safari 
         xmlHttp=new XMLHttpRequest(); 
    } 
     catch(e)   
    { 
         // Internet Explorer 
         try 
         { 
              xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); 
         } 
         catch(e)   
          { 
               try 
               { 
                    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); 
               } 
               catch(e)   
             {      
                    alert("Your browser does not support AJAX!"); 
                  return false; 
             } 
         } 
     } 
      
    xmlHttp.onreadystatechange=function() 
     { 
         if(xmlHttp.readyState==4) 
          { 
               if(effect == 'collapse') { document.getElementById(divId).style.display='none'; } 
               else                     { document.getElementById(divId).innerHTML=xmlHttp.responseText; } 
         } 
    } 
     xmlHttp.open("GET",page,true); 
    xmlHttp.send(null); 
}

function createExitPop()
{
	var theBody = document.getElementsByTagName('body')[0]; 
	var newdiv = document.createElement('div');
	newdiv.setAttribute('id','ExitDiv');
	theBody.setAttribute('id','body');
	newdiv.setAttribute('style', 'width: 100%; height: 100%;');
	
		// put div on page
		theBody.appendChild(newdiv);
	
		//add exit pop to page (contents are from your exit.php(or whatever you named it) page)
		document.getElementById('ExitDiv').value = ajaxGET('ExitDiv', ExitPopURL);
	
	// style exit pop to resemble its own page
	document.getElementById('ExitDiv').style.display = "none"; 
	document.getElementById('ExitDiv').style.top = '0px'; 
	document.getElementById('ExitDiv').style.left = '0px'; 
	document.getElementById('ExitDiv').style.position = 'relative'; 
	document.getElementById('ExitDiv').style.backgroundColor = '#FFFFFF';
	
}// end createExitPop

isExit = true;

function ExitPop(isExit) {
		if(isExit != false)	{
			isExit=false;
			isPop = true;
			var bodyTag = document.getElementById? document.getElementsByTagName("BODY")[0] : document.body;
			
			// add id="body" so that it can be referenced.
			centerPopup();
			ShowContent('popupContact');
			loadPopup();
			//replace body text with exit pop
			return AlertBox;
		} // end if
	}// end function

window.onbeforeunload = function(){
		
		// Lay down an exit pop!!
		return ExitPop(isExit);
		
	
}// end function onunload
