/*
Created 10/05/2004 Tatyana Sherepa
Last modified 15/06/2004 Tatyana Sherepa
15/08/2004 Tatyana Sherepa Uppercase issue resolved
27/10/2004 Tatyana Sherepa  Added function replaceSymbolToBlank
Simplified function strBool because of uselessness of "()"
*/

/*Delete left spaces from string*/
function ltrim(sWhere){
var k = sWhere.length;
for(i = 0; i < sWhere.length; i++){
    if (sWhere.charAt(i)!=" "){
			 k = i;
			 break;
		}	 		  
}
sWhere = sWhere.substring(k, sWhere.length);
return sWhere;
}

/*Delete right spaces from string*/
function rtrim(sWhere){
var k = 0;
for(i = sWhere.length - 1; i >=0; i--){
    if (sWhere.charAt(i)!= " "){
			 k = i;
		   break;
		}	 	
}	  
sWhere = sWhere.substring(0, k + 1);
return sWhere;
}

/*Replace piece of the string on another one */

function replaceStr(sWhere, sWhat, sInstead, all){
var index = sWhere.indexOf(sWhat);
while (index != -1) {
  sWhere = sWhere.substring(0, index ) + sInstead + sWhere.substring( index + sWhat.length , sWhere.length);
	if (!all) index += sInstead.length; 
  index = sWhere.indexOf(sWhat, index);
}
return sWhere;
}

/*Replace symbols(one by one) from the defined string to blanks */

function replaceSymbolToBlank(sWhere, sWhat){
for(i = 0; i < sWhat.length ; i++){
		sWhere = replaceStr(sWhere, sWhat.charAt(i), " ", false);
}
return sWhere;
}

/*If the string contains keyword*/
function checkStr(sWhere){
if (ltrim(sWhere) == "") return -1; 
sWhere = sWhere.toLowerCase();
var alphabet = new String("abcdefghijklmnopqrstuvwxyzàáâãäå¸æçèéêëìíîïðñòóôõö÷øùüúýþÿ³¿º");
alphabet += "0123456789";
for(i = 0; i < sWhere.length; i++){
		if (alphabet.indexOf(sWhere.charAt(i)) != -1)
			 return 0;
}
return -2;
}

/*Form correct boolean search expression
boolDefOperation: AND|OR
rootDef: true - add KW+$
oneLetter: true - add one letter+$
*/
function strBool_Index(strInput, boolDefOperation, rootDef, oneLetter){

var tmpStr = new String("");

tmpStr = ltrim(strInput);

tmpStr = replaceStr(tmpStr, "+", " or ", false);
tmpStr = replaceStr(tmpStr, "*", " and ", false);

tmpStr = replaceStr(tmpStr, "  ", " ", true);
tmpStr = replaceStr(tmpStr, "$$", "$", true);

tmpStr = replaceStr(tmpStr, " or or ", " or ", false);
tmpStr = replaceStr(tmpStr, " and and ", " and ", false);
tmpStr = replaceStr(tmpStr, " or and ", " or ", false);
tmpStr = replaceStr(tmpStr, " and or ", " or ", false);

tmpStr = replaceSymbolToBlank(tmpStr,".-/\:;,'=()");

tmpStr = ltrim(tmpStr);
tmpStr = rtrim(tmpStr);

if (tmpStr.length == 0) return "";

if ((boolDefOperation.toLowerCase()!= "and")&&(boolDefOperation.toLowerCase()!= "or"))
	 boolDefOperation = "and";
boolDefOperation = boolDefOperation + " ";
	 
var allKW = tmpStr.split(" ");

var partKW = new String("");
var mainStr = new String("");

for(i = 0; i < allKW.length; i++){
		partKW = allKW[i];
		if (partKW.length == 0)
			 continue;
			 
	 if ((partKW.toLowerCase() != "or")&&(partKW.toLowerCase() != "and")&&
	 (partKW != "(")&&(partKW != ")")){
    		 if ((oneLetter&&(partKW.length == 1))||
      	     (rootDef&&(partKW.CharAt(partKW.length - 1) != "$"))) partKW += "$";
      	 
     		partKW += "$ "; //To identify field where the search will be
				 
				 if(i + 1 <= allKW.length)
  				 if ((allKW[i+1]!= null)&&
					     (allKW[i+1].toLowerCase() != "or")&&(allKW[i+1].toLowerCase() != "and")&&
							 (allKW[i+1]!= ")"))
  				 		partKW += boolDefOperation;
		}
		
		if (partKW == ")")
  				 if ((allKW[i+1]!= null)&&
					     (allKW[i+1].toLowerCase() != "or")&&(allKW[i+1].toLowerCase() != "and")&&
							 (allKW[i+1]!= ")"))
  				 		partKW = partKW + " " + boolDefOperation;
		 
		if (i + 1 < allKW.length) partKW +=" ";	
    mainStr +=partKW;						 		 
		
} 
//alert(mainStr);
return mainStr;
}

function strBool(strInput, boolDefOperation, rootDef, oneLetter){

//strInput = replaceSymbolToBlank(strInput,".");
strInput = strBool_Index(strInput, boolDefOperation, rootDef, oneLetter);

return strInput;
}