/* products_search.js
 * by patrick schmider v1.0 000101
 * Copyright (c) 2000 Patrick Schmider. All Rights Reserved.
 * License to use is granted if and only if this entire
 * copyright notice is included.
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
*/


//---------------------------------------------------------
//---------------------------------------------------------
//---------------------------------------------------------
function search_and_output(ar,str,flds,show_flds,show_flds_names,sort_flds,search_msg,search_title,create_popup,lang) 
{
  /*
  ar = array of products to search in
  str = search string (posibly containg many search items
  flds = fields in 'ar[]to search in: ar[i][flds[j]]
  search_title = title of the search result window
  search_msg = message to show (e.g. "Matches found: ")
  show_flds = fiels of ar to show as part of the search results
  show_flds_names = names for the fields above (show_flds)
  sort_flds = optionally, list of fields to sort array (ar) -> search result also sorted !
  create_popup = 
  	1: write results to a blank popup window
  	0: return results in an array (see "result_array" beneath)
  search_items = str may consist of a different substrings,
      divided by a delimiter (blank); each substring is added to this array
  */
  
  /*
  this function uses "search_flds(...)" from beneath to...
  0) sorts the array (ar) to get alphabetically ordered matches
  1) get the matching fields index-numbers,
  2) create a new array containing all matching elements
  3) give out the search results in a new (popUp) window
  */
  
  //0) optionally
  if (sort_flds.length) {
    multi_field_sort(ar,sort_flds);
  }
  
  
  //1) + 2)
  var search_items_temp = str.split(" ");
  var search_items = new Array();
  //clean list from empty strings: ""
  var i = 0;
  var j = 0;
  for (i = 0; i<search_items_temp.length; i++) {
    if (search_items_temp[i] != "") {
      search_items[j] = search_items_temp[i];
      j++;
    }
  }
  if (search_items.length == 0) return;
  //  
  var search_results_indexs = search_array(ar, search_items, flds);
  var search_results = new Array();
  for (var i = 0; i < search_results_indexs.length; i++) {
    search_results[i] = ar[search_results_indexs[i]];
  }
    
  //2) Create string of search_items to show
  var group_indexs = [0, search_results.length-1];
  var group_name_string = search_msg + " ";
  for (var i = 0; i < search_items.length; i++) {
    if (i != 0) {
      group_name_string += (", ")
    }
    group_name_string += ("\"" + search_items[i] + "\"");
  }
  var group_name = [group_name_string];
  
  //3)
  if (create_popup) {
  	//Create window to show search results
  	//var popUp_url = "pop_up.htm";
  	//var popUp_name = "product_list";
  	var popUp_url = "../../products/html/prod_empty_pop_up.htm";
  	var popUp_name = "empty_product_pop_up";
	//
  	var search_popUp = window.open(popUp_url,popUp_name,'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=700,height=480');
  	//
  	write_output(search_popUp,search_results,group_indexs,group_name,show_flds,show_flds_names,search_title,lang);
  } else {
  	var result_array = new Array();
  	result_array[0] = search_results;
  	result_array[1] = group_indexs;
  	result_array[2] = group_name;
  	return result_array;
  }
  
} 




//---------------------------------------------------------
//---------------------------------------------------------
//---------------------------------------------------------
function search_array(ar, strs, flds)
{
  /*
  ar = array of products to search in
  strs = array of search strings
  flds = fields in 'ar[]to search in: ar[i][flds[j]]
  */
  
  /*
  function...
  - is NOT case sensitive (uses "...toLowerCase()...")
  - returns a list of matching index numbers,
    not the matching elements themselves !!!
  - each matching entry is only added once to the resulting array
  */
  
  var i,j,k;
  var results = new Array();
  var result_counter = 0;
  
  /*
  results = array of hit indexes
  results_counter = each new search hit is added to: 
      results[result_counter++]
  */
  
  for (i = 0; i<ar.length; i++) {
    for (j = 0; j<flds.length; j++) {
      for (k = 0; k<strs.length; k++) {
        if ( ar[i][flds[j]].toLowerCase().indexOf( strs[k].toLowerCase() ) != -1 ) {
              //store index of product in result array
              results[result_counter++] = i;
              //leave loop -> goto next article
              //btw: break would only leave nearest loop
              k = strs.length;
              j = flds.length;
        }
      }
    }
  }
  return results;
  
}

