var SetMenu="5_1";

function LTrim(str) {
 for (var i=0; str.charAt(i)<=" "; i++);
 return str.substring(i,str.length);
}

function RTrim(str) {
 for (var i=str.length-1; str.charAt(i)<=" "; i--);
 return str.substring(0,i+1);
}

function Trim(str) {
	var tempString;
	
	tempString = str;
	
	while (tempString.indexOf(" ") >= 0)
	{
		tempString = tempString.replace(" ", "");
	}

	if (tempString.length > 0)
	{
		return LTrim(RTrim(str));
	}else
		return "";
}

function do_search_key(event)
{
	var NS4 = (document.layers) ? true : false;
	var code = 0;
		
	if (NS4)
		code = event.which;
	else
		code = event.keyCode;
	
	if (code==13)
	{		
		return do_search();	
	}
}

function do_search_key_full(event)
{	
	var NS4 = (document.layers) ? true : false;
	var code = 0;
		
	if (NS4)
		code = event.which;
	else
		code = event.keyCode;

	
	if (code==13)
	{		
	
		event.returnValue=false;
        event.cancel = true;
		
		check_search();
	}
}

function check_search()
{	
    var searchtoken;
    searchtoken = Trim(document.getElementById("srchinput").value.toLowerCase());        
    bvalid = validateterm(searchtoken);
		
	if (bvalid == true){
		//window.parent.topFrame.document.location.reload();
	    document.searchform.srch.value = document.getElementById("srchinput").value;
	    document.searchform.submit();
	}else{		
		return false;
	}
	
}

function do_search()
{			
	var searchtoken;	
	document.quicksearch.ChunkStart.value = "";
	document.quicksearch.ChunkEnd.value = "";
	document.quicksearch.TimeDiff.value = "";
	document.quicksearch.OrderBy.value = "";
	document.quicksearch.SearchTerm.value = Trim(document.getElementById("SearchText").value);//document.quicksearch.SearchText.value;
	document.quicksearch.quicksub.value = "";
	document.quicksearch.quickpub.value = "";
	document.quicksearch.quickalpha.value = "";
	searchtoken = Trim(document.getElementById("SearchText").value.toLowerCase());//Trim(document.quicksearch.SearchText.value.toLowerCase());
	searchtoken = remove_noise(searchtoken);
	
	bvalid = validateterm(searchtoken);
		
	if (bvalid == true){
		document.quicksearch.submit();
	}else{		
		return false;
	}
}	

//function do_adv_search_key(event)
//{
//	var NS4 = (document.layers) ? true : false;
//	var code = 0;
//		
//	if (NS4)
//		code = event.which;
//	else
//		code = event.keyCode;
//	
//	if (code==13)
//	{		
//		return do_adv_search();		
//	}
//}

