﻿// Function to highlight typed text in auto suggest results
function ClientPopulated(source, eventArgs) {
    if (source._currentPrefix != null) {
        var list = source.get_completionList();
        var search = source._currentPrefix.toLowerCase();
        for (var i = 0; i < list.childNodes.length; i++) {
            var text = list.childNodes[i].innerHTML;
            //var index = text.toLowerCase().indexOf(search);
            var index = text.toLowerCase().replace(' - ', ' ').replace('-', ' ').replace("'", " ").indexOf(search);
            if (index != -1) {
                var value = text.substring(0, index);
                value += '<span class="AutoComplete_ListItemHiliteText">';
                value += text.substr(index, search.length);
                value += '</span>';
                value += text.substring(index + search.length);
                list.childNodes[i].innerHTML = value;
            }
        }
    }
}

// Function to remove highlight span once selected
function ClientItemSelected(source, e) {
    source.get_element().value = (document.all) ? e._item.innerText : e._item.textContent;
}

function fireButton(e, btID) {
    var keynum;

    if (window.event) // IE
    {
        keynum = e.keyCode;
    }
    else if (e.which) // Netscape/Firefox/Opera
    {
        keynum = e.which;
    }

    if (keynum == 13) {
        var bt = document.getElementById(btID);
        if (bt.tagName == 'A') {
            if (bt.onclick != null) {
                bt.onclick();
            }
            else {
                window.location = bt.href;
            }
        }

        return false;
    }

}

function favorisCB(s, pID) {

    if (!cbFavoris.InCallback()) {

        favObj = s;

        imgUrl = s.tagName == 'IMG' ? s.src : s.GetImageUrl();

        if (imgUrl.match('/bookmark_off.png$')) {
            cbFavoris.SendCallback(pID + '|1');
            favState = 1;
        }
        else {
            cbFavoris.SendCallback(pID + '|0');
            favState = 0;
        }

        if (s.tagName == 'IMG') {
            s.src = '/images/ajax-loader2.gif';            
        }
        else {
            s.SetImageUrl('/images/ajax-loader2.gif');
        }
    }
}

function favorisToggleImage(s, favState) {
    if (s.tagName == 'IMG') {
        if (favState == 1) {
            s.src='/images/bookmark.png';
        }
        else {
            s.src='/images/bookmark_off.png';
        }
    }
    else {
        if (favState == 1) {
            s.SetImageUrl('/images/bookmark.png');
        }
        else {
            s.SetImageUrl('/images/bookmark_off.png');
        }
    }
}
