// Copyright 2010 ESRI
// 
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
// 
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
// 
// See use restrictions at <your ArcGIS install location>/developerkit/userestrictions.txt.

/*
JavaScipt for handling most ORKA Processes.
*/
function compSwitch(pid1) {
    mapPids = "";
    dojo.byId("h_pid1").value = pid1;
    fetchSearchResults("Parcel_ID", 1, searchLimit);
}

/////////////////////////////////////////////////////
// onClick event for main Search Button.
/////////////////////////////////////////////////////
function fetchSearchResults(searchMethod, sIdx, sLim, sVal) {
    if (searchMethod == null || searchMethod == "")
        searchMethod = dojo.byId("h_searchButton").value;
    var treenode1 = "%";
    var username = dojo.byId("h_username").value;
    var password = dojo.byId("h_password").value;
    var browser = dojo.byId("h_browser").value;
    var selectNum = 1;
    var PP_Owner = "";
    searchIndex = sIdx;

    // Normally sLim is null or searchLimit.  It becomes
    // import if the user is performing a buffer.  In that
    // case we want to return all search resuls.
    if (sLim == null)
        sLim = searchLimit;

    if (mapPids != "") {
        searchMethod = "Map";
        selectNum = searchCount;
    }
    
    //if (searchMethod.indexOf(",") >= 0) {
        //PP_Owner = searchMethod;
        //searchMethod = "PP_POPUP";
    //}
    //alert(searchMethod);

    var dataString = "";
    var htmlTxt = "";

    // The searchTheme is either a broad "ORKA_SEARCH",
    // or based on a specific Theme Button.
    searchTheme = "ORKA_SEARCH";

    if (logLevel >= 2)
        console.log("function fetchSearchResults alert #1\nsearchMethod: " + searchMethod + "\nsearchTheme: " + searchTheme + "\nmapPids: " + mapPids);

    switch (searchMethod.toUpperCase()) {
        case "OWNER_NAME":
            var lastname = dojo.byId("lastname").value;
            var firstname = dojo.byId("firstname").value;

            //console.log("lastname: " + lastname +", firstname: "+ firstname);

            if (lastname == "") {
                alert("Please enter a last name.");
                return false;
            } else {
                if (firstname == "") firstname = "*";
                dataString = lastname + "|" + firstname;
            }

            break;

        case "ADDRESS":
            var strnum = dojo.byId("strnum").value;
            var strnumEndRange = dojo.byId("strnumEndRange").value;
            var strprefix = dojo.byId("strprefix").value;
            var strname = dojo.byId("strname").value;
            var strsuffix = dojo.byId("strsuffix").value;

            //alert(strnum +", "+ strname +", "+ strsuffix);

            if (strname == "") {
                alert("Please enter a street name.");
                return false;
            } else {
                if (strnum == "") strnum = "*";
                if (strnumEndRange == "") strnumEndRange = "*";
                if (strprefix == "") strprefix = "*";
                if (strsuffix == "") strsuffix = "*";
                dataString = strnum + "|" + strnumEndRange + "|" + strprefix + "|" + strname + "|" + strsuffix;
            }

            break;

        case "LEGAL":
            var section = dojo.byId("section").value;
            var township = dojo.byId("township").value;
            var range = dojo.byId("range").value;

            //console.log("section: " + section +", township: "+ township +", range: "+ range);

            if (section == "" || township == "" || range == "") {
                alert("Please enter more information.");
                return false;
            } else {
                                {
                    if (section.length < 2) section = "0" + section;
                    else section = section;
                }
                {
                    if (range.length < 2) range = "0" + range;
                    else range = range;
                }
                //if (firstname == "") firstname = "*";
                dataString = section + "|" + township + "|" + range;
            }

            break;

        case "SALES":
            var bedrooms = dojo.byId("bedrooms").value;
            var bathrooms = dojo.byId("bathrooms").value;
            var sqftFromSize = dojo.byId("sqftFromSize").value;
            var sqftToSize = dojo.byId("sqftToSize").value;
            var salesFromDate = dojo.byId("salesFromDate").value;
            var salesToDate = dojo.byId("salesToDate").value;
            var bldgStyle = dojo.byId("bldgStyle").value;
            var salesFromPrice = dojo.byId("salesFromPrice").value;
            var salesToPrice = dojo.byId("salesToPrice").value;
            var propertyType = dojo.byId("propertyType").value;
            var citySales = dojo.byId("citySales").value;
            var yrFromBuilt = dojo.byId("yrFromBuilt").value;
            var yrToBuilt = dojo.byId("yrToBuilt").value;
            var basement = dojo.byId("basement").value;
            var totFromAcres = dojo.byId("totFromAcres").value;
            var totToAcres = dojo.byId("totToAcres").value;
            var loggedIn = dojo.byId("h_logged_in").value;
            var townshipSales = dojo.byId("townshipSales").value;
            var rangeSales = dojo.byId("rangeSales").value;


            //console.log("section: " + sectionSales +", township: "+ townshipSales +", range: "+ rangeSales +", neighborhood: "+ neighborhoodCode +", salesFromDate: "+ salesFromDate +", salesToDate: "+ salesToDate +", salesFromPrice: "+ salesFromPrice +", salesToPrice: "+ salesToPrice);

            if (salesFromDate == "" || salesToDate == "") {
                alert("Please enter a sales date range.");
                return false;
            } else {
                //dataString = bedrooms + "|" + bathrooms + "|" + sqftFromSize + "|" + sqftToSize + "|" + salesFromDate + "|" + salesToDate + "|" + bldgStyle + "|" + salesFromPrice + "|" + salesToPrice + "|" + propertyType + "|" + citySales + "|" + yrFromBuilt + "|" + yrToBuilt + "|" + totFromAcres + "|" + totToAcres + "|" + basement;
		        dataString = bedrooms + "|" + bathrooms + "|" + sqftFromSize + "|" + sqftToSize + "|" + salesFromDate + "|" + salesToDate + "|" + bldgStyle + "|" + salesFromPrice + "|" + salesToPrice + "|" + propertyType + "|" + citySales + "|" + yrFromBuilt + "|" + yrToBuilt + "|" + totFromAcres + "|" + totToAcres + "|" + basement + "|" + loggedIn + "|" + townshipSales  + "|" + rangeSales;
            }

            break;

    case "SALES_POPUP":
    
        //alert("here 1");
    
		//var sectionSales1 = dojo.byId("sectionSales").value;
		//var townshipSales1 = dojo.byId("townshipSales").value;
		//var rangeSales1 = dojo.byId("rangeSales").value;
		//var neighborhoodCode1 = dojo.byId("neighborhoodCode").value;
		//var salesFromDate1 = dojo.byId("salesFromDate").value;
		//var salesToDate1 = dojo.byId("salesToDate").value;
		//var zipCode1 = dojo.byId("zipCode").value;
		//var salesFromPrice1 = dojo.byId("salesFromPrice").value;
		//var salesToPrice1 = dojo.byId("salesToPrice").value;
		//var propertyType1 = dojo.byId("propertyType").value;
		//var taxUnit1 = dojo.byId("taxUnit").value;
		searchMethod = "SALES";
		
	    var bedrooms = dojo.byId("bedrooms").value;
        var bathrooms = dojo.byId("bathrooms").value;
        var sqftFromSize = dojo.byId("sqftFromSize").value;
        var sqftToSize = dojo.byId("sqftToSize").value;
        var salesFromDate = dojo.byId("salesFromDate").value;
        var salesToDate = dojo.byId("salesToDate").value;
        var bldgStyle = dojo.byId("bldgStyle").value;
        var salesFromPrice = dojo.byId("salesFromPrice").value;
        var salesToPrice = dojo.byId("salesToPrice").value;
        var propertyType = dojo.byId("propertyType").value;
        var citySales = dojo.byId("citySales").value;
        var yrFromBuilt = dojo.byId("yrFromBuilt").value;
        var yrToBuilt = dojo.byId("yrToBuilt").value;
        var basement = dojo.byId("basement").value;
        var totFromAcres = dojo.byId("totFromAcres").value;
        var totToAcres = dojo.byId("totToAcres").value;
        var loggedIn = dojo.byId("h_logged_in").value;
        var townshipSales = dojo.byId("townshipSales").value;
        var rangeSales = dojo.byId("rangeSales").value;

		//console.log("section: " + sectionSales +", township: "+ townshipSales +", range: "+ rangeSales +", neighborhood: "+ neighborhoodCode +", salesFromDate: "+ salesFromDate +", salesToDate: "+ salesToDate +", salesFromPrice: "+ salesFromPrice +", salesToPrice: "+ salesToPrice);

		if (salesFromDate == "" || salesToDate == "") {
			alert("Please enter a sales date range.");
			return false;
		} else {
			//dataString = sectionSales1 +"|"+ townshipSales1 +"|"+ rangeSales1 +"|"+ neighborhoodCode1 +"|"+ salesFromDate1 +"|"+ salesToDate1 +"|"+ zipCode1 +"|"+ salesFromPrice1 +"|"+ salesToPrice1 +"|"+ propertyType1 +"|"+ taxUnit1;
		   dataString = bedrooms + "|" + bathrooms + "|" + sqftFromSize + "|" + sqftToSize + "|" + salesFromDate + "|" + salesToDate + "|" + bldgStyle + "|" + salesFromPrice + "|" + salesToPrice + "|" + propertyType + "|" + citySales + "|" + yrFromBuilt + "|" + yrToBuilt + "|" + totFromAcres + "|" + totToAcres + "|" + basement + "|" + loggedIn + "|" + townshipSales  + "|" + rangeSales;
		   //dataString = bedrooms + "|" + bathrooms + "|" + sqftFromSize + "|" + sqftToSize + "|" + salesFromDate + "|" + salesToDate + "|" + bldgStyle + "|" + salesFromPrice + "|" + salesToPrice + "|" + propertyType + "|" + citySales + "|" + yrFromBuilt + "|" + yrToBuilt + "|" + totFromAcres + "|" + totToAcres + "|" + basement;
		    
		    //alert("dataString: " + dataString);
		    
            //sendAjaxRequest(searchTheme, searchMethod, dataString, searchIndex, sLim, selectNum, treenode1, username, password, browser);

		    imgWin = window.open("", "imgWindow", "width="+w+", height=250, scrollbars=yes, resizable=yes, status=yes, menubar=yes, top=0, left="+(scrWidth - (w * 1.04)));
	        imgWin.location = popupSearchFile + "?modTheme=" + "ORKA_SEARCH" + "&searchMethod=SALES_POPUP" + "&dataString=" + dataString + "&searchIndex=" + searchIndex + "&searchLimit=" + searchLimit + "&selectNum=" + selectNum;
	        imgWin.focus();
	        
	        //alert("searchTheme: " + searchTheme + "   searchMethod: " + searchMethod);
		}

		break;
		
    case "PP_POPUP":

		searchMethod = "PP_POPUP";

		//console.log("section: " + sectionSales +", township: "+ townshipSales +", range: "+ rangeSales +", neighborhood: "+ neighborhoodCode +", salesFromDate: "+ salesFromDate +", salesToDate: "+ salesToDate +", salesFromPrice: "+ salesFromPrice +", salesToPrice: "+ salesToPrice);
        //var commaPosition = 0;
        //commaPosition = PP_Owner.indexOf(",");
        //var lastname = PP_Owner.substring(0,commaPosition);
        //var firstname = PP_Owner.substring(commaPosition+1);
        var NameID = sVal;

	    //dataString = lastname + "|" + firstname;
	    dataString = NameID;
	    
	    //alert("dataString: " + dataString);
	    
	    imgWin = window.open("", "imgWindow", "width="+w+", height=250, scrollbars=yes, resizable=yes, status=yes, menubar=yes, top=0, left="+(scrWidth - (w * 1.04)));
        imgWin.location = popupPPSearchFile + "?modTheme=" + "ORKA_SEARCH" + "&searchMethod=PP_POPUP" + "&dataString=" + dataString + "&searchIndex=" + searchIndex + "&searchLimit=" + searchLimit + "&selectNum=" + selectNum;
        imgWin.focus();
        
        dataString = "";
        
        //alert("searchTheme: " + searchTheme + "   searchMethod: " + searchMethod);

		break;
		
		case "PP_ADDRESS":
            var ppstrnum = dojo.byId("ppstrnum").value;
            var ppstrnumEndRange = dojo.byId("ppstrnumEndRange").value;
            var ppstrprefix = dojo.byId("ppstrprefix").value;
            var ppstrname = dojo.byId("ppstrname").value;
            var ppstrsuffix = dojo.byId("ppstrsuffix").value;

            //alert(strnum +", "+ strname +", "+ strsuffix);

            if (ppstrname == "") {
                alert("Please enter a street name.");
                return false;
            } else {
                if (ppstrnum == "") ppstrnum = "*";
                if (ppstrnumEndRange == "") ppstrnumEndRange = "*";
                if (ppstrprefix == "") ppstrprefix = "*";
                if (ppstrsuffix == "") ppstrsuffix = "*";
                dataString = ppstrnum + "|" + ppstrnumEndRange + "|" + ppstrprefix + "|" + ppstrname + "|" + ppstrsuffix;
            }
            
            break;

    case "PPADDR_POPUP":

		searchMethod = "PPADDR_POPUP";

		//console.log("section: " + sectionSales +", township: "+ townshipSales +", range: "+ rangeSales +", neighborhood: "+ neighborhoodCode +", salesFromDate: "+ salesFromDate +", salesToDate: "+ salesToDate +", salesFromPrice: "+ salesFromPrice +", salesToPrice: "+ salesToPrice);
        //var commaPosition = 0;
        var AddressID = sVal;

	    dataString = AddressID;
	    
	    //alert("dataString: " + dataString);
	    
	    imgWin = window.open("", "imgWindow", "width="+w+", height=250, scrollbars=yes, resizable=yes, status=yes, menubar=yes, top=0, left="+(scrWidth - (w * 1.04)));
        imgWin.location = popupPPAddrSearchFile + "?modTheme=" + "ORKA_SEARCH" + "&searchMethod=PPADDR_POPUP" + "&dataString=" + dataString + "&searchIndex=" + searchIndex + "&searchLimit=" + searchLimit + "&selectNum=" + selectNum;
        imgWin.focus();
        
        dataString = "";
        
        //alert("searchTheme: " + searchTheme + "   searchMethod: " + searchMethod);

		break;

        case "SALES_SEARCH":
            var somethingChecked = false;
            var f = document.forms[0];
            var id = "";
            var typ = "";
            var val = "";

            for (var i = 0; i < f.elements.length; i++) {
                if (f.elements[i].id.indexOf("h_") > -1 && f.elements[i].id.indexOf("_sale_") > -1) {
                    typ = f.elements[i].type;
                    id = f.elements[i].id;
                    val = f.elements[i].value;
                    if (val == "")
                        val = 0;
                    dataString += val + "|";
                    if (id.indexOf("chkbx") > -1 && val == "true")
                        somethingChecked = true;
                    //alert(typ +", "+ id +", "+ val);
                }
            }

            if (!somethingChecked) {
                alert("Please check a Sales Search criteria.");
                dataString = "";
            }

            break;
            
        case "PERSONAL_PROPERTY":
            var lastname = dojo.byId("ppLastName").value;
            var firstname = dojo.byId("ppFirstName").value;
            var nameID = dojo.byId("ppNameID").value;

            //console.log("lastname: " + lastname +", firstname: "+ firstname);

            if (lastname == "" && nameID == "") {
                alert("Please enter a last name or name ID.");
                return false;
            } else {
                if (lastname == "") lastname = "*";
                //dataString = lastname + "|" + firstname;
                
                if (firstname == "") firstname = "*";
                //dataString = lastname + "|" + firstname;
                
                if (nameID == "") nameID = "*";
                
                
                dataString = lastname + "|" + firstname + "|" + nameID;
            }

            break;

        // Parcel_ID or Map Input.     
        default:
            var pid1 = "";

            if (mapPids != "") {
                dataString = mapPids;
                //alert(mapPids + "\n" + dojo.byId("h_pid1Input").value + "\n" + dojo.byId("h_pid1").value);

            } else {
                if (dojo.byId("pid1Input") == null) {
                    pid1 = dojo.byId("h_pid1").value;
                }
                else {
                    pid1 = dojo.byId("pid1Input").value;
                    dojo.byId("h_pid1Input").value = pid1;
                }

                //alert(pid1 + "\n" + dojo.byId("h_pid1Input").value + "\n" + dojo.byId("h_pid1").value);

                if (pid1 == "") {
                    alert("Please enter more information.");
                    return false;
                } else {
                    //alert("pid1: " + pid1);
                    dataString = pid1;
                }
            }

            break;
    }

    if (logLevel >= 4)
        alert("function fetchSearchResults alert 2\nsearchMethod: " + searchMethod + "\nsearchTheme: " + searchTheme + "\nmapPids: " + mapPids);

    // QUERY TIME!!!
    // alert(dataString);
    // Function sendAjaxRequest is in ajax1.js.
    if (dataString != "") {
        togglePage('', '', 'hidden', 'hidden', 'hidden', '', '', '', '', 'visible', 'hidden', 'Please wait ', 'fetchSearchResults');
        sendAjaxRequest(searchTheme, searchMethod, dataString, searchIndex, sLim, selectNum, treenode1, username, password, browser);
    }
}

