//document.write('<b>(Testing ENV only - from click-survey.js rkrause@asq.org)</b> ' +  document.cookie);


var nowDate = new Date();  
nowDate.setMonth(nowDate.getMonth() + 6);  
dontExpire = nowDate.toGMTString();

/*function setCookie(cookieName, cookieValue, cookieExpires, cookiePath, cookieDomain)  
{ 
	cookieValue = escape(cookieValue);
	document.cookie = cookieName + "=" + cookieValue + "; expires=" + cookieExpires + "; path=" + cookiePath + "; domain=" + cookieDomain;
}*/

function getCookie(name)
{ 
	var cookieString = document.cookie;  
	var index = cookieString.indexOf(name + "=");  

	if (index == -1) 
		{ return null; } 

	index = cookieString.indexOf("=", index) + 1;  
	var endstr = cookieString.indexOf(";", index);  

	if (endstr == -1) 
		{ endstr = cookieString.length; } 

	return unescape(cookieString.substring(index, endstr)); 
}


//This is the funtion for both the ASQ and QP survey


function checkSurveyOLD()
{
    var pagesviewed = getCookie('ASQClickSurvey01');
	alert("JRA" + pagesviewed);
    if (pagesviewed != 'taken') // if the user has seen the popup do not show it again.
    {

        parseInt(pagesviewed);
        if (pagesviewed >= 5)
        {
            window.open("/includes/surveys/click-survey-intro.html","survey","menubar=no,width=500,height=300,toolbar=no");
            document.cookie = 'ASQClickSurvey01=taken;expires=' + dontExpire + ';path=/;domain=asq.org';
        }

        else
        {
            parseInt(pagesviewed);
            pagesviewed++;
            document.cookie = 'ASQClickSurvey01=' + pagesviewed + ';path=/;domain=asq.org';
        }
    }
}


function checkSurvey() { 

	var pagesviewed = getCookie('ASQClickSurvey01'); 
	if (pagesviewed == null) pagesviewed = ''; 

	//alert("JR1a pageviewed: " + pagesviewed  ); 

	/* alert("pagesviewed = " + pagesviewed +
		  "; location.href = " + location.href +
		  "; indexOf / = " + location.href.indexOf("/")); */

	// if the user has seen the popup do not show it again.  
	if (pagesviewed.indexOf('taken') == -1 ) { 
		//alert("JR2"); 
	
		parseInt(pagesviewed);
		//if (pagesviewed > 5) {
		if (pagesviewed >= 5) {

			
			var firstHit = getCookie('firstHit');
			if (firstHit == null) firstHit = ''; 
			if (firstHit.indexOf("/qualityprogress/") > -1 || firstHit.indexOf("/quality-progress/") > -1) firstHit = 'QP' ;
			else (firstHit = 'ASQ'); 

			//alert(firstHit + "  " + firstHit.indexOf('/qualityprogress') ) ;
			//alert(firstHit + "  " + firstHit.indexOf('/government') ) ;
			//alert("firstHit");

			// entering through QP triggers QP survey
			if ( firstHit == 'QP' ) { 
				document.cookie = 'ASQClickSurvey01=takenQP;expires=' + dontExpire + ';path=/;domain=asq.org';
				window.open("/includes/surveys/click-QPsurvey-intro.html","survey","menubar=no,width=500,height=280,toolbar=no");
			}
			// entering through ASQ or KC triggers ASQ survey
			else {
				document.cookie = 'ASQClickSurvey01=takenASQ;expires=' + dontExpire + ';path=/;domain=asq.org';
				window.open("/includes/surveys/click-survey-intro.html","survey","menubar=no,width=500,height=300,toolbar=no");
			}
		}

		
		else {
			//alert("JR3");
			parseInt(pagesviewed); 
			pagesviewed++;
			document.cookie = 'ASQClickSurvey01=' + pagesviewed + ';path=/;domain=asq.org';
			//alert("JR4 cookie: " + document.cookie);
		}

	}
}





// ######## ORIGINAL SURVEY ######## This function is for our normal pop-up survey.  Comment this out if a special survey needs to be used. This cookie needs to stay the same.
/*function checkSurvey() 
{ 
	var pagesviewed = getCookie('ASQClickSurvey01'); 
	if (pagesviewed != 'taken') // if the user has seen the popup do not show it again.
	{ 
	
		parseInt(pagesviewed);
		if (pagesviewed >= 5) 
		{ 
			window.open("/includes/surveys/click-survey-intro.html","survey","menubar=no,width=500,height=300,toolbar=no");
			document.cookie = 'ASQClickSurvey01=taken;expires=' + dontExpire + ';path=/;domain=asq.org';
		}
		
		else
		{
			parseInt(pagesviewed); 
			pagesviewed++;
			document.cookie = 'ASQClickSurvey01=' + pagesviewed + ';path=/;domain=asq.org';
		}
	}		
}*/




//This is the funtion that can be used for speical surveys.  It's the same as the upper function but it can have the link and cookie changed.
/*function checkSurvey() 
{ 
	var pagesviewed = getCookie('ASQClickSpecial02'); //Update my +1 for new survey.
	if (pagesviewed != 'taken') // if the user has seen the popup do not show it again.
	{ 
	
		parseInt(pagesviewed);
		
		if (pagesviewed >= 3) 
		{ 
			window.open("/includes/surveys/click-survey-intro.html","survey","menubar=no,width=500,height=300,toolbar=no");
			document.cookie = 'ASQClickCertSurvey=taken;expires=' + dontExpire + ';path=/;domain=asq.org';
		}
		
		else
		{
			parseInt(pagesviewed); 
			pagesviewed++;
			document.cookie = 'ASQClickCertSurvey=' + pagesviewed + ';path=/;domain=asq.org';
		}
	}		
}*/

//This is the funtion that can be used for speical surveys.  It's the same as the upper function but it can have the link and cookie changed. But contains an extra directory logic equation.
/*function checkSurvey() 
{ 
	var pagesviewed = getCookie('ASQUS02'); //Increment by one for new survey
	if (pagesviewed != 'taken') // if the user has seen the popup do not show it again.
	{ 
	
		parseInt(pagesviewed);
		
		if (pagesviewed > 5
			|| location.href.indexOf("/audit/") > -1
			|| location.href.indexOf("/auto/") > -1
			) 
		{ 
			window.open("/includes/surveys/click-survey-intro.html","survey","menubar=no,width=500,height=300,toolbar=no");
			document.cookie = 'ASQUS02=taken;expires=' + dontExpire + ';path=/;domain=asq.org';
		}
		
		else
		{
			parseInt(pagesviewed); 
			pagesviewed++;
			document.cookie = 'ASQUS02=' + pagesviewed + ';path=/;domain=asq.org';
		}
	}		
}*/
