//
// JavaScript code for the management of nice dialog boxes under any web browser.
//
// Author:  OrdinaSoft
//          Patrick Lanz
//          Lausanne
//          info@ordinasoft.ch
//
// First version: May 1st, 2007
//
// (c) 2007, OrdinaSoft, all rights reserved

//-----------------------------------------------------------------------------------------------
// Namespace initialization.

if (typeof OrdinaSoft == 'undefined')
  OrdinaSoft = new Object ();
if (typeof OrdinaSoft.DialogBox == 'undefined')
  OrdinaSoft.DialogBox = new Object ();

//-----------------------------------------------------------------------------------------------
// Global variables.

OrdinaSoft.DialogBox._PageMask = null;
OrdinaSoft.DialogBox._Stack = [];
OrdinaSoft.DialogBox.TitleClass = '';                 // CSS class for title
OrdinaSoft.DialogBox.ContentClass = '';               // CSS class for content
OrdinaSoft.DialogBox.ImageRoot = 'Images/DialogBox';  // base URL for images
OrdinaSoft.DialogBox.Opacity = 0.25;                  // opacity for the mask
OrdinaSoft.DialogBox.BtnClass = '';                   // CSS class for the buttons
OrdinaSoft.DialogBox.OkBtnText = 'OK';                // Text for 'OK' button
OrdinaSoft.DialogBox.CancelBtnText = 'Cancel';        // Text for 'Cancel' button
OrdinaSoft.DialogBox.YesBtnText = 'Yes';              // Text for 'Yes' button
OrdinaSoft.DialogBox.NoBtnText = 'No';                // Text for 'No' button

//-----------------------------------------------------------------------------------------------
// Dialog box constructor.

// Creates a dialog box, without displaying it.
//  - Content is the content of the dialog box.
//  - Title is the optional title of the dialog box.

OrdinaSoft.DialogBox.DialogBox = function () {

  this.Content = '&nbsp;';
  this.Title = '&nbsp;';
  this.FocusElmt = null;

  return true;
} // OrdinaSoft.DialogBox.DialogBox

//-----------------------------------------------------------------------------------------------
// Format of the dialog box.

// Formats the dialog box and returns a <div> element with the content of the dialog box.

OrdinaSoft.DialogBox.DialogBox.prototype._Format = function () {

  var Box = document.createElement ('div');
  var Style = Box.style;
  Style.position = 'fixed';
  Style.left = 0;
  Style.top = 0;
  Style.width = '100%';
  Style.height = '100%';

  var s = [];
  var Dir = OrdinaSoft.DialogBox.ImageRoot + '/';
  s [s.length] = '<table border="0" cellspacing="0" cellpadding="0" ' +
                   'style="height: 100%; width: 100%">';
  s [s.length] = '  <tr style="height: 100%; width: 100%">';
  s [s.length] = '    <td align="center" style="height: 100%; width: 100%">' +
                 '      <table border="0" cellpadding="0" cellspacing="0">';
  s [s.length] = '        <tr>';
  s [s.length] = '          <td><img src="' + Dir + 'TopLeft.gif" /></td>';
  s [s.length] = '          <td style="background-image: url(' + Dir + 'Top.gif)"></td>';
  s [s.length] = '          <td><img src="' + Dir + 'TopRight.gif" /></td>';
  s [s.length] = '        </tr>';
  s [s.length] = '        <tr>';
  s [s.length] = '          <td style="background-image: url(' + Dir + 'Left.gif)"></td>';
  s [s.length] = '          <td><div ' +
                                  ((OrdinaSoft.DialogBox.TitleClass == '') ?
                                    '' :
                                    'class="' + OrdinaSoft.DialogBox.TitleClass + '" ') +
                                    'style="background-color: #E4F1FF; cursor: default; ' +
                                           'padding: 2px; padding: 2px; ' +
                                           'padding-bottom: 5px; text-align: left">' +
                                this.Title + '</div></td>';
  s [s.length] = '          <td style="background-image: url(' + Dir + 'Right.gif)"></td>';
  s [s.length] = '        </tr>';
  s [s.length] = '        <tr>';
  s [s.length] = '          <td style="background-image: url(' + Dir + 'Left.gif)"></td>';
  s [s.length] = '          <td><table border="0" cellpadding="0" cellspacing="0" width="100%">';
  s [s.length] = '              <tr>';
  s [s.length] = '                <td><img src="' + Dir + 'TopLeft2.gif" /></td>';
  s [s.length] = '                <td style="background-image: url(' + Dir + 'Top2.gif)"></td>';
  s [s.length] = '                <td><img src="' + Dir + 'TopRight2.gif" /></td>';
  s [s.length] = '              </tr>';
  s [s.length] = '              <tr>';
  s [s.length] = '                <td style="background-image: url(' + Dir + 'Left2.gif); ' +
                                            'width: 2px"></td>';
  s [s.length] = '                <td><div ' +
                                        ((OrdinaSoft.DialogBox.ContentClass == '') ?
                                          '' :
                                          'class="' + OrdinaSoft.DialogBox.ContentClass + '" ') +
                                          'style="background-color: #FFFFFF; padding: 10px">' +
                                     this.Content + '</div></td>';
  s [s.length] = '                <td style="background-image: url(' + Dir + 'Right2.gif); ' +
                                            'width: 2px">' +
                                 '</td>';
  s [s.length] = '              </tr>';
  s [s.length] = '              <tr>';
  s [s.length] = '                <td><img src="' + Dir + 'BottomLeft2.gif" /></td>';
  s [s.length] = '                <td style="background-image: url(' + Dir + 'Bottom2.gif)"></td>';
  s [s.length] = '                <td><img src="' + Dir + 'BottomRight2.gif" /></td>';
  s [s.length] = '              </tr>';
  s [s.length] = '            </table></td>';
  s [s.length] = '          <td style="background-image: url(' + Dir + 'Right.gif)"></td>';
  s [s.length] = '        </tr>';
  s [s.length] = '        <tr>';
  s [s.length] = '          <td><img src="' + Dir + 'BottomLeft.gif" /></td>';
  s [s.length] = '          <td style="background-image: url(' + Dir + 'Bottom.gif)"></td>';
  s [s.length] = '          <td><img src="' + Dir + 'BottomRight.gif" /></td>';
  s [s.length] = '        </tr>';
  s [s.length] = '      </table></td>';
  s [s.length] = '  </tr>';
  s [s.length] = '</table>';

  Box.innerHTML = s.join ('\r\n');
  return Box;
} // OrdinaSoft.DialogBox.DialogBox.prototype._Format

