top.bind = bind = function bind( obj, payload ) {
    Element.extend(obj);
    if ( top.valid (payload) ) {
      if ( typeof payload == 'function' ) {
        payload( obj );
      } else if ( typeof payload == 'object' ) {
        if ( top.valid( payload.action ) && payload.action == 'function' ) {
          payload.action();
        }
      } else {
        payload = obj;
      }
    }
    return obj;
}

Array.prototype.append = function (obj) {
    this[this.length] = obj;
}

/*
 * appendElement
 *
 * Recursively clones and then injects an arbitrary HTML DOM Element into a specified area
 *
 * arguments:
 *   destinationID   id of destination element
 *   templateID      id of element wrapping the template to use
 *   position        prototype js position type (i.e., bottom, top, etc)
 */
function appendElement( destinationID, templateID, position ) {
    var destination = $(destinationID);
    var template = $(templateID);
    
    if ( valid( destination ) && valid( template ) ) {
        var elem = template.cloneNode(true);
        elem.writeAttribute('id', false);
        elem.identify();
        obj = {};
        obj['' + position] = elem;
        top.newElem = destination.insert(obj);
    }
}

top.toNumeric = toNumeric = function ( candidate ) {
    return isNumeric( candidate ) ? parseFloat( candidate ) : 0;
}

top.isNumeric = isNumeric = function ( candidate ) {
    var decRE = new RegExp("^[^\\.]*\\.?[^\\.]*$");
    var digitRE = new RegExp( '^[\\-\\+]?[\\d\\.]+$' );
    return decRE.match( candidate ) && digitRE.match( candidate );
}

top.valid = valid = function ( obj ) {
    return obj != null && typeof obj != 'undefined' && obj;
}

/*
=============================
    Class Updater
    
    As PrototypeJS's PeriodicalUpdater has issues with multiple 
    updater threads being called upon ajax completion, we use this class
    to wrap around a method that is closer to the native settimeout/setinterval 
    functions.
=============================    
*/
top.Updater = Updater = function Updater (divToUpdate, interval, url, params)
{    
       this.divToUpdate = divToUpdate;  
       this.interval = interval;
       this.url = url;
       this.params = params;
       this.executor = new PeriodicalExecuter(this.getUpdate.bind(this), this.interval);
};

/*
    Updater.stop()
*/
top.Updater.prototype.stop = function() {
   /* if ( top.valid(top.debug) ) { top.debug.log('#############  calling halt', this); } */
   if (this.executor) {
       this.executor.stop();
   }
};

/*
    Updater.getUpdate()
*/
top.Updater.prototype.getUpdate = function() {
   var options = {
       method: "POST",
       asynchronous: true,
       parameters: this.params,
       evalScripts: true,
       evalJSON: true,
       divToUpdate: this.divToUpdate,
       onComplete: function (obj, Json) {
           try {
                /*
                As we call AJAX.Updater, the only code we need here is anything additional to 
                the html arriving downstream.
                
                Debug logs here use string 'updater' as context since the "this" keyword 
                here is different due to the asynchronous usage of onComplete 
                */
                /* if ( top.valid(top.debug) ) { top.debug.log('updater iteration complete', 'Updater'); } */
                if ( obj.requestJSON ) {
                    json = obj.requestJSON;
                    if ( json.payload ) {
                        /* if ( top.valid(top.debug) ) { top.debug.log('have json payload', 'Updater'); } */
                        top.headeractions( json );
                    }
                }
           } catch ( updateE ) {
                if ( top.valid(top.debug) ) { top.debug.log('ERROR: ' + updateE, this); }
           }
       }
   };
   var oRequest = new Ajax.Updater(
        options.divToUpdate, 
        this.url, 
        options
   );
};

top.radioGroupValue = radioGroupValue = function radioGroupValue( groupName ) {
    var val = '';
    if ( ! top.valid( groupName ) ) return val;
    var selector = 'input[name="' + groupName + '"]';
    var group = $$(selector);
    if ( group.length <= 0 ) return val;
    var elem = group.detect( function( e ) { 
        var checked = false;
        try {
            checked = e.type == 'radio' && typeof e.checked == 'boolean' && e.checked;
        } catch (e) {}
        return checked;
    } );
    if (top.valid(elem)) val = elem.getValue();
    return val;
}
