var gIntMaxVal = 2147483647;

var subjectPin;

function SetChecked() {
    var selected = document.getElementById('hdSelected').value;
    var chkarray = document.getElementById('chkPin');
    if (chkarray == null) {
        return;
    }
    var len = chkarray.length;
    if (len == null) {
        chkarray = new Array(chkarray);
        len = chkarray.length;
    }
    for (i = 0; i < len; i++) {
        var s = chkarray[i].value;
        if (selected.search(s) != -1) {
            chkarray[i].checked = true;
        }
    }
}
function SelectPage(val) {
    var doc = document.all;
    var chkarray = doc.item('chkPin');
    if (chkarray == null) {
        return;
    }
    var len = chkarray.length;
    if (len == null) {
        chkarray = new Array(chkarray);
        len = chkarray.length;
    }
    for (j = 0; j < chkarray.length; j++) {
        chkarray[j].checked = val;
    }
}
function SelectAll(val) 
{
    var wait = document.getElementById("div_wait");
    if (wait != null) 
    {
        wait.style.display = "block";
    }
    /*
    if (val == true) {
        frmMainSubmit("SelectAll");
    }
    else {
        var doc = document.all.hdSelected;
        if (doc != null) {
            doc.value = "";
            SelectPage(false);
        }
        frmMainSubmit("DeselectAll");
    }
    */
    var formId = document.forms[0].id;
    if (formId != null) 
    {
        var form = document.getElementById(formId);
        if (form != null) 
        {
            var action = document.getElementById("hdAction");
            if (action != null)
            {
                if (val == true) {
                    action.value = "SelectAll";
                }
                else {
                    var selected = document.getElementById("hdSelected");
                    if (selected != null) {
                        selected.value = "";
                    }
                    action.value = "DeselectAll";
                }
            }
            form.submit();
        }
    }
}
function NewList(obj) {
    if (obj.value != "-1") {
        location = "../Search/ParcelsList.aspx?ListType=" + document.all.hdListType.value + "&ListID=" + obj.value;
    }
}
function CountSelection() {
    var result = 0;
    var doc = document.all;
    var chkarray = doc.item('chkPin');
    if (chkarray == null) {
        return 0;
    }
    var i = 0;
    var len = chkarray.length;
    if (len == null) {
        chkarray = new Array(chkarray);
        len = chkarray.length;
    }
    for (j = 0; j < chkarray.length; j++) {
        if (chkarray[j].checked == true) {
            ++result;
        }
    }
    return result;
}
var DLG_NEWLIST = "center:yes; dialogHeight:150px; dialogWidth:400px; help:no; resizable:yes; status:no; title:no";
var DLG_SIZE = "center:yes; dialogHeight:270px; dialogWidth:600px; help:no; resizable:yes; status:no; title:no";
var DLG_APPENDTOLIST = "center:yes; dialogHeight:270px; dialogWidth:600px; help:no; resizable:yes; status:no; title:no";
var DLG_APPENDTOBUFFER = "center:yes; dialogHeight:250px; dialogWidth:500px; help:no; resizable:no; status:no; title:Buffer";

function PinSelected(pin) {
    location.href = "../Datalet/Datalets.aspx?idx=2";
}

function CollectParcels() {
    var pins = GetCheckedIds();
    for (i = 0; i < pins.length; i++) {
        var pin = pins[i];
        var xml_list = getElement("xml_list_MainTable");
        var xpath = "//.[@id='" + pin + "']/ParcelID";
        var node = xml_list.selectSingleNode(xpath);
        if (node != null) {
            pins[i] = node.text;
        }
    }
    getElement('ParIDs').value = pins.join(",");
}

function IsChecked() {
    var checkCount = 0;
    var bResult = false;
    var Val = "";
    var chks = document.all("chkCheck");
    if (chks.length == 1) {
        if (chks.checked == true) {
            checkCount = 1;
        }
    }
    else {
        var e = new Enumerator(chks);
        for (; !e.atEnd(); e.moveNext()) {
            var el = e.item();
            if (el.checked == true) {
                ++checkCount;
                Val = el.value;
            }
        }
    }
    bResult = (checkCount > 0) ? true : false;
    return bResult;
}


