//| parseSearch.js (Angelo Hornak)
//|------------------------------------------------------------------------------
//| Copyright 2004 by Mark Anderson for Shoantel Limited
//| Site's catalogue content is copyright Angelo Hornak, templates and support
//| JS/NP files are copyright Shoantel (except where attributed) and  my not be
//| copied or re-used without prior permission
//|------------------------------------------------------------------------------
//| Language               : JavaScript
//| Author                 : Mark Anderson (mark@yeardley.demon.co.uk)
//|------------------------------------------------------------------------------
//| Description:
//|   Defines utility data variables/functions for Portfolio NetworkPublish site.
//|
//| History:
//|   28 Jul 2004          : 1.0 (Release)
//|   26 Jan 2004          : 1.01
//|   26 Jan 2004          : 1.02 (parseSearchNP - Safari issue) Current version.
//|------------------------------------------------------------------------------
//*

function parseSearch(aForm){
 theClause = 1;
 theJoin = "and";
 theOp = "starts with";
 // Break the list into keywords
 aKey = aForm.parseList.value.split(" ");
 for(i in aKey){
  switch(aKey[i]){
   case "":  //Two space together--ignore it.
     theClause--;
     break;
   case "and":  //An AND join. Set the join.
     theClause--;
     theJoin = "and";
     break;
   case "or":  //An OR join. Set the join.
     theClause--;
     theJoin = "or";
     break;
   case "not":  //A NOT operator. Set the operator.
     theClause--;
     theOp = "does not start with";
     break;
   case "AND":  //An AND join. Set the join.
     theClause--;
     theJoin = "and";
     break;
   case "OR":  //An OR join. Set the join.
     theClause --;
     theJoin = "or";
     break;
   case "NOT":  //A NOT operator. Set the operator.
     theClause --;
     theOp = "does not start with";
     break;
   default:  //A keyword. Set the clause and the join.
     aForm.elements[((theClause*4)-2)].value=aKey[i];
     aForm.elements[((theClause*4)-3)].value=theOp;
     if (theClause != 1){  //There is no Join1 field.
      aForm.elements[((theClause*4)-5)].value=theJoin;
     }
     theJoin = "and";
     theOp = "starts with";
     break;
  }
  if(++theClause==11){  //Exceeded the 10 clause limit
   alert('PortWeb can accept ten arguments (words), all words after ""' + aKey[(i-1)] + '" will be ignored.');
   break;
  }
 }
 return;
}

// Configurable parsed search code
// Specimen call: [args - parseSearchNP(aForm,myAndOr,myOp,myNotOp,lastTerm)]
// parseSearchNP(document.formName,'AND','starts with','does not start with', 40);
// ..or..
// parseSearchNP(document.formName,,'OR','contains','does not contain',40);
// lastTerm is the 0-based count of the input element following 'find',
//   which in turn follows the last 'value' element & parseList box. For all 10 inputs it is 40.
//   for 1-10 sets the number = (numOfSets * 4), e.g. for 1 set lastTerm = 4
function parseSearchNP(aForm,myAndOr,myOp,myNotOp,lastTerm){
// NOTE, it seems op and join values are not case sensitive in NP - but Field names are
  theClause = 1;
  theJoin = myAndOr;
  theOp = myOp;
  theNotOp = myNotOp;
  theQuestion ='';
  ///// ALT PARSING - STRIP QUOTES //////
  var parseString = aForm.parseList.value;
  parseString = parseString.replace(/\'/g,'');
  parseString = parseString.replace(/\"/g,'');
  var aKey = parseString.split(" ");
  //// ORIGINAL CODE /////////////
  //var aKey = aForm.parseList.value.split(" ");
  ////End ALT Section
  for(i = 0; i < aKey.length; i++){
    switch(aKey[i]){
      case "":     //Two spaces together--ignore it.
        theClause--;
        break;
      case "and":     //An AND join. Set the join.
        theClause--;
        theJoin = "AND";
        break;
      case "or":     //An OR join. Set the join.
        theClause--;
        theJoin = "OR";
        break;
      case "not":     //A NOT operator. Set the operator.
        theClause--;
        theOp = theNotOp;
        break;
      case "AND":     //An AND join. Set the join.
        theClause--;
        theJoin = "AND";
        break;
      case "OR":     //An OR join. Set the join.
        theClause--;
        theJoin = "OR";
        break;
      case "NOT":     //A NOT operator. Set the operator.
        theClause--;
        theOp = theNotOp;
        break;
      default:     //A keyword. Set the clause and the join.
        aForm.elements[((theClause*4)-2)].value=aKey[i];
        aForm.elements[((theClause*4)-3)].value=theOp;
        if (theClause != 1){     //There is no Join before the first value triplet
          aForm.elements[((theClause*4)-5)].value=theJoin;
        }
        theJoin = myAndOr;     // Re-set default (space) Join
        theOp = myOp;     // Re-set default Op
        break;
    } //end 'switch' statement
    if(++theClause==11){     // Increment 'theClause' and check if you've exceeded the 10 clause limit
      alert('Portfolio NetPublish queries can accept ten arguments (word values), all words after ""' + aKey[(i-1)] + '" will be ignored.');
      break;
    }
  } // end 'for' loop
  //
  //Folowing code section rem'd as it seems to crash Safari when auto-fill on (Safari bug!)
  //
  //now to disable the unused '[join]/field/op/value' element sets to avoid very long URLs
  //for POST method forms you can omit this section, though it still is useful
  //decrement var 'theClause' after the max length test above)
  /*
  theClause = theClause - 1;
  if(theClause==1){
    for (j = 3; j < lastTerm; j++){
      aForm.elements[j].disabled = true;
    }
  }else if ((theClause > 1) && (theClause < 10)) {
    var theStart = 3 + ((theClause -1)*4);
    for (k = theStart; k < lastTerm; k++){
      aForm.elements[k].disabled = true;
    }
  }
  */
  aForm.parseList.value ='';
  return;
}

