// NOTE: we are expecting these global variables to already be populated
//		service_base_url
//		php_ua

///
/// make our global references
///

var debug_mode = false ;

// create our web service object
var ws_comm = new cows_ajax({
	server_url			: service_base_url + 'lite_compatibility_server.php',
	success_handler		: handle_server_success,
	failure_handler		: handle_server_failure,
	display_processing_message : display_server_processing_message,
	debug_handler		: debug
}) ;




/*----------------------------------------------------------------------------\
|                      WEB SERVICE EXTENSIONS
\----------------------------------------------------------------------------*/

///
/// NOTE: it's not necessary to extend the class in this way, but it's nice so that you can easily parition everything about this connection
///		from other connections if we wanted to make some
///

// extend the members
ws_comm.known_ua = false ;
ws_comm.user_agent = php_ua ;				// set from PHP
ws_comm.js_user_agent = navigator.userAgent.toLowerCase() ;
ws_comm.response_ws_start = false ;				// got word back form the server that we successfully updated the db
ws_comm.response_heartbeat1 = false ;
ws_comm.response_heartbeat2 = false ;
ws_comm.heartbeat_cycle = 0 ;
// toggle if they js and php don't match - always use the PHP UA, but we'll note the JS one if it's different
ws_comm.mismatched_ua = (ws_comm.js_user_agent != ws_comm.user_agent) ;


// extend the methods

// this is how javascript return scripts interface
cows_ajax.prototype.read=function( remote_response)
{
	display('<b>Server Response:</b> ' + remote_response) ;
};


// got a response from the start up call
cows_ajax.prototype.response_web_service_start=function( remote_response)
{
	this.response_ws_start = true ;
	display('<b>Server Response:</b> ' + remote_response) ;
};


// got a response from the start up call
cows_ajax.prototype.response_heartbeat=function( remote_response, beat)
{
	display('<b>Server Response:</b> ' + remote_response) ;

	// set toggles
	if (beat == 1)
	{
		this.response_heartbeat1 = true ;
	}
	else if (beat == 2)
	{
		this.response_heartbeat2 = true ;
//uhhhh.... why do this?
		// let things settle down for .05 seconds sand then complete the test
		window.setTimeout("complete_test()", 50) ;
	}
};




/*----------------------------------------------------------------------------\
|                      WEB SERVICE HANDLERS
\----------------------------------------------------------------------------*/

function handle_server_success()
{
	debug('handle_server_success') ;

	// we have access to "this" b/c of the assignment we made in the comm class; calling this function outside of the framework
	//	means "this" won't be recognized; display this in debug mode to make the success stand out a bit more
	if (debug_mode)
	{
		display('unbridled enthusiasm [comm_id=' + this.connection_id + '][server_url=' + this.server_url + ']') ;
	}
}


//web_service.prototype.handle_failure=function( query_param)
function handle_server_failure( error_msg)
{
	debug('handle_server_failure') ;

	if (this.display_processing_message)
	{
		this.display_processing_message('off');
	}
	display( '<b>Connection Error:</b> ' + error_msg ) ;

	// finish the test
	complete_test() ;
}


function display_server_processing_message( on_off)
{
	debug('display_server_processing_message') ;

	if (on_off == 'on')
	{
		display('ping...');
	}

	// hide/show the div containing the indicator as appropraite
	var style = (on_off == 'off') ? 'none' : '' ;
	document.getElementById("indicator").style.display = style ;
}




function complete_test()
{	
	debug('complete_test') ;

	// connect back with the server one last time to store results... doesn;t matter if we hear back from the server (we won't b/c server won't try)
	//   note the second param so that we don't trigger the display handler
	debug('sending final update uni-directional') ;


	ws_comm.send_immediate( { query_param: '?mode=test_complete&step3=' + ws_comm.response_ws_start + '&step5=' + ws_comm.response_heartbeat1 + '&step7=' + ws_comm.response_heartbeat2 + '&ua=' + ws_comm.user_agent, no_display: true, uni_directional: true });

	// disconnect but send toggle to process one more request
	display_server_processing_message('off') ;
	ws_comm.disconnect();
	display('disconnected');

	// feedback for the user
	complete_test_message() ;
}


