﻿
$j(document).ready(function() {

    $j('.inputTransactionSearch').tooltip({ showURL: false, extraClass: "GoodLink" })

    $j('#btnTransactionSearch').click(function() {
        LaunchSearch();
    });

    readDefaultVal('#txtTransactionSearch');

    $j('#searchbox').parent().css("overflow", "visible");
    $j('#searchbox').parent().css("z-index", "20");

    function findValue(li) {
        if (li == null) return alert("No Company match!");
        // if coming from an AJAX call, let's use the CompanyID as the value
        if (!!li.extra) var sValue = li.extra[0];
        // otherwise, let's just display the value in the text box
        else var sValue = li.selectValue;
        if ($j('#hdnAccount').attr('value') == 3) {
            // Show Upgrade message
            $j('#hypUpgrade').click();
            return;
        }
        else {
            DNASpinnerStart();
            window.location.href = "/segmentview.aspx?ViewModes=1&CompanyID=" + sValue;
        }
    }

    function selectItem(li) {
        findValue(li);
    }

    function moveSelect(li) {
        if (li == null) return;
        var sValue = $j(li).text();
        $j("#txtTransactionSearch").val(sValue);
    }

    function formatItem(row) {
        return row[0];
    }

    function lookupAjax() {
        var oSuggest = $j("#txtTransactionSearch")[0].autocompleter;
        oSuggest.findValue();
        return false;
    }

    $j("#txtTransactionSearch").autocomplete(
       "/Company/AutoCompleteList.aspx",
       {
           delay: 5,
           minChars: 2,
           matchSubset: 1,
           matchContains: 1,
           cacheLength: 10,
           onItemSelect: selectItem,
           onFindValue: findValue,
           formatItem: formatItem,
           autoFill: true,
           resultsClass: 'searchautocomplete',
           loadingClass: 'searchautocomplete-loading',
           onMoveSelect: moveSelect
       }
   );



});

    function SearchOnEnter(e) {
        var key;
        if (window.event)
            key = window.event.keyCode;     //IE
        else
            key = e.which;     //firefox
        if (key == 13) {
            LaunchSearch();  return false;
        }
    }

    function LaunchSearch() {

        if (document.getElementById("txtTransactionSearch").value == "search for target or buyer by name ...") return;
        if ($j('#hdnAccount').attr('value') == 3) {
            // Show Upgrade message
            $j('#hypUpgrade').click();
            return;
        }
        else {
            DNASpinnerStart();
            window.location.href = "/segmentview.aspx?runsearch=" + encodeURI(document.getElementById("txtTransactionSearch").value);
        }

    }
