/**
  Copyright (c) 2008 National Cancer Institute (NCI)
  All Rights Reserved.

  cmn

  $Revision: 1.16 $
  $Author: shengh2 $
**/

function confirmUserDelete() {
	return confirm("Are you sure you want to delete the user?");	
}

// Given a checkbox object, form and an element name, this function 
// mirrors the action performed on this object to all other objects 
// within the form element with the same value.

function mirrorAction(object, formName, element) {
	foundCounter = 0;
	elementsName = formName.elements[element];	
	for (i = 0 ; i < elementsName.length; i++)
	{
		if (elementsName[i].value == object.value)
		{
			foundCounter++;
			elementsName[i].checked = object.checked; 
		}
	}
	if (foundCounter > 1)
	{
		alert("Mulitple instances of this program were found. This could happen if the program is associated with more than one goal. Your action to the program has been mirrored to all other instances of the program. ");
	}
}

// Given a checkbox object, form and an element name, this function 
// mirrors the action performed on this object to all other objects 
// within the form element.

function allAction(object, formName, element) {
	elementsName = formName.elements[element];	
	for (i = 0 ; i < elementsName.length; i++)
	{
		elementsName[i].checked = object.checked;
	}
}

// Function to clear out a document
function clearAllFormFields(aForm) {
	for (j=0; j<aForm.length; ++j) {
		anElement = aForm.elements[j];
		theType = anElement.type;
		if (theType == "text" && anElement.disabled == false) {
			anElement.value = "";
		}
		else if (theType == "radio") {
			anElement.checked = false;
		}
		else if (theType == "checkbox") {
			anElement.checked = false;
		}
		else if (theType == "select-one" && anElement.disabled == false) {
			anElement.selectedIndex = 0;
		}
		else if (theType == "select-multiple" && anElement.disabled == false) {
			anElement.selectedIndex = -1;
		}
		else if (theType == "password") {
			anElement.value = "";
		}
		else if (theType == "textarea") {
			anElement.value = "";
		}
		else if (theType == "file") {
			anElement.value = "";
		}
	}
}

// Function to clear out a form
function doClear(aForm) {
	if(typeof aForm == 'undefined'){
		for (i=0; i<document.forms.length; ++i) {
			form = document.forms[i];
			clearAllFormFields(form);
		}
	}
	else {
		clearAllFormFields(aForm);
		populateLists(aForm);
	}
}

// Function to reload the page with data retrieved from the database again.
function doRefresh() {
	 window.location.reload(true);
}

// Function to close the window
function doClose() {
    window.opener.location.reload(true);
	window.close();
}

// Function to print
function doPrint() {
	window.print();
}

// Function to pop up an information window
function doPopup(url) {
	window.open(url, '_blank', 'toolbar=no, directories=no, location=no, status=no, menubar=no, resizable=yes, scrollbars=yes, width=800, height=600');
	return false;
}

// Call to respond to the appropriate button action
function doAction(aForm, anAction, aParam) {
	if (anAction == 'Revert') {
		window.location.reload(true);
	}
	else if (anAction == 'Exit') {
		window.close();
	}
	else if (anAction == 'Close') {
		window.opener.location.reload(true);
		window.close();
	}
	else if (anAction == 'Print') {
		window.print();
	}
	else if (anAction == 'Clear') {
		for (j=0; j<aForm.length; ++j) {
			anElement = aForm.elements[j];
			theType = anElement.type;
			if (theType == "text" && anElement.disabled == false) {
				anElement.value = "";
			}
			else if (theType == "radio") {
				anElement.checked = false;
			}
			else if (theType == "checkbox") {
				anElement.checked = false;
			}
			else if ((theType == "select-one" || theType == "select-multiple") && anElement.disabled == false) {
				anElement.selectedIndex = 0;
			}
			else if (theType == "password") {
				anElement.value = "";
			}
			else if (theType == "textarea") {
				anElement.value = "";
			}
			else if (theType == "file") {
				anElement.value = "";
			}
		}
		
		clearLists(aForm);
	}
	else if (anAction == 'Popup') {
		window.open(param, '_blank', 'toolbar=no, directories=no, location=no, status=no, menubar=no, resizable=yes, scrollbars=no, width=600, height=300');
	}
	else if (anAction == 'Page') {
		aForm.action.value = action;
		aForm.pageDirection.value = param;
		aForm.submit();
	}
	else if (anAction == 'Delete') {
		var ok = confirm("Do you really want to delete the record from the database?"); 
		if (ok) {
			aForm.action.value = anAction;
			aForm.submit();
		} 		
	}
	else if (anAction == 'Update') {
		var toInbox = "";

		for (j=0; j<aForm.length; ++j) {
			anElement = aForm.elements[j];
			if (anElement.type == "checkbox" && anElement.checked == true) {
				toInbox = toInbox + anElement.name + ", ";
			}
		}

		if (typeof aForm.interestedIds != "undefined") {
			aForm.interestedIds.value = toInbox;
		}
		aForm.action.value = action;
		aForm.submit();
	}
	else if (anAction == 'Preview') {
		doPreview();
	}	
	else if (anAction == 'Assign') {
		doAssign();
	}
	else if (anAction == 'Select') {
		doSelect();
	}
	else {
		aForm.action.value = anAction;
		// Highlight multiple select list
		for (j=0; j<aForm.length; ++j) {
			anElement = aForm.elements[j];
			if (anElement.type == "select-multiple") {
				len = anElement.length;
				for (i=0; i<len; ++i) {
					anElement.options[i].selected = true;
				}
			}
		}
		if (anAction == 'Add') {
			aForm.objId.value = "";
		}

		aForm.submit();
	}
	return false;
}

function doPage(direction) {
	document.forms[0].action.value = "page";
	document.forms[0].pageDirection.value = direction;
	document.forms[0].submit();
	return false;
}

function doLogin(url) {

	if (window.opener == null) {
		if (this.parent != null) {
			this.parent.location.href = url;
		}
		else {
			this.location.href = url;
		}
	}
	else {
		window.opener.document.location.href = url;
		window.close();
	}
	return false;
}

function doAlert(action, trueUrl, falseUrl, message) {

	var ok = confirm(message);
	if (ok) {
		if (action != null) {
			doAction(action);
		}
		if (trueUrl != null) {
			window.location.href = trueUrl;
		}
		return true;
	}
	else {
		if (falseUrl != null) {
			window.location.href = falseUrl;
		}
		return false;
	}
}
