// path to images
wppns_img_path = "/Morris2006/images/phone_nums/";

// image types, by image id
wppns_img_types = new Array();
wppns_img_types["a"] = "jpg";

// natural phone numbers, if any (source of natural traffic -> image id)
// set to null if none
wppns_natural = null;

// text phone numbers, by phone number id
wppns_txt_nums = ["1-866-636-1648", "1-866-762-0250", "1-866-659-3696", "1-866-764-5763"];

// reqular expression to match text numbers (set to null if none)
//wppns_pn_txt_reg_exp = new RegExp("\(973\) 927-3009 or Toll Free 1-888-SEAMLESS&reg;", "g");
wppns_pn_txt_reg_exp = new RegExp("\\(973\\) 927-3009 or Toll Free 1-888-SEAMLESS®", "g");
//wppns_pn_txt_reg_exp = null;

function wppns()
{
	var i, loc, q, pn_id, referrer;
	
	pn_id = null;
	loc = String(window.location);
	i = loc.indexOf("?");
	
	// check url for phone number id
	if (i != -1)
	{
		q = parse_query(loc.substr(i + 1));
		pn_id = q["phnum"];
	}
	// check referrer (natural clicks)
	else if (wppns_natural)
	{
		referrer = document.referrer;
		for (i in wppns_natural)
		{
			reg_exp = new RegExp(i, "gi");
			if (referrer.match(reg_exp))
			{
				pn_id = wppns_natural[i];
				break;
			}
		}
	}
	
	// set cookie if found pn_id
	if (pn_id) set_cookie("wppns", pn_id, 0);
	
	// otherwise check for previously set cookie
	else pn_id = get_cookie("wppns");
	
	// if we have a pn_id, look for phone numbers on page to swap
	if (pn_id) look_for_pns(document, pn_id, 0);
}

function look_for_pns(base, pn_id, count)
{
	var i, elem, pn_id, img_path, text, old_number, new_number;
	
	for (i = 0; i < base.childNodes.length; ++i)
	{
		elem = base.childNodes[i];
		
		// replace text numbers
		if (elem.nodeName == "#text" && wppns_pn_txt_reg_exp)
		{
			//if (elem.nodeValue.length && elem.nodeValue.length > 10) alert(elem.nodeValue);
			elem.nodeValue = elem.nodeValue.replace(wppns_pn_txt_reg_exp, wppns_txt_nums[pn_id - 1]);
		}
		
		// look for images in the html nodes
		else if (elem.getAttribute)
		{
			// see if current element is an image phone number that needs replacing
			elem_id = elem.getAttribute("pn");
			if (elem_id)
			{
				img_path = wppns_img_path+pn_id+elem_id+"."+wppns_img_types[elem_id];
				if (elem.getAttribute("is_style_bg"))
				{
					elem.style.backgroundImage = "url("+img_path+")";
				}
				else if (elem.getAttribute("is_back"))
				{
					elem.setAttribute("background", img_path);
				}
				else if (elem.tagName == "IMG")
				{
					elem.src = img_path;
				}
			}
			
			if (elem.childNodes.length > 0 && count < 64)
				look_for_pns(elem, pn_id, count + 1);
		}
	}
}

function set_cookie(k, v, s)
{
	var d;
	
	if (s == 0)
	{
		document.cookie = k+"="+v+"; path=/";
	}
	else
	{
		d = new Date();
		d = new Date(d.getTime() + (s * 1000));
		document.cookie = k+"="+v+"; expires="+d.toUTCString()+"; path=/";
	}
}

function get_cookie(key)
{
	var cookie_str, i1, i2;
	
	cookie_str = document.cookie;
	i1 = cookie_str.indexOf(key + '=');
	if (i1 == -1) return false;
	if (i1 > 0 && cookie_str.charAt(i1 - 1) != " ") return false;
	i2 = cookie_str.indexOf(';', i1);
	if (i2 == -1) i2 = cookie_str.length;
	
	return cookie_str.substring(i1 + key.length + 1, i2);
}


function parse_query(q)
{
	var kv_pairs, kv_pair, i, r;
	
	r = new Array();
	kv_pairs = q.split("&");
	for (i = 0; i < kv_pairs.length; ++i)
	{
		kv_pair = kv_pairs[i].split("=");
		r[kv_pair[0]] = kv_pair[1];
	}
	return r;
}

wppns();
