// selection of functions to assist in search form parameter collection and 
// creation of SOAP packet.

function formSelect(bool, fi, sl) {
// bool for special occasions - usually should be AND
// fi is the DC field name
// sl is element of select list

  return {boolean:bool, field:fi, value:sl.value};
}


function formFuzzySelect(bool, fi, sl) {
// bool for special occasions - usually should be AND
// fi is the DC field name
// sl is element of select list

  return {boolean:bool, field:fi, value:'%' + sl.value + '%'};
}

function formStartSelect(bool, fi, sl) {
// bool for special occasions - usually should be AND
// fi is the DC field name
// sl is element of select list

  return {boolean:bool, field:fi, value:sl.value + '%'};
}

function formBooksJournalsRadio(bool, rd) {
// bool for special occasions - usually should be AND
// no field in this one - it should be dc_identifier
// rd is element of radio group

  var booksJournals = 3;

  for (i=0;i<rd.length;i++){
     if (rd[i].checked==true){
        booksJournals=i;
        break; //exist for loop, as target acquired.
     }
  }

  switch (booksJournals) {
     case 0: // books only
            return { boolean:bool, field:"dc_identifier", value:'NOT NULL'};
            break;
     case 1: // journals only - no identifier
            return { boolean:bool, field:"dc_identifier", value:'NULL'};
            break;
     default: // books and journals - no special clauses
            return;
            break;
  }
}

function formKeywordTextbox (bool, fiL, tb) {
// bool for special occasions - usually should be AND
// fiL is the DC field name list
// tb is element of text box

  var ret = new Array ([]);

  // additional parameters from the keywords / phrase input
  if (tb.value != "") {
    var myKeywords = tb.value;

    // create keyword list from phrases and keywords
    var pkMode = 'K'; 
    var myKeylist = [];
    var myWord = '';

    for (var a = 0; a < myKeywords.length; a++) {
      var myChar = myKeywords.charAt(a);
      if ((pkMode == 'K') && (myChar == '"')) {
        if (myWord.length) { myKeylist.push(myWord); }
        myWord = '';
        pkMode = 'P';
        continue;
      }
      if ((pkMode == 'K') && (myChar == ' ')) {
        if (myWord.length) { myKeylist.push(myWord); }
        myWord = '';
        continue;
      }
      if ((pkMode == 'P') && (myChar == '"')) {
        if (myWord.length) { myKeylist.push(myWord); }
        myWord = '';
        pkMode = 'K';
        continue;
      }
      myWord = myWord + myChar;
    }
    if (myWord.length) { myKeylist.push(myWord); }

    var myFieldList = fiL.split(" ");

    // firefox does not like pushing in the first - creates a dummy entry
    ret[0] = { boolean:bool, field:"SUB", value:"IN"};

    for (var i in myKeylist) {
      if (i == 0) {
        ret.push({ boolean:"", field:"SUB", value:"IN"});
      }
      else {
        ret.push({ boolean:"AND", field:"SUB", value:"IN"});
      }
      for (var j in myFieldList) {
          ret.push({ boolean:"OR", field:myFieldList[j], value:'%' + myKeylist[i] + '%'});
      }
      ret.push({ boolean:"", field:"SUB", value:"OUT"});
    }

    ret.push({ boolean:"", field:"SUB", value:"OUT"});
  }
  return ret;
}

