var initialload = true; // flag to run process on first load

var resultsTable; // table containing search and saved results
var searchTBODY // tbody for search results
var savedTBODY; // tbody for saved items
var resultsPreview; // div to preview # of results
var submitsearch; // submit button

var row_physicianname; // table row for physisian name
var row_physiciannew; // table row for new physicians
var row_facilitytype;
var row_physicianlanguage;
var row_physiciangender;


var psb; // physician search button
var fsb; // facility search button
var tabsearch; // search results tab
var tabsaved; // saved results tab
var expand_toggle; // toggle button for result list
var prpdf; // print results as pdf button

var ajaxobj = new AjaxObject("get","/ClassLibrary/com/locator/ajax_switch.cfml",null,null);
var ajaxobj2 = new AjaxObject("get","/ClassLibrary/com/locator/ajax_switch.cfml",null,null);

var SEARCHTYPE = "p"; // search type (physician or facility)
var PHYSICIAN_DATA = null;
var FACILITY_DATA = null;
var P_EXPANDED = 1;
var ALL_RECORDS = false;

var SAVED_DATA = {"p":"", "f":""}; // string with saved search data ids
var SAVED_DIFF = 1;
var SAVED_VISIBLE = 0;

addLoadEvent(function() {
		resultsTable = document.getElementById('locatorresults_table');
		searchTBODY = document.getElementById('locatorresults');
		savedTBODY = document.getElementById('savedresults');
		resultsPreview = document.getElementById('resultspreview');
		if (resultsPreview) {
			resultsPreview.onclick = function() {getResults('results');};
			resultsPreview.style.cursor = "pointer";
		}
		submitsearch = document.getElementById("submitsearch");
		
		row_physicianname = document.getElementById("row_physicianname");
		row_physiciannew = document.getElementById("row_physiciannew");
		row_facilitytype = document.getElementById("row_facilitytype");
		row_physicianlanguage = document.getElementById("row_physicianlanguage");
		row_physiciangender = document.getElementById("row_physiciangender");
		
		psb = document.getElementById('psearchbutton');
		if (psb) {
			psb.href = "#psearchtop";
			psb.onclick = function() { updateSearchForm('Physician',true); };
		}
		fsb = document.getElementById('fsearchbutton');
		if (fsb) {
			fsb.href = "#fsearchtop";
			fsb.onclick = function() { updateSearchForm('Facility',true); };
		}
		tabsearch = document.getElementById('tab_searchresults');
		tabsaved = document.getElementById('tab_savedresults');
		expand_toggle = document.getElementById("expand_toggle");
		prpdf = document.getElementById("printresultpdf");
		
		var frs = getElementsByClass("fr",null,"IMG");
			for(var i=0; i<frs.length; i++) { toggleDisplay(frs[i],true); }
			
		if (savedTBODY) {
			toggleDisplay(savedTBODY,false);
		}
			
		updateSearchForm("",false);
		loadSavedResults();
		//alert(getQueryString());
	});

function getQueryString() { // builds querystring from form fields
	var qs = "";
	if(SEARCHTYPE == "p") {
		var nv = "";
		if (document.getElementById('txt_name')) {
			nv = document.getElementById('txt_name').value;
		}
			qs += ((nv.length > 0) ? "&name="+escape(nv) : "" );
		if(document.getElementById('cb_new') && document.getElementById('cb_new').checked) {
			qs += "&new=1";
		}
		var lsv = ""
		if (document.getElementById('sel_lang')) {
			lsv = document.getElementById('sel_lang').options[document.getElementById('sel_lang').selectedIndex].value;
		}
			qs += ((lsv.length > 0) ? "&lang="+escape(lsv) : "" );
		if(document.getElementById('rad_gender_m') && document.getElementById('rad_gender_m').checked) { qs += "&gender=m"; }
		else if(document.getElementById('rad_gender_f') && document.getElementById('rad_gender_f').checked) { qs += "&gender=f"; }
	} else {
		var stv = "";
		if (document.getElementById('sel_type')) {
			stv = document.getElementById('sel_type').options[document.getElementById('sel_type').selectedIndex].value;
		}
			qs += ((stv.length > 0) ? "&type="+escape(stv) : "" );
	}
	var ssv = "";
	if (document.getElementById('sel_specialty')) {
		ssv = document.getElementById('sel_specialty').options[document.getElementById('sel_specialty').selectedIndex].value;
	}
		qs += ((ssv.length > 0) ? "&specialty="+escape(ssv) : "" );
	if(document.getElementById('sel_location') && document.getElementById('sel_location').options[document.getElementById('sel_location').selectedIndex].value.indexOf(",") > -1) {
		qs += "&city=" + escape(document.getElementById('sel_location').options[document.getElementById('sel_location').selectedIndex].value.split(",")[0]);
		qs += "&state=" + escape(document.getElementById('sel_location').options[document.getElementById('sel_location').selectedIndex].value.split(",")[1]);
	}
	var zv = "";
	if (document.getElementById('txt_zip')) {
		zv = document.getElementById('txt_zip').value;
	}
		qs += ((zv.length > 0) ? "&zipcode="+escape(zv) : "" );
	var rv = "";
	if (document.getElementById('txt_radius')) {
		rv = document.getElementById('txt_radius').value;
	}
		qs += ((rv.length > 0) ? "&radius="+escape(rv) : "" );
	if(document.getElementById("qreset") && document.getElementById("qreset").value == "1") { qs += "&reset=1"; }
	if(qs.length > 0) { ALL_RECORDS = false;} else { ALL_RECORDS = true; }
	//alert("ALL: "+ALL_RECORDS+"\n"+qs);
	return qs;
}

