/* Copyright (c) Microsoft Corporation. All rights reserved. */

function window_onbeforeunload() {
	if (!isSubmitting)
	{
		L_BeforeUnload_TXT = "If you navigate away from this page, you will lose all your changes.";
		event.returnValue = L_BeforeUnload_TXT;
	}
}

function ChangeCaret(SpanName)
{
	document.all(SpanName).style.cursor = "hand";
	document.all(SpanName).style.color = "#ffa500";
}

function NormalCaret(color, SpanName)
{
	document.all(SpanName).style.color = color;
}

function SubmitForm(Action)
{
	var oForm = document.forms(0);
	oForm.action = Action;
	oForm.submit();
}

function SubmitFormNoSel(Action)
{
	document.all("UserID").value = -1;
	var oForm = document.forms(0);
	oForm.action = Action;
	oForm.submit();
}

function SaveSelect(ListName, HiddenField)
{
	var selectedIndex = document.all(ListName).selectedIndex;
	var selectedValue = document.all(ListName).item(selectedIndex).value;
	document.all(HiddenField).value = selectedValue;
}

function SetSelectFromValue(ListName, SelValue)
{
        var oSelList = document.all(ListName).options;
        var lList = oSelList.length;
        for (var i = 0; i<lList; i++)
        {
                if (oSelList(i).value == SelValue)
                {
                        oSelList(i).selected = true;
                        break;
                }
        }
}

var L_NoCurrentClasses_TXT = "        (No Active Classes)";
var L_NoArchivedClasses_TXT = "        (No Archived Classes)";

//===============================================================
// Update the list element with new properties

function UpdateList(selList, numAvail, numArchived, numEnrolled, numEnrolledA)
{
	var oSelList = document.all(selList);
	oSelList.m_numAvail = numAvail;
	oSelList.m_numArchive = numArchived;
	oSelList.m_numEnrolled = numEnrolled;
	oSelList.m_numEnrolledA = numEnrolledA;
}

//===============================================================
// Add an option element to a select element.
function AddOptionElement(listName, index, innerText, value, selected)
{
	var oO = document.createElement("OPTION");
	document.all(listName).options.add( oO, index);
	oO.innerText = innerText;
	oO.value = value;
	oO.selected = selected;
}

/*
<select> items have the following private data fields:

m_numAvail = number of current classes user not enrolled
m_numEnrolled = number of current classes enrolled
m_numArchived = number of archived classes user not enrolled
m_numEnrolledA = number of archived classes user enrolled

AList
{
	listName
	curIndex	;	// index of current classes
	numCurrent		// num of current classes in the list
	arcIndex		// index of first archived class
	numArchived		// num of archived classes in the list
}
*/

//===============================================================
// FromList and ToList are objects (described as AList, above)
// that are modified with new values indicating which types of 
// objects have moved.
function MoveClasses(FromList, ToList, All, saveToList)
{
	var oOptions = document.all(FromList.listName).options;
	var oToList = document.all(ToList.listName).options;
	var fromSelect = document.all(FromList.listName);
	var toSelect = document.all(ToList.listName);
	var length = oOptions.length;
	var id;
	
	//remove all selection from ToList
	var lTo = oToList.length;
	for( i = 0; i< lTo; i++ )
		oToList(i).selected = false;

	// keep track of which type of class moved
	var movedCurrent = false;
	var movedArchived = false;

	// for each element in the 'FromList', see if it's selected and move it
	// to 'ToList' if it is
	for( i = 0; i < length; i++ )
	{

		if( oOptions(i).selected || All)
		{
			var strInnerText = oOptions(i).innerText;
			var value = oOptions(i).value;
			var index;

			// if value is 0, this is not a selectable element, so don't move
			// it.
			if (value == 0)
			{
				oOptions(i).selected = false;
				continue;	
			}
		
			// store the beginning & end (inclusive) of the current and archived
			// lists
			var FromListEndCur = Number(FromList.curIndex + FromList.numCurrent - 1);
			var ToListEndCur = Number(ToList.curIndex + ToList.numCurrent - 1);

			// if a "current" class is selected...
		 	if (( FromList.curIndex <= i ) && 
				(i <= FromListEndCur))
			{
				// if there are no current classes in ToList, then remove the 
				// placeholder text and set the index to insert at the top of 
				// the current list.
				if (ToList.numCurrent == 0)
				{
					oToList.remove(ToList.curIndex);
					index = 1;
				}
				else
				{
					index = FindLocationInRange(oToList, strInnerText, ToList.curIndex, ToListEndCur);
					// Update counters, only if this operation is not replacing
					// a placeholder element.
					ToList.arcIndex++;
				}
				

				// update the counters
				ToList.numCurrent++;
				FromList.numCurrent--;

				// if we did not just replace the last element, then update 
				// the index of the beginning of the list of archived classes
				if (FromList.numCurrent != 0)
					FromList.arcIndex--;

				// remember that we changed the current classes list
				movedCurrent = true;

			}
			else	// archived class is selected
			{
				
				// if there are no archived classes in ToList, then remove the 
				// placeholder text and set the index to insert at the top of 
				// the archived list
				if (ToList.numArchived == 0)
				{
					oToList.remove(ToList.arcIndex);
					index = ToList.arcIndex;
				}
				else
				{
					// there are archived classes in the ToList, so determine
					// the insertion point
					index = FindLocationInRange(oToList, strInnerText, ToList.arcIndex, oToList.length - 1);
				}
				
				// update the counters
				ToList.numArchived++;
				FromList.numArchived--;

				// remember that the archived classes list was changed
				movedArchived = true;
			}

			// Add new element to "To" list
			AddOptionElement(ToList.listName, index, strInnerText, value, true);

			// Remove the element from the "From" list
			document.all(FromList.listName).options.remove( i );

			length--; //removed from list
			i--; //decrement index back

		}	// end if (this element selected)
	} // end for(each element in FromList)

	// If all current classes were just removed, then add 
	// placeholder element
	if ( movedCurrent && (FromList.numCurrent == 0))
		AddOptionElement(FromList.listName, 1, L_NoCurrentClasses_TXT, 0, false);

	// If all archived classes were just removed, then add placeholder
	// element
	if ( movedArchived && (FromList.numArchived == 0))
		AddOptionElement(FromList.listName, FromList.arcIndex, L_NoArchivedClasses_TXT, 0, false);

}

