// +------------------------------------------------------------------------+
// | goCalendar                                                             |
// +------------------------------------------------------------------------+
// | Copyright (c) 2005 Starfield Technologies                              |
// | Authors       Vincent Fiduccia, David Koopman                          |
// | Email         support@starfieldtech.com                                |
// | Web           http://www.starfieldtech.com                             |
// +------------------------------------------------------------------------+
// | This source file is subject to version 1.0 of the Starfield            |
// | Technologies, Inc. License, that is available at                       |
// | http://www.starfieldtech.com/changme/to/license.txt                    |
// | that is available at http://www.php.net/license/3_0.txt.               |
// +------------------------------------------------------------------------+
//

// IE requires some events to be in window, and others in document.
// Don't delete the leading and trailing commas!
var window_events = ',onload,onunload,onresize,onscroll,onresizestart,onresizeend,';
var document_events = ',onclick,ondblclick,onmousedown,onmousemove,onmouseover,onmouseout,onmouseup,onmousewheel,'+
                      'ondrag,ondrop,onkeydown,onkeyup,onkeypress,'+
                      'onbeforecut,oncut,onbeforepaste,onpaste,'+
                      'oncontextmenu,onselectionchange,onstop,';

var events = new Array();

function AddEvent(ev,func)
{
//alert("AddEvent('"+ev+"','"+func+"'");
  if ( ! func ) return;

  // Decide what object to add the event to
  var which;
  if ( window_events.indexOf(','+ ev +',') >= 0 )        which = 'window';
  else if ( document_events.indexOf(','+ ev +',') >= 0)  which = 'document';
  else { alert( '"' + ev + '" needs to be defined in the list of event mappings'); return; }

  // Make an empty array of event actions if one doesn't exist already
  if ( ! events[ev] )
    events[ev] = Array();

  // Add the function definition and make it the handler for the event if it's not already assigned
  if ( !eval(which+'.'+ev) || !eval(which+'.'+ev).toString().indexOf("DoEvent") > 0 )
  {
    // Copy the existing event handler into the event queue
    if ( eval(which+'.'+ev) )
      events[ev][events[ev].length] = eval(which+'.'+ev);
    
    // Replace the event handler with our own
    // Don't change the name of the function without changing the indexOf above!
//    eval(which+'.'+ev + ' = function DoEvent_'+ev+'(a0,a1,a2,a3) { DoEvent("'+ev+'",a0,a1,a2,a3); }');
    eval(which+'.'+ev + ' = function (a0,a1,a2,a3) { DoEvent("'+ev+'",a0,a1,a2,a3); }');
  }
  events[ev][events[ev].length] = func;
}

// Event handler function
function DoEvent(ev,a0,a1,a2,a3) // Passed the name fo the event and upto 4 params
{
  for (var i=0 ; i < events[ev].length ; i++)
  {
    if ( typeof(events[ev][i]) == "function" )
      events[ev][i](a0,a1,a2,a3);
    else
      eval( events[ev][i] );
  }
}

