/* This Javascript codes requires prototype.js and MMTDiv.js to already be loaded. */

/* An array of MMTDivs instances.  */
var my_divs = [];

/* The values returned from the miniserver will be put into this array, called "d" (for data).  */
/* The array needs to be in the global namespace. */
var d = [];

/* This array can be used in miniserver2updater2.php for the keys returned by the miniserver.   */
var d_keys = [];

/* You need to assign this, as necessary, in your web page!!! */
/* The URL to get the data. */
var my_updater_url;

/* The updater refresh interval in seconds. Can be changed by the user. */
var my_updater_interval = 1;

/* You need to add array elements to "assign_ids" in your web page!!! */
/* The ids of HTML elements to be assigned to MMTDiv instances. */
var assign_ids = [];

/* The setInterval() id for our requester task.  Only used internally. */
var requester_id;

/*  Assign MMDiv instances.  Called by start() */
function assign() 
{
    var index;
    for (var key in assign_ids) {
	index=assign_ids[key];
	my_divs[index] = new MMTDiv( index );
    }
}

/* Make the ajax request.  Return Javascript that is evaluated upon the return.  
 * Use a "Content-type" header of "text/javascript" and 
 * the Javascript will be evaluated automatically. */ 
function requester() 
{
    start_time = new Date().getTime();
    var my_ajax = new Ajax.Request( my_updater_url, {
       method: 'get',
         onSuccess: function(transport) {
            transport.responseText.evalJSON();
         }
    } );
}

/* Restarting the requester task. */
function restart()
{
    stop();
    start();
}

/* Stopping the requester task. Callback for window.onunload(). */
function stop() 
{
    clearInterval( requester_id );
}

/* Starting the requester task. Callback for window.onload(). */
function start () 
{
    assign();

    /* Setting a new update interval, if appropriate. */
    /* Assumes as set of radio buttons, called "duration", in a form called "main" */
    if ( document.forms && document.forms.main && document.forms.main.duration  ) {
	if (document.forms.main.duration[0].checked) {
	    my_updater_interval = 1;
	} else if (document.forms.main.duration[1].checked) {
	    my_updater_interval = 5;
	} else  if (document.forms.main.duration[2].checked ) {
	    my_updater_interval = 30;
	}
    }
    
    if ( document.forms && document.forms.radio_buttons && document.forms.radio_buttons.duration  ) {
	if (document.forms.radio_buttons.duration[0].checked) {
	    my_updater_interval = 5;
	} else if (document.forms.radio_buttons.duration[1].checked) {
	    my_updater_interval = 30;
	} 
    }

    /* Start a request. */
    requester();
    /* And, keep doing it! */
    requester_id = window.setInterval("requester();", my_updater_interval * 1000);    
}


// Returns the number of the key that was pressed.  Used for capturing the "Enter"
// key from a text field.
function getkey(e)
{
    // Account for different browsers.
    if (window.event)
	// Microsoft
	return window.event.keyCode;
    else if (e)
	// non-Microsoft
	return e.which;
    else
	return null;
}


// Checks if the key pressed was the "enter" key (for the text fields).
function checkEnter(e) { //e is event object passed from function invocation
    var characterCode // literal character code will be stored in this variable
    
	characterCode = getkey(e)
    
	if( characterCode == 13 ){ //if generated character code is equal to ascii 13 (if enter key)
	    return true;
	} else {
	    return false;
	}
  
}