//===============================================================
// Move from one select> list to another. Optionally, move all elements.
function Move(FromList, ToList, All, saveToList)
{
	var oOptions = document.all(FromList).options;
	var length = oOptions.length;
	var oToList = document.all(ToList).options;
	
	//remove all selection from ToList
	var lTo = oToList.length;
	for( i = 0; i< lTo; i++ )
		oToList(i).selected = false;

	for( i = 0; i < length; i++ )
	{

		if( oOptions(i).selected || All)
		{
			var strInnerText = oOptions(i).innerText;
			var value = oOptions(i).value;
			var index;
			if( oToList.length > 0 ) //if more than one item is already in list
				index = FindLocation( oToList, strInnerText ); //find where to put new item
			else 
				index = 0;
			var oO = document.createElement("OPTION");
			document.all(ToList).options.add( oO, index);
			oO.innerText = strInnerText;
			oO.value = value;
			oO.selected = true;
			document.all(FromList).options.remove( i );

			length--; //removed from list
			i--; //decrement index back
		}
	}
}

//===============================================================
// Find the insertion location, within a specific range. The index 
// returned will not be less than <beginRange>, but (if the insertion
// point is the end) may be one greater than <endRange>.
//
// <beginRange> is the index of the first element in the existing list
// <endRange> is the index of the last element in the existing list
function FindLocationInRange( oOptions, str, beginRange, endRange)
{
	return BinaryFind(oOptions, str.toUpperCase(), beginRange, endRange );	
}

function FindLocation(oOptions, str)
{
	return FindLocationInRange(oOptions, str, 0, oOptions.length - 1);
}

//===============================================================
// returns the index that is the insertion point
function BinaryFind( oOptions, str, lowIndex, upIndex )
{
	//this is modified binary search. It returns the index of
	//where item should be inserted (unlike returning 'not found' if item not found)
	if( lowIndex >= upIndex || lowIndex+1 == upIndex )
	{
		//our stuff goes in the middle
		var txtFound = oOptions(lowIndex).innerText.toUpperCase();
//		if( str < txtFound )
		if( str.localeCompare(txtFound) < 0 )
			return lowIndex;
		else
		{
			txtFound = oOptions(upIndex).innerText.toUpperCase();
			if( str.localeCompare( txtFound) < 0 )
				return upIndex;
			else
				return upIndex+1;
		}
	}
	var middle = Math.ceil(lowIndex + ((upIndex-lowIndex)/2));
	var textFound = oOptions(middle).innerText.toUpperCase();
	if( textFound.localeCompare(str) < 0 )
		return BinaryFind( oOptions, str, middle+1, upIndex )
	else if( textFound.localeCompare( str ) > 0 )
		return BinaryFind( oOptions, str, lowIndex, middle-1);	
	else
		return middle; //same names
}
//===============================================================
// save values from an options list, within the begin/end range.
// <begin> and <end> are inclusive
// <AppendHid> is the suffix to append to the listName to get the
// name of the hidden element where the values will be saved
function SaveValues2(ListName, AppendHid, Begin, End)
{
	var ListNameHidValue = "";
	//var Count = document.all(ListName).options.length;
	var Count = 0;
	for (i = Begin; i <= End; i++)
	{
		// Don't include elements with a value of 0, as they are subtitles 
		// within the select element.
		if (document.all(ListName).options(i).value > 0)
		{
			ListNameHidValue += (Count > 0) ? "," : "";
			Count++;
			ListNameHidValue += document.all(ListName).options(i).value;
		}
	
	}
	document.all(ListName + AppendHid).value = ListNameHidValue;
}

//===============================================================
// Save values from a <select> list to its corresponding hidden 
// element.
function SaveValues(ListName)
{
	var ListNameHidValue = "";
	var Count = document.all(ListName).options.length;
	for (i = 0; i < Count; i++)
	{
		ListNameHidValue += document.all(ListName).options(i).value + (i < Count - 1 ? "," : "");
	
	}
	document.all(ListName + "Hid").value = ListNameHidValue;
}
//===============================================================
function Trim(vElement)
{
	var oForm = document.forms(0);
	var oldValue = document.all(vElement).value;
	var reg = /\s/g;
	var words = oldValue.split(reg);
	var newValue = "";
	for (i=0; i < words.length; i++)
		newValue = newValue + words[i] + (i < words.length - 1 ? " " : "");
	document.all(vElement).value = newValue;
}
