function showMenu(menuName) {
  if (('' + menuName !== "") && ('' + menuName !== "LAST")) {
    window.sessionStorage.lastMenu = menuName;
  }
  var mn = '';
  if ('' + menuName == "LAST") {
    mn = window.sessionStorage.lastMenu;
  } else {
    mn = '' + menuName;
  }
  var forceRefresh = '?' + moment().format();
  switch (mn) {
    case 'SM':
    case 'SO':
    case 'SE':
    case 'RS':
    case 'RE':
    case 'ESS':
    case 'SCM':
    case 'MRP':
    case 'CRM':
    case 'MIS':
    case 'BI':
      loadMenu('/App/Main/views/' + mn + '/menu' + mn + '.html' + forceRefresh);
      break;
    case 'BLANKPAGE':
      hideAll();
      break;
    default:
      hideMenu();
  }
}

function loadFile(elementid, filename) {
  if (filename !== "") {
    document.getElementById(elementid).style.display = 'block';
    $.ajax({
      url: filename,
      success: function (data) {
        $(elementid).html(data);
      }
    });
  }
}

function loadMenu(menuName) {
  if (menuName !== "") {
    document.getElementById('menubar').style.display = 'block';
    $.ajax({
      url: menuName,
      success: function (data) {
        $('#menubar').html(data);
      }
    });
    $("#mainmenu").ejMenu({
      width: "100%"
    });
  }
}

function hideMenu() {
  document.getElementById('menubar').style.display = 'none';
}

function hideAll() {
  document.getElementById('rootmenu').style.display = 'none';
  document.getElementById('menubar').style.display = 'none';
  //document.getElementById('footerpage').style.display = 'none';
}

function showButtonBar(buttonSet) {
  // switch ('' + buttonSet) {
  //   case 'PROCESS':
  //     $("#buttonBar").append('<button type="button" id="btnClose" class="btn btn-default" ng-click="close()">Close</button>');
  //     $("#buttonBar").append('<button type="button" id="btnProcess" class="btn btn-default" ng-click="process()">Process</button>');
  //     $("#buttonBar").append('<button type="button" id="btnCancel" class="btn btn-default" ng-click="cancel()">Cancel</button>');
  //     break;

  //   case 'EDIT':
  //     $("#buttonBar").append('<button type="button" id="btnClose" class="btn btn-default" ng-click="close()">Close</button>');
  //     $("#buttonBar").append('<button type="button" id="btnAdd" class="btn btn-default" ng-click="add()">Add</button>');
  //     $("#buttonBar").append('<button type="button" id="btnEdit" class="btn btn-default" ng-click="edit()">Edit</button>');
  //     $("#buttonBar").append('<button type="button" id="btnDelete" class="btn btn-default" ng-click="delete()">Delete</button>');
  //     $("#buttonBar").append('<button type="button" id="btnPrint" class="btn btn-default" ng-click="print()">Print</button>');
  //     $("#buttonBar").append('<button type="button" id="btnCancel" class="btn btn-default" ng-click="cancel()">Cancel</button>');
  //     $("#buttonBar").append('<button type="submit" id="btnSave" class="btn btn-default" ng-disabled="frm.$invalid">Save</button>');
  //     break;

  //   case 'DOCUMENT':
      $("#buttonBar").append('<button type="button" id="btnClose" class="btn btn-default" ng-click="close()">Close</button>');
      $("#buttonBar").append('<button type="button" id="btnAdd" class="btn btn-default" ng-click="add()">Add</button>');
      $("#buttonBar").append('<button type="button" id="btnOpen" class="btn btn-default" ng-click="open()">Open</button>');
      $("#buttonBar").append('<button type="button" id="btnEdit" class="btn btn-default" ng-click="edit()">Edit</button>');
      $("#buttonBar").append('<button type="button" id="btnDelete" class="btn btn-default" ng-click="delete()">Delete</button>');
      $("#buttonBar").append('<button type="button" id="btnPrint" class="btn btn-default" ng-click="print()">Print</button>');
      $("#buttonBar").append('<button type="button" id="btnMore" class="btn btn-default" ng-click="more()">More</button>');
      $("#buttonBar").append('<button type="button" id="btnCancel" class="btn btn-default" ng-click="cancel()">Cancel</button>');
      $("#buttonBar").append('<button type="button" id="btnCancelDoc" class="btn btn-default" ng-click="cancelDoc()">Cancel Document</button>');
      // $("#buttonBar").append('<button type="submit" id="btnSave" class="btn btn-default" ng-disabled="frm.$invalid">Save</button>');
      $("#buttonBar").append('<button type="button" id="btnSave" class="btn btn-default" ng-disabled="frm.$invalid" ng-click="submit()">Save</button>');
      $("#buttonBar").append('<button type="button" id="btnCheck" class="btn btn-default" ng-click="check()">Check</button>');
      $("#buttonBar").append('<button type="button" id="btnApprove" class="btn btn-default" ng-click="approve()">Approve</button>');
      $("#buttonBar").append('<button type="button" id="btnApprove2" class="btn btn-default" ng-click="approve2()">Approve 2</button>');
      $("#buttonBar").append('<button type="button" id="btnApprove3" class="btn btn-default" ng-click="approve3()">Approve 3</button>');      
      $("#buttonBar").append('<button type="button" id="btnRevise" class="btn btn-default" ng-click="revise()">Revise</button>');
      $("#buttonBar").append('<button type="button" id="btnProcess" class="btn btn-default" ng-click="process()">Process</button>');
  //     break;

  // }
}

