//July 28, 2009 mvaughan. Modified to support bing search engine and fix other bugs.
//Mar 26, 2009 mvaughan. Modified to work with cookies disabled, store/retrieve all values, and to return query string from cookie for url
//Apr 3, 2009 mvaughan. Added ppc_event
function getTrackingCode(){

  // first get values from stored cookie (if any)
  var IT = getCookie("IT");
  var ppc_geo = getCookie("ppc_geo");
  var ppc_land = getCookie("ppc_land");
  var ppc_kywd = getCookie("ppc_kywd");
  var ppc_lid  = getCookie("ppc_lid");
  var ppc_event  = getCookie("ppc_event");
  var referral_engine = getCookie("referral_engine");
  var referral_kywd = getCookie("referral_kywd");
  var ep = getCookie("ep");
  var first_click = getCookie("first_click");
  var furl = location.href;
  if (!is_empty(furl)) furl = furl.replace("http://", "");
  if (!is_empty(furl)) furl = furl.replace("https://", "");
  
  var cookieTime = new Date();

  var today = cookieTime.getDate() + "/" + (cookieTime.getMonth() + 1) + "/" + cookieTime.getFullYear();
  
  // cookie expires in 10 years (3650 days)
  cookieTime.setTime(cookieTime.getTime() + 3650 * 24 * 60 * 60 * 1000);

  // check for a query string and overwite the values if there are new ones
  if(location.search){
    var ary  = window.location.search.substr(1).split("&");
    var qStr = new Array();

    for(var i = 0; i < ary.length; i++) ary[i] = ary[i].split("=");
    for(var i = 0; i < ary.length; i++){
      ary[i][0] = ary[i][0].replace(/\+/g," ");
      ary[i][0] = unescape(ary[i][0]);
      ary[i][1] = ary[i][1].replace(/\+/g," ");
      ary[i][1] = unescape(ary[i][1]);
      qStr[ary[i][0]] = ary[i][1];
    }

    IT = qStr["IT"];
    ppc_geo = qStr["geo"];
    ppc_land = qStr["land"];
    ppc_kywd = qStr["kywd"];
    ppc_lid = qStr["lid"];
    ppc_event = qStr["event"];
    referral_engine = qStr["referral_engine"];
    referral_kywd = qStr["referral_kywd"];

    //Feb 26, 2009 Neo Wang. http://tracking.academyart.edu/browse/SEOMARKETING-22
    //use transfered first_click if current click is empty
        var first_click_transfered = qStr["first_click"];
    if (!is_empty(first_click_transfered) && is_empty(first_click)){
            first_click = first_click_transfered;
        }
    //non-null IT means PPC lead - even though IT may get value from cookie, null IT from PPC will set it to null.
    //don't set first_click cookie if it has value so we can keep the 'first' click without overwriting it
    if (!is_empty(IT)){
        
        if (is_empty(first_click)){
            first_click = "fd=" + today + ",fc=p,flid=" + ppc_lid;
        }
    }
    
    var ep_transfered = qStr["ep"];
    if (is_empty(ep_transfered)){
        ep = location.href;
    } else {
        ep = ep_transfered;
    }
  }

  if (document.referrer){
    ref = document.referrer;
    var start = ref.indexOf(":") + 3;
    var end = ref.indexOf("/",start);
    var prevPage= ref.substring(start,end); 
    if (prevPage.indexOf("art.edu") < 0 && prevPage.indexOf("academyartonline.com") < 0) {
      referral_engine = getReferralEngine(prevPage);
      var qsep = ref.indexOf("?") + 1;
      referral_kywd = getReferralKeywords(ref.substr(qsep), referral_engine);
      
      if (!is_empty(referral_engine)){
          if (is_empty(first_click)){//SEO
              first_click = "fd=" + today + ",fc=s,fr=" + referral_engine + ",fskywd=" + referral_kywd + ",furl=" + furl;
              ep = location.href;
          } else {
              if (first_click.indexOf("fc=p") >= 0 && first_click.indexOf("fr=") < 0){//PPC and not from other sites such as AON or art.edu
                  first_click = first_click + ",fr=" + referral_engine + ",fskywd=" + referral_kywd + ",furl=" + furl;
              }
          }
      }
      
    }
  }

  //Aug 5, 2010 Neo Wang. SEOMARKETING-147
  ep = document.referrer;

  // write the latest values back to the cookie
  setCookie("IT", IT, cookieTime);
  setCookie("ppc_geo", ppc_geo, cookieTime);
  setCookie("ppc_land", ppc_land, cookieTime);
  setCookie("ppc_kywd", ppc_kywd, cookieTime);
  setCookie("ppc_lid", ppc_lid, cookieTime);
  setCookie("ppc_event", ppc_event, cookieTime);
  setCookie("referral_engine", referral_engine, cookieTime);
  setCookie("referral_kywd", referral_kywd, cookieTime);
  if (!is_empty(ep)) ep = ep.replace("http://", "");
  if (!is_empty(ep)) ep = ep.replace("https://", "");
  setCookie("ep", ep, cookieTime);
  
  if (!is_empty(first_click)){
      setCookie("first_click", first_click, cookieTime);
  }
  
  return("?IT=" + IT + "&geo=" + ppc_geo + "&land=" + ppc_land + "&kywd=" + ppc_kywd + "&lid=" + ppc_lid + "&event=" + ppc_event + "&referral_engine=" + referral_engine + "&referral_kywd="+ referral_kywd + "&ep=" + escape(ep) + "&first_click=" + escape(first_click));
}

function gotoRequestInfo(){
  newURL = 'http://www.academyart.edu/inquiry/index.jsp' + getTrackingCode();
  window.open(newURL,"Applications","toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width=1000,height=730,left=0,top=0,xscreen=0,yscreen=0");
}

function gotoApply(){
  newURL = 'https://apply.academyart.edu/apply/login.htm' + getTrackingCode();
  window.open(newURL,"Applications","toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width=800,height=600,left=0,top=0,xscreen=0,yscreen=0");
}

function gotoArt(){
  newURL = 'http://media.academyart.edu/presentations/promoVideo/promoVideo.html';
  window.open(newURL,"tdaPopup","resizable,scrollbars=no,width=350,height=355");
}

function getQuickLink(URL){
  var selIndex = URL.selectedIndex;
  var selValue = URL.options[selIndex].value;

  if((selValue.substring(0,4) == "http" )){
   window.open(selValue,'_blank');
  }
  else window.open(selValue,'_parent');
}

function getReferralEngine (referrer) {
  var referrers = [
    ['baidu.','baidu'],
    ['bing.','bing'],
    ['google.','google'],
    ['yahoo.','yahoo'],
    ['aol.','aol']
  ];
  for (var i=0; i < referrers.length; i++) {
    if (referrer.indexOf(referrers[i][0]) > -1){
      return referrers[i][1];
    }
  }
  return referrer; 
}

function getQuerySeparator (engine) {
  var separators = [
    ['baidu','wd'],
    ['bing','q'],
    ['google','q'],
    ['yahoo','p'],
    ['aol','q']
  ]
   for (var i=0; i < separators.length; i++) {
    if (engine == separators[i][0]) {
      return separators[i][1];
    }
  }
  return "";
}

function getReferralKeywords(referrer, engine) {
    var term;
    var separator = getQuerySeparator(engine);
    var terms = referrer.split("&");
    for (var i = 0; i < terms.length; i++) {
      term = terms[i].split("=");
      if (term[0] == separator) {
        return term[1];
      }
    }
    return null;
}

function is_empty(str){
    return str == undefined || str == null || str == "" || str.search(/^null$/i)==0;
}