

// CONFIGURATION VARIABLES
// cookie name
var cName = "TBSsurvey20001023";
// cookie life (in days)
var cLife = 30;
// random() multiplier
var cMult = 10;
// chance cutoff value
var cChance = 12;        // originally 90 with mult of 100, roughly 1 in 10 chance
// survey question page
var sPage = "research/thankyouforwatching.html";
// appearance of survey page window
//var sConfig = "resizable=yes,scrollbars=yes";
var sConfig = "width=275,height=297,resizable=no,scrollbars=no,toolbar=no,status=no,menubar=0";
var eligible = false;

// 
// Randomly prompt for survey if no cookie
function nth_user_survey() {



  // basic chance of being asked or not
  if((Math.random()*cMult) < cChance) return;

  // been asked within cLife days, leave them alone
  if(getCookie(cName)) return;

  // setup cookie expiration date
  var expDate = new Date();
  // expires cLife days from today
  expDate.setDate(expDate.getDate() + cLife);
  // cookie expiration dates are GMT except...
  // day, month, 2 digit year are separated by "-" character
  var gmtStr = expDate.toGMTString();

  // IE uses GMT string, Netscape doesn't
  if(document.all != null) {
    setCookie(cName, window.location.href.substring(7), gmtStr);
  }
  else {
    var expStr1 = gmtStr.substring(0,7);
    var expStr2 = gmtStr.substring(8,11);
    var expStr3 = gmtStr.substring(14);
    var expStr = expStr1+"-"+expStr2+"-"+expStr3;

    setCookie(cName, window.location.href.substring(7), expStr);
  }
  eligible = true;
}

// open a window asking them about taking the survey
function prompt_survey() {

  if (eligible)
  	window.open(sPage, "research", sConfig);
}

// Sets cookie values. Expiration date is optional
function setCookie(name, value, expire) {
   document.cookie = name + "=" + escape(value) + ((expire == null) ? "" : ("; EXPIRES=" + expire));
}

// Get specified cookie
function getCookie(Name) {
  var search = Name + "=";
  if(document.cookie.length > 0) { // if there are any cookies
    var offset = document.cookie.indexOf(search)
    if(offset != -1) { // if cookie exists
      offset += search.length;
      // set index of beginning of value
      var end = document.cookie.indexOf(";", offset);
      // set index of end of cookie value
      if (end == -1) end = document.cookie.length;
      return unescape(document.cookie.substring(offset, end));
    }
  }
}


