﻿//global varialble to hold owner results
var ownerResults;
var ownerResultsCount;
var ownerNameSearched;

function getOwners() {
    ownerNameSearched = document.getElementById('txtOwnerName').value;

    if (ownerNameSearched == "") {
        alert("Please eneter an owner name.");
        return;
    }

    //show the busy indicator
    showBusy();

    var params = {"ownerName":'"' + ownerNameSearched + '"'}
    dojo.xhrGet(
    {
        url: "./wsSearches.asmx/GetAddrParcelOwner_ByOwnerName",
        handleAs: "json",
        contentType: "application/json; charset=utf-8",
        content: params,
        load: OnOwnerNameComplete,
        error: function(error,args){console.warn("error!",error);}
    });
}

function OnOwnerNameComplete(results, ioArgs) {
    //parse the results into an array of JSON objects
    //debugger;
    ownerResults = dojo.fromJson(results);
    ownerResultsCount = ownerResults.length;
    
    //make sure we don't have more than 400 records
    //
    //***  IMPORTANT NOTE  ***
    //
    //As of 22 October 2008 the back end can have more
    //than 400 records in the result set.  However, the
    //list is trimmed to 401 records if more than 400
    //are returned.  A simple check on returned results
    //will let us know if we have more than 400 and we
    //can notify the user.  Future backend code may contain
    //an error object that will note when 400 record limit
    //has been exceded.
    //
    if (ownerResultsCount > 400) {
        alert("More than 400 records were returned.  Only the first 400 results will be displayed.");
    } else if (ownerResultsCount == 0) {
        hideBusy();
        alert("Nothing found for: " + ownerNameSearched);
        return false;
    }
    displayOwners(0);
}

function displayOwners(startAt) {   
    var recsToDisplay = 15;
    
    var pagingInfo = getPagerString(startAt, ownerResultsCount, recsToDisplay, 5, "displayOwners");

    //looping control for the last record
    var lastRecord;
    if ((startAt + recsToDisplay) > (ownerResultsCount - 1)) {
        lastRecord = ownerResultsCount;
    } else {
        lastRecord = startAt + recsToDisplay;
    }

    var content = "";
    var rowClass;

    //add the pager
    content = "<table class='searchResults' width='240px' cellpadding='0' cellspacing='0'><tr><td align='left'>";
    content += pagingInfo["pageOf"];
    content += "</td><td align='right'>";
    content += pagingInfo["pageString"];
    content += "</td></tr><tr><td>&nbsp;</td></tr><tr><td colspan='2'>";
    //content += "<div style='overflow: auto; height: 100%;'>";
    content += "<table width='100%' cellpadding='0' cellspacing='0'><tr><td>";
        
    var n = 0;
    for (var i=startAt; i < lastRecord; i++) {
        //copy this parcel record to the parcels to highlight array
        var apoInfo = ownerResults[i];
        if ((i % 2) == 0) {
            rowClass = "row1";
        } else {
            rowClass = "row2";
        }
        
        content += "<table width='240' class='" + rowClass + "' cellpadding='1' cellspacing='0'>";
        content += "  <tr><td colspan='4'><img src='./images/pixel.gif' height='3' /></td></tr>";
        content += "  <tr>";
        content += "    <td colspan='4'>" + apoInfo.Owner + "</td>";
        content += "  </tr>";
        content += "  <tr>";
        content += "    <td colspan='4'>" + apoInfo.FullAddress + "</td>";
        content += "  </tr>";
        content += "  <tr>";
        content += "    <td width='192' align='left'>" + apoInfo.ParcelID + "</td>";
        content += "    <td width='16' align='center'><img style='cursor: hand;' src='./images/ViewDetails_12.png' onclick='getFullParcelInfo(\"" + apoInfo.ParcelID + "\")' /></td>";
        content += "    <td width='16' align='center'><img style='cursor: hand;' src='./images/ZoomToRecord_12.png' onclick='locateParcel(\"" + apoInfo.ParcelID + "\")' /></td>";
        content += "    <td width='16' align='center'><img style='cursor: hand;' src='./images/ViewDetailsAndZoom_12.png' onclick='locateParcel(\"" + apoInfo.ParcelID + "\");getFullParcelInfo(\"" + apoInfo.ParcelID + "\");' /></td>";
        content += "  </tr>";
        content += "  <tr><td colspan='4'><img src='./images/pixel.gif' height='3' /></td></tr>";        
        content += "</table>";
    }
    
    content += "</td></tr></table>";
    //content += "</div>";
    content += "</td></tr></table>";
    
    var resultsPane = dojo.byId("cpResults");
    resultsPane.innerHTML = content;
    
    //besure the left panel is open to display the data
    if (g_sLeftPaneState=="CLOSED") toggleLeft();
            
    dijit.byId("mainTabContainer").selectChild("tabResults");
    dijit.byId("acSearchResults").selectChild("apResults");

    //hide the busy notification
    hideBusy();
}


