﻿function DisplayCommunitySearchForm(pIsRefinement) {
    $("#tblCommunitySearch").css({ display: "block" });
    $("#tblCommunitySearchWaiting").css({ display: "none" });

    if (!pIsRefinement) {
        ResetCommunitySearchForm();
    }; //end if not refining

    $find("mpeCommunitySearch").show();
}; //end function

function DisplayCommunitySearchWhenLoaded() {
    if ($find("mpeCommunitySearch") == null) {
        setTimeout(DisplayCommunitySearchWhenLoaded, 10);
    } else {
        DisplayCommunitySearchForm();
    }; //end ifelse found
}; // this can be called and will keep calling itself until found

function CloseCommunitySearchForm() {
    $("#tblCommunitySearch").css({ display: "block" });
    $("#tblCommunitySearchWaiting").css({ display: "none" });
    $find("mpeCommunitySearch").hide();
}; //end function
function ResetCommunitySearchForm() {
    var mySubForms = ["TRANSPORTATION", "PEOPLE", "LABORFORCE", "BUDGET", "FECZONE"];
    for (var i = 0; i < mySubForms.length; i++) {
        ResetCommunitySubSearchForm(mySubForms[i]);
        UpdateCommunitySubSearchIndicator(mySubForms[i]);
    };
    return false;
}; //end functionn
function DoCommnitySearch() {
    $("#tblCommunitySearch").css({ display: "none" });
    $("#tblCommunitySearchWaiting").css({ display: "block" });

    //closes all search forms. This is neccessary because you can press from a search form and it will submit the form.
    CloseAllCommunitySubSearchForm();
    
    CallCommunitySearchService(SearchSuccess, SearchFail, 0, _SSR._PageSizeDEFAULT);

    return false;
}; //end function

function CallCommunitySearchService(pSuccessCallback, pFailureCallback, pStartRecordNumber, pEndRecordNumber) {
    var myParams = GetCommunitySearchParameters();
    myParams.StartRowID = pStartRecordNumber;
    myParams.EndRowID = pEndRecordNumber;
    myParams.SortBy = _SSR._GetSortBy("COMMUNITY");
    myParams.SortDirection = false;
    myParams.RequestID = GISPlanning_MapUtilities_GUID();
    var myRequestID = GISPlanning_MapUtilities_GUID();

    TVAGoogle.common.services.Community.GetCommunities(myParams, pSuccessCallback, pFailureCallback);
}; //end function

function CancelCommunitySearch() {
    //TODO: stop async in process
    CloseCommunitySearchForm();
}; //end method

function ShowCommunitySubSearchForm(pWhatForm) {
    var myMainSearchForm = $get("tblCommunitySearch");
    var mySubSearchForm = GetCommunitySubSearchForm(pWhatForm);


    myMainSearchForm.style.display = 'none';
    mySubSearchForm.style.display = 'block';



}; //end method


function ResetCommunitySubSearchForm(pWhatForm) {
    var myTable = GetCommunitySubSearchForm(pWhatForm);
    $(myTable).find(".resetable").each(function() { this.value = ""; });
    $(myTable).find(".checkable").each(function() { this.checked = false; });
    $(myTable).find("select").each(function() { this.options[0].selected = true; });
    Page_ClientValidate(pWhatForm.toLowerCase());
}; //end function

function CommunitySubSearchValidated(pWhatForm)
{
    var myMainSearchForm = $get("tblCommunitySearch");
    var mySubSearchForm = GetCommunitySubSearchForm(pWhatForm);

    myMainSearchForm.style.display = 'block';
    mySubSearchForm.style.display = 'none';

    //set image
    UpdateCommunitySubSearchIndicator(pWhatForm);
};//end function

function CloseCommunitySubSearchForm(pWhatForm,pIsCancel) {
    var myPageValid = Page_ClientValidate(pWhatForm.toLowerCase());
    if(myPageValid)
    {
        CommunitySubSearchValidated(pWhatForm);
    }
}; //end function

function CloseAllCommunitySubSearchForm() {
    var mySubForms = ["TRANSPORTATION", "PEOPLE", "LABORFORCE", "BUDGET", "FECZONE"];
    for (var i = 0; i < mySubForms.length; i++) {
        CloseCommunitySubSearchForm(mySubForms[i], false);
    };//end for each subform
}; //end function

function UpdateCommunitySubSearchIndicator(pWhatForm) {
    var myValueIsSet = DetermineIfCommunitySubSearchCriteriaExist(pWhatForm);
    var myButtonToUpdate = GetCommunitySubSearchFormImage(pWhatForm);
    if (myValueIsSet) {
        GISPlanning_SetImageActive(myButtonToUpdate);
    }
    else {
        GISPlanning_SetImageInactive(myButtonToUpdate);
    }; //end ifelse value set
};

