/* 
 * Ads two functions to HTML file for showing generic form
 * there must be some specific elements in the form for this 
 * to work correctly.
 * All of the names refer to Field ID, where name can be different if so desired.
 * The only exception is form itself which only has name field set.
 *
 * First there must be button called bNew which is used to add new values to DB
 * then form name shoud be fEditForm
 * there should also be two hidden fields called hSubmitAction and hEditID which
 *    define what action to take on submit and if edit action is send allso ID of edited item
 * Lastly submit button must be defined and called bConfirm
 *
 * Example:
 *
 * <input type="button" name="bNew" id="bNew" value="Nov vnos" class="fbtn" onclick="showEditForm('new','name')"/>
 *
 * <form name="fEditForm" action="index.php?action=users" method="post">
 * <input type="hidden" id="hSubmitAction" name="hSubmitAction" >
 * <input type="hidden" id="hEditID" name="hEditID" >
 *
 * ... other form elements ...
 *
 * <input type="submit" id="bConfirm" name="bSendusers" class="fbtn" value="Dodaj" />
 * </form>
 *
 */


  /**
   * show edit form
   *
   * @param action string   - action to execute
   * @param inFocus string  - set focus to element
   * @param id integer      - ID of editing item if editing
   */
  function showEditForm(action, inFocus, id){
    var inFocus = (inFocus == null) ? null : inFocus;
    var id = (id == null) ? null : id;

    // set parameters
    if (id) document.getElementById('hEditID').value = id;
    document.getElementById('hSubmitAction').value = action; // set action

    // remove all errors if any
    for(i=0; i<document.forms['fEditForm'].elements.length; i++){
      hideHint(document.forms['fEditForm'].elements[i]);
    }

    // set visibility and focus
    setEditFormVisible(true,action);
    setFocusOn(inFocus);
  }

  /**
   * sets visibility of form
   *
   * @param visibility boolean  - set form visible or hidden
   * @param action string       - action to execute
   */
  function setEditFormVisible(visibility, action){
    var action = (action == null) ? null : action;
    showHideDiv('bNew',!visibility);
    showHideDiv('newbox',visibility);
    // set parameters for custom action
    if (action){
      if ("new" == action) document.getElementById('bConfirm').value = "Dodaj";
      else
      if ("edit" == action) document.getElementById('bConfirm').value = "Popravi";
    }
  }
  /*
 * asks for permision to execute delete action
 *
 * @param inEvent event - stop propagation of event if set
 * @return boolean      - confirmation of delete action
 */
function confirmDelete(inEvent, inDesc){
  var inEvent = (inEvent == null) ? null : inEvent; // mask input like default value
  var inDesc = (inDesc == null) ? "Ta akcija bo izbrisala obstoje\u010di izpis iz baze podatkov!\n Ali vseeno želite nadaljevati?" : inDesc; // mask input like default value
  stopPropagation(inEvent);
  retRes = confirm(inDesc);
  return retRes;
}
