/**
* $Id$
*
* Exit Pops -- present user with ability to just press a single button to get rates.
*
* @author rICh <rich@intermundomedia.com>
*/
var redirTo = '';
var hasPopped = false;
var isInternalClick = false;
var optionClicked = false;
//get our QueryString & pass in the imm_cid & imm_mid
var	QueryString = $.parseQuery();
var exitPopCounter = 0;

if((typeof(cookieDomain) == 'undefined'))
{
	cookieDomain = location.hostname;
}

if(readCookie('has_popped') && !readCookie('pop_viewed'))
{
	window.location = '/abandonReport.php/?status=ratesredir&clicked='+optionClicked+'&querystring='+escape('imm_cid='+QueryString.imm_cid+'&imm_mid='+QueryString.imm_mid+'&propertyStateCode='+QueryString.propertyStateCode+'&typeOfLoan='+QueryString.typeOfLoan);
}

function fillAjaxAbandon(abandonUrl, abandonQuery)
{
	document.cookie = "viewing_rates=1; domain="+cookieDomain+"; path=/;";
	document.getElementById("dpr_container").innerHTML = '<div style="margin: 10px auto; background-image: url(https://www.nextinsure.com/ListingDisplay/Images/300x137_loading_bg.gif); text-align: center; width: 300px; height: 137px; font-family: Arial; font-weight: bold; color: rgb(51, 51, 51); background-repeat: no-repeat; font-size: 16px; padding-top: 30px;">Finding Your Matches.<br><img style="border: 0pt none ; width: 35px; height: 35px; margin-top: 5px;" src="https://www.nextinsure.com/ListingDisplay/Images/thumper.gif"></div>';
	$.ajax
	({
		data:abandonQuery,
		url:abandonUrl,
		cache:true,
		type:"GET",
		success:function(html)
		{
			document.getElementById("dpr_container").innerHTML = html;
			document.getElementById("sh_initialLoading").innerHTML = '<div style="margin: 10px auto; background-image: url(https://www.nextinsure.com/ListingDisplay/Images/300x137_loading_bg.gif); text-align: center; width: 300px; height: 137px; font-family: Arial; font-weight: bold; color: rgb(51, 51, 51); background-repeat: no-repeat; font-size: 16px; padding-top: 30px;">Finding Your Matches.<br><img style="border: 0pt none ; width: 35px; height: 35px; margin-top: 5px;" src="https://www.nextinsure.com/ListingDisplay/Images/thumper.gif"></div>';
			callResults("0");
		}
	});


}

function checkForReload() {
	hasPopped = checkPopped();
	var doRedir = false;

	if(redirTo == 'rates_feed' || redirTo == 'rates_ajax') {
		doRedir = true;
	}
	else if(!hasPopped && exitPopCounter > 3600)
	{
		if(timeOut())
		{
			redirTo = 'rates_ajax';
		}
		else
		{
			exitPopCounter = 0;
		}
	}
	else
	{
		exitPopCounter++;
	}

	if(doRedir == true)
	{
		//Allow it to fire once, block all subsequent
		hasPopped = true;
		//pass with params (need to pull out the host name as this code is called from several subdomains)
		var domainParts = location.hostname.split(".");

		var synthhost = "";

		for(i = 0; i < domainParts.length; i++)
		{
			if(domainParts[i].toLowerCase() == "www"){ continue; }
			if(domainParts[i].toLowerCase() == "form"){ continue; }
			synthhost += "." + domainParts[i].toLowerCase();
		}

		if(redirTo == 'rates_feed')
		{
			window.location = '/abandonReport.php/?status=ratesredir&clicked='+optionClicked+'&querystring='+escape('imm_cid='+QueryString.imm_cid+'&imm_mid='+QueryString.imm_mid+'&propertyStateCode='+QueryString.propertyStateCode+'&typeOfLoan='+QueryString.typeOfLoan);
			clearInterval(abandonInterval);
		}
		else if(redirTo == 'rates_ajax')
		{
			fillAjaxAbandon("mortgage-results.ajaxVersion.php", 'clicked='+optionClicked+'&imm_cid='+QueryString.imm_cid+'&imm_mid='+QueryString.imm_mid+'&propertyStateCode='+QueryString.propertyStateCode+'&typeOfLoan='+QueryString.typeOfLoan);
			clearInterval(abandonInterval);
		}
		redirTo = '';
	}
}

