
var cookie_name = "enter_ref";

//-----------------------------------------------------------------------------

function getRefCookie() {
  var prefix = cookie_name + "=";
  var cookieStartIndex = document.cookie.indexOf(prefix);
  if (cookieStartIndex == -1) return null;
  var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length);
  if (cookieEndIndex == -1) cookieEndIndex = document.cookie.length;
  return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

function setRefCookie() {
  if (getRefCookie() != null) return;
  var value = document.referrer;
  if (value == "") return;
  if (value.length > 100) value = value.substr(0, 100);
  var expires = new Date();
  expires.setYear(expires.getYear() + 1);
  var curCookie = cookie_name + "=" + escape(value) +
      "; expires=" + expires.toGMTString() +
      "; path=/";
 //alert(curCookie);     
//    ((domain) ? "; domain=" + domain : "");
  document.cookie = curCookie;
}

//-----------------------------------------------------------------------------

function redirectWithRef() {
  var url = window.location.href;
  if (url.indexOf('?') < 0) {
    var ref = getRefCookie();
    if (ref != null) window.location.href = url + "#ref=" + escape(ref);
    //if (ref == null) return;
    //var new_url = url + "?ref=" + escape(ref);
    //document.writeln('<META HTTP-EQUIV="refresh" CONTENT="0;URL=' + new_url + '" >');
  }
}

//-----------------------------------------------------------------------------

setRefCookie();


