// Setup some variables
var disabledExitSurvey = false;
var surveyWindow;

function popSurvey()
{
        leftVal = ((screen.width  - 600) / 2);
        topVal  = ((screen.height - 300) / 2);

	surveyWindow = window.open(
		'http://www.FrameVillage.com/exitSurvey.html', '', 
		'width=600,height=300,left='+leftVal+',top='+topVal+','+
		'scrollbars=0,resizable=0,toolbar=0,location=0,menubar=0,'+
		'status=0,directories=0');
}

function focusSurvey()
{
	if (surveyWindow)
	{
		surveyWindow.focus();
	}
}

function get_cookie(Name)
{
	var search = Name + "=";
	var returnvalue = "";

	if (document.cookie.length > 0)
	{
		offset = document.cookie.indexOf(search);
		if (offset != -1)
		{
			// if cookie exists
			offset += search.length;

			// set index of beginning of value
			end = document.cookie.indexOf(";", offset);

			// set index of end of cookie value
			if (end == -1)
				end = document.cookie.length;

			returnvalue = unescape(
				document.cookie.substring( offset, end )
				);
		}
	}

	return returnvalue;
}

function exitSurvey()
{
	if (!disabledExitSurvey)
	{
		if (get_cookie('exitSurvey')=='')
		{
			if (confirm("Please help us improve our storefront by taking a quick 1 question survey"))
			{
				location.href="http://www.FrameVillage.com/exitSurvey.html";
				// popSurvey();
				// sleep( 1 );
				// focusSurvey();
			}
		}
	}

	// disabledExitSurvey = false;
}

function addLoadEvent(func)
{
	var oldonload = window.onload;

	if (typeof window.onload != 'function')
	{
		window.onload = func;
	}
	else
	{
		window.onload = function()
		{
			if (oldonload)
			{
				oldonload();
			}

			func();
		}
	}
}

function addBeforeUnloadEvent(func)
{
	var oldOnBeforeUnload = window.onbeforeunload;

	if (typeof window.onbeforeunload != 'function')
	{
		window.onbeforeunload = func;
	}
	else
	{
		window.onbeforeunload = function()
		{
			if (oldOnBeforeUnload)
			{
				oldOnBeforeUnload();
			}

			func();
		}
	}
}

function addUnloadEvent(func)
{
	var oldOnUnload = window.onunload;

	if (typeof window.onunload != 'function')
	{
		window.onunload = func;
	}
	else
	{
		window.onunload = function()
		{
			if (oldOnUnload)
			{
				oldOnUnload();
			}

			func();
		}
	}
}

function internalLink()
{
	disabledExitSurvey = true;
}

function fixLinks()
{
	for( var i=0; i<document.links.length; i++ )
	{
		if (typeof document.links[i].onclick != 'function')
		{
			document.links[i].onclick = internalLink;
		}
		else
		{
			var oldOnClick = document.links[i].onclick;
			document.links[i].onclick = function()
			{
				if (oldOnClick)
				{
					oldOnClick();
				}

				internalLink();
			}
		}
	}
}

// addLoadEvent( fixLinks );
// addBeforeUnloadEvent( exitSurvey );
// addUnloadEvent( focusSurvey );

// addUnloadEvent( exitSurvey );
