
function getParameter(name)
{
	var element = document.getElementsByName(name);
	var val = ((element != undefined) && (element[0] != undefined)) ? element[0].value : null;
	return val;
}

function getRadioParameter(name)
{
	var result = null;
	
	var element = document.getElementsByName(name);
	if (element.length == 1)
	{
		result = element[0].value;
	}
	else
	{
		for (var i = 0; i < element.length; i++)
		{
			var inputElement = element[i];
			if (inputElement.checked)
			{
				result = inputElement.value;
				break;
			}
		}
	}
	
	return result;
}

function getUrlParameter(parameterName)
{
	var result = null;
	var href = window.location.href;

	if ( href.indexOf("&") > -1 )
	{
		var parameterString = href.substr(href.indexOf("&"));
		var parameterArray = parameterString.split("&");
		
		for ( var i = 0; i < parameterArray.length; i++ )
		{
			if (parameterArray[i].indexOf(parameterName + "=") > -1 )
			{
				var parameter = parameterArray[i].split("=");
				result = parameter[1];
				break;
			}
		}
	}

	return result;
}

function initDropDowns()
{
	populateDropDownsUsingSearchType("doctor");
	populateDropDownsUsingSearchType("facility");
}

function resetDropDownsForNetwork()
{
	var networkId = getRadioParameter("networkId");
	
	populateDropDownsForNetwork(networkId, "doctor", "WI", "no.selection");
	resetState("doctor");
	resetCounty("doctor");
	resetSpecialty("doctor");
	
	populateDropDownsForNetwork(networkId, "facility", "WI", "no.selection");
	resetState("facility");
	resetCounty("facility");
	resetSpecialty("facility");
}

function populateDropDownsForCounty()
{
	var searchType = getRadioParameter("searchType");
	populateDropDownsUsingSearchType(searchType);
}

function populateDropDownsForState()
{
	var networkId = getRadioParameter("networkId");
	var searchType = getRadioParameter("searchType");
	var state = getSelectedState(searchType);
	
	//They changed the state, so reset the county
	var county = "no.selection";
		
	populateDropDownsForNetwork(networkId, searchType, state, county);
	
	//Set the dropdown to "Select a County"
	resetCounty(searchType);
}

function populateDropDownsUsingSearchType(searchType)
{
	var networkId = getRadioParameter("networkId");
	var state = getSelectedState(searchType);
	
	//Get the county and the state
	var county = getSelectedCounty(searchType);
	
	if(county != "no.selection" && county != null)
	{
		//County is an array where index 0 is the county code and 1 holds the state
		if(state != county[1]) {
			state = county[1];
			
			//Select the state in the dropdown
			select = document.getElementById(searchType + "State");
			reselect(select, state);
		}
		
		county = county[0];
	}

	populateDropDownsForNetwork(networkId, searchType, state, county);
}

function populateDropDownsForNetwork(networkId, searchType, state, county)
{
	var networkIdParam = "&networkId="+networkId;
	
	//alert("Performing population for: " + searchType + "\nUsing network: " + networkId + "\nUsing county: " + county + "\nUsing state: " + state + "\nSpecialty was: " + specialty);
	
	//Build the url parameters
	var countyParam = ((county != null) && (county != "no.selection"))
		? "&county=" + county
		: "";
		
	var stateParam = ((state != null) && (state != "no.selection"))
		? "&state=" + state
		: "";

	var searchTypeParam = (searchType != null)
		? "&searchType=" + searchType
		: "";

	var planYear = getParameter("planYear");
	var planYearParam = (planYear != null)
		? "&planYear=" + planYear
		: "";
		
	new Ajax.Request("fadAjax.do", {
		onSuccess : function(resp) {
	    	// alert("The response from the server is: " + resp.responseText);
	    	var xmlDoc = resp.responseXML;

	    	var counties = xmlDoc.getElementsByTagName("county");
	    	var states = xmlDoc.getElementsByTagName("state");
	    	var specialties = xmlDoc.getElementsByTagName("specialty");
			
			repopulateDropDowns(searchType, counties, states, specialties);
 	 },
	 onFailure : function(resp) {
	   // alert("Oops, there's been an error.");
	 },
	 parameters : "action=populateDropDowns" + networkIdParam + stateParam + planYearParam + countyParam + searchTypeParam
	});
}

function repopulateDropDowns(searchType, counties, states, specialties)
{
	//Populate the county select box and reselect the county
	select = document.getElementById(searchType + "CountyName");
	repopulateSelect(select,counties,"Select a County","no.selection");
		
	//Populate the state select box and reselect the state
	select = document.getElementById(searchType + "State");
	repopulateSelect(select,states,"Select a State","no.selection");
		
	//Populate the specialty select box and reselect the specialty
	select = document.getElementById(searchType + "Specialty");
	repopulateSelect(select,specialties,"Select a Specialty","no.selection");
}

