/*Create MOSTool instance*/
var mos = new MOSTool(TestState_Callback, TestProgress_Callback, TestResult_Callback, TestInfo_Callback);

/*
 * TestState_Callback ()
 *  This function will be called by MOS_Tool javascript API, as soon as test state changes
 *	The test States are : idle, running, noload, load, error
 *	If user does not want this callback, then pass "null" for TestState_Callback while creating MOSTool instance.
 */
function TestState_Callback(test_state)
{
	document.getElementById("test_status").innerHTML = test_state;
	if(test_state == "idle" || test_state == "error") {
		document.getElementById("start_button").value = "Start OPScore Test";
		document.getElementById("start_button").onclick = startMOSTest;
		// ShowGraph();
	    document.getElementById('loader').style.display = 'none';
		}
		else {
		  document.getElementById('loader').style.display = '';
		}
}

/*
 * TestProgress_Callback ()
 *  This function will be called by MOS_Tool javascript API, after every 5% of test progress
 *	If user does not want this callback, then pass "null" for TestProgress_Callback while creating instance
 */
function TestProgress_Callback(test_progress)
{
	var progress = test_progress / 10;
	var block = document.getElementById("block" + progress);

	block.innerHTML = "&nbsp;&nbsp;&nbsp;";
	block.style.backgroundColor = "forestgreen";
}

/*
 * TestResult_Callback ()
 *	This function will be called by MOS_Tool javascript API, as soon as test is finish and results are ready
 *	If user does not want this callback, then pass "null" for TestResult_Callback while creating instance
 *
 *	test_type -	gives the test mode :  	0 for noload 
 *		            				1 for load
 *	param -	    gives the parameter name 
 *			    The parameter names are - mos, owd, meanjitter, peakjitter, packetloss and packetlate ect
 *	value -	    gives the actual result of the parameter
 */	
function TestResult_Callback(test_type, param, value)
{
	test = (test_type == load) ? "load" : "noload";
	var param_id = test + "_" + param;

	document.getElementById(param_id).innerHTML = value;
}

/*
 * TestInfo_Callback ()
 *  This function will be called by MOS_Tool javascript API and will give the current action done by MOS test
 *	If user does not want this callback, then pass "null" for TestInfo_Callback while creating instance
 */
function TestInfo_Callback(test_info)
{
	if(document.getElementById("test_info"))
		document.getElementById("test_info").value += test_info + "\n";
}
	
/*
 * StartMOSTest()
 *  Start MOS test by calling startMOSTest from MOS Tool class
 */
function startMOSTest()
{
	document.getElementById("test_info").value = "";

	for(i=1; i <= 10; i++)
	{
		document.getElementById("block" + i).innerHTML = "";
		document.getElementById("block" + i).style.background = "white";
	}
	if(mos != null) {		
		mos.StartMOSTest();
		document.getElementById("start_button").value = "Stop OPScore Test";
		document.getElementById("start_button").onclick = stopMOSTest;
		return;
	} else {
		alert("Can't create MOS_Tool instance successfully");
	}
		
}

/*
 * StopMOSTest()
 *  Stop MOS test by calling stopMOSTest from MOS Tool class
 */
function stopMOSTest()
{
	if(mos != null) {
		mos.StopMOSTest();
		document.getElementById("start_button").value = "Start OPScore Test";
		document.getElementById("start_button").onclick = startMOSTest;
		return;
	}
}
	
/*
 * ShowGraph ()
 *	Show the graph after test is finish. It the quality of VoIP with respect to MOS
 */
function ShowGraph()
 {
	noload_mos = mos.GetMOSTest_Result(noload, "MOS");
	load_mos = mos.GetMOSTest_Result(load, "MOS");
	var graph = new Graph("MOS_Graph",noload_mos,load_mos);
		
	graph.draw_graph();
  }

function page_load()
  {
	document.getElementById("test_status").innerHTML = "Loading...";
	document.getElementById('loader').style.display = '';
	return;
  }

