Array.prototype.find = function(searchString) {
    for (var i = 0; i < this.length; i++) {
        if (searchString == this[i]) {
            return i;
        }
    }
    return -1;
}

function IGetCategoriesList() { return GetCategoriesList(); }
function IGetSearchText(){return GetSearchText();}

var Categories = new Array();
function CategoryParent(description) {
    this.Children = new Array();
    this.Description = description;
}
function CategoryChild(id, description) {
    this.Id = id;
    this.Description = description;
}
function CreateCategoryLocal() {
    var categoriesList = IGetCategoriesList();
    for (var nParent = 0; nParent < categoriesList.getElementsByTagName("OPTGROUP").length; nParent++) {
        Categories[nParent] = new CategoryParent(categoriesList.getElementsByTagName("OPTGROUP")[nParent].label);
        for (var nChild = 0; nChild < categoriesList.getElementsByTagName("OPTGROUP")[nParent].childNodes.length; nChild++) {
            Categories[nParent].Children[nChild] = new CategoryChild(categoriesList.getElementsByTagName("OPTGROUP")[nParent].childNodes[nChild].value,
                                                                     categoriesList.getElementsByTagName("OPTGROUP")[nParent].childNodes[nChild].text);
        }
    }
}
function TextSearchBusinessCategories(bPreserveSelected) {
    var categoriesList = IGetCategoriesList();
    var sTextSearchWords = IGetSearchText().value;
    var arrCurrentItems = new Array();
    for (var nParent = 0; bPreserveSelected && nParent < categoriesList.getElementsByTagName("OPTGROUP").length; nParent++) {
        for (var nChild = 0; nChild < categoriesList.getElementsByTagName("OPTGROUP")[nParent].childNodes.length; nChild++) {
            if (categoriesList.getElementsByTagName("OPTGROUP")[nParent].childNodes[nChild].selected == true) {
                arrCurrentItems.push(IGetCategoriesList().getElementsByTagName("OPTGROUP")[nParent].childNodes[nChild].value);
            }
        }
    }
    categoriesList.innerHTML = "";
    var Matches = 0;
    var bSelected = false;
    var selectOption, optGroup;
    for (var nParent = 0; nParent < Categories.length; nParent++) {
        optGroup = document.createElement('OPTGROUP');
        optGroup.label = Categories[nParent].Description;
        for (var nChild = 0; nChild < Categories[nParent].Children.length; nChild++) {
            bSelected = (arrCurrentItems.find(Categories[nParent].Children[nChild].Id) > -1);
            if (Categories[nParent].Children[nChild].Description.toLowerCase().indexOf(sTextSearchWords) > -1 ||
                bSelected ) {
                Matches++;
                selectOption = document.createElement('OPTION');
                selectOption.text = Categories[nParent].Children[nChild].Description;
                selectOption.innerText = Categories[nParent].Children[nChild].Description;
                selectOption.value = Categories[nParent].Children[nChild].Id;
                selectOption.selected = bSelected;
                try { optGroup.appendChild(selectOption, null); } catch (e) { };
                try { optGroup.appendChild(selectOption); } catch (e) { };
            }
        }
        if (Matches > 0) {
            try { categoriesList.appendChild(optGroup, null); } catch (e) { };
            try { categoriesList.appendChild(optGroup); } catch (e) { };
            Matches = 0;
        }
    }
}
function TextSearchBusinessCategories2(bPreserveSelected) {
    var test = new String();
    var categoriesList = IGetCategoriesList();
    var sTextSearchWords = IGetSearchText().value;

    var arrCurrentItems = new Array();
    var header;
    var labelText;
    for (i = 0; i < categoriesList.childNodes.length; i++) {
        if (categoriesList.childNodes.item(i).tagName == "H5") {
            header = categoriesList.childNodes.item(i);
            header.style.display = "none";
        } else if (categoriesList.childNodes.item(i).tagName == "DIV") {

            if (categoriesList.childNodes.item(i).lastChild.tagName == "LABEL") {
                var wtfisthis = categoriesList.childNodes.item(i).lastChild;
                labelText = (categoriesList.childNodes.item(i).lastChild.innerText)
                    ? categoriesList.childNodes.item(i).lastChild.innerText
                    : categoriesList.childNodes.item(i).lastChild.innerHTML ;
                if (labelText.toLowerCase().indexOf(sTextSearchWords) > -1) {
                    categoriesList.childNodes.item(i).style.display = "block";
                    header.style.display = "block";
                }  else
                    categoriesList.childNodes.item(i).style.display = "none";
            }
        }
    }   
}
function SelectAllVisibleCategories() {
    var categoriesList = IGetCategoriesList();
    categoriesList.style.display = 'none';
    for (var i = 0; i < IGetCategoriesList().options.length; i++) {
        categoriesList.options[i].selected = true;
    }
    categoriesList.style.display = 'block';
}

function ExpandCatPanel(id) {
    var bShow = false;
    var target = document.getElementById(id);
    for (var i = 1; i < target.childNodes.length; i++) {
        if (target.childNodes[i].className.indexOf("hidden", 0) > -1) {
            target.childNodes[i].className = target.childNodes[i].className.replace(/hidden/g, "");
            bShow = true;
        } else {
            target.childNodes[i].className += " hidden";
        }
    }
    if (bShow) {
        if (target.scrollIntoView) {
            target.scrollIntoView();  
        } else {
        document.getElementById("lstCategories").scrollTo(0, target.offsetTop);
        }
    }
     
}
function SelectAllCatPanelItems(id) {
    var target = document.getElementById(id);
    var bChecked = target.firstChild.firstChild.checked;
    for (var i = 1; i < target.childNodes.length; i++) {
        if (target.childNodes[i].firstChild) {
            target.childNodes[i].firstChild.checked = bChecked;
            
            if (bChecked) {
                target.childNodes[i].className += " Selected";
            }
            else {
                target.childNodes[i].className = target.childNodes[i].className.replace(/Selected/g, "");
            }
        }
    }
}
function CheckItem(e) {
    if (!e) {
        e = window.event;
    }
    var sender;
    if (e.target) { sender = e.target; }
    else if (e.srcElement) { sender = e.srcElement }
    
    var s = new String();

    if (sender.type.toUpperCase() == "RADIO")
        return;
    
    
    
    var parentDiv = sender.parentNode;

    if (sender.checked) {
        parentDiv.className += " Selected";
    }
    else {
        parentDiv.className = parentDiv.className.replace(/Selected/g, "");
    }
    var bAllCheck = true;
    for (var i = 1; i < parentDiv.parentNode.childNodes.length; i++) {
        if (parentDiv.parentNode.childNodes[i].firstChild.checked == false) {
            bAllCheck = false;
        }
    }

    parentDiv.parentNode.firstChild.firstChild.checked = bAllCheck;

}
