/********************
 *
 *	JAVASCRIPT_CUSTOMLIST.JS
 *	Custom-list javascript for ITC
 * 
********************/

// Adds an entered item to the list box
function customList_AddItem(AddListItemName, FormName, GetValueCallBackFunction) {
	
	GetValueCallBackFunction2 = GetValueCallBackFunction;
	
	eval('FormObj = document.' + FormName + ';');
	eval('ListItemObj = FormObj.' + AddListItemName + ';');
	
	if (!GetValueCallBackFunction)
		ListItemObj.disabled = true;
	
	ListItemIDsArray = new Array();
	for (var i = 0; i < FormObj.elements.length; i++) {
		if (FormObj.elements[i].name != null && FormObj.elements[i].name.substr(0,AddListItemName.length+1) == AddListItemName + '_')
			ListItemIDsArray.push(FormObj.elements[i].value);
	}
	
	GETStr = null;
	if (GetValueCallBackFunction) {
		eval('NewValue = ' + GetValueCallBackFunction + '();');
	}
	else {
		NewValue = document.getElementById(AddListItemName + '_ID').value;
		
		if (!NewValue) {
			alert('You did not enter anything to add');
			return;
		}
	}
	
	if (!NewValue || NewValue == null)
		return;
	
	GETStr = '&NewValue=' + NewValue;
	
	// Check if serial number already exists
	if (in_array((NewValue.indexOf('&') != -1)?NewValue.substr(0, NewValue.lastIndexOf('&')):NewValue, ListItemIDsArray)) {
		alert('The information you entered is already in the list');
		
		if (!GetValueCallBackFunction) {
			ListItemObj.value    = '';
			ListItemObj.disabled = false;
		}
		
		return;
	}
	
	AddListItemName2 = AddListItemName;
	
	AJAX_sendRequest(true,
	
	'?CustomListAction=' + AddListItemName + GETStr,
	
	"if (VarForResponseText.substr(0,7) == 'OUTPUT:') { ReturnedDataArray = VarForResponseText.substr(7).split('|'); " +
	"AddListItemID = ReturnedDataArray[0]; Data = ReturnedDataArray[1]; customList_AddItem_Action(AddListItemName2, " + 
	"AddListItemID, Data); document.getElementById('" + AddListItemName + "_ID').value = ''; } else { alert(VarForResponseText);" +
	"} if (!GetValueCallBackFunction2) ListItemObj.disabled = false;");
}

// Runs the action for adding a custom list item
function customList_AddItem_Action(AddListItemName, AddListItemID, ContainedOutput) {
	NewDiv = document.createElement('div');
	with (NewDiv) {
		setAttribute('id', AddListItemName + '_Container_' + AddListItemID);
		innerHTML = ContainedOutput;
	}
	
	// Hide 'nothing here' msg
	document.getElementById('CustomListContainer_NoItems_' + AddListItemName).style.display = 'none';
	
	// Add new data
	document.getElementById('CustomListMasterContainer_' + AddListItemName).appendChild(NewDiv);
}

// deletes an item from a custom list
function deleteCustomListItem(ListItemName, ListItemID) {
	MasterContainer = document.getElementById('CustomListMasterContainer_' + ListItemName);
	MasterContainer.removeChild(document.getElementById(ListItemName + '_Container_' + ListItemID));
	
	NoItemsMsgDiv = document.getElementById('CustomListContainer_NoItems_' + ListItemName);
	if (MasterContainer.lastChild == NoItemsMsgDiv || MasterContainer.lastChild == '[object Text]')
		NoItemsMsgDiv.style.display = 'block';
}
