
function aff_cookie_check() {
  // get the current cookie (if it exists) and URL
  var allCookies = document.cookie;
  var winLoc = window.location.href

  // search the cookie for the affiliate id
  var pos = allCookies.indexOf("AFF_ID=");

  // create a RegExp to see if there is an aff_id in the URL
  var reAff = /aff(_id)?=(\d+)/i;

  /**
  * Always use aff_id from URL if it is passed
  * If there is no aff_id in URL and cookie alreay exists,
  * leave it as it is.
  * If there is no aff_id in the URL and there is no cookie,
  * set it to the default value.
  **/
  if (reAff.test(winLoc)) {
    // there is an aff id in the URL so use it
    var m = reAff.exec(winLoc);
    // m[0] contains the complete RegExp match
    // m[1] contains either '_id' or is undefined
    // m[2] contains the digits matched
    set_aff_cookie(m[2]);

  var pos = allCookies.indexOf("SUB_ID=");

    var sub_regexp = /sub(_id)?=(.+)/i;
    // is there an aff_id on the url set the cookie
    if (sub_regexp.test(winLoc)){
      var m = sub_regexp.exec(winLoc);
      set_sub_cookie( m[2]);
    }
  }

}

function set_aff_cookie(val) {
  var aff_cookie = "AFF_ID=" + val + ";path=/;domain=.bluesq.com;";
  document.cookie = aff_cookie;
}
function set_sub_cookie(val) {
  var sub_cookie = "SUB_ID=" + val + ";path=/;domain=.bluesq.com;";
  document.cookie = sub_cookie;
}