function Process(action) {
    var pinCount = setSelection();
    if (pinCount == 0) {
        alert("Please select parcel(s)");
        return;
    }
    else if (action == "Compare") {
        if (document.all.comparableMax != null) {
            if (CountSelection() > document.all.comparableMax.value) {
                alert("Please select a maximum of " + document.all.comparableMax.value + " sales for comparables.");
                return;
            }
            else if (CountSelection() < document.all.comparableMin.value) {
                alert("Please select a minimum of " + document.all.comparableMin.value + " sale(s) for comparables.");
                return;
            }
        }
    }

    var url = "";
    document.all.hdAction.value = action;
    IframeReload();
}

function IframeReload(fromMap) {
    var action = document.getElementById('hdAction').value;
    var ListType = document.getElementById('hdListType').value;
    var sIndex = document.getElementById('hdIndex').value;
    var doc = document.all; // test
    if (fromMap && fromMap == "yes") {
        ListType = "PA";
    }
    else {
        try {
            if (mode != null && (mode == "SALES" || mode == "COMPSALES")) {
                ListType = "SA";
            }
            else if (mode != null && (mode == "OWNER" || mode == "ADVANCED")) {
                ListType = "PA-PP";
            }
            else if (mode != null && mode == "PERSPROP") {
            ListType = "PP";
            }
        }
        catch (e) {
            ListType = "PA";
        }
    }
    if (doc == null) {
        doc = document.frmMain;
    }
    if (doc == null) {
        doc = document.frmMain;
    }
    switch (action) {
        case "OpenInAnalyze":
            var scenId = ListType == "SA" ? gIntMaxVal - 1 : gIntMaxVal;
            var win = PopupWindow("../ianalyzev2/IAHome.aspx?resetData=1&scenId=" + scenId, true, "iAnalyzeV2");
            if (win != null) win.focus();
            break;
        case "SaveNewList":
            url = "../Search/SaveNewList.aspx?frameset=1&ListType=" + ListType + "&sIndex=" + sIndex + "&fromMap=" + fromMap;
            ret = window.showModalDialog(url, null, DLG_NEWLIST);
            if (ret != null) {
                if (ret == "size") {
                    url = "../Search/ListSizeWarning.aspx?frameset=1&action=" + "CreateAppendPins";
                    ret = window.showModalDialog(url, null, DLG_SIZE);
                    if (ret == "cancel") {
                        return;
                    }
                }
                if (ret == "Ok") {
                    alert("Your new list was successfully created");
                }
                else if (ret.indexOf(",") != -1) {
                    var msg = ""
                    var vals = ret.split(",");
                    if (vals[0] == "OK") {
                        msg = vals[2];
                        if (vals[1] == "create") {
                            msg += " parcels saved in the list";
                        }
                        else {
                            msg += " parcels appended to the list";
                        }
                        alert(msg);
                        return;
                    }
                    else {
                        alert(ret);
                    }
                }
                else if (ret && ret != "Cancel") {
                    alert(ret);
                }
            }
            break;
        case "AppendToList":
            url = "../Search/AppendToList.aspx?frameset=1&ListType=" + ListType + "&sIndex=" + sIndex + "&fromMap=" + fromMap;
            var ret = window.showModalDialog(url, null, DLG_APPENDTOLIST);
            if (ret == "") {
                return;
            }
            else if (ret === null) {
                alert("Error to append to the list");
                return;
            }
            if (ret == "size") {
                url = "../Search/ListSizeWarning.aspx?frameset=1&action=" + "AppendPins";
                ret = window.showModalDialog(url, null, DLG_SIZE);
            }
            if (ret == "NO") {
                alert("Selected parcel(s) already in the list");
            }
            else if (ret == "OK") {
                alert("Selected parcel(s) appended to the list");
            }
            else if (ret.indexOf("Warning:") != -1) {
                alert(ret);
                return;
            } else if (ret.indexOf(":") != -1) {
                var parts = ret.split("|");
                var msg = "The selected parcel(s) have been appended to list " + parts[0] + ".\n\r" +
                "Note - any duplicate records or records that were already in the list will not get appended.\n\r" +
                "Parcels not saved: " + ret;
                alert(msg);
            }
            break;
        case "AppendToArcGISBuffer":
            url = "../ArcGISMaps/ArcGISBufferRecords.aspx?frameset=1&ListType=" + ListType + "&sIndex=" + sIndex + "&fromMap=" + fromMap;
            window.showModalDialog(url, null, DLG_APPENDTOBUFFER);
            break;
        case "AppendToBuffer":
            url = "../Search/BufferRecords.aspx?frameset=1&ListType=" + ListType + "&sIndex=" + sIndex + "&fromMap=" + fromMap;
            window.showModalDialog(url, null, DLG_APPENDTOBUFFER);
            break;
        case "DeleteFromList":
            document.getElementById('hdAction').value = "DeleteFromList";
            document.frmMain.submit();
            break;
        case "SaveSalesList":
            var url = "../Search/SaveSalesList.aspx?frameset=1&sIndex=" + sIndex;
            window.showModalDialog(url, null, DLG_NEWLIST);
            break;
        case "ViewMap":
            var url = "../Maps/MapAdvanced.aspx?sIndex=" + sIndex;
            if (SearchMode != null && SearchMode != "") {
                url = url + "&SearchMode=" + SearchMode;
                if (SearchMode == "CompSales") {
                    url = url + "&pageType=Comparables";
                    url = url + "&subjectPin=" + subjectPin;
                }
            }
            document.getElementById('hdLink').value = url;
            document.getElementById('hdAction').value = "Link";
            if (document.frmMain != null) document.frmMain.submit();
            else window.location.href = url;
            break;
        case "ViewArcGISMap":
            var url = "../ArcGISMaps/ArcGISMapAdvanced.aspx?sIndex=" + sIndex;
            if (SearchMode != null && SearchMode != "") {
                url = url + "&SearchMode=" + SearchMode;
                if (SearchMode == "CompSales") {
                    url = url + "&pageType=Comparables";
                }
            }
            if (subjectPin != null) {
                url = url + "&subjectPin=" + subjectPin;
            }
            document.getElementById('hdLink').value = url;
            document.getElementById('hdAction').value = "Link";
            if (document.frmMain != null) document.frmMain.submit();
            else window.location.href = url;
            break;
        case "ViewArcGISJSMap":
            var url = "../Maps/MapAdv.aspx?sIndex=" + sIndex;
            if (SearchMode != null && SearchMode != "") {
                url = url + "&SearchMode=" + SearchMode;
                if (SearchMode == "CompSales") {
                    url = url + "&pageType=Comparables";
                }
            }
            if (subjectPin != null) {
                url = url + "&subjectPin=" + subjectPin;
            }
            document.getElementById('hdLink').value = url;
            document.getElementById('hdAction').value = "Link";
            if (document.frmMain != null) document.frmMain.submit();
            else window.location.href = url;
            break;
        case "OverviewMap":
            var url = "../MapPoint/MapOverviewAdvanced.aspx?sIndex=" + sIndex;
            document.getElementById('hdLink').value = url;
            document.getElementById('hdAction').value = "Link";
            if (document.frmMain != null) document.frmMain.submit();
            else window.location.href = url;
            break;
        case "RouteMap":
            var url = "../MapPoint/MapRouteAdvanced.aspx?sIndex=" + sIndex;
            document.getElementById('hdLink').value = url;
            document.getElementById('hdAction').value = "Link";
            if (document.frmMain != null) document.frmMain.submit();
            else window.location.href = url;
            break;
        case "Compare":
            var url = "../Comparables/Comparables.aspx?sIndex=" + sIndex + "&pins=" + doc.hdSelected.value;
            url = url + "&subjectPin=" + subjectPin;
            window.open(url);
            break;
    }
    document.getElementById('hdAction').value = "";
}
function Refresh() {
    //	document.all.hdAction.value = "";
    //	var url = "../Search/ParcelsList.aspx?ListID=" + document.all.ListID.value;
    //	location = url;
}
var DLG_SEARCH = "center:yes; dialogHeight:450px; dialogWidth:580px; help:no; resizable:yes; status:no; title:no";
function SearchHelp() {
    var ret = window.showModelessDialog("../Search/Search_Tips.aspx", null, DLG_SEARCH);
}

