
function ajax_javascript_detect()
{
   
    $.ajax({
                type: "POST",
                url: "/SiteEvents.asmx/AjaxJavascriptDetect",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json"
                
            });

}

AutoSuggestControl = function(oTextbox, oProvider)
{
    this.cur = -1;
    this.layer = null;
    this.messageLayer = null;
    this.provider = oProvider;
    this.textbox = oTextbox;
    this.last = '';
    this.lastSelectedNode = null;
    this.navigateDone = false;
    this.titleText = $(oTextbox).attr('title');
    if (this.textbox.value == '' || this.textbox.value == this.titleText)
    {
        $(oTextbox).val(this.titleText).addClass('blur');
    }
    this.init();
};

AutoSuggestControl.prototype =
{
    init: function()
    {
        var oThis = this;

        this.textbox.onkeyup = function(oEvent)
        {
            if (!oEvent)
            {
                oEvent = window.event;
            }
            oThis.handleKeyUp(oEvent);
        };

        this.textbox.onkeydown = function(oEvent)
        {
            if (!oEvent)
            {
                oEvent = window.event;
            }
            return oThis.handleKeyDown(oEvent);
        };

        this.textbox.onblur = function()
        {
            oThis.closeDropdown();
        };

        this.textbox.onfocus = function()
        {
            oThis.navigateDone = false;
            $(oThis.messageLayer).hide();
            $(oThis.textbox).removeClass('blur');
            if (oThis.textbox.value == oThis.titleText)
            {
                $(oThis.textbox).val('');
                oThis.last = '';
            }
            else
            {
                oThis.last = oThis.textbox.value;
                oThis.selectRange(0, oThis.textbox.value.length);
            }

        };

        this.createDropDown();
        this.createMessage();
    },

    selectRange: function(iStart, iLength)
    {
        if (this.textbox.createTextRange)
        {
            var oRange = this.textbox.createTextRange();
            oRange.moveStart("character", iStart);
            oRange.moveEnd("character", iLength - this.textbox.value.length);
            oRange.select();
        }
        else if (this.textbox.setSelectionRange)
        {
            this.textbox.setSelectionRange(iStart, iLength);
        }
        this.textbox.focus();
    },

    typeAhead: function(sSuggestion)
    {
        if (this.textbox.createTextRange || this.textbox.setSelectionRange)
        {
            var iLen = this.textbox.value.length;
            this.textbox.value = this.textbox.value + sSuggestion.substr(iLen, sSuggestion.length - iLen);
            this.selectRange(iLen, sSuggestion.length);
        }
    },

    autosuggest: function(aSuggestions, bTypeAhead)
    {
        if (aSuggestions.length > 0 && this.textbox.value.length > 0)
        {
            if (bTypeAhead)
            {
                this.typeAhead(aSuggestions[0][0]);
            }
            this.showSuggestions(aSuggestions, this.textbox.value);
        }
        else
        {
            this.hideSuggestions();
        }
    },

    handleKeyUp: function(oEvent)
    {
        var iKeyCode = oEvent.keyCode;
        if (iKeyCode == 8 || iKeyCode == 46)
        {
            this.provider.requestSuggestions(this, false);
        }
        else if (iKeyCode < 32 || (iKeyCode >= 33 && iKeyCode <= 46) || (iKeyCode >= 112 && iKeyCode <= 123))
        {
            //ignore
        }
        else
        {
            this.provider.requestSuggestions(this, false);
        }
    },

    handleKeyDown: function(oEvent)
    {
        switch (oEvent.keyCode)
        {
            case 38: //up arrow
                this.previousSuggestion();
                return true;
            case 40: //down arrow 
                this.nextSuggestion();
                return true;
            case 9: //tab
                this.navigateSuggestion(true);
                return false;
            case 13: //enter
                this.navigateSuggestion(true);
                this.navigateDone = true;
                return false;
            case 27: //escape
                this.hideSuggestions();
                this.navigateDone = true;
                $(this.textbox).val(this.last).blur();
                return false;
        }
    },

    closeDropdown: function()
    {
        this.hideSuggestions();
        if (this.textbox.value === '')
        {
            $(this.textbox).val(this.titleText).addClass('blur');
        }
        else if (this.navigateDone = false)
        {
            this.navigateSuggestion(false);
        }
        this.navigateDone = false;
    },

    createDropDown: function()
    {
        this.layer = document.createElement("div");
        this.layer.className = "suggestions";
        this.layer.style.visibility = "hidden";
        this.layer.style.zIndex = 100;
        document.body.appendChild(this.layer);
        var oThis = this;
        this.layer.onmousedown = this.layer.onmouseup =
        this.layer.onmouseover = function(oEvent)
        {
            oEvent = oEvent || window.event;
            oTarget = oEvent.target || oEvent.srcElement;
            if (oEvent.layerY)
            {
                iLayerYPos = oEvent.layerY; // || oEvent.offsetY;
                iLayerYPos = Math.max(iLayerYPos - 1, 0);
            }
            else
            {
                iLayerYPos = oEvent.clientY + document.documentElement.scrollTop - oThis.layer.offsetTop;
                if (navigator.appName == 'Microsoft Internet Explorer')
                {
                    var ua = navigator.userAgent;
                    var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
                    if (re.exec(ua) != null)
                    {
                        rv = parseFloat(RegExp.$1);
                    }
                    if (rv >= 8)
                    {
                        iLayerYPos = Math.max(iLayerYPos - 1, 0);
                    }
                    else
                    {
                        iLayerYPos = Math.max(iLayerYPos - 3, 0);
                    }
                }
                else
                {
                    iLayerYPos = Math.max(iLayerYPos - 1, 0);
                }

            }

            iIndex = Math.min(Math.floor(iLayerYPos / oThis.layer.childNodes[0].clientHeight), oThis.layer.childNodes.length - 1);
            //document.title = iLayerYPos + ": " + iIndex;
            oTarget = oThis.layer.childNodes[iIndex];

            if (oEvent.type == "mousedown")
            {
                if (!oTarget._name)
                {
                    oThis.closeDropdown();
                }
                else
                {
                    oThis.selectSuggestion(oTarget._name);
                    oThis.navigateSuggestion(true);
                }
            }
            else if (oEvent.type == "mouseover")
            {
                oThis.cur = oTarget._index;
                oThis.highlightSuggestion(oTarget);
            }
            else
            {
                oThis.textbox.focus();
            }
        };
    },

    createMessage: function()
    {
        this.messageLayer = document.createElement("div");
        this.messageLayer.className = "suggestions_message";

        $(this.messageLayer).hide();
        this.messageLayer.style.zIndex = 150;
        var oThis = this;
        this.messageLayer.onmousedown = function(oEvent)
        {
            oEvent = oEvent || window.event;
            $(oThis.messageLayer).hide();
        }

        document.body.appendChild(this.messageLayer);

    },

    getLeft: function()
    {
        var oNode = this.textbox;
        var iLeft = 0;
        while (oNode.tagName != "BODY")
        {
            iLeft += oNode.offsetLeft;
            oNode = oNode.offsetParent;
        }
        return iLeft;
    },

    getTop: function()
    {
        var oNode = this.textbox;
        var iTop = 0;
        while (oNode.tagName != "BODY")
        {
            iTop += oNode.offsetTop;
            oNode = oNode.offsetParent;
        }
        return iTop;
    },

    highlightSuggestion: function(oSuggestionNode)
    {
        for (var i = 0; i < this.layer.childNodes.length; i++)
        {
            var oNode = this.layer.childNodes[i];
            if (oNode == oSuggestionNode)
            {
                oNode.className = "current"
                this.lastSelectedNode = oSuggestionNode;
            }
            else if (oNode.className == "current")
            {
                oNode.className = "";
            }
        }
    },

    showSuggestions: function(aSuggestions, keyword)
    {
        this.lastSelectedNode = null;
        var oDiv = null;
        this.layer.innerHTML = "";
        for (var i = 0; i < aSuggestions.length; i++)
        {
            oDiv = document.createElement("div");
            $(oDiv).html(this.markKeyword(aSuggestions[i][0], keyword));
            oDiv._data = aSuggestions[i][1];
            oDiv._index = i;
            oDiv._name = this.provider.getName(aSuggestions[i][0]);
            this.layer.appendChild(oDiv);
        }
        this.layer.style.left = this.getLeft() + "px";
        this.layer.style.top = (this.getTop() + this.textbox.offsetHeight) + "px";
        this.layer.style.visibility = "visible";
        this.firstSuggestion();
    },

    markKeyword: function(textToMark, keyword)
    {
        var mCurrentKeyWords = this.provider.getKeywords(textToMark, " ");
        var iStartOffset = 0;
        var sNewText = "";
        var bFoundAWord = false;
        mSearchWords = keyword.toLowerCase().split(" ");
        for (var KeyWordIndex = 0; KeyWordIndex < mCurrentKeyWords.length; KeyWordIndex++)
        {
            if (mCurrentKeyWords[KeyWordIndex].length > 0)
            {
                bFoundAWord = false;
                for (var SearchWordIndex = 0; SearchWordIndex < mSearchWords.length; SearchWordIndex++)
                {
                    keyword = mSearchWords[SearchWordIndex];
                    if (keyword.length > 0 && mCurrentKeyWords[KeyWordIndex].toLowerCase().indexOf(keyword) == 0)
                    {
                        sNewText += "<strong>" + textToMark.substr(iStartOffset, keyword.length) + "</strong>" + textToMark.substr(iStartOffset + keyword.length, mCurrentKeyWords[KeyWordIndex].length - keyword.length);
                        iStartOffset += mCurrentKeyWords[KeyWordIndex].length;
                        bFoundAWord = true;
                    }
                }
                if (!bFoundAWord)
                {
                    sNewText += textToMark.substr(iStartOffset, mCurrentKeyWords[KeyWordIndex].length);
                    iStartOffset += mCurrentKeyWords[KeyWordIndex].length;
                }
            }
            sNewText += textToMark.substr(iStartOffset, 1);
            iStartOffset += 1;
        }
        return sNewText;
    },

    firstSuggestion: function()
    {
        this.cur = 0;
        var cSuggestionNodes = this.layer.childNodes;
        if (cSuggestionNodes.length > 0 && this.cur < cSuggestionNodes.length)
        {
            var oNode = cSuggestionNodes[this.cur];
            this.highlightSuggestion(oNode);

        }

    },

    nextSuggestion: function()
    {
        var cSuggestionNodes = this.layer.childNodes;
        if (cSuggestionNodes.length > 0 && this.cur < cSuggestionNodes.length - 1)
        {
            var oNode = cSuggestionNodes[++this.cur];
            this.highlightSuggestion(oNode);
            this.selectSuggestion(oNode._name);
        }
    },

    previousSuggestion: function()
    {
        var cSuggestionNodes = this.layer.childNodes;

        if (cSuggestionNodes.length > 0 && this.cur > 0)
        {
            var oNode = cSuggestionNodes[--this.cur];
            this.highlightSuggestion(oNode);
            this.selectSuggestion(oNode._name);
        }
    },

    selectSuggestion: function(sSelectedText)
    {
        this.textbox.value = sSelectedText;
        this.selectRange(0, this.textbox.value.length);
    },

    showMessage: function(sMessage)
    {
        var oDiv = null;
        this.messageLayer.innerHTML = "";
        oDiv = document.createElement("div");
        $(oDiv).html('<p>' + sMessage + '</p>');
        this.messageLayer.appendChild(oDiv);

        this.messageLayer.style.left = this.getLeft() + "px";
        this.messageLayer.style.top = (this.getTop() + this.textbox.offsetHeight - 50) + "px";
        //this.messageLayer.style.visibility = "visible";
        $(this.messageLayer).fadeIn(500).animate({ opacity: 1.0 }, 1000).fadeOut(2000);
    },

    navigateSuggestion: function(doAction)
    {
        var cSuggestionNodes = this.layer.childNodes;
        this.layer.style.visibility = "hidden";
        if (cSuggestionNodes.length > 0 && this.cur >= 0)
        {
            var oNode = cSuggestionNodes[this.cur];
            return this.doNavigateSuggestion(oNode, doAction);
        }
        else if (cSuggestionNodes.length > 0 && this.cur == -1)
        {
            var oNode = cSuggestionNodes[0];
            return this.doNavigateSuggestion(oNode, doAction);
        }
        else
        {
            var _data = this.provider.isSuggestion(this.textbox.value);
            if (_data >= 0)
            {
                this.cur = -1;
                if (doAction)
                {
                    return this.provider.doAction(_data);
                }
                else
                {
                    return false;
                }
            }
            else if (this.textbox.value != this.last)
            {
                this.cur = -1;
                if (!this.provider.allowUnlisted)
                {
                    if (this.titleText == this.textbox.value || this.textbox.value == "")
                    {
                        this.showMessage('Please type a category!');
                    }
                    else
                    {
                        this.showMessage('Cannot find: ' + this.textbox.value);
                    }
                    $(this.textbox).val(this.titleText).addClass('blur');
                }
                else
                {
                    if (doAction)
                    {
                        this.provider.doAction();
                        this.showMessage('New catergory: ' + this.textbox.value);
                    }
                }
                return false;
            }
        }
    },

    navigateLastSuggestion: function(doAction)
    {
        if (this.lastSelectedNode != null)
        {
            return this.doNavigateSuggestion(this.lastSelectedNode, doAction);
        }
        else
        {
            if (this.titleText == this.textbox.value || this.textbox.value == "")
            {
                this.showMessage('Please type a category!');
            }
            else
            {
                this.showMessage('Cannot find: ' + this.textbox.value);
            }
            $(this.textbox).val(this.titleText).addClass('blur');
        }
    },

    doNavigateSuggestion: function(oNode, doAction)
    {
        this.cur = -1;
        this.selectSuggestion(oNode._name);
        if (doAction)
        {
            return this.provider.doAction(oNode._data);
        }
        else
        {
            return false;
        }
    },

    hideSuggestions: function()
    {
        this.cur = -1;
        this.layer.style.visibility = "hidden";
        this.layer.innerHTML = "";
    }

};

