var CurrentCity = "";
var CurrentCounty = "";
var CurrentSearchType = "";

function AddRand()
{
     return "&z=" + Math.random();
}
function CreateAJAXObject()
{
	if(navigator.appName == "Microsoft Internet Explorer")
		return new ActiveXObject("Microsoft.XMLHTTP");
	return new XMLHttpRequest();
}

function Ajax(ResponceFunction, RequestURL)
{
       var a = new Object();

       a.AJAXObject = CreateAJAXObject();
       a.Execute = function(){
               a.AJAXObject.open('get', RequestURL);
               a.AJAXObject.onreadystatechange = function(){
                       if(a.AJAXObject.readyState == 4 && a.AJAXObject.status == 200){
                               ResponceFunction(a.AJAXObject.responseText);
                       }
               }
               a.AJAXObject.send(null);
       }
       return a;
}
function AddRand()
{
     return "&z=" + Math.random();
}

function SetSearchType(SearchType)
{
     CurrentSearchType = SearchType;
     if(SearchType)
     {
          if(SearchType == "Location")
          {
               document.getElementById("LocationSearchTable").style.display = "inline";
               document.getElementById("DistanceSearchTable").style.display = "none";
          }
          else if(SearchType == "Distance")
          {
               document.getElementById("LocationSearchTable").style.display = "none";
               document.getElementById("DistanceSearchTable").style.display = "inline";
          }
     }
}

function DoSearch(Source)
{
     var SearchType  = CurrentSearchType;
     var NetworkID = document.getElementById("NetworkID").value;
     if(!NetworkID)
     {
          alert("You need to select a network");
          return;
     }
     if(Source=="Test")
          var URL = "search_ajax2.php?a=DoSearch&g="+NetworkID+"&b=";
     else
          var URL = "search_ajax.php?a=DoSearch&g="+NetworkID+"&b=";

     if(SearchType == "Location")
     {
          var State = document.getElementById("LocationState").value;
          if(!State)
          {
               alert("You need to select a state")
               return;
          }
          var County = document.getElementById("LocationCounty").value;
          var City = document.getElementById("LocationCity").value;
          URL += "LocationSearch&c="+State+"|"+County+"|"+City+"|";
     }
     else if(SearchType == "Distance")
     {
          var ZipCode = document.getElementById("DistanceZip").value;
          if(ZipCode.length != 5)
          {
               alert("You need to enter a valid zip code");
               return;
          }
          var Distance = document.getElementById("DistanceDistance").value;
          if(Distance.length == 0)
          {
               alert("You need to enter a search distance");
               return;
          }
          URL += "DistanceSearch&c="+ZipCode+"|"+Distance+"|";
     }
     var FacilityName = "";//document.getElementById("ProviderFacilityName").value;
     var DoctorLastName   = document.getElementById("ProviderDoctorLastName").value;
     var DoctorFirstName   = document.getElementById("ProviderDoctorFirstName").value;
     var Specialty = document.getElementById("Specialty").value;
     URL += FacilityName+"|"+DoctorLastName+"|"+DoctorFirstName+"|"+Specialty+AddRand();

     document.getElementById("SearchingDiv").className = "visible";
     document.getElementById("ResultsDiv").className = "hidden";
     Ajax(DoSearchResponse, URL).Execute();
}
function DoSearchResponse(Response)
{
     var Results = Response.split('|');

     document.getElementById("SearchingDiv").className = "hidden";
     document.getElementById("ResultsDiv").className = "visible";
     document.getElementById("SearchResults").innerHTML = Results[0];
}

