var query = "";

//clickShift string
var csString = "";
getURL();

if (isCheckQuery()) {
	//if setClickshift actually set a value
	if (setClickshift()) {
		collectLinks();
	}
}

function getURL() {
	query = location.search;
}

function isCheckQuery() {
	if (query == "" || query == " " || query == null) {
		return false;
	}
	
	return true;
}

function setClickshift() {
	var regex = /(cshift_ck=.*)&/;
	var matches = regex.exec(query);
	
	if (matches == null) {
		return false;
	}
	else {
		//index 1 is the capture group
		csString = matches[1];
	}
	
	return true;
}

function collectLinks() {
		
	for(var index in document.links) {	
		//if the link is appendable then append the csString to it
		if (isAppendable(document.links[index].href)) {
			document.links[index].href += "&" + csString;
		}
	}
}

function isAppendable(strHref) {
	var nonAppend = ["mailto", "[.]at/", "[.]ca/", "[.]fr/", "[.]de/", "[.]it/", "[.]jp/", "[.]es/", "[.]uk/"];
	var regex = "";
	
	for (var index in nonAppend) {
		regex = new RegExp(nonAppend[index]);
		
		var matches = regex.exec(strHref);
		if (matches != null) {
			return false;
		}
	}
	
	return true;	
}

