Есть скрипт по поиску в базе данных но вот в чём проблема: когда идёт посик по базе )в ней существуют 4 группы ( так вот поиск должен отображать найденое по групам сначала 1 група потом 2 и так далее -а он собака отображает не по групам а по сходству с словом которое ищут!
// ====== global variables ======================
// array with search results
var results = new Array();
// search started flag
var isSearchStarted;
// Previouse search query, used to avoid search
// when getting back from some page
var prevQuery = "";
var keywords = new Array();
var currentKeywords = new Array();
var curKeywordIndex = -1;
var completionDiv;
// define browsers
var isIE = (navigator.userAgent.toLowerCase().indexOf("explorer") != -1) ||
(navigator.userAgent.toLowerCase().indexOf("msie") != -1); //document.all ? true : false;
var isOpera = navigator.userAgent.toLowerCase().indexOf('opera') != -1;
// This function will return the value of the spefcified GET parameter
function getParam(paramName){
var paramStr = document.location.search;
if (paramStr == "")
return "";
// remove '?' in front of paramStr
if (paramStr.charAt(0) == "?")
paramStr = paramStr.substr(1);
var arg = (paramStr.split("&"));
for (i=0; i < arg.length; i++) {
arg_values = arg[i].split("=")
if (unescape(arg_values[0]) == paramName)
{
if (USE_UTF8 == 1 && self.decodeURIComponent) // check if decodeURIComponent() is defined
ret = decodeURIComponent(arg_values[1]);
else
ret = unescape(arg_values[1]); // IE 5.0 and older does not have decodeURI
return ret;
}
}
return "";
}
// fast string trim
function trim(str) {
str = str.replace(/^\s+/, '');
for (var i = str.length - 1; i >= 0; i--) {
if (/\S/.test(str.charAt(i))) {
str = str.substring(0, i + 1);
break;
}
}
return str;
}
// gets left coord of given element
function getLeft(element){
var curNode = element;
var left = 0;
do{
left += curNode.offsetLeft;
curNode = curNode.offsetParent;
}while(curNode.tagName.toLowerCase() != 'body');
return left;
}
// gets top coord of given element
function getTop(element){
var curNode = element;
var top = 0;
do{
top += curNode.offsetTop;
curNode = curNode.offsetParent;
}while(curNode.tagName.toLowerCase() != 'body');
return top;
}
// initialization of keywords list
function buildKeywords(){
// if first run
if(keywords.length == 0){
for(index in aData){
kw = aData[index][1].split(",");
for(i in kw){
keywords[trim(kw[i])] = 1;
}
keywords[trim[aData[index][3]]];
}
// create keywords div
completionDiv = document.createElement("div");
completionDiv.setAttribute("id", "keywords");
completionDiv.className = "keywords-block";
document.searchForm.appendChild(completionDiv);
hideKeywords();
}
}
function hideKeywords(){
completionDiv.style.visibility = "hidden";
curKeywordIndex = -1;
}
function completeText(index){
if(completionDiv.style.visibility == "visible" &&
index != -1)
{
var srch = document.searchForm.search;
srch.value = currentKeywords[index];
}
}
function buildKeywordsBlock(){
var hintText = "";
for(i = 0; i < currentKeywords.length; i++){
var kwClass = (i == curKeywordIndex) ?
"selected-keyword-item" : "keyword-item";
hintText += "<div class=\"" + kwClass + "\" onclick=\"completeText("+i+");\">" + currentKeywords[i] + "</div>"
}
completionDiv.innerHTML = hintText;
}
// KeyPressed event handler prevents form submit on Enter
function preventEnter(e){
if (isIE) e = window.event;
if ((e.keyCode == 13 || isOpera) &&
completionDiv.style.visibility == "visible") return false;
}
// Function which handles the keypress event
function processKeyDown(e){
if (isIE) e = window.event;
var keyCode = e.keyCode;
switch (keyCode) {
// Return/Enter
case 13:
if(completionDiv.style.visibility != "visible"){
submitform();
}else{
completeText(curKeywordIndex);
hideKeywords();
}
e.returnValue = false;
e.cancelBubble = true;
break;
// Escape
case 27:
hideKeywords();
e.returnValue = false;
e.cancelBubble = true;
break;
// Up arrow
case 38:
if(curKeywordIndex > 0) curKeywordIndex--;
buildKeywordsBlock();
return false;
// Tab
case 9:
hideKeywords();
return;
// Down arrow
case 40:
if(curKeywordIndex < currentKeywords.length - 1)
curKeywordIndex++;
buildKeywordsBlock();
return false;
}
}
// Function which handles the keyup event
function processKeyUp(e){
// Mozilla
if (isIE) e = window.event;
var keyCode = e.keyCode;
switch (keyCode) {
case 13:
completeText(curKeywordIndex);
hideKeywords();
e.returnValue = false;
e.cancelBubble = true;
break;
case 27:
hideKeywords();
e.returnValue = false;
e.cancelBubble = true;
break;
case 38:
case 40:
return false;
default:
showKeywords();
break;
}
}
// Shows completion block containing keywords
// that begins with entered text
function showKeywords(){
var srch = document.searchForm.search;
var userInput = document.searchForm.search.value;
if(trim(userInput) == ""){
hideKeywords();
}else{
var counter = 0;
var left = getLeft(srch);
var top = getTop(srch) + srch.offsetHeight;
var width = srch.offsetWidth;
currentKeywords = new Array();
for(word in keywords){
if(word.indexOf(userInput) == 0){
currentKeywords.push(word);
counter++;
}
if((MAX_KEYWORDS != -1) && (counter > MAX_KEYWORDS)) break;
}
if(currentKeywords.length > 0){
completionDiv.style.visibility = "visible";
}else{
hideKeywords();
}
buildKeywordsBlock();
// Lets position it
completionDiv.style.left = left + 'px';
completionDiv.style.top = top + 'px';
completionDiv.style.width = width + 'px';
completionDiv.style.zIndex = '99';