function formIsFilled() {
	if(document.getElementById('txt_zip') && document.getElementById('txt_zip').value !== "") {return true;}
	if(document.getElementById('sel_location') && document.getElementById('sel_location').selectedIndex > 1) {return true;}
	if(document.getElementById('txt_name') && document.getElementById('txt_name').value !== "") {return true;}
	if(document.getElementById('cb_new') && document.getElementById('cb_new').checked) {return true;}
	if(document.getElementById('sel_specialty') && document.getElementById('sel_specialty').selectedIndex > 1) {return true;}
	if(document.getElementById('sel_type') && document.getElementById('sel_type').selectedIndex > 1) {return true;}
	if(document.getElementById('sel_lang') && document.getElementById('sel_lang').selectedIndex > 1) {return true;}
	if((document.getElementById('rad_gender_m') && document.getElementById('rad_gender_m').checked) || (document.getElementById('rad_gender_f') && document.getElementById('rad_gender_f').checked)) {return true;}
	return false;
}

function checkLocation(id) {
	if(id == "txt_zip") { document.getElementById('sel_location').selectedIndex = 0; }
	else if(id == "sel_location") {
		document.getElementById('txt_zip').value = "";
		document.getElementById('txt_radius').value = "";
	}
	else if(id == "txt_radius") {
		var rad = document.getElementById("txt_radius");
		var zip = document.getElementById("txt_zip");
		if((!isNaN(rad.value)) && (rad.value !== "")) {
			if((isNaN(zip.value)) || (zip.value === "")) {
				alert("Please enter a Zip Code if you wish to use the radius search option.");
				zip.focus();
			}
		}
	}
	getResults('preview');
}

function getResults(type) {
	//alert("getResults()");
	var url = "";
	var querystring = getQueryString();
	var action = SEARCHTYPE + "_" + type;
			
	if(action == "p_preview") {
		ajaxobj.setQueryString("?action=p_match"+querystring);
		ajaxobj.setHandler(displayPhysicianResultsCount);
		ajaxobj.execute();
	} else if(action == "p_results") {
		showSearchResults();
		clearData(searchTBODY);
		document.location.hash = "psearchtop";
		displayDescription("Processing Physician Search Results ...");
		ajaxobj2.setQueryString("?action=p_results"+querystring);
		ajaxobj2.setHandler(displayPhysicianResults_expanded);
		ajaxobj2.execute();
		P_EXPANDED = 1;
		updateExpandToggle();
	} else if(action == "f_preview") {
		ajaxobj.setQueryString("?action=f_match"+querystring);
		ajaxobj.setHandler(displayFacilityResultsCount);
		ajaxobj.execute();
	} else if(action == "f_results") {
		showSearchResults();
		clearData(searchTBODY);
		document.location.hash = "fsearchtop";
		displayDescription("Processing Facility Search Results ...");
		ajaxobj2.setQueryString("?action=f_results"+querystring);
		ajaxobj2.setHandler(displayFacilityResults);
		ajaxobj2.execute();
	}
	return false;
}

function displayPhysicianResultsCount(xmlHttp) {
	var tmp = parseInt(xmlHttp.responseText,10);
	if(!isNaN(tmp)) {
		showResultsPreview(tmp);
	} else {
		showResultsPreview("");
	}
}

function displayFacilityResultsCount(xmlHttp) {
	var tmp = parseInt(xmlHttp.responseText,10);
	if(!isNaN(tmp)) {
		showResultsPreview(tmp);
	} else {
		showResultsPreview("");
	}
}

