function pbsprivate_takesurvey(surveyurl) {
    if(surveyurl)
      window.open(surveyurl, "surveyPopUp",
                       'toolbar=0,location=0,scrollbars=1,menubar=0,' +
                       'resizable=1,width=800,height=600');

    if(window.location.search.indexOf("?return=") == 0) {
      window.location = 'http://' + window.location.hostname + window.location.search.substr(8);

    } else if(window.history.length > 1) {
      // If the history is correct, this should return the user
      // to the page they meant to visit in the first place.
      window.history.back();

    } else if(document.referrer.indexOf("http://www.pbs.org/") == 0) {
      // For some reason, this History is too short, so use
      // the referrer instead.
      window.location = document.referrer;

    } else {
      // Can't figure out where the user came from, so just follow
      // the hard-coded URL on the invite page.
      return true;
    }

    return false;
}

// Pass -1 as the frequency to force opening survey.
function pbsprivate_open_survey(inviteurl, cookiename, frequency) {
    var take = (frequency == -1);

    if(!take) {
      // Check to see if they've already taken the survey.
      var surveyflag = pbs_get_cookieval(cookiename);
      if(surveyflag != "1") {
        var myrand = Math.random() * 500;
        pbs_set_cookieval(cookiename, "1");
        if(myrand <= frequency)
          take = true;
      }
    }

    if(take)
      window.location = inviteurl;

    return false;
}
