//<![CDATA[
var currentObj = null;
var isBasket = false;
var timer;
var arrTotalLocal = [];
var total = 0.00;

function setTotal(value) {
	total = value;
}

function checkKey(e, obj, id, catId, sbcatId, divide) {
	var key = (document.all) ? e.keyCode : e.which;

	if (key == 13) {		
		var elements = document.forms[1].elements;
		var i;
		for (i = 0; i < elements.length; i++) {
			if (elements[i].name == obj.name) {
				break;
			}
		}
		i = i + 4;
		
		if (typeof (elements[i]) != 'undefined' && elements[i].type == 'text') {
			
			try {
				elements[i].focus();
				elements[i].select();
			} catch (e) {
			}
		}
	}

	if (key == 37 || key == 39) {
		return true;
	}

	if (key == 9 || key == 13) {
		if (currentObj != null && currentObj.value == '') {
			currentObj.value = '0';
		} else if (obj.value == '') {
			obj.value = '0';
		}

		return true;
	}

	var isCommaWrite = (obj.value.indexOf('.') == -1 && obj.value.indexOf(',') == -1) ? false
			: true;
	var isFirstChar = (obj.value == '' && (key == 190 || key == 188)) ? true
			: false;
	var isFirstZero = ((obj.value == '' || obj.value == '0') && key == 48 && divide == 0) ? true
			: false;

	if (!e.shiftKey
			&& ((key >= 48 && key <= 57 && !isFirstZero)
					|| (key >= 96 && key <= 105 && !e.shiftKey) || key == 8
					|| key == 46 || ((key == 190 || key == 188) && divide == 1
					&& !isCommaWrite && !isFirstChar))) {
		/*
		 * if(obj.value == '0'){ obj.value = ''; }
		 */
		
		timer = setTimeout('calculateProductPrice(' + id + ',' + catId + ','
				+ sbcatId + ')', 1);
		return true;
	}

	return false;
}

function checkValue(obj, id) {
	if (currentObj != null && currentObj.value == '') {
		currentObj.value = '0';
	}

	if (obj.value == '0') {
		obj.value = '';
	}
	obj.select();
	currentObj = obj;
}

function calculateProductPrice(id, catId, sbcatId) {
	var oForm = document.frmProducts;
	var oLocalLayer = document.getElementById('imp_' + id);
	var oTotalLayer = document.getElementById('total');

	if (oForm.elements['qntty_' + id].value == ''
			|| oForm.elements['prc_' + id].value == '0.00') {
		oLocalLayer.innerHTML = '0,00';
		// oForm.elements['qntty_' + id].value = 0;
		removeProduct(id, catId, sbcatId, null, false);
	} else {
		var price = parseFloat(oForm.elements['prc_' + id].value);
		var productPrice = price;
		oForm.elements['qntty_' + id].value = oForm.elements['qntty_' + id].value
				.replace(',', '.')
		var quantity = parseFloat(oForm.elements['qntty_' + id].value);
		var ratio = parseFloat(oForm.elements['rat_' + id].value);

		if (ratio > 0.00) {
			price = ratio * price;
		}

		var result = price * quantity;
		if (typeof (arrTotalLocal[id]) != 'undefined') {
			total = total - arrTotalLocal[id];
		}

		arrTotalLocal[id] = result;
		oLocalLayer.innerHTML = result.toFixed(2).replace('.', ',');
		addProductToBasket(id, catId, sbcatId, quantity, productPrice, ratio);
	}
	clearTimeout(timer);
}
//]]>