function displayPhysicianResults(xmlHttp) {
	if(typeof xmlHttp != "undefined") { PHYSICIAN_DATA = xmlHttp.responseXML.getElementsByTagName("p"); }
	else if(PHYSICIAN_DATA === null) { return false; }
	clearData(searchTBODY);
	if(PHYSICIAN_DATA.length > 0) {			
		var header_col1 = document.createElement("th");
			header_col1.appendChild(document.createTextNode("Physician Name"));
			updateClassName(header_col1,"column,name","");
		var header_col3 = document.createElement("th");
			header_col3.appendChild(document.createTextNode("Specialty"));
			updateClassName(header_col3,"column,column_specialty","");
		var header_col2 = document.createElement("th");
			header_col2.appendChild(document.createTextNode("Location"));
			updateClassName(header_col2,"column,column_location","");
		var header_col4 = document.createElement("th");
			//header_col4.innerHTML = 'All? <input type="checkbox" name="saveall" onclick="javascript: saveItem(\'p\',0,this);" />';
			header_col4.appendChild(document.createTextNode(" "));
			updateClassName(header_col4,"column,column_save","");
		var header_col_row = document.createElement("tr");
			header_col_row.appendChild(header_col1);
			header_col_row.appendChild(header_col3);
			header_col_row.appendChild(header_col2);
			header_col_row.appendChild(header_col4);
		searchTBODY.appendChild(header_col_row);

		for (var i=0; i<PHYSICIAN_DATA.length; i++) {
			var current_physician = PHYSICIAN_DATA[i];
			var pid = current_physician.getAttribute("id");
			var cell_name = document.createElement("td");
				cell_name.appendChild(document.createTextNode(current_physician.getElementsByTagName("t")[0].firstChild.nodeValue));
			var cell_specialty = document.createElement("td");
				cell_specialty.appendChild(parseDelimitedList(current_physician.getElementsByTagName("spec")[0].firstChild.nodeValue,";","br"));
			var cell_location = document.createElement("td");
				cell_location.noWrap = true;
				cell_location.appendChild(parseDelimitedList(current_physician.getElementsByTagName("loc")[0].firstChild.nodeValue,";","br"));
			var cell_save = document.createElement("td");
				cell_save.innerHTML = '<img src="/Images/locator/save.gif" onclick="javascript: saveItem(\'p\','+pid+',this);" style="cursor:pointer;" />';
			var row = document.createElement("tr");
				updateClassName(row,"compact","");
				row.appendChild(cell_name);
				row.appendChild(cell_specialty);
				row.appendChild(cell_location);
				row.appendChild(cell_save);
			if((i % 2) === 0) {
				updateClassName(row,"alt","");
			}
			row.onmouseover = function() { highlightRow(this); };
			row.onmouseout = function() { unhighlightRow(this); };
			(function(pid) { // closure to properly scope the iterator (i) variable for onclick method
				var pid2 = pid;
				cell_name.onclick = function() { showPhysicianDetail(pid2); };
				cell_specialty.onclick = function() { showPhysicianDetail(pid2); };
				cell_location.onclick = function() { showPhysicianDetail(pid2); };
			})(pid);
			searchTBODY.appendChild(row);
		}
	} else {
		displayDescription("No physicians matched your search.");
	}
	showResultsPreview(PHYSICIAN_DATA.length);
	swapSubmitButton("update");
	return true;
}

