// eCommerce FO JavaScript

AMOUNTBOX_NAME 		= "amountbox";
AMOUNT_NAME 		= "amount";
PRICE_NAME 			= "price";
CALCPRICE_NAME 		= "calcprice";


// Disables all inputs with a given name. Use it in "onload" event.
function initInputs ( controlInputName ) {
	var 
		num = 1,
		definedElements = arguments;
	obj = document.getElementById(controlInputName + '_' + num);
	while (obj) {
		if (obj.type == 'checkbox' && obj.checked)
			highlightRow(obj);
		num++;
		obj = document.getElementById(controlInputName + '_' + num);
	}

	if ( definedElements [1] )

		for ( var j = 1; j < definedElements.length; j++ ) {
			num = 1;
			while ( document.getElementById ( definedElements [j] + '_' + num ) ) {
				var currentInput = document.getElementById ( definedElements [j] + '_' + num );
				var trClass = currentInput.parentNode.parentNode.className;
				
				if ( trClass == 'selected' )
					enableInput ( currentInput );
				else
					disableInput ( currentInput );

				num++;
			}
		}
	else
		return;
	var obj = document.getElementById('updateButton');
	if(obj) obj.disabled = 1;		

}

function disableInput ( oInput ) {
	if ( oInput )
		if ( oInput.type == 'checkbox' || oInput.type == 'radio' )
			oInput.checked = false;
		else 
			oInput.disabled = true;
	else return;
}

function enableInput ( oInput ) {
	if ( oInput )
		if ( oInput.type == 'checkbox' || oInput.type == 'radio' )
			oInput.checked = true;
		else 
			oInput.disabled = false;
	else return;
}


// Selects the entire contents of input element when the user clicks on it. Use in "onclick" event.
function focusInput ( oInput ) {
	if (oInput && !oInput.readOnly) {
		oInput.select ();
	}
}
// highlights selected row in the table with items.
function highlightRow ( tObject )
{
	var
		tr = tObject.parentNode.parentNode,
		num = tObject.id.split ( '_' ) [1],
		input = document.getElementById( 'amount_' + num ),
		minus = document.getElementById( 'minus_' + num ),
		plus = document.getElementById( 'plus_' + num );
	if ( tr.className != 'selected' ) {
		tr.className = 'selected';
		input.disabled = false;
		minus.disabled = false;
		plus.disabled = false;
		if ( input.value == '0') {
			input.value = '1';
		}
	}
	else {
		tr.className = '';
		input.value = '0';
		input.disabled = true;
		minus.disabled = true;
		plus.disabled = true;
		//stripeRows ( 'prod-list' );
		
	}
	//calculatePrice ( 'price_' + num, 'amount_' + num , 'calcprice_' + num, 0);		
}
// Controls user input and calculates the price using the following formula: calcPrice = price * amount.
function calculatePrice ( priceId, amountId, targetId, maxAmount) {
	
	var 
		price = document.getElementById ( priceId ).innerHTML.replace(" ", "").replace(",", "."),
		amount = parseInt ( document.getElementById ( amountId ).value ),
		target = document.getElementById ( targetId ),
		newPrice = null;
	price = toNum ( price );
	
	if ( isNaN ( amount ) || amount < 0 ) {
		amount = 0;
		document.getElementById ( amountId ).value = '';
	}
	if (maxAmount > 0 && amount >= maxAmount) {
		amount = maxAmount - 1;
		document.getElementById ( amountId ).value = amount;
	}

	num = priceId.split ( '_' ) [1];
	var chkObj = document.getElementById ( 'chk_' + num );

	
	newPrice = price * amount * chkObj.checked;
	target.innerHTML = toScreenNum ( newPrice );
	target.nextSibling.value = newPrice;
	calculateSumm ( 'cell', 'total' );
	var obj = document.getElementById('updateButton');
	if(obj) obj.disabled = 0;
		
}
// Verifies if amount input is empty and sets value to zero
// Use in "onblur" event.
function setAmount ( amountObject ) {
	var num = null;
	if ( amountObject.value == '' || amountObject.value == 0 ) {
		num = amountObject.id.split ( '_' ) [1];
		var chkObj = document.getElementById ( 'chk_' + num );
		chkObj.checked = false;
		highlightRow ( chkObj );
	}
	else return;
}
// Calculates the sum for all items in the table using 'cell' name of hidden inputs.
function calculateSumm ( name , target ) {
	var 
		allCells = document.getElementsByName ( name ),
		summ = null;
	if ( allCells )
		for ( var i = 0; i < allCells.length; i++ ) {
			num = allCells[i].id.split ( '_' ) [1];
			var chkObj = document.getElementById ( 'chk_' + num );
			if (chkObj.checked)
				summ += parseFloat ( allCells[i].value.replace(",", ".").replace(" ", "") );
		}
	summ = toScreenNum ( summ );
	if (document.getElementById ( target )) {	
		document.getElementById ( target ).innerHTML = summ;
	}
}
// Converts a number to price format using separator from argument string. If no separator specified, a default one will be used.
function toScreenNum ( number ) {
	var separator = arguments [1] ? arguments [1] : ',' ;
	if ( number != 0 ) {
		var calcNum = ( Math.round (number * 100) ).toString ( );
		if (calcNum < 10) calcNum = '00' + calcNum;
		else if (calcNum < 100) calcNum = '0' + calcNum;
		number = calcNum.substring ( 0 , calcNum.length - 2 ) + separator + calcNum.substring ( calcNum.length - 2 , calcNum.length);
	}
	else
		number = '0' + separator + '00';
	
	return number;
}
// Converts string in price format to a float
function toNum ( string ) {
	var separator = arguments [1] ? arguments [1] : ',' ,
		calcStr = string.split ( separator );
		
	string = calcStr.join ( '.' );
	return string;
}