function displaySearchResults(searchResults, pid1, dspCount, totCount) {
    if (logLevel >= 2)
        alert("function displaySearchResults\npid1: " + pid1 + "\ndspCount: " + dspCount + "\ntotCount: " + totCount + "\nsearchResults: " + searchResults);

    // searchCount is a global variable that controls Incrementation.
    searchCount = Number(totCount);

    togglePage('', '', 'visible', 'hidden', 'hidden', '', '', '', '', 'hidden', 'hidden', searchResults, 'displaySearchResults');


    if (initApp) {
        //var sr = dojo.byId("searchResults");
        //dojo.parser.parse(sr);
        initApp = false;
    }

    // Fragment Identifier to pop the page back up to the top where it typically belongs.
    document.location.href = "#top2";

    if ((pid1 != null && pid1 != "")) {
        window.clearTimeout(reloadTimer);

        if (dojo.byId("h_searchTheme").value == "Map") {
            if (mapPids == "") {
                if (pid1.indexOf("|") > -1)
                    mapPids = pid1;

                fetchThemeResults(pid1);
            }
        }
        else {
            if (searchCount == 1) {
                //alert(pid1);
                // Although h_pid1 is handled in fetchThemeResults, we set it
                // here too because we might be dealing with Comparables.
                dojo.byId("h_pid1").value = pid1;

                // TIMEOUT is required for C#.NET application, but not Java.
                window.clearTimeout(reloadTimer);
                reloadTimer = window.setTimeout("fetchThemeResults('" + pid1 + "','99')", timer);
            }
        }
    }

}

