function cqlise(){
 var operator = getSelectedOperator();

 var userQuery = document.getElementById("userquery").value;

 var finalString = "";

/*do not allow double quote, single quote or equals entered by itself*/
var RegExToMatchSingleProblematicCharacters = /^\s*("|'|=)\s*$/;
if(userQuery.match(RegExToMatchSingleProblematicCharacters)){
    userQuery = '"search undefined"';
}

 /*remove characters that cause errors in an SRU request*/
 var regExForDifficultCharacters =  /~|!|@|\^|\*|\(|\)|\{|\}|\[|\]|\/|\?|\+/g;
 userQuery = userQuery.replace(regExForDifficultCharacters, "");
 

 /*replace any instances of "" with " "*/
 userQuery = userQuery.replace(/""/g, '" "');


 /*find all cql type queries, conjoin with operator and save them to the final string,
 then remove them from the main userQuery strings*/
 var regExToMatchSimpleCQLQueryWithQuotes = /((\w+\.)?)(\b\w*\b)(\s*)=(\s*)"((\w*|\s*)*)"/g;
 var cqlQueriesArray = userQuery.match(regExToMatchSimpleCQLQueryWithQuotes);

 if(cqlQueriesArray){
  for(i=0; i < cqlQueriesArray.length; i++){
    finalString = finalString + cqlQueriesArray[i] + operator;
  }
 }

 userQuery = userQuery.replace(regExToMatchSimpleCQLQueryWithQuotes,"");



 /*find all groups of words in quotes, conjoin with operator and save them to the final string,
 then remove them from the main userQuery string*/
 var regExForWordsInQuotes = /"((\w*|\s*)*)"/g;
 var phrasesArray = userQuery.match(regExForWordsInQuotes);

 var i=0;

 if (phrasesArray){
  for(i=0; i < phrasesArray.length; i++){
    finalString = finalString + phrasesArray[i] + operator;
  }
 }

 userQuery = userQuery.replace(regExForWordsInQuotes,"");

 /*remove operators input by the user (not from phrases)*/
 userQuery = userQuery.replace(/\b(AND|OR|NOT)\b/gi, " ");

 userQuery = trim(userQuery); //remove trailing spaces

 /*split the remaining string where there are spaces and add in the appropriate operator*/
 var remainingStringAsArray = userQuery.split(/\s+/);


 if( remainingStringAsArray ){
  for(i=0; i < remainingStringAsArray.length; i++){
    if( i != (remainingStringAsArray.length -1)){
      finalString += remainingStringAsArray[i].replace('"', "") + operator;
   } else{        
    finalString += remainingStringAsArray[i].replace('"', "");    
   }
  }
 }

 finalString = finalString.replace(/\b(AND|OR|NOT)\b\s*$/i, ""); //removes trailing operators

 if (finalString.match(/^\s*$/)){
     finalString = '"empty term unsupported"';
 }

 document.getElementById("query").value = finalString;
}



function getSelectedOperator() {
 /*based on http://homepage.ntlworld.com/kayseycarvey/jss3p11.html*/
 var chosen = ""
 var len = document.sruSearchForm.operator.length

 for (i = 0; i <len; i++) {
  if (document.sruSearchForm.operator[i].checked) {
   chosen = document.sruSearchForm.operator[i].value
  }
 }

 return chosen;
}

function trim(string){
 //http://www.nicknettleton.com/zine/javascript/trim-a-string-in-javascript
 return string.replace(/^\s+|\s+$/g, '');
 }
