Вот продолжение
srch.parentNode.insertBefore(completionDiv, srch);
// Prevent select dropdowns showing thru
if (isIE) {
completionDiv.style.left = left + 10 + 'px';
completionDiv.style.top = top + 17 + 'px';
var iframe = document.createElement('iframe');
iframe.id = 'search_iframe';
iframe.style.position = 'absolute';
iframe.style.top = '0';
iframe.style.left = '0';
iframe.style.width = '0px';
iframe.style.height = '0px';
iframe.style.zIndex = '98';
iframe.style.visibility = 'hidden';
srch.parentNode.insertBefore(iframe, srch);
}
}
return true;
}
// Not used yet
function pattern2regexp(pattern){
pattern = pattern.replace(/\#/g, "\\#");
pattern = pattern.replace(/\$/g, "\\$");
pattern = pattern.replace(/\./g, "\\.");
pattern = pattern.replace(/\*/g, "[\\d\\S]*");
pattern = pattern.replace(/\?/g, ".?");
return pattern;
}
// Replaces special chars from query
function htmlspecialchars(query) {
query = query.replace(/\&/g, " ");
query = query.replace(/\</g, " ");
query = query.replace(/\>/g, " ");
query = query.replace(/\"/g, " ");
query = query.replace(/\'/g, " ");
query = query.replace(/\++/g, " ");
return query;
}
// Lets replace this long function
// with a smaller one
function print(text){
document.write(text);
}
// Form validation, checks if anything is entered for now.
function formatSearchQuery(){
if(document.searchForm.search.value.length == 0) return false;
return true;
}
// Submits search form
function submitform(){
document.searchForm.submit();
// history.go(0)
}
// Function is called when one of the category is selected.
// Adds Category text in the search box and submits form
function category(catID){
var form = document.searchForm;
var val = form.search.value;
var pos = val.indexOf(':');
form.search.value = aCat[catID] + ":" + trim(val.substr(pos+1));
submitform();
}
// Prints warning with the specified id
function printWarning(warningID){
var message = "";
switch(warningID){
case 1: message = WARNING_CATEGORY_NOT_FOUND;
}
print("<br><div class=\"warning-block\">" + message + "</div>");
}
// Creates div with categories
// Each category is in div too.
function printCategories(){
var i=0;
var cnt=0;
print("<div class=\"categories-block\">");
for (key in aCat){
i++;
}
print("<table border=\"0\"><tr><td>");
for (key in aCat){
cnt++;
if (cnt == i/2+1)
{
//print("</div>");
//print("<div class=\"categories-block2\">");
print("</td><td>");
}
print("<div class=\"cat-block\"><a class=\"cat-link\" href=\"javascript: category(\'"+key+"\')\">" + aCat[key] + "</a></div>");
}
print("</tr></table>");
print("</div>");
}
// Returns category index from it's name
function findCategoryIndex(catName){
if(catName.length == 0) return -1;
for(index in aCat){
if(catName == aCat[index]) return index;
}
return 0;
}
// Returns html with banners
function buildBanners(){
var banners = "";
var filename;
for(it in results){
// if client type "2"
if( aData[results[it][0]][0] == "2"){
filename = CLIENTS_FOLDER + results[it][0] + "/" + CLIENT_BANNER;
banners += "<a href=\""+ CLIENTS_FOLDER+ results[it][0] + "/" + CLIENT_LINK_TARGET + "\">"
+ "<object>" // width=\"1000\" height=\"400\"
+ "<param name=\"movie\" value=\"" + filename + "\">"
+ "<embed src=\"" + filename + "\">" // width=\"550\" height=\"400\"
+ "</embed>"
+ "</object>"
+ "</a>" + "<br>";
}
}
return banners;
}
// function returns relation coeficient for the specified
// customer data with specified search combination and category.
// The category should match exactly.
function getDataRel(data, searchString, cat){
if(cat != -1 && data[2] != cat) return 0;
var rel = 0;
// if name or keywords contains string exactly
if(data[3].toLowerCase().indexOf(searchString) != -1) rel += 1000;
else if(data[1].toLowerCase().indexOf(searchString) != -1) rel += 500;
else{
// split string into tokens and search for each token
var tokens = searchString.split(/[\,\s\.\?\!]+/);
var name = data[3].toLowerCase();
var keywords = data[1].toLowerCase();
for(var i = 0; i < tokens.length; i++){
if(name.indexOf(tokens[i]) != -1){
// add rel
rel += (tokens.length - i) * 20;
}
}
for(var i = 0; i < tokens.length; i++){
if(keywords.indexOf(tokens[i]) != -1){
// add rel
rel += (tokens.length - i) * 10;
}
}
}
// add correlation with client type
if(rel > 0){
rel += parseInt(400 - data[0] * 100);
}
return rel;
}
// function prints formatted seach result
function formatResult(index, id, data){
var result = index + ". ";
result += "<a href=\"" + CLIENTS_FOLDER+ id + "/" + CLIENT_LINK_TARGET +
"\" class=\"company-name\">" + data[3] + "</a> <br>";
result += data[4] + "<br>" + PHONE_NUMBER_PREFIX + data[5] + "<br>";
result += "<div class=\"descr-block\">" + aDescr[id] + "</div>";
return result;
}
// criteria for sorting search results
// Resulta are sorted by relation coficient for now.
function resultsSortCriteria(a, b){
return b[1] - a[1];
}
// searches and prints results for the specified search query
function printResult(searchQuery){
// divide into category and search string
var pos = searchQuery.indexOf(':');
var searchCategory = "";
var searchString = searchQuery;
if(pos > 0){
searchCategory = trim(searchQuery.substr(0, pos));
searchString = trim(searchQuery.substr(pos+1));
}
var category = findCategoryIndex(searchCategory);
if(category == 0) printWarning(1);
if(prevQuery != searchQuery){
prevQuery = searchQuery;
var rel = 0;
var lsearch = searchString.toLowerCase();
// form data to output
for(key in aData){
if((rel = getDataRel(aData[key], lsearch, category)) > 0){
results.push(new Array(key, rel));
}
if((MAX_SEARCH_RESULTS != -1) && (results.length > MAX_SEARCH_RESULTS)) break;
}
results.sort(resultsSortCriteria);
if(results.length == 0){
// error message: no data found
var errorDiv = document.createElement('div');
errorDiv.className = "error-block";
errorDiv.innerHTML = NO_DATA_FOUND_MESSAGE;
document.body.appendChild(errorDiv);
}else{
// out data
var mainDiv = document.createElement('div');
mainDiv.className = "results-block";
var bannerDiv = document.createElement('div');
bannerDiv.className = "banner-block";
bannerDiv.innerHTML = buildBanners();
var cntr = 1;
// print results
for(index in results){
var divElem = document.createElement('div');
divElem.className = "client-block";
divElem.style.background = colorsClientType[aData[results[index][0]][0]];
divElem.innerHTML = formatResult(cntr++, results[index][0], aData[results[index][0]]);
mainDiv.appendChild(divElem);
}
// append search results and banner blocks
document.body.appendChild(mainDiv);
document.body.appendChild(bannerDiv);
}
}
}
// inits search form
function initForm(searchQuery){
var divClass = isSearchStarted ? "search-block" : "standalone-search-block";
document.getElementById("divForm").setAttribute("class", divClass);
document.searchForm.search.value = searchQuery;
}
// puts search string into input box after returning on
// the search page from anywhere
function initsearch(){
var searchQuery = htmlspecialchars(getParam("search"));
document.searchForm.search.value = searchQuery;
}
// all functions called throught this method
function search(){
buildKeywords();
var searchQuery = htmlspecialchars(getParam("search"));
isSearchStarted = (trim(searchQuery) != "");
initForm(searchQuery);
if(!isSearchStarted) printCategories();
if(isSearchStarted){
printResult(searchQuery);
}
}