// v1.7.3.7.a
// ***************************************************************************
// common.js
// Contains the JS functions used by more than one JS file
// ***************************************************************************

// ***************************************************************************
// FUNCTION: void setNavigationState(element navigation, boolean disabled)
// DESCRIPTION: enables and disables a navigation.
// ***************************************************************************
function setNavigationState(navigation, disabled)
{
	if (disabled) {
		setClass(navigation, __CSSNavDisabled);
	}else {
		setClass(navigation, __CSSNavEnabled);
	}
	navigation.disabled = disabled;
}
// ***************************************************************************

// ***************************************************************************
// FUNCTION: element getMyIU(element me)
// DESCRIPTION: returns the tag of my IU.
// ***************************************************************************
function getMyIU(me)
{
	var IUTag = me;
	while (IUTag.id != null && IUTag.id.indexOf(__IUIdPart) == -1)
	{
		IUTag = IUTag.parentNode;
		if (IUTag == null)
		{
			return null;
		}
	}
	return IUTag;
}
// ***************************************************************************

// ***************************************************************************
// FUNCTION: element[] getIUTags()
// DESCRIPTION: returns all the tags that are IU in the document.
// ***************************************************************************
function getIUTags()
{
	var IUTagArray = new Array();
	var divTags = document.getElementsByTagName(__divTAG);

	for (var i = 0; i < divTags.length; i++) {
		if (divTags[i].id != null && divTags[i].id.indexOf(__IUIdPart) != -1) {
			IUTagArray[IUTagArray.length] = divTags[i];
		}
	}
	return IUTagArray;
}
// ***************************************************************************

// ***************************************************************************
// FUNCTION: element searchElementById(string id)
// DESCRIPTION: Searches the page element with the id 'id'.
// ***************************************************************************
function searchElementById(id) {
	if (document.getElementById) {
		return document.getElementById(id);
	} else if (document.all) {
		return document.all[id];
	} else return null;
}
// ***************************************************************************

// ***************************************************************************
// FUNCTION: void setClass(element elmnt, string newClass)
// DESCRIPTION: Changes the class name to 'newClass' of the element 'elmnt'.
// ***************************************************************************
function setClass(elmnt, newClass) {
	if (elmnt == null) {
		return;
	}
	elmnt.className = newClass;
}
// ***************************************************************************

// ***************************************************************************
// FUNCTION: void disableNavigations(element IUTag)
// DESCRIPTION: Disables the navigations for the IU 'IUTag'.
// ***************************************************************************
function disableNavigations(IUTag) {
	var myNavigations = getMyFirstSubTag(IUTag, __divTAG, __NavigationIdPart)
	if(myNavigations == null) {
		return;
	}
	var navigationLinks = myNavigations.getElementsByTagName(__aTAG);

	for (i = 0; i < navigationLinks.length; i++) {
		setNavigationState(navigationLinks[i], true);
	}

	// Disable subNavigations
	var myNavArray = null;
	for (var i = 0; i < __navigationsArray.length; i++) {
		if (__navigationsArray[i][0] == IUTag.id) {
			myNavArray = __navigationsArray[i];
			break;
		}
	}

	if (myNavArray == null) {
		return;
	}

	for (i = 1; i < myNavArray.length; i++) {
		var SubIUTag = searchElementById(myNavArray[i]);
		disableNavigations(SubIUTag);
	}

}
// ***************************************************************************

// ***************************************************************************
// FUNCTION: element getMyFirstSubTag(element startTag, string tagName, string idName)
// DESCRIPTION: Searches the page tag 'tagname' with an id holding 'idName'
//              within 'startTag'.
// ***************************************************************************
function getMyFirstSubTag(startTag, tagName, idName) {
	var subTags = startTag.getElementsByTagName(tagName);

	for (var i = 0; i < subTags.length; i++) {
		if (subTags[i].id != null && subTags[i].id.indexOf(idName) != -1) {
			return subTags[i];
		}
	}
}
// ***************************************************************************

// ***************************************************************************
// FUNCTION: element getRow(element me)
// DESCRIPTION: Returns the row containing 'me', or null.
// ***************************************************************************
function getRow(me) {
	var trElement = me;
	while (trElement != null && trElement.tagName != __trTAG) {
		trElement = trElement.parentNode;
	}
	return trElement;
}
// ***************************************************************************

// ***************************************************************************
// FUNCTION: void setSelectClass(element row, boolean selected)
// DESCRIPTION: Sets the selection CSS clas for 'Row' according to 'selected'.
// ***************************************************************************
function setSelectClass(row, selected) {
	var rowPos = __OldClassArray.length;
	for (var i = 0; i < __OldClassArray.length; i++) {
		if (__OldClassArray[i][0] == row) {
			rowPos = i;
		}
	}
	if (selected) {
		if (row.className == __CSSRowSelected) {
			return;
		}
		__OldClassArray[rowPos] = [row, row.className];
		setClass(row, __CSSRowSelected);
	} else {
		// __OldClassArray[rowPos] contains the row for sure.
		if (rowPos == __OldClassArray.length) {
			return;
		}
		setClass(row, __OldClassArray[rowPos][1]);
	}
}
// ***************************************************************************