function GetCommunitySubSearchFormImage(pWhatForm) {
    var myImage = null;
    switch (pWhatForm) {
        case "TRANSPORTATION":
            myImage = $get("imgTransportationButton");
            break;
        case "PEOPLE":
            myImage = $get("imgPeopleButton");
            break;
        case "LABORFORCE":
            myImage = $get("imgLaborForceButton");
            break;
        case "BUDGET":
            myImage = $get("imgBudgetButton");
            break;
        case "FECZONE":
            myImage = $get("imgFECZoneButton");
            break;
    }; //end switch

    return myImage;

}; //end function

function DetermineIfCommunitySubSearchCriteriaExist(pWhatForm) {
    var myValueIsSet = false;
    switch (pWhatForm) {
        case "TRANSPORTATION":
            myValueIsSet = (
                $get("ctl00_cphModal_tbox_CommuteTravelTime_Min").value +
                $get("ctl00_cphModal_tbox_CommuteTravelTime_Max").value +
                ($get("lboxAirports").options[0].selected == true ? "" : "SEL") +
                $get("ctl00_cphModal_tboxNearestInterstate_Min").value +
                $get("ctl00_cphModal_tboxNearestInterstate_Max").value +
                $get("ctl00_cphModal_tboxNearestRail_Min").value +
                $get("ctl00_cphModal_tboxNearestRail_Max").value != "");
            break;
        case "PEOPLE":
            myValueIsSet = (
                $get("ctl00_cphModal_tboxPopulationSize_Min").value +
                $get("ctl00_cphModal_tboxPopulationSize_Max").value +
                $get("ctl00_cphModal_tboxLaborForceSize_Min").value +
                $get("ctl00_cphModal_tboxLaborForceSize_Max").value +
                $get("ctl00_cphModal_tboxUnemploymentRate_Min").value +
                $get("ctl00_cphModal_tboxUnemploymentRate_Max").value +
                $get("ctl00_cphModal_tboxMedianAge_Min").value +
                $get("ctl00_cphModal_tboxMedianAge_Max").value != "");
            break;
        case "BUDGET":
            myValueIsSet = (
                $get("ctl00_cphModal_tboxHouseholdIncome_Min").value +
                $get("ctl00_cphModal_tboxHouseholdIncome_Max").value +
                $get("ctl00_cphModal_tboxHomeValue_Min").value +
                $get("ctl00_cphModal_tboxHomeValue_Max").value != "");
            break;
        case "LABORFORCE":
            myValueIsSet = (
                $get("ctl00_cphModal_tboxBachelorDegree_Min").value +
                $get("ctl00_cphModal_tboxBachelorDegree_Max").value +
                $get("ctl00_cphModal_tboxHighSchool_Min").value +
                $get("ctl00_cphModal_tboxHighSchool_Max").value +
                $get("ctl00_cphModal_tboxWhiteCollar_Min").value +
                $get("ctl00_cphModal_tboxWhiteCollar_Max").value +
                $get("ctl00_cphModal_tboxBlueCollar_Min").value +
                $get("ctl00_cphModal_tboxBlueCollar_Max").value +
                $get("ctl00_cphModal_tboxYoungEducated_Min").value +
                $get("ctl00_cphModal_tboxYoungEducated_Max").value +
                ($get("lboxEducation").options[0].selected == true ? "" : "SEL") != "");
            break;
        case "FECZONE":
            myValueIsSet = $get("cboxFECZone").checked == true;
            break;
    }; //end switch

    return myValueIsSet;
}; //end function

function GetCommunitySubSearchForm(pWhatForm) {
    var mySubSearchForm;

    switch (pWhatForm) {
        case "TRANSPORTATION":
            mySubSearchForm = $get("tblTransportation");
            break;
        case "PEOPLE":
            mySubSearchForm = $get("tblPeople");
            break;
        case "BUDGET":
            mySubSearchForm = $get("tblBudgets");
            break;
        case "LABORFORCE":
            mySubSearchForm = $get("tblLaborForce");
            break;
        case "FECZONE":
            mySubSearchForm = $get("tblFECZone");
            break;
    }; //end switch

    return mySubSearchForm;
};


