// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

_translations = {
  "OK": "OK",
  "Now": "Sada",
  "Today": "Danas"
}

Date.weekdays = $w("N P U Sr Č P Su");

Date.months = $w("Siječanj Veljača Ožujak Travanj Svibanj Lipanj Srpanj Kolovoz Rujan Listopad Studeni Prosinac" );





function change_qty(qty_div_id, sum_div_id, step, unit_price, mu, allow_zero, use_step)
{
    var qty_div = document.getElementById(qty_div_id);
    if(qty_div)
    {
        var qty_div_value = new Number(get_value(qty_div));      
        if(isNaN(qty_div_value)) 
        {
          set_value(qty_div,  formatted_value(step, mu, 1));
          update_sum(sum_div_id, qty_div, unit_price);
          return false;
	}
        else if(qty_div_value > 999.9)
        {
          qty_div_value = find_real_qty(999.9, mu, step);
          set_value(qty_div, qty_div_value);
          update_sum(sum_div_id, qty_div, unit_price);
          return false;
        }
        else if(qty_div_value < new Number(step))
        {
          qty_div_value = Math.abs(new Number(step));
          set_value(qty_div, formatted_value(qty_div_value, mu, 1));
          update_sum(sum_div_id, qty_div, unit_price);
          return false;
        }
        
        qty_div_value = find_real_qty(qty_div_value, mu, step);
        set_value(qty_div, formatted_value(qty_div_value, mu, 1));
        update_sum(sum_div_id, qty_div, unit_price);
        
        if((qty_div_value + new Number(step)) <= 999.9)
        {
          if(use_step)
            qty_div_value += (new Number(step));
          
          if((allow_zero && qty_div_value.toFixed(1) >= 0) || (qty_div_value.toFixed(1) >= Math.abs(new Number(step)).toFixed(1)))
          {
            set_value(qty_div, formatted_value(qty_div_value, mu, 1));
            update_sum(sum_div_id, qty_div, unit_price);
          } 
        }
    }        
    return false;
}

function get_value(o) 
{
  if (o.value)
    return o.value;
  else
    return o.innerHTML;
}

function set_value(o, val)
{
  if (o.value || o.value == '')
    o.value = val;
  else
    o.innerHTML = val;
}

function formatted_value(value, mu, places)
{
  if(mu == "KG")
    return new Number(value).toFixed(places);
  else
    return new Number(value).toFixed(0);
}

function update_sum(sum_div_id, qty_div, unit_price)
{
  var sum_div = document.getElementById(sum_div_id);
  if(sum_div && unit_price)
  {
    var qty = new Number(get_value(qty_div));  
    if(isNaN(qty)) 
      qty = new Number(0);
  
    var sum = new Number(qty * unit_price);
    set_value(sum_div, formatted_value(sum, 'KG', 2));
  }
}

function find_real_qty(value, mu, step)
{
  if(mu == 'KO')
  {
    if(value > 999)
      return Number(999);
    else
      return Number(new Number(value).toFixed(0));
  }
  else
  {
    for(i = value.toFixed(1); i > 0; i=Number(i - 0.1).toFixed(1))
    {
      var modul =  Number(i % Math.abs(step)).toFixed(1);
      if(modul == '0.0' || modul == Math.abs(step).toFixed(1))
          return Number(i);
    }
    return 0;
  }  
}

function do_change_qty(e, qty_div_id, sum_div_id, step, unit_price, mu, use_step)
{
  var code;
  if (!e) 
    var e = window.event; 
  
  if (e.keyCode) 
    code = e.keyCode;
  else if (e.which) 
    code = e.which;
  else
    code = 8;

  if(code == 8 || code == 46 || (code >= 48 && code <= 90) || (code >= 96 && code <= 105))
    return change_qty(qty_div_id, sum_div_id, step, unit_price, mu, use_step);
  else
    return false;
}

//Function is called on onload event in body element
//It purpose is to position smallcart to show product 
//which qty is last changed.
function positionScrollbar( elementId, cont )
{
	basketItem = document.getElementById(elementId);
	container = document.getElementById(cont);
	if ( container == null )
	  return;
	if (basketItem != null )
	{
	  //we are calculating offset from first <td> element 
	  //inside <tr> to containg <table> element.
	  //because of safari bug it is not possible to calculate
	  //directly from <tr> element. 
	  yCoordElementId = basketItem.cells[0].offsetTop;
	}
	//cart scrollbar position is maintained between requests
	//saved in cookie 
	strCook = document.cookie; 
	strPos = 0;
	if( strCook.indexOf("!~") != 0 )
	{ 
	       intS = strCook.indexOf("!~"); 
	       intE = strCook.indexOf("~!"); 
	       strPos = strCook.substring(intS+2,intE); 
	}
	if ( basketItem != null )
	{ 
		if ( strPos <= yCoordElementId && yCoordElementId < ( parseInt(strPos) + parseInt(container.clientHeight)-10))
		{
		   scrollTopPos = strPos;
		} else
		{
		   scrollTopPos = yCoordElementId;
		}
	} else
	{
	    scrollTopPos = strPos;
	}
	container.scrollTop = scrollTopPos;
    setScrollPosition( cont );
}

//Function is called on scroll event in the smallcart.
//Purpose is to save position of scrollbar in cookie
function setScrollPosition( container )
{ 
    container = document.getElementById( container );
    intY = container.scrollTop;
    document.cookie = "yPos=!~" + intY + "~!;path=/"; 
}


