// JavaScript Document
/*
This is a dynamic ajax query script. It uses the variables, enabling it to work with any page without having to recreate a new query script. Currently this is only for querying data. 
I am working on one for inserting and updating data also.

whichElement and whichLink are defined in the page calling the function.

I.E.

whichLink = "nameQuery.asp" this is the page making the query to the database.
whichElement = "nameList" this is the element where the data will be returned on the original page.

this function uses both the "name" attribute and the "id" attribute but could just as easily only use one or the other.

-created by krstofer@teamshibby.com. Based off of script found @ w3schools.com
*/
var xmlHttp1

function showData1()
{ 
	//this shows the "working" graphic when the query page is retrieving data

	
	//document.getElementById('insert_response').style.display='block'
	//document.getElementById(whichElement).innerHTML = "<div align=center><b>Caricamento in corso....</b><br><br><img src='workingBar.gif'></div>"

	//by setting these values to "" it prevents an undefined error. Also defining these outside of the function will add existing data to the variable which will cause an error.
	var theForm = ""
	var howManyElement = ""
	var daString = ""
	var daString0 = ""
	var daString1 = ""
	var daString2 = ""

	//alert(document.getElementById('area').options[document.getElementById('area').selectedIndex].value)

	//theForm = document.form1
	//this finds the number of elements on the form and assigns the number to the howManyElement variable.
	//howManyElement = theForm.elements.length;
	
	
	xmlHttp1=GetXmlHttpObject();
	
	//this for block creates the string with all the data in the url. The loop goes through each element and assigns the form name and the value to the string creating a "first=brian&last=collier' string if the form contains a 'first' field and a 'last' field.

	//for (i=0; i<howManyElement; i++){
		
	//	 daString = daString + theForm.elements[i].name+ "="+theForm.elements[i].value+"&";
	
	//}
	
	daString0 = "categoria=" + document.cerca.categoria.value;
	daString1 = "area="+document.getElementById('area').options[document.getElementById('area').selectedIndex].value;
	daString2 = "marchi=" + document.getElementById('marchi').options[document.getElementById('marchi').selectedIndex].value;
	
	daString=daString0+"&"+daString1+"&"+daString2
	
	if (xmlHttp1==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  } 
 
	//the 'whichLink' variable is assigned on the page making the request. 
	var url1=whichLink1;
	url1=url1+"?"+daString;
	url1=url1+"&sid="+Math.random();
	//alert('url1= '+url1)
	xmlHttp1.onreadystatechange=stateChanged1;
	xmlHttp1.open("GET",url1,true);
	xmlHttp1.send(null);
	
	//alert(url)//this is used to view the URL being sent to the query page.
}

function stateChanged1() 
{ 
//alert(xmlHttp1.readyState)
	if (xmlHttp1.readyState==4)
	{ 
		//alert(xmlHttp1.responseText)
		document.getElementById(whichElement1).innerHTML=xmlHttp1.responseText;
	}
}

function GetXmHttpObject()
{
	var xmlHttp1=null;
	try
	  {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp1=new XMLHttpRequest();
	  }
	catch (e)
	  {
	  // Internet Explorer
	  try
	    {
	    xmlHttp1=new ActiveXObject("Msxml2.XMLHTTP");
	    }
	  catch (e)
	    {
	    xmlHttp1=new ActiveXObject("Microsoft.XMLHTTP");
	    }
	  }
	return xmlHttp1;
}