function exitcheck(popType){
	
	if(!isInternalClick) {
		//'has_popped' is set inside here, but also fall back on 'Converted' cookie (set by '/WriteConversion.php')
		hasPopped = checkPopped();
		if(!hasPopped) {
			if (quit()) {
				// user clicked OK -- send them to the results
				if(typeof(popType) == "undefined")
				{
					redirTo = 'rates_feed';
				}
				else if(popType == "ajax")
				{
					redirTo = 'rates_ajax';
				}
				else
				{
					redirTo = 'rates_feed';
				}
			}
		}
	} else {
		// reset 'internalClick'
		isInternalClick = false;
	}
}

function quit() {
	//write our cookie to ensure we only pop once.
	document.cookie = "has_popped=true; domain="+cookieDomain+"; path=/;";
	var newImg = document.createElement("img");
	newImg.id = "immQuitPxl";
	newImg.src = document.location.protocol + "//"+window.location.hostname+"/abandonReport.php/?status=abandon&browser="+escape(navigator.appName+"~:~"+navigator.appVersion)+"&url="+escape(location.href);
	newImg.height = "1";
	newImg.width = "1";
	document.body.appendChild(newImg);

	if (confirm(getQuitMessage())) {
		document.getElementById("immQuitPxl").src = document.location.protocol + "//"+window.location.hostname+"/abandonReport.php/?status=ok_abandon";
		optionClicked = "OK";
		var1 += ":"+optionClicked;
		document.cookie = "abandon_clicked="+optionClicked+"; domain="+cookieDomain+"; path=/;";
		return true;
	} else {
		//document.cookie = "pop_viewed=true; domain="+cookieDomain+"; path=/;";
		document.getElementById("immQuitPxl").src = document.location.protocol + "//"+window.location.hostname+"/abandonReport.php/?status=cn_abandon";
		optionClicked = "cancel";
		var1 += ":"+optionClicked;
		document.cookie = "abandon_clicked="+optionClicked+"; domain="+cookieDomain+"; path=/;";
		return true;
	}
}

function timeOut() {
	var newImg = document.createElement("img");
	newImg.id = "immTOPxl";
	newImg.src = document.location.protocol + "//"+window.location.hostname+"/abandonReport.php/?status=timeout&browser="+escape(navigator.appName+"~:~"+navigator.appVersion)+"&url="+escape(location.href);
	newImg.height = "1";
	newImg.width = "1";
	document.body.appendChild(newImg);

	//write our cookie to ensure we only pop once.
	if (confirm(getQuitMessage())) {
		document.getElementById("immTOPxl").src = document.location.protocol + "//"+window.location.hostname+"/abandonReport.php/?status=ok_timeout";
		document.cookie = "has_popped=true; domain="+cookieDomain+"; path=/;";
		optionClicked = "OK";
		var1 += ":"+optionClicked;
		document.cookie = "abandon_clicked="+optionClicked+"; domain="+cookieDomain+"; path=/;";
		return true;
	} else {
		document.getElementById("immTOPxl").src = document.location.protocol + "//"+window.location.hostname+"/abandonReport.php/?status=cn_timeout";
		return false;
	}
}

function getQuitMessage()
{
	return "No time to fill out a form? Just click OK to browse top loan offers in your area right away.";
}

function checkPopped()
{
	return (readCookie('has_popped') || (readCookie('Converted') == 'YES'));
}

// scan for onBeforeUnload event, and redirect if the user would like to get rates
var abandonInterval = setInterval('checkForReload();',100);
window.onbeforeunload = exitcheck;
