	//Order of operations
	// OnPageLoad we call surveyCheck()
	// Async call is made to fetch php settings
	// Once returned we trigger check count
	// if we are on or past the counter we then check the time
	// the user has been on the site.

	
	var phpVars = '';
	var ShowEveryXVists = "";
	var WindowName = "";
	var CurrentTime = "";
	var WindowProps = "";
	var SurveyHTML = "";
	var TimeBeforeShow = "";
	var notTakenCookieExpire="";
	var takenCookieExpire = "";
	

	function surveyCheck()
	{		

		var request = createXMLHttpRequest();
		
		request.open("GET", "getVar.php", true);
		request.onreadystatechange = function() 
		{
		    if (request.readyState == 4) 
		    {
				phpVars = request.responseText;
				eval(phpVars);
				checkForCookie();
		    }
		}
		request.send(null);
	}

	function checkForCookie()
	{

		// we need to move check count code here to determine if this unique visit is the
		// trigger to show the window, all the B path should do is not show the quiz for someone
		// who has gone on path A and already set the cdounter and their local cookie
	
		if(readCookie("value")=="") //document.cookie == '')
		{			
			//alert("A");
			var date = new Date();
			//			
			var oneYearFromNow = date.getTime() + (180 * 24 * 60 * 60 * 1000);			
			date.setTime(oneYearFromNow);			
			var expires = "; expires=" + date.toGMTString();		
			document.cookie = 'value=' + CurrentTime + expires;			
			
			var request = createXMLHttpRequest();				
			request.open("GET", "HitCounter.php?t=get", true);
			request.onreadystatechange = function() 
			{
			    if (request.readyState == 4) 
			    {			
				//alert(request.responseText);			
			        if(request.responseText == "true")
		        	{

			        	//checkTimeElapsed();
					//alert(TimeBeforeShow*1000 + " time to wait");
					var t = setTimeout(showWindowAfterTimeout, TimeBeforeShow*1000);

			        }
			    }
			}
			request.send(null);
			
		}
		else
		{
			//alert("B");
		}
	}


	function showWindowAfterTimeout()
	{
		var surveyWindow = dhtmlwindow.open("surveyWindow", "iframe", SurveyHTML, WindowName, WindowProps, "recal");
		surveyWindow.onclose=function()
		{
			var date = new Date();
			date.setTime(date.getTime() + (365*24*60*60*1000));
					
			var expires = "; expires="+date.toGMTString();
			
			document.cookie = 'value=' + notTakenCookieExpire + expires;
			return window.confirm("Are you sure you don't want to take the survey?")
		} 
	}

	
	function checkTimeElapsed()
	{				
		var cookieTime = readCookie('value');		
		var currentTime = CurrentTime;		
        	var timePast = TimeBeforeShow;

		//alert("cookieTime=" + cookieTime + " currentTime=" + currentTime + " timePast=" + timePast);

	        if((Number(currentTime) - Number(cookieTime)) >= Number(timePast))
        	{
			alert("about to show window");
		        var surveyWindow = dhtmlwindow.open("surveyWindow", "iframe", SurveyHTML, WindowName, WindowProps, "recal");
			surveyWindow.onclose=function()
			{
				var date = new Date();	
				date.setTime(date.getTime() + (365*24*60*60*1000));					
				var expires = "; expires="+date.toGMTString();			
				document.cookie = 'value=' + notTakenCookieExpire + expires;
				return window.confirm("Close window?")
			}        		
        	}
	}
	
	


	function readCookie(name) 
	{
		var cookiename = name + "=";	
		var ca = document.cookie.split(';');	
		for(var i=0;i < ca.length;i++) 
		{
			var c = ca[i];	
			while (c.charAt(0)==' ') c = c.substring(1,c.length);	
			if (c.indexOf(cookiename) == 0) return c.substring(cookiename.length,c.length);
		}
	
		return "";	
	}	