
/* --------------------------------------------------------------------- */

/* --- Action Functions --- */
 
// Does an alert box with given message (comma delimited) - bmargolis 4.3.2006
function alertSelect (theForm, theSelect, message, valueList, boolSelectionShouldBelongToList, boolClearSelect) {
	alertSelectWithDelimiter (theForm, theSelect, message, valueList, ',', boolSelectionShouldBelongToList, boolClearSelect);
}

// Does an alert box with given message - bmargolis 4.3.2006
function alertSelectWithDelimiter (theForm, theSelect, message, valueList, delimiter, boolSelectionShouldBelongToList, boolClearSelect) {
	var selection = getSelectValue(theSelect);
	if (boolSelectionShouldBelongToList != 0) 
		boolSelectionShouldBelongToList = true;
	else 
		boolSelectionShouldBelongToList = false;
		 
	if (belongsWithDelimiter(selection, valueList, delimiter) == boolSelectionShouldBelongToList) { 
		alert(message);
		if (boolClearSelect) 
			theSelect.selectedIndex = 0;
	} 
}


// Does a confirm box based on a specific selection, then redirects to new BranchID if "Yes" (Comma delimited valueList) - bmargolis 4.3.2006
function confirmSelectRedirect (theForm, theSelect, message, valueList, boolSelectionShouldBelongToList, newBID) {
	confirmSelectRedirectWithDelimiter (theForm, theSelect, message, valueList, ',', boolSelectionShouldBelongToList, newBID);
}

// Does a confirm box based on a specific selection, then redirects to new BranchID if "Yes"  - bmargolis 4.3.2006
function confirmSelectRedirectWithDelimiter (theForm, theSelect, message, valueList, delimiter, boolSelectionShouldBelongToList, newBID) {
	var selection = getSelectValue(theSelect); 
	if (boolSelectionShouldBelongToList != 0) 
		boolSelectionShouldBelongToList = true;
	else 
		boolSelectionShouldBelongToList = false;

	if (belongsWithDelimiter(selection, valueList, delimiter) == boolSelectionShouldBelongToList) { 
		if (confirm(message)) {
			 theForm.isselectsubmit.value = 1;
			 if (newBID) {
			 	theForm.bid.value = newBID; 
			 } 
			 theForm.submit();
		} 
	}  
}

/* --------------------------------------------------------------------- */

/* --- Utility Functions --- */


// verifies that value is a member of list, ignoring case (comma delimited) - bmargolis 4.3.2006
function belongs(value, list) {
	return belongsWithDelimiter(value, list, ',');
}

// verifies that value is a member of list, ignoring case - bmargolis 4.3.2006
function belongsWithDelimiter(value, list, delimiter) {
	var actionables = list2ArrayWithDelimiter(list, delimiter);
	for (var i = 0; i < actionables.length; i++) {
		if (value.toLowerCase() == actionables[i].toLowerCase()) { 
			return true;
		}  
	} 
	return false;
}
 	

// converts list to array with a comma as delimiter - bmargolis 4.3.2006
function list2Array (strList) {
	return list2ArrayWithDelimiter(strList, ',');
}

// converts list to array with given delimiter - bmargolis 4.3.2006
function list2ArrayWithDelimiter (strList, delimiter) {
	var arr = new Array();
	var token = ""; // temp Word
	var c = ''; // temp Char
	
	for (var i = 0; i < strList.length; i++) {
		c = strList.charAt(i);
		if (c == delimiter) {
			arr[arr.length] = token;
			token = "";
		} else {
			token = token + c;
		}
	}
	
	// put the last token on the array
	if (token.length)
		arr[arr.length] = token;
	
	return arr;
}

// gets the value of the selected row on a select box - bmargolis 4.3.2006 
function getSelectValue(theSelect) {
	return theSelect.options[theSelect.selectedIndex].value;
}

// gets the text of the selected row on a select box - bmargolis 4.3.2006 
function getSelectText(theSelect) {
	return theSelect.options[theSelect.selectedIndex].text;
}
