
/*
 * 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;
}

/*
This function sets all checkbox objects in the specified 'form' to the value of the specified 'chkBox'. It also calls
the highlight() function.
*/
function checkAll(form, chkBox) {
	// Loop through each element in the form
	for (var i = 0; i < form.elements.length; i++) {
		// Only take action for 'checkbox' form elements
		if (form.elements[i].type == "checkbox") {
			// Simulate the 'onclick' event of the checkbox
			// This should ONLY occur if the current 'checked' property is not the same as the 'checkall' checkbox
			if (form.elements[i].checked != chkBox.checked)
				form.elements[i].click();
		}
	}
}

/*
This function sets all checkbox objects in the specified 'form' to the value of the specified 'chkBox'. It also calls
the highlight() function.
*/
function check(form, chkBoxesToCheck)
{
	// Loop through each element in the form
	for (var i = 0; i < form.elements.length; i++)
	{
		// Only take action for 'checkbox' form elements
		if (form.elements[i].type == "checkbox")
		{
			// Simulate the 'onclick' event of the checkbox
			// This should ONLY occur if the current 'checked' property is not the same as the 'checkall' checkbox
			if (form.elements[i].name == "read[]" && !form.elements[i].checked && (chkBoxesToCheck == 0 || chkBoxesToCheck == 2))
			{
				form.elements[i].click();
			}
			else if (form.elements[i].name == "read[]" && form.elements[i].checked && (chkBoxesToCheck != 0 && chkBoxesToCheck != 2))
			{
				form.elements[i].click();
			}
			else if (form.elements[i].name == "unread[]" && !form.elements[i].checked && (chkBoxesToCheck == 0 || chkBoxesToCheck == 3))
			{
				form.elements[i].click();
			}
			else if (form.elements[i].name == "unread[]" && form.elements[i].checked && (chkBoxesToCheck != 0 && chkBoxesToCheck != 3))
			{
				form.elements[i].click();
			}
		}
	}
}

/*
This function changes the background color of the selected TR. The 'obj' argument is assumed to be a checkbox object
contained in a TD, which is contained in a TR. The conditional logic is necessary to ensure the original TR class
is preserved if the checkbox object is unchecked.
*/
function highlight(chkBox) {
	var i;
	// Get the TR object
	var objTR = chkBox.parentNode.parentNode;
	// Split the TR classes by space character and put into an array.
	var arrTRclasses = objTR.className.split(" ");
	// If the checkbox is checked, set the TR class name to the original name plus " highlight". If the checkbox
	// is not checked, set the TR class name to the original name.
	if (chkBox.checked)
	{
		objTR.className = "";
		
		for(i=0;i<arrTRclasses.length;i++)
		{
			objTR.className += arrTRclasses[i] + " ";
		}
		
		objTR.className = objTR.className + "highlight";
	}
	else
	{
		objTR.className = arrTRclasses[0];
		
		for(i=1;i<(arrTRclasses.length-1);i++)
		{
			objTR.className += " " + arrTRclasses[i];
		}
	}
}