var xmlHttp;

function showResult(str, type, container) {
	if(str.length==0) { 
		document.getElementById(container).innerHTML = "";
		document.getElementById(container).style.border = "0px";
		return;
	}

	xmlHttp = GetXmlHttpObject();
	
	if(xmlHttp==null) {
		alert("Browser does not support HTTP Request");
		return;
	}
	
	var url = "http://www.communityphonebook.com/ajaxsearch.php";
	url = url + "?search=" + str;
	url = url + "&type=" + type;
	url = url + "&sid=" + Math.random();
	xmlHttp.onreadystatechange = stateChanged;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
	xmlHttp.container = container;
}

function hideDiv(id) {
	document.getElementById(id).style.display = 'none';
}

function hideCityResults() {
	hideDiv("city_results");
}

function hideCategoryResults() {
	hideDiv("category_results");
}

function hideZipResults() {
	hideDiv("zip_results");
}

function setCategoryFields(id, title) {
	document.getElementById('category').value = id;
	document.getElementById('category_input').value = title;
}

function setField(field, value) {
	document.getElementById(field).value = value;
	hideDiv(xmlHttp.container);
}

function stateChanged() {
	if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
		if(xmlHttp.responseText.length > 3) {
			document.getElementById(xmlHttp.container).innerHTML = xmlHttp.responseText;
			document.getElementById(xmlHttp.container).style.border = "1px solid #A5ACB2";
			document.getElementById(xmlHttp.container).style.display = 'block';
		}
		else {
			hideDiv(xmlHttp.container);
		}
	}
}

function GetXmlHttpObject() {
	var xmlHttp = null;

	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	}
	catch(e) {
		// Internet Explorer
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e) {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}

	return xmlHttp;
}
