/*
popup.js
Created: 12-13-2002
Modified: 09-15-2003

Copyright (c) 2003, NetReflector.com, Inc. All Rights Reserved. 

NetReflector grants its customers permission to modify this code for their own use and at their own risk. NetReflector prohibits reproduction or distribution to others in any manner without written permission from NetReflector.

THE CODE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES THAT THE PROGRAM IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE CODE IS WITH YOU. SHOULD ANY PART OF THE CODE PROVE DEFECTIVE IN ANY RESPECT, YOU ASSUME THE COST OF ANY NECESSARY SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. NO USE OF THE CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER THIS DISCLAIMER.

*/
var EveryNth = 10; //For each EveryNth visitors popup will be shown
var SurveyName = 'ValsparSurveyTest'; //var with survey name  *** NOTE: Do NOT include spaces.  Cookie will not be found since they will be converted to %20 -- (cnapoli)
//URL with popup html file
//var SurveyURL = 'https://is-nri.com/take?i=126047&h=P5Nd1dHy1jplzGLEhuoOEA'; 
//var SurveyURL = 'http://is-nri.com/take?i=127127&h=MQcL5qLiD3SKHE6X1YbpiQ'; 
var SurveyURL = 'http://www.surveymonkey.com/s.aspx?sm=JUP9%2fkJ779Cy6pZ%2f7Ns8hQ%3d%3d';
var expiresDays = 30; //Time period (number of days) when cookie will expire
var isAOL;
var testingURL = 'associationsurveys.com;gmitestsite.com;is4.instantsurvey.com;is-nri.com;instantsurvey.com;https://www.instantsurvey.com'; // checking URLs delimited by ";" must be at the same site 
if (navigator.appVersion.lastIndexOf('AOL')!=-1) isAOL = true;

function openSurvey(SurveyName,SurveyPath)
{                                                                
	if (window.screenX) var x=',screenX='+window.screenX;
	else var x = ',left='+ window.screenLeft;

	if (window.screenY) var y=',screenY='+window.screenY;
	else var y = ',top=0'+ (window.screenTop-30);

	var expirationdate = new Date();
	expirationdate.setTime(expirationdate.getTime() + (expiresDays * 24 * 60 * 60 * 1000));

	var isNav = false;

	if (navigator.appName == "Netscape" && parseInt(navigator.appVersion) < 5) isNav = true;
	if (navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion.indexOf('MSIE 4')>0) isNav = true;

	var JScriptCode, JScriptCodeIE,JScriptCodeN;
	JScriptCode = 'var time = 2;';
	JScriptCode += 'var testingURL = "'+ testingURL +'";';
	JScriptCode += 'var sourceURL;';
	JScriptCode += 'var real = time * 1000;';
	JScriptCode += 'var i;';
	JScriptCode += 'var a = 0;';
	
	JScriptCode += 'function loaded()'
	JScriptCode += '{if (top.opener) {';
	JScriptCode += 'top.opener.focus();'
	JScriptCode += 'self.onerror = handleError;';
	JScriptCode += 'top.resizeTo(1000,600);';
	JScriptCode += 'checkURL()}}';
	JScriptCode += 'function handleError(msg){;';
	JScriptCode += 'if ((msg == "Permission denied") || (msg.indexOf("access") >= 0)){'
	JScriptCode += 'self.focus();return true;}};';



	JScriptCodeN = 'function checkURL() {var found=false;';
	JScriptCodeN += 'sourceURL = top.opener.location;';
	JScriptCodeN += 'sourceURL = sourceURL.toString();';
	JScriptCodeN += 'testingURLs = testingURL.split(\';\');';
	JScriptCodeN += 'for (var t=0; t<testingURLs.length;t++) {';
	JScriptCodeN += 'i = sourceURL.indexOf(testingURLs[t]);if (i!=-1){found=true;break;}}';
	JScriptCodeN += 'if (!found) {top.focus();}';
	//JScriptCodeN += 'setTimeout("checkURL()",real);';
	JScriptCodeN += '}loaded();';
	
	JScriptCodeIE = 'function checkURL() {var found=false;';
	JScriptCodeIE += 'try { sourceURL = top.opener.location.href;';
	JScriptCodeIE += 'testingURLs = testingURL.split(\';\');';
	JScriptCodeIE += 'for (var t=0; t<testingURLs.length;t++) {';
	JScriptCodeIE += 'i = sourceURL.indexOf(testingURLs[t]);if (i!=-1){found=true;break;}}';
	JScriptCodeIE += 'if (!found) {top.focus();}';
	//JScriptCodeIE += 'setTimeout("checkURL()",real);';
	JScriptCodeIE += '} catch(errorObject) {';
	JScriptCodeIE += 'self.focus();}};loaded();';
	

	popupHTML = '<frameset rows="0,*"><frame name="is4frame1" src="about:blank" marginwidth="10" marginheight="10" frameborder="0"></frame><frame name="" src="'+SurveyURL+'" marginwidth="10" marginheight="10" scrolling="auto" frameborder="0"></frameset>';

	defwin = window.open('', 'Survey', 'Toolbar=no,location=yes,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes'+y+x+',width=1,height=1,z-lock=yes');
	defwin.document.write('<html><head><title>Survey</title>');
	defwin.document.write('<'+'script language="JavaScript">'+JScriptCode+'</'+'script>');
	if (isNav){ defwin.document.write('<'+'script language="JavaScript">'+JScriptCodeN+'</'+'script>');}
	else { defwin.document.write('<'+'script language="JavaScript">'+JScriptCodeIE+'</'+'script>');}

	defwin.document.write(popupHTML);
	defwin.document.write('</body></html>')
	defwin.document.close();

	SetCookie('Survey', SurveyName, expirationdate);
	return null;
}

