//** JavaScript for parsing URL to obtain Google DFP key value and replace it in the Google attributes JS

function gup(name) {
  var params = {};
  var parts = (window.location.search || '').split(/[&?]/);
  for (var i = 0; i < parts.length; ++i) {
	var eq = parts[i].indexOf('=');
	if (eq < 0) continue;
	params[decodeURIComponent(parts[i].substring(0, eq))]
		= decodeURIComponent(parts[i].substring(eq+1));
  }
  return Object.hasOwnProperty.call(params, name)
	  ? params[name] : null;
}

var keyValue1 = gup( 'pid1' );

var keyValue2 = gup( 'pid2' );

var keyValue3 = gup( 'aid1' );

var keyValue4 = gup( 'aid2' );

var keyValue5 = gup( 'mrk1' );

var keyValue6 = gup( 'prd' );

//** JavaScript for parsing URL, take all variables, append varaibles to a tags with name addVar

function updateHrefs(){

var fullQuerystring = window.location.search;  //Get the querystring from URL
var querystring = fullQuerystring.substr(1); //Chop off the ?

var links = document.getElementsByName("addVar");  //Grab all of the anchor elements where name equals addVar
for(var i=0,len=links.length;i<len;i++){  //loop through all of the anchors
	var currentLink = links[i];  //get the current anchor
	var href = currentLink.href;  //grab its link
	if(href.indexOf("javascript:")===0){  //make sure the link is not hardcoded to JavaScript [bad practice]
		continue;  //skip this element and continue on
	}
	var hrefSeperator = href.indexOf("?") > 0 ? "&" : "?";  //figure out if the link already has a querystring, pick right seperator to use
	currentLink.href = currentLink.href + hrefSeperator + querystring; //update value to the link's href
}

}

//Call the code on page load
if (window.addEventListener){
  window.addEventListener('load', updateHrefs, false); //W3C
} else if (window.attachEvent) {
  window.attachEvent('onload', updateHrefs);  //OLDER MSIE
}