//-----------------------------------------------------------------------------------------------
// Buttons.

// Returns HTML code with an OK button.
//  - idOK is the identifier of the OK button. Its default value is dlg_OK.
//  - fOK is the name of the function to call when the OK button is pressed. Its default value is
//    to close the dialog box.

OrdinaSoft.DialogBox.GetOK = function (idOK, fOK) {

  if (idOK == null)
    idOK = 'dlg_OK';
  if (fOK == null)
    fOK = 'OrdinaSoft.DialogBox.Close ()';

  var s = [];
  s [s.length] = '<div style="height: 10px">&nbsp;</div>';
  s [s.length] = '<div align="center">';
  s [s.length] = '  <input type="button" ' +
                           ((OrdinaSoft.DialogBox.BtnClass == '') ?
                             '' :
                             'class="' + OrdinaSoft.DialogBox.BtnClass + '" ') +
                          'id="' + idOK + '" ' +
                          'value="' + OrdinaSoft.DialogBox.OkBtnText + '" ' +
                          'onclick="' + fOK + '" />';
  s [s.length] = '</div>';

  return s.join ('\r\n');
} // OrdinaSoft.DialogBox.GetOK



// Returns HTML code for 2 buttons.
//  - idFirst is the identifier of the first button.
//  - TxtFirst is the text for the first button.
//  - fFirst is the name of the function to call when the first button is pressed. Its default
//    value is to close the dialog box.
//  - idSecond is the identifier of the second button.
//  - TxtSecond is the text for the second button.
//  - fSecond is the name of the function to call when the second button is pressed. Its default
//    value is to close the dialog box.

OrdinaSoft.DialogBox._Get2Buttons = function
  (idFirst, TxtFirst, fFirst, idSecond, TxtSecond, fSecond) {

  if (fFirst == null)
    fFirst = 'OrdinaSoft.DialogBox.Close ()';
  if (fSecond == null)
    fSecond = 'OrdinaSoft.DialogBox.Close ()';

  var ClassTxt = (OrdinaSoft.DialogBox.BtnClass == '') ?
                  '' :
                  'class="' + OrdinaSoft.DialogBox.BtnClass + '" ';

  var s = [];
  s [s.length] = '<div style="height: 10px">&nbsp;</div>';
  s [s.length] = '<div align="center">';
  s [s.length] = '  <input type="button" ' + ClassTxt +
                          'id="' + idFirst + '" ' +
                          'value="' + TxtFirst + '" ' +
                          'onclick="' + fFirst + '" />';
  s [s.length] = '  &nbsp;&nbsp;';
  s [s.length] = '  <input type="button" ' + ClassTxt +
                          'id="' + idSecond + '" ' +
                          'value="' + TxtSecond + '" ' +
                          'onclick="' + fSecond + '" />';
  s [s.length] = '</div>';

  return s.join ('\r\n');
} // OrdinaSoft.DialogBox._Get2Buttons



