﻿function OfferForm(quantityElementID) {
    var textElement = $('#' + quantityElementID).find('input[rel=UnloadingPoints]');
    if (textElement.length == 0) { return; }
    textElement[0].Container = $('#' + quantityElementID);
    textElement[0].item2Element = textElement[0].Container.find('div[rel=item2]');
    textElement[0].item3Element = textElement[0].Container.find('div[rel=item3]');

    textElement.keyup(
        function(e) {
            this.checkValue();
        }
    );

    textElement[0].checkValue = function() {
        var match = this.value.match(/^\d+$/);
        if (!match || parseInt(match) < 1 || parseInt(match) > 3) {
            match = 1;
        }

        if (match >= 2) {
            this.item2Element.show();
        } else {
            this.item2Element.hide();
        }
        this.disableValidators($(this.item2Element).find('span[rel=item2]'), match >= 2);

        if (match >= 3) {
            this.item3Element.show();
        } else {
            this.item3Element.hide();
        }
        this.disableValidators($(this.item3Element).find('span[rel=item3]'), match >= 3);
    };

    textElement[0].disableValidators = function(validators, enable) {
        validators.each(
            function(i) {
                this.enabled = enable != false;
            }
        );
    };

    textElement[0].checkValue();
}