function noPercent(x) {
	if (x) {
		x = unescape(x);
		return x.replace(/\+/g," ").replace(/^\s\s*/, '').replace(/\s\s*$/, '');
	} else {
		return x;
	}
}

function getCookie(name) {      
      var nameRegex = RegExp("(?:\\||\\.|^|; )" + name + "=([^|;]+)"),
            nameValue;
      nameValue = nameRegex.exec(noPercent(document.cookie));
      if (nameValue) {
            return nameValue[1];
      } else {
            return " ";
      }
}

function getQuery(name) { 
	var nameRegex = RegExp("(?:\\?|&)" + name + "=([^&]+)");
	nameValue = nameRegex.exec(noPercent(location.search));
	if(nameValue) {
		return nameValue[1];
	} else {
		return null;
	}
}

function setNumCookie() {
	var cookieDomainName = ".merchantcommerce.com";
	var cookieExpDate = new Date();
		cookieExpDate.setTime(cookieExpDate.getTime() + 15768000000);
		
	var myCamp = new Object();
		myCamp["branded"]="877.352.9102";
		myCamp["credit cards"]="877.352.9104";
		myCamp["credit cards - accept"]="877.352.9105";
		myCamp["credit cards - machine"]="877.352.9106";
		myCamp["credit cards - process"]="877.352.9107";
		myCamp["credit cards - processing"]="877.352.9108";
		myCamp["[mobile] credit cards - processing"]="877.352.9108";
		myCamp["credit cards - processor"]="877.352.9109";
		myCamp["credit cards - terminal"]="877.352.9110";
		myCamp["merchant account (only)"]="877.352.9113";
		myCamp["merchant account/services"]="877.352.9114";
		myCamp["[mobile] merchant account/services"]="877.352.9114";
		myCamp["payment gateway"]="877.352.9116";
	var myAdgr = new Object();
		myAdgr["merchant account \(phrase\)"]="877.376.6685";
		myAdgr["credit card processing \(only\) - exact"]="877.376.6771";
		myAdgr["merchant services \(only\) - exact"]="877.376.6790";
		myAdgr["credit card processing \(only\)"]="877.376.6791";
		myAdgr["credit card terminals - wireless"]="877.376.6795";
		myAdgr["merchant account \(broad\)"]="877.376.6797";
		myAdgr["merchant services \(only\)"]="877.376.7065";
		myAdgr["credit card terminals \(only\)"]="877.376.7066";
		myAdgr["merchant account \(exact\)"]="877.376.7069";
		myAdgr["credit card machines - small business"]="877.376.7071";
		myAdgr["credit card processing - online"]="877.376.7072";
		myAdgr["credit card machines \(only\)"]="877.376.7076";
		myAdgr["credit card processing - wireless"]="877.376.7079";
		myAdgr["credit card terminals - small business"]="877.376.7085";
		myAdgr["accept card credit"]="877.376.7086";
		myAdgr["credit card processing - companies"]="877.376.7091";
		myAdgr["credit card machines \(only\)"]="877.376.7761";
		myAdgr["credit card terminals \(only\) - exact"]="877.376.7769";
		myAdgr["merchant processing - credit card"]="877.376.7772";
		myAdgr["merchant processing \(only\)"]="877.376.7789";
		myAdgr["credit card processors - \(only\)"]="877.376.7790";
		myAdgr["credit card machines \(only\) - exact"]="877.376.7791";
		myAdgr["credit card machines - portable"]="877.376.7792";
		myAdgr["merchant account - providers"]="877.376.7821";
		myAdgr["merchant account - online"]="877.376.7851";	
	
	if (getQuery("utm_source")=="msn") 
	{
		document.cookie = "num=1.866.360.4454;expires=" + cookieExpDate.toGMTString() + ";path=/;" + cookieDomainName;
	} 
	else if (getQuery("ga_adgroup") && myAdgr[getQuery('ga_adgroup')]) 
	{
		document.cookie = "num=1." + myAdgr[getQuery('ga_adgroup')] + ";expires=" + cookieExpDate.toGMTString() + ";path=/;" + cookieDomainName;
	} 
	else if (getQuery("ga_campaign") && myCamp[getQuery('ga_campaign')]) 
	{
		document.cookie = "num=1." + myCamp[getQuery('ga_campaign')] + ";expires=" + cookieExpDate.toGMTString() + ";path=/;" + cookieDomainName;
	}
}

function whichNum() {
	setNumCookie();
	
	if(getCookie("num").length > 1) 
	{
		document.write(getCookie("num"));
	} 
	else 
	{
		document.write("1.888.320.8472");
	}
}