function displayPhysicianResults_expanded(xmlHttp) {
	if(typeof xmlHttp != "undefined") { PHYSICIAN_DATA = xmlHttp.responseXML.getElementsByTagName("p"); }
	else if(PHYSICIAN_DATA === null) { return false; }
	clearData(searchTBODY);
	if(PHYSICIAN_DATA.length > 0) {
		var header_col1 = document.createElement("th");
			//header_col1.appendChild(document.createTextNode("Photo"));
			header_col1.appendChild(document.createTextNode(" "));
			//header_col1.innerHTML = '&nbsp;';
			updateClassName(header_col1,"column,column_photo","");
		var header_col3 = document.createElement("th");
			header_col3.appendChild(document.createTextNode("Name & Specialty"));
			updateClassName(header_col3,"column,column_name","");
		var header_col2 = document.createElement("th");
			header_col2.appendChild(document.createTextNode("Clinics"));
			updateClassName(header_col2,"column,column_clinics","");
		var header_col4 = document.createElement("th");
			//header_col4.innerHTML = 'All? <input type="checkbox" name="saveall" onclick="javascript: saveItem(\'p\',0,this);" />';
			header_col4.innerHTML = '&nbsp;';
			updateClassName(header_col4,"column,column_save","");
		var header_col_row = document.createElement("tr");
			header_col_row.appendChild(header_col1);
			header_col_row.appendChild(header_col3);
			header_col_row.appendChild(header_col2);
			header_col_row.appendChild(header_col4);
		searchTBODY.appendChild(header_col_row);
				//console.log(PHYSICIAN_DATA.length);
		for (var i=0; i<PHYSICIAN_DATA.length; i++) {
			var current_physician = PHYSICIAN_DATA[i];
			var pid = current_physician.getAttribute("id");
			
			var ptype = current_physician.getAttribute("type");
			if (ptype && ptype.length && !document.getElementById("nonsanfordheader")){
				var cell_non = document.createElement("td");
				cell_non.appendChild(document.createTextNode("Non-Sanford Physicians"));
				cell_non.setAttribute("colSpan", "4");
				
				var row = document.createElement("tr");
					row.setAttribute("id", "nonsanfordheader");
					row.appendChild(cell_non);

				searchTBODY.appendChild(row);
			}
			
			var cell_img = document.createElement("td");
				if(current_physician.getElementsByTagName("img")[0].firstChild.nodeValue == "1") {
					cell_img.innerHTML = '<img src="http://www.sanfordhealth.org/ClassLibrary/__sitecomponents/locator/images/physicians/'+current_physician.getAttribute("id")+'.jpg" height="100" width="89" style="border: 1px solid #9dbad5;" />';
				} else {
					cell_img.appendChild(document.createTextNode(""));
				}
			var cell_ns = document.createElement("td");
				var tmphtml = '<h3 class="svred">'+current_physician.getElementsByTagName("t")[0].firstChild.nodeValue+"</h3>";
				var tmpspecs = current_physician.getElementsByTagName("spec")[0].firstChild.nodeValue.split(";");
				for(var s=0; s<tmpspecs.length; s++) {
					tmphtml += tmpspecs[s] + "<br />";
				}
				cell_ns.innerHTML = tmphtml;
				cell_ns.onclick = function() {  }
			var cell_fac = document.createElement("td");
				if (current_physician.getElementsByTagName("fac")[0].firstChild === null){
					cell_fac.appendChild(document.createTextNode(""));
				} else {
					cell_fac.appendChild(parseDelimitedList(current_physician.getElementsByTagName("fac")[0].firstChild.nodeValue,";","br"));
				}
			var cell_save = document.createElement("td");
				cell_save.innerHTML = '<img src="/Images/locator/save.gif" onclick="javascript: saveItem(\'p\','+pid+',this);" style="cursor:pointer;" />';
				
			var row = document.createElement("tr");
				row.appendChild(cell_img);
				row.appendChild(cell_ns);
				row.appendChild(cell_fac);
				row.appendChild(cell_save);
			if((i % 2) === 0) {
				updateClassName(row,"alt","");
			}
			row.onmouseover = function() { highlightRow(this); };
			row.onmouseout = function() { unhighlightRow(this); };
			(function(pid) { // closure to properly scope the iterator (i) variable for onclick method
				var pid2 = pid;
				cell_img.onclick = function() { showPhysicianDetail(pid2); };
				cell_ns.onclick = function() { showPhysicianDetail(pid2); };
				cell_fac.onclick = function() { showPhysicianDetail(pid2); };
			})(pid);
			searchTBODY.appendChild(row);
		}
	} else {
		displayDescription("No physicians matched your search.");
	}
	showResultsPreview(PHYSICIAN_DATA.length);
	swapSubmitButton("update");
	if (prpdf) {
		toggleDisplay(prpdf,true);
	}
	return true;
}

