﻿/// <reference path="jqueryv1_intellisense.js/>
var LRSearch =
{
    _browseTrack: [],
    _topicType: "",
    onError: function(dispDivText, parentDivID)     // DON'T CHANGE THE ARGUMENT NAMES. IF YOU MUST DO THE SAME CHANGE IN .vb PAGES ALSO
    {
        //Displaying a pop-up box if any error occurred
        alert("Exception :\n" + dispDivText);
        window.status = "An error has occurred.";
    },

    searchOnfocus: function() {
        var searchTxt = LRUtil.Trim($("#txtSearch").val());
        if (searchTxt == "enter a question, full/partial word, or memo number" || searchTxt == "enter a question, full/partial word, or form number") {
            $("#txtSearch").val("").attr("class", "txt-search");
        }
    },

    searchOnBlur: function(type) {
        if (LRUtil.Trim($("#txtSearch").val()) == "") {
            $("#txtSearch").val((type == "F") ? "enter a question, full/partial word, or form number" : "enter a question, full/partial word, or memo number").attr("class", "txt-search fa-graytxt");
        }
    },

    appendBrowse: function(dispDivText, parentDivID) {
        if (parentDivID == "TB") {
            //
        } else {
            var showChildDiv, parentDiv, childLevel, childDivID, childDiv, currOpenedLevel, parentTopLevelID;

            showChildDiv = true;

            parentTopLevelID = parentDivID.substr(0, 5); //div15
            childLevel = parentDivID.length - 4;

            if (LRSearch._browseTrack.length > 0) {
                if (LRSearch._browseTrack.indexOf(parentTopLevelID) == -1) {
                    LRSearch.removeAllChildren(parentTopLevelID);
                } else {
                    if (LRSearch._browseTrack.indexOf(parentDivID) != -1) {
                        LRSearch.removeChildrenByLevel(childLevel, parentDivID);
                        showChildDiv = false;
                    } else {
                        LRSearch.removeChildrenByLevel(childLevel);
                    }
                }
            }
            if (showChildDiv) {
                childDivID = "divM" + childLevel;

                childDiv = $("<div></div>");
                childDiv.attr("id", childDivID)
                childDiv.attr("class", "f2");
                //childDiv.css("padding-left", (childLevel * 10) + "px");
                //childDiv.css("padding-left", "10px");
                childDiv.css("padding-left", "20px");
                childDiv.html(dispDivText);

                parentDiv = $("#" + parentDivID);
                parentDiv.attr("class", "f2bc fa-sel");
                parentDiv.after(childDiv);
                LRSearch._browseTrack.include(parentDivID);
            }
            window.location.hash = "#th=" + parentDivID.substr(3);
        }
        LRSearch.storeTopictTrail(dispDivText, parentDivID, _topicType);
    },

    removeAllChildren: function(parentID) {
        var filter = "div[id^='divM']";
        if ($(filter).length > 0) {
            $(filter).remove();
            var filter = "div[id='" + LRSearch._browseTrack[0] + "']"; //top level
            $(filter).attr("class", "f2 fa-normal");
            LRSearch._browseTrack = [];
        }
    },

    removeChildrenByLevel: function(level, divID) {
        var filter = "div[id^='divM" + level + "']";
        var arrayLen = LRSearch._browseTrack.length;
        if ($(filter).length > 0) {
            $(filter).remove();

            if (divID != null) {
                filter = "div[id='" + divID + "']";
                $(filter).attr("class", "f2 fa-normal");
            } else {
                filter = "div[id='" + LRSearch._browseTrack[level - 1] + "']";
                $(filter).attr("class", "f2 fa-normal");
            }
            LRSearch._browseTrack.splice(level - 1, (arrayLen - (level - 1)));
        }
    },

    initBrowTrack: function(topicid) {
        var i = 2;
        //debugger
        topicid = String(topicid);
        while (i <= topicid.length) {
            LRSearch._browseTrack.include("div" + topicid.substr(0, i));
            i++;
        }
    },

    ShowMemoDiv: function(pMeomID, pTopicID, pPageType) {
        var cook;
        var arrCook;
        var blnDupMemo = false;
        var memo, memonew;
        if (pPageType == "FA") {  // FIND ANSWER
            cook = this.GetCookie("TOPICIDFA");
            memo = this.GetCookie("TOPICMEMO");
        }
        else if (pPageType == "FF") { // FIND FORM
            cook = this.GetCookie("TOPICIDFF");
            memo = this.GetCookie("TOPICFORM");
        }
        if (memo == null) { memo = ""; }
        if (memo == "") {
            arrCook = pTopicID + ":" + pMeomID;
            if (pPageType == "FA") {
                document.cookie = "TOPICMEMO=" + arrCook;
            }
            else {
                document.cookie = "TOPICFORM=" + arrCook;
            }
        }
        else {
            var arrayMemo = memo.split(",");
            for (i = 0; i < arrayMemo.length; i++) {
                if (arrayMemo[i].split(":")[0] == pTopicID) {
                    blnDupMemo = true;
                    arrayMemo.remove(arrayMemo[i]);
                    memonew = pTopicID + ":" + pMeomID;
                    arrayMemo.include(memonew);
                    memo = arrayMemo.join(",");
                    if (pPageType == "FA") {
                        document.cookie = "TOPICMEMO=" + memo;
                    }
                    else {
                        document.cookie = "TOPICFORM=" + memo;
                    }
                }
            }
            if (!blnDupMemo) {
                memonew = pTopicID + ":" + pMeomID;
                memo = memo + "," + memonew;
                if (pPageType == "FA") {
                    document.cookie = "TOPICMEMO=" + memo;
                }
                else {
                    document.cookie = "TOPICFORM=" + memo;
                }

            }
        }

        var blnDup = false;
        if (cook == null) { cook = ""; }

        if (cook == "") {
            if (pPageType == "FA") {
                document.cookie = "TOPICIDFA=" + pTopicID;
            }
            else if (pPageType == "FF") {
                document.cookie = "TOPICIDFF=" + pTopicID;
            }
        } else {
            var arrayCook = cook.split(",");
            for (i = 0; i < arrayCook.length; i++) {
                if (arrayCook[i] == pTopicID) {
                    blnDup = true;
                    break;
                }
            }
            if (!blnDup) {
                cook = cook + "," + pTopicID;
                if (pPageType == "FA") {
                    document.cookie = "TOPICIDFA=" + cook;
                }
                else if (pPageType == "FF") {
                    document.cookie = "TOPICIDFF=" + cook;
                }
            }
        }
        window.location.href = "Memo_Redirect.aspx?txtMemoID=" + pMeomID;
    },
    storeTopictTrail: function(pMeomID, pTopicID, pPageType) {
        var cook, arrayCook;
        var pTopicID = pTopicID.substring(3);
        if (pPageType == "FA") {  // FIND ANSWER
            cook = this.GetCookie("TOPICIDFA");
        }
        else if (pPageType == "FF") { // FIND FORM
            cook = this.GetCookie("TOPICIDFF");
        }
        var blnDup = false;
        var blnDupPar = false;
        if (cook == null) { cook = ""; }

        if (cook == "") {
            if (pPageType == "FA") {
                document.cookie = "TOPICIDFA=" + pTopicID;
            } else if (pPageType == "FF") {
                document.cookie = "TOPICIDFF=" + pTopicID;
            }
        } else {
            arrayCook = cook.split(",");
            for (i = 0; i < arrayCook.length; i++) {
                if (arrayCook[i] == pTopicID) {
                    blnDup = true;
                    break;
                } else {
                    if (arrayCook[i] == pTopicID.substr(0, pTopicID.length - 1)) {
                        //cook = cook.replace(arrayCook[i], pTopicID);
                        // arrayCook.splice(i, arrayCook[i]);
                        arrayCook.remove(arrayCook[i]);
                        arrayCook.include(pTopicID);
                        cook = arrayCook.join(",");
                        blnDupPar = true;
                        break;
                    }
                }
            }
            if (!blnDup) {
                if (!blnDupPar) {
                    cook = cook + "," + pTopicID;
                }

                arrayCook = cook.split(",");
                if (arrayCook.length > 10) {
                    arrayCook.splice(0, arrayCook.length - 10);
                    cook = arrayCook.join(",");
                }

                if (pPageType == "FA") {
                    document.cookie = "TOPICIDFA=" + cook;
                } else if (pPageType == "FF") {
                    document.cookie = "TOPICIDFF=" + cook;
                }
            }
        }

    },

    GetCookie: function(pName) {
        var cname = pName + "=";
        var dc = document.cookie;
        if (dc.length > 0) {
            begin = dc.indexOf(cname);
            if (begin != -1) {
                begin += cname.length;
                end = dc.indexOf(";", begin);
                if (end == -1) end = dc.length;
                return unescape(dc.substring(begin, end));
            }
        }
        return null;
    },

    OnSearchTxtKeyPress: function(e, pType) {
        var keyCode;
        isEnter = 1;
        if (window.event) {
            keyCode = e.keyCode;
        }
        else if (e.which) {
            keyCode = e.which;
        }
        if (keyCode == 13) {
            this.SearchAns(pType);
            return false;
        }
    },

    SearchAns: function(pType) {
        var txtSearch;
        txtSearch = LRUtil.Trim(document.getElementById("txtSearch").value);
        if (txtSearch.length == 0 || txtSearch == 'enter a question, full/partial word, or memo number' || txtSearch == 'enter a question, full/partial word, or form number') {
            alert("Enter search text");
            document.getElementById("txtSearch").focus();
        }
        else {
            if (pType == "M") {
                window.location.href = "Search.aspx?SER=" + txtSearch;
            }
            else if (pType == "F") {
                window.location.href = "Search_F.aspx?SER=" + txtSearch;
            }
        }
        return false;
    },

    GoMemoList: function(pType, pTopicID) {
        window.location.href = "MemoList.asp?MM=" + pType + "&TID=" + pTopicID;
    },

    GoHrefPage: function(pPage, pQryStr) {
        window.location.href = pPage + "?" + pQryStr;
    },

    SelectInd: function(pPage) {
        if (pPage == "ISG")     // INDUSTRY_SG
        {
            alert("You have to continue selecting a sub industry until you see your industry displayed.")
        }
        else if (pPage == "ITSG")       // INDUSTRYTITLE_SG
        {
            alert("You have to select an industry");
        }
        else {
            alert("ERROR");
        }
    },

    ShowDivProgress: function() {
        var scrollWidth;
        var scrollHeight;
        var divProgress;

        scrollWidth = document.body.scrollWidth;
        scrollHeight = document.body.scrollHeight;

        if (scrollHeight == 0) {
            scrollHeight = (window.innerHeight / 2) - 150;
        }
        else {
            scrollHeight = scrollHeight / 2;
        }

        divProgress = document.getElementById("divProgress");
        with (divProgress.style) {
            left = (scrollWidth / 2) - 150 + 'px';
            top = scrollHeight + 'px';
            position = 'absolute';
            display = 'block';
            zIndex = 101;
        }
    },

    HideDivProgress: function() {
        var divProgress = document.getElementById("divProgress");
        divProgress.style.display = 'none';
    }
};