﻿//
// Dynamic display of search results
//
DScript.DScovery.Search.ResultsTemplateContainer.implement
({  

    // Override Render function
    onRender : function(control,response)
    {
        //if (response == null) return;
        //if ( window.MessageBox != null ) {
        //    window.MessageBox.control.Visible();
        //}
        
        // Search has completed and the default datasource has more than 0 hits
        console.log(response);
        src = DScript.DScovery.DataGroups.context[0].datasources[0];
        console.log(src);
        //if (response.issearching == 0 && src.hitcount > 0)
        if (src.hitcount > 0)
        {
            console.log("Posting to overview");
            control.style.display = "none";
            
            if (!theForm.onsubmit || (theForm.onsubmit() != false)) 
            {
                DScript.DScovery.DScoveryManager.saveState(); // Save our search state;
                theForm.action = './Overview.aspx?src=' + src.fullname;
                theForm.submit();
            }       
        }
    }
});

function CalmViewOnFinishSearch(sender)
{
    hits = 0;
    
    if ( sender.datasources != null ) {    
        // Object is data groups. 
        sender.datasources.each( function(datagrp, index)
        {
            datagrp.datasources.each( function(src, index)
            {
                if (src.ischecked == true && hits >= 0)
                {
                    if (src.hitcount > 0)
                    {
                        hits += src.hitcount;
                    }
                    else if (src.hitcount < 0)
                    {
                        if ( window.MessageBox != null ) {
                            window.MessageBox.write(src.htmlstring);                        
                        }
                        hits = -1;
                    }
                }
            });
        });
    }    
    
    if (hits == 0) {
        if ( window.MessageBox != null ) {
            window.MessageBox.write("No Search Results found.");
        }
    }
    return hits;
}


DScript.DScovery.Search.DScoverySearch.implement
({
    onFinishSearch : function (sender)
    {
        CalmViewOnFinishSearch(sender);
    }
});

DScript.DScovery.Search.SearchTemplateContainer.implement
({  
    setAsDefault : function()
    {
        DSCoverySearch1.SearchTemplateContainer.SearchText.HTMLControl().addEvent('keydown', function(event){
            if (event.keyCode == 13)
            {
                DSCoverySearch1.ButtonTemplateContainer.StartSearch.HTMLControl().focus();
            }
        });
    }
});

// On Page Load

function Page_Load()
{
        DsCoveryManager1.visible = false;        
        try
        {            
            var targetctrl = $$('.searchmessage');
            if (targetctrl != null)
            {
                var msgBox = new DScript.HTML.HTMLControls.Control();
                msgBox.control = targetctrl;                
                window.MessageBox = new DScript.DScovery.Message.MessageBox(msgBox);
            }
        }
        catch(err)
        {            
            return;
        }
}

addLoadEvent(Page_Load);
