// Stand: 16.8.04
// Version 2

function readReferrer() {
  var expMinutes = 60; // number of minutes the cookie should last
  var expDate = new Date();
  expDate.setTime(expDate.getTime() +  (60 * 1000 * expMinutes));

  // check for the need to write the cookie
  // check that there IS a referrer to write to cookie
  // and that the cookie has NOT already been written before
  if(document.referrer!="" && document.cookie.indexOf("referrer=")==-1){
    SetCookie('referrer', document.referrer, expDate, "/");
  }
}

function branchedRedirect(redirectUrl) {
  // try to read a cookie
  visited = GetCookie('visited');
  if(!visited) {
    // if the cookie 'visited' is not set, we have
    // not been on this page within last 15 seconds.
    // assuming that the user is here the 1st time
    // we send him to new destination.
    var expDate = new Date();
    var expSeconds = 15;
    expDate.setTime(expDate.getTime() +  ( 1000 * expSeconds));
    SetCookie('visited','true', expDate, "/");
    window.location.href = redirectUrl;
  } else {
    // if it is set we have been here within the
    // the last 15 seconds. user has probably
    // hit the backbutton. we will send him back
    // to his referrer, since this is what he
    // most likely expects.
    var referrer = GetCookie('referrer');
    if((referrer != null) && (referrer.length >= 10)) {
      DeleteCookie('visited');
      window.location.href = referrer;
    } else {
      // We failed to read the cookie. Or the url in the cookie is too short to be valid.
      // So we can't redirect to referrer. Let's do the second best thing.
      // Return the user to the redirect destination.
      window.location.href = redirectUrl;
    }
  }
}
