﻿var timer = null;
//var incrementalSearchCharacterCount = <%=IncrementalSearchCharacterCount%>;
var lastSearchText = null;

// This function needs improving to exclude alpha/numeric characters i.e. it'll still
// trigger a search if the user moves the cursor within the text box via the cursor
// keys:-
function SearchTextKeyUp(keyCode, postbackStr)
{
    //alert(keyCode);
    // if the keyCode is -10000, this is directly from the search button clicked event
    // so we want to run the search with no search delay.
    if (keyCode == -10000)
    {
        postbackStr();
        //<%=searchPostbackStr%>;
        return;
    }

    //    // otherwise, this is an incremental search, so to do it with a typing delay
    //    var searchDelay = 500;
    //    try
    //    {
    //        searchDelay = parseInt(<%=searchDelay%>);
    //    }
    //    catch (err)
    //    {
    //        searchDelay = 500;
    //    }

    // Check we've got an event to use:-
    if (keyCode)
    {
        var searchText = $get(txtSearchDetails).value;
        searchText = searchText.replace(" ", "");

        if (lastSearchText == searchText)
        {
            // search text not changed
            return;
        }

        lastSearchText = searchText;

        if ($get(ddlSearchMode).value == 0 && $get(radCompanyName).checked == true)
        {
            if (searchText.length < incrementalSearchCharacterCount)
            {
                //Clear search
                //set message to users
                if (searchText.length > 0)
                {
                    ShowErrorText();
                }
                else
                {
                    HideErrorText();
                }
                return false;
            }
        }

        if ($get(ddlSearchMode).value == 0
		&& $get(radRegistrationNumber).checked == false
		&& $get(radPostCode).checked == false)
        {
            // Clear any currently running timer:-
            clearTimeout(timer);

            // If the timer expires, i.e., the user has stopped typing then ...
            timer = window.setTimeout(function()
            {
                // Perform a full post-back targetting the search button's on click event:-
                postbackStr();
                //<%=searchPostbackStr%>
            }, searchDelay);
        }
    }
}

function SearchTextKeyPress(keyCode, postbackStr)
{
    //alert(keyCode);

    // Check we've got an event to use:-
    if (keyCode)
    {
        // Check if the enter key has been pressed and instigate a search, ONLY if the
        // incremental search is not checked OR we're searching on registration number:-
        if (keyCode == 13 && ($get(ddlSearchMode).value == 1 || $get(radRegistrationNumber).checked == true))
        {
            // Perform a full post-back targetting the search buttons on click event:-
            postbackStr();
            //<%=searchPostbackStr%>
        }

        // Return false here to prevent a second full post-back from occurring:-
        return false;
    }
}

function ToggleIncrementalSearchOptions(visible)
{
    if (!visible)
    {
        // We need to hide the incremental search options. Hiding the labels is easy ...:-
        $get(lblInclude).style.visibility = 'hidden';
        $get(lblSearchMode).style.visibility = 'hidden';
        $get(ddlSearchMode).style.visibility = 'hidden';

        // ... the asp:CheckBox controls are a little more tricky as they're rendered in a containing
        // <span> tag I think:-
        $get(chkDissolved).parentNode.style.visibility = 'hidden';
        $get(imgAdverseInfo).style.visibility = 'hidden';
        $get(chkPreviousCompanyNames).parentNode.style.visibility = 'hidden';
        $get(imgIdentity).style.visibility = 'hidden';

        HideErrorText();
    }
    else
    {
        $get(lblInclude).style.visibility = 'visible';
        $get(lblSearchMode).style.visibility = 'visible';
        $get(ddlSearchMode).style.visibility = 'visible';
        $get(chkDissolved).parentNode.style.visibility = 'visible';
        $get(chkPreviousCompanyNames).parentNode.style.visibility = 'visible';

        if ($get(chkPreviousCompanyNames).checked == true)
        {
            $get(imgIdentity).style.visibility = 'visible';
        }

        if ($get(chkDissolved).checked == true)
        {
            $get(imgAdverseInfo).style.visibility = 'visible';
        }

        HideErrorText();
    }
}

function ToggleDissolvedIndicator()
{
    if ($get(chkDissolved).checked == true)
    {
        $get(imgAdverseInfo).style.visibility = 'visible';
    }
    else
    {
        $get(imgAdverseInfo).style.visibility = 'hidden';
    }
}

function TogglePreviousNamesIndicator()
{
    if ($get(chkPreviousCompanyNames).checked == true)
    {
        $get(imgIdentity).style.visibility = 'visible';
    }
    else
    {
        $get(imgIdentity).style.visibility = 'hidden';
    }
}

function ToggleDissolvedSearchOptions()
{
    // We need to hide the incremental search options. Hiding the labels is easy ...:-
    document.getElementById(lblInclude).style.visibility = 'visible';
    document.getElementById(lblSearchMode).style.visibility = 'hidden';
    document.getElementById(ddlSearchMode).style.visibility = 'hidden';

    document.getElementById(chkPreviousCompanyNames).parentNode.style.visibility = 'hidden';
    $get(imgIdentity).style.visibility = 'hidden';

    // ... the asp:CheckBox controls are a little more tricky as they're rendered in a containing
    // <span> tag I think:-
    document.getElementById(chkDissolved).parentNode.style.visibility = 'visible';

    if ($get(chkDissolved).checked == true)
    {
        $get(imgAdverseInfo).style.visibility = 'visible';
    }

    HideErrorText();
}


function HideErrorText()
{
    if (document.getElementById(divErrorTextContainer) != null)
    {
        document.getElementById(divErrorTextContainer).style.visibility = 'hidden';
        document.getElementById(divErrorTextContainer).style.display = 'none';
    }
}

function ShowErrorText()
{
    document.getElementById(companySearchResults).style.visibility = 'hidden';

    if (document.getElementById(divErrorTextContainer) != null)
    {
        document.getElementById(divErrorTextContainer).attributes["class"].value = "divCompanySearchIncrementalTextContainer";

        document.getElementById(divErrorTextContainer).style.visibility = 'visible';
        document.getElementById(divErrorTextContainer).style.display = 'inline';
        document.getElementById(lblErrorText).innerHTML = "Please enter at least " + incrementalSearchCharacterCount + " characters for incremental search.";
    }
}

function ResetSearchText()
{
    $get(txtSearchDetails).value = '';
}