// used on all pages showing list results

// calling page must include the following script:
// function validateThisForm()

// calling page must call the following scripts:
//
//<script language="JavaScript1.2" src="formHandler.js" type="text/javascript"></script>	
//<script language="JavaScript" src="library.js" type="text/javascript"></script>
//<script language="JavaScript" src="date.js" type="text/javascript"></script>
//<script language="JavaScript" src="AnchorPosition.js" type="text/javascript"></script>
//<script language="JavaScript" src="PopupWindow.js" type="text/javascript"></script>
//<script language="JavaScript" src="CalendarPopup.js" type="text/javascript"></script>


NS4 = (document.layers) ? true : false;
function checkEnter(event) { 	
// checks for Enter key for submission
	var code = 0;
	if (NS4) {
		code = event.which;
	} else {
		code = event.keyCode;
	}
	if (code==13){
		submitSearch();
	}
}
function submitSearch() {
	document.searchForm.submit();
}
function getDetail(ID, page) {
	document.thisForm.ID.value = ID;
	document.thisForm.action = page;
	document.thisForm.submit();
}
function sortList(advSearch, page, column, direction) {
	document.thisForm.action = page;
	document.thisForm.sort.value = column;
	document.thisForm.dir.value = direction;
	document.thisForm.submit();
}
function getPage(advSearch, page, pageNum) {
	document.thisForm.action = page;
	document.thisForm.page.value = pageNum;
	document.thisForm.submit();
}
function checkSearchDate(elem) {
	if (!dateCheck(elem.value)) {
		alert("Please enter a valid date.");
		elem.value = "";
		elem.focus();
		return false;
	}
	return true;
}
function resetSearch() {
	for (i = 0; i < document.searchForm.length; i++) {
		if (document.searchForm.elements[i].type == 'text') {
			document.searchForm.elements[i].value = "";
		}
		if (document.searchForm.elements[i].type.indexOf('select') != -1) {
			document.searchForm.elements[i].options[0].selected = true;
		}
		if (document.searchForm.elements[i].type == 'checkbox') {
			document.searchForm.elements[i].checked = 0;
		}
	}

}
function getObj(objId){
  if(document.getElementById) 
    return document.getElementById(objId);
  return document.all(objId);
}

function highlightRow(currElem) {
// changes row style
	var i = 0;
	var sClass = "";
	while (getObj("r" + i) != null) {
// check for overriding row style
		sClass = getObj("r" + i).className;
		if ((sClass == "listRow_Current") || (sClass == "listRow_Even") || (sClass == "listRow_Odd")) {
			if (i == currElem) {	// clicked this row
				getObj("r" + i).className = 'listRow_Current';
			} else { 	// user hasn't clicked this row; reset
				if (i % 2 == 1) {
					getObj("r" + i).className = 'listRow_Even';
				} else {
					getObj("r" + i).className = 'listRow_Odd';
				}
			}
		}
		i ++;
	}
}

function highlightGridElements(elemPrefix, newClass) {
// changes cell style for elements in grid
// td must have an ID name to highlight specific cells
	var i = 0;
	while (getObj(elemPrefix + i) != null) {
		getObj(elemPrefix + i).className = newClass;
		highlightElement(elemPrefix + i, newClass);
		i ++;
	}
}

function highlightElement(objID, newClass) {
// changes cell style for an element
	getObj(objID).className = newClass;
}

function selectRow(elem, objID, origClass) {
// changes row style
	if (elem.checked) {
		getObj(objID).className = 'listRow_Selected';
	} else {
		getObj(objID).className = origClass;
		document.thisForm.chkAll.checked = 0;
	}
}

function selectRow_All() {
// changes all row styles
	var elem = document.thisForm.selectedID;
	for (i = 0; i < elem.length; i ++) {
		if (elem[i].type == "checkbox") {
			elem[i].checked = 1;
			getObj("r" + i).className = 'listRow_Selected';
		}
	}
}