// Increases amount of items by one
function plusItem ( priceId, amountId, targetId, maxCount) {
	var 
		priceObj = document.getElementById ( priceId ),
		amountObj = document.getElementById ( amountId ),
		price = parseFloat (priceObj.innerHTML),
		amount = parseInt (amountObj.value);
	if ( !amountObj.disabled ) {
		if (maxCount > 0) {
			if (amount < maxCount - 1) amount++;
			else return;
		}
		else {
			amount++;
		}

		document.getElementById ( amountId ).value = amount;
		calculatePrice ( priceId, amountId, targetId, 0);
	}
	else return;
}
// Decreases amount of items by one.
function minusItem ( priceId, amountId, targetId ) {
	var 
		priceObj = document.getElementById ( priceId ),
		amountObj = document.getElementById ( amountId ),
		price = parseFloat (priceObj.innerHTML),
		amount = parseInt (amountObj.value);
	
	if ( !amountObj.disabled ) {
		if (amount > 1) amount--;
		else return;
		
		document.getElementById ( amountId ).value = amount;
		calculatePrice ( priceId, amountId, targetId, 0);
	}
	else return;
}

// Increases amount of boxes by one
function plusBox(num, inBox, maxAmount) {
	var 
		amountBoxObj = document.getElementById (AMOUNTBOX_NAME + "_" + num),
		amountObj = document.getElementById (AMOUNT_NAME + "_" + num),
		amountBox = parseInt (amountBoxObj.value);
	if (inBox == 0) {
		inBox = 1;		
	}
	maxCount = parseInt((maxAmount - 1) / inBox);

	if ( !amountBoxObj.disabled ) {
		tmpAmountBox = amountBox + 1;
		if (amountBox < maxCount) {
			amountBox = tmpAmountBox;
		}
		
		amountBoxObj.value = amountBox;
		amountObj.value = tmpAmountBox * inBox;
		calculatePrice (PRICE_NAME + "_" + num, AMOUNT_NAME + "_" + num, CALCPRICE_NAME + "_" + num, maxAmount);
	}
	else return;
}

// Decreases amount of boxes by one.
function minusBox(num, inBox) {
	var 
		amountBoxObj = document.getElementById (AMOUNTBOX_NAME + "_" + num),
		amountObj = document.getElementById (AMOUNT_NAME + "_" + num),
		amountBox = parseInt (amountBoxObj.value);
	
	if ( !amountBoxObj.disabled ) {
		if (amountBox > 1) amountBox--;
		else return;
		
		amountBoxObj.value = amountBox;
		amountObj.value = amountBox * inBox;
		calculatePrice (PRICE_NAME + "_" + num, AMOUNT_NAME + "_" + num, CALCPRICE_NAME + "_" + num, 0);
	}
	else return;
}

function setBoxAmount (boxAmountObject) {
	var num = null;
	if ( boxAmountObject.value == '' || boxAmountObject.value == 0 ) {
		num = amountObject.id.split ( '_' ) [1];
		var chkObj = document.getElementById ( 'chk_' + num );
		chkObj.checked = false;
		highlightRow ( chkObj );
	}
	else return;
}

// Controls user input and calculates the price using the following formula: calcPrice = price * amount.
function calculateBoxPrice(num, inBox, maxAmount) {
	
	var amount = parseInt(document.getElementById(AMOUNTBOX_NAME + "_" + num).value);
	if (inBox == 0) {
		inBox = 1;		
	}
	maxCount = parseInt(maxAmount / inBox);
	
	if (isNaN(amount) || amount < 0) {
		amount = 0;
		document.getElementById (AMOUNTBOX_NAME + "_" + num).value = '';
	}
	amountObj = document.getElementById (AMOUNT_NAME + "_" + num),
	amountObj.value = amount * inBox;
	if (amount >= maxCount) {
		amount = maxCount;
		document.getElementById(AMOUNTBOX_NAME + "_" + num).value = amount;
	}
	calculatePrice (PRICE_NAME + "_" + num, AMOUNT_NAME + "_" + num, CALCPRICE_NAME + "_" + num, maxAmount);
}


// Stripe table rows using classes from CSS file. For even rows use tr.even td; for odd rows use tr.odd td;
// If tr already has a class assigned, the function won't redefine it.
function stripeRows( id ) {
	var 
		even = false,
		table = document.getElementById(id);

	if (! table) return;

	var tbodies = table.getElementsByTagName('tbody');

	for (var h = 0; h < tbodies.length; h++) {
    
		var trs = tbodies[h].getElementsByTagName('tr');
		
		for (var i = 0; i < trs.length; i++) {
			if ( !trs[i].className ) 
				trs[i].className = even ? 'even' : 'odd';
			
			even =  ! even;
		}
	}
}
