var assoctrac_id = 5406;    //Assoctrac ID
var cookie_name  = 'CC_2.0'; //Assoctrac cookie name


// Add the following line between the <head></head> tags
// <script language="javascript" src = "at.js"></script>

// Add the following JavaScript between the <head></head> tags
// <script language="javascript">
//   var myvalue = at_cookie_value('a');
// </script>

// Add the following JavaScript in the PayPal button area.  This
// creates the 'custom' field, so if you have a custom field already
// remove it and use this one instead.
// <script language=JavaScript type=text/javascript>
//   document.write('<input type=hidden name=custom value=' + myvalue + '>');
// </script>


function at_cookie_value(param) {
  var query_string = get_cookie(cookie_name);


  if(query_string != null) {
    return decode_query_string(decode_query_string(query_string,
                               'QUERY_STRING'), param);
  } else {
    if(param == 'a') return assoctrac_id;
  }


  return null;
}


function get_cookie(cookie_name) {
  if (document.cookie.length > 0) {


    //find the start of the cookie value
    start = document.cookie.indexOf(cookie_name + '=');
    if (start != -1) {
      start += cookie_name.length + 1;


      //find the end of the cookie value
      end = document.cookie.indexOf(';', start);
      if (end == -1) end = document.cookie.length;


      return unescape(document.cookie.substring(start, end));
    }
  }


  return null;
}


function decode_query_string(query_string, name) {


  //figure out the delimiter between the environment variables
  var delimiter = ';';
  if(query_string.indexOf(';') == -1) delimiter = '&';


  //split the query string
  var environment = query_string.split(delimiter);
  var parameters  = new Array;


  for(var parameter in environment) {
    var segments = environment[parameter].split('=');
    parameters[unescape(segments[0])] = unescape(segments[1]);
  }


  return parameters[name];
}