function displayFacilityResults(xmlHttp) {
	//displayResultsHeader("Facility Search Results");
	if(typeof xmlHttp != "undefined") { FACILITY_DATA = xmlHttp.responseXML.getElementsByTagName("facility"); }
	else if(FACILITY_DATA === null) { return false; }
	clearData(searchTBODY);
	if(FACILITY_DATA.length > 0) {
		var header_col1 = document.createElement("th");
			header_col1.appendChild(document.createTextNode("Facility Name"));
			updateClassName(header_col1,"column,column_name","");
		var header_col2 = document.createElement("th");
			header_col2.appendChild(document.createTextNode("Location"));
			updateClassName(header_col2,"column,column_location","");
		var header_col3 = document.createElement("th");
			header_col3.appendChild(document.createTextNode("Facility Type"));
			updateClassName(header_col3,"column,column_facility","");
		//var header_col4 = document.createElement("th");
		//	//header_col4.innerHTML = 'All? <input type="checkbox" name="saveall" onclick="javascript: saveItem(\'f\',0,this);" />';
		//	header_col4.innerHTML = '&nbsp;';
		//	updateClassName(header_col4,"column,column_save","");
		var header_col_row = document.createElement("tr");
			header_col_row.appendChild(header_col1);
			header_col_row.appendChild(header_col2);
			header_col_row.appendChild(header_col3);
		//	header_col_row.appendChild(header_col4);
		searchTBODY.appendChild(header_col_row);
		
		for (var i=0; i<FACILITY_DATA.length; i++) {
			var current_facility = FACILITY_DATA[i];
			var fid = current_facility.getAttribute("id");
			var cell_name = document.createElement("td");
				cell_name.appendChild(document.createTextNode(current_facility.getElementsByTagName("name")[0].firstChild.nodeValue));
			var cell_location = document.createElement("td");
				cell_location.noWrap = true;
				cell_location.appendChild(document.createTextNode(current_facility.getElementsByTagName("location")[0].firstChild.nodeValue));
			var cell_specialty = document.createElement("td");
				cell_specialty.appendChild(parseDelimitedList(current_facility.getElementsByTagName("type")[0].firstChild.nodeValue,";","br"));
			//var cell_save = document.createElement("td");
			//	cell_save.innerHTML = '<img src="/Images/locator/save.gif" onclick="javascript: saveItem(\'f\','+fid+',this);" style="cursor:pointer;" />';
			var row = document.createElement("tr");
				row.appendChild(cell_name);
				row.appendChild(cell_location);
				row.appendChild(cell_specialty);
			//	row.appendChild(cell_save);
			if((i % 2) === 0) {
				updateClassName(row,"alt","");
			}
			row.onmouseover = function() { highlightRow(this); };
			row.onmouseout = function() { unhighlightRow(this); };
			(function(fid) { // closure to properly scope the iterator (i) variable for onclick method
				var fid2 = fid;
				cell_name.onclick = function() { showFacilityDetail(fid2); };
				cell_location.onclick = function() { showFacilityDetail(fid2); };
				cell_specialty.onclick = function() { showFacilityDetail(fid2); };
			})(fid);
			searchTBODY.appendChild(row);
		}
	} else {
		displayDescription("No facilities matched your search.");
	}
	showResultsPreview(FACILITY_DATA.length);
	swapSubmitButton("update");
	toggleDisplay(prpdf,false);
	return true;
}

function displayResultsHeader(title,obj) {
	var h2 = document.createElement("h2");
		h2.appendChild(document.createTextNode(title));
	var header = document.createElement("th");
		header.appendChild(h2);
		header.colSpan = 4;
		header.setAttribute("colspan","4");
		updateClassName(header,"heading","");
	var header_row = document.createElement("tr");
		header_row.appendChild(header);
	if (searchTBODY) {
		if(typeof obj == "undefined") {
			searchTBODY.appendChild(header_row);
		} else {
			obj.appendChild(header_row);
		}
	}
}

function displayDescription(txt,obj) {
	var tmp_cell = document.createElement("td");
		tmp_cell.appendChild(parseDelimitedList(txt,";","br"));
		tmp_cell.colSpan = 4;
		tmp_cell.setAttribute("colspan","4");
	var tmp_row = document.createElement("tr");
		tmp_row.appendChild(tmp_cell);
	if (searchTBODY) {
		if(typeof obj == "undefined") {
			searchTBODY.appendChild(tmp_row);
		} else {
			obj.appendChild(tmp_row);
		}
	}
}

function resetSearch() {
	document.getElementById('txt_zip').value = "";
	document.getElementById('txt_radius').value = "";
	document.getElementById('sel_location').selectedIndex = 0;
	document.getElementById('txt_name').value = "";
	document.getElementById('cb_new').checked = false;
	document.getElementById('sel_specialty').selectedIndex = 0;
	document.getElementById('sel_type').selectedIndex = 0;
	document.getElementById('sel_lang').selectedIndex = 0;
	document.getElementById('rad_gender_n').checked = true;
	if(SEARCHTYPE == "f") {updateSearchForm("Facility",false);}
	else {updateSearchForm("",false);}
}

function showResultsPreview(num) {
	clearData(resultsPreview);
	var results = parseInt(num,10);
	var label = " records";
	if(results == 1) {label = " record";}
	var fadefrom = "#0761b8";
	if(SEARCHTYPE == "f") {
		fadefrom = "#b60008";
	}
	if (resultsPreview) {
		if(!isNaN(results)) {
			resultsPreview.appendChild(document.createTextNode(results));
			resultsPreview.appendChild(document.createTextNode(label));
		} else {
			resultsPreview.appendChild(document.createTextNode("N/A"));
		}
	}
	initialload = false;
}

