// insertAND does the basic function of inserting AND between terms for a keyword
// search and handles:
// Characters like &, | , and, or, not; all case insensitive wherever applicable
// The hypenated strings are inserted in quotes. eg: levi-strauss => "levi-strauss"
// Parantheses are addressed: All above should hold good inside parantheses in
// addition to being insensitive to spaces between (,) & any strings.

// Sample queries:
// var s="One two \"three four\" five six";
// var s = "xml ( databases | levi-strauss ) & \"computer\"\"science\"";


function insertAND(s)
{
// remove : and ; in the input query
/*
    var toremove = /[\:\;]/g;
    if( s.search(toremove) != -1 )
    {
        s.replace(toremove,"");
        alert( s );
    }
*/



// handle the parenthesis cases, we are forcing a space between brackets and terms.
    s = s.replace(/(\(|\))/g," $1 ");

// Pick out words from the input query. Treat phrases as a single word.
// used [a-zA-Z0-9_-] instead of \w since we need -&?(),etc

// Fix to allow diacritics in query
// var r=/(\"[^"]*\"|[a-zA-Z0-9_\-\)\(\?\&\']+)/g;
    var r=/(\"[^"]*\"|\S+)/g;
// end of diacritic fix

    s = s.replace(r,"$1#");
    a = s.split("#");
//      alert(a);
    len = a.length - 1;


// str will have the new modified query
    str = "";


// pword is to indicate if 'AND' needs to be inserted for a particular word.
// Eg: pword would be set for words such as and,or,not where in no 'AND' is needed.
    pword = 0;

// Parsing through each of the input words individually
    for(i=0;i<len;i++)
    {

//Test for words starting with quotes
        if(a[i].match(/^\s*"/))
        {
            if(i == 0 || pword == 1)
            {
                str = str + a[i];
                pword = 0;
            }
            else
            {
                str = str + " AND " + a[i];
            }
        }

// Implies words with no quotes
        else
        {
// Test if the word matches - and, or, not
            if (a[i].match(/^\s*and\s*$/i))
            {
                if (pword == 1) {}
                else
                {
                    str = str + "AND";
                    pword = 1;
                }
            }
            else if(a[i].match(/^\s*or\s*$/i))
            {
                if (pword == 1){}
                else
                {
                    str = str + "OR";
                    pword = 1;
                }
            }
            else if(a[i].match(/^\s*not\s*$/i))
            {
                str = str + "NOT";
                pword = 1;
            }
// If & exists by itself then combine the pre & post terms and treat as a phrase
            else if(a[i].match(/&/))
            {
                str = str.replace(/\s*([a-zA-Z0-9_\-\(\)\?]+)\s+$/," \"$1");
                str = str + " & " + a[i+1] + "\" ";
                i++;
            }
// Looking for some keywords
            else if (a[i].match(/(^\s*GKEY\s*$|^\s*IALL\s*$|^\s*ISBN\s*$|^\s*ISSN\s*$|^\s*JKEY\s*$|^\s*KPPD\s*$|^\s*LSUB\s*$|^\s*NKEY\s*$|^\s*NOTE\s*$|^\s*SERI\s*$|^\s*SKEY\s*$|^\s*TKEY\s*$|^\s*\d{3}[ABCKLNT]\s*$)/))
            {
                if (pword == 1 || i == 0)
                {
                    str = str + a[i];
                }
                else
                {
                    str = str + " AND " + a[i];
                }
                pword = 1;
            }
// Handling parentheses
            else if(a[i].match(/\(/))
            {
                if(pword == 1 || i == 0)
                {
                    str = str + " (";
                }
                else
                {
                    str = str + " AND" + " (";
                    pword = 1;
                }
                if (i == 0)
                {
                    pword = 1;
                }
            }
            else if(a[i].match(/\)/))
            {
                str = str + ")";
                pword = 0;
            }
// Look for hypenated terms and put them in quotes.
            else if(a[i].match(/-/))
            {
                if (pword == 1 || i == 0)
                {
                    str = str + "\"" + a[i] + "\"";
                    pword = 0;
                }
                else
                {
                    str = str + " AND " + "\"" + a[i] + "\"";
                }
            }
// all other cases
            else
            {
                if(i == 0 || pword == 1)
                {
                    str = str + a[i];
                    pword = 0;
                }
                else
                {
                    str = str + " AND " + a[i];
                }
            }
        }
// Closing else for no quote words
        str = str + " ";
    }
// closing For loop
// alert('New Query is: '+str);
//    document.querybox.Search_Arg.value = str;

    return ( str );
}