function callSurvey(SurveyName,SurveyPath)
{
	if (document.cookie.indexOf('Survey='+SurveyName) != -1) return;
	if (!everyNth(EveryNth) || isAOL) return;
	
	// only open survey if cookies are enabled
	if(checkCookies("both")) {
		openSurvey(SurveyName,SurveyPath);
	}
}

function Random(N)
	{
		return Math.round(N*Math.random());
	}


function everyNth(nth)
	{
		if (nth==0) return false;

		if ( Random(nth+2)> nth) {
			return true; 
		}
		else {
			return false;
	}
}

function SetCookie(sName, sValue, exp_date)
{
	document.cookie = escape(sName) + "=" + escape(sValue) + ";path=/;expires=" + exp_date.toGMTString();

}
// Retrieve the value of the cookie with the specified name.


function GetCookie(sCookie)
{
	// Real cookie name is encoded
	var sCookieHash = escape(sCookie);
	// cookies are separated by semicolons
	var aCookie = document.cookie.split(";");
	for (var i=0; i < aCookie.length; i++)
	{
		// a name/value pair (a crumb) is separated by an equal sign
		var aCrumb = aCookie[i].split("=");
		if (sCookieHash == aCrumb[0] || ( aCrumb[0].charAt(0)==" " && sCookieHash == aCrumb[0].substr(1)))
		if(aCrumb.length==1)
			return null;
		else
			return unescape(aCrumb[1]);
	}
	// a cookie with the requested name does not exist
	return null;
}

function checkCookies(cookieType) {
	var cookieEnabled = {
		temporary  : "",
		persistent : "",
		both : ""
	}; // cookieEnabled

	// set dummy cookies (they'll get deleted, don't worry!)
	document.cookie="CookieTestTemp=success";
	document.cookie="CookieTestPers=success; expires=Tue, 25-Dec-2012 12:34:56 GMT";

	// check temporary cookies
	if(document.cookie.indexOf( "CookieTestTemp=success") != -1) {
  		cookieEnabled.temporary = true;
  		document.cookie="CookieTestTemp=JavaScript; expires=Thu, 25-Dec-1980 12:34:56 GMT";
	}
	else {
  		cookieEnabled.temporary = false;
	}

	// check persistent cookies
	if (document.cookie.indexOf( "CookieTestPers=success" ) != -1) {
  		cookieEnabled.persistent = true;
  		document.cookie="CookieTestPers=JavaScript; expires=Thu, 25-Dec-1980 12:34:56 GMT";
	}
	else {
  		cookieEnabled.persistent = false;
	}

	// check if both are enabled
	if(cookieEnabled.temporary && cookieEnabled.persistent) {
		cookieEnabled.both = true;
	}
	else {
		cookieEnabled.both = false;
	}

	if(cookieType) {
		return cookieEnabled[cookieType];
	}
	else {
		return cookieEnabled;
	}

} // checkCookies

callSurvey(SurveyName, SurveyURL);

