
/*
 * This function is carried over from previous design versions.
 * It is not clear whether or not it is still necessary
 */
function MM_openBrWindow(theURL,winName,features) {
	window.open(theURL,winName,features);
}

/**
 * Selects all the text within a textarea
 * 
 * used on referral banners page
 */
function highlight(id) {
	code = document.getElementById(id);
	code.focus();
	code.select();
}

/**
 * Checks and warns against popup blocker usage
 * 
 * used on Paid Email Read Page
 */
function check_popup_blocker(popup_url, popup_blocked_msg, new_location) 
{
	// IF NO POP UP DISPLAY MESSAGE
	if(!window.open(popup_url))
	{
		if(popup_blocked_msg)
			alert(popup_blocked_msg);
		return false;
	}
	
	// CHANGE CURRENT PAGE LOCATION TO MEMBER READ PAGE
	if(new_location)
		location.href=new_location;
	return true;
}

/**
 * This allows for more than one 'onload' function to be triggered on a page
 * 
 * Can be used in many places, mostly for offers
 *
 * Usage:
 * addLoadListener(function_name1);
 * addLoadListener(function_name2);
 */
function addLoadListener(fn) {
	if(typeof window.addEventListener != 'undefined') {
		window.addEventListener('load', fn, false);
	} else if (typeof document.addEventListener != 'undefined') {
		document.addEventListener('load', fn, false);
	} else if (typeof window.attachEvent != 'undefined') {
		window.attachEvent('onload', fn)
	} else {
		var oldfn = window.onload;
		if(typeof window.onload != 'function') {
			window.onload = fn;
		} else {
			window.onload = function() {
				oldfn();
				fn();
			};
		}
	}
}


/**
 * This will listen for an event on a particular element
 *
 * Used in conjunction with addLoadListener, and you can setup numerous functions to run after the page has loaded.
 *
 * usage: 
 * theTarget = getElementById('theTarget');
 * attachEventListener(theTarget, 'click', function_name, false);
 */
function attachEventListener(target, eventType, functionRef, capture) {
	if(typeof target.addEventListener != 'undefined') {
		target.addEventListener(eventType, functionRef, capture);
	} else if (typeof target.attachEvent != "undefined") {
		target.attachEvent("on" + eventType, functionRef);
	} else {
		eventtype = "on" + eventType;
		
		if (typeof target[eventType] == "function") {
			var oldListener = target[eventType];
			
			target[eventType] = function() {
				oldListener();
				return functionRef();
			};
		} else {
			target[eventType] = functionRef;
		}
	}
}

/**
 * Similar to getElementsById - should be obvious what it does
 */
function getElementsByAttribute(attribute, attributeValue) {
	var elementArray = new Array();
	var matchedArray = new Array();
	
	if (document.all) {
		elementArray = document.all;
	} else {
		elementArray = document.getElementsByTagName("*");
	}
	
	for (var i = 0; i < elementArray.length; i++) {
		if (attribute == "class") {
			var pattern = new RegExp("(^| )" + attributeValue + "( |$)");
			if(pattern.test(elementArray[i].className)) {
				matchedArray[matchedArray.length] = elementArray[i];
			}
		} else if (attribute == "for") {
			if (elementArray[i].getAttribute("htmlFor") || elementArray[i].getAttribute("for")) {
				if(elemtnArray[i].htmlFor == attributeValue) {
					matchedArray[matchedArray.length] = elementArray[i];
				}
			}
		} else if (elementArray[i].getAttribute(attribute) == attributeValue) {
			matchedArray[matchedArray.length] = elementArray[i];
		}
	}
	return matchedArray;
}


// Total dummy function to test javascript usage in the offers page. If you see this in here after December 31st 2008, please feel free to delete it.
function _dummy(pageName) {
	document.write("<a href=\"/pages/"+pageName+"\"><img src=\"/graphics/advertisers/offers/cashoffer.png\"></a>");
	return false;
}
