// Quick Search
//
// Use this script with the corresponding SEARCH form and RESULTS page to enable searching against
// an AgentAchieve powered application. Search results will appear on the results page in an iFrame.
// Make sure this script is referenced in both the search and results pages and the iFrame has the
// correct ID set. Additional values may be added from the Property Search API.
//
// Please work with the CoreLogic Web Implementation team to define the variables below.
//
var searchPath = "http://public.madisonprops.com/search/default.aspx";
var countyList = "JEFFERSON,DOUGLAS,DENVER,ARAPAHOE,ADAMS,WELD,PARK,LARIMER,ELBERT,CLEAR CREEK";
var State = "CO";
var Additional = "";
//
//

function clearForEntry(obj) {
    if (obj.value == "City" || obj.value == "Zip" || obj.value == "MLS#")
        obj.value = "";
}

function testForValidEntry(obj) {
    var confirmText = "You can only search by one of these fields (City, Zip Code or MLS) at a time.\n\nWould you like to clear out the other fields and keep the value that you just entered?";

    switch (obj.id) {
        case "txtCity":
            if (obj.value == "")
                obj.value = "City";
            else {
                if (obj.value != "City" && (document.getElementById('txtZipCode').value != "Zip" || document.getElementById('txtMLSID').value != "MLS#")) {
                    if (confirm(confirmText)) {
                        document.getElementById('txtZipCode').value = "Zip";
                        document.getElementById('txtMLSID').value = "MLS#";
                    }
                    else
                        document.getElementById('txtCity').value = "City";
                }
            }
            break;
        case "txtZipCode":
            if (obj.value == "")
                obj.value = "Zip";
            else {
                if (obj.value != "Zip" && (document.getElementById('txtCity').value != "City" || document.getElementById('txtMLSID').value != "MLS#")) {
                    if (confirm(confirmText)) {
                        document.getElementById('txtCity').value = "City";
                        document.getElementById('txtMLSID').value = "MLS#";
                        validateZipCode(obj.value);
                    }
                    else
                        document.getElementById('txtZipCode').value = "Zip";
                }
                else {
                    validateZipCode(obj.value);
                }
            }
            break;
        case "txtMLSID":
            if (obj.value == "")
                obj.value = "MLS#";
            else {
                if (obj.value != "MLS#" && (document.getElementById('txtCity').value != "City" || document.getElementById('txtZipCode').value != "Zip")) {
                    if (confirm(confirmText)) {
                        document.getElementById('txtCity').value = "City";
                        document.getElementById('txtZipCode').value = "Zip";
                    }
                    else
                        document.getElementById('txtMLSID').value = "MLS#";
                }
            }
            break;
    }
}

function validateZipCode(number) {
    if (number != "Zip" && (isNaN(number) || (number.toString().length != 5))) {
        alert("Please enter a valid zip code.");
        document.getElementById('txtZipCode').value = "Zip";
    }
}

function submitQuickSearch() {
    var bolError = false;
    var errorMessage = "";

    if (document.getElementById('txtCity').value == "City" && document.getElementById('txtZipCode').value == "Zip" && document.getElementById('txtMLSID').value == "MLS#") {
        errorMessage += "Please enter a City, Zip Code or MLS number.\n";
        bolError = true;
    }

    if (document.getElementById("cboMinPrice").value != "" && document.getElementById("cboMaxPrice").value != "") {
        if (parseInt(document.getElementById("cboMinPrice").value) > parseInt(document.getElementById("cboMaxPrice").value)) {
            errorMessage += "Your minimum price cannot be higher that your maximum price.\n";
            bolError = true;
        }
    }

    if (document.getElementById("cboMinBeds").value != "" && document.getElementById("cboMaxBeds").value != "") {
        if (parseInt(document.getElementById("cboMinBeds").value) > parseInt(document.getElementById("cboMaxBeds").value)) {
            errorMessage += "Your minimum number of bedrooms cannot be higher that your maximum number of bedrooms.\n";
            bolError = true;
        }
    }

    if (document.getElementById("cboMinBaths").value != "" && document.getElementById("cboMaxBaths").value != "") {
        if (parseInt(document.getElementById("cboMinBaths").value) > parseInt(document.getElementById("cboMaxBaths").value)) {
            errorMessage += "Your minimum number of bathrooms cannot be higher that your maximum number of bathrooms.\n";
            bolError = true;
        }
    }

    if (bolError)
        alert(errorMessage);
    else {


        var l = "";

        if (document.getElementById('txtMLSID').value == "MLS#")
            params = "?County=" + countyList + "&state=" + State + Additional + "&type=R&submit=T" + (encodeURIComponent(document.getElementById('txtCity').value) != "City" ? "&City=" + encodeURIComponent(document.getElementById('txtCity').value) : "") + (document.getElementById('txtZipCode').value != "Zip" ? "&zip=" + encodeURIComponent(document.getElementById('txtZipCode').value) : "") + "&class=" + encodeURIComponent(document.getElementById("cboPropertyType").value) + "&NPR=" + encodeURIComponent(document.getElementById("cboMinPrice").value) + "&XPR=" + encodeURIComponent(document.getElementById("cboMaxPrice").value) + "&NBD=" + encodeURIComponent(document.getElementById("cboMinBeds").value) + "&XBD=" + encodeURIComponent(document.getElementById("cboMaxBeds").value) + "&NBA=" + encodeURIComponent(document.getElementById("cboMinBaths").value) + "&XBA=" + encodeURIComponent(document.getElementById("cboMaxBaths").value);
        else
            params = "?ID=" + "&type=I&submit=T&LID=" + encodeURIComponent(document.getElementById('txtMLSID').value) + "&class=" + encodeURIComponent(document.getElementById("cboPropertyType").value) + "&NPR=" + encodeURIComponent(document.getElementById("cboMinPrice").value) + "&XPR=" + encodeURIComponent(document.getElementById("cboMaxPrice").value) + "&NBD=" + encodeURIComponent(document.getElementById("cboMinBeds").value) + "&XBD=" + encodeURIComponent(document.getElementById("cboMaxBeds").value) + "&NBA=" + encodeURIComponent(document.getElementById("cboMinBaths").value) + "&XBA=" + encodeURIComponent(document.getElementById("cboMaxBaths").value);

        window.location.href = "search.html" + params;

    }
}

function results() {
    if (document.getElementById('results') != null) {
        params = window.location.search;
        document.getElementById('results').src = searchPath + params;
    }
}

window.onload = results;
