﻿function MapWidget($container, widgetID) {
    this.widgetID = widgetID;
    this.$container = $container
    var myMapWidget = this;
    
}

function SavedSearchesWidget($container, widgetID, clientID) {
    this.widgetID = widgetID;
    this.clientID = clientID;
    this.$container = $container;
    var mySavedSearchesWidget = this;
    this.deleteSearch = function (searchID) {
        if (confirm("Are you sure you want to delete this saved search?")) {
            $aa.Registration.Execute({ 'toolkitAction': $aa.Registration.Action.RemoveSearch, 'searchID': searchID }, 'Remove Search',
                function () {
                    $("#" + mySavedSearchesWidget.clientID + "_ctlWidgetSavedSearch_" + searchID).remove();

                    if ($(".item", mySavedSearchesWidget.$container).length == 0)
                        $("#" + mySavedSearchesWidget.clientID + "_divMarketingCopy_LoggedIn").show();
                }
            );
        }
    }
}

function MortgageCalculatorWidget($container, listPrice,  defaults) {

    this.PaymentCalculator = new $aa.MortgageCalculator($container);
    var $calculatorWidgets = $("[ id $='divMortgageCalculator']");
    this.PaymentCalculator.isFirstCalculator = $calculatorWidgets.first().attr("id") == $container.attr("id");
    this.PaymentCalculator.Load(SE_StripDollarComma(listPrice), defaults);
    
    this.DownPaymentChange = function () { this.PaymentCalculator.DownPaymentChange() };
    this.DownPercentChange =  function () { this.PaymentCalculator.DownPercentChange() };
    this.CalculatePayment =  function () { this.PaymentCalculator.CalculatePayment() };
    this.Clear =  function () { this.PaymentCalculator.Clear() };
}

function RequestInformationWidget($container, widgetID, clientID) {
    this.widgetID = widgetID;
    this.clientID = clientID;
    this.$container = $container;
    
    $aa.Registration.LoadContent(this.$container, true);
    this.$container.find(".reg-phone").showDefaultText($aa.optional, "default");
    
    this.$container.wrapInner("<form action='/'></form>");   
    this.validator = this.$container.find("form").validate($aa.Forms.ValidationSettings);
    this.$container.find(".reg-first-name").rules("add", { requiredwhenvisible: true });
    this.$container.find(".reg-last-name").rules("add", { requiredwhenvisible: true });
    this.$container.find(".reg-email").rules("add", { requiredwhenvisible: true });

    this.sendRequest = function () {
        if ($(".reg-send-button", this.$container).hasClass("button_disabled"))
            return;
        $(".reg-send-button", this.$container).addClass("button_disabled");
        if (!this.$container.find(".reg-email").is(":visible"))
            this.$container.find(".reg-email").removeClass("email")

        if (!this.$container.find("form").valid())
            return;
        var post = {};

        post.registrationAction = $aa.Registration.Action.RequestInfo;
        post.email = this.$container.find(".reg-email").val();
        post.firstName = this.$container.find(".reg-first-name").val();
        post.lastName = this.$container.find(".reg-last-name").val();
        post.phone = this.$container.find(".reg-phone").val();
        post.pageTitle = $aa.pageSettings.pageTitle;
        if (post.phone == $aa.optional)
            post.phone = "";
        post.comments = this.$container.find(".reg-comments").val();

        if ($aa.publicSearch && $aa.publicSearch.searchID > 0) {
            post.searchID = $aa.publicSearch.searchID;
        }
        else
            post.listingID = $aa.listing.concatListingID;
        if ($aa.listing && $aa.listing.fromSearchID)
            post.searchID = $aa.listing.fromSearchID;
        if ($aa.agent && $aa.agent.userID)
            post.agentID = $aa.agent.userID;
        post.requestTitle = this.$container.parent().find(".wg_title").text().trim();

        var myWidget = this;
        $.post($aa.urls.toolkitRegistration, post, function (data, textStatus, jqXHR) {
            if (data.indexOf("<!DOCTYPE") != -1)
                alert("We had a problem processing your request.");
            else {
                var resp = $.parseJSON(data);
                if (resp.contact) {
                    $aa.contact = resp.contact;
                    $aa.Registration.setWelcomeArea();
                    myWidget.$container.find(".reg-form").hide();
                }
                alert(resp.responseMessage);
            }
            $(".reg-send-button", this.$container).removeClass("button_disabled");

        });
    }
}

function RealtyBidWidget($container, widgetID, clientID, defaults) {
    this.widgetID = widgetID;
    this.$container = $container
    var myMapWidget = this;

    this.placeBid = function () {
        var post = {};
        post.listingID = $aa.listing.concatListingID;
        post.companyID = $aa.company.companyID;
        if ($aa.agent && $aa.agent.userID)
            post.agentID = $aa.agent.userID;
        if ($aa.contact && $aa.contact.contactID)
            post.contactID = $aa.contact.contactID;
        if (defaults && defaults.leadTarget)
            post.leadTarget = defaults.leadTarget

        $.post("http://" + location.hostname + "/common/publicpropsearch/actionhandler.aspx?searchtype=realtybid&sourceapp=" + $aa.pageSettings.source, post, function (data, textStatus, jqXHR) {
            var resp = $.parseJSON(data);

            if (resp.IsError)
                alert(resp.responseMessage);
            else {
                var url = 'http://www.realtybid.com/ROMS/ROMS_offer.cfm';
                Post_ObjectToURL(url, resp);
            }
        });
    }
}
