var kf_form
var kf_fieldsCount = 0;

function kf_init() {
	kf_form = document.frmProducts
	kf_totalPrice = 'tbKF_tP'
	kf_countFields()
}

function kf_removeAll() {
	for(i=0; i<kf_fieldsCount; i++) {
		name = 'selKF_' + i
		kf_form[name].selectedIndex = -1
	}
	kf_countAllPrices()
}

function kf_countFields() {
	licz = 0
	for(i=0; i<kf_form.elements.length; i++) {
		poz = kf_form.elements[i].name.indexOf("selKF_")
		if (poz == 0) {
			licz++
		}
	}
	kf_fieldsCount = licz
}

// GET

function kf_getFieldText(nr) {
	name = 'selKF_' + nr
	field_idx = kf_form[name].selectedIndex
	var field_text = ''
	if (field_idx > -1) {
		field_text = kf_form[name].options[kf_form[name].selectedIndex].text
	}
	return field_text
}

function kf_getFieldValue(nr) {
	name = 'selKF_' + nr
	field_idx = kf_form[name].selectedIndex
	var field_value = ''
	if (field_idx > -1) {
		field_value = kf_form[name].options[kf_form[name].selectedIndex].value
	}
	return field_value
}

function kf_getPrice(nr) {
	field_value = kf_getFieldValue(nr)
	poz1 = field_value.indexOf("_")
	poz2 = field_value.indexOf("#")
	if (poz1 > -1 && poz2 > -1) {
		tmp_cena = field_value.substr(poz1+1, poz2-poz1-1)
		tmp_cena = Math.round(parseFloat(tmp_cena) * 100)
		cena = tmp_cena / 100
		//if (tmp_cena % 100 == 0) cena = cena + "00"
		//else if (tmp_cena % 10 == 0) cena = cena + "0"
	} else
		cena = 0
	return cena
}

function kf_getCatNum(nr) {
	field_value = kf_getFieldValue(nr)
	poz = field_value.indexOf("#")
	if (poz > -1)
		cat_num = field_value.substr(poz+1)
	else
		cat_num = ''
	return cat_num
}

function kf_getQty(nr) {
	name = 'tbKF_qty_' + nr
	qty = kf_form[name].value
	return qty
}

// SET

function kf_setPrice(nr, price) {
	if (price == 0) price = ''
	price = kf_formatFloat(price)
	kf_form['tbKF_c_' + nr].value = price
}

function kf_setQty(nr, qty) {
	name = 'tbKF_qty_' + nr
	kf_form[name].value = qty
}

function kf_countAllPrices() {
	var totalPrice = 0;
	for(i=0; i<kf_fieldsCount; i++) {
		wartosc = kf_getPrice(i)		
		if (wartosc == '') wartosc = 0
		kf_setPrice(i, wartosc)
		cat_num = kf_getCatNum(i)
		if (cat_num == '') {
			name = 'selKF_' + i
			kf_form[name].selectedIndex = -1
			kf_setQty(i, '')
			continue
		} else {
			cnt = kf_getQty(i)
		}
		totalPrice += wartosc * cnt
	}
	// wpisz cene w pole podsumowania
	totalPrice = kf_formatFloat(totalPrice)
	kf_form[kf_totalPrice].value = totalPrice
	return totalPrice
}

function kf_chooseProduct(nr) {
	qty = kf_getQty(nr)
	if (qty == '') kf_setQty(nr, 1)
	kf_countAllPrices()
}

function kf_removeProduct(nr) {
	name = 'selKF_' + nr
	kf_form[name].selectedIndex = -1
	kf_countAllPrices()
}

function kf_onChangeQty(nr) {
	name = 'selKF_' + nr
	if (kf_form[name].selectedIndex == -1) kf_setQty(nr, '')
	kf_countAllPrices()
}

function kf_loadSet(id) {
	// wczytanie elementow zestawu do tabeli
}

function kf_showDescription(nr) {
	cat_num = kf_getCatNum(nr)
	if (cat_num != '') {
		cat_num = escape(cat_num)
		categorie = kf_form['hCat' + nr].value
		// link = 'http://www.techmarket.pl/description.php?id=' + cat_num + '&categorie=' + categorie
		link = 'http://tmpro.dynalias.net/shop.php?a=opis_prod&id=' + cat_num + '&categorie=' + categorie
		var wnd
		wnd = window.open(link,'produkt','width=600,height=200,top=100,left=400,resizable,scrollbars=yes,toolbar=yes,status=yrd,directories=yes,menubar=yes,location=yes')
	}
}

function kf_formatFloat(nr) {
	number = nr + ''
	if (number != '') {
		poz = number.indexOf('.')
		if (poz == -1) {
			number += '.00'
		} else if (poz == number.length-2) {
			number += '0'
		} else {
			number = number.substr(0, poz+3)
		}
	}
	// alert("FORMAT_FLOAT(" + nr + ") = " + number)
	return number
}

