/**
 * JavaScript for genSample module.
 */

/*** Global variables.***/

/**
 * Current rowNo.
 */
var rowNo = 0;

/**
 * Selected Table Edit table row.
 */
var selectedTableRow = 0;

/**
 * Number of rows per page.
 */
var pageRows = 0;

/**
 * Row Count.
 */
var rowCount = 0;

/**
 * Go to the menu page.
 */
function doMenuBtnOnClick()
{
  var url = "simplemenu.jsp?fwModuleName=genSample&fwModuleAction=open";
  window.location = url;
}//function goToMenu()

/**
 * Open a form page.
 */
function openFormPage(src, dataset)
{
  /* Set up the List page.*/
  setResponseURL(src);

  /* Execute the query and set the first row.*/
  executeQuery(dataset);

  /* Append a row if no row exists for the main DataSet.*/
  appendIfNoRow(dataset);

  /* Set the first row.*/
  setFirstRow(dataset);

  /* Submit form to load the page.*/
  submitForm(window, "fwForm");
}//function openFormPage(src, dataset)

/**
 * Navigate to the exit page.
 */
function exitApp()
{
  if(!confirm("Are you sure you want to exit the application?"))
  {
    return;
  }//if(!confirm("Are you sure you want to exit the application?"))
  setResponseURL("genExit.jsp");
  submitForm(window, "fwForm");
}//function exitApp()

/*** Form page functions ***/
/**
 * New button.
 */
function doFormNewOnClick(dataset)
{
  appendRow(dataset);
  setResponseURL("genEmployeeForm.jsp");
  submitForm(window, "fwForm");
}//function doNewOnClick()

/**
 * Save button.
 */
function doFormSaveOnClick(dataset)
{
  saveDataSet(dataset);
  submitForm(window, "fwForm");
}//function doSaveOnClick()

/**
 * Delete button.
 */
function doFormDeleteOnClick(dataset)
{
  /* Outline:
   *
   * Confirm with the user.
   * Delete the row.
   * Save the buffer.
   * Execute the query.
   * Submit the form.
   */

  /* Confirm with the user.*/
  var returnVal = confirm("Are you sure you want to delete the current record?");
  if(!returnVal)
  {
    return;
  }//if(!returnVal)

  /* Delete the row.*/
  deleteRow(dataset);

  /* Save the buffer.*/
  saveDataSet(dataset);

  /* Execute the query.*/
  executeQuery(dataset);

  setResponseURL("genEmployeeForm.jsp");
  submitForm(window, "fwForm");
}//function doDeleteOnClick()

/**
 * FirstRow button.
 */
function doFormFirstOnClick(dataset)
{
  setFirstRow(dataset);
  setResponseURL("genEmployeeForm.jsp");
  submitForm(window, "fwForm");
}//function doFirstOnClick()

/**
 * PriorRow button.
 */
function doFormPriorOnClick(dataset)
{
  setPriorRow(dataset);
  setResponseURL("genEmployeeForm.jsp");
  submitForm(window, "fwForm");
}//function doPriorOnClick()

/**
 * NextRow button.
 */
function doFormNextOnClick(dataset)
{
  setNextRow(dataset);
  setResponseURL("genEmployeeForm.jsp");
  submitForm(window, "fwForm");
}//function doNextOnClick()

/**
 * LastRow button.
 */
function doFormLastOnClick(dataset)
{
  setLastRow(dataset);
  setResponseURL("genEmployeeForm.jsp");
  submitForm(window, "fwForm");
}//function doLastOnClick()

/**
 * Call the demoExtension class using server-side script.
 */
function invokeDemoExtension(dataset)
{
  /* Build the script segment.*/
  var segment = 'demoExtension.getDataSetStatus(' + buildStringParam(dataset) + ')';
  buildScriptSegment(segment);

  alert("Writing record status to the log...\n"
    + "Server-side Script: " + segment);
  submitForm(window, "fwForm");
}//function testExtension(msg)

/*** Table Edit page functions ***/
/**
 * New button.
 */
function doTableEditNewOnClick(dataset)
{
  /* Append the row to the dataset.*/
  appendRow(dataset);

  /* For a table edit, always set the row back to 1.*/
  setFirstRow(dataset);
  submitForm(window, "fwForm");
}//function doNewOnClick()

function doTableEditNewOnClick(dataset,callURL)
{
  mainDataFrameTarget = top.frames['mainFrame'];
  /* Append the row to the dataset.*/
  appendRow(dataset);

  /* For a table edit, always set the row back to 1.*/
  setFirstRow(dataset);
  //alert("set first row");
  
  //chad
  if (callURL != null)
  {
    responseURL = callURL;
  }
   submitInProcess = false;  
  submitForm(window, "fwForm");
}//function doNewOnClick()

function newTableRowMainFrame(dataset)
{
  mainDataFrameTarget = top.frames['mainFrame'];
  //alert("append new row");
  /* Append the row to the dataset.*/
  appendRow(dataset);

  /* For a table edit, always set the row back to 1.*/
  setFirstRow(dataset);
  submitForm(mainDataFrameTarget, "fwForm");
}//function newTableRowMainFrame()
/**
 * New button for a paging table edit.
 */
