function NLWISFindRegisterForEvents(){
    var gnEventID = goEventManager.findEventID('MORE_OPTIONS_CHANGED');
    goEventManager.registerForEvent(gnEventID, "NLWISFindMoreOptionsChanged");
    
    goCWCJSAPI.RegisterEvent(MAP_EXTENT_CHANGED, "goFind.moveSearchingImg");
}

function NLWISFindMoreOptionsChanged(){
    var oElem = document.getElementById("NLWISFindTypes");
    if (oElem != null){
        goFind.draw0();
    }
}

function NLWISFindInit(){
    goFind = new nlwisFind();
    goFind.initSearchingLayer();
    goFind.draw0(true);
}

function nlwisFind(){
    this.initSearchingLayer = function(){
        content = '<img id="SearchingImg" name="SearchingImg" src="' + gNlwisFindWaitImage + '" width="' +
                  gNlwisFindWaitImageWidth + '" height="' + gNlwisFindWaitImageHeight + '" border="0">';
        CWCDHTML_CreateLayer("Searching",
                             gMapWhspc + gMapWiWidth/2 - gMapDHTMLWaitImageWidth/2,
                             gMapWvspc + gMapWiHeight/2 - gMapDHTMLWaitImageHeight/2,
                             gNlwisFindWaitImageWidth, gNlwisFindWaitImageHeight,
                             false, content);
        CWCDHTML_SetLayerZOrder("Searching", 30);
    }
    this.moveSearchingImg = function(){
        mapWidth = getMapWidth();
        mapHeight = getMapHeight();
        imgHeight = document.images["SearchingImg"].height;
        imgWidth = document.images["SearchingImg"].width;
        CWCDHTML_SetLayerPos("Searching", gMapWhspc + mapWidth/2 - imgWidth/2, gMapWvspc + mapHeight/2 - imgHeight/2);
    }

    // tab 1
    this.draw0 = function (bInit) {
        if (!bInit){ // not necessary on startup
            this.clearPoints();
            this.clear(this.oTopElem);
        }

        this.createAndAppendTitle(1);
        var oElem = this.createAndAppendElem("p",this.oTopElem,28);
        var oUL = this.initFindTypesUL();
        this.appendElem(oUL,this.oTopElem);
    }
    this.initFindTypesUL = function(){
        var aFindTypes = NLWISFindGetFindTypes(document.forms[0].setOption && (document.forms[0].setOption.value == "true") ? true : false);
        var oUL = this.createElem("ul");
        oUL.setAttribute("id","NLWISFindTypes");
        var nStr, oLI, oA;
        for(var i=0;i<aFindTypes.length;i++){
            switch(aFindTypes[i]){
                case "nts":
                    nStr=5;
                    break;
                case "postalcode":
                    nStr=4;
                    break;
                case "watershed":
                    nStr=2;
                    break;
                case "slc":
                    nStr=32;
                    break;
                case "placename":
                default:
                    nStr=3;
                    break;
            }
            oA = this.createFindTypesA(nStr);
            oLI = this.createElem("li",null);
            oLI.appendChild(oA);
            oUL.appendChild(oLI);
        }
        return oUL;
    }
    this.createFindTypesA = function(nStr){
        var oA = this.createElem("a",nStr);
        oA.setAttribute("name",nStr);
        oA.setAttribute("href","");
        this.addListener(oA,"onclick",goFind.draw1Nav,goFind.draw1IE);
        return oA;
    }
    this.draw1Nav = function(event){
        event.preventDefault();
        goFind.draw1(event.currentTarget.name);
    }
    this.draw1IE = function(event){
        event.returnValue = false;
        goFind.draw1(event.srcElement.name);
    }

    //tab 1+
    this.draw1 = function(nType){
        this.clear(this.oTopElem);
        this.drawFindSection(nType);
        var oElem = document.getElementById("NLWISFindDetails");
        oElem.focus();
    }
    this.drawFindSection = function(nType){
        var nTitle,nFindLabel;
        this.nFindType = nType;

        switch(nType){
            case "5":
                // nts
                nTitle = 13;
                nFindLabel = 14;
                break;
            case "4":
                // postalcode
                nTitle = 17;
                nFindLabel = 18;
                break;
            case "2":
                // watershed
                nTitle = 21;
                nFindLabel = 22;
                break;
            case "32":
                // slc
                nTitle = 34;
                nFindLabel = 35;
                break;
            case "3":
            default:
                // placename
                if(document.forms[0].setOption && (document.forms[0].setOption.value) == "true" ? true : false){
                    nTitle = 1;
                } else {
                    nTitle = 9;
                }
                nFindLabel = 11;
                break;
        }

        this.createAndAppendTitle(nTitle);
        this.createAndAppendElem("p",this.oTopElem,nFindLabel);

        var oElem = this.createElem("input");
        oElem.setAttribute("type","text");
        oElem.setAttribute("id","NLWISFindDetails");
        if (isIE){
            oElem.style.marginTop = '-7px';
        }
        this.addListener(oElem,"onkeypress",goFind.chkEnterKeyNav,goFind.chkEnterKeyIE);
        this.appendElem(oElem,this.oTopElem);

        oElem = this.createElem("div");
        this.setClass(oElem, "button");
        var oA = this.createElem("a",0);
        this.setClass(oA, "find");
        oA.style.backgroundImage = 'url('+gNlwisFindImgPathFind+')';
        this.addListener(oA,"onclick",goFind.searchNav,goFind.searchIE);
        oElem.appendChild(oA);
        this.appendElem(oElem,this.oTopElem);
    }
    this.chkEnterKeyNav = function(event){
        if (event.which == 13){
            goFind.searchNav(event);
        }
    }
    this.chkEnterKeyIE = function(event){
        if (event.keyCode == 13){
            goFind.searchIE(event);
        }
    }
    this.searchNav = function(event){
        event.preventDefault();
        goFind.search();
    }
    this.searchIE = function(event){
        event.returnValue = false;
        goFind.search();
    }
    this.applyNav = function(event){
        event.preventDefault();
        goFind.apply();
    }
    this.applyIE = function(event){
        event.returnValue = false;
        goFind.apply();
    }
    this.draw0Nav = function(event){
        event.preventDefault();
        goFind.draw0();
    }
    this.draw0IE = function(event){
        event.returnValue = false;
        goFind.draw0();
    }

    this.search = function(){
        var oElem = document.getElementById("NLWISFindDetails");
        if (oElem.value == null || oElem.value == ""){
            alert(NLWISFindGetString(26));
            return;
        }
        this.searchNow(oElem.value);
    }
    
    // needed to pull this out of search() into own function to use setTimeout functionality
    // properly. Needed to pull out only the code required to run in a loop until the
    // map was loaded in the application.
    this.searchNow = function(strDetails){
        // make sure initial map is loaded before proceeding with an SLC search
        if (this.nFindType == "32" && !this.bMapLoaded){
            var layer = CWCDHTML_GetLayer("ActivityLayer");
            if (layer != null){
                var str;
                if (CWCIsNav4){
                    str = "show";
                } else if (CWCIsIE || CWCIsNav6){
                    str = "visible";
                }
                if (layer.visibility == str){
                    setTimeout("goFind.searchNow("+strDetails+")", 1500);
                    return false;
                }
                this.bMapLoaded = true;
            }
        }
        
        CWCDHTML_ShowLayer('Searching');
        
        var item = 0;
        var aHiddenVars = new Array();
        aHiddenVars[item] = new Array(2);
        aHiddenVars[item][0] =  "NLWISFIND_FINDTYPE";
        aHiddenVars[item][1] = this.nFindType;
        aHiddenVars[++item] = new Array(2);
        aHiddenVars[item][0] =  "NLWISFIND_DETAILS";
        aHiddenVars[item][1] = strDetails;
        
        goCWCJSAPI.CallServer("goFind.searchDone()", aHiddenVars);
    }
    
    this.searchDone = function(){
        CWCDHTML_HideLayer('Searching');

        var oElem, oElem2InsertBefore,oA;
        oElem = document.getElementById("NLWISFindTitle");
        oElem2InsertBefore = oElem.nextSibling;
        oElem = document.getElementById("NLWISFindDetails");
        var str = NLWISFindGetString(30) + oElem.value;

        if (document.getElementById("NLWISFindResultsStr") == null){
            oElem = this.createAndInsertElem("h3",oElem2InsertBefore,29);
            oElem.setAttribute("id","NLWISFindResultsStr");
            oElem = this.createAndInsertElem("p",oElem2InsertBefore,str);
            oElem.setAttribute("id", "NLWISFindSearchedFor");
            // results (from below) will go in here
            oElem = this.createAndInsertElem("div",oElem2InsertBefore);
            oElem.setAttribute("id", "NLWISFindDivSeparator");

            // change title in Find Section and add link to bottom
            var nSubTitle;
            switch(this.nFindType){
                case "5":
                    // nts
                    nSubTitle = 12;
                    break;
                case "4":
                    // postalcode
                    nSubTitle = 16;
                    break;
                case "2":
                    // watershed
                    nSubTitle = 20;
                    break;
                case "32":
                    // slc
                    nSubTitle = 33;
                    break;
                case "3":
                default:
                    // placename
                    if(document.forms[0].setOption && (document.forms[0].setOption.value) == "true" ? true : false){
                        nSubTitle = 19;
                    } else {
                        nSubTitle = 8;
                    }
                    break;
            }
            oElem = this.createAndInsertElem("h3", oElem.nextSibling, NLWISFindGetString(nSubTitle));
            if (isIE){
                oElem.style.marginTop = '7px';
            }

            oElem = this.createElem("p");
            oA = this.createElem("a",NLWISFindGetString(24));
            oA.setAttribute("href","");
            this.addListener(oA,"onclick",goFind.draw0Nav,goFind.draw0IE);
            oElem.appendChild(oA);
            if (isIE){
                oElem.style.marginTop = '7px';
            }
            this.oTopElem.appendChild(oElem);
        } else {
            oElem = document.getElementById("NLWISFindSearchedFor");
            oElem.firstChild.nodeValue = str;
        }

        oElem2InsertBefore = document.getElementById("NLWISFindDivSeparator");

        var oResults = document.getElementById("NLWISFindResults");
        if (oResults == null){
            oResults = this.createAndInsertElem("div",oElem2InsertBefore);
            oResults.setAttribute("id","NLWISFindResults");
        } else {
            this.clear(oResults,oResults);
        }

        var doc = goCWCJSAPI.GetDocumentObject();
        var strResults = doc.forms[0].NLWISFIND_RESULTS.value;
        if (strResults == ""){
            oResults.appendChild(this.createElem("p",NLWISFindGetString(7)));
        } else if (strResults == NLWISFindGetString(31)){
            oResults.appendChild(this.createElem("p",strResults));
        } else {
            var nResultsLabel;
            switch(this.nFindType){
                case "23":
                    // watershed
                    nResultsLabel = 23;
                    break;
                case "32":
                    // slc
                    nResultsLabel = 36;
                    break;
                default:
                    nResultsLabel = 10;
                    break;
            }
            oElem = this.createElem("p",NLWISFindGetString(nResultsLabel));
            if (isIE){
                oElem.style.marginBottom = '7px';
            }
            oResults.appendChild(oElem);

            var oSelect = this.createElem("select");
            oSelect.setAttribute("id","NLWISFindPlaceCoor");
            oSelect.setAttribute("size","7");

            var temp = new Array();
            temp = strResults.split('~');
            for (var i=0;i<temp.length;i++){
//                if (temp.length > 1){
                    oSelect.appendChild(this.createResultOptions(temp[i],(i+1)));
//                } else {
//                    oSelect.appendChild(this.createResultOptions(temp[i]));
//                }
            }

            if (isIE){
                // IE doesn't display the select box at a height of 7 when the Find button is hit
                // and a Results SELECT is already displayed. Opens to a single-selection, drop-down
                // list. Need to do a refresh, so had to put it in a table in IE only.
                var oTable = this.createElem("table");
                var oTBody = this.createElem("tbody");
                var oTRow = this.createElem("tr");
                var oTCell = this.createElem("td");
                oTCell.appendChild(oSelect);
                oTRow.appendChild(oTCell);
                oTBody.appendChild(oTRow);
                oTable.appendChild(oTBody);
                oResults.appendChild(oTable);
                oTable.refresh();
            } else {
                oResults.appendChild(oSelect);
                oResults.appendChild(this.createElem("br"));
            }

            oElem = this.createElem("div");
            this.setClass(oElem,"button");
            oA = this.createElem("a",NLWISFindGetString(27));
            this.setClass(oA,"updateMap");
            oA.setAttribute("href","");
            oA.style.backgroundImage = 'url('+gNlwisFindImgPathUpdate+')';
            this.addListener(oA,"onclick",goFind.applyNav,goFind.applyIE);
            oElem.appendChild(oA);

            oResults.appendChild(oElem);
            oResults.appendChild(this.createElem("br"));
        }
    }
    this.createResultOptions = function(strResult, cnt){
        var temp = new Array();
        temp = strResult.split('|');
        
        var oElem;
        if (cnt){
            oElem = this.createElem("option",cnt+") "+temp[temp.length-1]);
//        } else {
//            oElem = this.createElem("option",temp[temp.length-1]);
        }
        oElem.setAttribute("value",temp[0]+"|"+temp[1])
        return oElem;
    }

    this.apply = function(){
        var oElem = document.getElementById("NLWISFindPlaceCoor");
        if (oElem.selectedIndex == -1){
            alert(NLWISFindGetString(6));
            return;
        }

        CWCDHTML_ShowLayer("ActivityLayer");

        // szLatLong is a | delimited string.  First element is the name or ID,
        // second is the coords ... third is possibly a feature (or just the name/id
        // displayed in the select list
        var szLatLong = oElem.options[oElem.selectedIndex].value;

        var item = 0;
        var aHiddenVars = new Array();
        aHiddenVars[item] = new Array(2);
        aHiddenVars[item][0] =  "NLWISFIND_LATLONG";
        aHiddenVars[item][1] = szLatLong;
        aHiddenVars[++item] = new Array(2);
        aHiddenVars[item][0] =  "NLWISFIND_FINDTYPE";
        aHiddenVars[item][1] = this.nFindType;
        goCWCJSAPI.CallServer("goCWCJSAPI.MapExtentsUpdated()", aHiddenVars);
    }
    
    this.clearPoints = function(){
        var aHiddenVars = new Array();
        aHiddenVars[0] = new Array(2);
        aHiddenVars[0][0] =  "NLWISFIND_CLEARPOINTS";
        aHiddenVars[0][1] = 1;
        goCWCJSAPI.CallServer("goCWCJSAPI.MapExtentsUpdated()", aHiddenVars);
    }

    // DOM editing functions
    this.clear = function(oElem,oTopElem){
        if (oTopElem == null){
            oTopElem = this.oTopElem;
        }
        if (oElem.childNodes.length == 0){
            oElem.parentNode.removeChild(oElem);
        } else {
            for (var i=oElem.childNodes.length-1;i>-1;i--){
                this.clear(oElem.childNodes[i]);
            }
            if (oElem.id != oTopElem.id){
                this.clear(oElem);
            }
        }
    }
    this.createElem = function(elem,txt){
        var oElem;
        if (typeof elem == "string"){
            oElem = document.createElement(elem);

            if (txt != null){
                var oTxt;
                if (typeof txt == "string"){
                    oTxt = document.createTextNode(txt);
                } else { // typeof txt == "integer"
                    oTxt = document.createTextNode(NLWISFindGetString(txt));
                }
                oElem.appendChild(oTxt);
            }
        } else { // object
            oElem = elem;
        }
        oElem = this.fix4IE(oElem);
        return oElem;
    }
    this.fix4IE = function(oElem){
        if (isIE){
            switch((oElem.tagName).toLowerCase()){
                case "p":
                case "h3":
                case "ul":
                    oElem.style.marginTop = '-7px';
                    break;
            }

        }
        return oElem;
    }
    this.appendElem = function(oElem,oElem2Append2){
        return oElem2Append2.appendChild(oElem);
    }
    this.createAndAppendElem = function(elemType,oElem2Append2,nTxt){
        var oElem = this.createElem(elemType,nTxt);
        return this.appendElem(oElem,oElem2Append2);
    }
    this.createAndInsertElem = function(elem,oElem2InsertBefore,nTxt){
        var oNewElem = this.createElem(elem,nTxt);
        return this.oTopElem.insertBefore(oNewElem, oElem2InsertBefore)
    }
    this.createAndAppendTitle = function(nTitle){
        var oElem = this.createAndAppendElem("h2",this.oTopElem,nTitle);
        oElem.setAttribute("id","NLWISFindTitle");
    }

    // IE vs Nav handlers
    this.addListener = function(obj,name,funcNav,funcIE){
        if (obj.addEventListener){
            obj.addEventListener(name.replace( /^on/, ''), funcNav, false);
        } else if (obj.attachEvent){
            obj.attachEvent(name, funcIE);
        } else {
            obj[name] = funcNav;
        }
    }
    this.setClass = function(obj,name){
        if (obj.addEventListener){
            obj.setAttribute("class",name);
        } else if (obj.attachEvent){
            obj.setAttribute("className",name);
        } else {
            obj[name] = funcNav;
        }
    }

    // div id to fill in
    this.oTopElem = document.getElementById(gNlwisFindTopElement);
    // the current find type the user is searching (e.g., "3" is placename)
    this.nFindType;
    // whether initial map has been loaded or not. For Find SLC, the map's 
    // extents limit the query. If the map hasn't loaded, the extents haven't
    // been set, so the user will get no records returned if the search is
    // processed.
    this.bMapLoaded = false;
}

