﻿// Search
var _SearchType = "1";
var _SelSpecialtyID = "";
var _SelSubSpecialtyID = "";
var _SelAreaID = "";
var _TxtNameID = "";

function Home_ShowOptions (tmpSearchType, tmpSelSpecialtyID, tmpSelSubSpecialtyID, tmpSelAreaID, tmpTxtNameID)
{			
	_SearchType = tmpSearchType;
    _SelSpecialtyID = tmpSelSpecialtyID;
    _SelSubSpecialtyID = tmpSelSubSpecialtyID;
    _SelAreaID = tmpSelAreaID;
    _TxtNameID = tmpTxtNameID;
	
	for (i=1; i<=4 ; i++ )
	{
		document.getElementById ('searchBox_button_' + i).className = 'searchBox_button';
		document.getElementById ('search_options_' + i).style.display = 'none';
	}

	document.getElementById ('searchBox_button_' + tmpSearchType).className = 'searchBox_button searchBox_button_selected';
	document.getElementById ('search_options_' + tmpSearchType).style.display = '';
}

function Home_SetSpecialties()
{
    var specialtySel = document.getElementById(_SelSpecialtyID);
    specialtySel.length = _ArrSpecialty.length+1;
    specialtySel.options[0].text = "-- בחר התמחות --";
    specialtySel.options[0].value = "";
    for (var i = 0 ; i < _ArrSpecialty.length ; i++)
    {
        if (_ArrSpecialty[i] != "")
        {
            var specialtyValues = _ArrSpecialty[i].split("|");
            specialtySel.options[i+1].text = specialtyValues[0];
            specialtySel.options[i+1].value = specialtyValues[1];
        }
    }
    specialtySel.selectedIndex = 0;
}
function Home_GetSubSpecialties(tmpSelSpecialty, tmpSelSubSpecialty)
{
    _SelSpecialtyID = tmpSelSpecialty;
    _SelSubSpecialtyID = tmpSelSubSpecialty;

    var selectedSpecialty = document.getElementById(_SelSpecialtyID).value;
    if (selectedSpecialty != "")
    {
        // Get the sub-specialties
        CallService("SpecialtiesService.aspx", "parent=" + selectedSpecialty, "Home_SetSubSpecialities(xhr.responseText)");  
    }
    else
    {
        document.getElementById(_SelSubSpecialtyID).selectedIndex = 0;
    }
}
function Home_SetSubSpecialities(listSubSpecialities)
{
    var selSubSpecialty = document.getElementById(_SelSubSpecialtyID);
    var arrSubSpec = listSubSpecialities.split("~");
    selSubSpecialty.length = arrSubSpec.length+1;
    for (var i = 0 ; i < arrSubSpec.length ; i++)
    {
        if (arrSubSpec[i] != "")
        {
            var subSpecValues = arrSubSpec[i].split("|");
            selSubSpecialty.options[i+1].text = subSpecValues[0];
            selSubSpecialty.options[i+1].value = subSpecValues[1];
        }
    }
    selSubSpecialty.selectedIndex = 0;
}
function Search()
{
    var objType;
    
    switch(_SearchType)
    {
        case "1": // doctors
            objType = "1"; // doctors
            break;
        case "2":
            objType = "2"; // institutes
            break;
        case "3": // dentists
            objType = "1"; // doctors
            break;
        case "4":
            objType = "4"; // alternative
            break;
    }
    
    var searchFilter = "";
    var searchType = "";
    var area = "";

    var nameTxt = document.getElementById(_TxtNameID);
    var name = nameTxt.value;
    
    if (name == "")
    {
        var specialtySel = document.getElementById(_SelSpecialtyID);
        var subSpecSel = "";
        if (_SelSubSpecialtyID != "")
        {
            subSpecSel = document.getElementById(_SelSubSpecialtyID);
        }
        var areaSel = document.getElementById(_SelAreaID);

        if (areaSel.value != "-1" && areaSel.value != "") 
        { 
            area = areaSel.value;
        }

        if (specialtySel.value != "-1" && specialtySel.value != "") 
        { 
            searchFilter += specialtySel.value; 
        }
        if (_SelSubSpecialtyID != "")
        {
            if (subSpecSel.value != "-1" && subSpecSel.value != "") 
            { 
               searchFilter += "%3b" + subSpecSel.value; 
            }
        }

        if (searchFilter == "") { alert("אנא בחרו התמחות"); return false;}

        if (_SelSubSpecialtyID != "" && subSpecSel.value != "-1" && subSpecSel.value != "") 
        { 
            searchType = "3";
        }
        else if (specialtySel.value != "-1" && specialtySel.value != "") 
        { 
            searchType = "2";
        }
    }
    else // search by name
    {
        searchFilter = name;
        searchType = "1";
    }

    if (searchType != "")
    {
        var page = "";
        switch(objType)
        {
            case "1": // doctors
                page = "/resultsexperts.aspx";
            case "4": // alternative
                page = "/resultsexperts.aspx";
                break;
            case "2": // institutes
                page = "/resultsinstitutes.aspx";
                break;
        }
        location.href = page + "?o=" + objType + "&a=" + area + "&t=" + searchType + "&k=" + searchFilter;
    }
    
    return false;
}