function fetchThemeResults(pid1, selectNum) {
    // Fragment Identifier to pop the page back up to the top where it typically belongs.
    document.location.href = "#top1";

    /////////////////////////////////////////////////////
    /////////////////////////////////////////////////////
    /////////////////////////////////////////////////////
    // This is a rather critical block of code.  If the user  
    // is requesting a photo of a Comparable Property, we
    // don't want to change the internal PID (i.e., h_pid1).
    // See default case below.
    /////////////////////////////////////////////////////
    //console.log("in");
    console.log("pid1: " + pid1);

    var treeNode1 = dojo.byId("h_treeNode1").value;
    
    var username = dojo.byId("h_username").value;
    var password = dojo.byId("h_password").value;
    var browser = dojo.byId("h_browser").value;

    if (pid1 == null || pid1 == "") {
        pid1 = dojo.byId("h_pid1").value;

        //KJ edit- editing code regarding County code
        if (pid1.substring(0, 3) != countyCode) {
            pid1 = countyCode + pid1;
        }
    } else if (selectNum == "99" && (searchTheme.toUpperCase().indexOf("PROPERTY_PHOTO") == 0 || searchTheme.toUpperCase().indexOf("ORKA_SEARCH") == 0)) {
        dojo.byId("h_searchTheme").value = "General";
        dijit.byId("General").attr("checked", true);
    } else {
        if (searchTheme.toUpperCase().indexOf("COMP") == -1 && searchTheme.toUpperCase().indexOf("PROPERTY_PHOTO") == -1)
            dojo.byId("h_pid1").value = pid1;
    }
    //console.log("pid1: " + pid1 +"\n"+ dojo.byId("h_pid1").value);

    // Setting the global searchTheme in this manner is necessary if
    // this function is being called immediately after a ORKA_SEARCH.
    searchTheme = dojo.byId("h_searchTheme").value;
    console.log('AI TEST ' + searchTheme);

    if (searchTheme.toUpperCase() == "COMP_PHOTO") {
        searchTheme = "Property_Photo";
        dojo.byId("h_searchTheme").value = searchTheme;
    }

    var modTheme = searchTheme + "_Theme";

    /////////////////////////////////////////////////////
    /////////////////////////////////////////////////////
    /////////////////////////////////////////////////////

    // A value within selectNum indicates a Building Sketch number - 1, 2, 3 etc.
    if (selectNum == null || selectNum == "" || Number(selectNum) == 99)
        selectNum = 1;

    if (logLevel >= 2)
        alert("function fetchThemeResults\nPid1: " + pid1 + "\nsearchTheme: " + searchTheme + "\nmodTheme: " + modTheme);

    switch (searchTheme.toUpperCase()) {
        case "PROPERTY_REC._CARD":
        case "TAXES":
            // TBD...
            break;

        case "MAP":
            //alert(pid1);
            //highlightBtn('buttonOver', searchTheme, 'over', 'theme', 'buttonOut');
            loadMap(pid1, searchTheme);
            break;

        case "HELP":
            loadHelp();
            break;
            
        case "Full_Report":
            loadFullReport();
            break;

        case "BUILDING_SKETCH_POPUP":
            if (popupSkt) {
                //modTheme = searchTheme.toUpperCase() + "_POPUP";
                imgWin = window.open("", "imgWindow", "width=" + w + ", height=" + h + ", scrollbars=yes, resizable=yes, status=yes, menubar=yes, top=0, left=" + (scrWidth - (w * 1.04)));
                imgWin.location = popupFile + "?modTheme=" + searchTheme + "&searchMethod=Parcel_ID" + "&dataString=" + pid1 + "&searchIndex=" + searchIndex + "&searchLimit=" + searchLimit + "&selectNum=" + selectNum;
                imgWin.focus();

            } else {
                togglePage('hidden', 'hidden', '', '', '', 'hidden', 'hidden', 'hidden', 'hidden', 'hidden', 'visible', 'Please wait ', 'fetchThemeResults_bldgskt');
                sendAjaxRequest(modTheme, "Parcel_ID", pid1, searchIndex, searchLimit, selectNum, treeNode1, username, password, browser);
            }
            break;

        case "PROPERTY_PHOTO":
            //alert(modTheme);
            //highlightBtn('buttonOver', searchTheme, 'over', 'theme', 'buttonOut');

            if (popupPic) {
                //modTheme = searchTheme.toUpperCase() + "_POPUP";
                imgWin = window.open("", "imgWindow", "width=" + w + ", height=" + h + ", scrollbars=yes, resizable=yes, status=yes, menubar=yes, top=0, left=" + (scrWidth - (w * 1.04)));
                imgWin.location = popupFile + "?modTheme=" + modTheme + "&searchMethod=Parcel_ID" + "&dataString=" + pid1 + "&searchIndex=" + searchIndex + "&searchLimit=" + searchLimit + "&selectNum=" + selectNum;
                imgWin.focus();

            } else {
                togglePage('hidden', 'hidden', '', '', '', 'hidden', 'hidden', 'hidden', 'hidden', 'hidden', 'visible', 'Please wait ', 'fetchThemeResults_photo');
                sendAjaxRequest(modTheme, "Parcel_ID", pid1, searchIndex, searchLimit, selectNum, treeNode1, username, password, browser);
            }
            break;

        default:
            togglePage('hidden', 'hidden', '', '', '', 'hidden', 'hidden', 'hidden', 'hidden', 'hidden', 'visible', 'Please wait ', 'fetchThemeResults_default');

            // If we've gotten this far, it is now appropriate to 
            // change the internal PID, regardless of the searchTheme.
            dojo.byId("h_pid1").value = pid1;
            sendAjaxRequest(modTheme, "Parcel_ID", pid1, searchIndex, searchLimit, selectNum, treeNode1, username, password, browser);

            break;
    }
}