function SuggestionProvider() 
{
    //any initializations needed go here
};

SuggestionProvider.prototype.requestSuggestions = function (oAutoSuggestControl) 
{
    var aSuggestions = new Array();
    oAutoSuggestControl.autosuggest(aSuggestions);
};

// ********** CategorySelector ***********************

CatSelectorSuggestions = function(sNavigateURL)
{
    this.allowUnlisted = false;
    this.navigateURL = sNavigateURL;
    this.init();
};

CatSelectorSuggestions.prototype =
{
    init: function()
    {
        var oThis = this;
        Sys.Net.WebServiceProxy.invoke(
            "/Controls/Common/CategorySelectorService.asmx",
            "GetCategoriesSuggestions",
            false,
            {},
            Function.createDelegate(this, this.getCategoriesSuggestionsComplete),
            null);
    },

    getCategoriesSuggestionsComplete: function(result)
    {
        this.keywords = result;
    },


    getKeywords: function(keywordText, replaceString)
    {
        var mCurrentKeyWords = [];
        var sNameKeyWords = "";
        sNameKeyWords = keywordText.replace(/[,]+/, replaceString);
        sNameKeyWords = sNameKeyWords.replace(/[(]+/, replaceString);
        sNameKeyWords = sNameKeyWords.replace(/[)]+/, replaceString);
        mCurrentKeyWords = sNameKeyWords.split(" ");
        return mCurrentKeyWords;
    },

    getName: function(sCatKeywords)
    {
        var iKeyStart = sCatKeywords.indexOf("(");
        if (iKeyStart > 0)
        {
            return sCatKeywords.substr(0, iKeyStart - 1);
        }
        else
        {
            return sCatKeywords;
        }
    },

    doAction: function(sCatID)
    {
        if (!sCatID)
        {
            return false;
        }
        else
        {
            location.href = this.navigateURL + sCatID;
            return true;
        }
    },

    isSuggestion: function(sTextboxValue)
    {
        var sTextboxValue = sTextboxValue.toLowerCase();
        if (sTextboxValue.length > 0)
        {
            if (!this.keywords)
            {
                return -1;
            }
            else
            {
                var mCurrentSearchWords = sTextboxValue.split(" ");
                var mCurrentKeyWords = [];
                var mFoundAllWords = true;
                var mFoundAWord = true;
                for (var i = 0; i < this.keywords.length; i++)
                {
                    mFoundAllWords = true;
                    mCurrentKeyWords = this.getKeywords(this.keywords[i][0].toLowerCase(), "");
                    for (var currentSearchWord = 0; currentSearchWord < mCurrentSearchWords.length; currentSearchWord++)
                    {
                        mFoundAWord = false;
                        for (var KeyWordIndex = 0; KeyWordIndex < mCurrentKeyWords.length; KeyWordIndex++)
                        {
                            if (mCurrentKeyWords[KeyWordIndex].indexOf(mCurrentSearchWords[currentSearchWord]) == 0)
                            {
                                mFoundAWord = true;
                                break;
                            }
                        }
                        if (!mFoundAWord)
                        {
                            mFoundAllWords = false;
                            break;
                        }
                    }
                    if (mFoundAllWords)
                    {
                        return this.keywords[i][1];
                    }
                }
                return -1;
            }
        }
        else
        {
            return -1;
        }
    },

    requestSuggestions: function(oAutoSuggestControl, bTypeAhead)
    {
        var aSuggestions = [];
        var mSecondSuggestions = [];
        var sTextboxValue = oAutoSuggestControl.textbox.value.toLowerCase();

        if (sTextboxValue.length > 0)
        {
            if (!this.keywords)
            {
            }
            else
            {
                var mCurrentSearchWords = sTextboxValue.split(" ");
                var mCurrentKeyWords = [];
                var mFoundAllWords = true;
                var mFoundAWord = true;
                var sCurrentKeywordSet = "";
                for (var i = 0; i < this.keywords.length; i++)
                {
                    mFoundAllWords = true;
                    bAllTitleWords = true;
                    sCurrentKeywordSet = this.keywords[i][0].toLowerCase();
                    mCurrentKeyWords = this.getKeywords(sCurrentKeywordSet, "");
                    KeyWordStart = sCurrentKeywordSet.indexOf("(");
                    for (var currentSearchWord = 0; currentSearchWord < mCurrentSearchWords.length; currentSearchWord++)
                    {
                        mFoundAWord = false;
                        for (var KeyWordIndex = 0; KeyWordIndex < mCurrentKeyWords.length; KeyWordIndex++)
                        {
                            if (mCurrentKeyWords[KeyWordIndex].indexOf(mCurrentSearchWords[currentSearchWord]) == 0)
                            {
                                if (sCurrentKeywordSet.indexOf(mCurrentKeyWords[KeyWordIndex]) > KeyWordStart)
                                {
                                    bAllTitleWords = false;
                                }

                                mFoundAWord = true;
                                break;
                            }
                        }
                        if (!mFoundAWord)
                        {
                            mFoundAllWords = false;
                            break;
                        }
                    }
                    if (mFoundAllWords)
                    {
                        if (bAllTitleWords)
                        {
                            aSuggestions.push(this.keywords[i]);
                        }
                        else
                        {
                            mSecondSuggestions.push(this.keywords[i]);
                        }
                    }
                }
            }
            for (var i = 0; i < mSecondSuggestions.length; i++)
            {
                aSuggestions.push(mSecondSuggestions[i]);
            }

            oAutoSuggestControl.autosuggest(aSuggestions, bTypeAhead);
        }
        else
        {
            oAutoSuggestControl.hideSuggestions();
        }
    }
};