function complete_test_message()
{
	var alert_msg = '' ;
	if ((ws_comm.response_heartbeat2) && (!ws_comm.known_ua))
	{
		alert_msg = alert_msg + "  Your browser is compatible and we didn't previously know that!  So thank you very much for testing.  We've updated our records." ;
	}
	else if (ws_comm.response_heartbeat2)
	{
		alert_msg = alert_msg + "  Your browser is compatible and we've updated our records." ;
	}
	else
	{
		alert_msg = alert_msg + "  Your browser is not compatible" ;
		alert_msg = alert_msg + ((!ws_comm.known_ua) ? " and we didn't previously know that!  So thank you very much for testing.  We've updated our records." : ".") ;
	}

	display('<b>Test complete.</b> ' + alert_msg + '  Thanks for testing!') ;
	alert('Test complete. ' + alert_msg + '  Thanks for testing!') ;
}




/*----------------------------------------------------------------------------\
|                      GENERAL FUNCTIONS
\----------------------------------------------------------------------------*/

// the onload event calls this
function check_compatibility()
{
	// we had hidden things by default for noscript folks, so now unhide it
	document.getElementById("main_content").style.visibility = '' ;
	
	// record the user agent string
	display('<b>checking compatibility for this user agent string:</b>')
	display('&nbsp;&nbsp;&nbsp;&nbsp;' + ws_comm.user_agent) ;
	// it's okay to access the object member since this is not an operation the class will normally be doing

	if (ws_comm.known_ua)
	{
		display('&nbsp;&nbsp;&nbsp;&nbsp;<b>already on compatiblity list</b>') ;
	}
	else
	{
		display("&nbsp;&nbsp;&nbsp;&nbsp;<b>adding new user agent to the list!</b>") ;
	}

	
	// for those we are certain don't make the cut, then don't waste people's time
	// it's okay to access the object member since this is not an operation the class will normally be doing
	if (ws_comm.known_exclude)
	{
		display('<br /><span style="color:#FF0000;font-weight:bold;">ATTN:</span> Your user agenet [' + ws_comm.user_agent + '] is known to be incompatiable and will not be processed.<br />So far this "honor" is limited exclusively to Opera which is overly strict on iframe security.  <h2>Thanks anyway!</h2>') ;
		return ;
	}

	
//	ws_comm.check_cows_ajax_aware('parent.run_test();', 'parent.not_available();') ;
	run_test() ;
}


function run_test()
{
//alert('foo') ;
//return ;
	// if we made it here, then we know what replace method workd for subsequent communications
//	var replace_method = is_cows_ajax_aware ;

	// create the comm iframe and make initial connection
	var unmatched_param = (ws_comm.mismatched_ua) ? '&unmatched_ua=' + ws_comm.js_user_agent : '' ;
	var query_param = '?mode=web_service_start' + '&ua=' + ws_comm.user_agent + unmatched_param ;
	// the "true" will force an alert notification if we can't connect... although we still gotta hang around for the timeout
//	ws_comm.connect( {query_param: query_param, replace_method: replace_method, failure_alert: true }) ;
	ws_comm.connect( {query_param: query_param }) ;

// why send the ua again? - we could send an id or something, but good enough for this little exercise
	ws_comm.send( {query_param: '?mode=heartbeat&beat=1&ua=' + ws_comm.user_agent}) ;
	ws_comm.send( {query_param: '?mode=heartbeat&beat=2&ua=' + ws_comm.user_agent}) ;
}


function not_available()
{
	complete_test() ;
}



// add a message to the debug window
function debug( message)
{
	if (debug_mode)
	{
		message = '<span style="color:red;">' + message + '</span>' ;
		var debug_div = document.getElementById('spellingcow_debug') ;
		debug_div.innerHTML = (debug_div.innerHTML == '') ? message : debug_div.innerHTML + '<br/>' + message ;
	}
}


// add a message to the debug window
function display( message)
{
	var debug_div = document.getElementById('spellingcow_debug') ;
	debug_div.innerHTML = (debug_div.innerHTML == '') ? message : debug_div.innerHTML + '<br/>' + message ;
}






// the following is hiding/showing all the FF and IE variants
hide_msie = true ;
hide_ff = true ;

function show_hide( id)
{
	var hide ;
	if (id == 'show_ie')
	{
		hide_msie = !hide_msie ;
		hide = hide_msie ;
	}
	else
	{
		hide_ff = !hide_ff ;
		hide = hide_ff ;
	}

	if (hide)
	{
		document.getElementById(id).style.display='none';
	}
	else
	{
		document.getElementById(id).style.display='';
		
	}
}