function GetCommunitySearchParameters() {
    
    var myEntZone = ($get("cboxFECZone").checked == true ? 1 : 0)
    
    var myParams = {
        commType: 3, //hardcoded to community
        popmin: GISP_NullOrInputValue("ctl00_cphModal_tboxPopulationSize_Min"),
        popmax: GISP_NullOrInputValue("ctl00_cphModal_tboxPopulationSize_Max"),
        uermin: GISP_NullOrInputValue("ctl00_cphModal_tboxUnemploymentRate_Min"),
        uermax: GISP_NullOrInputValue("ctl00_cphModal_tboxUnemploymentRate_Max"),
        edfac: GISP_NullOrInputValue("lboxEducation"),
        airport: GISP_NullOrInputValue("lboxAirports"),
        lfsmin: GISP_NullOrInputValue("ctl00_cphModal_tboxLaborForceSize_Min"),
        lfsmax: GISP_NullOrInputValue("ctl00_cphModal_tboxLaborForceSize_Max"),
        bdpmin: GISP_NullOrInputValue("ctl00_cphModal_tboxBachelorDegree_Min"),
        bdpmax: GISP_NullOrInputValue("ctl00_cphModal_tboxBachelorDegree_Max"),
        hspMin: GISP_NullOrInputValue("ctl00_cphModal_tboxHighSchool_Min"),
        hspMax: GISP_NullOrInputValue("ctl00_cphModal_tboxHighSchool_Max"),
        hhimin: GISP_NullOrInputValue("ctl00_cphModal_tboxHouseholdIncome_Min"),
        hhimax: GISP_NullOrInputValue("ctl00_cphModal_tboxHouseholdIncome_Max"),
        medmin: GISP_NullOrInputValue("ctl00_cphModal_tboxMedianAge_Min"),
        medmax: GISP_NullOrInputValue("ctl00_cphModal_tboxMedianAge_Max"),
        dintMax: GISP_NullOrInputValue("ctl00_cphModal_tboxNearestInterstate_Max"),
        drailMax: GISP_NullOrInputValue("ctl00_cphModal_tboxNearestRail_Max"),
        yedumin: GISP_NullOrInputValue("ctl00_cphModal_tboxYoungEducated_Min"),
        yedumax: GISP_NullOrInputValue("ctl00_cphModal_tboxYoungEducated_Max"),
        homeMin: GISP_NullOrInputValue("ctl00_cphModal_tboxHomeValue_Min"),
        homeMax: GISP_NullOrInputValue("ctl00_cphModal_tboxHomeValue_Max"),
        blupctmin: GISP_NullOrInputValue("ctl00_cphModal_tboxBlueCollar_Min"),
        blupctmax: GISP_NullOrInputValue("ctl00_cphModal_tboxBlueCollar_Max"),
        whtpctmin: GISP_NullOrInputValue("ctl00_cphModal_tboxWhiteCollar_Min"),
        whtpctmax: GISP_NullOrInputValue("ctl00_cphModal_tboxWhiteCollar_Max"),
        travMin: GISP_NullOrInputValue("ctl00_cphModal_tbox_CommuteTravelTime_Min"),
        travMax: GISP_NullOrInputValue("ctl00_cphModal_tbox_CommuteTravelTime_Max"),
        entzone: myEntZone,
        SubsetToken: _GISP_Theme
    };
    return myParams;

}; //end function

function GetCommunitySearchDisplayParameters() {
    var myParams = GetCommunitySearchParameters();
    var myDisplayParams = {
        Population_Min: myParams.popmin,
        Population_Max: myParams.popmax,
        Unemplyment_Min: myParams.uermin,
        Unemplyment_Max: myParams.uermax,
        Education: (myParams.edfac != 0 ? GISP_GetDropDownSelectedDisplayValue($get("lboxEducation")) : null),
        Airports: (myParams.airport != 0 ? GISP_GetDropDownSelectedDisplayValue($get("lboxAirports")) : null),
        Labor_Force_Size_Min: myParams.lfsmin,
        Labor_Force_Size_Max: myParams.lfsmax,
        Bachelors_Degree_Min: myParams.bdpmin,
        Bachelors_Degree_Max: myParams.bdpmax,
        High_School_Degree_Min: myParams.hspMin,
        High_School_Degree_Max: myParams.hspMax,
        Household_Income_Min: myParams.hhimin,
        Household_Income_Max: myParams.hhimax,
        Median_Age_Min: myParams.medmin,
        Median_Age_Max: myParams.medmax,
        Distance_Interstate: myParams.dintMax,
        Distance_Rail: myParams.drailMax,
        Young_and_Educated_Min: myParams.yedumin,
        Young_and_Educated_Max: myParams.yedumax,
        Home_Value_Min: myParams.homeMin,
        Home_Value_Max: myParams.homeMax,
        Blue_Collar_Min: myParams.blupctmin,
        Blue_Collar_Max: myParams.blupctmax,
        White_Collar_Min: myParams.whtpctmin,
        White_Collar_Max: myParams.whtpctmax,
        Commute_Time_Min: myParams.travMin,
        Commute_Time_Max: myParams.travMax,
        Enterprize_Zone: myParams.entzone
    };

    return myDisplayParams;
}; //end function

function CancelCommunitySearch() {
    $find("mpeCommunitySearch").hide();
};


function SearchPropertiesInCommunity(pCountyID) {
    ResetSubSearchForm("COUNTY");
    AddGeoEntityToSearch(pCountyID, "COUNTY","+");
    DisplaySearch();
};
function SearchPropertiesInCommunitResults() {
    var myCommunites = _SSR.GetResults("COMMUNITY");
    for (var i = 0; i < myCommunites.length; i++) {
        for (var j = 0; j < myCommunites[i].length; j++) {
            AddGeoEntityToSearch(myCommunites[i][j].ID.replace("COMMUNITY_", ""), "COUNTY","+");
        }; //end for each communityPage
    }; //end for each communityPage
    DisplaySearch();
};