CatSelectorSuggestions.registerClass("CatSelectorSuggestions");


// ********** WizCategorySelector ***********************

WizCatSelectorSuggestions = function(oCatIDObject, oTextBoxObject, bEventMode, sPostBackControlID)
{

    WizCatSelectorSuggestions.initializeBase(this,[""]);
    this.allowUnlisted = true;
    this.catIDObject = oCatIDObject;
    this.textBoxObject = oTextBoxObject;
    this.eventMode = bEventMode;
    this._PostBackControlID = sPostBackControlID
   

};

WizCatSelectorSuggestions.prototype =
{

    doAction: function(sCatID)
    {
        if (!sCatID)
        {
            this.catIDObject.value = '-1';

        }
        else
        {
            this.catIDObject.value = sCatID;
        }
        if (this.eventMode)
        {
            __doPostBack(this._PostBackControlID, this.catIDObject.value + "%" + this.textBoxObject.value);
        }
        else
        {
            //this.textBoxObject.form[(this.findTabIndex(this.textBoxObject) + 1) % this.textBoxObject.form.length].focus();
        }
        return false;
    },

    findTabIndex: function(input)
    {
        var index = -1, i = 0, found = false;
        while (i < input.form.length && index == -1)
            if (input.form[i] == input) index = i;
        else i++;
        return index;
    }
};

