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 = ".commercepaymentsystems.com";
	var cookieExpDate = new Date();
		cookieExpDate.setTime(cookieExpDate.getTime() + 15768000000);
	
	var myCamp = new Object();
		myCamp["\[roi\] branded"]="888.548.1785";
		myCamp["\[roi\] credit cards"]="888.548.1786";
		myCamp["\[roi\] credit cards - accept"]="888.548.1789";
		myCamp["\[roi\] credit cards - machine"]="888.548.1790";
		myCamp["\[roi\] credit cards - process"]="888.548.1793";
		myCamp["\[roi\] credit cards - processing"]="888.548.1794";
		myCamp["\[roi\] \[mobile\] credit cards - processing"]="888.548.1794";
		myCamp["\[roi\] credit cards - processor"]="888.548.1795";
		myCamp["\[roi\] credit cards - terminal"]="888.548.1796";
		myCamp["\[roi\] merchant account (only)"]="888.548.1797";
		myCamp["\[roi\] merchant account/services"]="888.548.1798";
		myCamp["\[roi\] \[mobile\] merchant account/services"]="888.548.1798";
		myCamp["\[roi\] payment gateway"]="888.495.5196";
		myCamp["\[roi\] display network \(image ads\)"]="888.534.0793";		myCamp["\[roi\] remarketing \(image ads\)"]="888.534.0794";		myCamp["\[roi\] remarketing 2 \(image ads\)"]="888.534.0795";
	var myAdgr = new Object();
		myAdgr["credit card processing \(only\)"]="877.204.9873";
		myAdgr["credit card processing \(only\) - exact"]="877.327.3024";
		myAdgr["accept credit cards \(only\)"]="877.376.0577";
		myAdgr["merchant services \(only\) - exact"]="877.376.0578";
		myAdgr["merchant services \(only\)"]="877.376.0579";
		myAdgr["credit card machines \(only\)"]="877.376.0581";
		myAdgr["merchant account \(phrase\)"]="877.376.0586";
		myAdgr["merchant account \(exact\)"]="877.376.0587";
		myAdgr["credit card processing - company"]="877.376.0588";
		myAdgr["credit card processing - small business"]="877.376.0592";
		myAdgr["credit card machines - wireless"]="877.376.0593";
		myAdgr["credit card machines \(only\) -exact"]="877.376.4605";
		myAdgr["merchant processing - credit card"]="877.376.4607";
		myAdgr["merchant account - credit card"]="877.376.4608";
		myAdgr["credit card terminals \(only\)"]="877.376.4613";
		myAdgr["merchant processing \(only\)"]="877.376.4629";
		myAdgr["accept credit cards - online"]="877.376.4634";
		myAdgr["merchant services - credit card"]="877.376.4635";
		myAdgr["credit card terminals \(only\) - exact"]="877.376.4639";
		myAdgr["credit card processing - online"]="877.376.4641";
		myAdgr["credit card processors - \(only\)"]="877.376.4650";
		myAdgr["credit card processing - costco"]="877.376.6617";
		myAdgr["credit card processors - \(only\)"]="877.376.6641";
		myAdgr["merchant account - online"]="877.376.6670";
		myAdgr["credit card machines - small business"]="877.376.6681";
		
	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;
	}
	else if (getQuery("utm_source")=="msn") 
	{
		document.cookie = "num=1.888.534.0792;expires=" + cookieExpDate.toGMTString() + ";path=/;" + cookieDomainName;
	}
	else if (getQuery("utm_source")=="yahoo") 
	{
		document.cookie = "num=1.877.352.9097;expires=" + cookieExpDate.toGMTString() + ";path=/;" + cookieDomainName;
	}
}

function whichNum() {
	setNumCookie();
	
	if(getCookie("num").length > 1) 
	{
		document.write(getCookie("num"));
	} 
	else 
	{
		document.write("1.866.999.4622");
	}
}
