/*****************************************************************************************/
	
	ajaxHttp.onRequestComplete = function() {
		document.getElementById(ajaxHttp.setDivId).innerHTML =  ajaxHttp.getResponseText();
	}
	  
	function setContent(id, source, method, string) {
		ajaxHttp.setDivId = id;
		if(method = "GET") {
			var value = source + string;
			ajaxHttp.getRequest(value);
		} else if(method = "POST") {
			if(string == null) {string = ""}
			ajaxHttp.postRequest(source, string);
		}
	}

/*****************************************************************************************/

	function show_confirm(task, type) {
		var confirm_text = "";
		var associates = "";
		if(type == "volume") {associates = "issues";}
		if(type == "issue") {associates = "papers";}
		if(task == "delete") {
			confirm_text = confirm("Are you sure you want to " + task + " this " + type + " and its associated " + associates + "?");
		}
		if(task == "publish") {
			confirm_text = confirm("Publishing makes the " + type + " and its associated " + associates + " visible on the website. All unsaved edits will be lost. Are you sure you want to " + task + " this " + type + " and its associated " + associates + "?");
		}
		return confirm_text;
	}

/*****************************************************************************************/

	function toggleMenu(objID) {
		if (!document.getElementById) return;
		var ob = document.getElementById(objID).style;
		ob.display = (ob.display == 'block') ? 'none' : 'block';
	}
	
	function hideMenu(objID) {
		if (!document.getElementById) return;
		var ob = document.getElementById(objID).style;
		ob.display = 'none';
	}

/*****************************************************************************************/

	function insert_tag(id, tag, val) {
		var browser = navigator.appName;
		var obj = document.getElementById(id);
		var start_tag = "<" + tag + ">";
		var end_tag = "</" + tag + ">";
		var range;
	
		if(browser == "Microsoft Internet Explorer") {
			range = document.selection.createRange();
			if(!val)
			val = "";
			if(range.text)
			val = range.text;
			range.text = start_tag + val + end_tag;
			range.moveStart("character", -(end_tag.length + val.length));
			range.moveEnd("character", -(end_tag.length));
			range.select();
		} else {
			var s = obj.selectionStart;
			var e = obj.selectionEnd;
			var x = obj.value.substring(s, e);
			var beginning = obj.value.substring(0, s);
			var ending = obj.value.substring(e, obj.value.length);
			
			obj.value = beginning + start_tag + x + end_tag + ending;
			obj.selectionStart = s + start_tag.length;
			obj.selectionEnd = s + x.length - end_tag.length;
		}
		obj.focus();
	} // insert_tag()
	
/*****************************************************************************************/

	/*** This portion has variables specific to the author search and not to the search as a whole***/
	var add = "adder"; // id of the button to add authors to the list
	var remove = "remover"; // id of the button to remove authors from the list
	var destAjax = "suggestions"; // id of ajax div content to replace
	var auth = "author_id"; // id of where the author id's get stored for saving
	var instr = "input_str"; // id of the tet input where the query comes from

	/*** This portion of script deals with the auto-suggesting and related timer ***/

	var prev_input = ""; // The previously typed value
	var time; // variable used for the Timer
	var delay = 250; // set the amount of time (delay) after typing before querying the database
	
	/* takes in the typed string and won't query it until the delay has completed */
	function search_timer(input_str, number) {
		input = input_str; // record typed string
		num = number; // the id number of all the div that connect to a particular paper
		clearTimeout(time); // clear any previous delay settings
		time = setTimeout("lookup(input, num)", delay); // run the lookup() function after the delay
	} // search_timer()
	
	/* takes in the typed string and queries the database */
	function lookup(input_str, number) {
		// clear out spaces at the beginning and the end of the string so it has a better chance of matching
		input_str = input_str.replace(/(^\s*)|(\s*$)/g, "");
		num = number; // the id number of all the div that connect to a particular paper
		
		// Check the current string against the last entered query
		// If the text to search has changed then continue with the query
		if(prev_input != input_str) {
			prev_input = input_str; // save the current string for future comparison
			
			// this part is used to ensure selection of exact matches
			// the button is disabled before every query
			var add_button = add+num;
			document.getElementById(add_button).style.visibility = "hidden";
			
			var suggest = destAjax+num;
			if(input_str != "") { // also make sure the string isn't blank
				// run ajax to display the results of the query
				setContent(suggest, '/jpl/admin/form/rpc.php?num='+num+'&q=', 'GET', input_str);
			} else {
				// if the text input is empty remove the contents of the query results
				document.getElementById(suggest).innerHTML = "";
			}
		}
	} // lookup()
	
	// takes the selected value from the suggestion list and puts it into the text input
	function fill(thisValue, id, number) {
		prev_input = thisValue; // set the selected value as the previously searched for string
		num = number; // the id number of all the div that connect to a particular paper
		
		var input = instr+num;
		document.getElementById(input).value = thisValue; // put the selected value in the text input
		var author_id = auth+num;
		document.getElementById(author_id).value = id; // put the selected id in the hidden text input
		
		// this part enables the submit button when an exact match from the database has been made
		var add_button = add+num;
		document.getElementById(add_button).style.visibility = "visible";
		
		// empties the suggestion list once one has been selected
		var suggest = destAjax+num;
		document.getElementById(suggest).innerHTML = "";
	} // fill()
	
	// Clears the default contents of the text box once focused on
	function clearText(srcText) {
		text = document.getElementById(srcText);
		if(text.value == "Search for an author" || text.value == "Title of the paper to be added") {text.value = "";}
	}

