/*
Fichero de Javascript que contendra las funciones necesarias para parsear una cadena.
*/



/**
  * Esta funcion mira que hay antes, si un Or, un And o unas comillas.
  * Se supone que alguno de los tres es distinto de -1.
  * Devuelve el indice del menor.
  * @return
  */
  function giveSmaller(iOr, iAnd, iComillas)
  {
      var smaller = -1;
      if (iOr == -1) // no hay or
      {
        if (iAnd == -1)  // no hay ni or ni and
        {
          smaller = iComillas;
        }
        else //Hay and
        {
          if (iComillas == -1)  //no hay comillas
          {
            smaller = iAnd;
          }
          else  // hay and y comillas
          {
            if (iAnd < iComillas)
            {
              smaller = iAnd;
            }
            else
            {
              smaller = iComillas;
            }
          }
        }
      }
      else // Si hay or
      {
        if (iAnd == -1)  //hay or pero no And
        {
          if (iComillas == -1) //hay or pero no and ni comillas
          {
            smaller = iOr;
          }
          else  //hay or y comillas
          {
            if (iOr < iComillas)
            {
              smaller = iOr;
            }
            else
            {
              smaller = iComillas;
            }
          }
        }
        else  //hay or y hay and
        {
          if (iComillas == -1)  //hay or y and y no comillas
          {
            if (iOr < iAnd)
            {
              smaller = iOr;
            }
            else
            {
              smaller = iAnd;
            }
          }
          else //hay de todo
          {
            if ((iOr < iAnd) && (iOr < iComillas))
            {
              smaller == iOr;
            }
            else if ((iAnd < iOr) && (iAnd < iComillas))
            {
              smaller = iAnd;
            }
            else
            {
              smaller = iComillas;
            }
          }
        }
      }
      return smaller;
    }



    /**
     * Parsea un elemento, que no puede ser nulo.
     * @return String
     */
    function parseElement(userText)
    {
      //alert("Entrando a parseElement");
      var index = 0;
      var indicadorInicio = 0;
      var indicadorFinal = 0;
      var element = "";
      while (userText.charAt(index) == " ")  //parto de la premisa de que un elemento no puede ser nulo.
      {
        index++;
      }

      if (userText.charAt(index) == '"')
      {
        indicadorFinal = userText.indexOf('"', index+1);
        element = element.concat(userText.substring(indicadorInicio, indicadorFinal+1));
        element = element.concat(parseElement(userText.substring(indicadorFinal+1, userText.length)));
      }
      else
      {
        var regExpOr = /[ ][O|o][R|r][ ]/;
        var regExpAnd = /[ ][A|a][N|n][D|d][ ]/;
        indexOr = userText.search(regExpOr);
        indexAnd = userText.search(regExpAnd);
        indexComillas = userText.indexOf('"');
        if ((indexOr == -1) && (indexAnd == -1) && (indexComillas == -1))
        {
          element = userText;
        }
        else
        {
          var smaller = giveSmaller(indexOr, indexAnd, indexComillas);
          if (smaller == indexComillas)
          {
            element = element.concat(userText.substring(indicadorInicio, indexComillas));
            element = element.concat(parseElement(userText.substring(indexComillas, userText.length)));
          }
          else
          {
            element = element.concat(userText.substring(indicadorInicio, smaller));
            //aqui ya no tengo que seguir parseando la cadena.
          }
        }
      }
      //alert("saliendo de parseElement");
      return element;
    }



    /**
     * Devuelve el operador.  Se supone que es el primer elemento que encuentra
     * en la cadena userText.
     * @return String (operador)
     */
    function readOperator(userText){
      var op = "";
      var index = 0;
      if (userText != "")
      {
         while ((userText.charAt(index) == " ") && (index < userText.length))
         {
          index++;
         }
          if (index >= userText.length)
          {
            op = ""
            userText = "";
          }
          else
          {
            var indexEsp = userText.indexOf(" ", index);
            op = userText.substring(index, indexEsp);
            userText = userText.substring(indexEsp+1, userText.length);
          }
      }
      return op;
    }

    /**
     * Parsea el texto introducido por el usuario.  En elementArray se devuelven
     * los elementos. En operatorArray se devuelven los operadores.
     * @return
     */
    function parseUserText(userText, elementArray, operatorArray){
      var elemento;
      //alert("Entrando a parseUserText");
      elemento = parseElement(userText); //modificara userText.  Dejara un op o vacia.
      elementArray[elementArray.length] = elemento;
      userText = userText.substring(userText.indexOf(elemento)+elemento.length, userText.length);
      op = readOperator(userText);  //modificara usertext.
      if (op == "")
      {
        //Ya no hay nada mas...
      }
      else
      {
        //guardarlo en el operatorArray
        operatorArray[operatorArray.length] = op;
        userText = userText.substring(userText.indexOf(op)+op.length, userText.length);
        parseUserText(userText, elementArray, operatorArray);
      }

    }


    function detailedParseUserText(userText, elementArray, operatorArray){
      var elemento;
      var aux = new Array();
      var subElementArray = new Array();

      elemento = parseElement(userText); //modificara userText.  Dejara un op o vacia.
      //

      if (elemento.search('"')!= -1)
      {
        //No tenemos que partir el elemento.
        elementArray[elementArray.length] = elemento;
      }
      else
      {
        aux = elemento.split(' ');
        for (var k=0;k<aux.length;k++)
        {
          if (aux[k].length > 0)
          {
            subElementArray[subElementArray.length] = aux[k];
          }
        }

        //elementArray = elementArray.concat(subElementArray);
        for (k=0;k<subElementArray.length;k++)
        {
          elementArray[elementArray.length] = subElementArray[k];
        }
        for (k=0;k<subElementArray.length-1;k++)
        {
          operatorArray[operatorArray.length] = 'OR';
        }
      }
      //
      //elementArray[elementArray.length] = elemento;

      userText = userText.substring(userText.indexOf(elemento)+elemento.length, userText.length);
      op = readOperator(userText);  //modificara usertext.
      if (op == "")
      {

      }
      else
      {
        //guardarlo en el operatorArray
        operatorArray[operatorArray.length] = op;
        userText = userText.substring(userText.indexOf(op)+op.length, userText.length);
        parseUserText(userText, elementArray, operatorArray);
      }
    }