function displayThemeResults(searchResults, pid1, src) {
    var dateStamp = getDateStamp();
    var browser = dojo.byId("h_browser").value;

    if (logLevel >= 2)
        alert("function displayThemeResults\nsrc: " + src + "\nsearchTheme: " + searchTheme);

    var htmlTxt = '<TABLE border="0" width="98%" height="100%" cellspacing="0" cellpadding="0">\n';
    htmlTxt += '<TR>';
    htmlTxt += '<TD class="themeHdrTD">&nbsp;';
    switch (src.toUpperCase()) {
        case "PROPERTY_PHOTO_THEME":
            htmlTxt += replaceUnderscore(searchTheme);
            //htmlTxt += " " + dojo.byId("h_pid1").value;
            break;
        case "COMPARABLES_THEME":
            htmlTxt += "Comparable Properties";
            break;
        case "SALES_SEARCH_FORM":
            htmlTxt += "Sales Search";
            break;
        default:
            htmlTxt += replaceUnderscore(searchTheme) + " Information";
            break;
    }
    htmlTxt += '</TD>';
    htmlTxt += '<TD class="timeHdrTD">';
    htmlTxt += dateStamp;
    htmlTxt += '&nbsp;</TD>';
    htmlTxt += '</TR>';
    htmlTxt += '<TR>';
    htmlTxt += '<TD colspan="2">';
    htmlTxt += searchResults;
    htmlTxt += '</TD>';
    htmlTxt += '</TR>';
    htmlTxt += '</TABLE>';

    togglePage('hidden', 'visible', '', '', '', 'hidden', 'hidden', 'hidden', 'hidden', 'hidden', 'hidden', htmlTxt, src);
}

