// Keep IE from beeping when the enter key is pressed.
// To use, attach to onKeyDown event like this:
// <input type="text" onKeyDown="return pushedKeyboard(event);" name="description" value="SQL Server">
pushedKeyboard = function(evt) {
	 var keyCode = evt.keyCode;
	 if (keyCode == 13) {
		//Send();
		evt.cancelBubble = true;
		return false;
	 }
	 return true;
 }

// tabbed navigation functions

function activateNavSet(itemSetId, skipAjax) {

	deactivateNavSet();
	navSet = document.getElementById(itemSetId);
	if (navSet != null) navSet.style.display = 'block';
	navTab = document.getElementById(itemSetId + "Tab");
	if (navTab != null) navTab.className = 'tabSelected';
	//document.forms[0].elements['application_section'].value = itemSetId;

	//updateSelectedTab(itemSetId);
	if(!skipAjax) {
		var args = new Hash();
		args.addItem( 'tab', itemSetId );
		sendAjaxRequest( 'GET', 'app/Admin/Ajax/Tabs', args, true, null) ;
	}
}

function deactivateNavSet() {
	for (i = 0; i< navSets.length; i++) {
		navSet = document.getElementById(navSets[i]);
		if (navSet != null) navSet.style.display = 'none';
		navTab = document.getElementById(navSets[i] + "Tab");
		if (navTab != null) navTab.className = '';
	}
}

//The next 3 functions work together to implement the following feature:
//If "other" is chosen from a select box, a new text input field is added to allow the user to enter text for that field.
//If another selection is made, this text input field is deleted.
//For these to work, <select> must have id "selectId" and be inside a <td> with id "selectId"+"Cell"
function updateOtherField(selectId) {
	var selectElement = document.getElementById(selectId);
	var lastIndex = selectElement.options.length - 1;
	// Set to True or false: true if the last select option is selected (I.E. "Other"), false otherwise
	var otherSelected = selectElement.options[lastIndex].selected;
	// If they picked "Other" and there is not already a "selectId_other" element, add one.
	var otherField = document.getElementById(selectId+'_other');
	if (otherSelected) {
		//If they selected "Other" AND there isn't one already, add it.
		if(!otherField) addOtherField(selectId, lastIndex);
	}
	else {
		//Otherwise, if the other field exists, delete it.
		if(otherField) deleteOtherField(selectId, otherField);
	}
	return false;
}

// Swiped from alieffring's Citation/Edit.tpl javascript by Todd
// Adds a text input with id "cellId_other" to a table cell, and a label for it.
// Used when "Other" is selected from a pulldown menu
function addOtherField(selectId, lastIndex) {
	var cell = document.getElementById(selectId+'Cell');
	var selectElement = document.getElementById(selectId);
	var newField = '<input name="' + selectId + '_other" type="text" id="' + selectId + '_other'+'" size="30"/>';

// This is because setting innerHTML to itself + newField didnt work right in firefox.
	var labelField = document.createElement("span");
	labelField.setAttribute("for", selectId+"_other");
	labelField.setAttribute("id", selectId+"_other_label");
	labelField.innerHTML = 'Please specify:*';
	cell.appendChild(labelField);

	var newField = document.createElement("input");
	newField.setAttribute("type", "text");
	newField.setAttribute("name", selectId+"_other");
	newField.setAttribute("id", selectId+"_other");
	newField.setAttribute("size", "30");
	cell.appendChild(newField);

} // func addField

//For some reason, victim.parentElement is not getting the parent. So, we do this the hard way.
function deleteOtherField(selectId, otherField) {
	var parentCell = document.getElementById(selectId+'Cell');
	var labelElement = document.getElementById(selectId+'_other_label');
	//Check if there is a victim to delete. I.E.  <input id="affilation_other">
	if (otherField && parentCell) {
		parentCell.removeChild(otherField);
		if(labelElement) parentCell.removeChild(labelElement);
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
