var menu = new Array();
function addMenu(_i) {
  var _next = menu.length?menu.length:0;
  menu[_next] = new Array();
  menu[_next][NOMBEDI]=database[_i][NOMBEDI];

  }

// this function takes these arguments
// 1> pattern to search for - deliminated by spaces.
// 2> true for OR
// 3> true for AND
// 4> true for EXACT
// return value is the index found or -1 if none found
function findThis(_searchfor,_and,_or) {
  var minusculas = _searchfor.toLowerCase();	
  //var S = _searchfor.split(" ");
  var S = minusculas.split(" ");
  for(var i=0;i<database.length;i++) {
    if(_or) {
	  for (var j=0;j<S.length;j++) {
	    if (database[i][NOMBEDI].indexOf(S[j])      != -1 ) {
		  addMenu(i);
		}
	  }
	} else if(_and) {
	  // AND
	  var count=0;
	  for (var j=0;j<S.length;j++) {
	    if (database[i][NOMBEDI].indexOf(S[j])      != -1 ) {
	      count++;
		}
	  }
	  //alert(count+"\n"+S.length);
	  if (count==S.length) {
	    addMenu (i);
	  }
	} else if (_exact) {
	  //alert("["+_searchfor+"]\n["+database[i][DESCRIPCION]+"]")
	  if (database[i][NOMBEDI].indexOf(minusculas)      != -1 ) {
		addMenu(i);
	  }
	}
  }
  return (-1);
}

// this function ENTRY
// is called when the new search is requested.
function entry() { 
  if ((document.entryform.keyword.value.length == 0) || (document.entryform.keyword.value == " ")) { 
    alert("Escriba la palabra o frase a buscar."); 
    return false; 
  } 
  and_search = (document.entryform.and_or.selectedIndex == 0?"or":"and"); 
  //if (document.entryform.and_or.selectedIndex == 2) 
    //and_search = "exact"; 
  location.href = location.pathname + "?"+ escape(document.entryform.keyword.value) + (and_search != "and"?"&"+and_search:""); 
  return false; 
}

var string="";
var and_search="";
function parseParms() {
  var _l1 = document.location.toString().split("?");
  var _l2 = "";
  var _l3 = "";
  if (_l1.length>=1)
    _l2 = _l1[1];
  if (_l2 && _l2.length>0) {
    if (_l2.indexOf("&") != -1) {
	  _l3 = _l2.split("&")[1];
	  _l2 = _l2.split("&")[0];
	}
  }
  string = unescape(_l2 || "");
  and_search= unescape(_l3 || "and");
}
parseParms();
if (string && string != "") findThis(string,and_search=="and",and_search=="or");
var _a  = '';
    _a += '<form name="entryform" onSubmit="return entry()">';
	_a += '<TABLE border="0" ><TR>'
	_a += '<td><input type="text" size=30 name="keyword" value="'+string+'"> ';
	_a += '<input type="button" value="Buscar" onClick="entry()"><br>';
	_a += '<select name="and_or" size=2>';  	
  	_a += '<option'+(and_search=="or"?" selected":"")+'>Que contenga alguna de las palabras (OR)';
	_a += '<option'+(and_search=="and"?" selected":"")+'>Que contenga todas las palabras (AND)';
	_a += '</select></td>';
	_a += '<TD><FONT FACE="VERDANA, ARIAL" SIZE="2" COLOR="#4A5ACE"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Instrucciones de uso</b></font><br><ul><li>';
	_a += '<FONT FACE="VERDANA, ARIAL" SIZE="2" COLOR="#4A5ACE">Puede buscar mas de una editorial separadas por un espacio indicando la opción OR.</li><li> No escriba acentos, comillas simples o dobles.</font></li></lu></td>';
	_a += '</TR></TABLE>'
	_a += '</form>';
document.write(_a);
if (location.search.length > 1)
document.write('<FONT FACE="VERDANA, ARIAL" SIZE="2" COLOR="#4A5ACE"><b>Resultados:</b><br><br>'); 
for (n=0; n<menu.length; n++) {
  var _a  = '';
      _a += "   > "+menu[n][NOMBEDI].toUpperCase()+"<br> ";

//alert("n=" + n);
if (n == 0)
{
	document.write(_a);
}
else 
{
	//alert(menu[n][NOMBEDI] + " " + menu[n-1][NOMBEDI]);

	if (menu[n][NOMBEDI] == menu[n-1][NOMBEDI])
	{
		//alert(" no escribo");
	}
	else
	{
		document.write(_a);
	}
}
  
}
document.write("</font>");
if ((menu.length == 0) && (location.search.length > 1)) 
  document.write('<FONT FACE="VERDANA, ARIAL" SIZE="2" COLOR="#4A5ACE">Editorial "'+string+'" no encontrada\n</font>'); 

if (n > 5){
  var _pie  = '';
      _pie += '<CENTER><A HREF="buscar.htm#Inici"><IMG SRC=graf/f_up.gif BORDER=0></CENTER>';
	   document.write(_pie);
}