﻿$(document).ready(function () {



    // If enter is pressed while input field for zip code is active then submit for zip code lookup
    $("#MainContent_CheckZipCodeTextBox").keypress(function (event) {
        if (event.which == 13) {
            event.preventDefault();
            $("#MainContent_CheckZipCode").trigger("click");
        }
    });

    // Popup for when [Get Started] is clicked. 
    $("#MainContent_lnkGetStartedButton").click(function (e) {
        e.preventDefault();

        //Prefer this was put back into the page and grabbed via JQUERY for the popup
        var PopupContent = "<div id='FindHeroPopupContent' style='color:#666 !important; margin-top:20px;'> " +
                           "    <h1 class='LOGO_BLUE' style='font-size:24px;'>Find a HERO Program</h1> " +
                           "    <h2 style='color:#666'>Enter your zip code to see if HERO Financing is available where you live.</h2>" +
                           "    <table style='width:150px; float:left; margin-top:10px;'>" +
                           "        <tr><td></td><td></td></tr><tr><td> <input style='display:inline; float:left; width:80px; text-align:left;' type='text' maxlength='5' id='FindHeroPopupContentTextField' value='Zip code' /></td>" +
                           "        <td><button style='display:inline; float:left; cursor:pointer; -moz-border-radius:none; margin-top:1px; border-radius:none; -moz-box-shadow:none; box-shadow:none;'  type='submit' id='FindHeroPopupContentGoButton' value='' /></td>" +
                           "    </tr></table>" +
                           "</div>";
        $.colorbox({
            html: PopupContent,
            height: "240px",
            width: "625px",
            onLoad: function () {
                // hide the close button
                $("#cboxClose").css('display', 'none');
                
                


            },

            onComplete: function () {

                $("#FindHeroPopupContentTextField").click(function () {
                    if ($(this).val() == "Zip code") {
                        $(this).val("");
                        $("#FindHeroPopupContentTextField").mask("99999", { placeholder: "" });
                    }
                });



                // all this does is set the main Zipcode field and fires that buttons click event!
                $("#FindHeroPopupContentGoButton").click(function () {
                    $("#MainContent_CheckZipCodeTextBox").val($("#FindHeroPopupContentTextField").val());
                    $("#MainContent_CheckZipCode").trigger('click');
                });

                $("#FindHeroPopupContentTextField").keypress(function (event) {
                    if (event.which == 13) {
                        event.preventDefault();
                        $("#FindHeroPopupContentGoButton").trigger("click");
                    }
                });

            }
        });
    });



    // Clears the zipcode on the main Find a program by zipcode
    $("#MainContent_CheckZipCodeTextBox").click(function () {
        if ($(this).val() == "ZIP CODE") {
            $(this).val("");
            $("#MainContent_CheckZipCodeTextBox").mask("99999", { placeholder: "" });
        }
    });
});


function ShowRACheckAddress() {
    $.get(URL_PATH + "/Content/Static/CheckAddressPopupContent_ZipCheck.htm", function (data) {
        $.fn.colorbox({ overlayClose: true, width: 910, height: 520, close: "", title: "", html: data,
            onComplete: function () {
                var zip = $("#MainContent_hidCheckAddressZipCode").val();
                $("#myPopupZipcode").html(zip);
                $("#PropertyZipCode").val(zip);

                $("#btnCheckAddress").click(function (e) {

                    e.preventDefault();
                    var Validates = true;

                    //Reset
                    $(".Validation").css('visibility', 'hidden');
                    $(".CheckAddressResult").hide();

                    //Get all the divs in the editor Field
                    var RequiredFields = $("#CheckAddressContent .RequiredInput");
                    for (var i = 0; i < RequiredFields.length; i++) {

                        if ($(RequiredFields[i]).val().length == 0) {
                            $(RequiredFields[i]).parent().children(".Validation").css("visibility", "visible");
                            Validates = false;
                        }
                    }

                    //                    if ($("#PropertyCity").val() == "NULL") {
                    //                        $("#PropertyCity").parent().children(".Validation").css("visibility", "visible");
                    //                        Validates = false;
                    //                    }

                    if (Validates) {

                        $.ajax({
                            type: "POST",
                            url: URL_PATH + "/Site/Ajax/AddressLookupHandler.asmx/CheckAddress",
                            data: $("#formCheckAddress").serialize(),
                            timeout: (1000 * 60 * 5), //Timeout 5 Minutes
                            success: function (responseXML) {
                                var result = 'False';

                                result = responseXML.getElementsByTagName("string")[0].childNodes[0].nodeValue;

                                if (result == "False") {
                                    $("#CheckAddressResult_Fail").show();
                                }
                                else {
                                    switch (window.location.hostname) {
                                        case "dev.renovateamerica.com":
                                            window.location = "http://devweb.herofinancing.com/?Zip=RA";
                                            break;

                                        case "qa.renovateamerica.com":
                                            window.location = "http://qaweb.herofinancing.com/?Zip=RA";
                                            break;

                                        case "stage.renovateamerica.com":
                                            window.location = "http://stageweb.herofinancing.com/?Zip=RA";
                                            break;

                                        default:
                                            window.location = "http://wrcog.herofinancing.com/?Zip=RA";
                                            break;
                                    }
                                }
                            }
                        });
                    }

                });
            }
        });

    });
}