/////////////////////////////////////////////////////
// onClick event for Theme Buttons, Map Links and Small Photo.
/////////////////////////////////////////////////////
function setTheme(btn, pid1, compPids) {
    // Fragment Identifier to pop the page back up to the top where it typically belongs.
    document.location.href = "#top1";

    var btn_orig = "";
    var username = dojo.byId("h_username").value;
    var password = dojo.byId("h_password").value;
    var browser = dojo.byId("h_browser").value;

    if (pid1 == null || pid1 == "")
        pid1 = dojo.byId("h_pid1").value;

    // Normally, clicking the radio button takes care of this,
    // but this might have come from the "locate on Map" or 
    // the "click Image" links on the Results.
    if (btn == "Residential_Detail" || btn == "Commercial_Detail" || btn == "Commercial_Detail_Sub" || btn == "Improvement_Detail" || btn == "Manufactured_Home_Detail" || btn == "Report") {
        dojo.byId("h_treeNode1").value = pid1;
        pid1 = dojo.byId("h_pid1").value;
        treeNode1 = dojo.byId("h_treeNode1").value;
    } else if (btn == "Login") {
        btn_orig = btn;
        btn = "General";
        dojo.byId("h_treeNode1").value = "%";
        treeNode1 = "%";
        dijit.byId(btn).attr("checked", true);
    } else if (btn == "Property_Photo") {
        dojo.byId("h_treeNode1").value = "%";
        treeNode1 = "%";
        dojo.byId("h_PropertyID").value = pid1;
    } else if (btn == "Tax_Detail") {
        dojo.byId("h_treeNode1").value = pid1;
        pid1 = dojo.byId("h_pid1").value;
        treeNode1 = dojo.byId("h_treeNode1").value;
    } else {
        dojo.byId("h_treeNode1").value = "%";
        treeNode1 = "%";
        dijit.byId(btn).attr("checked", true);
    }

    if (logLevel >= 2)
        console.log("function setTheme\nButton: " + btn + "\nsearchTheme: " + searchTheme + "\nPid1: " + pid1);

    switch (btn.toUpperCase()) {
        case "ABOUT_ORKA":
            alert("About ORKA!");
            break;

        case "MAP":
        case "COMP_MAP":
            searchTheme = "Map";
            if (btn.toUpperCase() == "COMP_MAP")
                mapPids = compPids;

            dojo.byId("h_searchTheme").value = searchTheme;
            //highlightBtn('buttonOver', "Map", 'over', 'theme', 'buttonOut');
            togglePage('hidden', 'hidden', '', '', '', 'visible', 'visible', 'hidden', 'hidden', 'hidden', 'hidden', '', 'setTheme_map');
            if (btn.toUpperCase() == "MAP")
                loadMap(pid1, btn);
            else
                loadMap(mapPids, btn);
            break;

        case "HELP":
            loadHelp();
            break;
            
        case "FULL_REPORT":
            loadFullReport();
            break;

        default:
            //alert(pid1);
            searchTheme = btn;
            dojo.byId("h_searchTheme").value = searchTheme;

            if (btn == "Residential_Detail" || btn == "Commercial_Detail" || btn == "Commercial_Detail_Sub" || btn == "Improvement_Detail" || btn == "Property_Photo" || btn == "Manufactured_Home_Detail" || btn == "Report") {
                fetchThemeResults(pid1);
            }
            else if (btn_orig == "Login") {
                if (pid1 == "") {
                    //setLogin(dojo.byId("h_username").value, dojo.byId("h_password").value);
                    clearIt(2);
                }
                else if (pid1.indexOf("|") > -1) {
                    //setLogin(dojo.byId("h_username").value, dojo.byId("h_password").value);
                }
                else {
                    //setLogin(dojo.byId("h_username").value, dojo.byId("h_password").value);
                    fetchThemeResults(pid1);
                }
            }
            else {
                if (pid1 == "" || pid1.indexOf("|") > -1) {
                    alert("You must establish a Subject Property prior to using this option");
                    if (pid1 == "")
                        clearIt(2);
                }
                else {
                    fetchThemeResults(pid1);
                }
            }
            break;
    }
}

