wpct_img_path = "/wpro/";

// image phone number info
wpct_info = {
	phone:
	{
		text:
		{
			default_number:"1-888-735-0040",
			tracking_numbers:["888-252-2987", "888-332-5651", "888-260-3380"]
		}
	}
};

// set the phone number id. check url first, then check for any previously set cookies
function ct_set_id()
{
	var i, loc, q, ct_id, referrer;
	
	ct_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));
		ct_id = q["ct"];
		
		// make sure ct_id is a valid number
		if (ct_id && (isNaN(ct_id) || ct_id.toString().indexOf(".") != -1 || ct_id < 1)) ct_id = null;
	}
	
	// set cookie if found ct_id
	if (ct_id) set_cookie("wpct", ct_id, 2678400);
	
	// otherwise check for previously set cookie
	else ct_id = get_cookie("wpct");
	
	wpct_id = ct_id;
}

function check_wp(the_form)
{
	if (wpct_id) the_form.is_wp.value = 1;
}

function ct_phone_image(number_id)
{
	var img, img_attributes, i, attr;
	
	img = wpct_info[number_id].img;
	
	if (wpct_id)
		img_attributes = ' src="'+wpct_img_path + number_id + wpct_id +'.'+img.extension+'"';
	else
		img_attributes = ' src="'+img.default_src+'"';
	
	for (i in img.other_attrs)
	{
		// ie doesn't like objects with properites named "class", so we call them class_name and put class back in here
		attr = (i == "class_name") ? "class" : i;
		img_attributes += " "+attr+"=\""+img.other_attrs[i]+"\"";
	}
	document.write("<img"+img_attributes+">");
}

function ct_phone_text(number_id)
{
	var num_info, ml_before, ml_after;
	
	// get the info for this number id
	num_info = (wpct_info[number_id]) ? wpct_info[number_id].text : null;
	
	// set before and after ml
	ml_before = (num_info && num_info.ml_before) ? num_info.ml_before : "";
	ml_after  = (num_info && num_info.ml_after)  ? num_info.ml_after  : "";
	
	// if we have an id for this user, show the tracking number
	if (wpct_id && wpct_id <= num_info.tracking_numbers.length) {
		document.write(ml_before + wpct_info[number_id].text.tracking_numbers[wpct_id - 1] + ml_after);
	} else { 	// show default number
		document.write(ml_before + wpct_info[number_id].text.default_number + ml_after);
	}
}

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 null;
	if (i1 > 0 && cookie_str.charAt(i1 - 1) != " ") return null;
	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]] = unescape(kv_pair[1]);
	}
	return r;
}


function add_onload_func(new_func)
{
	var cur_func;
	
	cur_func = window.onload;
	window.onload = function() {
		if (cur_func) cur_func();
		new_func();
	}
}


// the phone number id
wpct_id = null;

ct_set_id();
