// ********************************
// Author: 	Jessica Wilkinson
// Date: 	5/7/09
// Title:	Contact form overlay after timeout
// ********************************

//global variables
var secTilPopUp = 60;
var cookieDate, cookieHasRun, now, visitDate, interval;

window.onload = function() {
	cookieHasRun = lavidge_getCookie("hasRun");
	
	if(!cookieHasRun || cookieHasRun != "true")
	{
		now = new Date();
		now.setTime( now.getTime() );
		
		cookieDate = lavidge_getCookie("visitDate");
		//alert("cookie date: " + cookieDate);
		
		if(cookieDate)
		{
			//visitDate = new Date();
			//visitDate.setUTCMilliseconds(cookieDate);
			visitDate = cookieDate;
			
			//set timer function
			lavidge_setTimeFunction();
		}
		else
		{
			visitDate = now;
			//cookieDate = visitDate.getUTCMilliseconds();
			cookieDate = visitDate.getTime();
			//alert("setting cookie time to : " + cookieDate);
			lavidge_setCookie("visitDate", cookieDate);
			//set timer function
			lavidge_setTimeFunction();
		}
	}
}

//functions
function lavidge_setTimeFunction()
{
	interval = setInterval("lavidge_tryShowEmailForm()", 1000);
}

function lavidge_cancelTimerFunction()
{
	clearInterval(interval);
}

function lavidge_tryShowEmailForm()
{
	now = new Date();
	//now.setTime( now.getTime() );
	//alert(visitDate)
	var difSec =(now.getTime() - visitDate)/1000;
	//alert(difSec + " - " + secTilPopUp);
	if(difSec >= secTilPopUp)
	{
		//pop up the form
		lavidge_showEmailForm();
	}
}

function lavidge_showEmailForm()
{
	//cancel timer function
	lavidge_cancelTimerFunction();
	lavidge_setCookie("hasRun", "true");
	lavidge_showModalEmailForm();	
}

function lavidge_getCookie(sKey)
{
	var sRE = "(?:; )?" + sKey + "=([^;]*);?";
	var oRE = new RegExp(sRE);
	
	if(oRE.test(document.cookie)) {
		var match = decodeURIComponent(RegExp["$1"]);
		if(match.indexOf("&") > -1)
			match = match.substring(0, match.indexOf("&"));
		return match;
	}
	else {
		return null;
	}
}

function lavidge_setCookie(key, value)
{
	var aExpires, aPath, aSecure, aDomain, today, aExpires_date;
	today = new Date();
	today.setTime( today.getTime() );
	
	aDomain = ".cachethomes.net";
	aExpires = 1;
	if (aExpires)
	{
		aExpires = aExpires * 1000 * 60 * 60 * 24;
		aExpires_date = new Date( today.getTime() + (aExpires) );
	}
	
	aPath = "/";
	document.cookie = key + "=" + value +	
		( ( aPath ) ? ";path=" + aPath : "" ) + 
		( ( aDomain ) ? ";domain=" + aDomain : "" ) +
		( ( aSecure ) ? ";secure" : "" );
}

function lavidge_showModalEmailForm()
{	
	lavidge_prepareIE('100%', 'hidden');
	lavidge_toggleDDL('none');
	
	var dFull = document.getElementById("overlay");
	var dSub = document.getElementById("lightbox");	
	var dForm = document.getElementById("emailForm");
	
	if(dFull != null)
		dFull.style.display = "block";
	if(dSub != null)
	{
		dSub.style.display = "block";	
		
		var formHTML = dForm.innerHTML;
		dForm.innerHTML = "";
		
		dSub.innerHTML = formHTML;
	}			
}

function lavidge_closeModalEmailForm()
{
	lavidge_prepareIE('auto', 'auto');
	var dFull = document.getElementById("overlay");
	var dSub = document.getElementById("lightbox");	
		
	lavidge_toggleDDL('inline');

	if(dFull != null)
		dFull.style.display = "none";
	if(dSub != null)
	{
		dSub.innerHTML = "";	
		dSub.style.display = "none";
	}
}

function lavidge_prepareIE(height, overflow)
{
	try
	{
		var htm = document.getElementsByTagName('html')[0];
		htm.scrollTop = 0;
		htm.style.height = height;
		htm.style.overflow = overflow;
	}
	catch(ex)
	{
		//alert(ex);	
	}
}

function lavidge_toggleDDL(dis)
{
	//loop through controls and all selects
	//compensate for an IE bug called burnthrough where select boxes do not respect z-index
	//document.forms[0]["giftCertificate5:ddlRegion"].style.display = "none";
	//document.forms[0]["giftCertificate5:ddlRegion"].style.display = "inline";
	var elem = document.forms[0].elements; 	
	for(var i = 0; i < elem.length; i++)
	{
		if(elem[i].type == 'select-one')
		{
			elem[i].style.display = dis;
		}
	}		
}
