// holds an instance of XMLHttpRequest
var xmlHttp = createXmlHttpRequestObject();

// creates an XMLHttpRequest instance
function createXmlHttpRequestObject() 
{
  // will store the reference to the XMLHttpRequest object
var http = false; 
http = window.XMLHttpRequest ? new window.XMLHttpRequest : (window.ActiveXObject ? new ActiveXObject("MSXML2.XMLHTTP"): null);



    return http;
}

/*---------------------------------------------------------Build 'matrix' table-------------------------------------------*/

// called to read a file from the server
function GetMatrixTable(object)
{

var table = object.parentNode.parentNode.parentNode.parentNode;

for(var count=0, row = table.rows; count < row.length; count++) {

	row[count].className = '';

	
	
}

 if(object.checked)
    object.parentNode.parentNode.className = 'highlight';
   else
    object.parentNode.parentNode.className = ''; 
    
  // only continue if xmlHttp isn't void
  if (xmlHttp)
  {
    // try to connect to the server
    try
    {
		//build variables to pass to php file
		
			//loop through application elements 
			var user_input = "a";
			for (i=0;i<document.forms[0].TechniqueMatrix.length;i++) {
				if (document.forms[0].TechniqueMatrix[i].checked) {
				user_input = document.forms[0].TechniqueMatrix[i].value;
				}
			}		

			//build url
			
			var url= "";
			url = url + "/ajaxApplications.php?function=Matrix&selection="+user_input;
				
			
		
      // call the php file
      xmlHttp.open("GET", url, true);
      xmlHttp.onreadystatechange = GetMatrixTableHandleRequestStateChange;
      xmlHttp.send(null);
    }
	
    // display the error in case of failure
    catch (e)
    {
      
    }
  }
}


// function that handles the http response
function GetMatrixTableHandleRequestStateChange() 
{
  // obtain a reference to the <div> element on the page
  myDiv = document.getElementById('Matrix');
      myDiv2 = document.getElementById('AnalyteMatrix');
	   myDiv3 = document.getElementById('Results');
try {	   
	    if (xmlHttp.status == 200) 
	    {
	      try
	      {
		// read the message from the server
		response = xmlHttp.responseText;
		// display the message 
			myDiv.innerHTML = "";
			myDiv2.innerHTML = "";
			myDiv3.innerHTML = "";



	       // myDiv.innerHTML += "Request status: 4 (complete). Server said: <br/>";
		myDiv.innerHTML += response;


	      }
	      catch(e)
	      {
		
	      }
	    } 
	    else
	    {
	      // display status message
	    
	    }
    
    } catch(e) { }
}

/*-----------------------------------Build Analytical table----------------------------------------------------- */

function GetAnalyticalClassTable(object)
{

var table = object.parentNode.parentNode.parentNode.parentNode;

for(var count=0, row = table.rows; count < row.length; count++) {

	row[count].className = '';

	
	
}

 if(object.checked)
    object.parentNode.parentNode.className = 'highlight';
   else
    object.parentNode.parentNode.className = ''; 
    
  // only continue if xmlHttp isn't void
  if (xmlHttp)
  {
    // try to connect to the server
    try
    {
		//build variables to pass to php file
		
			//loop through application elements 
			var user_input = "";
			for (i=0;i<document.forms[0].Matrix.length;i++) {
				if (document.forms[0].Matrix[i].checked) {
				user_input = document.forms[0].Matrix[i].value;
				}
			}	
			
			var analytical = "a";
			for (i=0;i<document.forms[0].TechniqueMatrix.length;i++) {
				if (document.forms[0].TechniqueMatrix[i].checked) {
				analytical = document.forms[0].TechniqueMatrix[i].value;
				}
			}			
			
			if(user_input == ""){
			user_input = document.forms[0].Matrix.value;
			}	

			//build url
			
			var url= "";
			url = url + "/ajaxApplications.php?function=Analytics&selection="+user_input+"&analytical="+analytical;
     
		      xmlHttp.open("GET",url, true);
		      xmlHttp.onreadystatechange = GetAnalyticalClassTableHandleRequestStateChange;
		      xmlHttp.send(null);
    }
    // display the error in case of failure
    catch (e)
    {
      
    }
  }
}



