function getCookie(NameOfCookie)
{
  if (document.cookie.length > 0) 
  { 
    begin = document.cookie.indexOf(NameOfCookie+"="); 
    if (begin != -1)
    { 
    begin += NameOfCookie.length+1; 
      end = document.cookie.indexOf(";", begin);
      if (end == -1) end = document.cookie.length;
      return unescape(document.cookie.substring(begin, end));       
    } 
  }
  return null;  
}

function findAllLinks(){
	var exclude=["domainA.com","domainB.com","doPostBack"] // This is the array you modify

	var excludeRegExp=new RegExp(exclude.join("|"),"i") // This generates a regular expression based on the excluded domains.
	var links=document.getElementsByTagName('a')  // Get all the links
	for(var link=0;link<links.length;link++){
		if(!excludeRegExp.test(links[link])){ // If none of the domains in the RegExp exists in the URL, modify the link.
			links[link].href=setTracking(links[link].href);  // Prepend the clicktracker.
		}
	}
}

function setTracking(url)
{
	var vars = [], hash;
	vars = getUrlVarsFromString(url);
	vars = updateURLVar(vars, "utm_source");
	vars = updateURLVar(vars, "utm_medium");
	vars = updateURLVar(vars, "utm_content");
	vars = updateURLVar(vars, "utm_term");
	vars = updateURLVar(vars, "utm_campaign");
	var parsedURL = parseUri(url);
	var updatedURL = parsedURL.protocol + '://' 
	if (parsedURL.userInfo)
	{
		updatedURL += parsedURL.userInfo + "@";
	}
	updatedURL += parsedURL.host + parsedURL.path + "?";
	for(i in vars)
	{
		updatedURL += i + "=" + vars[i] + "&";
	}
	return updatedURL;
}

function updateURLVar(vars, urlvar)
{
	if(getCookie("otTracking" + urlvar) != null)
	{
		vars[urlvar] = getCookie("otTracking" + urlvar);
	}
	return vars;
}

function getUrlVarsFromString(url){    
	var vars = [], hash;    
	var hashes = url.slice(url.indexOf('?') + 1).split('&');     
	
	for(var i = 0; i < hashes.length; i++)    
	{
		hash = hashes[i].split('=');        
		//vars.push(hash[0]);        
		vars[hash[0]] = hash[1];    
	}
	return vars;
}


// parseUri 1.2.2
// (c) Steven Levithan <stevenlevithan.com>
// MIT License

function parseUri (str) {
	var	o   = parseUri.options,
		m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
		uri = {},
		i   = 14;

	while (i--) uri[o.key[i]] = m[i] || "";

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});

	return uri;
};

parseUri.options = {
	strictMode: false,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q:   {
		name:   "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};