function selectDownload() {
// displays dialog to download records
// requires certain fields in form: chkAll, selectedID
	var downloadValue = "";
	var currForm = document.thisForm;
	var param = "";
	var totalCount = currForm.chkAll.value;
	var elem = currForm.selectedID;
	var elemValue = "";
	var elemCount = 0;
	if (isNaN(elem.length)) {	//only one element
		if (elem.checked) {
			elemValue = elem.value;
			elemCount += 1;	
		}
	} else {
		for (i = 0; i < elem.length; i ++) {
			if (elem[i].checked) {
				elemValue = elem[i].value;
				elemCount += 1;	
			}
		}
	}
	for (i = 0; i < elem.length; i ++) {
		if (elem[i].checked) {
			elemValue = elem[i].value;

		}
	}
	param = elemValue + '&selected=' + elemCount + '&total=' + totalCount;
	downloadValue = selectNewItem('selectDownload.asp', param , 'Download Results', '');
	if (downloadValue != "") {
		var currForm = document.thisForm;
		currForm.action = "downloadResults.asp?download=" + downloadValue;
		currForm.submit();
		currForm.action = "#";	// reset action
	}
}

function retrieveSavedSearch(searchTitle) {
// save the current search results or retrieve a saved search
	var currForm = document.savedSearch;
	if (currForm.searchID[currForm.searchID.selectedIndex].value == "") {
		alert("Please select a saved search to load.");
		return false;
	}
	if (currForm.searchID[currForm.searchID.selectedIndex].value == "0") {
		// user must submit a title for the search results
		currForm.title.value = searchTitle;
		currForm.title.value = prompt("Please enter the name of this search", currForm.title.value);
		if (currForm.title.value == "") {
			alert("You must enter a name for this search before you can save the search results.");
			return false;
		}
		if (currForm.title.value == "null") {
			return false;
		}
		currForm.desc.value = currForm.title.value;
		currForm.saveSearch.value = true;
	} else {
		currForm.saveSearch.value = false;
	}
	return true;
}



//----------NOTES --------------------------------------------------------

function getNote(noteID, recordID, table, title) {
// update triggered on new window, not here
	var randomParam = Math.random();
	var url = "noteDialog.asp?ID=" + noteID;
	url += "&recordID= " + recordID;
	url += "&table=" + table;
	url += "&pageTitle=" + title;
	url += "&random=" + randomParam;
	var returnValue = openWindow(url,'425','323'); //use regular window to allow user to leave note open
	if (returnValue == "false") {
		self.location = "login.asp";
	}
	returnValue = null;
}

//----------ACTIVITIES --------------------------------------------------------

function getActivity(activityID, recordID, table, title) {
// update triggered on new window, not here
	var randomParam = Math.random();
	var url = "activityDialog.asp?ID=" + activityID;
	url += "&recordID= " + recordID;
	url += "&table=" + table;
	url += "&pageTitle=" + title;
	url += "&random=" + randomParam;
	var returnValue = openWindow(url,'425','425'); //use regular window to allow user to leave activity open
	if (returnValue == "false") {
		self.location = "login.asp";
	}
	returnValue = null;
}

function downloadActivity(activityID) {
	var currForm = document.thisForm;
	document.thisForm.action = "downloadvCalendar.asp";
	currForm.ID.value = activityID;
	currForm.submit();
}

//----------GENERIC --------------------------------------------------------

function deleteItem(itemID, itemName) {
	if (confirm("Are you sure you want to delete this " + itemName + "?")) {
	var currForm = document.thisForm;
	currForm.DeleteID.value += itemID;
	currForm.submit();
	}
}

function openItem(itemID, selectID, recordID, sort, dir) {
	var currForm = document.thisForm;
	currForm.OpenID.value += itemID;
	currForm.submit();
}

function closeItem(itemID) {
	var currForm = document.thisForm;
	currForm.CloseID.value += itemID;
	currForm.submit();
}

