// eCommerce FO JavaScript

// Disables all inputs with a given name. Use it in "onload" event.
function initInputs ( controlInputName ) {
	var 
		num = 1,
		definedElements = arguments;
		
	while ( document.getElementById ( controlInputName + '_' + num ) ) {
		
		if ( document.getElementById ( controlInputName + '_' + num ).type == 'checkbox' 
			&& document.getElementById ( controlInputName + '_' + num ).checked )
		
			highlightRow ( document.getElementById ( controlInputName + '_' + num ) )
			
		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.match(/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.match(/selected/) ) {
		tr.className = tr.className + ' selected';
		input.disabled = false;
		minus.disabled = false;
		plus.disabled = false;
		if ( input.value == '0') {
			input.value = '1';
		}
	}
	else {
		tr.className = tr.className.replace(/[ ]?selected([ ]?)/,'$1');
		//input.value = '0';
		input.disabled = true;
		minus.disabled = true;
		plus.disabled = true;
		stripeRows ( 'prod-list' );
		
	}
	calculatePrice ( 'price_' + num, 'amount_' + num , 'calcprice_' + num );		
}
// Controls user input and calculates the price using the following formula: calcPrice = price * amount.
function calculatePrice ( priceId, amountId, targetId ) {
	
	var 
		price = document.getElementById ( priceId ).innerHTML,
		amount = parseInt ( document.getElementById ( amountId ).value ),
		target = document.getElementById ( targetId ),
		newPrice = null;
	
	price = toNum ( price );
	
	if ( isNaN ( amount ) ) {
		amount = 0;
		document.getElementById ( amountId ).value = '';
	}
	
	newPrice = price * amount;
	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++ )
			summ += parseFloat ( allCells[i].value );
	
	summ = toScreenNum ( summ );
		
	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 ) {
	var 
		priceObj = document.getElementById ( priceId ),
		amountObj = document.getElementById ( amountId ),
		price = parseFloat (priceObj.innerHTML),
		amount = parseInt (amountObj.value);
	if ( !amountObj.disabled ) {
		amount++;
		document.getElementById ( amountId ).value = amount;
		calculatePrice ( priceId, amountId, targetId );
	}
	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 );
	}
	else return;
}

// 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;
		}
	}
}
