/**
 * 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);
    
    /*
    Correctly handle PNG transparency in Win IE 5.5 & 6.
    http://homepage.ntlworld.com/bobosola. Updated 18-Jan-2006.
    */

    if ( navigator.appVersion.indexOf('MSIE') != -1 )
    {
       var arVersion = navigator.appVersion.split('MSIE')
       var version = parseFloat(arVersion[1])

       if ((version >= 5.5) && (document.body.filters)) 
       {
          for(var i=0; i<document.images.length; i++)
          {
             var img = document.images[i]
             var imgName = img.src.toUpperCase()
             if (imgName.substring(imgName.length-3, imgName.length) == 'PNG')
             {
                var imgID = ((img.id) ? 'id="' + img.id + '" ' : '');
                var imgClass = ((img.className) ? 'class="' + img.className + '" ' : '');
                var imgTitle = ((img.title) ? 'title="' + img.title + '" ' : 'title="' + img.alt + '" ');
                var imgStyle = 'display:inline-block;' + img.style.cssText;
                if (img.align == 'left') imgStyle = 'float:left;' + imgStyle;
                if (img.align == 'right') imgStyle = 'float:right;' + imgStyle;
                if (img.parentElement.href) imgStyle = 'cursor:hand;' + imgStyle;
                var strNewHTML = '<span ' + imgID + imgClass + imgTitle
                  + ' style="' + 'width:' + img.width + 'px; height:' + img.height + 'px;' + imgStyle + ';'
                  + 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader'
                  + '(src=\'' + img.src + '\', sizingMethod=\'scale\');"></span>' ;
                img.outerHTML = strNewHTML;
                i = i-1;
             }
          }
       }
    }
 }


/**
 * 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 == '' ) { 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
      }
   }
}
