<?xml version="1.0" encoding="UTF-8"?>
<dynaForm name="patterns_Evaluate" id="patterns_Evaluate" 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[Evaluation]]></en>
  </TITLE>
  <ROU_TYPE type="hidden"/>
  <GRID_EVALUATE_TYPE type="grid" xmlgrid="patterns/patterns_GridEvaluateType" addRow="1" deleteRow="1" editRow=""/>
  <BTN_CANCEL type="button" onclick="cancel();">
    <en><![CDATA[Cancel]]></en>
  </BTN_CANCEL>
  <SAVE type="button" onclick="savePattern();">
    <en><![CDATA[Save]]></en>
  </SAVE>
  <JS type="javascript"><![CDATA[

var _oVarsPanel_;
var showDynaformsFormVars = function(sFieldName, sAjaxServer, sProcess, sSymbol) {
	_oVarsPanel_ = new leimnud.module.panel();
	_oVarsPanel_.options = {
    limit    : true,
    size     : {w:600,h:420},
    position : {x:0,y:0,center:true},
    title    : '',
    theme    : 'processmaker',
    statusBar: false,
    control  : {drag:false,resize:true,close:true},
    fx       : {opacity:true,rolled:false,modal:true}
  };
  _oVarsPanel_.make();
  _oVarsPanel_.events = {
    remove:function() {
      delete _oVarsPanel_;
    }.extend(this)
  };
  _oVarsPanel_.loader.show();
  oRPC = new leimnud.module.rpc.xmlhttp({
    url   : sAjaxServer,
    method: 'POST',
    args  : 'sFieldName=' + sFieldName + '&sProcess=' + sProcess + '&sSymbol=' + sSymbol
  });
  oRPC.callback = function(oRPC) {
    _oVarsPanel_.loader.hide();
    var scs = oRPC.xmlhttp.responseText.extractScript();
    _oVarsPanel_.addContent(oRPC.xmlhttp.responseText);
    scs.evalScript();
  }.extend(this);
  oRPC.make();
};

var insertFormVar = function(sFieldName, sValue) {
	oAux = document.getElementById(sFieldName);
	if (oAux.setSelectionRange) {
		var rangeStart = oAux.selectionStart;
    var rangeEnd   = oAux.selectionEnd;
    var tempStr1   = oAux.value.substring(0,rangeStart);
    var tempStr2   = oAux.value.substring(rangeEnd);
    oAux.value     = tempStr1 + sValue + tempStr2;
	}
	else {
	  if (document.selection) {
	    oAux.focus();
      document.selection.createRange().text = sValue;
	  }
	}
	_oVarsPanel_.remove();
};
var bModified = false;
var modified  = function() {
  bModified   = true;
};
var oGrid          = object_patterns_Evaluate.getElementByName('GRID_EVALUATE_TYPE');
oGrid.onaddrow     = modified;
oGrid.ondeleterow  = modified;
oGrid.setFunctions([{sFieldName: 'ROU_NEXT_TASK',    sFunction: modified}]);
oGrid.setFunctions([{sFieldName: 'ROU_CONDITION',    sFunction: modified}]);
oGrid.setFunctions([{sFieldName: 'ROU_TO_LAST_USER', sFunction: modified}]);
Pm.tmp.derivationsPanel.events.remove = function() {
  if (bModified) {
  		if(confirm(G_STRINGS.ID_SAVE_DERIVATION_RULES_BEFORE_CLOSING)){
	  		savePattern();
	  		return false;
  		}else{
  			return 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();
      }.extend(this),
      cancel: function() {
        Pm.tmp.derivationsPanel.cancelClose=false;
        bModified = false;
        Pm.tmp.derivationsPanel.remove();
      }
    });*/
    return false;
  }
  else {
    return true;
  }
};

var savePattern = function()
{ oForm=document.getElementById('patterns_Evaluate');
  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>
