﻿
/// <summary>
/// Returns a reference to the current dialog.
/// </summary>
function getRadWindow() {
	var oWindow = null;
	if (window.radWindow) oWindow = window.radWindow;
	else if (window.frameElement && window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
	return oWindow;
}

/// <summary>
/// Returns a reference to the current dialog.
/// </summary>
function GetRadWindow() {
	return getRadWindow();
}

/// <summary>
/// Closes the current dialog.
/// </summary>
function closeRadWindow() {
	var win = GetRadWindow();
	if (win != null) win.close();
	return false;
}

/// <summary>
/// Sets whether the hyperlink is enabled or not.
/// </summary>
/// <param name="link">The hyperlink element.</param>
/// <param name="enabled">Whether the link should be enabled or not.</param>
function enableLink(link, enabled) {

	// check for valid argument
	if (!link) return;

	// first store the original value
	if (!link.originalOnClick) {
		if (link.onclick) link.originalOnClick = link.onclick;
		else link.originalOnClick = function() { return true; };
	}

	// set the click event handler
	if (enabled) link.onclick = link.originalOnClick;
	else link.onclick = function() { return false; }

	// set the link as being disabled
	if (enabled) link.removeAttribute('disabled');
	else link.setAttribute('disabled', 'disabled');

}

/// <summary>
/// Returns the first parent element to have the specified tag name.
/// </summary>
/// <param name="item">The element whose parent to find.</param>
/// <param name="tagName">The type of parent element to find.</param>
function getParentElement(item, tagName) {
	while (item.tagName != tagName) item = item.parentNode;
	return item;
}

/// <summary>
/// Sets the checked status of an element.
/// </summary>
/// <param name="element">Any element in the same row as the check box you wish to find.</param>
/// <param name="checked">Whether the item should be checked or not.</param>
function setRowCheckBox(element, checked) {
	var row = getParentElement(element, 'TR');
	var chkBoxes = row.getElementsByTagName('input');
	chkBoxes[0].checked = checked;
	rowSelect(chkBoxes[0]);
}

/// <summary>
/// Sets the style of the table row to indicate whether it's been selected or not.
/// </summary>
/// <param name="chkBox">The check box that indicates whether the row has been selected.</param>
function rowSelect(chkBox) {
	try {

		// set the class name
		var row = getParentElement(chkBox, 'TR');
		var table = getParentElement(chkBox, 'TABLE');

		var oldClass = chkBox.getAttribute('previousClassName');
		chkBox.setAttribute('previousClassName', row.className);

		if (chkBox.checked) row.className = 'My-Account-SelectedFilterRow';
		else {
		    if (oldClass != null && oldClass != "")
		        row.className = oldClass;
		    else
		        row.className = "My-Account-Row";
		}

		// see whether to unselect the header check box
		if (!chkBox.checked && getSelectionCountFromTable(table.id) == 0) {
			var chkBoxes = table.getElementsByTagName('INPUT');
			if (chkBoxes[0].parentNode.tagName == 'TH') {
				chkBoxes[0].checked = false;
			}
		}

		// notify everyone of the event
		updateLinkStates();

	} catch (e) { ; }
}

/// <summary>
/// Sends notice to all registered listeners that a check box has changed state.
/// </summary>
function updateLinkStates() {
	try {

		// always call this method when defined even if not registered
		if (window.setLinkStates) setLinkStates();

		// check whether any others have been defined
		if (!window.linkStates) return;

		// notify each of them of the event
		for (i=0; i<window.linkStates.length; i++) {
			window.linkStates[i]();
		}

	} catch (e) {; }
}

/// <summary>
/// Registers a method to be called whenever the check boxes change state.
/// </summary>
/// <param name="method">The method to be called when a check box gets selected.</param>
function registerLinkStates(method) {
	try {

		// initialize the array
		if (!window.linkStates) {
			window.linkStates = new Array();
		}

		// check what the name of this method is
		var thisMethodName = getMethodName(method);

		// check whether already included in the array
		for (i=0; i<window.linkStates.length; i++) {
			if (getMethodName(window.linkStates[i]) == thisMethodName) {
				window.linkStates.splice(i, 1);
				break;
			}
		}

		// include this method in the array
		window.linkStates.push(method);

	} catch (e) { ; }
}

/// <summary>
/// Sets all the check boxes in the table to match the state of the specified check box.
/// </summay>
/// <param name="chkBox">An element that controls the selection state of the entire table.</param>
function tableSelect(chkBox) {
	try {

		// find all the check boxes
		var table = getParentElement(chkBox, 'TABLE');
		var chkBoxes = table.getElementsByTagName('input');

		// then select each one
		for (i = 0; i < chkBoxes.length; i++) {

			// see whether the header check box
			if (chkBoxes[i] == chkBox) continue;

			// see whether not a check box
			if (chkBoxes[i].type.toLowerCase() != 'checkbox') {
				continue;
			}

			// select the item and toggle the row style
			chkBoxes[i].checked = chkBox.checked;
			var row = getParentElement(chkBoxes[i], 'TR');
			if (chkBox.checked) row.className = 'My-Account-SelectedFilterRow';
			else row.className = "My-Account-Row";

		}

		// notify everyone of the event
		updateLinkStates();

	} catch (e) { ; }
}

/// <summary>
/// Returns the number of selected check boxes in the table.
/// </summary>
/// <param name="tableName">The table whose check boxes to count.</param>
function getSelectionCountFromTable(tableName) {

	// initialize
	var chkBoxes = null;
	var count = 0;

	// find all the check boxes
	var table = document.getElementById(tableName);
	if (table) chkBoxes = table.getElementsByTagName('input');
	if (!chkBoxes) return count;

	// count how many are selected
	for (i = 0; i < chkBoxes.length; i++) {
		if (chkBoxes[i].parentNode.tagName == 'TH') continue;
		if (chkBoxes[i].checked) count++;
	}

	// return value
	return count;

}

/// <summary>
/// Returns the name of the method if defined.
/// </summary>
/// <param name="method">The method whose name to return.</param>
function getMethodName(method) {
	var matches = method.toString().match(/^function\s(\w+)/);
	if (matches.length > 1) return matches[1];
	return 'anonymous';
}
