var tfnTurbo = function() {};

(function() {
	var formattedNew = '';
	window.newTel = false;
	var docRef = document.referrer;
	
	var getQueryStringVal=function(val) {
		var lc = location.search.substring(1);
		var qs = lc.split("&");
		for (var i=0;i<qs.length;i++) {
			var cur = qs[i].split("=");
			if (cur[0] == val) {
				return cur[1];
			}
		}
		return false;	
	};
	var phoneUrlSubs = {
	'tfn=v1':{'tel':'8002561486','cookie':true},
	'tfn=v2':{'tel':'8002561492','cookie':true}
	};
	var phoneReferrerSubs = {};
	
	//Strip whitespace, just in case someone separate with comma-space instead of just a comma
	/*apcString = apcString.replace(/\s/, "");
	allowedPhoneChange = apcString.split(',');
	rpcString = rpcString.replace(/\s/, "");
	referrerPhoneChange = rpcString.split(',');*/
	
	
	
	var qsVal = getQueryStringVal('tfn');
	if(qsVal && phoneUrlSubs['tfn='+qsVal]) {
		newTel = phoneUrlSubs['tfn='+qsVal].tel;
		if(phoneUrlSubs['tfn='+qsVal].cookie == true) {
			CookieStuff.setCookie("vz_mb_tfn", newTel, 1);
		}
	}
	
	// First, check the URL-encoded variables for matches
	/*for(var a=0; a<allowedPhoneChange.length; a++) {
		qs = allowedPhoneChange[a];
		keyVal = qs.split("=");
		var qsVal = getQueryStringVal(keyVal[0]);
		if(keyVal[1] == qsVal) {
			newTel = phoneUrlSubs[qs].tel;
			if(phoneUrlSubs[qs].cookie == true) {
				CookieStuff.setCookie("vz_mb_tfn", newTel, 1);
			}
			break;
		}
	}*/
	
	// If that fails, check to see if SMB is the referrer
	if(!newTel) {
		var cookieData = CookieStuff.getCookie("vz_mb_tfn");
		if(cookieData != "" && cookieData != "undefined") {
			newTel = cookieData;
		}
	}
	tfnTurbo = function() {
		var tels = $$('.tel');
		if (!tels || tels.length == 0) {
			tels = [];	
		}
		var toplink = $('ghlinkcont');
		if (toplink) {
			tels.push(toplink.getElement('td+td').getElement('a'));		
		}
		
		
		if (newTel) {
			
			tels.each(function(p) {
				var pNum = p.get('text');
				if(pNum.match(/^[1]-[0-9]{3}-[0-9]{3}-[0-9]{4}$/)) {
					var format = "1-###-###-####";
				} else if(pNum.match(/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/)) {
					var format = "###-###-####";
				} else if(pNum.match(/^\([0-9]{3}\)\s[0-9]{3}-[0-9]{4}$/)) {
					var format = "(###) ###-####";
				} else if(pNum.match(/^\([0-9]{3}\)[0-9]{3}-[0-9]{4}$/)) {
					var format = "(###)###-####";
				} else {
					var format = "1-###-###-####";
				}
				
				var formattedNew = "";
				var c2 = 0;
				// Build a new string based on the format
				for(var c=0; c<format.length; c++) {
					// If there's a #, place the next digit in orderTeleNumber here
					if(format.charAt(c) == "#") {
						formattedNew += newTel.charAt(c2);
						c2++;
					// If there's not a #, place the next digit of format here (it's a paren or a dash)
					} else {
						formattedNew += format.charAt(c);	
					}
				}
				p.set('text',formattedNew);
				p.setStyle('visibility','visible');	
			});
		} else {
			tels.each(function(p) {
				p.setStyle('visibility','visible');
			});
		}
	};
})();