/*
	quadGraphCheck(elem, swfObject, url);
	ajax_change_form(elem, form);
	ajax_get_counties(state, elem);
	ajax_get_crops(state, county, defaultCrop, elem);
	ajax_get_pests(crop, elem, defaultValue);
	ajax_get_anti_pests(crop, elem);
	ajax_get_sites(elem);
	ajax_get_user_level(username, elem);
*/

function quadGraphCheck(elem, swfObject, url){
	var xmlHttp = getXMLHttp();
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState == 4){
//this is the output (after call below)
			if(xmlHttp.responseText == 0){
				elem.innerHTML += '<br /><br /><br /><div style="text-align:center; margin-left:auto; margin-right:auto;">(No Data Available)</div>';
			}
			else{
				swfObject.addVariable("data", url);
				swfObject.addParam("allowScriptAccess", "always");
				swfObject.write(elem.id);
			}
		}
	}
//this is the input
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function ajax_change_form(elem, form){
	hide_ajax_messages();
	ajax_loading(getElement('selectFormChoice'));
//window.alert(getStyle(getElement('selectFormChoice'), 'width'));
	var xmlHttp = getXMLHttp();
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState == 4){
//this is the output (after call below)
			ajax_change_form_update(xmlHttp.responseText, elem);
			ajax_loading_done();
		}
	}
//this is the input
	xmlHttp.open("GET", "/php/pest_graph_forms/ajax_change_form.php?form="+form, true);
	xmlHttp.send(null);
}
function ajax_change_form_update(response, elem){
	var response = response.split("^^");
	elem.innerHTML = response[0];
	//firstFieldFocus(getElement(response[1]));
	//removed, because selecting a new form could create an ajax error - not a problem per se, but distracting...
}

function ajax_get_counties(state, elem){
	ajax_loading(elem);
	var xmlHttp = getXMLHttp();
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState == 4){
//this is the output (after call below)
			ajax_get_counties_update(xmlHttp.responseText, elem);
			ajax_loading_done();
		}
	}
//this is the input
	xmlHttp.open("GET", "/php/pest_graph_forms/ajax_get_counties.php?state="+state, true);
	xmlHttp.send(null);
}
function ajax_get_counties_update(response, elem){
	// clear the existing contents
	elem.innerHTML = '';
	// elem is a SELECT
	// response is a ^ separated array of concern values
	response = response.split("^");
	// response is now response[]
	var newElem;
	for(i = 0; i < (response.length-1); i++){
		newElem = document.createElement("OPTION");
		newElem.value = response[i];
		newElem.innerHTML = response[i];
		elem.appendChild(newElem);
	}
	if(response == ""){
		//alert that there are no pests associated with the crop
	}
}

function ajax_get_fixed_site_crops(state, county, area, date, span, defaultValue, elem){
	ajax_loading(elem);
	var xmlHttp = getXMLHttp();
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState == 4){
//this is the output (after call below)
			ajax_get_fixed_site_crops_update(xmlHttp.responseText, defaultValue, elem);
			ajax_loading_done();
		}
	}
//this is the input
	xmlHttp.open("GET", "/php/pest_graph_forms/ajax_get_fixed_site_crops.php?state="+state+"&county="+county+"&area="+area+"&date="+date+"&span="+span, true);
	xmlHttp.send(null);
}
function ajax_get_fixed_site_crops_update(response, defaultValue, elem){
	// clear the existing contents
	elem.innerHTML = '';
	// elem is a SELECT
	// response is a ^ separated array of concern values
	response = response.split("^");
	// response is now response[]
	var newElem;
	if(defaultValue == ""){
		newElem = document.createElement("OPTION");
		newElem.id = elem.id+'_blank';
		newElem.selected = true;
		newElem.value = ''
		newElem.innerHTML = 'Please select a crop...'
		elem.appendChild(newElem);
	}
	for(i = 0; i < (response.length-1); i++){
		newElem = document.createElement("OPTION");
		if(response[i] == defaultValue){
			newElem.selected = true;
		}
		newElem.value = response[i];
		newElem.innerHTML = response[i];
		elem.appendChild(newElem);
	}
	if(response == ""){
		//alert that there are no pests associated with the crop
	}
}

function ajax_get_fixed_site_areas(state, county, date, span, defaultValue, elem){
	ajax_loading(elem);
	var xmlHttp = getXMLHttp();
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState == 4){
//this is the output (after call below)
			ajax_get_fixed_site_areas_update(xmlHttp.responseText, defaultValue, elem);
			ajax_loading_done();
		}
	}
//this is the input
	xmlHttp.open("GET", "/php/pest_graph_forms/ajax_get_fixed_site_areas.php?state="+state+"&county="+county+"&date="+date+"&span="+span, true);
	xmlHttp.send(null);
}
function ajax_get_fixed_site_areas_update(response, defaultValue, elem){
	// clear the existing contents
	elem.innerHTML = '';
	// elem is a SELECT
	// response is a ^ separated array of concern values
	response = response.split("^");
	// response is now response[]
	var newElem;
	if(defaultValue == ""){
		newElem = document.createElement("OPTION");
		newElem.id = elem.id+'_blank';
		newElem.selected = true;
		newElem.value = ''
		newElem.innerHTML = 'Please select a county area...'
		elem.appendChild(newElem);
	}
	for(i = 0; i < (response.length-1); i++){
		newElem = document.createElement("OPTION");
		if(response[i] == defaultValue){
			newElem.selected = true;
		}
		newElem.value = response[i];
		newElem.innerHTML = response[i];
		elem.appendChild(newElem);
	}
	if(response == ""){
		//alert that there are no pests associated with the crop
	}
}

