function wordSearch() {
  // tarvitaan AND:it sanat1- ja sanat2-muuttujiin välilyöntien paikoille
  document.haku.ftquery.value = "";
  andTerm = document.haku.tekstikaikilla.value;
  phraseTerm = document.haku.tekstilause.value;
  orTerm = document.haku.tekstimilla.value;
  notTerm = document.haku.tekstiilman.value;

  if(andTerm != '')
    document.haku.ftquery.value="("+replaceWhitespaceWith(andTerm, " AND ")+")";
  if(phraseTerm != '')
    document.haku.ftquery.value+=(andTerm != '' ? " AND " : "")+"\""+phraseTerm+"\"";
  if(orTerm != '')
    document.haku.ftquery.value+=(andTerm != '' || phraseTerm != '' ? " AND " : "")+"("+replaceWhitespaceWith(orTerm, " OR ")+")";
  if((notTerm != '') && (andTerm != '' || orTerm != '' || phraseTerm != ''))
    document.haku.ftquery.value+=" AND NOT ("+replaceWhitespaceWith(notTerm," OR ")+")";
    document.haku.ftquery.value=editColon(document.haku.ftquery.value);
    document.haku.ftquery.value=expandSearch(document.haku.ftquery.value);
  // doSubmit();
}


function proximitySearch() {
  var proximity;

  if(document.haku.tekstisisalla.value=='') return;
  if(document.haku.tekstivali.value=='') proximity = 0;
  else proximity = document.haku.tekstivali.value;

  document.haku.ftquery.value="(\""+document.haku.tekstisisalla.value+"\"~"+proximity+")";
  // doSubmit();
  }

function phraseSearch() {
  if(document.haku.tekstilause.value=='') return;
  document.haku.ftquery.value="\""+document.haku.tekstilause.value+"\"";
  //doSubmit();
}


function replaceWhitespaceWith(str, w){
  var retval="";
  var justReplaced=false;
  for(var i=0; i<str.length; i++){
    if(str.charAt(i)==' '){
      if(!justReplaced){
        justReplaced=true;
        retval+=w;
      }
    }else{
      justReplaced=false;
      retval+=str.charAt(i);
    }
  }
  return retval;
}

function stripWhiteSpace(str){   
    var i;
    var returnString = "";

    for (i = 0; i < str.length; i++)
    {   
        var c = str.charAt(i);
        if (c!=' ') returnString += c;
    }

    return returnString;
}

function replaceChar(str,chr,rep){
    var i;
    var returnString = "";

    for (i = 0; i < str.length; i++)
    {
        var c = str.charAt(i);
        if (c==chr) returnString += rep;
	else returnString += c;
    }

    return returnString;
}

