// JavaScript Document

// тип  - корзина
function MyBasket() {
    this.iCount = 0;
    this.fTotal = 0;
    this.aGoods = new Array();
}

MyBasket.instance = null;

MyBasket.getInstance = function () {

	if(!MyBasket.instance) {
		MyBasket.instance = new MyBasket();
	}
	return MyBasket.instance;
}

// Тип - Товар
function MyGoods(id,price,count,name) {
    this.id = id;
    this.price = price;
    this.count = count;
    this.name = name;
}

//objBasket = new MyBasket();

// функция пересчета стоимости
// @param id ид товара
// @param val количество товара
// @param код группы товаров внутри которой идет пересчет
MyBasket.prototype.reCalc = function(id, val, group_id) {
    //if( parseInt(val) < 0 ) return false;
    var aInput = document.getElementsByName(id);
    var price = aInput[0].getAttribute('price');
    var name = aInput[0].getAttribute('title');
		price = price.replace(' ','');
    this.iCount = 0;
    this.fTotal = 0.0;

    var isNew = 1;
    for(var i=0; i<this.aGoods.length; i++) {
        objGoods = this.aGoods[i];
        if( objGoods && objGoods.id && (objGoods.id == id) ) {
            this.aGoods[i].count = parseInt( val );
            this.aGoods[i].price = parseFloat( price );
            isNew = 0;
        }

        this.fTotal = this.fTotal + parseInt( this.aGoods[i].count ) * parseFloat( this.aGoods[i].price );
        this.iCount = this.iCount + parseInt( this.aGoods[i].count );
    } // end for

    if( isNew ) {
        newGood = new MyGoods(id,price,val,name);
        this.fTotal = this.fTotal + parseInt( newGood.count ) * parseFloat( newGood.price );
        this.iCount = this.iCount + parseInt( newGood.count );
        this.aGoods[this.aGoods.length] = newGood;

    }

    aInput = document.getElementsByName('mybasket_grp_'+group_id);
    for(i=0; i < aInput.length; i = i + 1) {
        aInput[i].innerHTML = 'Товаров ' + this.iCount + ' на общую сумму ' + this.fTotal.toFixed(2);
    }
	
}

MyBasket.prototype.AddToBasket = function(group_id) {

    for(var i=0; i<this.aGoods.length; i++) {
        objGood = this.aGoods[i];
		if( objGood && objGood.id ) {
			//добавляем к заказу
			umiBasket.getInstance().addElement(objGood.id,objGood.count);
			
			//очищаем значение
			aInput = document.getElementsByName(objGood.id);
			for(var j=0; j < aInput.length; j++) {
				aInput[j].value = 0;
			}//end for j
		}//end if
	}//end for
	
    aInput = document.getElementsByName('mybasket_grp_'+group_id);
    for(i=0; i < aInput.length; i = i + 1) {
        aInput[i].innerHTML = '0';
    }
	
	this.aGoods = new Array();
}
