var dirty = false;

// Used to retrieve the currently selected radio button,
// and updates the background colors to match the selected item.
function getSelectedRadioButton() {
	//var form = document.forms("ItemOrdering");
	var form = document.forms[0];
	var formElements = form.elements;
	var returnRadio = null;
	var tdElement;
	
	for (var i = 0; i < formElements.length; i++) {
		if (formElements[i].type == "radio") {
			tdElement = findNearestParentOfType(formElements[i], "TD");
			if (formElements[i].checked) {
				returnRadio = formElements[i];
				tdElement.style.backgroundColor = "#CCCCDD";
			} else {
				tdElement.style.backgroundColor = "#FFFFFF";
			}						
		}
	}
	return returnRadio;
}

// Moves the selected button's table row up or down.
function moveRow(bUpDown) {
    dirty = "true";
	var radioButton = getSelectedRadioButton();
	
	// traverse up the parent chain until we reach the nearest
	// parent TR element, if there is one
	var pTableRow = findNearestParentOfType(radioButton, "TR");
	
	if (pTableRow != null) {
		var nRowIndex = pTableRow.rowIndex;
	
		var pTable = findNearestParentOfType(pTableRow, "TABLE");
		
		if (pTable != null) {
			if (bUpDown) {
				pTable.rows[nRowIndex].swapNode(pTable.rows[nRowIndex - 1]);
			} else {
				pTable.rows[nRowIndex].swapNode(pTable.rows[nRowIndex + 1]);
			}
		}
	}
	
	// Reset the selection and enable/disable buttons as necessary.
	radioButton.checked = true;
	toggleButtons();			
}

function toggleButtons() {
	var radioButton = getSelectedRadioButton();
	
	// Climb up the hierarchy to find the enclosing <tr>
	var pParentElement = radioButton;
	
	// traverse up the parent chain until we reach the nearest
	// parent TR element, if there is one
	var pTableRow = findNearestParentOfType(radioButton, "TR");
	
	if (pTableRow != null) {
		var nRowIndex = pTableRow.rowIndex;
		
		// If its the first row, disable "Up".
		if (nRowIndex == 0) {
			document.all("UpButton").disabled = true;
		} else {
			document.all("UpButton").disabled = false;
		}
		
		var pTable = findNearestParentOfType(pTableRow, "TABLE");
		
		// If its the last row, disable "Down".
		if (pTable != null) {			
			var numRows = pTable.rows.length;
			
			if (nRowIndex == numRows - 1) {
				document.all("DownButton").disabled = true;
			} else {
				document.all("DownButton").disabled = false;
			}
		}
	}
}

function findNearestParentOfType(element, strType) {
	while ( (element != null) &&
			(element.tagName != strType) )
	{
		element = element.parentElement;
	}
	
	// if we found a table row, return it.
	if ( (element != null) && 
		(element.tagName == strType) )
	{
		return element;
	} else {
		return null;
	}
}

function setOrderAndSubmit() {
	// Find the table and list the order.
	var table = document.all("tblItems");
	
	for (var i = 0; i < table.rows.length; i++) {
		// Retrieve the corresponding hidden element for the row.
		//var thisHiddenElement = document.forms("ItemOrdering").item(String(i));
		var thisHiddenElement = document.forms[0].item(String(i));
		
		// Set the value of the hidden element.
		thisHiddenElement.value = table.rows[i].id;
	}
	
	// Now submit the form.
	//document.forms("ItemOrdering").submit();
	document.forms[0].submit();
}

function setReferenceMaterialOrderAndSubmit() {
	// Find the table and list the order.
	var table = document.getElementById("GlobalWithSubNav_PageContent_ucAdmin_SortReferenceMaterial_tblItems");
	var referenceMaterialIDs;
	referenceMaterialIDs = "";
	
	for (var i = 0; i < table.rows.length; i++) {
		var currentSortOrdinal = i + 1
		var currentRowID = table.rows[i].id;
		currentRowID = currentRowID.replace("GlobalWithSubNav_PageContent_ucAdmin_SortReferenceMaterial_", "");
		if (referenceMaterialIDs.length == 0)
		{
		    referenceMaterialIDs = currentRowID + ":" + currentSortOrdinal;
		}
	    else
	    {
	        referenceMaterialIDs = referenceMaterialIDs + "|" + currentRowID + ":" + currentSortOrdinal;
	    }
	}

    var sortOrder = document.getElementById("GlobalWithSubNav_PageContent_ucAdmin_SortReferenceMaterial_txtReferenceMaterialsSortOrder");
    sortOrder.value = referenceMaterialIDs;
    document.getElementById("GlobalWithSubNav_PageContent_ucAdmin_SortReferenceMaterial_btnSaveReferenceMaterialSortOrder").click();
}

function retrieveLiteratureFiles(control)
{
    if (dirty)
    {
        var answer = confirm("Are you sure you want to continue?  All changes will be lost")
        if (answer == false)
        {
            if (control.toUpperCase() == "LITERATURECATEGORY")
            {
                //if user changed category but did not want to lose changes, then set catgory back to previous selection
                var currentCategoryID = document.getElementById("GlobalWithSubNav_PageContent_ucAdmin_SortReferenceMaterial_txtCurrentCategoryID").value;
                var categories = document.getElementById("GlobalWithSubNav_PageContent_ucAdmin_SortReferenceMaterial_ddlLiteratureCategory");
                for (var i=0; i < categories.options.length; i++)
                {
                    if (categories.options[i].value == currentCategoryID)
                    {
                        categories.options[i].selected = true;
                    }
                }
            }
            return false;
        }
    }

    if (control.toUpperCase() == "LOADLITERATUREFILES")
    {
        __doPostBack("GlobalWithSubNav_PageContent_ucAdmin_SortReferenceMaterial_btnLoadLiteratureFiles", "");                      
    }
    else if (control.toUpperCase() == "LITERATURECATEGORY")
    {
        __doPostBack("GlobalWithSubNav_PageContent_ucAdmin_SortReferenceMaterial_ddlLiteratureCategory", "");
    }
}

function onCancel() {
	window.close();
}