function loadMap(pid1, src) {
    //alert("function loadMap: " + pid1);
    console.log("function loadMap: " + pid1);
    if (pid1 == null || pid1 == "") {
        pid1 = dojo.byId("h_pid1").value;
    }

    //var pid1 = "39045A32700";
    //console.log("pid1: " + pid1);
    executeMapQuery(pid1, src);
}

function showPic(arg) {
    //alert(arg);
    dojo.byId("picLargeImg").src = arg;
}

function fetchSketch(arg) {
    // See resetSpecificButton for explanation.
    dojo.byId("h_sketchNumber").value = Number(arg);
    fetchThemeResults("", Number(arg));
}

////////////////////////////////////////////////////////////
// Functions for Incremental Arrows.
////////////////////////////////////////////////////////////
function incrRecords(arg) {
    //alert(arg);
    switch (arg) {
        case "bof":
            searchIndex = 1;
            fetchSearchResults(null, searchIndex, searchLimit);
            break;
        case "prev":
            searchIndex = ((Number(searchIndex) - 1) - Number(searchLimit));
            if (searchIndex < 1) searchIndex = 1;
            fetchSearchResults(null, searchIndex, searchLimit);
            break;
        case "next":
            searchIndex = ((Number(searchIndex) + 1) + Number(searchLimit));
            if (searchIndex > searchCount) searchIndex = 1;
            fetchSearchResults(null, searchIndex, searchLimit);
            break;
        default:
            searchIndex = (Number(searchCount) - Number(searchLimit));
            //alert(searchIndex +'\n'+ searchCount);
            fetchSearchResults(null, searchIndex, searchLimit);
            break;
    }
}

