/**
 * This file holds the keep-connection-alive or "heart beart" which
 * is targetted for loggers with the built-in modem.  When working
 * in dex (Jango, Montecito, Needa and Logger home) the pages remain
 * static, from a communiation to the logger point of view.  Thus 
 * the modem will close the connection to the internet if no activity
 * is detected.  This is somewhat of a disaster if the user is relying
 * on the connection to the logger remainaing, so when the user eventually
 * tried to save a config to the logger only to find out that the
 * logger has switch the modem off and you're in limbo.
 * 
 * So this file sends a regular request to the logger to retrieve
 * the logger's time.  This request will keep the modem connected to
 * the Internet and allow the use to continue to work with the logger
 * even after extended periods of non-communicado.
 */


// number of seconds between heart-beats.
var HEART_BEAT_PERIOD_S = 60


// number of seconds after the JSON request was sent before declaring the request as failed.
var TIMEOUT_S = 30


// start the periodic timer to send a request for the logger's time.
var intervalId = setInterval( postRpc, HEART_BEAT_PERIOD_S * 1000 );


/**
 * Post the RPC to the logger.
 */
function postRpc(  )
{
	var req =   {
					type: "POST",
					url: '/services/rpc/json/rpc.cmd',
					data: "{ 'method':'queryLogger','id':1,'params':['system.date_time'] }",
					error: handleError,
					success: handleSuccess,
					timeout: TIMEOUT_S * 1000
				};
	
	jQuery.ajax( req );
}


/**
 * The connection was confirmed.  However, if the return was from,
 * oh I don't know, a recalitrant firewall which inervenes with its
 * own return pages rather than just letting the browser handle it
 * like everywhere else, also treat as an error.
 * 
 * @param data
 * The data returned by the server.
 * 
 * @param code
 * Will contain the string "success".
 * 
 * @param xhr
 * Contains lots of return information such as: readyState, responseText
 * status and statusText.
 */
function handleSuccess( data, code, xhr )
{
	if ( data === null || 200 != xhr.status )
	{
		processConnectionLost( xhr.status );
	}
}


/**
 * The connection was lost.
 * 
 * @param event
 * Contains lots of return information such as: readyState, responseText
 * status and statusText.
 * 
 * @param xhr
 * Will contain the string that caused the error.
 * 
 * @param ajaxOpts
 * Will contain the string that caused the error.
 * 
 * @param thrownError
 * ?
 */
function handleError( event, xhr, ajaxOpts, thrownError )
{
	processConnectionLost( event.status );
}


/**
 * The connection was lost.  Inform the user if appropriate.
 * 
 * @param code
 * The return code from the failed request.
 */
function processConnectionLost( status )
{
	if ( -1 != document.location.pathname.indexOf( "jango" ) )
	{
		var returnString = "Warning - The connection to the logger has been lost!\n\nTo prevent losing any configuration changes you have made, save your current configuration to disc.\n\nStatus return code = " + status;
		
		alert( returnString );
	}
	
	clearInterval( intervalId );	
}