WizCatSelectorSuggestions.registerClass("WizCatSelectorSuggestions", CatSelectorSuggestions);


// ********** ForumCategorySelector ***********************


ForumCatSelectorSuggestions = function(sNavigateURL)
{

    ForumCatSelectorSuggestions.initializeBase(this, [sNavigateURL]);
    


};

ForumCatSelectorSuggestions.prototype =
{
    init: function()
    {
        var oThis = this;
        Sys.Net.WebServiceProxy.invoke(
            "/Controls/Common/CategorySelectorService.asmx",
            "GetForumCategoriesSuggestions",
            false,
            {},
            Function.createDelegate(this, this.getCategoriesSuggestionsComplete),
            null);
    }
};

ForumCatSelectorSuggestions.registerClass("ForumCatSelectorSuggestions", CatSelectorSuggestions);

NewThreadCatSelectorSuggestions = function(oCatIDObject, oTextBoxObject, bEventMode, sPostBackControlID)
{

    NewThreadCatSelectorSuggestions.initializeBase(this);
    this.allowUnlisted = false;
    this.catIDObject = oCatIDObject;
    this.textBoxObject = oTextBoxObject;
    this.eventMode = bEventMode;
    this._PostBackControlID = sPostBackControlID


};

NewThreadCatSelectorSuggestions.prototype =
{
    init: function()
    {
        var oThis = this;
        Sys.Net.WebServiceProxy.invoke(
            "/Controls/Common/CategorySelectorService.asmx",
            "GetForumCategoriesSuggestions",
            false,
            {},
            Function.createDelegate(this, this.getCategoriesSuggestionsComplete),
            null);
    },

    doAction: function(sCatID)
    {
        if (!sCatID)
        {
            return false;

        }
        else
        {
            this.catIDObject.value = sCatID;
        }
        if (this.eventMode)
        {
            __doPostBack(this._PostBackControlID, this.catIDObject.value + "%" + this.textBoxObject.value);
        }
        else
        {
            this.textBoxObject.form[(this.findTabIndex(this.textBoxObject) + 1) % this.textBoxObject.form.length].focus();
        }
        return false;
    },

    findTabIndex: function(input)
    {
        var index = -1, i = 0, found = false;
        while (i < input.form.length && index == -1)
            if (input.form[i] == input) index = i;
        else i++;
        return index;
    }
};

NewThreadCatSelectorSuggestions.registerClass("NewThreadCatSelectorSuggestions", CatSelectorSuggestions);
// **********