function displaySearchListResults(searchResults, pid1, theme) {

    console.log(pid1 + "---" + theme + "---" + searchResults);

    var htmlTxt = '<TABLE border="0" width="100%" height="100%" cellspacing="0" cellpadding="0">\n';
    htmlTxt += '<TR><TD class="themeHdrTD">&nbsp;';
    htmlTxt += '</TD></TR>';
    htmlTxt += '<TR><TD>';
    htmlTxt += searchResults;
    htmlTxt += '</TD></TR>';
    htmlTxt += '</TABLE>';

    if (htmlTxt != "") {
        document.getElementById("searchPanePopup").innerHTML = htmlTxt;

    }

}

////////////////////////////////////////////////////////////

function reloadSearchTools() {
    var srchBtn = dojo.byId("h_searchButton").value;
    switch (srchBtn) {
        case "Sales_Search":
            togglePage('', '', 'hidden', 'visible', 'hidden', '', '', '', '', 'hidden', 'hidden', '', 'reloadSearchTools_Sales_Search');
            break;
        default:
            togglePage('', '', 'hidden', 'visible', 'visible', '', '', '', '', 'hidden', 'hidden', '', 'reloadSearchTools_default');
            break;
    }

    if (connectMouseOver != null) {
        dojo.disconnect(connectMouseOver);
        connectMouseOver = null;
    }

    mapPids = "";

    focusDelay(srchBtn);
}

