// registry keys used by saOC.PutProperty() and saOC.GetProperty()

var singleProviderKey   = "SingleProvider";
var previousSearchesKey = "PreviousSearches";
var settingsVersionKey  = "SettingsVersion";
var webProviderKey      = "Panel@Web";

function load()
{
    if (submitSearch())
    {
        document.all.content.style.display = "";
        window.onfocus = shiftFocus;
        document.all.loading.style.display = "none";
    }
    document.oncontextmenu = cancelContextAndSelect;
    document.body.onselectstart = cancelContextAndSelect;
    
    try
    {
        if (saOC.ShellFeaturesEnabled) 
        {
            document.all.nt5panel.style.display = "";

            if (saOC.ShowFindPrinter)        
                nt5printPanel.style.display = "";
        }
    }
    catch (e){}    

    status = "";
    
    document.all.adspan.innerText = GetPartnerText();
}

function GetPartnerText()
{
    var adspanText = saOC.GetProperty(true, webProviderKey);
    var providerTok = String.fromCharCode(4);
    var subcatNameTok   = String.fromCharCode(3);

    if (adspanText.length == 0)
        adspanText = readProvierName("msn");
        
    if (adspanText.indexOf(providerTok) > -1)
    {
        var scat = adspanText.split(subcatNameTok);
        var prov = scat[1].split(providerTok);
        adspanText = readProvierName(prov[0]);
    }
    
    if (adspanText.indexOf(subcatNameTok) > -1)
    {
        var prov = adspanText.split(subcatNameTok);
        if (prov[1] == null)
           aspanText = srchDefault;
        else
           adspanText = readProvierName(prov[1]);
    }
    
    if (adspanText == null)
        adspanText = srchDefault;
    
    return adspanText;
}

function shiftFocus()
{
    document.all.SearchText.focus();
}

function submitSearch()
{
    if (location.search.indexOf("SearchText=") > 0)
    {
        var srch = GetPartnerText();
        if (srch == null || srch == "" || srch.length == 0)
            setDefault();
            
        var srchstr = buildUrl(srch);
        srchstr = srchstr.substr(0,srchstr.indexOf("**SearchText**"));
        srchstr += location.search.substr(12);
        location.replace(srchstr);
    }
    else
        return true;    
} 

function setDefault()
{
    saOC.PutProperty(true, webProviderKey, srchDefault);
}   

function otherSearch(type)
{        
    switch (type) {
        case 0: // Files or Folders
            saOC.FindFilesOrFolders();
            break;

        case 1: // Computers
            if (saOC.Version >= 2)
                saOC.FindComputer();                
            break;
            
        case 2: // Printers
            saOC.FindPrinter();
            break;
            
        case 3: // People
            saOC.FindPeople();
            break;
    }
}


function loadSettings()
{
    // map events
    //document.body.onkeypress = handleKeyPress;
    document.all.divLoading.style.display = "";
    
    // determine if using search assistant or single provider
    var selectedSearchTool = saOC.SearchAssistantDefault ? 0 : 1;
    
    // determine if browser has search companion
    // if search companion is available, make that option available and check if it's used
    var ua = navigator.userAgent;
    
    if ((self.location.toString().toLowerCase().indexOf("en-us") > -1) && (ua.indexOf("Windows NT 5") > -1) && (ua.indexOf("Windows NT 5.0") == -1))
    {
        document.all.radioSC.style.display = "";

        if (saOC.UseSearchCompanion)
        selectedSearchTool = 2;
    }
    document.all.searchTool[selectedSearchTool].click();
    document.all.searchTool[selectedSearchTool].focus();
    
    document.all.providers.style.display = "";
    document.all.divLoading.style.display = "none";
}

function showChoice(choice)
{    
    // make search assistant options div visible and hides all other divs
    if (choice == 0) {
        document.all.divSearchAssistant.style.display = "";
        document.all.divSingleProvider.style.display = "none";        
        document.all.divSearchCompanion.style.display = "none";
        document.all.divSearchAssistantAutoSearch.style.display = "";
    }
    
    // make single provider options div visible and hides all other divs
    else if (choice == 1) {        
        document.all.divSearchAssistant.style.display = "none";
        document.all.divSingleProvider.style.display = "";
        document.all.divSearchCompanion.style.display = "none";
        document.all.divSearchAssistantAutoSearch.style.display = "";
    }
    
    // make search companion options div visible and hides all other divs
    else {
        document.all.divSearchAssistant.style.display = "none";
        document.all.divSingleProvider.style.display = "none";        
        document.all.divSearchCompanion.style.display = "";
        document.all.divSearchAssistantAutoSearch.style.display = "";
    }
    readDefaultSettings();
}

function update()
{    
    var searchURL = "";     // by default, if single provider is not chosen, the url is blank
    var usingSC = false;    // assume we are not using search companion at this point

    // search assistant options div is selected            
    if (document.all.searchTool[0].checked) 
    {
        if(webSearchChoice.selectedIndex != -1)
            saOC.PutProperty(true, webProviderKey, webSearchChoice[webSearchChoice.selectedIndex].value);
        else
            saOC.PutProperty(true, webProviderKey, webSearchChoice[0].value);
    }
    // single provider options div is selected
    else if (document.all.searchTool[1].checked) 
    {
        
        // save provider's name and url
        saOC.PutProperty(true, singleProviderKey, singleSearchChoice.value);
        if(singleSearchChoice.selectedIndex != -1){
          searchURL = singleSearchChoice[singleSearchChoice.selectedIndex].url;    
        }else{
            searchURL = singleSearchChoice[0].url;
        }
    }
    
    // search companion options div is selected
    else        
        usingSC = true;
        
    // persist the search companion setting
    try 
    {
        var hasSC = saOC.SearchCompanionAvailable;
        saOC.UseSearchCompanion = usingSC;
    }
    catch (e) {}       

    saOC.ASProvider = autoSearchChoice.value;


    saOC.SetDefaultSearchUrl(searchURL);    // save single provider url (blank if not using this option)
    saOC.NotifySearchSettingsChanged();     // causes main search pane to refresh
    window.close();
}

function readDefaultSettings()
{
    if (document.all.searchTool[0].checked)
    {
        var value = saOC.GetProperty(true, webProviderKey);
        if (value != "")
            webSearchChoice.value = value;
    }
    else if (document.all.searchTool[1].checked)
    {
        var value = saOC.GetProperty(true, singleProviderKey);
        if (value != "")
            singleSearchChoice.value = value;
    }

    var regProvider = saOC.ASProvider;

    if (regProvider == "" || regProvider == null || regProvider == "MSN")
        regProvider = "msn";

    autoSearchChoice.value = regProvider;
}

function setDefaults()
{
    readDefaultSettings();
}

function cancelContextAndSelect()
{
    if (window.event.srcElement.tagName != "INPUT")
        event.returnValue = false;
}
