/*
   name - name of the cookie
   value - value of the cookie
   [expires] - expiration date of the cookie
     (defaults to end of current session)
   [path] - path for which the cookie is valid
     (defaults to path of calling document)
   [domain] - domain for which the cookie is valid
     (defaults to domain of calling document)
   [secure] - Boolean value indicating if the cookie transmission requires
     a secure transmission
   * an argument defaults when it is assigned null as a placeholder
   * a null placeholder is not required for trailing omitted arguments
*/

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}


/*
  name - name of the desired cookie
  return string containing value of specified cookie or null
  if cookie does not exist
*/

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}


/*
   name - name of the cookie
   [path] - path of the cookie (must be same as path used to create cookie)
   [domain] - domain of the cookie (must be same as domain used to
     create cookie)
   path and domain default if assigned null or omitted if no explicit
     argument proceeds
*/

function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"

function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
} 

function PageQuery(q) {
  if(q.length > 1) this.q = q.substring(1, q.length);
  else this.q = null;
  this.keyValuePairs = new Array();
  if(q) {
    for(var i=0; i < this.q.split("&").length; i++) {
      this.keyValuePairs[i] = this.q.split("&")[i];
    }
  }
  this.getKeyValuePairs = function() { return this.keyValuePairs; }
    this.getValue = function(s) {
      for(var j=0; j < this.keyValuePairs.length; j++) {
	if(this.keyValuePairs[j].split("=")[0] == s)
	  return this.keyValuePairs[j].split("=")[1];
      }
      return false;
    }
    this.getParameters = function() {
      var a = new Array(this.getLength());
      for(var j=0; j < this.keyValuePairs.length; j++) {
	a[j] = this.keyValuePairs[j].split("=")[0];
      }
      return a;
    }
    this.getLength = function() { return this.keyValuePairs.length; }
    }

function queryString(key){
  var page = new PageQuery(window.location.search);
  return unescape(page.getValue(key));
}

function displayItem(key){
  if(queryString(key)=='false')
    {
      document.write("you didn't enter a ?name=value querystring item.");
    }else{
      document.write(queryString(key));
    }
}


var REFERRAL_PARAMETER = "id";
var REFERRAL_COOKIE = "referral";
var REFERRAL_CLASS = "referral";

function addReferralToCookie() {
  var name_url = queryString(REFERRAL_PARAMETER);
  var name_cookie = getCookie(REFERRAL_COOKIE);
  
  name_url = name_url!="false" ? name_url : null;
  name = name_url ? name_url : name_cookie;

  if (name && name != null && name != 'null'){
    setCookie(REFERRAL_PARAMETER, name);
  }
  
}

function getReferral() {
  addReferralToCookie();
  return getCookie(REFERRAL_PARAMETER);
}

function testLink () {
  a = document.getElementById("test");
  a.setAttribute("href", "test.html?name=" + "aaron");
}

/**
 * http://simon.incutio.com/archive/2004/05/26/addLoadEvent
 */

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function addReferralToURL(url, ref) {
  return url + ref;

}

function addReferralToLinks() {
  var ref = getReferral();
  if(document && ref) {
    var tags = document.getElementsByTagName('a');
    for (var i=0;i<tags.length;i++)
      if (tags[i].className.indexOf(REFERRAL_CLASS) != -1 &&
	  !tags[i].value) {
	tags[i].href = addReferralToURL(tags[i].href, ref);
      }
  }
}

function addReferralToTags(tag, attr) {
  var ref = getReferral();
  var url = null;
  if(document) {
    var meta_tags = document.getElementsByName(REFERRAL_CLASS);
    if (meta_tags && meta_tags.length >= 1) {
      url = meta_tags[0].content;
    }

  }
  if(document && url) {
    var tags = document.getElementsByTagName(tag);
    for (var i=0;i<tags.length;i++)
      if (tags[i].className.indexOf(REFERRAL_CLASS) != -1 &&
	  !tags[i].value) {
	/* var old_attr = tags[i].getAttribute(attr); */
	var new_url = ref ? addReferralToURL(url, ref) : url;
	tags[i].setAttribute(attr, new_url);
      }
  }
}

function addReferralToFrame() {
  addReferralToTags('frame','src');
}

function addReferralToIFrame() {
  addReferralToTags('iframe','src');
}

function changeNodesWithClass(tag, classname, changefun) {
  if(document) {
    var tags = document.getElementsByTagName(tag);
    if (!tags || tags.length < 1) {
      return null;
    }
    //var newtags = new Array();
    for (var i=0;i<tags.length;i++) {
      if (tags[i].className.indexOf(classname) != -1 ) {
	  //&& !tags[i].value
	changefun(tags[i]);
      }
    }
    return null;
  }
  else {
    return null;
  }
}


function addUsername() {
  var username = queryString('username');
  function userNameChangeNode(node) {
    if (username) {
      node.lastChild.nodeValue = username;
    }
  }

  if (username) {
    changeNodesWithClass('span','username', userNameChangeNode);
  }
}

function addProgram() {
  var program = queryString('program');
  function programChangeNode(node) {
    if (program) {
      node.lastChild.nodeValue = program;
    }
  }

  if (program) {
    changeNodesWithClass('span','program', programChangeNode);
  }
}

addLoadEvent(getReferral);
addLoadEvent(addReferralToLinks);
addLoadEvent(addReferralToFrame);
addLoadEvent(addReferralToIFrame);
addLoadEvent(addUsername);
addLoadEvent(addProgram);
