﻿
var cityIds = new Array();
function checkPostalCode(obj) {
    var defaultText = "Select the City:" +
                                "<br />" +
                                "<span class='notation'>" +
                                "Cities will appear here " +
                                "once you have provided a " +
                                "postal code</span>";
    var emptyText = "No U.S. cities were found with that postal code.";
    if (obj.value.length >= 5) {
        $.ajax({
            type: "POST",
            url: "/Location/LookupPostalCode?postalCode=" + obj.value,
            beforeSend: function(xhr) {
                xhr.setRequestHeader("Content-type",
                            "application/json; charset=utf-8");
            },
            dataType: "json",
            success: function(msg) {
                var items = eval(msg);
                if (items.length > 0) {
                    $("#locationRegion").empty();

                    if (items.length > 1) {
                        $("#locationRegion").append(
                                "<div>" +
                                    "Select the city:" +
                                "</div>");
                    }
                    for (var i = 0; i < items.length; i++) {
                        var selected = "";
                        if (i == 0) {
                            selected = "checked";
                        }

                        $("#locationRegion").append("<div class='clear' />");
                        $("#locationRegion").append(
                                "<div class='formfield'>");
                        $("#locationRegion").append(
                                    "<input id='city" + items[i].Id + "' name='rdoCity' type='radio' value='" + items[i].City + "' class='cityRadio' " + selected + "/>" +
                                    "<label for='city" + items[i].Id + "'>" + items[i].City + ", " + items[i].State + ", " + items[i].Country + "</label>"
                                    );
                        $("#locationRegion").append("</div>");
                    }

                    $(".cityRadio").bind("click", function(e) {

                    });
                    $("#locationRegion").show("slow");

                }
                else {
                    $("#locationRegion").empty().append(emptyText);
                    $("#locationRegion").show("slow");
                }
            }
        });
    }
    else {
        $("#locationRegion").empty().append(defaultText);
    }
}


//this function validates AddResidential Form 
function ValidateForm() {

    //validating Title field
    if ($("#Title").val().length == 0) {
        alert("Title is required");
        $("#Title").focus();
        return false;
    }

    //validating Subdivision field
    if ($("#Subdivision").val().length == 0) {
        alert("Subdivision is required");
        $("#Subdivision").focus();
        return false;
    }

    //validating Subdivision field
    if ($("#Township").val().length == 0) {
        alert("Township is required");
        $("#Township").focus();
        return false;
    }

    //validating Description field
    if ($("#Description").val().length == 0) {
        alert("Description is required");
        $("#Description").focus();
        return false;
    }
    //validating Line1 field
    if ($("#Line1").val().length == 0) {
        alert("Line1 is required");
        $("#Line1").focus();
        return false;
    }

    //validating PostalCode field
    if ($("#PostalCode").val().length == 0) {
        alert("Postal Code is required");
        $("#PostalCode").focus();
        return false;
    }

    //validating PostalCode being a number 
    if (parseInt($("#PostalCode").val()) == 0) {
        alert("Invalid Postal Code");
        $("#PostalCode").focus();
        return false;
    }

    //validating Year Built field
    if ($("#YearBuilt").val().length == 0) {
        alert("Year Built is required");
        $("#YearBuilt").focus();
        return false;
    }

    //validating Year Built being a number 
    var yearVal = parseInt($("#YearBuilt").val());
    if (yearVal == 0 || yearVal < 1800 || $("#YearBuilt").val().length != 4) {
        alert("Invalid Year Built");
        $("#YearBuilt").focus();
        return false;
    }

    //validating Price field
    if ($("#Price").val().length == 0) {
        alert("Price is required");
        $("#Price").focus();
        return false;
    }

    //validating Price being a number 
    if (isNaN(parseInt($("#Price").val()))) {
        alert("Invalid Price");
        $("#Price").focus();
        return false;
    }

    //validating ListingSubType select
    var dropDown = document.getElementById("ListingSubType");
    if (dropDown == null || dropDown.selectedIndex == 0) {
        alert("Must select a Property Type");
        $("#ListingSubType").focus();
        return false;
    }

    //validating ListingSubType select
    dropDown = document.getElementById("Zoning");
    if (dropDown == null || dropDown.selectedIndex == 0) {
        alert("Must select a Zone");
        $("#Zoning").focus();
        return false;
    }

    //validating ListingTemplate select
    dropDown = document.getElementById("ListingTemplate");
    if (dropDown == null || dropDown.selectedIndex == 0) {
        alert("Must select a Template");
        $("#ListingTemplate").focus();
        return false;
    }

    //validating ListingSubType select
    dropDown = document.getElementById("OwnershipType");
    if (dropDown == null || dropDown.selectedIndex == 0) {
        alert("Must select a Ownership Type");
        $("#OwnershipType").focus();
        return false;
    }

    //validating PurchaseType select
    dropDown = document.getElementById("PurchaseType");
    if (dropDown == null || dropDown.selectedIndex == 0) {
        alert("Must select a Purchase Type");
        $("#PurchaseType").focus();
        return false;
    }

    //validating ListingSubType select
    dropDown = document.getElementById("PropertyLevel");
    if (dropDown == null || dropDown.selectedIndex == 0) {
        alert("Must select a Property Level");
        $("#PropertyLevel").focus();
        return false;
    }

    return true;
}

function onlyint(e) {
    e = e || window.event;
    if (e.keyCode == 0x08) {
        e.returnValue = true;
    }
    else if (e.keyCode == 0x09) {
        e.returnValue = true;
    }
    else if (e.keyCode == 45) {
        e.returnvalue = true;
    }
    else if (!(e.keyCode >= 0x30 && e.keyCode <= 0x39)) {
        e.returnValue = false;
    }
}
        