//search functions
var tag = "";
var mode = "filter";		//'highlight' or 'filter'
//var mode = "highlight";		//'highlight' or 'filter'

function docsSearchKeyPress(e,source) {
	var KeyID = (window.event) ? event.keyCode : e.keyCode;
	if (KeyID == 13) docsSearchResultsGet(source);
}

function docsSearchResultsGet(source) {
	tag = trim(document.getElementById('docsSearchTag').value);
	var params = 'urlkey=' + encodeURIComponent(searchURLParams) + '&media=' + source + '&tag='+encodeURIComponent(tag);
	var result = rpc_Call('docsSearchButton','Search',params,'docsSearchResultsHighlight');
}

function docsSearchResultsHighlight(xmlDoc) {
	//highlight the documents matched

	var listDocumentIDs = rpc_SingleResult(xmlDoc,"documentids");

	//highlight the documents that match
	if (document.getElementsByTagName) {  
		
		//initialise 
   		var table = document.getElementById("tb10");   
   		var rows = table.getElementsByTagName("tr");   
		var currentHeadRow = -1;
   		var bSectionDocumentMatch = false;
		var tmp = "";
		listDocumentIDs = "," + listDocumentIDs + ",";
		
		//loop over each table row
		for (i = 0; i < rows.length; i++) {

   			//check if a heading row, if so, then keep a note of it
			if (rows[i].id == "head") {
				if (currentHeadRow != -1) {
					//if no rows from the previous group are displayed, then hide the heading row
					tmp = docsSearchResultsHighlight_Head(rows,currentHeadRow,bSectionDocumentMatch);
				}
				//reset heading checks for next section
				currentHeadRow = i;
				bSectionDocumentMatch = false;
			}

			//set the row display
    		if (rows[i].className != "blank") {
	    		if ((listDocumentIDs.indexOf("," + rows[i].id + ",") > -1) || (tag == '')) {
					//document match so display it as a match
					if (mode == "filter" || tag == '') {
			   			rows[i].className = docsSearchResultsHighlight_RowClass(i); 
					} else if (mode == "highlight") {
						rows[i].className = "match"; 
					}
					bSectionDocumentMatch = true;
				} else {
					//no match so do not display as a match
					if (mode == "filter") {
						rows[i].className = "hiddenSection";
					} else if (mode == "highlight") {
			   			rows[i].className = docsSearchResultsHighlight_RowClass(i); 
					} 
				}
				//attribute the row with its default className
				rows[i].setAttribute('origCl',rows[i].className);
				rows[i].origCl = rows[i].className;
			}
		
		}
		//final check for display of last heading row
		tmp = docsSearchResultsHighlight_Head(rows,currentHeadRow,bSectionDocumentMatch);
	}
	//reset the tag
	tag = '';
}

function docsSearchResultsHighlight_Head(rows,i,bSectionDocumentMatch) {
	var classname = "blank";
	//check if the heading row should be hidden
	if (mode == "filter") {
		if (!bSectionDocumentMatch) {
			classname = "hiddenSection"; 
		}
	}
	//set the class for the heading row
	rows[i].className = classname;
	if (i > 0) {
		rows[i-1].className = classname; 
	}
}

function docsSearchResultsHighlight_RowClass(i) {
	if (i % 2 == 0) { 
		return "even"; 
   	} else { 
    	return "odd"; 
	}
}