//################################################################################################################################//	

//grabs the settings from the page, passes it to the PHP script to create & populate the table, and then writes the table to page
function ajax() {

	//grab the variables from the HTML page
	var rowcount = document.getElementById('rowcount').options[document.getElementById('rowcount').selectedIndex].text;
	var columncount = document.getElementById('columncount').options[document.getElementById('columncount').selectedIndex].text;	
	var imagecount = document.getElementById('imagecount').options[document.getElementById('imagecount').selectedIndex].text;	
	//var size = document.getElementById('imagesize').options[document.getElementById('imagesize').selectedIndex].text;
	var size = document.imgform.formatflag[getradioindex('formatflag')].value;		
	var tags = document.getElementById('tags').value;
	
	//set appropriate imagesize
	switch(size) {
		case "Square":
			var imagesize = "r";
			break;
		case "Landscape":
			var imagesize= "l";
			break;						
		default:
			var imagesize = "l";
	}//switch
	
	
	//create the URL and the variable needed for AJAX-y magickz :)
	if (tags) {
		var url = "pavi.php?rowcount="+rowcount+"&columncount="+columncount+"&imagecount="+imagecount+"&imagesize="+imagesize+"&tags="+tags;
	} else {
		var url = "pavi.php?rowcount="+rowcount+"&columncount="+columncount+"&imagecount="+imagecount+"&imagesize="+imagesize;
		}
	
	//DEBUG
	//alert("url: "+url);
	
	
	var ajaxRequest;	
	
	//Browser Support Code
	try {
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try {
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser doesn't support AJAX!");
				return false;
			}//catch
		}//catch
	}//catch
	
	
	// Receive data from the PHP script and write it out to the HTML page
	ajaxRequest.onreadystatechange = function() {
//		if (ajaxRequest.readyState == 1){
			//show loading indicator
			document.getElementById('loadingit').innerHTML = "<p class=\"loader\"><img src=\"images/loader.gif\" /></p>"; 
//		}
		if (ajaxRequest.readyState == 4){
			//php script has returned the grid, so write it to the HTML page
			document.getElementById('grid').innerHTML = ajaxRequest.responseText;
			document.getElementById('loadingit').innerHTML = " ";
			//initialize the grid
			initialize();	
			if (zoomflag!=0) showzoom();		
		}
		if (ajaxRequest.status == '404'){
			document.getElementById('grid').innerHTML = "Oh noez! Error 404 file - not found";
		}
	}
	
	//send the actual request to the server
	ajaxRequest.open("GET", url, true);
	ajaxRequest.send(null); 
}
