
//-----------------------------------------------------------------------
// Copyright (C) Motorwebs Corporation. All rights reserved.
//-----------------------------------------------------------------------
// PaymentSearch Class

Type.registerNamespace('Motorwebs.UI');

Motorwebs.UI.PaymentSearch = function(element) {
  Motorwebs.UI.PaymentSearch.initializeBase(this, [element]);

  this._paymentElement = null;
  this._termElement = null;
  this._rateElement = null;
  this._tradeElement = null;
  this._doSearchElement = null;
  this._browseName = "";
  this._pageState = "";
}

Motorwebs.UI.PaymentSearch.prototype = {

    initialize: function() {
        Motorwebs.UI.PaymentSearch.callBaseMethod(this, 'initialize');

        $addHandlers(this._paymentElement, { click: this._onElementClick }, this._paymentElement);
        $addHandlers(this._rateElement, { click: this._onElementClick }, this._rateElement);
        $addHandlers(this._tradeElement, { click: this._onElementClick }, this._tradeElement);
        $addHandlers(this._doSearchElement, { click: this._onDoSearchClick }, this);
    },

    dispose: function() {
        $clearHandlers(this._paymentElement);
        $clearHandlers(this._rateElement);
        $clearHandlers(this._tradeElement);
        $clearHandlers(this._doSearchElement);

        Motorwebs.UI.PaymentSearch.callBaseMethod(this, 'dispose');
    },

    // event handlers ////////////////////////////////////////////////////////////////
    _onElementClick: function(e) {
        if (this.title + ":" == this.value) this.value = "";
    },

    _onDoSearchClick: function(e) {

        if (!this._validateContactForm()) return false;

        var payment = $common.parseNumber(this._paymentElement.value);
        var rate = $common.parseNumber(this._rateElement.value.replace(" interest rate", "")) / 1200;
        var term = $common.parseNumber(this._termElement.value);
        var trade = $common.parseNumber(this._tradeElement.value);

        var maxPrice = (rate === 0) ? ((payment * term) + trade) : (payment * ((1 - (1 / Math.pow(1 + rate, term))) / rate) + trade);
        maxPrice = Math.floor(maxPrice);

        window.location.href = "InventoryDisplay.aspx?brw=" + this._browseName + "&s=" + this._pageState + "&dis=paymentSearch&maxprice=" + maxPrice.toString();
    },

    // private methods //////////////////////////////////////////////////////////////
    _validateContactForm: function() {
        if ($common.isNotEmpty(this._paymentElement)) {
            if ($common.isChosen(this._termElement)) {
                if ($common.isNotEmpty(this._rateElement)) {
                    return true;
                }
            }
        }
        return false;
    },

    // public properties //////////////////////////////////////////////////////////////
    get_paymentElement: function() {
        return this._paymentElement;
    },

    set_paymentElement: function(value) {
        this._paymentElement = value;
    },

    get_termElement: function() {
        return this._termElement;
    },

    set_termElement: function(value) {
        this._termElement = value;
    },

    get_rateElement: function() {
        return this._rateElement;
    },

    set_rateElement: function(value) {
        this._rateElement = value;
    },

    get_tradeElement: function() {
        return this._tradeElement;
    },

    set_tradeElement: function(value) {
        this._tradeElement = value;
    },

    get_doSearchElement: function() {
        return this._doSearchElement;
    },

    set_doSearchElement: function(value) {
        this._doSearchElement = value;
    },

    get_browseName: function() {
        return this._browseName;
    },

    set_browseName: function(value) {
        this._browseName = value;
    },

    get_pageState: function() {
        return this._pageState;
    },

    set_pageState: function(value) {
        this._pageState = value;
    }
    // public methods ////////////////////////////////////////////////////////////////
}

Motorwebs.UI.PaymentSearch.registerClass('Motorwebs.UI.PaymentSearch', Sys.UI.Control);