// Define global variables
var SEARCHANY     = 1;
var SEARCHALL     = 2;
var SEARCHURL     = 4;
var searchType  = '';
var showMatches   = 10;
var currentMatch  = 0;
var copyArray   = new Array();
var docObj      = document;
var searchHTML;
var profiles = new Array();

//change grade

function changeGrade() {
	whichGrade = document.forms[0].grade_id.selectedIndex;
	if (whichGrade == 0) {
		profiles = grade1;
	} else if (whichGrade == 1) {
		profiles = grade2;
	} else if (whichGrade == 2) {
		profiles = grade3;
	}
}

// Determine the type of search, and make
// sure the user has entered something

function validate(entry) {

	if (entry.charAt(0) == "+") {
    	entry = entry.substring(1,entry.length);
    	searchType = SEARCHALL;
    } else if (entry.substring(0,4) == "url:") {
    	entry = entry.substring(5,entry.length);
    	searchType = SEARCHURL;
    }  else { 
    	searchType = SEARCHANY; 
    }

    while (entry.charAt(0) == ' ') {
    	entry = entry.substring(1,entry.length);
    	document.forms[0].query.value = entry;
    }

  	while (entry.charAt(entry.length - 1) == ' ') {
    	entry = entry.substring(0,entry.length - 1);
    	document.forms[0].query.value = entry;
    }

  	if (entry.length < 3) {
    	alert("You cannot search strings that small. Elaborate a little.");
    	document.forms[0].query.focus();
    	return;
    }
    
  	convertString(entry);
}

// Put the search terms in an array and
// and call appropriate search algorithm

function convertString(reentry) {
	var searchArray = reentry.split(" ");
	
	if (searchType == (SEARCHANY | SEARCHALL)) { 
		requireAll(searchArray); 
	} else { 
		allowAny(searchArray);
	}
}

// Define a function to perform a search that requires
// a match of any of the terms the user provided

function allowAny(t) {
	//profiles = grade1;
	var findings = new Array(0);
	
	for (i = 0; i < profiles.length; i++) {
    	var compareElement  = profiles[i].toUpperCase();
    
    	if (searchType == SEARCHANY) { 
    		var refineElement  = compareElement.substring(0,compareElement.indexOf('|TASK'));
    	} else { 
    		var refineElement = compareElement.substring(compareElement.indexOf('|TASK'), compareElement.length); 
    	}
    
    	for (j = 0; j < t.length; j++) {
    		var compareString = t[j].toUpperCase();
	
	    	if (refineElement.indexOf(compareString) != -1) {
        		findings[findings.length] = profiles[i];
		        break;
        	}
      	}
    }
	verifyManage(findings);
}

// Define a function to perform a search that requires
// a match of all terms the user provided
function requireAll(t) {
	var findings = new Array();
	
	for (i = 0; i < profiles.length; i++) {
    	var allConfirmation = true;
    	var allString = profiles[i].toUpperCase();
	    var refineAllString = allString.substring(0,allString.indexOf('|TASK'));
    
    	for (j = 0; j < t.length; j++) {
    		var allElement = t[j].toUpperCase();
    		if (refineAllString.indexOf(allElement) == -1) {
        		allConfirmation = false;
        		continue;
        	}
      	}
    
    	if (allConfirmation) {
      		findings[findings.length] = profiles[i];
      	}
    }
    //alert(finding);
  	verifyManage(findings);
}

// Determine whether the search was successful
// If so print the results; if not, indicate that, too

function verifyManage(resultSet) {
	
	if (resultSet.length == 0) { 
		noMatch(); 
	} else {
    	copyArray = resultSet.sort();
    	formatResults(copyArray, currentMatch, showMatches);
    }
}

// Define a function that indicates that the returned no results

function noMatch() {
	searchHTML = '<FONT FACE=Arial><B>"' +
    			 document.forms[0].query.value +
    			 '" returned no results. <p> Please be aware that this search system does not support quotes as in "search terms."';
	
	replaceHTML();    			 
    			 
}

// Define a function to print the results of a successful search

function formatResults(results, reference, offset) {

	var currentRecord = (results.length < reference + offset ? results.length : reference + offset);
	//docObj.open();
	
	searchHTML = 'Search Query: <I>' + document.forms[0].query.value + '</I><BR><BR>' +
    			 //'Search Results: <I>' + (reference + 1) + ' - ' + currentRecord + ' of ' + results.length + '</I><BR><BR></FONT>' + 
    			 '<FONT FACE=Arial><B>' + '\n\n<!-- Begin result set //-->\n\n\t\n';

	if (searchType == SEARCHURL) {
		for (var i = reference; i < currentRecord; i++) {
			var divide = results[i].split("|");
			searchHTML = searchHTML + '<p>' + '<A HREF="' + divide[2] + '">' + divide[2] + '</A>' + '<br>' + divide[1] + '</P>\n\n';
		}
	} else {
		for (var i = reference; i < currentRecord; i++) {
			var divide = results[i].split('|');
			searchHTML = searchHTML + '<p>' + '<A HREF="' + divide[2] + '">' + divide[0] + '</A>' + '<br>' +  divide[1] + '</P>\n\n';
		}
    }

    searchHTML = searchHTML + prevNextResults(results.length, reference, offset);
        
    //if (isMac == true && isExp == true) {
    //	alert("Internet Explorer Users\n\nPlease press enter or click 'OK' to continue your search.\n\nThank you.");
    //	replaceHTML();
	//} else {
		replaceHTML();
	//}
    
   	//node = document.getElementById('search');
	//node.innerHTML = searchHTML;
       
}

function replaceHTML() {
	node = document.getElementById('searchRep');
	//setTimeout('node.innerHTML = searchHTML', 100);
	node.innerHTML = searchHTML;
}

// Define a function to dynamically display Prev and Next buttons

function prevNextResults(ceiling, reference, offset) {
	var prevNextOutput = '<CENTER><FORM>';
	if(reference > 0) {
		prevNextOutput = prevNextOutput + '<INPUT TYPE=BUTTON VALUE="Prev ' + offset + ' Results" ' + 'onClick="formatResults(copyArray, ' + 
		(reference - offset) + 
		', ' + 
		offset + 
		')">';
    }
	if(reference >= 0 && reference + offset < ceiling) {
    	var trueTop = ((ceiling - (offset + reference) < offset) ? ceiling - (reference + offset) : offset);
    	var howMany = (trueTop > 1 ? "s" : "");
    	prevNextOutput = prevNextOutput + '<INPUT TYPE=BUTTON VALUE="Next ' + trueTop + ' Result' + howMany + '" ' +
    				   'onClick="formatResults(copyArray, ' +
    				   (reference + offset) + ', ' + offset + ')">';
    }
	prevNextOutput = prevNextOutput + '</FORM></CENTER>';
	return prevNextOutput;
}