/* ------------------------------------------------------------------------------------------------------------------------------ */
// subscribe checking and popup display control/initialization
/* ------------------------------------------------------------------------------------------------------------------------------ */
function checkSubscribe() {
	
	var cookiesenabled		= hasCookies();

	if (cookiesenabled) {

		// direct processing according to domain (DD vs non-DD)
		var loc	= document.location.toString();
	
		if (loc.match(/dragondoor.com/i)) {
			// DD domain popup processing
			processSubscribeCheck();
	
		} else {
			// non-DD domains popup processing
	
			// test if 3rd party cookies are supported for non-DD domains
			// set and confirm that a DD test cookie can created
			testDDCookie();
	
			// initiate the DD useremail cookie check
			// - loads images reflecting DD useremail status
			// - set a local useremail cookie
			checkDDUserEmail();
			
			// calls processSubscribeCheck() if DD cookie confirmed
			// - delayed 2 secs to allow JS img requests to complete
			setTimeout('testDDCookieConfirmed()', 2000);
		}
	}

}
/* ------------------------------------------------------------------------------------------------------------------------------ */

/* ------------------------------------------------------------------------------------------------------------------------------ */
function processSubscribeCheck() {

	try {
		// see common.js for cookie functions
		// called via body onload
		var subcookie			= readCookie('useremail');

		if (!subcookie) {
			document.getElementById("popupWrapper").style.position	= "fixed"; // no scroll
			activateDHTMLPopUp();
		} else {
			document.getElementById("popupWrapper").style.position	= "absolute";
		}

	}
	catch (err) {
		//if (isDev()) {alert(err) }
	};
}
/* ------------------------------------------------------------------------------------------------------------------------------ */


/* ------------------------------------------------------------------------------------------------------------------------------ */
// subscribe and defer processing
/* ------------------------------------------------------------------------------------------------------------------------------ */
function submitDHTMLSubscribe(frm) {

	try {
		var subresult	= processSubscribeRequest(frm);
	}
	catch (err) {
		var msg	= 'error<br>' + err;
		writeToElement('subscribesubmitmsg',msg);

		hideElement('subscribesubmit');
		showElement('subscribesubmitmsg');
	}
	return false;


}
/* ------------------------------------------------------------------------------------------------------------------------------ */