// ***************************************************************************
// FUNCTION: void computeNavigationsState(element NavigationDiv, boolean isInstance)
// DESCRIPTION: enables and disables navigations.
// ***************************************************************************
function computeNavigationsState(navigationDiv, isInstance) {
	if ((navigationDiv == null) || isInstance) {
		return;
	}
	// Navigations
	var navigations = navigationDiv.getElementsByTagName(__aTAG);
	var count = 0;
	var myIUTag = getMyIU(navigationDiv);

	var enableNavigations = false;
	enableNavigations = computeEnabledNavigationState(myIUTag);
	for (i = 0; i < navigations.length; i++) {
		setNavigationState(navigations[i], !enableNavigations);
	}
}
// ***************************************************************************

// ***************************************************************************
// FUNCTION: void addToIUArray(string IUName, string MasterIUName)
// DESCRIPTION: adds an interaction unit to the array of interaction units.
// ***************************************************************************
function addToIUArray(IUName, MasterIUName) {
	if (MasterIUName == null) {
		__navigationsArray[__navigationsArray.length] = [IUName];
		return;
	}
	for (var i = 0; i < __navigationsArray.length; i++) {
		if (__navigationsArray[i][0] == MasterIUName) {
			var pos = __navigationsArray[i].length;
			__navigationsArray[i][pos] = IUName;
			__navigationsArray[__navigationsArray.length] = [IUName];
			return;
		}
	}
}
// ***************************************************************************

// ***************************************************************************
// FUNCTION: void addToIIUArray(string IUName)
// DESCRIPTION: adds an instance interaction unit to the array of instance
//              interaction units.
// ***************************************************************************
function addToIIUArray(IUName) {
	__IIUArray[__IIUArray.length] = IUName;
}
// ***************************************************************************

// ***************************************************************************
// FUNCTION: void initPage()
// DESCRIPTION: Sets the initial state of the navigations and selects the
//              appropriate rows.
// ***************************************************************************
function initPage() {
	computeAllNavigations();

	// Change the class of the selected rows
	var IUTagsArray = getIUTags();
	for (var i = 0; i < IUTagsArray.length; i++) {
		if (isInstanceIU(IUTagsArray[i].id)) {
			continue;
		}
		var checkBoxesArray = getChildCheckBoxes(IUTagsArray[i]);
		for (var j = 0; j < checkBoxesArray.length; j++) {
			if (checkBoxesArray[j].checked) {
				var checkBoxRow = getRow(checkBoxesArray[j]);
				if (checkBoxRow == null || checkBoxRow.className == "headerDataGrid") {
					continue;
				}
				setSelectClass(checkBoxRow, true);
			}
		}
	}
}
// ***************************************************************************

// ***************************************************************************
// FUNCTION: void initPageAndRegisterRequest()
// DESCRIPTION: Sets the initial state of the navigations and selects the
// appropriate rows. It also registers the UpdatePanel request handler (the
// handler event will be rised when the UpdatePanel request is over).
// ***************************************************************************
function initPageAndRegisterRequest() {
	// Sets the initial state of the navigations and selects the appropriate rows.
	initPage();

	// Register asynchrounous AJAX UpdatePanel request evant handler.
	Sys.WebForms.PageRequestManager.getInstance().add_endRequest(updatePanelRequestHandler);
}
// ***************************************************************************

// ***************************************************************************
// FUNCTION: void updatePanelRequestHandler()
// DESCRIPTION: This UpdatePanel request handler will be rised when the
// UpdatePanel request is over.
// ***************************************************************************
function updatePanelRequestHandler(sender, args) {
	// Sets the initial state of the navigations and selects the appropriate rows.
	initPage();
}
// ***************************************************************************

// ***************************************************************************
// FUNCTION: void computeAllNavigations()
// DESCRIPTION: Sets the disabled state for all the navigations in the
//              document.
// ***************************************************************************
function computeAllNavigations()
{
	var IUTagArray = getIUTags();
	UpdateIUNavigations(IUTagArray[0]);
}
// ***************************************************************************

// ***************************************************************************
// FUNCTION: void UpdateIUNavigations(element IUTag)
// DESCRIPTION: updates the navigations state for the given IUTag. It also
//              disables the navigations of the
// ***************************************************************************
function UpdateIUNavigations(IUTag) {
	var myNavArray = null;
	var isInstance = isInstanceIU(IUTag.id);

	for (var i = 0; i < __navigationsArray.length; i++) {
		if (__navigationsArray[i][0] == IUTag.id) {
			myNavArray = __navigationsArray[i];
			break;
		}
	}

	var myNavigations = null;
	var NavTagArray = new Array();
	var divTags = IUTag.getElementsByTagName(__divTAG);

	for (var i = 0; i < divTags.length; i++) {
		if (divTags[i].id != null && divTags[i].id.indexOf(__NavigationIdPart) != -1) {
			myNavigations = divTags[i];
			break;
		}
	}

	computeNavigationsState(myNavigations, isInstance);
	for (i = 1; i < myNavArray.length; i++) {
		var SubIUTag = searchElementById(myNavArray[i]);
		UpdateIUNavigations(SubIUTag);
	}
}
// ***************************************************************************

// ***************************************************************************
// FUNCTION: boolean isInstanceIU(string IUTagId)
// DESCRIPTION: Says if an IU is an instance IU or not
// ***************************************************************************
function isInstanceIU(IUTagId) {
	var isInstance = false;
	for (var i = 0; i < __IIUArray.length && !isInstance; i++) {
		isInstance = __IIUArray[i] == IUTagId;
	}
	return isInstance;
}

