function update_div(div_name, app_path, query_string) {
		
	var xmlHttpReq = false;	// Default to false just incase we can't create a req object

	if (window.XMLHttpRequest) {					// Non-IE browsers can do XMLHttpRequest
		xmlHttpReq = new XMLHttpRequest();
	} else if (window.ActiveXObject) {				// IE handles an ActiveXObject
		xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else{
		xmlHttpReq = new XMLHttpRequest();
	}
	
	xmlHttpReq.open('POST', app_path, true);
	xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	
	xmlHttpReq.onreadystatechange = function() {
		if (xmlHttpReq.readyState == 4) {
			updatepage(xmlHttpReq.responseText);
		}
	}
			
	xmlHttpReq.send(query_string);
}


function updatepage(html_output){
	alert(html_output);
	//document.getElementById(div_name).innerHTML = html_output;
}

function send_newsletter_email(the_form) {
	var email_address = the_form.email_address_newsletter.value;
	update_div('newsletterbox','elements/php/apps/send_newsletter_email.php','email_address='+email_address);
}
function send_report_email(the_form) {
	var email_address = the_form.email_address_report.value;
	update_div('newsletterbox','elements/php/apps/send_report_email.php','email_address='+email_address);
}

function send_form(div_name, app_path,the_form) {
	//show_screen();
	var full_app_path = app_path;
	var query_string = "";
	
	for(i=0; i<the_form.elements.length; i++) {
		//if(the_form.elements[i].type == "text" || the_form.elements[i].type == "textarea" || the_form.elements[i].type == "hidden" || the_form.elements[i].type == "radio") {
				//alert(the_form.elements[i].value);
			if(query_string != "") {
				query_string = query_string + "&";
			}
			
			if((the_form.elements[i].type == "checkbox") && (the_form.elements[i].checked == 0)) {
				continue;
			}
			
			query_string = query_string + escape(the_form.elements[i].name) + '=' + escape(the_form.elements[i].value);
		//}
	}
		
	update_div(div_name,full_app_path,query_string);
}

function load_businesses(sort_category) {
	update_div('business_list','elements/php/apps/load_businesses.php','sort=' + sort_category);
}

function search_businesslist(sort_category) {
	var search_str = document.getElementById('search_str').value;
	setTimeout("run_search('"+sort_category+"','"+search_str+"')",2000);
}

function run_search(sort_category,search_string) {
	var comp_search_str = document.getElementById('search_str').value;
	if(comp_search_str == search_string) {
		update_div('business_list','elements/php/apps/load_businesses.php','sort=' + sort_category + '&search=' + search_string + '');	
	}
}