/* -------------------------------------------------------------------------------------------------------------------------------- */
function processDHTMLDeferralRequest(element) {

	// set a deferral cookie
	var expireDate		= new Date;
	var yr				= expireDate.getFullYear();
	var time			= expireDate.getTime();
	
	var loc		= document.location.toString();
	var domain	= loc.match(/http:\/\/([\w|\.]+)\//i);
	domain		= domain[1].match(/(\w+\.\w+)$/i);
	domain		= '.' + domain[1];
	
	var msg;

	if (element) {
		if (element.match(/never/)) {
			setDDUserEmail(element);
			msg					= "No problem, if you find you want to subscribe to a newsletter you will find many opportunities on the site";

		} else if (element.match(/week/)) {
			setDDUserEmail(element);
			msg					= "Thanks, see you in a week";
		}
		
		if (msg) {
			msg	+= "<br><font style=\"font-size:8pt\"><a href=\"javascript:closeDHMTLPopUp()\">Click here if this window doesn't close after 5 seconds.</a></font>";
			msg	= '<div style="margin:10px;padding:10px;background-color:#FFFFFF;font-size:10pt">'+msg+'</div>';
			writeToElement('subscribesubmitmsg',msg);
			hideElement('subscribesubmit');
			showElement('subscribesubmitmsg');
		}
	}

	setTimeout( 'closeDHMTLPopUp()', 4000 );
}
/* ------------------------------------------------------------------------------------------------------------------------------ */

/* ------------------------------------------------------------------------------------------------------------------------------ */
// pop css control
/* ------------------------------------------------------------------------------------------------------------------------------ */
function activateDHTMLPopUp() {

	// called after 2 seconds
	// checkDDUserEmail() sets a useremail cookie based on the DD useremail cookie
	// the DD cookie is check via an img loading status checking process
	var subcookie	= readCookie('useremail');

	if (!subcookie) {
		document.getElementById("popupWrapper").style.position	= "fixed"; // no scroll (except IE)
		showDHTMLPopUp();
	} else {
		document.getElementById("popupWrapper").style.position	= "absolute";
	}
}
/* ------------------------------------------------------------------------------------------------------------------------------ */

/* ------------------------------------------------------------------------------------------------------------------------------ */
function showDHTMLPopUp() {
	// load and display the subscribe popup
	getPopUpHTML();
	try {
		document.getElementById("popupBackground").className	= "block";
		document.getElementById("popupForm").className			= "block";
	}
	catch(err) {
	};
}
/* ------------------------------------------------------------------------------------------------------------------------------ */

/* ------------------------------------------------------------------------------------------------------------------------------ */
function getPopUpHTML() {
	// load the popup html via an ajax request
	var status = AjaxRequest.get(
		{	'url'		: '/includes/newsubpop_dhtml.html',
			'onSuccess'	: function(req) { writeToElement('popupForm', req.responseText); },
			'onLoading'	: function(req) { writeToElement('popupForm','sent request'); }
		}
	);
	return status;
}
/* ------------------------------------------------------------------------------------------------------------------------------ */

/* ------------------------------------------------------------------------------------------------------------------------------ */
function closeDHMTLPopUp() {
	// close the dhtml popup
	try {
		document.getElementById("popupBackground").className	= "none";
		document.getElementById("popupForm").className			= "none";
		document.getElementById("popupWrapper").style.position	= "absolute";
	}
	catch(err) {
	};
}
/* ------------------------------------------------------------------------------------------------------------------------------ */


/* ------------------------------------------------------------------------------------------------------------------------------ */
// DD useremail cookie processing - non-DD domains
/* ------------------------------------------------------------------------------------------------------------------------------ */
function checkDDUserEmail() {

	// check for the dragondoor.com useremail cookie
	var img		= new Image();
	img.onload	= function() {	processDDUserEmailResult(img); };
	img.id		= 'defer';
	img.src		= 'http://www.dragondoor.com/cgi-bin/subscribe_check.pl';
}
/* ------------------------------------------------------------------------------------------------------------------------------ */

/* ------------------------------------------------------------------------------------------------------------------------------ */
function checkDDUserEmailWeek() {

	// check for the dragondoor.com useremail cookie (week deferral)
	var img		= new Image();
	img.onload	= function() {	processDDUserEmailResult(img); };
	img.id		= 'week';
	img.src		= 'http://www.dragondoor.com/cgi-bin/subscribe_check.pl?week=1';
}
/* ------------------------------------------------------------------------------------------------------------------------------ */

/* ------------------------------------------------------------------------------------------------------------------------------ */
function processDDUserEmailResult(img) {

	var ue	= '';

	// check if the img loaded - create local cookie (no pop-up)
	if (getUserEmailImage(img)) {
		// image loaded - check which onload call week/defer
		// get the img id value - indicates which cookie is set on DD
		var uecookievalue	= img.id.match(/week/) ? 'week' : 'defer';
		if (!img.id.match(/week/)) {
			// useremail cookie exists - check if its the 7 day defer cookie
			checkDDUserEmailWeek();		// calls this function, handled below
			
			// check if checkDDUserEmailWeek() set a useremail cookie
			var ue	= readCookie('useremail');
			if (!ue) {
				createDomainCookie('useremail','deferred',730);
				uecookievalue	= 'deferred';
			}
		} else {
			// deferred - 7 days
			createDomainCookie('useremail','week',7);
			uecookievalue	= 'week';
		}
		//msg	+= ' loaded ' + uecookievalue;
	}
	//msg	+= '<br>';
	//writeToElement('debugmsg',msg, 'a');
	return 1;
}
/* ------------------------------------------------------------------------------------------------------------------------------ */

/* ------------------------------------------------------------------------------------------------------------------------------ */
function getUserEmailImage(img) {

	// check the loading status of the specified image
	if (navigator.userAgent == 'MSIE') {
		// IE specific
		if (!img.complete) {	return false;	}
	} else {
		if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0) {
			return false;
		}
	}
	return true;
}
/* ------------------------------------------------------------------------------------------------------------------------------ */

/* ------------------------------------------------------------------------------------------------------------------------------ */
function setDDUserEmail(value) {

	// create a local domain cookie and ensure a matching DD cookie exists
	var uecookievalue	= value.match(/week/) ? 'week' : 'never';
	if (!value.match(/week/)) {
		createDomainCookie('useremail',uecookievalue,730);
	} else {
		createDomainCookie('useremail',uecookievalue,7);
	}

	var loc	= document.location.toString();
	if (!loc.match(/dragondoor.com/i)) {
		// set the dragondoor.com useremail cookie
		var img	= new Image();
		img.src		= 'http://www.dragondoor.com/cgi-bin/subscribe_check.pl?ue='+value;
	}

}
/* ------------------------------------------------------------------------------------------------------------------------------ */


