﻿
function EstimateWrapperControl(mainContainerId) {
    var control = {
        id: CreateUUID(),
        controlContainerId: mainContainerId,
        isInitialized: false,
        pickupAddress: new AddressPrototype(),
        destinationAddress: new AddressPrototype(),
        postedForm : {
            form : null,
            pickupAddressKey : "pickup_location",
            destinationAddressKey : "dropoff_location"
        }
    };

    control.IsInitialized = function () {
        return this.isInitialized;
    };

    control.IsVisible = function () {
        return IsExists(this.controlContainerId);
    };

    control.UpdateValidatedPickupAddress = function () {
        if (this.IsInitialized())
            this.pickupAddress.UpdateValidatedAddress();
    };
    control.UpdateValidatedDestinationAddress = function () {
        if (this.IsInitialized())
            this.destinationAddress.UpdateValidatedAddress();
    };

    control.InitPostedForm = function (path) {
        this.postedForm.form = new PostedFormPrototype(path);
    };

    control.SubmitPostedForm = function () {
        if (this.postedForm.form &&
            !this.pickupAddress.IsValueChanged() &&
            !this.destinationAddress.IsValueChanged()) {

            var params = [];
            params[this.postedForm.pickupAddressKey] = this.pickupAddress.GetText();
            params[this.postedForm.destinationAddressKey] = this.destinationAddress.GetText();

            this.postedForm.form.SetParams(params);
            this.postedForm.form.Submit();
        }
    };

    return control;
}

function EstimateWrapperControl_Initialization(control) {
    Trace("EstimateWrapperControl_Initialization(" + control.id + ")");

    if (control && control.IsVisible()) {
        control.pickupAddress.Activate();
        control.destinationAddress.Activate();
        control.isInitialized = true;
    }
}

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