function do_adv_search(form)
{			
	var searchtoken;	
	searchtoken = form.andft.value.toLowerCase();
	if (Trim(searchtoken) != ""){
		if (searchtoken.indexOf("\"") >= 0){
			alert("Invalid Query. Please remove any quotes from your full text entry.")
			form.andft.focus();
			return false;}
		if (searchtoken.indexOf("'") >= 0){
			alert("Invalid Query. Please remove any quotes from your full text entry.")
			form.andft.focus();
			return false;}
		bvalid = validateterm(searchtoken)	
		if (bvalid == false){
			return false;}
	}	
	
	searchtoken = form.exactft.value.toLowerCase();	
	if (Trim(searchtoken) != ""){
		if (searchtoken.indexOf("\"") >= 0){
			alert("Invalid Query. Please remove any quotes from your full text entry.")
			form.exactft.focus();
			return false;
		}
		
		if (searchtoken.indexOf("'") >= 0){
			alert("Invalid Query. Please remove any quotes from your full text entry.")
			form.exactft.focus();
			return false;
		}
	
		bvalid = validateterm(searchtoken)
		if (bvalid == false){
			return false;
		}
	}
	
	searchtoken = form.orft.value.toLowerCase();	
	if (Trim(searchtoken) != ""){
		if (searchtoken.indexOf("\"") >= 0){
			alert("Invalid Query. Please remove any quotes from your full text entry.")
			form.orft.focus();
			return false;}
		if (searchtoken.indexOf("'") >= 0){
			alert("Invalid Query. Please remove any quotes from your full text entry.")
			form.orft.focus();
			return false;}
		bvalid = validateterm(searchtoken)	
		if (bvalid == false){
			return false;
		}
	}

	searchtoken = form.notft.value.toLowerCase();	
	if (Trim(searchtoken) != ""){
		if (form.orft.value == "" && form.exactft.value == "" && form.andft.value == ""){
			alert("Invalid Query. Please add another full-text search term. Cannot search on excluded words alone.")
			form.andft.focus();
			return false;
		}
		
		if (searchtoken.indexOf("\"") >= 0){
			alert("Invalid Query. Please remove any quotes from your full text entry.")
			form.notft.focus();
			return false;}
		if (searchtoken.indexOf("'") >= 0){
			alert("Invalid Query. Please remove any quotes from your full text entry.")
			form.notft.focus();
			return false;}
		bvalid = validateterm(searchtoken)	
		if (bvalid == false){
			return false;
		}
	}

//	selection = document.advsearch.subjectlist
///	if (selection.options.length > 0){
//		for (i = 0; i < selection.options.length; i++)
//		{
//			selection.options[i].selected = true;
//		}	
//	}	


	//searchtoken = document.advsearch.keyword.value.toLowerCase();	
	//if (Trim(searchtoken) != ""){
	//	bvalid = validateterm(searchtoken)	
	//	if (bvalid == false){
	//		return;
	//	}
	//}
	searchtoken = form.meshsubject.value.toLowerCase();	
	if (Trim(searchtoken) != ""){
		bvalid = validateterm(searchtoken)	
		if (bvalid == false){
			return false;
		}
	}
	

	//validate the year to ensure its not the default and is a number	
	if ((form.publishyear.value == "YYYY") || (form.publishyear.value == ""))
	{
		// for future 
	}
	else
	{
		if (isNaN(form.publishyear.value))
		{
			// its the default or its not a number so warn them, reset it, go to the field and cancel the submit
			alert("Please enter only numbers for the publish year");
			form.publishyear.value = "";
			form.publishyear.focus();
			return false;	
		}
		else
		{
			// it is a number so check it
			// make sure its in a year format
			if (form.publishyear.value.length != "4") 
			{
				// there are not the right anmount of numbers for a year value so state so, reset it, go to it and cancel the submit
				alert("Please enter only 4 numbers for the publish year");
				form.publishyear.value = "";
				form.publishyear.focus();
				return false;	
			}
			// by now its a number and of the proper size so check its within a range
			if ((form.publishyear.value < 1400) ||  (form.publishyear.value > 2050))
			{
				// its not in the right range, so say so, reset it, go to it and cancel the search
				alert("Please enter a valid year value");
				form.publishyear.value = "";
				form.publishyear.focus();
				return false;	
			}
		}	 
	}
	//it made it his far so the year is correct
	
	
	searchtoken = form.keywords.value.toLowerCase();	
	if (Trim(searchtoken) != ""){
		bvalid = validateterm(searchtoken)	
		if (bvalid == false){
			return false;
		}
	}
	
	searchtoken = form.title.value.toLowerCase();	
	if (Trim(searchtoken) != ""){
		bvalid = validateterm(searchtoken)	
		if (bvalid == false){
			return false;
		}
	}
	searchtoken = form.author.value.toLowerCase();	
	if (Trim(searchtoken) != ""){
		bvalid = validateterm(searchtoken)	
		if (bvalid == false){
			return false;
		}
	}
	searchtoken = form.isbn.value.toLowerCase();	
	if (Trim(searchtoken) != ""){
		bvalid = validateterm(searchtoken)	
		if (bvalid == false){
			return false;
		}
	}

    

//if(selectObj.options[i].value == letter)

	//searchtoken = document.advsearch.publisher.value.toLowerCase();	
	//if (Trim(searchtoken) != ""){
	//	bvalid = validateterm(searchtoken)	
	//	if (bvalid == false){
	//		return false;
	//	}
	//}
	
	//searchtoken = document.advsearch.subject.value.toLowerCase();	
	//if (Trim(searchtoken) != ""){
	//	bvalid = validateterm(searchtoken)	
	//	if (bvalid == false){
	//		return false;
	//	}
	//}
	
	return true;
}	