/* ------------------------------------------------------------------------------------------------------------------------------ */
// DD test cookie processing for non-DD domains
// Ensure the browser supports setting a DD cookie from a non-DD domain
/* ------------------------------------------------------------------------------------------------------------------------------ */
var ddCookies	= '';
/* ------------------------------------------------------------------------------------------------------------------------------ */
function testDDCookieConfirmed() {

	var msg		=  '';
	if (ddCookies.match(/cookieconfirmed/)) {
		//msg		=  'confirmed ' + ddCookies;
		// DD cookie test confirmed
		processSubscribeCheck();
	} else {
		//msg		=  'not confirmed ' + ddCookies;
		// no DD cookie - deferred
		createDomainCookie('useremail','never',730);
	}
}
/* ------------------------------------------------------------------------------------------------------------------------------ */

/* ------------------------------------------------------------------------------------------------------------------------------ */
function testDDCookie() {
	// set dragondoor.com test cookie - ensure browser compliance
	var img		= new Image();
	img.onload	= function() {	testDDCookieResult(img); };
	img.id		= 'cookietest';
	img.src		= 'http://www.dragondoor.com/cgi-bin/subscribe_check.pl?test=set';
}
/* ------------------------------------------------------------------------------------------------------------------------------ */

/* ------------------------------------------------------------------------------------------------------------------------------ */
function testDDCookieResult(img) {

	// called via img.onload - check if an image was returned
	if (getUserEmailImage(img)) {
		if (!ddCookies.match(/cookieconfirm/)) {
			// initial test cookie should be set - confirm cookie
			var img2	= new Image();
			img2.id		= 'cookieconfirm';
			img2.onload	= function() {	testDDCookieResult(img2); };
			img2.src	= 'http://www.dragondoor.com/cgi-bin/subscribe_check.pl?test=confirm';
			ddCookies	= 'cookieconfirm';
		}
		if (ddCookies.match(/cookieconfirm/) && img.id != 'cookietest') {
			ddCookies	= 'cookieconfirmed';
		}
	} else {
		// negative server reply
	}
}
/* ------------------------------------------------------------------------------------------------------------------------------ */


/* ------------------------------------------------------------------------------------------------------------------------------ */
// confirmation dhtml popup processing
/* ------------------------------------------------------------------------------------------------------------------------------ */
function displayDHTMLConfirmPopUp(request) {

	// load the subscribe confirmation popup
	try {
		window.scroll(0,0);	// scroll to top - for column subscriptions
		document.getElementById("popupForm").className			= "none";
		document.getElementById("popupWrapper").style.position	= "fixed"; // no scroll (except IE)
		loadDHTMLConfirmPopUp(request);
	}
	catch(err) {
	};
	//return 1;
}
/* ------------------------------------------------------------------------------------------------------------------------------ */

/* ------------------------------------------------------------------------------------------------------------------------------ */
function loadDHTMLConfirmPopUp(request) {
	// load and display the subscribe popup
	loadConfirmPopUpHTML(request);
	try {
		document.getElementById("popupConfirm").className		= "block";
		document.getElementById("popupBackground").className	= "block";
	}
	catch(err) {
	};
	//alert('ok');
}
/* ------------------------------------------------------------------------------------------------------------------------------ */

/* ------------------------------------------------------------------------------------------------------------------------------ */
function loadConfirmPopUpHTML(request) {
	// load the popup html via an ajax request
	// called via subscribe_ajax.js - processSubscriptionRequestResponse(response,request)
	var newsform				= request.newsform;

	var confirmrequest			= new Object();
	confirmrequest.confirmpopup	= 1;
	confirmrequest.email		= newsform.subscriber.value;

	if (request.pbp) {	confirmrequest.pbp	= 1	};
	if (request.pu) { 	confirmrequest.pu	= 1	};
	if (request.pn) { 	confirmrequest.pn	= 1	};

	var status = AjaxRequest.get(
		{	'url'		: '/cgi-bin/subscribe_confirmation.pl',
			'parameters': confirmrequest,
			'onSuccess'	: function(req) { writeToElement('popupConfirm', req.responseText); },
			'onLoading'	: function(req) { writeToElement('popupConfirm','sent request'); }
		}
	);
	return status;
}
/* ------------------------------------------------------------------------------------------------------------------------------ */

/* ------------------------------------------------------------------------------------------------------------------------------ */
function closeDHMTLConfirmPopUp() {
	// close the dhtml popup
	try {
		document.getElementById("popupBackground").className	= "none";
		document.getElementById("popupConfirm").className		= "none";
		document.getElementById("popupWrapper").style.position	= "absolute";
	}
	catch(err) {
	};
}
/* ------------------------------------------------------------------------------------------------------------------------------ */

