﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("Tensator");

Tensator.Basket = function(element) {
    Tensator.Basket.initializeBase(this, [element]);

    this._quantityControls = null;
    this._valueControl = null;
}

Tensator.Basket.prototype = {
    initialize: function() {
        Tensator.Basket.callBaseMethod(this, 'initialize');

        this._quantityChangedHandler = Function.createDelegate(this, this.quantityChanged);

        var element; var i; var controls = this.get_quantityControls();

        if (controls) {
            for (i = 0; i < controls.length; i++) {
                element = $get(controls[i].Id);
                if (element != null) {
                    $addHandler(element, "change", this._quantityChangedHandler);
                }
            }
        }
    },
    dispose: function() {
        var element; var controls = this.get_quantityControls();
        if (controls) {
            for (i = 0; i < controls.length; i++) {
                element = $get(controls[i].Id);
                if (element != null) {
                    $removeHandler(element, "change", this._quantityChangedHandler);
                }
            }
        }

        $clearHandlers(this.get_element());

        delete this._quantityChangedHandler;

        Tensator.Basket.callBaseMethod(this, 'dispose');
    },
    //properties
    get_quantityControls: function() {
        return this._quantityControls;
    },
    set_quantityControls: function(value) {
        if (this._quantityControls !== value)
            this._quantityControls = value;
    },
    get_valueControl: function() {
        return this._valueControl;
    },
    set_valueControl: function(value) {
        if (this._valueControl !== value)
            this._valueControl = value;
    },
    // methods    
    methodCallComplete: function(result, userContext, methodName) {
    },
    methodCallError: function(error, userContext, methodName) {
        if (error !== null) {
            alert(error.get_message());
        }
    },
    addCommas: function(strValue) {
        var objRegExp = new RegExp('(-?[0-9]+)([0-9]{3})');

        //check for match to search criteria
        while (objRegExp.test(strValue)) {
            //replace original string with first group match, a comma, then second group match
            strValue = strValue.replace(objRegExp, '$1,$2');
        }
        return strValue;
    },
    // event handlers
    quantityChanged: function(sender, e) {
        var quantity; var price; var controls = this.get_quantityControls();
        var newPrice; var total = parseFloat(0.00);
        for (i = 0; i < controls.length; i++) {
            quantity = $get(controls[i].Id);
            price = $get('s' + controls[i].Id);

            if (quantity != null && price != null) {
                if (isNaN(quantity.value))
                    quantity.value = '1';

                newPrice = (quantity.value * controls[i].Price).toFixed(2);
                total = parseFloat(total) + parseFloat(newPrice);

                price.innerHTML = this.addCommas(newPrice);
            }
        }
        var element = $get(this.get_valueControl());
        element.innerHTML = this.addCommas(total.toFixed(2));
    }
}
Tensator.Basket.registerClass('Tensator.Basket', Sys.UI.Control);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