// Returns HTML code with an OK and a Cancel button.
//  - idOK is the identifier of the OK button. Its default value is dlg_OK.
//  - fOK is the name of the function to call when the OK button is pressed. Its default value is
//    to close the dialog box.
//  - idCancel is the identifier of the Cancel button. Its default value is dlg_Cancel.
//  - fCancel is the name of the function to call when the Cancel button is pressed. Its default
//    value is to close the dialog box.

OrdinaSoft.DialogBox.GetOKCancel = function (idOK, fOK, idCancel, fCancel) {

  if (idOK == null)
    idOK = 'dlg_OK';
  if (idCancel == null)
    idCancel = 'dlg_Cancel';
  return OrdinaSoft.DialogBox._Get2Buttons
    (idOK, OrdinaSoft.DialogBox.OkBtnText, fOK,
     idCancel, OrdinaSoft.DialogBox.CancelBtnText, fCancel);
} // OrdinaSoft.DialogBox.GetOKCancel



// Returns HTML code with a Yes and a No button.
//  - idYes is the identifier of the Yes button. Its default value is dlg_Yes.
//  - fYes is the name of the function to call when the Yes button is pressed. Its default value
//    is to close the dialog box.
//  - idNo is the identifier of the No button. Its default value is dlg_No.
//  - fNo is the name of the function to call when the No button is pressed. Its default value is
//    to close the dialog box.

OrdinaSoft.DialogBox.GetYesNo = function (idYes, fYes, idNo, fNo) {

  if (idYes == null)
    idYes = 'dlg_Yes';
  if (idNo == null)
    idNo = 'dlg_No';
  return OrdinaSoft.DialogBox._Get2Buttons
    (idYes, OrdinaSoft.DialogBox.YesBtnText, fYes,
     idNo, OrdinaSoft.DialogBox.NoBtnText, fNo);
} // OrdinaSoft.DialogBox.GetYesNo

//-----------------------------------------------------------------------------------------------
// Showing and closing the dialog box.

// Shows the dialog box.

OrdinaSoft.DialogBox.DialogBox.prototype.Show = function () {

  OrdinaSoft.DialogBox._ShowMask ();
  this.Box = this._Format ();
  OrdinaSoft.DialogBox._Stack [OrdinaSoft.DialogBox._Stack.length] = this;
  document.forms [0].appendChild (this.Box);

  if (this.FocusElmt != null) {
    var FocusElmt = document.getElementById (this.FocusElmt);
    if (FocusElmt != null)
      FocusElmt.focus ();
  }  // this.FocusElmt != null

  return true;
} // OrdinaSoft.DialogBox.DialogBox.prototype.Show



// Closes the last opened dialog box.

OrdinaSoft.DialogBox.Close = function () {

  var Stack = OrdinaSoft.DialogBox._Stack;
  if (Stack.length != 0) {
    var Obj = Stack [Stack.length - 1];
    if (Obj != null) {
      var Dlg = Obj.Box;
      if (Dlg != null)
        Dlg.parentNode.removeChild (Dlg);
      if (typeof Obj.OnClose == 'function')
        Obj.OnClose ();
    }  // Obj != null
    Stack.length = Stack.length - 1;
    if (Stack.length == 0)
      OrdinaSoft.DialogBox._HideMask ();
  }  // OrdinaSoft.DialogBox._Stack.length != 0

  return true;
} // OrdinaSoft.DialogBox.Close



OrdinaSoft.DialogBox.DialogBox.prototype.Close = function () {

  return OrdinaSoft.DialogBox.Close ();
} // OrdinaSoft.DialogBox.DialogBox.prototype.Close

//-----------------------------------------------------------------------------------------------
// Dialog box tools.

// Indicates if a dialog box is actually displayed.
//  - The return value is true if a dialog box is displayed and false otherwise.

OrdinaSoft.DialogBox.IsDialogBoxDisplayed = function () {

  return OrdinaSoft.DialogBox._Stack.length != 0;
} // OrdinaSoft.DialogBox.IsDialogBoxDisplayed

//-----------------------------------------------------------------------------------------------
// Message boxes.