/*****************************************************************************************/

	// Compare two options within a list by VALUE
	function compareOptionValues(a, b) {
		// Radix 10: for numeric values
		// Radix 36: for alphanumeric values
		var sA = parseInt(a.value, 36);
		var sB = parseInt(b.value, 36);
		return sA - sB;
	}
	
	// Compare two options within a list by TEXT
	function compareOptionText(a, b) { 
		// Radix 10: for numeric values
		// Radix 36: for alphanumeric values
		var sA = parseInt(a.text, 36);
		var sB = parseInt(b.text, 36);
		return sA - sB;
	}
	
	// Compare two options within a list by TITLE
	function compareOptionTitle(a, b) { 
		// Radix 10: for numeric values
		// Radix 36: for alphanumeric values
		var sA = parseInt(a.title, 36);
		var sB = parseInt(b.title, 36);
		return sA - sB;
	}
	
	// Move authors from auto-suggest entry to selected list
	function moveAuthor(srcList, destList, id, number) {
	
		newDestList = new Array(destList.options.length);
		
		var len = 0;
		num = number;
		
		for(len = 0; len < destList.options.length; len++) {
			if (destList.options[len] != null) {
				newDestList[len] = new Option(destList.options[len].text, destList.options[len].value);
				newDestList[len].setAttribute('title', destList.options[len].title);
			}
		}
		
		newDestList[len] = new Option(srcList.value, id.value);
		newDestList[len].setAttribute('title', id.value);
		
		// Populate the destination with the items from the new array
		for (var j = 0; j < newDestList.length; j++) {
			if (newDestList[j] != null) {
				destList.options[j] = newDestList[j];
			}
		}
		
		var remove_button = remove+num;
		document.getElementById(remove_button).style.visibility = "visible";
		var add_button = add+num;
		document.getElementById(add_button).style.visibility = "hidden";
		srcList.value = "";
	
	} // End of moveAuthor()
	
	function removeAuthor(srcList, number) {
		num = number;
		// Erase source list selected elements
		for(var i = srcList.options.length - 1; i >= 0; i-- ) { 
			if (srcList.options[i] != null && (srcList.options[i].selected == true)) {
				// Erase Source
				srcList.options[i] = null;
			}
		}
		var remove_button = remove+num;
		if(srcList.options.length == 0) {document.getElementById(remove_button).style.visibility = "hidden";}
	} // End of removeAuthor()
	
	function selectAuthors(srcList) {
		for(len = 0; len < srcList.options.length; len++) {
			srcList.options[len].selected = true;
		}
	}

/*****************************************************************************************/

	// Compare two options within a list by VALUE
	function compareOptionValues(a, b) {
		// Radix 10: for numeric values
		// Radix 36: for alphanumeric values
		var sA = parseInt(a.value, 36);
		var sB = parseInt(b.value, 36);
		return sA - sB;
	}
	
	// Compare two options within a list by TEXT
	function compareOptionText(a, b) { 
		// Radix 10: for numeric values
		// Radix 36: for alphanumeric values
		var sA = parseInt(a.text, 36);
		var sB = parseInt(b.text, 36);
		return sA - sB;
	}
	
	// Compare two options within a list by TITLE
	function compareOptionTitle(a, b) { 
		// Radix 10: for numeric values
		// Radix 36: for alphanumeric values
		var sA = parseInt(a.title, 36);
		var sB = parseInt(b.title, 36);
		return sA - sB;
	}
	
	// Dual list move function
	function moveDualList(srcList, destList, moveAll, doSort) {
	
		// Do nothing if nothing is selected
		if ((srcList.selectedIndex == -1) && (moveAll == false)) {
			return;
		}
	
		newDestList = new Array(destList.options.length);
		
		var len = 0;
		
		for(len = 0; len < destList.options.length; len++) {
			if (destList.options[len] != null) {
				newDestList[len] = new Option(destList.options[len].text, destList.options[len].value/*, destList.options[len].defaultSelected, destList.options[len].selected*/);
				newDestList[len].setAttribute('title', destList.options[len].title);
			}
		}
	
		for( var i = 0; i < srcList.options.length; i++ ) { 
			if (srcList.options[i] != null && (srcList.options[i].selected == true || moveAll)) {
				// Statements to perform if option is selected
				// Incorporate into new list
				newDestList[len] = new Option(srcList.options[i].text, srcList.options[i].value/*, srcList.options[i].defaultSelected, srcList.options[i].selected*/);
				newDestList[len].setAttribute('title', srcList.options[i].title);
				
				len++;
			}
		}
		
		if(doSort == true) {
			// Sort out the new destination list
			//newDestList.sort(compareOptionValues);   // BY VALUES
			//newDestList.sort(compareOptionText);   // BY TEXT
			newDestList.sort(compareOptionTitle);   // BY TITLE
		}
		
		// Populate the destination with the items from the new array
		for (var j = 0; j < newDestList.length; j++) {
			if (newDestList[j] != null) {
				destList.options[j] = newDestList[j];
			}
		}
		
		// Erase source list selected elements
		for( var i = srcList.options.length - 1; i >= 0; i-- ) { 
			if (srcList.options[i] != null && (srcList.options[i].selected == true || moveAll)) {
				// Erase Source
				//srcList.options[i].value = "";
				//srcList.options[i].text  = "";
				srcList.options[i] = null;
			}
		}
	
	} // End of moveDualList()
	//  End -->