function multiSelectSetup(sFormName, sOne, sTwo) {
	if (sOne) {
		var oOne = eval('document.forms.' + sFormName + '.' + sOne);
		
		for (var i=oOne.options.length-1; i >= 0; i--) {
			if (oOne.options[i].value == "") {
				oOne.options[i] = null;
				break;
			}
		}
	}
	
	if (sTwo) {
		var oTwo = eval('document.forms.' + sFormName + '.' + sTwo);
		
		for (var i=oTwo.options.length-1; i >= 0; i--) {
			if (oTwo.options[i].value == "") {
				oTwo.options[i] = null;
				break;
			}
		}
	}
}

function reportBug() {
	var oForm = document.forms.bugform;
	
	oForm.submit();
	
	return false;
}

// Selects every option of myForm.sel
function selectAll(myForm, sel) {
	me = eval("myForm." + sel);
	
	if (me) {
		for (i=0; i < me.options.length; i++) {
			me.options[i].selected = true;
		}
	}
}

// Deselects every option of myForm.sel
function deselectAll(myForm, sel) {
	me = eval("myForm." + sel);
	
	if (me) {
		for (i=0; i < me.options.length; i++) {
			me.options[i].selected = false;
		}
	}
}

function moveOptions(formName, source, dest, bCopyAll, singleSelectName) {
	var s = eval('document.forms.' + formName + '.' + source);
	var d = eval('document.forms.' + formName + '.' + dest);
	
	var singSel = null;
	if (singleSelectName) {
		singSel = eval('document.forms.' + formName + '.' + singleSelectName);
	}
	
	for (var i=s.options.length-1; i >= 0; i--) {
		if (bCopyAll || s.options[i].selected) {
			
			d.options[d.options.length] = new Option(s.options[i].text, s.options[i].value);
			
			// Use singSel
			if (singSel) {
				// If it is in single sel, remove it
				var bFoundInSS = false;
				for (var j=singSel.options.length-1; j >= 0; j--) {
					if (singSel.options[j].value == s.options[i].value) {
						if (singSel.options[j].selected) {
							singSel.options[0].selected = true;
						}
						
						singSel.options[j] = null;
						bFoundInSS = true;
						break;
					}
				}
				
				// If it was not in single sel, add it now
				if (!bFoundInSS) {
					singSel.options[singSel.options.length] = new Option(s.options[i].text, s.options[i].value);
				}
			}
			
			
			s.options[i] = null;
		}
	}
	
	return false;
}

// Set the specified form field to the given value, and submits the form if needed
function setFormValue(sFormName,sElementName,sValue,bSubmitForm) {
	var oElement = eval('document.forms.' + sFormName + '.' + sElementName);
	oElement.value = sValue;
	
	if (bSubmitForm) {
		oElement.form.submit();
	}
	
	return false;
}


// Moves selected options from source to dest.  Also adds the options to fPrimary.
function moveCategories(source, dest, bCopyAll) {
	var s = eval('myForm.' + source);
	var d = eval('myForm.' + dest);
	var p = myForm.fPrimary;
	
	for (i=s.options.length-1; i >= 0; i--) {
		if (bCopyAll || s.options[i].selected) {
			
			d.options[d.options.length] = new Option(s.options[i].text, s.options[i].value);
			
			// Copying to current
			if (source == 'fAvail') {
				p.options[p.options.length] = new Option(s.options[i].text, s.options[i].value);
			
			// Removing from current
			} else {
				for (j=p.options.length-1; j >= 0; j--) {
					if (p.options[j].value == s.options[i].value) {
						p.options[j] = null;
						break;
					}
				}
			}
			
			s.options[i] = null;
		}
	}
	
	return false;
}


/*
// Moves selected options from source to dest (similar to moveCategories, but doesn't use fPrimary)
function moveOptions(source, dest, bCopyAll) {
	var s = eval('myForm.' + source);
	var d = eval('myForm.' + dest);
	
	for (i=s.options.length-1; i >= 0; i--) {
		if (bCopyAll || s.options[i].selected) {
			
			d.options[d.options.length] = new Option(s.options[i].text, s.options[i].value);
			
			s.options[i] = null;
		}
	}
	
	return false;
}
*/


// Creates a popup window for previewing the specified survey
function previewSurvey(ID, bPopup, bPreview) {
	var params = "";
	
	if (bPreview) {
		params = "?preview=1&s_id="
	} else {
		params = "?s_id="
	}
	
	var winPrev = window.open(baseURL + "/display/survey.cfm" + params + ID, "winPrev", "width=780,height=600,scrollbars,resizable");
	/*
	if (bPopup == 1) {
		var winPrev = window.open(baseURL + "/display/survey.cfm" + params + ID, "winPrev", "width=800,height=600,scrollbars,resizable");
	} else {
		var winPrev = window.open(baseURL + "/display/survey.cfm" + params + ID, "winPrev");
	}
	*/
	
	return false;
}

// View an answer in a popup window
function viewAnswer(ansID) {
	var winAns = window.open(baseURL + "/app_logic/answer_view.cfm?aans_id=" + ansID, "winAns", "width=800,height=300,scrollbars,resizable");
	
	return false;
}


// Gets called when adding a user to the select box s
function addUser(ID, fullName) {
	var bPresent = false;
	
	// Make sure user is not already present
	for (i=0; i<s.options.length; i++) {
		if (s.options[i].value == ID) {
			bPresent = true;
			break;
		}
	}
	
	if (!bPresent) {
		s.options[s.options.length] = new Option(fullName, ID);
	}
	
	return false;
}


function addUsersPerson(bInternalEmail) {
	var winSelect = window.open(baseURL + "/app_logic/user_search_person.cfm?bInternalEmail="  + bInternalEmail, "winS", "width=550,height=560,scrollbars,resizable");
	winSelect.focus();
	
	return false;
}

function addUsersRole(bInternalEmail) {
	var winSelect = window.open(baseURL + "/app_logic/user_search_role.cfm?bInternalEmail="  + bInternalEmail, "winS", "width=550,height=560,scrollbars,resizable");
	winSelect.focus();
	
	return false;
}


// Will open up a window to display all the info for the users ids in s
function viewDetails(bInternalEmail) {
	var lIDs = "";
	
	for (i=0; i<s.options.length; i++) {
		if (s.options[i].selected) {
			lIDs = lIDs + s.options[i].value + ",";
		}
	}
	
	if (lIDs.length) {
		// Remove the final comma
		lIDs = lIDs.substring(0, lIDs.length);
		
		var winDetails = window.open(baseURL + "/app_logic/user_details.cfm?bInternalEmail=" + bInternalEmail + "&lUIDs=" + lIDs, "winD", "width=300,height=300,resizable,scrollbars");
		winDetails.focus();
	}
	
	return false;
}


// Removes the selected items in s
function removeSelected() {
	for (i=s.options.length-1; i >=0; i--) {
		if (s.options[i].selected) {
			s.options[i] = null;
		}
	}
	
	return false;
}

// Counts and alerts the number of items in s
function countUsers() {
	if (s.options.length == 1) {
		alert ('There is currently 1 user in the list.');
	} else {
		alert ('There are currently ' + s.options.length + ' users in the list.');
	}
	
	return false;
}