function updateSearchForm(type,showresults) {
	//initVars();
	var stype = type;
	//alert(location.hash);
	if(stype === "") {
		if(location.hash.indexOf("fsearch") > -1 || location.search.indexOf("searchtype=Facility") > -1) { stype = "Facility"; } 
	}
	if(stype == "Facility") {
		SEARCHTYPE = "f";
		document.getElementById("searchtype").value = "Facility";
		document.getElementById("locatorsearchcontainer").style.borderColor = "#b60008";
		row_physicianname.style.display = "none";
		row_physiciannew.style.display = "none";
		row_physicianlanguage.style.display = "none";
		row_physiciangender.style.display = "none";
		row_facilitytype.style.display = "";
		PHYSICIAN_DATA = null; // reset physician search data
		toggleDisplay(prpdf,false);
	} else {
		SEARCHTYPE = "p";
		if (document.getElementById("searchtype")) { document.getElementById("searchtype").value = "Physician"; }
		if (document.getElementById("locatorsearchcontainer")) { document.getElementById("locatorsearchcontainer").style.borderColor = "#1a4966"; }
		if (row_physicianname) { row_physicianname.style.display = ""; }
		if (row_physiciannew) { row_physiciannew.style.display = ""; }
		if (row_physicianlanguage) { row_physicianlanguage.style.display = ""; }
		if (row_physiciangender) { row_physiciangender.style.display = ""; }
		if (row_facilitytype) { row_facilitytype.style.display = "none"; }
		if (prpdf) { toggleDisplay(prpdf,true); }
	}
	
	if(initialload == true && (location.hash.indexOf("savetop") > -1 || location.search.indexOf("&saved=") > -1)) {
		loadSavedResults();
		showSavedResults();
	} else if(showresults || (formIsFilled() && initialload)) {
		getResults("results");
	} else {
		getResults('preview');
		clearData(searchTBODY);
		displayResultsHeader(stype + " Search Results");
		displayDescription("Select your search criteria using the form above.;To view the results, press the \"View Results\" button.");
	}
	swapSubmitButton("view");
	return false;
}

function saveItem(type,id,img) {
	//alert("saveItem("+type+","+id+")");
	SAVED_DATA[type] += id+",";
	setCookie("pflsave",SAVED_DATA["p"]+";"+SAVED_DATA["f"],30,"/")
	//alert(SAVED_DATA["p"]+"\n"+SAVED_DATA["f"]+"\n"+getCookie("pflsave"));
	SAVED_DIFF = 1;
	toggleDisplay(img,false)
}

function removeItem(type,id) {
	//alert("removeItem("+type+","+id+")");
	var ns = "";
	var tmp = SAVED_DATA[type].split(",");
	for(var i=0; i<tmp.length; i++) {
		if(tmp[i] != id && tmp[i] != '') { ns += tmp[i]+","; }
	}
	SAVED_DATA[type] = ns;
	setCookie("pflsave",SAVED_DATA["p"]+";"+SAVED_DATA["f"],30,"/")
	toggleDisplay(type+id,false);
	//alert(SAVED_DATA["p"]+"\n"+SAVED_DATA["f"]+"\n"+getCookie("pflsave"));
	SAVED_DIFF = 1;
}

function loadSavedResults() {
	var cval = getCookie("pflsave");
	if(cval !== null) {
		var split = cval.split(";");
		if(split.length == 2) {
			SAVED_DATA["p"] = split[0];
			SAVED_DATA["f"] = split[1];
		}
	}
	//alert(SAVED_DATA["p"]+"\n"+SAVED_DATA["f"]+"\n"+getCookie("pflsave"));
	SAVED_DIFF = 1;
}

function expandResults() {
	if(SAVED_VISIBLE == 0) {
		if(SEARCHTYPE == "p") {
			showSearchResults();
			if(P_EXPANDED == 1) {
				displayPhysicianResults();
				P_EXPANDED = 0;
			} else {
				displayPhysicianResults_expanded();
				P_EXPANDED = 1;
			}
			updateExpandToggle();
		} else if(SEARCHTYPE == "f")  {
			//alert("Not Available");
		}
	}
}
function updateExpandToggle() {
	toggleDisplay(expand_toggle,true);
	if(P_EXPANDED == 1) { expand_toggle.src = expand_toggle.src.replace("_expand","_compact"); }
	else { expand_toggle.src = expand_toggle.src.replace("_compact","_expand"); }
}