function togglePage(helpStatus, themeResultsStatus, searchResultsStatus, searchMethodButtonsStatus, searchInputsStatus, mapStatus, mapStructureStatus, sketchStatus, sketchStructureStatus, searchMessageStatus, resultsMessageStatus, htmlTxt, src) {
    console.log("helpStatus: " + helpStatus + ", themeResultsStatus: " + themeResultsStatus + ", searchResultsStatus: " + searchResultsStatus + ", searchMethodButtonsStatus: " + searchMethodButtonsStatus + ", searchInputsStatus: " + searchInputsStatus + ", mapStatus: " + mapStatus + ", mapStructureStatus: " + mapStructureStatus + ", sketchStatus: " + sketchStatus + ", sketchStructureStatus: " + sketchStructureStatus + ", searchMessageStatus: " + searchMessageStatus + ", resultsMessageStatus: " + resultsMessageStatus + ", src: " + src);

    var boolPrintSketch = dojo.byId("h_printSketch").value;
    //console.log("dojo.byId('h_printSketch'): " + boolPrintSketch);

    /*
    Status variables are passed in as 'visible' or 'hidden'.
    As of JS API 2.0 visibility of all dojo components is controlled by esri.show and esri.hide.
    This was done due to changes to the TabContainer.
    As appropriate, some elements get populated wih htmlTxt.
    */

    if (dojo.byId("h_printSketch").value == 'true' && sketchStatus == 'visible' && sketchStructureStatus == 'visible') {
        if (sketchStatus != '') {
            if (dojo.byId("agsSketch") != null)
                dojo.byId("agsSketch").style.visibility = sketchStatus;
        }
        if (sketchStructureStatus != '') {
            if (dojo.byId("agsSketchStructure") != null)
                dojo.byId("agsSketchStructure").style.visibility = sketchStructureStatus;
        }

        //alert("here");
    } else {
        if (mapStatus != '') {
            if (dojo.byId("agsMap") != null) {
                dojo.byId("agsMap").style.visibility = mapStatus;
                //dojo.byId("agsMap").style.overflow = "hidden";
                console.log(dojo.byId("agsMap").style.visibility);
            }
            if (mapLayer1 != null) {
                if (mapStatus == 'hidden')
                    mapLayer1.hide();
                else
                    mapLayer1.show();
            }
            if (mapLayer2 != null) {
                if (mapStatus == 'hidden')
                    mapLayer2.hide();
                else
                    mapLayer2.show();
            }
            if (mapLayer3 != null) {
                if (mapStatus == 'hidden')
                    mapLayer3.hide();
                else
                    mapLayer3.show();
            }
            if (mapLayer4 != null) {
                if (mapStatus == 'hidden')
                    mapLayer4.hide();
                else
                    mapLayer4.show();
            }            
        }

        if (mapStructureStatus != '') {
            if (dojo.byId("agsMapStructure") != null) {
                dojo.byId("agsMapStructure").style.visibility = mapStructureStatus;
                //dojo.byId("agsMapStructure").style.overflow = "hidden"
                //console.log(dojo.byId("agsMapStructure").style.visibility);
            }
        }
        if (helpStatus != '') {
            if (dojo.byId("helpContent") != null)
                dojo.byId("helpContent").style.visibility = helpStatus;
        }
        if (themeResultsStatus != '') {
            if (dojo.byId("themeResults") != null)
                dojo.byId("themeResults").style.visibility = themeResultsStatus;
        }
        if (searchResultsStatus != '') {
            if (dojo.byId("searchResults") != null)
                dojo.byId("searchResults").style.visibility = searchResultsStatus;
        }
        if (searchMethodButtonsStatus != '') {
            if (dojo.byId("searchMethodButtons") != null) {
                switch (searchMethodButtonsStatus) {
                    case "visible":
                        esri.show(dojo.byId("searchMethodButtons"));
                        break;
                    default:
                        esri.hide(dojo.byId("searchMethodButtons"));
                        break;
                }
            }
        }
        if (sketchStatus != '') {
            if (dojo.byId("agsSketch") != null) {
                dojo.byId("sketchLegend").style.visibility = sketchStatus;
                dojo.byId("agsSketch").style.visibility = sketchStatus;
            }
        }
        if (sketchStructureStatus != '') {
            if (dojo.byId("agsSketchStructure") != null) {
                dojo.byId("agsSketchStructure").style.visibility = sketchStructureStatus;
            }
        }
        if (searchMessageStatus != '') {
            if (dojo.byId("searchMessageContent") != null)
                dojo.byId("searchMessageContent").style.visibility = searchMessageStatus;
        }
        if (resultsMessageStatus != '') {
            if (dojo.byId("resultsMessageContent") != null)
                dojo.byId("resultsMessageContent").style.visibility = resultsMessageStatus;
        }
        if (htmlTxt != "") {
            if (helpStatus == 'visible' && dojo.byId("helpContent") != null) {
                dojo.byId("helpContent").innerHTML = htmlTxt;
            }
            if (themeResultsStatus == 'visible' && dojo.byId("themeResults") != null) {
                dojo.byId("themeResults").innerHTML = htmlTxt;
            }
            if (searchResultsStatus == 'visible' && dojo.byId("searchResults") != null) {
                dojo.byId("searchResults").innerHTML = htmlTxt;
            }
            if (searchMessageStatus == 'visible' && dojo.byId("searchMessageContent") != null) {
                dojo.byId("searchMessageContent").innerHTML = "<img src='orka/images/processing.gif' border='0' /> " + htmlTxt;
            }
            if (resultsMessageStatus == 'visible' && dojo.byId("resultsMessageContent") != null) {
                dojo.byId("resultsMessageContent").innerHTML = "<img src='orka/images/processing.gif' border='0' /> " + htmlTxt;
            }
        }
    }
}

function unloadDojo() {

}

