

function formChange(form) {
	var images = document.getElementById("images");
	var image = document.getElementById("graphImage");
	//image.src = "loading.jpg";
	var loadingIndicator = document.getElementById("loadingIndicator");
	loadingIndicator.innerHTML = "LOADING ... ";
	path = window.location.pathname.replace(/(.*)\/(.+\.php)/, "$1");
	//alert(path);
	image.src = "http://"+ window.location.hostname+path+"/image_graph_generated1.php?" + getQueryString(form);

	/*
	location=" + form.location.value
					+ "&lines="+form.lines.checked
					+ "&dots="+form.dots.checked
					+ "&endDate="+form.endDate.value;
	*/
	updateSummaryTable(form);
}
function getQueryString(form) {
	var qs =  "location=" + form.location.value
			+ "&span="+ form.span.options[form.span.selectedIndex].value
			+ "&marker=" + form.markerSelect.selectedIndex
			//+ "&lines="+form.lines.checked
			//+ "&dots="+form.dots.checked
			+ "&endDate="+form.endDate.value;
	if(form.background) {
		qs = qs + "&background=" + form.background.value;
	}
	return qs;
}
function dateChanged(calendar) {
    // Beware that this function is called even if the end-user only
    // changed the month/year.  In order to determine if a date was
    // clicked you can use the dateClicked property of the calendar:
    //if (calendar.dateClicked) {
      // OK, a date was clicked, redirect to /yyyy/mm/dd/index.php
      var y = calendar.date.getFullYear();
      var m = calendar.date.getMonth()+1;     // integer, 0..11
      var d = calendar.date.getDate();      // integer, 1..31
	  var endDate = document.getElementById("endDate");
	  endDate.value = y+"-"+m+"-"+d+" 00:15:00";
      formChange(document.getElementById('form1'));
	  // redirect...
      //window.location = "/" + y + "/" + m + "/" + d + "/index.php";
    //}
}

function onLoad() {
	document.calendar = Calendar.setup({flat: "calendar-container",flatCallback : dateChanged, date: new Date()});
	//document.calendar.setDate(new Date());
	formChange(document.getElementById('form1')); 
}

function loadingIndicatorOff() {
	var loadingIndicator = document.getElementById("loadingIndicator");
	loadingIndicator.innerHTML = "";
	
}

function getXMLHTTPRequest() {
	req = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
	return req;
}
var req;
function updateSummaryTable(form) {
	req = getXMLHTTPRequest();
	if(req) {
		req.onreadystatechange = processNewSummaryTable;
		path = window.location.pathname.replace(/(.*)\/(.+\.php)/, "$1");
		//alert("path: " + path);
		req.open("POST", "http://"+window.location.hostname+path+"/" + form.tableFileName.value, true);
		//req.open('POST',"summaryTable.php",false);
		req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		req.send(getQueryString(form));
		//req.send(null);
	}
	//summaryTableDiv.innerHTML = image.src;
	
}
function processNewSummaryTable() {
	// only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            document.getElementById("summaryTables").innerHTML = req.responseText;
			
        } else {
            alert("There was a problem retrieving the XML data:\n" +
                req.statusText);
        }
    }
	else {
		//alert(req.readyState);
	}
}