function RefreshLists()
{
     var NetworkID = document.getElementById("NetworkID").value;
     var State = document.getElementById("LocationState").value;
     if(!NetworkID || !State)
          return;
     document.getElementById('WaitDiv').style.display = "block";
     var County = document.getElementById("LocationCounty").value;
     var City = document.getElementById("LocationCity").value;
     CurrentCity = City;
     CurrentCounty = County;
     var Specialty = document.getElementById("Specialty").value;

     var URL = "search_ajax.php?a=RefreshLists&b="+NetworkID+"&c="+State+"|"+County+"|"+City+"|"+Specialty+AddRand();
     Ajax(RefreshListsResponse, URL).Execute();
}
function RefreshListsResponse(Response)
{
     var Results = Response.split('|');
     if(Results[0] == "1")
     {
          var Counties = Results[1].split('~');
          var Count = document.getElementById("LocationCounty").length; // standards compliant
          for(i = 1 ; i <= Count ; i++)
               document.getElementById("LocationCounty").remove(0);
          for(i = 0; i < Counties.length-1 ; i++)
          {
               var CountyData = Counties[i].split('^');

               var Option = document.createElement('option');
               Option.text = CountyData[0];
               Option.value = CountyData[1];
               try
               {
                    document.getElementById('LocationCounty').add(Option,null); // standards compliant
               }
               catch(ex)
               {
                    document.getElementById('LocationCounty').add(Option); // IE only
               }
          }
          document.getElementById("LocationCounty").value = CurrentCounty;

          var Cities = Results[2].split('~');
          Count = document.getElementById("LocationCity").length; // standards compliant
          for(i = 1 ; i <= Count ; i++)
               document.getElementById("LocationCity").remove(0);
          for(i = 0; i < Cities.length-1 ; i++)
          {
               var CityData = Cities[i].split('^');

               var Option = document.createElement('option');
               Option.text = CityData[0];
               Option.value = CityData[1];
               try
               {
                    document.getElementById('LocationCity').add(Option,null); // standards compliant
               }
               catch(ex)
               {
                    document.getElementById('LocationCity').add(Option); // IE only
               }
          }
          document.getElementById("LocationCity").value = CurrentCity;
     }
     else
     {
          alert(Results[1]);
     }

     document.getElementById('WaitDiv').style.display = "none";
}
function RefreshSpecialties()
{
     var NetworkID = document.getElementById("NetworkID").value;
     if(!NetworkID)
          return;
     var URL = "search_ajax.php?a=RefreshSpecialties&b="+NetworkID+AddRand();
     Ajax(RefreshSpecialtiesResponse, URL).Execute();
}
function RefreshSpecialtiesResponse(Response)
{
     var Results = Response.split('|');
     if(Results[0] == "1")
     {
          var Specalties = Results[1].split('~');
          var Count = document.getElementById("Specialty").length; // standards compliant
          for(i = 1 ; i <= Count ; i++)
               document.getElementById("Specialty").remove(0);
          for(i = 0; i < Specalties.length-1 ; i++)
          {
               var Specialty = Specalties[i].split('^');;

               var Option = document.createElement('option');
               Option.text = Specialty[0];
               Option.value = Specialty[1];
               try
               {
                    document.getElementById('Specialty').add(Option,null); // standards compliant
               }
               catch(ex)
               {
                    document.getElementById('Specialty').add(Option); // IE only
               }
          }
     }
     else
          alert(Results[1]);
}
function PrintSearch()
{
    // alert("This is not currently available");
    // return;
     var Specialty = document.getElementById("Specialty").value;
     var SearchType  = CurrentSearchType;//document.getElementById("SearchType").value;
     var NetworkID = document.getElementById("NetworkID").value;
     var URL = "print.php?g="+NetworkID+"&a=";

     if(SearchType == "Location")
     {
          var State = document.getElementById("LocationState").value;
          if(!State)
          {
               alert("You need to select a state");
               return;
          }
          var County = document.getElementById("LocationCounty").value;
          var City = document.getElementById("LocationCity").value;
          URL += "LocationSearch&b="+State+"|"+County+"|"+City+"|";
     }
     else if(SearchType == "Distance")
     {
          var ZipCode = document.getElementById("DistanceZip").value;
          if(ZipCode.length != 5)
          {
               alert("You need to enter a valid zip code");
               return;
          }
          var Distance = document.getElementById("DistanceDistance").value;
          if(Distance.length == 0)
          {
               alert("You need to enter a search distance");
               return;
          }
          URL += "DistanceSearch&b="+ZipCode+"|"+Distance+"|";
     }
     var FacilityName = "";//document.getElementById("ProviderFacilityName").value;
     var DoctorLastName   = document.getElementById("ProviderDoctorLastName").value;
     var DoctorFirstName   = document.getElementById("ProviderDoctorFirstName").value;
     var Specialty = document.getElementById("Specialty").value;
     URL += FacilityName+"|"+DoctorLastName+"|"+DoctorFirstName+"|"+Specialty+AddRand();

     window.open(URL);
}
function ClearSearch()
{
     window.location = "search2.php";
}