function validateterm(term)
{
	var searchtoken;
	var tempstring;
	
	searchtoken = term
	tempstring = searchtoken	
			
	if (tempstring == ''){
		alert("Search is empty. Please try again.")
		document.quicksearch.SearchText.focus();
		return false;
	}
			
	if (tempstring.length < 3){
		alert("Search must have at least 3 letters. Please try again.")
		document.quicksearch.SearchText.focus();
		return false;
	}
			
			
	// Check for invalid characters
	//tempstring = Trim(tempstring.replace(/[\w\u00C0-\u00FF\*\#\.\+\-\/\\\&" ]+?/gi,""));
	tempstring = Trim(tempstring.replace(/[\w\*\#\.\+\-\&" ]+?/gi,""));	
	tempstring = Trim(tempstring.replace("\u00C0",""));
	tempstring = Trim(tempstring.replace("\u00C1",""));
	tempstring = Trim(tempstring.replace("\u00C2",""));
	tempstring = Trim(tempstring.replace("\u00C3",""));
	tempstring = Trim(tempstring.replace("\u00C4",""));
	tempstring = Trim(tempstring.replace("\u00C5",""));
	tempstring = Trim(tempstring.replace("\u00C6",""));
	tempstring = Trim(tempstring.replace("\u00C7",""));		
	tempstring = Trim(tempstring.replace("\u00C8",""));
	tempstring = Trim(tempstring.replace("\u00C9",""));
	tempstring = Trim(tempstring.replace("\u00CA",""));
	tempstring = Trim(tempstring.replace("\u00CB",""));
	tempstring = Trim(tempstring.replace("\u00CC",""));
	tempstring = Trim(tempstring.replace("\u00CD",""));
	tempstring = Trim(tempstring.replace("\u00CE",""));
	tempstring = Trim(tempstring.replace("\u00CF",""));
	tempstring = Trim(tempstring.replace("\u00D0",""));
	tempstring = Trim(tempstring.replace("\u00D1",""));
	tempstring = Trim(tempstring.replace("\u00D2",""));
	tempstring = Trim(tempstring.replace("\u00D3",""));
	tempstring = Trim(tempstring.replace("\u00D4",""));
	tempstring = Trim(tempstring.replace("\u00D5",""));
	tempstring = Trim(tempstring.replace("\u00D6",""));
	tempstring = Trim(tempstring.replace("\u00D7",""));
	tempstring = Trim(tempstring.replace("\u00D8",""));
	tempstring = Trim(tempstring.replace("\u00D9",""));
	tempstring = Trim(tempstring.replace("\u00DA",""));
	tempstring = Trim(tempstring.replace("\u00DB",""));
	tempstring = Trim(tempstring.replace("\u00DC",""));
	tempstring = Trim(tempstring.replace("\u00DD",""));
	tempstring = Trim(tempstring.replace("\u00DE",""));
	tempstring = Trim(tempstring.replace("\u00DF",""));
	tempstring = Trim(tempstring.replace("\u00E0",""));
	tempstring = Trim(tempstring.replace("\u00E1",""));
	tempstring = Trim(tempstring.replace("\u00E2",""));
	tempstring = Trim(tempstring.replace("\u00E3",""));
	tempstring = Trim(tempstring.replace("\u00E4",""));
	tempstring = Trim(tempstring.replace("\u00E5",""));
	tempstring = Trim(tempstring.replace("\u00E6",""));
	tempstring = Trim(tempstring.replace("\u00E7",""));
	tempstring = Trim(tempstring.replace("\u00E8",""));
	tempstring = Trim(tempstring.replace("\u00E9",""));
	tempstring = Trim(tempstring.replace("\u00EA",""));
	tempstring = Trim(tempstring.replace("\u00EB",""));
	tempstring = Trim(tempstring.replace("\u00EC",""));
	tempstring = Trim(tempstring.replace("\u00ED",""));
	tempstring = Trim(tempstring.replace("\u00EE",""));
	tempstring = Trim(tempstring.replace("\u00EF",""));
	tempstring = Trim(tempstring.replace("\u00F0",""));
	tempstring = Trim(tempstring.replace("\u00F1",""));
	tempstring = Trim(tempstring.replace("\u00F2",""));
	tempstring = Trim(tempstring.replace("\u00F3",""));
	tempstring = Trim(tempstring.replace("\u00F4",""));
	tempstring = Trim(tempstring.replace("\u00F5",""));
	tempstring = Trim(tempstring.replace("\u00F6",""));
	tempstring = Trim(tempstring.replace("\u00F7",""));
	tempstring = Trim(tempstring.replace("\u00F8",""));
	tempstring = Trim(tempstring.replace("\u00F9",""));
	tempstring = Trim(tempstring.replace("\u00FA",""));
	tempstring = Trim(tempstring.replace("\u00FB",""));
	tempstring = Trim(tempstring.replace("\u00FC",""));
	tempstring = Trim(tempstring.replace("\u00FD",""));
	tempstring = Trim(tempstring.replace("\u00FE",""));
	tempstring = Trim(tempstring.replace("\u00FF",""));	
				
	if (tempstring != '') {
		alert(tempstring + " is invalid in search. Please try again.");	
		return false;
	}
					
	tempstring = searchtoken;
			
	// Check for balanced double quotes
	tempstring = tempstring.replace(/\"[^\"]+?\"/gi,"")	
				
	if (tempstring.indexOf('""') > -1) {		
		alert('Double quotes are empty. Please try again.');	
		return false;
	}
		
	if (tempstring.indexOf('"') > -1) {		
		alert('Double quotes are not balanced. Please try again.');	
		return false;
	}
		
	return true;
}

function remove_noise(phrase)
{
	//dimensionalizing variables
	noisearray=new Array();
	array_phrase=new Array();
	array_pruned=new Array();
	
	//phrase = " " + Trim(phrase.replace(/[\*\#\.\+\-\/\\\&\"]+?/gi," ")) + " ";
	//phrase = Trim(phrase.replace(/ [1|2|3|4|5|6|7|8|9|0|about|after|all|also|an|and|another|any|are|as|at|be|because|been|before|being|between|both|but|by|came|can|come|could|did|do|each|for|from|get|got|has|had|he|have|her|here|him|himself|his|how|if|in|into|is|it|like|make|many|me|might|more|most|much|must|my|never|now|of|on|only|other|our|out|over|said|same|see|should|since|some|still|such|take|than|that|the|their|them|then|there|these|they|this|those|through|to|too|under|up|very|was|way|we|well|were|what|where|which|while|who|with|would|you|your|a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z] /gi, " "));
	//alert(phrase);
	//return false;

	// Replace symbol characters here *****	

	var noisearray=new Array
	("about", "1", "after", "2", "all", "also", "3", "an", "4", "and", "5", "another", "6", "any", "7", "are", "8", "as", "9", "at", "0", "be", "$", "because", "been", "before", "being", "between", "both", "but", "by", "came", "can", "come", "could", "did", "do", "each", "for", "from", "get", "got", "has", "had", "he", "have", "her", "here", "him", "himself", "his", "how", "if", "in", "into", "is", "it", "like", "make", "many", "me", "might", "more", "most", "much", "must", "my", "never", "now", "of", "on", "only", "other", "our", "out", "over", "said", "same", "see", "should", "since", "some", "still", "such", "take", "than", "that", "the", "their", "them", "then", "there", "these", "they", "this", "those", "through", "to", "too", "under", "up", "very", "was", "way", "we", "well", "were", "what", "where", "which", "while", "who", "with", "would", "you", "your", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "*", "\"", "-", "About", "After", "All", "Also", "An", "And", "Another", "Any", "Are", "As", "At", "Be", "Because", "Been", "Before", "Being", "Between", "Both", "But", "By", "Came", "Can", "Come", "Could", "Did", "Do", "Each", "For", "From", "Get", "Got", "Has", "Had", "He", "Have", "Her", "Here", "Him", "Himself", "His", "How", "If", "In", "Into", "Is", "It", "Like", "Make", "Many", "Me", "Might", "More", "Most", "Much", "Must", "My", "Never", "Now", "Of", "On", "Only", "Other", "Our", "Out", "Over", "Said", "Same", "See", "Should", "Since", "Some", "Still", "Such", "Take", "Than", "That", "The", "Their", "Them", "Then", "There", "These", "They", "This", "Those", "Through", "To", "Too", "Under", "Up", "Very", "Was", "Way", "We", "Well", "Were", "What", "Where", "Which", "While", "Who", "With", "Would", "You", "Your", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "o", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z");
	
	var array_pruned_length=0;
	var counter=0;
	var counter1=0;
	var counter2=0;
	var phrase1;
	var length=0;
	var noise_array_counter=0;
	var search_phrase="";

	//spliting the search phrase into an array
	array_pruned=phrase.split(" ");
	array_pruned_length=array_pruned.length;
	for (counter=0;counter <array_pruned_length;counter++)
	{
	counter2=0;
	counter1=0;

		for (counter1=0;counter1<noisearray.length+1;counter1++)
		{
			if(array_pruned[counter]==noisearray[counter1])
			{
				search_phrase= " " + array_pruned[counter] + " ";
			    search_phrase_first_position= array_pruned[counter] + " ";
				counter2 = phrase.indexOf(search_phrase,0);
				counter3 = phrase.indexOf(search_phrase_first_position,0);
				if(counter3==0)
				{
					counter2=counter3;
				}

				while (counter2>=0)
				{			      
					temp_string=array_pruned[counter];
			      	length=temp_string.length;
			      	phrase = phrase.substr(0,counter2) + phrase.substr(counter2+length+1);
			      	counter2=phrase.indexOf(search_phrase,0);			      
					break;
				}
			}
		}
	}
	return phrase;
}

function MM_openBrWindow(theURL,winName,features) 
{
	window.open(theURL,winName,features);
}

function removesubjects(){
	selection = document.advsearch.subjectlist;
	
	if (selection.options.length == 0){
		alert('There are no subjects to remove');
		return false;
	}
	
	if (selection.selectedIndex == -1){
		alert('Please select a subject to remove');
		return false;
	}
	
	for (i = 0; i < selection.options.length; i++)
		{
			if (selection.options[i].selected == true){							
				selection.options[i] = null;
				i = i - 1;
			}
		}	
}