/**
 * Counts amount of the order
 * 
 * @param id
 * @return amount of the order
 * @author Pawel
 */
function countAmount() 
{
    arr = document.getElementsByClassName('amount');
    
    tmp = new Array(arr.length);
    
    sum = 0;
    
    for (i=0;i<arr.length;i++)
    {                             
      if (isNaN(parseFloat(arr[i].value)))
        tmp[i] = 0;
      else
        tmp[i] = parseInt(arr[i].value.replace(',', '.'));
          
      rowAmount = tmp[i]*parseFloat(document.getElementById('_' + arr[i].id).value) 
      
      $('row_' + arr[i].id).innerHTML = rowAmount;
      
      
      rowAmountStr = rowAmount.toFixed(2);
      
      rowAmountStr += '';
      
      rowAmountStr = rowAmountStr.replace('.',',');
      
      parsedStr = '';
      
      counter = -1;
      
      for (j=rowAmountStr.length -1; j>=0; j--)
      {
    	  char = rowAmountStr.charAt(j);
    	  
    	  if (counter >= 0) counter++;
    	  
    	  if (char == ',') counter = 0;    	      	  
    	  
    	  parsedStr = char + parsedStr;
    	  
    	  if (counter == 3)
    	  {
    		  parsedStr = ' ' + parsedStr;
    		  
    		  counter = 0;
    	  }
      }
      
      $('row_visible_' + arr[i].id).innerHTML = parsedStr;
      
      
      sum += rowAmount;
    }
    
    return sum;
}

/**
 * Method run when change box value is changed. When setting checkbox it sets value
 * of the input field to 0. When unsetting checkbox it sets input value to empty string
 * 
 * 
 * @param id of the checkbox 
 * @return
 */
function updateCheckbox(id,startDiscountArray)
{
	if ($('contract_price_list_checkbox_'+id).checked == 1)
	{
		$('contract_price_list_'+id).value = '1';
	}
    else
    {
    	$('contract_price_list_'+id).value = '';
    }
	
	showDiscounts(startDiscountArray);
	
	return; 
}

/**
 * 
 * Method run when input has been changed. If input is set to 0 then sets input value to empty
 * string and unsets corresponding checkbox
 * 
 * @param id
 * @return
 * @author Pawel
 */
function updateInput(id,startDiscountArray)
{
	value = parseInt($('contract_price_list_'+id).value);
	
	if (!value)
	{
		$('contract_price_list_checkbox_'+id).checked = 0;
	      
	    $('contract_price_list_'+id).value = '';
	}
	else
	{		
		if (value != 0)
	    { 
	      $('contract_price_list_checkbox_'+id).checked = 1;
	      
	      $('contract_price_list_'+id).value = value;
	    }
	    else
	    {
	      $('contract_price_list_checkbox_'+id).checked = 0;
	      
	      $('contract_price_list_'+id).value = '';
	    }
	}
	
	showDiscounts(startDiscountArray);
	
	return;
}

/**
 * As an argument method receives array of the form "discount_id"=>"discount_start_amount"
 * 
 * 
 * @param startDiscountArray
 * @author Pawel
 * @return
 */
function showDiscounts(startDiscountArray)
{
	sum = countAmount();		
	
	array = eval("(" + startDiscountArray + ")");
	
	if (array.length < 1) return;
	
	for (var index in array)
	{
		if (!$('discount_tick_'+index)) continue;
		
		if (sum >= array[index])
		{
			$('discount_tick_'+index).show();
		}
		else
		{
			$('discount_tick_'+index).hide();
		}			
	}
}
