/**
 * Common javascript
 *
 *    Copyright (C) 2007, Mike Tyson <mike@tzidesign.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * $Id$
 */



 /**
  * Initialise system
  */
 function init_common()
 {
    // Create spinner
    var spinner = document.createElement('div');
    spinner.setAttribute('id', 'spinner');
    spinner.className = 'spinner';
    spinner.style.display = 'none';
    document.getElementsByTagName('body')[0].appendChild(spinner);

    var spinnerHandler = {
        onCreate: function() {
            Effect.Appear('spinner');
        },
        onComplete: function() {
            Effect.Fade('spinner');
        }
    };

    Ajax.Responders.register(spinnerHandler);

    // Create notifier/dialog
    var outer = document.createElement('div');
    var inner = document.createElement('div');
    outer.setAttribute('id', 'dialog_outer');
    inner.setAttribute('id', 'dialog_inner');
    outer.style.display = 'none';
    document.getElementsByTagName('body')[0].appendChild(outer);
    outer.appendChild(inner);
 }


/**
 * Report success
 */
function reportSuccess(summary, xmlDoc)
{
   notify(summary);
      
   var script;
   try {
      script =  xmlDoc.getElementsByTagName('script')[0].childNodes[0].nodeValue;
   } catch ( e ) { /* Ignore */ }
   
   if ( script != '' )
   {
      // We have some javascript to run
      try {
         eval(script);
      } catch ( e ) {
         // Ignore the exception
      }
   }
}

/**
 * Make URL from form elements
 *
 * Returns string of '&name=value' for each form element.
 *
 * @param form Form object
 */
function makeURLFromForm(form)
{
   var url='';
   var elements = form.elements;
   for ( var i=0; i<elements.length; i++ )
   {
      var elt = elements[i];
      var value = elt.value;
      if ( elt.type == 'checkbox' ) value = (elt.checked?value:'');
      if ( elt.type == 'radio' && !elt.checked ) continue;
      url += '&'+encodeURIComponent(elt.name)+'='+encodeURIComponent(value);
   }
   return url;
}

/** 
 * Notify
 */
function notify(message)
{
   var notifier_outer = $('dialog_outer');
   var notifier = $('dialog_inner');
   
   notifier_outer.className = 'notifier_outer';
   notifier.className = 'notifier';
   
   notifier.innerHTML = message;
   notifier_outer.style.top = '-100px';
   
   // Slide from top and fade in
   new Effect.Appear(notifier_outer, {to: notifier_outer.getOpacity()});
   new Effect.Move(notifier_outer, {y: 200, mode: 'absolute', transition: Effect.Transitions.sinoidal});
   
   new Effect.Fade(notifier_outer, {duration: 2});
}

/** 
 * Show dialog
 */
function dialog(html, instant)
{
   var dialog_outer = $('dialog_outer');
   var dialog = $('dialog_inner');
   
   if ( dialog_outer.style.display != 'none' && dialog_outer.className == 'floatingdialog_outer' )
   {
      // Dialog already shown - just change innerHTML
      dialog.innerHTML = html;
      return;
   }
   
   dialog_outer.className = 'floatingdialog_outer';
   dialog.className = 'floatingdialog';
   
   dialog.innerHTML = html;

   // Fade in
   if ( instant == true )
   {
      dialog_outer.style.display = 'block';
   }
   else
   {
      new Effect.Appear(dialog_outer, {to: dialog_outer.getOpacity(), duration: 0.2});
   }
}

function closeDialog()
{
   new Effect.Fade($('dialog_outer'), {duration: 1});
}


function reportFailure(summary, xmlDoc)
{
   var message;
   var script;
   try {
      message =  xmlDoc.getElementsByTagName('message')[0].childNodes[0].nodeValue;
      script =  xmlDoc.getElementsByTagName('script')[0].childNodes[0].nodeValue;
   } catch ( e ) { /* Ignore */ }

   if ( message == undefined ) { message = 'There was an unexpected error.'; }

   alert(summary+'\n\n'+message);
   
   if ( script != '' )
   {
      // We have some javascript to run
      try {
         eval(script);
      } catch ( e ) {
         // Ignore the exception
      }
   }
}