function repopulateSelect(selectElement, dataElement, promptText, promptValue)
{
	if (selectElement != undefined)
	{
		var lastSelection = clearSelectOptions(selectElement);
	
	   	addSelectOption(selectElement,promptText,promptValue);
	    for (var i = 0; i < dataElement.length; i++)
	   	{
	   		var data = dataElement[i].firstChild.nodeValue;
	   		var label = dataElement[i].getAttribute("label");
	   		   		
	   		if ((label == null) || (label == ""))
	   		{
	   			label = data;
	   		}
	   		
	   		if(label.indexOf("&amp;") > 0)
	   		{
	   			label = label.replace(/&amp;/g, "&");
	   		}
	   			   		
	   		addSelectOption(selectElement,label,data);
	   	}
	   	if (lastSelection != null)
	   	{
	   		reselect(selectElement,lastSelection);
	   	}
	   	else
	   	{
		   	selectElement.selectedIndex = 0;
	   	}
	}
}

function reselect(selectElement, selection)
{
	if (selectElement != undefined)
	{
		for (var i = 0; i < selectElement.length; i++)
		{
			if (selectElement[i].value == selection)
			{
				selectElement[i].selected = true;
			}
		}
	}
}

function clearSelectOptions(selectElement)
{
	var lastSelection = null;
	if (selectElement != undefined)
	{
		// Always clear an option list from the last entry to the first
		for (var i = selectElement.length; i > 0; i--)
		{
			var element = selectElement[i-1];
			if ((element.selected) && (element.value != 'no.selection'))
			{
				// alert(element.value + " is checked");
				lastSelection = element.value;
			}
			selectElement[i-1] = null;
		}
	}
	
	return lastSelection;
}

function addSelectOption(selectElement, optionText, optionValue)
{
   // Add option to the bottom of the list
   selectElement[selectElement.length] = new Option(optionText, optionValue);
}

function addFormFields(link)
{
	var url = link.protocol + "//" + link.hostname + getPathName(link);
	var params = link.search.slice(1);
	
	var formParams = Form.serialize(document.forms["basicSearchForm"]);
	return url + "?" + params + "&" + formParams;
}

/**
function addMemberNumber(link)
{
	var url = link.protocol + "//" + link.hostname + getPathName(link);
	var params = link.search.slice(1);
	
	var forms = document.forms;
	for (var i = 0; i < forms.length; i++)
	{
		var form = forms[i];
		var elements = form.elements;
		var element = elements["memberNumber"];
		if (element)
		{
			var param = Form.Element.serialize(element);
			url = url + "?" + params + "&" + param;
			break;
		}
	}
	
	return url;
}
**/

function getPathName(link)
{
	// IE doesn't include the initial '/' but mozilla does. WTF?!?
	return (link.pathname.charAt(0) != "/")
		? "/" + link.pathname
		: link.pathname;
	
}

// This function moves the page links up a row for formatting
// Should be used when setting the page links on top of the display table
// YOU MUST HAVE A SPAN ABOVE THE TABLE OR THIS FUNCTION WILL NOT WORK!
function copyPageLinkSpan(toSpanId)
{
	var spans = document.getElementsByTagName("span");
	var theid = document.getElementById(toSpanId);
	var pagelinkIndex = -1;

	for (var i=0;i<spans.length;i++)
	{
		if (spans[i].className == 'pagelinks')
		{
			pagelinkIndex = i;
		}
	}
		
	if(pagelinkIndex != -1)
	{
		theid.innerHTML = spans[pagelinkIndex].innerHTML;
	}
}

function removeSpanByClass(spanClassName)
{
	var spans = document.getElementsByTagName("span");
	for (var i=0;i<spans.length;i++)
	{
		if (spans[i].className == spanClassName)
		{
			spans[i].innerHTML="";
			spans[i].outerHTML="";
		}
	}
}

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

function setSearchType(searchtype)
{
	if (document.forms['basicSearchForm'] == undefined) return;
	if (document.forms['basicSearchForm'].elements['searchType'] == undefined) return;
	var radioObj = document.forms['basicSearchForm'].elements['searchType'];
	if (radioObj.length == undefined) return;
	setCheckedValue(radioObj,searchtype);
}

function setWantsCoverPage(value)
{
	if (document.forms['basicSearchForm'] == undefined) return;
	if (document.forms['basicSearchForm'].elements['wantsCoverPage'] == undefined) return;
	var radioObj = document.forms['basicSearchForm'].elements['wantsCoverPage'];
	if (radioObj.length == undefined) return;
	setCheckedValue(radioObj,value);
}

function isArray(a)
{
    return isObject(a) && a.constructor == Array;
}

function isFunction(a)
{
    return typeof a == 'function';
}

function isObject(a)
{
    return (a && typeof a == 'object') || isFunction(a);
}

function getSelectedCounty(searchType)
{
	var county = getParameter(searchType + "CountyName");
	var county_state_array = "no.selection";
	
	if(county != "no.selection" && county != null) {
		county_state_array=county.split(" ");
	}
	
	return(county_state_array);
}

function getSelectedState(searchType)
{
	var state = getParameter(searchType + "State");
		
	if(state == null) {
		state = "no.selection";
	}
	
	return(state);
}

function resetState(searchType)
{
	select = document.getElementById(searchType + "State");
	reselect(select, "WI");
}

function resetCounty(searchType)
{
	select = document.getElementById(searchType + "CountyName");
	reselect(select, "no.selection");
}

function resetSpecialty(searchType)
{
	select = document.getElementById(searchType + "Specialty");
	reselect(select, "no.selection");
}