/**** This file brings up a lead capture form if the user does not have a certain cookie set.
	* Designed by Christopher Denyer of Denyer Admin LTD, Great Britain.
	* Copyright (c) Christopher Denyer 2010
	****/
	

// Settings you can configure
// **************************
	
	// Dimmer opacity %.
	// How transparent should the page be? A value between 0 and 100.
	// 0 = no shadow; 100 = page completely hidden.
	var opacity = 80;
	
// Settings you CAN'T configure
// ****************************

	// Check cookie support
	document.cookie = 'cookieSupport';
	var cookiesAllowed = true;
	if ( !/\bcookieSupport\b/.test( document.cookie ) ){
		// Cookies not supported
		cookiesAllowed = false;
	}

	// Set a page identifier cookie
	var date = new Date();
	date.setTime( date.getTime() + 24 * 60 * 60 * 1000 );
	document.cookie = 'isLeadForm=Most certainly is Watson; path=/; domain=' + location.hostname + '; expires=' + date.toGMTString();

	// Ajax
	function getHTTP() {
		if (typeof XMLHttpRequest != 'undefined') {
			return new XMLHttpRequest();
		}
		try {
			return new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				return new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
		return false;
	}
	var http = getHTTP();

	
// This function shows the lead capture popup
// ******************************************
function showLeadCapture()
{
	// Don't even bother if cookies unsupported
	if(!cookiesAllowed){
		return false;
	}
	
	// Check if cookie is set
	var cookiePattern = /\bcaptured=([^;]*)/;
	if( !cookiePattern.test( document.cookie )){
		// Cookie not found
		// Dim the page first
		var overlay = document.createElement( 'div' );
		overlay.id					= 'lcOverlay';
		overlay.style.zIndex 		= '1000';
		overlay.style.position 		= 'fixed';
		overlay.style.top			= '0px';
		overlay.style.left			= '0px';
		overlay.style.width			= '100%';
		overlay.style.height		= '100%';
		overlay.style.opacity		= opacity / 100 + '';
		overlay.style.filter		= 'alpha(opacity='+opacity+')';
		overlay.style.backgroundColor = 'black';
		document.getElementsByTagName('body')[0].appendChild( overlay );
		
		var container = document.createElement( 'div' );
		container.id				= 'lcContainer';
		container.style.zIndex		= '1010';
		container.style.position	= 'fixed';
		container.style.left		= '0px';
		container.style.top			= '0px';
		container.style.height		= '100%';
		container.style.width		= '100%';
		document.getElementsByTagName('body')[0].appendChild( container );
		
		// Add stylesheet
		var stylesheet = document.createElement( 'link' );
		stylesheet.href				= 'leadcapture/styles.css';
		stylesheet.type				= 'text/css';
		stylesheet.rel				= 'stylesheet';
		document.getElementsByTagName('head')[0].appendChild( stylesheet );
		
		// Then Download template
		var dateVar = new Date();
		http.open( "GET", "http://" + location.hostname + "/leadcapture/template.html?nocache=" + dateVar );
		http.onreadystatechange = function () {
			if( http.readyState == 4 ){
				// Received
				var overlay = document.getElementById( 'lcContainer' );
				overlay.innerHTML = http.responseText;
				
				// Attach verification code
				var jsVerify = document.createElement('script');
				jsVerify.src = "/leadcapture/verify.js";
				jsVerify.type = "text/javascript";
				document.getElementsByTagName( 'head' )[0].appendChild( jsVerify );
			}
		};
		http.send( null );
	}
}

// This is a debugging function to erase the capture cookie and allow you to see the form a second time.
// *****************************************************************************************************
function flushCookie()
{
	// Erase capture cookie
	var date = new Date();
	date.setTime( date.getTime() - 24 * 60 * 60 * 1000 );
	document.cookie = 'captured=; path=/; domain=' + location.hostname + ';expires=' + date.toGMTString();
	
	alert( 'Cookie should be gone, refresh the page to see the form once again.');
}
