
  /* ---------------------------------------------------------------------------

    Copyright (C) 2009   Vintage Media Ltd   www.vintagewebworks.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 3 as
    published by the Free Software Foundation.

    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 General Public License for more details.

    A copy of the GNU General Public License is included in the 'docs' folder
    and online at: http://www.gnu.org/licenses/gpl.txt

  --------------------------------------------------------------------------- */


  /*

    SCRIPT INFO:
    ************

    Description:    General javascripts to be loaded for every website page
    Revision No:    002
    Created:        01 Jan 2009
    Last updated:   14 Apr 2009
    Author:         Vintage Web Works - www.vintagewebworks.com
    Help:           www.typepublish.org/v1/manual

  */


  window.addEvent('domready', function() {

    /* Fade-on-hover effect for links and buttons: */

    $each($$('a'), function(el) {
      /* If class contains 'nofade' it should not have the fade-effect applied */
      if (!el.hasClass('nofade')) {
        el.setStyle('opacity', '0.7');
        var morph = new Fx.Morph(el,{ 'duration':'300', link:'cancel' });
        el.addEvents({
          'mouseenter' : function() { morph.start({ 'opacity':'1' }) },
          'mouseleave' : function() { morph.start({ 'opacity': '0.8' }) }
        });
      }
    });

    $each($$('button'), function(el) {
      el.setStyle('opacity', '0.7');
      var morph = new Fx.Morph(el,{ 'duration':'300', link:'cancel' });
      el.addEvents({
        'mouseenter' : function() { morph.start({ 'opacity':'1' }) },
        'mouseleave' : function() { morph.start({ 'opacity': '0.7' }) }
      });
    });

  });



  function submitForm(el) {

    // Ajaxify all input forms
//    $each($$('.input-form'), function(el) {
      //alert('Input form loaded');
//      el.addEvent('submit', function(e) {

//        alert('Boo');

//        e.stop(); // Prevent default form submit from going ahead
        var tpui_messages = $('tpui-messages'); // Set form_error div
        tpui_messages.empty()
        tpui_messages.removeClass('clean'); tpui_messages.removeClass('info'); tpui_messages.removeClass('success');
        tpui_messages.removeClass('warning'); tpui_messages.removeClass('error'); tpui_messages.addClass('tpui-waiting');
        tpui_messages.setStyle('display', '');
        new Fx.Scroll(window).toTop(); // Return to top
        $(el).set('send', { // 'this' refers to $('input-form')
//        this.set('send', { // 'this' refers to $('input-form')
          onComplete: function(response) {
            var data = eval('(' + response + ')'); // JSON will be returned
            var end_result = 'success'; // Will change to 'invalid' on fail validation of 'fail' on system/database failure
            if (data.results.length > 0) { // There should always be results of some sort
              for (var i=0;i<data.results.length;i++) {
                if (data.results[i].errors) { // Deal with errors
                  end_result = 'invalid';
                  for (var e=0;e<data.results[i].errors.length;e++) {
                    var input_id = data.results[i].errors[e].error.item; // Highlight field with error
                    var note_id = data.results[i].errors[e].error.item + '_note'; // Show error message in field 'note'
                    if (input_id != 'system_error') { // This error message is used when data passed validation but there was a problem saving to the database
                      $(note_id).innerHTML = data.results[i].errors[e].error.message;
                      $(note_id).addClass('error');
                    }
                    else if (input_id == 'system_error') {
                      end_result = 'fail';
                    }
                  }
                } else if (data.results[i].onsuccess) { // Grab message for onsuccess and onfailure
                  var msg_onsuccess = data.results[i].onsuccess;
                } else if (data.results[i].oninvalid) {
                  var msg_oninvalid= data.results[i].oninvalid;
                } else if (data.results[i].onfailure) {
                  var msg_onfailure = data.results[i].onfailure;
                }
              }
              tpui_messages.removeClass('tpui-waiting');
              if (end_result == 'success') {
                tpui_messages.addClass('success');
                tpui_messages.set('html', '<p>' + msg_onsuccess + '</p>');
              } else if (end_result == 'invalid') {
                tpui_messages.addClass('warning');
                tpui_messages.set('html', '<p>' + msg_oninvalid + '</p>');
              } else if (end_result == 'fail') {
                tpui_messages.addClass('error');
                tpui_messages.set('html', '<p>' + msg_onfailure + '</p>');
              }
            }
          }
        });
//        this.send(); // Send the form
        $(el).send();
//      });
//    });

//  });

  }