function swapSubmitButton(status) {
	var pathroot = location.protocol + "//" + location.host;
	var view = "/Images/locator/viewresults.gif";
	var update = "/Images/locator/updateresults.gif";
	if (submitsearch) {
		if(status == "update") {
			if(submitsearch.src != pathroot+update) {
				submitsearch.src = update;
			}
		} else {
			if(submitsearch.src != pathroot+view) {
				submitsearch.src = view;
			}
		}
	}
}

function showSearchResults() {
	if(SEARCHTYPE == "f") { toggleDisplay(prpdf,false); }
	toggleDisplay(savedTBODY,false);
	savedTBODY.style.display = "none";
	savedTBODY.style.width = "100%";
	toggleDisplay(searchTBODY,true);
	searchTBODY.style.display = "";
	searchTBODY.style.width = "100%";
	resultsTable.style.borderColor = "#1a4966";
	SAVED_VISIBLE = 0;
}

function showSavedResults() {
	toggleDisplay(searchTBODY,false);
	savedTBODY.style.display = "";
	savedTBODY.style.width = "100%";
	toggleDisplay(savedTBODY,true);
	searchTBODY.style.display = "none";
	searchTBODY.style.width = "100%";
	toggleDisplay(prpdf,true);
	resultsTable.style.borderColor = "#b60008";
	
	if(SAVED_DIFF == 1) {
		clearData(savedTBODY);
		document.location.hash = "#searchtop";
		displayDescription("Loading Saved Results ...",savedTBODY);
		ajaxobj.setQueryString("?action=s_results&p="+SAVED_DATA["p"]+"&f="+SAVED_DATA["f"]);
		ajaxobj.setHandler(displaySavedResults);
		ajaxobj.execute();
	}
	SAVED_VISIBLE = 1;
}

function displaySavedResults(xmlHttp) {
	var physicians = xmlHttp.responseXML.getElementsByTagName("p");
	var facilities = xmlHttp.responseXML.getElementsByTagName("f");
	clearData(savedTBODY);
	if(physicians.length > 0) {
		var h1 = document.createElement("th");
			h1.appendChild(document.createTextNode("Physician Name"));
			updateClassName(h1,"column,column_name","");
		var h2 = document.createElement("th");
			h2.appendChild(document.createTextNode("Specialty"));
			updateClassName(h2,"column,column_specialty","");
		var h3 = document.createElement("th");
			h3.appendChild(document.createTextNode("Clinics"));
			updateClassName(h3,"column,column_clinics","");
		var h4 = document.createElement("th");
			h4.innerHTML = '&nbsp;';
			updateClassName(h4,"column,column_remove","");
		var header_col_row = document.createElement("tr");
			header_col_row.appendChild(h1);
			header_col_row.appendChild(h2);
			header_col_row.appendChild(h3);
			header_col_row.appendChild(h4);
		savedTBODY.appendChild(header_col_row);
		for (var i=0; i<physicians.length; i++) {
			var current_item = physicians[i];
			var thisid = current_item.getAttribute("id");
			var d1 = document.createElement("td");
				d1.appendChild(document.createTextNode(current_item.getElementsByTagName("d1")[0].firstChild.nodeValue));
			var d2 = document.createElement("td");
				d2.appendChild(parseDelimitedList(current_item.getElementsByTagName("d2")[0].firstChild.nodeValue,";","br"));
			var d3 = document.createElement("td");
				d3.appendChild(parseDelimitedList(current_item.getElementsByTagName("d3")[0].firstChild.nodeValue,";","br"));
			var cell_del = document.createElement("td");
				cell_del.innerHTML = '<img src="/Images/locator/remove.gif" onclick="javascript: removeItem(\'p\','+thisid+');" style="cursor:pointer;" />';
			var row = document.createElement("tr");
				row.appendChild(d1);
				row.appendChild(d2);
				row.appendChild(d3);
				row.appendChild(cell_del);
			if((i % 2) === 0) {
				updateClassName(row,"alt","");
			}
			row.onmouseover = function() { highlightRow(this); };
			row.onmouseout = function() { unhighlightRow(this); };
			row.setAttribute("id","p"+thisid);
			(function(thisid) { // closure to properly scope the iterator (i) variable for onclick method
				var thisid2 = thisid;
				d1.onclick = function() { showPhysicianDetail(thisid2,true); };
				d2.onclick = function() { showPhysicianDetail(thisid2,true); };
				d3.onclick = function() { showPhysicianDetail(thisid2,true); };
			})(thisid);
			savedTBODY.appendChild(row);
		}
	}
	
	if(facilities.length > 0) {
		var h1 = document.createElement("th");
			h1.appendChild(document.createTextNode("Facility Name"));
			updateClassName(h1,"column,column_name","");
		var h2 = document.createElement("th");
			h2.appendChild(document.createTextNode("Type"));
			updateClassName(h2,"column,column_type","");
		var h3 = document.createElement("th");
			h3.appendChild(document.createTextNode("Location"));
			updateClassName(h3,"column,column_location","");
		var h4 = document.createElement("th");
			h4.innerHTML = '&nbsp;';
			updateClassName(h4,"column,column_remove","");
		var header_col_row = document.createElement("tr");
			header_col_row.appendChild(h1);
			header_col_row.appendChild(h2);
			header_col_row.appendChild(h3);
			header_col_row.appendChild(h4);
		savedTBODY.appendChild(header_col_row);
		for (var i=0; i<facilities.length; i++) {
			var current_item = facilities[i];
			var thisid = current_item.getAttribute("id");
			var d1 = document.createElement("td");
				d1.appendChild(document.createTextNode(current_item.getElementsByTagName("d1")[0].firstChild.nodeValue));
			var d2 = document.createElement("td");
				d2.appendChild(parseDelimitedList(current_item.getElementsByTagName("d2")[0].firstChild.nodeValue,";","br"));
			var d3 = document.createElement("td");
				d3.appendChild(parseDelimitedList(current_item.getElementsByTagName("d3")[0].firstChild.nodeValue,";","br"));
			var cell_del = document.createElement("td");
				cell_del.innerHTML = '<img src="/Images/locator/remove.gif" onclick="javascript: removeItem(\'f\','+thisid+');" style="cursor:pointer;" />';
			var row = document.createElement("tr");
				row.appendChild(d1);
				row.appendChild(d2);
				row.appendChild(d3);
				row.appendChild(cell_del);
			if((i % 2) === 0) {
				updateClassName(row,"alt","");
			}
			row.onmouseover = function() { highlightRow(this); };
			row.onmouseout = function() { unhighlightRow(this); };
			row.setAttribute("id","f"+thisid);
			(function(thisid) { // closure to properly scope the iterator (i) variable for onclick method
				var thisid2 = thisid;
				d1.onclick = function() { showFacilityDetail(thisid2,true); };
				d2.onclick = function() { showFacilityDetail(thisid2,true); };
				d3.onclick = function() { showFacilityDetail(thisid2,true); };
			})(thisid);
			savedTBODY.appendChild(row);
		}
	}
	if(physicians.length == 0 && facilities.length == 0) {
		displayDescription("There are no physicians in your saved results.",savedTBODY);
	}
	SAVED_DIFF = 0; // set this to OFF so that we don't reload if not necessary
	getResults('preview');
}