function doPagingTableEditNewOnClick(dataset)
{
  var startRowNo = 0;
  var lastPageRowCount = (rowCount + 1) % pageRows;

  /* Append the row to the dataset.*/
  appendRow(dataset);

  /* For a paging table edit, always set to the last page.*/
  if(lastPageRowCount == 0)
  {
    lastPageRowCount = pageRows;
  }//if(lastPageRowCount == 0)

  if(rowCount - lastPageRowCount + 1 < 1)
  {
    startRowNo = 1;
  }//if(rowCount - lastPageRowCount + 1 < 1)
  else
  {
    startRowNo = rowCount - lastPageRowCount + 1;
  }//else

  setRow(dataset, startRowNo);
  submitForm(window, "fwForm");

}//function doNewOnClick()

/**
 * Save button.
 */
function doTableEditSaveOnClick(dataset)
{
  saveDataSet(dataset);
  submitForm(window, "fwForm");
}//function doSaveOnClick()

/**
 * Table Edit on focus event.
 * Captures the current selectedTableRow.
 */
function doTableRowInputOnFocus(argRowNo)
{
  selectedTableRow = argRowNo;
}//function doTableRowInputOnFocus(argRowNo)

/**
 * Delete button.
 */
function doTableEditDeleteOnClick(dataset)
{
  /* Outline:
   *
   * If no row is selected, return.
   * Confirm with the user.
   * Get the selected row.
   * Delete the row.
   * Save the buffer.
   * Execute the query.
   * Submit the form.
   */

  /* Declare variables.*/

  /* If no row is selected, return.*/
  if(selectedTableRow < 1)
  {
    alert("You must select a record to delete.");
    return;
  }//if(rowNo < 1)

  /* Confirm with the user.*/
  var returnVal = confirm("Are you sure you want to delete the current record?");
  if(!returnVal)
  {
    return;
  }//if(!returnVal)

  /* Get the selected row.*/
  setRow(dataset, selectedTableRow);

  /* Delete the row.*/
  deleteRow(dataset);

  /* Save the buffer.*/
  saveDataSet(dataset);

  /* Execute the query.*/
  executeQuery(dataset);

  submitForm(window, "fwForm");
}//function doDeleteOnClick()

function doTableHeaderOnClick(dataset, sortString, src)
{
  /* Sort the data set.*/
  sortDataSet(dataset, sortString);
  setResponseURL(src);
  submitForm(window, "fwForm");

}//function doTableHeaderOnClick(dataset, columnName)

/**
 * Go to the first page of records.
 */
function doTableEditFirstPageOnClick(dataset, src)
{
  setFirstRow(dataset);
  setResponseURL(src);
  submitForm(window, "fwForm");
}//function doTableEditFirstPageOnClick(dataset)

/**
 * Go to the next page of records.
 */
function doTableEditNextPageOnClick(dataset, src)
{
//check submitInProcess
//get submitInProcess from
submitInProcess = getSubmitInProcess();
  if(submitInProcess)
  {
    alert("Please allow time for page to load to completion. Thank You!");
    return;
  }//if(submitInProcess)
  var startRowNo = 0;

  if(rowNo + pageRows <= rowCount)
  {
    startRowNo = rowNo + pageRows;
  }//if(rowNo + pageRows <= rowCount)
  else
  {
    startRowNo = rowNo;
  }//else

  setRow(dataset, startRowNo);
  setResponseURL(src);
  submitForm(window, "fwForm");
}//function doTableEditNextPageOnClick(dataset, src)

/**
 * Go to the prior page of records.
 */
function doTableEditPriorPageOnClick(dataset, src)
{
  var startRowNo = 0;

  if(rowNo - pageRows > 1)
  {
    startRowNo = rowNo - pageRows;
  }//if(rowNo - pageRows > 1)
  else
  {
    startRowNo = 1;
  }//else

  setRow(dataset, startRowNo);
  setResponseURL(src);
  submitForm(window, "fwForm");
}//function doTableEditPriorPageOnClick(dataset, src)

/**
 * Go to the last page of records.
 */
function doTableEditLastPageOnClick(dataset, src)
{
  var startRowNo = 0;
  var lastPageRowCount = rowCount % pageRows;
  if(lastPageRowCount == 0)
  {
    lastPageRowCount = pageRows;
  }//if(lastPageRowCount == 0)

  if(rowCount - lastPageRowCount + 1 < 1)
  {
    startRowNo = 1;
  }//if(rowCount - lastPageRowCount + 1 < 1)
  else
  {
    startRowNo = rowCount - lastPageRowCount + 1;
  }//else

  setRow(dataset, startRowNo);
  setResponseURL(src);
  submitForm(window, "fwForm");
}//function doTableEditLastPageOnClick(dataset, src)

/**
 * Process the table edit on load event.
 */
function doEmployeeTableEditOnLoad(targetFrame)
{
  var targetForm = targetFrame.document.forms["fwForm"]

  /* Get the paging control variables.*/
  rowNo = parseInt(targetForm.elements["fwRowNo"].value);
  rowCount = parseInt(targetForm.elements["fwRowCount"].value);
  pageRows = parseInt(targetForm.elements["fwPageRows"].value);

}//function doEmployeeTableEditOnLoad(targetFrame)

/**
 * Process the doAfterCommonOnLoad event to display any server message.
 */
function doAfterCommonOnLoad(targetFrame)
{
  var messageText;

  if(targetFrame.document.forms["fwForm"].elements["fwMessageText"] != null)
  {
    messageText = targetFrame.document.forms["fwForm"].elements["fwMessageText"].value;
    if(messageText != "")
    {
      messageText = messageText.substring(0, messageText.length - 1);
      alert("Server message: " + messageText);
    }//if(messageText != "")
  }//if(targetFrame.document.forms["fwForm"].elements["fwMessageText"].value != "")
}//function doAfterCommonOnLoad(targetFrame)