function ajax_get_pests(crop, elem, defaultValue){
	ajax_loading(elem);
	var xmlHttp = getXMLHttp();
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState == 4){
//this is the output (after call below)
			ajax_get_pests_update(xmlHttp.responseText, elem, defaultValue);
			ajax_loading_done();
		}
	}
//this is the input
	xmlHttp.open("GET", "/php/pest_graph_forms/ajax_get_pests.php?crop="+crop, true);
	xmlHttp.send(null);
}
function ajax_get_pests_update(response, elem, defaultValue){
	// clear the existing contents
	elem.innerHTML = '';
	// elem is a SELECT
	// response is a ^ separated array of concern values
	response = response.split("^");
	// response is now response[]
	var newElem;
	for(i = 0; i < (response.length-1); i++){
		newElem = document.createElement("OPTION");
		if(response[i] == defaultValue){
			newElem.selected = true;	
		}
		newElem.value = response[i];
		newElem.innerHTML = response[i];
		elem.appendChild(newElem);
	}
	if(response == ""){
		//alert that there are no pests associated with the crop
	}
}

function ajax_get_anti_pests(crop, elem){
	ajax_loading(elem);
	var xmlHttp = getXMLHttp();
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState == 4){
//this is the output (after call below)
			ajax_get_anti_pests_update(xmlHttp.responseText, elem);
			ajax_loading_done();
		}
	}
//this is the input
	xmlHttp.open("GET", "/php/pest_graph_forms/ajax_get_anti_pests.php?crop="+crop, true);
	xmlHttp.send(null);
}
function ajax_get_anti_pests_update(response, elem){
	// clear the existing contents
	elem.innerHTML = '';
	// elem is a SELECT
	// response is a ^ separated array of concern values
	response = response.split("^");
	// response is now response[]
	var newElem;
	for(i = 0; i < (response.length-1); i++){
		newElem = document.createElement("OPTION");
		newElem.value = response[i];
		newElem.innerHTML = response[i];
		elem.appendChild(newElem);
	}
	if(response == ""){
		//alert that there are no pests associated with the crop
	}
}

function ajax_get_sites(elem){
	ajax_loading(elem);
	var xmlHttp = getXMLHttp();
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState == 4){
//this is the output (after call below)
			ajax_get_sites_update(xmlHttp.responseText, elem);
			ajax_loading_done();
		}
	}
//this is the input
	xmlHttp.open("GET", "/php/pest_graph_forms/ajax_get_sites.php", true);
	xmlHttp.send(null);
}
function ajax_get_sites_update(response, elem){ // Response alternates between ID and Name...
	// clear the existing contents
	elem.innerHTML = '';
	// elem is a SELECT
	// response is a ^ separated array of concern values
	response = response.split("^");
	// response is now response[]
	var newElem;
	for(i = 0; i < (response.length-1); i+=2){
		newElem = document.createElement("OPTION");
		//if(response[i] == defaultValue){
		//	newElem.selected = true;	
		//}
		newElem.value = response[i];
		newElem.innerHTML = response[i+1];
		elem.appendChild(newElem);
	}
	if(response == ""){
		//alert that there are no pests associated with the crop
	}
}

function ajax_get_user_level(username, elem){
	ajax_loading(elem);
	var xmlHttp = getXMLHttp();
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState == 4){
//this is the output (after call below)
			ajax_get_user_level_update(xmlHttp.responseText, elem);
			ajax_loading_done();
		}
	}
//this is the input
	xmlHttp.open("GET", "/php/pest_graph_forms/ajax_get_user_level.php?user="+username, true);
	xmlHttp.send(null);
}
function ajax_get_user_level_update(response, elem){
	if(response == 2){ // status=2 means user submits fixed site data
		hide('antiSiteDiv');
		show('siteDiv');
		ajax_get_sites(getElement('site'));
	}
	else{
		var sd = getElement('site');
		sd.innerHTML = '';
		var opt = document.createElement("OPTION");
		opt.value = -1;
		opt.innerHTML = '-1';
		opt.selected = 'selected';
		sd.appendChild(opt);
		hide('siteDiv');
		show('antiSiteDiv');
	}
}

function ajax_history_crop(crop, elem, graph, date){
	ajax_loading(elem);
	graph.innerHTML = '<div id="changingGraphx"></div>';
	var so = new SWFObject("http://berrygrape.org/OFC/open-flash-chart.swf", "chart", "95%", "300", "9", "#FFFFFF");
	so.addVariable("data", "http://berrygrape.org/pest-graphs/or/swd/"+crop.toLowerCase()+".php?date="+date);
	so.addParam("allowScriptAccess", "always");
	so.write("changingGraphx");
	setTimeout("ajax_loading_done();", 350);
}