<?xml version="1.0" encoding="UTF-8"?>
<dynaForm name="reportTables/reportTables_Edit" type="xmlform" width="100%" enableTemplate="1">
  <LANG type="private"/>
  <PRO_UID type="hidden"/>
  <REP_TAB_UID type="hidden"/>
  <TITLE type="title" group="1">
    <en><![CDATA[Report Table Information]]></en>
  </TITLE>
  <REP_TAB_TITLE type="text" size="50" maxlength="150" required="1">
    <en><![CDATA[Title]]></en>
  </REP_TAB_TITLE>
  <REP_TAB_NAME type="text" size="50" maxlength="100" validate="Field" required="1">
    <en><![CDATA[Table Name]]></en>
  </REP_TAB_NAME>
  <REP_TAB_TYPE type="dropdown" required="1">
    <en><![CDATA[Type]]><option name="NORMAL"><![CDATA[Global]]></option><option name="GRID"><![CDATA[Grid]]></option></en>
  </REP_TAB_TYPE>
  <REP_TAB_GRID type="dropdown" sqlConnection="dbarray" required="1">
SELECT * FROM processGridFields
  <en><![CDATA[Grid Fields]]></en>
</REP_TAB_GRID>
  <FIELDS type="listbox" sqlConnection="dbarray" defaultValue="" required="1" size="20" width="350">
SELECT * FROM processFields
  <en><![CDATA[Fields]]></en>
</FIELDS>
  <BTN_CANCEL type="button" onclick="history.back();">
    <en><![CDATA[Cancel]]></en>
  </BTN_CANCEL>
  <SAVE type="button" onclick="reportTablesSave(this.form);">
    <en><![CDATA[Save]]></en>
  </SAVE>
  <JS type="JavaScript"><![CDATA[
var sOldTableName = getField('REP_TAB_NAME').value;
leimnud.event.add(getField('REP_TAB_TYPE'), 'change', function() {
  if (getField('REP_TAB_TYPE').value == 'NORMAL') {
    hideRowById('REP_TAB_GRID');
    showRowById('FIELDS');
  }
  else {
    hideRowById('FIELDS');
    showRowById('REP_TAB_GRID');
  }
});
leimnud.event.add(getField('REP_TAB_NAME'), 'change', function() {
  if (sOldTableName != getField('REP_TAB_NAME').value) {
    oSaveButton = getField('SAVE');
    oSaveButton.disabled = true;
    var oRPC = new leimnud.module.rpc.xmlhttp({
      url   : '../reportTables/reportTables_Ajax',
      async : false,
      method: 'POST',
      args  : 'action=tableExists&sTableName=' + getField('REP_TAB_NAME').value
    });
    oRPC.make();
    if (oRPC.xmlhttp.responseText == '0') {
      new leimnud.module.app.alert().make({
        label:'@G::LoadTranslation(ID_TABLE_ALREADY_EXISTS)'
      });
    }
    else {
      oSaveButton.disabled = false;
    }
  }
  else {
    if (sOldTableName != '') {
      oSaveButton.disabled = false;
    }
  }
});
if (getField('REP_TAB_TYPE').value == 'NORMAL') {
  hideRowById('REP_TAB_GRID');
}
else {
  hideRowById('FIELDS');
}

function cancel(){
    currentPopupWindow.remove();
}

//////

function reportTablesSave(form)
{
  if (getField("REP_TAB_TITLE").value.trim() == "") {
      new leimnud.module.app.alert().make({label: _("ID_TITLE_REQUIRED")});
      return false;
  }

  if (getField("REP_TAB_NAME").value.trim() == ""){
      new leimnud.module.app.alert().make({label: _("ID_NAME_REQUIRED")});
      return false;
  }

  var j = 0;
  if (getField('REP_TAB_TYPE').value == 'NORMAL') {
    var i;
    var oAux = getField('FIELDS');
    var iMax = oAux.length;
    for (i = 0; i < oAux.length; i++) {
      if (oAux.options[i].selected) {
        j++;
      }
    }
    if ((j < 1) || (j > 80)) {
      new leimnud.module.app.alert().make({label: _("ID_SELECT_LEAST_FIELD")});
      return false;
    }
  } else {
    if (getField("REP_TAB_GRID").value == "") {
        new leimnud.module.app.alert().make({label: _("ID_GRID_FIELDS_REQUIRED")});
        return false;
    }
  }

  ajax_post(form.action, form, 'POST');
  history.back();
}

function reportTablesDelete(sUID)
{
  new leimnud.module.app.confirm().make({
    label:'@G::LoadTranslation(ID_MSG_CONFIRM_DELETE_REPORT_TABLE)',
    action:function()
    {
      ajax_function('@G::encryptlink(@#reportTablesDelete)', '', 'REP_TAB_UID=' + sUID, 'POST');
      //@#PAGED_TABLE_ID.refresh();
    }.extend(this)
  });
}
]]></JS>
</dynaForm>
