/*
 *	ES Notification interface stuff.
 */ 
 
// Note that popup var is defined in including pages

// Fill in the options for noteList from hidden noteListOptions field.
// Note this is called on init, and from popup to refresh list after update.
function WriteNoteListOptions(theForm) {
	var noteListOptions = theForm['noteListOptions'].value;
	var noteList = theForm['noteList'];
	var noteAry, ary, days;

	if (noteListOptions != "") {
		if (noteListOptions.search("^^") == -1) {
			noteAry = Array(noteListOptions);
		} else {
			noteAry = noteListOptions.split("^^");
		}

		// Clear the list
		for (var i = noteList.options.length-1; i >= 0; i--) {
			noteList.options[i] = null;
		}

		// Build the options
		for (var i = 0; i < noteAry.length; i++) {
			ary = noteAry[i].split("|");
			days = ary[2];
			// Format days to be 3 chars wide, including +/- sign
			if (parseInt(days) < 0) {
				days = "\ -" + days;
			} else if (parseInt(days) > 0) {
				days = "+" + days;
			} else {
				// Is 0, try to make it look 3 chars wide
				days = "\ \ \ 0";			}
			if (days.length == 2) {
				days = " " + days;
			}
			// Text is "[days] - Name"; value is "id"
			noteList.options[i] = new Option(("["+ary[3]+"][" + days + "] " + ary[0]), ary[1], false, false);
		}
	}
}


// Returns ids of notes in noteList
function GetNoteListIds(noteList) {
	var idList = "";
							
	for (var i = 0; i < noteList.length; i++) {
		if (i == 0) {
			idList = noteList.options[i].value;
		} else {
			idList = idList + "," + noteList.options[i].value;
		}
	}
	return idList;
}


// Add note(s) to the note list
function AddNotesToNoteList(theForm, ownerType, ownerId, curNoteIds) {
	var popupUrl = "esNotesAdd.asp?ownerType=" + ownerType + "&ownerId=" + ownerId + "&curNoteIds=" + curNoteIds;
	if (!(popup == null || popup.closed)) {
		// Close the left-open popup so only one up at a time
		popup.close();
	}
	// Show the popup
	popup = window.open(popupUrl,'popupPage','width=400,height=300,top=100,left=100');
}


// Edit a note in the list
function EditNoteListItem(theForm, ownerType, ownerId) {
	var optList = theForm['noteList'].options;
	var nSelected = 0;
	var noteId = "";
	
	// Make sure just one item is selected; list is MULTIPLE	
	for (var i = 0; i < optList.length; i++) {
		if (optList[i].selected) {
			noteId = optList[i].value;
			nSelected++;
		}
	}
	if (nSelected == 0) {
		alert("Please select an item.");
		return;
	} 
	if (nSelected > 1) {
		alert("Please select a single item.");
		return;
	}

	var popupUrl = "esNoteEdit.asp?noteId=" + noteId + "&ownerType=" + ownerType + "&ownerId=" + ownerId;
	if (!(popup == null || popup.closed)) {
		// Close the left-open popup so only one up at a time
		popup.close();
	}
	// Show the popup
	popup = window.open(popupUrl,'popupPage','width=640,height=506,top=100,left=100,scrollbars=1');
}


// Delete notes from the list
function DeleteNoteListItems(theForm) {
	var optList = theForm['noteList'].options;
	var nSelected = 0;

	// Check for selection(s)
	for (var i = 0; i < optList.length; i++) {
		if (optList[i].selected) {
			nSelected++;
		}
	}
	if (nSelected == 0) {
		alert("Please select item(s).");
		return;
	} 

	if (!confirm("Are you sure you want to remove?")) {
		return;
	}

	// Delete those selected, bottom up
	var remainingNoteIds = "|";
	for (var i = optList.length-1; i >= 0; i--) {
		if (optList[i].selected) {
			optList.options[i] = null;
		} else {
			remainingNoteIds += optList.options[i].value + "|";
		}
	}
	
	// Rebuild hidden noteListOptions field to equal list's current contents
	var noteListOptions = theForm['noteListOptions'].value;
	var noteAry, re, noteId;
	var newOptInfo = "";

	if (remainingNoteIds != "|" && noteListOptions != "") {
		// List not empty
		if (noteListOptions.search("^^") == -1) {
			noteAry = Array(noteListOptions);
		} else {
			noteAry = noteListOptions.split("^^");
		}
		// Collect elements of noteAry with ids matching one of remainingNoteIds
		for (var i = 0; i < noteAry.length; i++) {
			// Break out the note id
			noteId = noteAry[i].split("|")[1];
			re = new RegExp("[\|]" + noteId + "[\|]");

			if (remainingNoteIds.search(re) != -1) {
				// Is one of remaining ids, keep this element
				newOptInfo += (newOptInfo == "")? "" : "^^";
				newOptInfo += noteAry[i];
			}
		}
	}

	// Update the field
	theForm['noteListOptions'].value = newOptInfo;
}
