// For use with element focusing logic
var lastElementFocused = new Array();
lastElementFocused['main'] = '';

// Set logic to warn the user before exiting if changes were made.
var changesMade = false;
window.onbeforeunload = confirmExit;

function markChanges()
{
	changesMade=true;
	
	// Turn on the save button if there is one
	focusNewElement('save_button', 'save_buton');
}

function toggleCheckbox(id)
{
	element= document.getElementById(id);
	if (element.checked)
	{
		element.checked = false;
	}
	else
	{
		element.checked = true;
	}

	// Mark that changes were made
	markChanges();
} // end toggleCheckbox

function checkAllByName(prefix, checkValue)
{
	if (!checkValue) { checkValue = 0; }
	var inputs = document.getElementsByTagName("INPUT");
	for (x in inputs)
	{
		if (inputs[x].name)
		{
			if (inputs[x].name.substr(0, prefix.length)==prefix) 
			{
				inputs[x].checked = checkValue;
			} // end if name matches prefix
		} // end if input has a name
	} // end for each input

	// Mark that changes were made
	markChanges();

} // end checkAllByName()

function checkAllInRange(startnum, howmany, checkValue)
{
	// If no boxes to check, get out now
	if (!howmany) { return }

	if (!checkValue) {checkValue = 0;}

	if (!startnum) 
		{startnum = 0;}

	endnum = startnum + howmany;
	startnum ++;

	// Set the boxes from startnum to endnum
	for(i=startnum; i<=endnum; i++)
	{
		thisName = "cbx_" + i;
		
		thisBox=document.getElementById(thisName);
		thisBox.checked = checkValue;
	}

	markChanges();

} // end checkAllFromNum()

function confirmExit()
{
	if (changesMade)
	{
		return "You have attempted to leave this page.  If you have made any changes to the fields without clicking the Save button, your changes will be lost.  Are you sure you want to exit this page?";
	}
} // end confirmExit()

// Unfocus the last focused element in the group
function unFocusLastElement(group) 
{
	if (!group) { group = 'main'; }

	// Hide previous focus element
	if (lastElementFocused[group])
	{
		document.getElementById(lastElementFocused[group]).style.display='none';
	}
}

function focusNewElement(id, group) 
{
	if (!group) { group = 'main'; }

	// Check if we're focusing the same element again - if so, just return
	if (lastElementFocused[group] == id) { return; }
	
	// Hide previous focus element
	unFocusLastElement(group)
	
	// Show the new element, and remember that we did
	lastElementFocused[group] = id;
	document.getElementById(id).style.display='block';
} // end FocusNewElement

function setClass(id, newclass)
{
	document.getElementById(id).className=newclass;
} // end setClass 

function toggleClass(id, class_1, class_2)
{
	if (document.getElementById(id).className==class_1) 
	{
		document.getElementById(id).className=class_2;
	} 
	else 
	{
		document.getElementById(id).className=class_1;
	}
} // end toggleClass 

function toggleVisible(id) 
{
	if (document.getElementById(id).style.display=='none') 
	{
		document.getElementById(id).style.display='block';
	} 
	else 
	{
		document.getElementById(id).style.display='none';
	}
} // end toggleVisible
