function tcountForm(type, boxParams, productParams, syncParams) {

    this.formId = boxParams.formId;
    this.summPriceId = boxParams.summPriceId;
    this.curCountId = boxParams.curCountId;
    this.discont = parseInt(boxParams.discont);

    this.maxCount = parseInt(productParams.maxCount);
    this.price = parseInt(productParams.price);

    this.syncUrl = syncParams.syncUrl;
    this.syncParams = syncParams.syncParams;
    this.syncCallback = syncParams.syncCallback;

    this.init(parseInt(productParams.curCount), type);
    
}


tcountForm.prototype = {

    formId: null,
    formBox: null,

    minCount: 1,
    maxCount: null,
    curCount: null,

    productId: null,

    summPriceId: null,
    summPriceBox: null,
    summPrice: null,
    prePrice: null,
    
    price: null,
    discont: null,

    curCountId: null,
    curCountBox: null,

    syncUrl: null,
    syncParams: null,
    syncCallback: null,
    beforeSyncEvent: null,


    init: function(curCount) {
        
        this.formBox = $("#" + this.formId);
        this.summPriceBox = $("#" + this.summPriceId);
        
        if (this.curCountId)
            this.curCountBox = $("#" + this.curCountId);

        this.setCurrentCount(curCount, 1);
        this.bindButtons();
        
    },

    bindButtons: function() {
        var self = this;
        $("select.curCount", this.formBox)[0].onchange = function() {
                self.changeCurrentCount();
            };
    },


    incCurrentCount: function() {
        this.setCurrentCount(parseInt(this.curCount) + 1);
    },


    decCurrentCount: function() {
        this.setCurrentCount(parseInt(this.curCount) - 1);
    },
    
    changeCurrentCount: function() {
        this.setCurrentCount(parseInt($("select.curCount", this.formBox)[0].value));
    },

    setCurrentCount: function(value, nosync) {
        if (!value)
            value = this.minCount;
        if (value >= this.minCount && value <= this.maxCount) {
            this.curCount = value;

            this.showCurrentCount();
            this.showCurrentPrice();

            if (this.beforeSyncEvent)
                this.beforeSyncEvent();

            if (!nosync)
                this.setSyncTimeout();
            
            if (this.onSetCurrentCount)
                this.onSetCurrentCount();
        }
    },


    
   interval     : 100,
   maxDelay     : 3000,

   delay        : 3000,
   delayStep    : 1000,
   delayId      : 0,


    setSyncTimeout: function() {
        var self = this;

        var upd = function() {
            if (self.delay > 0) {
                self.delay = self.delay - self.delayStep;
                self.delayId = setTimeout(upd, self.interval);
            }
            else {
                self.delay = self.maxDelay;
                self.delayId = 0;
                self.syncAction();
            }
        }
        
        self.delay = self.maxDelay;
        
        if (self.delayId == 0)
            upd();
    },


    syncAction: function() {
        var self = this;
        if (self.syncUrl == null) {
            return;
        }

        self.syncParams["selectedCount"] = self.curCount;

        $.post(self.syncUrl, self.syncParams, function(data) {
            if (data && data.length)
                alert(data);
            else
                if (self.syncCallback)
                    self.syncCallback(self);
        });
    },


    showCurrentCount: function() {
        if (this.curCountBox)
            this.curCountBox.html(this.curCount);
        
        $(".curCount", this.formBox)[0].value = this.curCount;
    },


    showCurrentPrice: function() {        
        if (this.discont)
        {
        this.prePrice = this.curCount * this.price;
        this.summPrice = this.prePrice - Math.round(this.discont / 100 * this.prePrice);
        }
        else
        {
        this.summPrice = this.curCount * this.price;
        }
        this.summPriceBox.html(this.summPrice);
    },

    /** callback **/
    onSetCurrentCount: function() {
        
    }

}