// ********    INSTRUCTIONS BELOW    *********


// at the bottom is a variable called cookieName - change this as appropriate
// under function loadornot() is a call to function generateRandomValue(70) - 70 is the nth value - change as needed
// surveyURL under function openURL() refers to the popup window that is used as the invitation
// also under function openURL(), you will see "var heightWin = 300" and "var widthWin = 350".  These control the height and width of the popup window (not the survey window which is controled by the link on the popup window).
// there are 2 methods to running this code.
//    1. add <script language = javascript> to the very top of this code and </script> to the very bottom.  Then add this entire code (with script tags) inside the <HEAD> </HEAD> tags of the desired page.
//    2. upload this code WITHOUT script tags to your server.  Add <script language=javascript src=LOCATION OF .JS FILE ON YOUR SERVER></script> inside the <HEAD> </HEAD> tags of the desired page.




function getCookie (name) {
var dcookie = document.cookie; 
var cname = name + "=";
var clen = dcookie.length;
var cbegin = 0;
        while (cbegin < clen) {
        var vbegin = cbegin + cname.length;
                if (dcookie.substring(cbegin, vbegin) == cname) { 
                var vend = dcookie.indexOf (";", vbegin);
                        if (vend == -1) vend = clen;
                return unescape(dcookie.substring(vbegin, vend));
                }
        cbegin = dcookie.indexOf(" ", cbegin) + 1;
                if (cbegin == 0) break;
        }
return null;
}

// Returns a random number between 1 and n
function generateRandomValue(n){

    var random = Math.floor(Math.random() * n) + 1;
    return random;
}

//set a cookie
function setCookie (name, value, expires) {
  if (!expires) expires = new Date();
  document.cookie = name + "=" + escape (value) + "; expires=" + expires.toGMTString() +  "; path=/"; 
}


// run the intercept or not

function loadornot()
{
  if(getCookie(cookieName) == null)
  {
  i=generateRandomValue(70);
  //i=1; alert(i);alert(cookieName);
    if(i == 1)
    {
      var expdate = new Date ();
      expdate.setTime (expdate.getTime() + (1000 * 60 * 60 * 24 * 90));  // 90 day expiration
      setCookie(cookieName, "yes", expdate);
      openURL();
    }

  }
}

var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,total,thestring,name;

function checkIt(string)
{
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

// Opens a new window containing the survey

function openURL(){

var surveyURL="https://survey.openvenue.com/cs/07844/int14.html";


// POP-UP SURVEY Center of Screen Variables
    
    var heightWin = 400
    var widthWin = 350

    var winl = (screen.width - widthWin) / 2;
    var wint = (screen.height - heightWin) / 2;

    // Test Browser type and set new window properties

    if (checkIt('msie')) {
       name = "Microsoft Internet Explorer";
    }

    if (name == "Microsoft Internet Explorer") {
    	winprops = 'height='+heightWin+',width='+widthWin+',top='+wint+',left='+winl+',taskbar=0,sidebar=0,';
        winprops += 'channelmode=0,dependent=0,directories=0,fullscreen=0,location=0,menubar=0,resizable=1,';
        winprops += 'scrollbars=1,status=1,toolbar=0,personalbar=0,hotkeys=0';
    } else {
	winprops = 'height='+heightWin+',width='+widthWin+',top='+wint+',left='+winl+',screenX='+winl+',screenY='+wint+',scrollbars,status,resizable=1';
    }

   // Open the new window based on platform

   if(navigator.userAgent.indexOf('Mac') != -1) {
	var macwindowname = 'Mac';
	var winprops = 'scrollbars,status';
	//window.open(surveyURLU,macwindowname,winprops);
	window.open(surveyURL,macwindowname,winprops);
    } else {
	//window.open(surveyURLU,"Survey2",winprops);
	window.open(surveyURL,"Survey",winprops);
    }
  //this.focus();
}

var cookieName="_MRWEB";

loadornot();
