﻿var ignorekeys = ',32,37,38,39,40,16,18,17,20,13,9,27,'
var mainsearhtimer;
var highlightindex = -1;
var totalindex = 0;
function DisableSubmit(e) {
    if (getKey(e) == 13) {
        return false;
    }
}
function searchboxonfocus() {
    jQuery(thesearchbox).focus();
}
function DoSearchExpert(e) {

    var thekey = getKey(e)

    if (thekey == 27) { //esc key
        clearsearch();
    }
    if (thekey == 40){
        movelightrow(1);
    }
    if (thekey == 38) {
        movelightrow(-1);
    }
    if (thekey == 39) {
        showExpertDetails();
    }
    if (thekey == 37) {
        HideExpertDetails();
    }
    if (thekey == 13 ) { //enter key
        dofullsearch();
    }
    if (ignorekeys.indexOf("," + getKey(e) + ",", 0) > 0) {
        return false;
    }
    timersearch();
}
function DoFullSearchExpert(e) {
    var thekey = getKey(e)
    if (thekey == 13 || thekey == 0) { //enter key
        MyFullSearchKey = jQuery(thefullsearchbox).val();
        GoToPage(1);
    }
}
function DoFullSearchExpertNow() {
    MyFullSearchKey = jQuery(thefullsearchbox).val();
    GoToPage(1);
}
function DoFullSearchExpertFromDiv() {
   
    
        MyFullSearchKey = jQuery(thefullsearchbox).val();
        GoToPage(1);
    
}
function dofullsearch() {
    window.location = FullSearchUrl + "&fullexpertsearchkey=" + firedsearchkey
}
function GoToPage(PageNum) {
    window.location = baseurl + "&fullexpertsearchkey=" + MyFullSearchKey + "&page=" + PageNum
}
function timersearch() {
    var searchkey = escape(jQuery(thesearchbox).val())
    if (searchkey != firedsearchkey) {
        window.clearTimeout(searchtimer);
        firedsearchkey = searchkey
        if (searchkey.length > 0) {
            thetempresultbox.html("")
            //theresultbox.html("")
            ShowHideSearchResult(1); // theresultbox.css({ 'visibility': 'hidden' });
            searchtimer = window.setTimeout("ajaxSearch('" + searchkey + "')", 500);
        }
        else {
            clearsearch()
        }
    }
}
function ajaxSearch(searchkey) {

    if (searchkey == firedsearchkey) {
        loadingsearchResultdiv.html(Loading);
        thetempresultbox.load(baseurl + "&expertsearchkey=" + searchkey + " #ExpertSearchResult");
        window.setTimeout("ajustsearchbox()", 100);
    }
   
}
function ajustsearchbox() {
    if (thetempresultbox.html() == "") {
        window.setTimeout("ajustsearchbox()", 100);
    }
    else {
        if (thetempresultbox.html() != "done") {
            theresultbox.html(thetempresultbox.html())
            thetempresultbox.html("done")
            theresultbox.css({ 'visibility': 'visible' });
            loadingsearchResultdiv.html(close);
            searchtimer = 0;
            wrapsearchresult();
        }
        var boxheight = theresultbox.innerHeight()
        theresultbox.css({ "margin-top": -1 * boxheight })
        
    }
}
function wrapsearchresult() {
    var therows = jQuery(".QS_Row")
    therows.each(function(index) {
        if (index >= totalindex) {
            totalindex = index
        }
        var theobject = jQuery(this);
        theobject.hover(
        function() { highlightindex = index; highlightrow(highlightindex); },
        function() { }
        )
    }
    )
    var themoreinfobuttons = jQuery(".QS_element_MoreInfo")
    themoreinfobuttons.click(function() {
        showExpertDetails();
        searchboxonfocus();
    });
    highlightindex = -1;
}
function movelightrow(direction) {
    highlightindex = highlightindex + direction;
    if (highlightindex > totalindex){
        highlightindex = 0;
    }
    if(highlightindex < 0){
        highlightindex = totalindex;
    }
    highlightrow(highlightindex);
}
function highlightrow(highlightindex) {
    if (highlightindex > -1) {
        var therows = jQuery(".QS_Row")
        therows.each(function(index) {
            var theobject = jQuery(this);
            if (index == highlightindex) {
                theobject.addClass("QS_Row_Highlight");
            }
            else {
                theobject.removeClass("QS_Row_Highlight");
            }
        }
        );
    }
}
function showExpertDetails() {
    if (highlightindex > -1) {
        HideExpertDetails();
        var thebox = jQuery(".QS_Row_Highlight .QS_ExpertDetails");
        if (thebox) {
            thebox.show();
        }
    }
    ajustsearchbox();
}
function HideExpertDetails() {
    var thebox = jQuery(".QS_ExpertDetails")
    if (thebox) {
        thebox.hide();
    }
    ajustsearchbox();
}
function getKey(key) {
    if (key == null) {
        keycode = event.keyCode;
        // To Mozilla
    } else {
        keycode = key.keyCode;
    }
    // Return the key in lower case form
    //return String.fromCharCode(keycode).toLowerCase();
    return keycode
}
function hidesearchresult() {
    window.clearTimeout(searchtimer);
    searchtimer = window.setTimeout("ShowHideSearchResult(0)", 500) 
}
function showsearchresult() {
    var searchkey = jQuery(thesearchbox).val();
    if (searchkey == firedsearchkey && theresultbox.html().length > 5) {
        window.clearTimeout(searchtimer);
        searchtimer = window.setTimeout("ShowHideSearchResult(1)", 500) 
    }
}
function ShowHideSearchResult(visible) {
    if (visible == 0) {
        theresultbox.filter(":visible").hide();//css({ 'visibility': 'hidden' });
    }
    else {
        theresultbox.filter(":hidden").show();//css({ 'visibility': 'visible' });
    }
}

function clearsearch() {
    jQuery(thesearchbox).attr("value", "");
    loadingsearchResultdiv.html(loadingdefault);
    hidesearchresult();
}
function popupemail(manageremail) {
    showdialog('', '/EmailBox.aspx', 700, 90, 'createContainerMLC')
}