<%@language = "javascript" %>
<% /*
     $Revision$
     $Id$
*/ %>
<%
var JSON_Result = true;
%>
<!-- #include file="../Shared/common.inc" -->
<%
  protectRequest.validateToken();
  var mgroep = getFParamInt('mgroep', -1);
  if(mgroep === 0 || mgroep === 1 || mgroep === 2)
     user.checkAutorisation("WEB_PRSSYS");
  var menuKey = getFParamInt('menu_key');
  var previousMenuKey = getFParamInt('previous_menu_key');
  var nextMenuKey = getFParamInt('next_menu_key');
  var sql = "SELECT fac_menu_key, fac_menu_volgnr"
           + " FROM fac_v_menu"
           + " WHERE groep = " + mgroep
           + " AND fac_menu_key IN (" + menuKey + ", " + previousMenuKey + ", " + nextMenuKey + ")"
           + " ORDER BY fac_menu_volgnr ASC";
  var oRs = Oracle.Execute(sql);

  var data = {
    previousRow: {},
    oldRow: {},
    nextRow: {}
  };

  while(!oRs.eof) {
    switch(oRs("fac_menu_key").Value) {
      case menuKey:
      data.oldRow = {
        menuKey: oRs("fac_menu_key").Value,
        volgNr: oRs("fac_menu_volgnr").Value
      }
        break;
      case previousMenuKey:
      data.previousRow = {
        menuKey: oRs("fac_menu_key").Value,
        volgNr: oRs("fac_menu_volgnr").Value
      }
        break;
      case nextMenuKey:
      data.nextRow = {
        menuKey: oRs("fac_menu_key").Value,
        volgNr: oRs("fac_menu_volgnr").Value
      }
        break;
    }
    oRs.MoveNext();
  }
  oRs.Close();

  if(data.previousRow.volgNr > data.oldRow.volgNr
  || data.nextRow.volgNr < data.oldRow.volgNr ) {
    var hasPrevious = (previousMenuKey && previousMenuKey != -1);
    var hasNext = (nextMenuKey && nextMenuKey != -1);
    var movedUp;
    var newVolgNr;

    if(hasPrevious && hasNext) { // between rows
      if(data.oldRow.volgNr > data.nextRow.volgNr ) { // moved up
        newVolgNr = data.nextRow.volgNr;
        movedUp = true;
      } else if (data.oldRow.volgNr < data.previousRow.volgNr) { // moved down
        newVolgNr = data.previousRow.volgNr;
        movedUp = false;
      } else {
        // All volgNrs are equal, check volgnrs before previous/next - TODO
        newVolgNr = data.nextRow.volgNr ;
        movedUp =  true;
      }
    } else if(hasPrevious) { // moved to the end
        movedUp = false;
        newVolgNr = data.previousRow.volgNr;
    } else if(hasNext) { // moved to the start
        movedUp = true;
        newVolgNr = data.nextRow.volgNr;
    }

    // Get volgnrs between old and new volgNr
    var isMenuTabs = mgroep === 5 || mgroep === 6;
    var hasPerslidKey = isMenuTabs ? (" AND prs_perslid_key = " + user_key) : "";
    var sql2 = "SELECT fac_menu_volgnr, fac_menu_key FROM fac_menu"
            + " WHERE fac_menu_volgnr BETWEEN"
            + " LEAST(" + newVolgNr + ", " + data.oldRow.volgNr  + ")"
            + " AND GREATEST(" + newVolgNr + ", " + data.oldRow.volgNr  + ")"
            + " AND fac_menu_altgroep = " + mgroep
            + hasPerslidKey
            + " ORDER BY fac_menu_altgroep, fac_menu_volgnr";


    oRs = Oracle.Execute(sql2);
    var menuKeys = [];
    var volgNrs = [];
    while(!oRs.eof) {
      menuKeys.push(oRs("fac_menu_key").Value);
      volgNrs.push(oRs("fac_menu_volgnr").Value);
      oRs.MoveNext();
    }
    oRs.Close();

    if(movedUp) {
        var lastKey = menuKeys.pop();
        menuKeys.unshift(lastKey);
    } else {
        var firstKey = menuKeys.shift();
        menuKeys.push(firstKey);
    }

    var sql3 = "BEGIN";
    for(var i = 0; i < menuKeys.length; i++) {
      sql3 += " UPDATE fac_menu SET fac_menu_volgNr = " + volgNrs[i]
            + " WHERE fac_menu_key = " + menuKeys[i] + ";";
    }
    sql3 += " END;";
    Oracle.Execute(sql3);
  }

Response.Write(JSON.stringify({success:true}));
%>
<% ASPPAGE_END(); %>