function showPhysicianDetail(id,saved) {
	//alert(id);
	var qs = getQueryString();
	if(saved == true) { qs += "&saved=1"; }
	window.location = "?pid="+id+qs;
}
function showFacilityDetail(id,saved) {
	//alert(id);
	var qs = getQueryString();
	if(saved == true) { qs += "&saved=1"; }
	window.location = "?fid="+id+qs;
}

function highlightRow(row) {
	updateClassName(row,"over","");
}
function unhighlightRow(row) {
	updateClassName(row,"","over");
}

function printResultsPDF() {
	var pids = "";
	var fids = "";
	if(SAVED_VISIBLE == 1) {
		pids = SAVED_DATA["p"];
		fids = SAVED_DATA["f"];
		window.open("/ClassLibrary/com/locator/pdf_results.cfm?p="+pids,'rpdf');
		//alert("Print Saved PDF\np:"+pids+"\nf:"+fids);
	} else if(SEARCHTYPE == "p") {
		if(PHYSICIAN_DATA !== null) {
			for(var i=0; i<PHYSICIAN_DATA.length; i++) {
				pids += PHYSICIAN_DATA[i].getAttribute("id")+",";
			}
			var allflag = "all=0";
			if(ALL_RECORDS == true) { allflag = "all=1"; }
			//alert("/ClassLibrary/com/locator/pdf_results.cfm?p="+pids);
			window.open("/ClassLibrary/com/locator/pdf_results.cfm?"+allflag+"&p="+pids,'rpdf');
			//window.open("/ClassLibrary/com/locator/pdf_results.cfm?",'rpdf','height=650,width=770');
		}
		//alert("Print Physician PDF\np:"+pids);
	} else if(SEARCHTYPE == "f") {
		if(FACILITY_DATA !== null) {
			for(var i=0; i<FACILITY_DATA.length; i++) {
				fids += FACILITY_DATA[i].getAttribute("id")+",";
			}
		}
		//window.open("/ClassLibrary/com/locator/pdf_results.cfm?f="+fids,'rpdf');
		//alert("Print Facility PDF\nf:"+fids);
	}
}