/**
* Function search state after page loading
* Use for state search additional attribute
*/
function loadStates()
{
    var wRequest =  new Sys.Net.WebRequest();
    
    if(typeof(extend) != 'undefined')
    {
       wRequest.set_body("extend=true");
    }
    wRequest.set_url("/views/search/index_search_form.php");
    wRequest.set_httpVerb("POST");
    wRequest.add_completed(specialtiesRequestCompleted);
    wRequest.invoke();
    
    if(typeof(loadStatesFlag) == 'undefined')
        loadFavorite();
    
}
           
/**
* Function start search by selecte sdtate
*/
function SearchStart()
{
        state = document.getElementById('State').options[document.getElementById('State').selectedIndex].value.toLowerCase();
        if(state == '')
			document.searchForm.action = '/find-an-emdr-therapist.html';
		else
			document.searchForm.action = '/therapy-' + state + '-mental-health.html';
			
		document.searchForm.submit();
}

/**
* Function parse states string to 'option' list
*/
function specialtiesRequestCompleted(executor, eventArgs)
{
        if(executor.get_responseAvailable()) 
        {
            response = executor.get_responseData();
            if(response == '')
                return;
                
            if(typeof(newStateList) != 'undefined')
            {
                dropDownChangeState = function()
                {
                    ChangeSearchRadioButton(1,-1);
                };
            
                 dropDownState.add_change(dropDownChangeState);
                 
                 var asOptions = new Array();
                 asOptions.push("Choose...");
                 
                 // Parse states string
                 var opt = response.split(";");
                 
                 var selectedIn = 0;
                 for(i = 0; i < opt.length; i++)
                 {
                    state = opt[i].split("-");
                    
                    if(typeof(extend) != 'undefined')
                        asOptions.push("<span short='"+state[0]+"' value='"+state[1]+"'>" + state[1] + "</span>");   // in Training search user long state names
                    else
                        asOptions.push("<span value='"+state[0]+"'>" + state[1] + "</span>");
                    
                    if(typeof(st) != 'undefined')
                    {
                        // select previouse state
                        if(state[0] == st.toUpperCase())
                            selectedIn = i + 1;
                    }
                 }
                  
                 dropDownState.set_Options(asOptions);
             
                 dropDownState.set_SelectedIndex(selectedIn);
                 dropDownState.set_Disabled(false);
            }
            else
            {
                var sel = document.getElementById('State');
            
                sel.disabled = false;
                sel.innerHTML = "";
            
                // Parse states string
                var opt = response.split(";");
                
                var obj;
                
                // Create option tag for select drop-down list box
                obj = document.createElement("option");
                obj.appendChild(document.createTextNode('Choose...'));
                obj.value = "";
                sel.appendChild(obj);
                for(option in opt)
                {
                    var state = opt[option].split("-");
                    obj = document.createElement("option");
                    obj.value = state[0];
                    
                    if(typeof(st) != 'undefined')
                    {
                        // select previouse state
                        if(obj.value == st.toUpperCase())
                            obj.selected = true;
                    }
                    
                    obj.appendChild(document.createTextNode(state[1]));
                    
                    // Add state 'select' element 
                    sel.appendChild(obj);
                }
            }
            
            // Enable search button
            var divButton = document.getElementById('linkSearch');
            divButton.innerHTML = "<a href=\"javascript: SearchStart();\" title=\"Start search\">" +
                                  "<img src=\"/images/go_bt.gif\" alt=\"Start Search\" border=\"0\" align=\"top\" />" + 
                                  "<br /></a>";
        }
}