// Message Box constructor.
//  - Message is the message to display.
//  - Title is the title of the message box. Its default value is an empty text.
//  - Button is the HTML code for the button(s). Its default value is to have only an OK button.
//  - Icon is the icon to display in the message box. Its default value is to display no icon.

OrdinaSoft.DialogBox.MessageBox = function (Message, Title, Button, Icon) {

  // Gets the content
  var s = [];
  s [s.length] = '<table border="0" cellpadding="0" cellspacing="0">';
  s [s.length] = '  <tr>';

  if (Icon != null) {  // we have an icon
    s [s.length] = '    <td valign="center">' +
                        '<img src="' + OrdinaSoft.DialogBox.ImageRoot + '/' + Icon + '.gif" />' +
                       '</td>';
    s [s.length] = '    <td width="15">&nbsp;</td>';
  }  // typeof Icon != 'undefined'

  s [s.length] = '    <td>' + Message + '</td>';
  s [s.length] = '  </tr>';
  s [s.length] = '</table>';

  if (Button == null) { // default button
    Button = OrdinaSoft.DialogBox.GetOK ('mb_OK');
    this.FocusElmt = 'mb_OK';
  }  // Button == null
  s [s.length] = Button;
  this.Content = s.join ('\r\n');

  // Title
  this.Title = Title;

  return true;
} // OrdinaSoft.DialogBox.MessageBox

OrdinaSoft.DialogBox.MessageBox.prototype = new OrdinaSoft.DialogBox.DialogBox ();



// Displays an information message box, with a 'Question' icon.
//  - Message is the message to display in the message box.
//  - Title is the optional title of the message box.
//  - fYes is the function to call if the result is 'Yes'.
//  - fNo is the function to call if the result is 'No'.
//  - The result is the message box just displayed.

OrdinaSoft.DialogBox.MessageBox.Question = function (Message, Title, fYes, fNo) {

  var Dlg = new OrdinaSoft.DialogBox.MessageBox
    (Message, Title, OrdinaSoft.DialogBox.GetYesNo ('mb_Yes', fYes, 'mb_No', fNo), 'Question');
  Dlg.Show ();
  return Dlg;
} // OrdinaSoft.DialogBox.MessageBox.Question



// Displays an information message box, with an 'Info' icon.
//  - Message is the message to display in the message box.
//  - Title is the optional title of the message box.
//  - The result is the message box just displayed.

OrdinaSoft.DialogBox.MessageBox.Info = function (Message, Title) {

  var Dlg = new OrdinaSoft.DialogBox.MessageBox (Message, Title, null, 'Info');
  Dlg.Show ();
  return Dlg;
} // OrdinaSoft.DialogBox.MessageBox.Info



// Displays an error message box, with an 'Error' icon.
//  - Message is the message to display in the message box.
//  - Title is the optional title of the message box.
//  - The result is the message box just displayed.

OrdinaSoft.DialogBox.MessageBox.Error = function (Message, Title) {

  var Dlg = new OrdinaSoft.DialogBox.MessageBox (Message, Title, null, 'Error')
  Dlg.Show ();
  return Dlg;
} // OrdinaSoft.DialogBox.MessageBox.Info

//-----------------------------------------------------------------------------------------------
// Displaying and hiding the mask.

// Private function to display the mask on top of the user page.

OrdinaSoft.DialogBox._ShowMask = function () {

  if (OrdinaSoft.DialogBox._PageMask != null)
    return true; // have already a mask

  var Mask = document.createElement ('div');
  var Style = Mask.style;
  Style.backgroundColor = '#000000';
  Style.position = 'fixed';
  Style.left = 0;
  Style.top = 0;
  Style.width = '100%';
  Style.height = '100%';
  Style.filter = 'alpha(opacity=' + (100 * OrdinaSoft.DialogBox.Opacity).toString () + ')';
  Style.opacity = OrdinaSoft.DialogBox.Opacity;

  document.forms [0].appendChild (Mask);
  OrdinaSoft.DialogBox._PageMask = Mask

  return true;
} // OrdinaSoft.DialogBox._ShowMask



// Private function to hide the mask on top of the user page.

OrdinaSoft.DialogBox._HideMask = function () {

  var Elmt = OrdinaSoft.DialogBox._PageMask;
  if (Elmt != null) {
    Elmt.parentNode.removeChild (Elmt);
    OrdinaSoft.DialogBox._PageMask = null;
  }  // Elmt != null

  return true;
} // OrdinaSoft.DialogBox._HideMask

//-----------------------------------------------------------------------------------------------

OrdinaSoft_DialogBox_Initialized = true;  // indicates that the library is initialized
