/**
 * bt_c2c_popup
 *
 * This function displays the BT click to call popup
 *
 * @return void
 */
function bt_c2c_popup(e) {
	if (e.stopPropagation) {
		e.stopPropagation();
	}
	if (typeof(e.cancelBubble) != "undefined") {
		e.cancelBubble = true;
	}
	if (e.preventDefault) {
		e.preventDefault();
	}
	if (window.event) {
		window.event.cancelBubble = true;
	}
	if (e.target && e.target.tagName == 'A') {
		var href = e.target.href;
	} else if (e.currentTarget && e.currentTarget.tagName == 'A') {
		var href = e.currentTarget.href;
	} else if (e.rangeParent && e.rangeParent.tagName == 'A') {
		var href = e.rangeParent.href;
	} else if (e.originalTarget && e.originalTarget.tagName == 'A') {
		var href = e.originalTarget.href;
	} else if (e.srcElement) {
		var el = e.srcElement;
		while(el && el.tagName != 'A') {
			el = el.parentNode;
		}
		var href = el.href;
	}
	if (href) {
		var popup_window = window.open(href, "bt_c2c_popup_window", "location=0, status=1, scrollbars=0, width=430, height=410");
		return false;
	}
	return true;
}

/**
 * get_c2c_links
 *
 * This function gets all the click to call links
 *
 * @return Array The click to call links
 */
function get_c2c_links(c2c_class, c2c_tag) {
	var links = new Array();
	if (c2c_tag) {
		var divs = document.body.getElementsByTagName(c2c_tag);
		for (var i = 0; i < divs.length; i++) {
			if (divs[i].className == c2c_class) {
				links.push(divs[i].getElementsByTagName('a')[0]);
			}
		}
	} else {
		var as = document.body.getElementsByTagName('a');
	
		for (var i = 0; i < as.length; i++) {
			if (as[i].className == c2c_class) {
				links.push(as[i]);
			}
		}
	}
	return links;
}

/**
 * set_c2c_events
 *
 * This function sets all the click to call links to have an event
 *
 * @return Array The click to call links
 */
function set_c2c_events(c2c_class, c2c_tag) {
	var links = get_c2c_links(c2c_class, c2c_tag);
	for (var i = 0; i < links.length; i++) {
		if (links[i].addEventListener) {
			links[i].addEventListener('click', bt_c2c_popup, true);
		} else if (links[i].attachEvent) {
			links[i].attachEvent('onclick', bt_c2c_popup);
		}
	}
}

window.onload = function() {
	set_c2c_events('click_to_call');	
};