function setButtons(buttonSet, runMode) {
      $("#btnClose").hide();
      $("#btnAdd").hide();
      $("#btnOpen").hide();
      $("#btnEdit").hide();
      $("#btnDelete").hide();
      $("#btnPrint").hide();
      $("#btnMore").hide();
      $("#btnCancel").hide();
      $("#btnCancelDoc").hide();
      $("#btnSave").hide();                  
      $("#btnCheck").hide();
      $("#btnApprove").hide();
      $("#btnApprove2").hide();
      $("#btnApprove3").hide();
      $("#btnRevise").hide();
      $("#btnProcess").hide();
      
        
  switch ('' + buttonSet) {
    case 'PROCESS':
      if (runMode === "VIEW") {
        $("#btnProcess").show();
        $("#btnClose").show();
        // $("#btnCancel").hide();
      } else {
        // $("#btnProcess").hide();
        // $("#btnClose").hide();
        $("#btnCancel").show();
      }
      break;

    case 'EDIT':
      // $("#btnAdd").hide();
      // $("#btnPrint").hide();
      // $("#btnDelete").hide();
      if (runMode === "VIEW") {
        $("#btnEdit").show();
        $("#btnClose").show();
        // $("#btnSave").hide();
        // $("#btnCancel").hide();
      } else {
        // $("#btnEdit").hide();
        // $("#btnClose").hide();
        $("#btnSave").show();
        $("#btnCancel").show();
      }
      break;

    case 'EDITWITHDELETE':
      // $("#btnAdd").hide();
      // $("#btnPrint").hide();
      if (runMode === "VIEW") {
        $("#btnEdit").show();
        $("#btnClose").show();
        // $("#btnSave").hide();
        // $("#btnCancel").hide();
        $("#btnDelete").show();
      } else {
        // $("#btnEdit").hide();
        // $("#btnClose").hide();
        $("#btnSave").show();
        $("#btnCancel").show();
        // $("#btnDelete").hide();
      }
      break;

    case 'EDITWITHPRINT':
      // $("#btnAdd").hide();
      // $("#btnDelete").hide();
      if (runMode === "VIEW") {
        $("#btnEdit").show();
        $("#btnClose").show();
        // $("#btnSave").hide();
        // $("#btnCancel").hide();
        $("#btnPrint").show();
      } else {
        // $("#btnEdit").hide();
        // $("#btnClose").hide();
        $("#btnSave").show();
        $("#btnCancel").show();
        // $("#btnPrint").hide();
      }
      break;

    case 'DOCUMENT':
      // $("#btnAdd").hide();
      // $("#btnDelete").hide();
      // $("#btnMore").hide();
      // $("#btnProcess").hide();
      // $("#btnOpen").hide();
      // $("#btnCheck").hide();
      // $("#btnApprove").hide();
      // $("#btnApprove2").hide();
      // $("#btnApprove3").hide();
      // $("#btnRevise").hide();
      // $("#btnPrint").hide();
      if (runMode === "VIEW") {
        $("#btnEdit").show();
        $("#btnClose").show();
        // $("#btnSave").hide();
        // $("#btnCancel").hide();
      } else {
        // $("#btnEdit").hide();
        // $("#btnClose").hide();
        $("#btnSave").show();
        $("#btnCancel").show();
      }
      break;

    case 'LIST':
      $("#btnAdd").show();
      $("#btnOpen").show();
      // $("#btnCheck").hide();
      // $("#btnApprove").hide();
      // $("#btnRevise").hide();
      // $("#btnPrint").hide();
      // $("#btnEdit").hide();
      $("#btnClose").show();
      // $("#btnSave").hide();
      // $("#btnCancel").hide();
      // $("#btnDelete").hide();
      // $("#btnMore").hide();
      // $("#btnProcess").hide();

      break;
  }

}

