// execute doOnLoad when the page loads
if (window.addEventListener) { // w3c event model
  window.addEventListener("load", doOnLoad, false);
} else if (window.attachEvent) { // ie event model
  window.attachEvent("onload", doOnLoad);
} 
// uncomment to support currently unsupported browsers
//else { //this will override any other onload event handlers!)
//  window.onload = doOnload;
//}

function doOnLoad() {
  var queryString;
  var cidSource;
  var currentCid;
  //var cookie;
  var hiddenCid;
  
  // The following will be sent from the form if there was no
  // cid set via the querystring or cookie
  // Might be useful for tracking or it can be set to ''
  var NO_CID = 'cid not found';
  
  queryString = new QueryString();
  //cookie = new Cookie();
  currentCid = queryString.get('cid');
  
  // querystring value overrides cookie
  // TODO:  is this the desired behavior?
  if (currentCid) {
    cidSource = 'querystring';
    expires = 0; // will expire at end of session
    path = '/';  // cookie is valid in the entire site
    //cookie.setCookie('cid', currentCid, expires, path);
  } else {
    // if cid from querystring is null, check the cookie
    //currentCid = cookie.getCookie('cid');
    //cidSource = (currentCid) ? 'cookie' : null;
  }
  
  // BEGIN:  example for DEMO purposes (should be removed!!!)
  // this can be removed for production (along w/the
  // associated markup in the html pages...)
  //   try {
  //     var cidField = document.getElementById('cid_field');
  //     var sourceField = document.getElementById('source_field');
  //     cidField.innerHTML = (currentCid.length > 0) ? currentCid : 'not found';
  //     sourceField.innerHTML = (typeof cidSource != 'undefined') ? cidSource : '';
  //   } catch(e) {
  //     alert(e);
  //   }
  // END:  example for DEMO purposes (should be removed!!!)
  
  // if we are on our form, set the value of the hidden field
  hiddenCid = document.getElementById('Campaign_ID');
  if (hiddenCid) {
    hiddenCid.value = (currentCid) ? currentCid : NO_CID;
  }
}
