<?xml version="1.0" encoding="UTF-8"?>
<dynaForm name="patterns_Sequential" type="xmlform" width="100%" enableTemplate="1">
  <action type="hidden"/>
  <LANG type="private"/>
  <ROU_UID type="hidden"/>
  <PROCESS type="hidden"/>
  <TASK type="hidden"/>
  <ROU_TYPE_OLD type="hidden"/>
  <TITLE type="title" showInTable="0">
    <en><![CDATA[Sequential]]></en>
  </TITLE>
  <ROU_TYPE type="hidden"/>
  <ROU_NEXT_TASK type="dropdown"><![CDATA[
SELECT TAS_UID, CON_VALUE FROM TASK LEFT JOIN CONTENT ON (TAS_UID = CON_ID AND CON_CATEGORY = 'TAS_TITLE' AND CON_LANG = '@#LANG') WHERE PRO_UID = '@#PROCESS'
  ]]><en><![CDATA[Next Task]]><option name="-1"><![CDATA[End of process]]></option></en></ROU_NEXT_TASK>
  <BTN_CANCEL type="button" onclick="cancel();">
    <en><![CDATA[Cancel]]></en>
  </BTN_CANCEL>
  <SAVE type="button" onclick="savePattern(this.form);">
    <en><![CDATA[Save]]></en>
  </SAVE>
  <JS type="javascript"><![CDATA[
var bModified = false;
var modified  = function() {
  bModified   = true;
};
leimnud.event.add(getField('ROU_NEXT_TASK'),    'change', modified);
//leimnud.event.add(getField('ROU_TO_LAST_USER'), 'change', modified);
Pm.tmp.derivationsPanel.events.remove = function() {
  if (bModified) {
    Pm.tmp.derivationsPanel.inRemove = false;
    Pm.tmp.derivationsPanel.cancelClose=true;
    new leimnud.module.app.confirm().make({
      label:  G_STRINGS.ID_SAVE_DERIVATION_RULES_BEFORE_CLOSING,
      action: function() {
        Pm.tmp.derivationsPanel.cancelClose=false;
        bModified = false;
        getField('SAVE').onclick();
      },
      cancel: function() {
        Pm.tmp.derivationsPanel.cancelClose=false;
        bModified = false;    
        Pm.tmp.derivationsPanel.remove();
      }
    });
    return false;
  }
  else {
    return true;
  }
};

var savePattern = function(oForm)
{
  var oAux;
  var oType     = getField('ROU_TYPE');
  var oOldType  = getField('ROU_TYPE_OLD');
  var i         = 1;
  var aData     = {};
  var bContinue = true;
  if (oType.value != oOldType.value)
  {
    if (!confirm('You are changing the pattern, are you sure?'))
    {
      return false;
    }
  }
  switch (oType.value)
  {
    case 'SELECT':
      var aAux = [];
      while (oAux = document.getElementById('form[GRID_SELECT_TYPE][' + i + '][ROU_NEXT_TASK]'))
      {
        if (!aAux.inArray(oAux.value))
        {
          aAux.push(oAux.value);
        }
        else
        {
          bContinue = false;
        }
        i++;
      }
    break;
    case 'EVALUATE':
      var aAux = [];
      while (oAux = document.getElementById('form[GRID_EVALUATE_TYPE][' + i + '][ROU_NEXT_TASK]'))
      {
        if (!aAux.inArray(oAux.value))
        {
          aAux.push(oAux.value);
        }
        else
        {
          bContinue = false;
        }
        i++;
      }
    break;
    case 'PARALLEL':
      var aAux = [];
      while (oAux = document.getElementById('form[GRID_PARALLEL_TYPE][' + i + '][ROU_NEXT_TASK]'))
      {
        if (!aAux.inArray(oAux.value))
        {
          aAux.push(oAux.value);
        }
        else
        {
          bContinue = false;
        }
        i++;
      }
    break;
    case 'PARALLEL-BY-EVALUATION':
      var aAux = [];
      while (oAux = document.getElementById('form[GRID_PARALLEL_EVALUATION_TYPE][' + i + '][ROU_NEXT_TASK]'))
      {
        if (!aAux.inArray(oAux.value))
        {
          aAux.push(oAux.value);
        }
        else
        {
          bContinue = false;
        }
        i++;
      }
    break;
  }
  if (!bContinue)
  {
    alert('You cannot select to the same task several times!');
    return bContinue;
  }
  i             = 1;
  aData.tas_uid = getField('TASK').value;
  aData.data    = [];
  switch (oType.value)
  {
    case 'SEQUENTIAL':
      aData.type = 0;
      aData.data.push(getField('ROU_NEXT_TASK').value);
    break;
    case 'SELECT':
      aData.type = 1;
      while (oAux = document.getElementById('form[GRID_SELECT_TYPE][' + i + '][ROU_NEXT_TASK]'))
      {
        aData.data.push(oAux.value);
        i++;
      }

    break;
    case 'EVALUATE':
      aData.type = 2;
      while (oAux = document.getElementById('form[GRID_EVALUATE_TYPE][' + i + '][ROU_NEXT_TASK]'))
      {
        aData.data.push(oAux.value);
        i++;
      }
    break;
    case 'PARALLEL':
      aData.type = 3;
      while (oAux = document.getElementById('form[GRID_PARALLEL_TYPE][' + i + '][ROU_NEXT_TASK]'))
      {
        aData.data.push(oAux.value);
        i++;
      }
    break;
    case 'PARALLEL-BY-EVALUATION':
      aData.type = 4;
      while (oAux = document.getElementById('form[GRID_PARALLEL_EVALUATION_TYPE][' + i + '][ROU_NEXT_TASK]'))
      {
        aData.data.push(oAux.value);
        i++;
      }
    break;
    case 'SEC-JOIN':
      aData.type = 5;
      aData.data.push(getField('ROU_NEXT_TASK').value);
    break;
  }
  Pm.data.build.derivation(aData);
  ajax_post(oForm.action, oForm, 'POST');
  bModified = false;
  Pm.tmp.derivationsPanel.remove();
};

function cancel(){
    Pm.tmp.derivationsPanel.remove();
}

]]></JS>
</dynaForm>