// function that handles the http response
function GetAnalyticalClassTableHandleRequestStateChange() 
{
  // obtain a reference to the <div> element on the page
  myDiv = document.getElementById('AnalyteMatrix');
    myDiv2 = document.getElementById('Results');
  
  try{
	  if (xmlHttp.status == 200) 
	    {
	      try
	      {

		response = xmlHttp.responseText;
		myDiv.innerHTML = "";
		myDiv2.innerHTML = "";
		myDiv.innerHTML += response;


	      }
	      catch(e)
	      {
		
	      }
	    } 
	    else
	    {
	     
	    }
    }  catch(e)
	      {}
}


/*----------------------------------Get Results function ---------------------------------------------------------*/

function GetResults(object)


{

var table = object.parentNode.parentNode.parentNode.parentNode;

for(var count=0, row = table.rows; count < row.length; count++) {

	row[count].className = '';

	
	
}

 if(object.checked)
    object.parentNode.parentNode.className = 'highlight';
   else
    object.parentNode.parentNode.className = ''; 
    
  // only continue if xmlHttp isn't void
  if (xmlHttp)
  {
    // try to connect to the server
    try
    {
	//build variables to pass to php file
		
			//loop through application elements 
			var user_input = "";      
			var ii = 1; 
			
			for (i=0;i<document.forms[0].AnalyteMatrix.length;i++) {
				if (document.forms[0].AnalyteMatrix[i].checked) {
				user_input = document.forms[0].AnalyteMatrix[i].value;
				}
			}	
			
			var matrix = "a";
			for (i=0;i<document.forms[0].Matrix.length;i++) {
				if (document.forms[0].Matrix[i].checked) {
				matrix = document.forms[0].Matrix[i].value;
				}
			}	
			
			var analytical = "a";
			for (i=0;i<document.forms[0].TechniqueMatrix.length;i++) {
				if (document.forms[0].TechniqueMatrix[i].checked) {
				analytical = document.forms[0].TechniqueMatrix[i].value;
				}
			}	
			
			
			if(user_input == ""){
			user_input = document.forms[0].AnalyteMatrix.value;
			}	
			
			if(matrix == "a"){
			matrix = document.forms[0].Matrix.value;
			}	
			
			if(analytical == "a"){
			analytical = document.forms[0].TechniqueMatrix.value;
			}	
			
			

			//build url
			
			var url= "";
			url = url + "/ajaxApplications.php?function=Results&selection="+user_input+"&matrix="+matrix+"&analytical="+analytical;
      // initiate reading the async.txt file from the server
      xmlHttp.open("GET",url, true);
      xmlHttp.onreadystatechange = GetResultsTableHandleRequestStateChange;
      xmlHttp.send(null);
    }
    // display the error in case of failure
    catch (e)
    {
     
    }
  }
}



// function that handles the http response
function GetResultsTableHandleRequestStateChange() 
{
  // obtain a reference to the <div> element on the page
  myDiv = document.getElementById('Results');
try {
	    if (xmlHttp.status == 200) 
	    {
	      try
	      {
		// read the message from the server
		response = xmlHttp.responseText;
		// delete the contentes of the div 
			myDiv.innerHTML = "";
	       // myDiv.innerHTML += "Request status: 4 (complete). Server said: <br/>";
		myDiv.innerHTML += response;
		window.scroll(0,450);

	      }
	      catch(e)
	      {
		// display error messages and stop updating the window
		
	      }
	    } 
	    else
	    {
	      // display status message
	     
	    }
    }  catch(e)
	      {}
}



/*-----------------------------------Reset functions --------------------------------------------------------------*/
function Reset()
{
	  if (xmlHttp)
  {
    // try to connect to the server
    try
    {
		
		
      // initiate reading the async.txt file from the server
      xmlHttp.open("GET", "/ajaxApplications.php", true);
      xmlHttp.onreadystatechange = handleRequestStateChange2;
      xmlHttp.send(null); 
	
    }
    // display the error in case of failure
    catch (e)
    {
     
    }
  }
}



function handleRequestStateChange2() 
{

	  // obtain a reference to the <div> element on the page
  myDiv = document.getElementById('Results');
  atrix = document.getElementById('Matrix');
  analyteMatrixDiv = document.getElementById('AnalyteMatrix');
  
    if (xmlHttp.status == 200) 
    {
      try
      {
		  
        // read the message from the server
        response = xmlHttp.responseText;
        
		
	
		// display the message 
		myDiv.innerHTML = "";
    	atrix.innerHTML = "";
	 	analyteMatrixDiv.innerHTML = "";
		document.forms[0].reset()

		
      }
      catch(e)
      {
        // display error messages and stop updating the window
       
      }
    } 
    else
    {
     
    }
}

