function __Swap()
{
	var service = document.getElementById('service').value, 
		specialty = document.getElementById('specialty');

	// Clear specialty.
	specialty.options.length = 0;

	// Add an "(all)" option.
	var useIndex = false;
	try
	{
		specialty.add(new Option("(all)",-1));
	}
	catch(e)
	{
		useIndex = true;
		specialty.add(new Option("(all)",-1),null);	
	}
	
	// Add the new specialties.
	for (var i=1,l=Specialty.length;i<l;i++)
		if (Specialty[i][0] == service)
		{		
			var s = Specialty[i][1];
			for (var j=1,l2=s.length;j<l2;j++)
				if (!useIndex)
					specialty.add(new Option(s[j][1],s[j][0]));
				else
					specialty.add(new Option(s[j][1],s[j][0]),null);
			break;
		}
	
	// Select the "(all)" option.
	specialty.options[0].selected = true;
}

function __Search(value)
{
	var timeout = 10000;
	var r = document.getElementById('results');

    // Create an XMLHttpRequest object.
    var ua = navigator.userAgent.toLowerCase(), x = (/msie/.test(ua) && !/opera/.test(ua)) ? new ActiveXObject(ua.indexOf("msie 5") >= 0 ? "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP") : new XMLHttpRequest();   
    x.open("GET", 'Default.aspx?'+value, true);
        
	var onreadystatechange = function(istimeout){
		if (x && x.readyState == 4) {
			r.innerHTML = x.status == 200 ? x.responseText : "An error occurred performing the search, please try again.";
			if (r.getElementsByTagName("td").length == 0) r.innerHTML = "<span class=\"b\">No results were found.</span>";
			x.onreadystatechange = function(){};
			x = null;
		}
	};
	x.onreadystatechange = onreadystatechange;
	
	
	// Timeout the request.
	setTimeout(function(){
		if (x) {
			x.abort();
			x = null;
		}
	}, timeout);
	
	// Send the request.
	x.send(null);
}

function __Initialize()
{
	var c = function(){document.getElementById('results').innerHTML = "";}
	var inputs = document.getElementById('search1').getElementsByTagName('input');
	inputs[0].onclick = function(){__Search("Service=" + document.getElementById('service').value + "," + document.getElementById('specialty').value);}
	inputs[1].onclick = c;
	
	inputs = document.getElementById('search2').getElementsByTagName('input');
	inputs[0].onclick = function(){__Search("LastName=" + encodeURIComponent(document.getElementById('lastname').value));}
	inputs[1].onclick = c;
	
	document.getElementById('lastname').value = '';
	
	var s = document.getElementById('service');
	s.onchange = __Swap;
	s.selectedIndex = 0;
	__Swap();
}