function showGrid(iGridId, iData, iQuery, selectCallBack, iColoumns, iRunMode, iPageSize, iWidth, iHeight) {
  if (iData === null) {
    var data = [];
  } else {
    var data = iData;
  }
  switch ('' + iRunMode) {
    case 'EDIT':
    case 'ADD':
      if (iPageSize === 0 || iPageSize === undefined) {
        $(iGridId).ejGrid({
          dataSource: data,
          cssClass: "gridDocEditor",
          selectionMode: "cell",
          allowFiltering: false,
          allowScrolling: true,
          allowResizing: true,
          // scrollSettings: { height: iHeight },          
          editSettings: {
            rowPosition: "bottom",
            allowEditing: true,
            allowAdding: true,
            allowDeleting: true,
            editMode: ej.Grid.EditMode.Batch,
            showConfirmDialog: false
          },
          toolbarSettings: {
            showToolbar: true,
            toolbarItems: [ej.Grid.ToolBarItems.Add, ej.Grid.ToolBarItems.Edit, ej.Grid.ToolBarItems.Delete, ej.Grid.ToolBarItems.Cancel] //ej.Grid.ToolBarItems.Update,
          },
          keyConfigs: {
            saveRequest: "", //Default value - 13 set this to empty to avoid conflict with ENTER 
            moveCellRight: "9,13" //Default value – 9 added 13, so will move on both ENTER and TAB. 
          },
          columns: iColoumns
        });
        if (iHeight !== 0 && iHeight !== undefined) { $(iGridId).ejGrid({ scrollSettings: { height: iHeight } }); }
      }
      else { 
        $(iGridId).ejGrid({
          dataSource: data,
          cssClass: "gridDocEditor",
          allowResizing: true,
          allowScrolling: false,
          allowPaging: true,
          allowFiltering: false,
          enableHeaderHover: false,
          pageSettings: {
            pageSize: iPageSize
          },
          editSettings: {
            rowPosition: "bottom",
            allowEditing: true,
            allowAdding: true,
            allowDeleting: true,
            editMode: ej.Grid.EditMode.Batch
          },
          toolbarSettings: {
            showToolbar: true,
            toolbarItems: [ej.Grid.ToolBarItems.Add, ej.Grid.ToolBarItems.Edit, ej.Grid.ToolBarItems.Delete, ej.Grid.ToolBarItems.Cancel] //ej.Grid.ToolBarItems.Update,
          },
          keyConfigs: {
            saveRequest: "", //Default value - 13 set this to empty to avoid conflict with ENTER 
            moveCellRight: "9,13" //Default value – 9 added 13, so will move on both ENTER and TAB. 
          },          
          columns: iColoumns
        });
      }
      break;

    case 'LIST':
      if (iPageSize === 0 || iPageSize === undefined) {
        $(iGridId).ejGrid({
          dataSource: data,
          // cssClass: "gridDocEditor",
          allowResizing: true,
          allowScrolling: true,
          allowPaging: false,
          // scrollSettings:{width:900,height:400},
          // scrollSettings:{height:380},
          allowFiltering: true,
          enableHeaderHover: true,
          pageSettings: {
            pageSize: iPageSize
          },
          editSettings: {
            allowEditing: false,
            allowAdding: false,
            allowDeleting: false
          },
          toolbarSettings: {
            showToolbar: true,
            toolbarItems: []
          },
          filterSettings: {
            filterType: "excel"
          },
          columns: iColoumns
        });
        if (iHeight !== 0 && iHeight !== undefined) { $(iGridId).ejGrid({ scrollSettings: { height: iHeight } }); }
      } else {
        $(iGridId).ejGrid({
          dataSource: data,
          // cssClass: "gridDocEditor",
          allowResizing: true,
          allowScrolling: false,
          allowPaging: true,
          allowFiltering: true,
          enableHeaderHover: true,
          pageSettings: {
            pageSize: iPageSize
          },
          editSettings: {
            allowEditing: false,
            allowAdding: false,
            allowDeleting: false
          },
          toolbarSettings: {
            showToolbar: true,
            toolbarItems: []
          },
          filterSettings: {
            filterType: "excel"
          },
          columns: iColoumns
        });
      }
      break;

    case 'VIEW':
      if (iPageSize === 0 || iPageSize === undefined) {
        $(iGridId).ejGrid({
          //   // scrollSettings: { "enableVirtualization": true },         
          //   allowPaging: false,
          //   enableHeaderHover: false,
          //   scrollSettings: {height: iHeight },          
          //   // pageSettings: {
          //   //   pageSize: iPageSize
          //   // },

          dataSource: data,
          cssClass: "gridDocEditor",
          editSettings: {
            allowEditing: false,
            allowAdding: false,
            allowDeleting: false
          },
          toolbarSettings: {
            showToolbar: true,
            toolbarItems: []
          },
          selectionMode: "cell",
          allowFiltering: false,
          allowScrolling: true,
          allowResizing: true,
          scrollSettings: { height: iHeight },
          columns: iColoumns
        });
        // if (iHeight !== 0 && iHeight !== undefined) { $(iGridId).ejGrid({ scrollSettings: {height: iHeight } }); }        
        

      } else {
        $(iGridId).ejGrid({
          dataSource: data,
          cssClass: "gridDocEditor",
          allowResizing: true,
          allowScrolling: false,
          allowPaging: true,
          allowFiltering: false,
          enableHeaderHover: false,
          pageSettings: {
            pageSize: iPageSize
          },
          editSettings: {
            allowEditing: false,
            allowAdding: false,
            allowDeleting: false
          },
          toolbarSettings: {
            showToolbar: true,
            toolbarItems: []
          },
          columns: iColoumns
        });
      }
      break;

  }
}
