function redirect(uri)
{
  document.location = uri;
}

function submitForm(id)
{
  document.getElementById(id).submit();
}

function ribbonInit(objId, elementCount, visibleCount, scrollStep, leftButtonId, rightButtonId)
{
  rObject = document.getElementById(objId);
  if (!rObject) return null;
  
  animSteps = 10;
  animStepDelay = 20;

  ribbon = new Object();
  ribbon.position = 1;
  ribbon.offset = 0;

  ribbon.lastPosition = elementCount - visibleCount + 1;
  if ( ribbon.lastPosition <= 0 ) ribbon.lastPosition = 1;
  ribbon.elementCount = elementCount;
  ribbon.scrollStep = scrollStep;

  ribbon.leftButton = document.getElementById(leftButtonId);
  ribbon.rightButton = document.getElementById(rightButtonId);

  ribbon.leftButton.className = "button-left inactive";

  if (elementCount <= visibleCount)
  {
    ribbon.rightButton.className = "button-right inactive";
  }
  
  rObject.style.position = 'relative';
  rObject.style.left = 0;
  rObject.ribbon = ribbon;
  
  rObject.ribbonScrollLeft = function()
  {
    if (this.ribbon.position == 1) return false;

    for (i = 0; i <= animSteps; i++)
    {
      offset = this.ribbon.offset + (this.ribbon.scrollStep / animSteps * i);
      setTimeout("ribbonDoScroll('" + this.id + "', " + offset + ");", animStepDelay * i);
    }

    this.ribbon.offset += this.ribbon.scrollStep;
    --this.ribbon.position;
    
    if (this.ribbon.position == 1)
    {
      this.ribbon.leftButton.className = "button-left inactive";
    }

    this.ribbon.rightButton.className = "button-right";
    
  }

  rObject.ribbonScrollRight = function()
  {
    if (this.ribbon.position == this.ribbon.lastPosition) return false;

    for (i = 0; i <= animSteps; i++)
    {
      offset = this.ribbon.offset - (this.ribbon.scrollStep / animSteps * i);
      setTimeout("ribbonDoScroll('" + this.id + "', " + offset + ");", animStepDelay * i);
    }

    this.ribbon.offset -= this.ribbon.scrollStep;
    ++this.ribbon.position;
    
    if (this.ribbon.position == this.ribbon.lastPosition)
    {
      this.ribbon.rightButton.className = "button-right inactive";
    }
    
    this.ribbon.leftButton.className = "button-left";
    
  }

  return rObject;

}

function ribbonDoScroll(objId, value)
{
  document.getElementById(objId).style.left = Math.round(value) + "px";
}

function showHideBlock(id)
{
  elm = document.getElementById(id);
  if ( elm.style.display == 'none' ) elm.style.display = 'block';
  else elm.style.display = 'none';
}

function showHideSwitch(id, caller)
{
  elm = document.getElementById(id);
  if ( elm.style.display == 'none' )
  {
    elm.style.display = 'block';
    caller.className = 'switch on';
  }
  else
  {
    elm.style.display = 'none';
    caller.className = 'switch';
  }
  
}

function switchBlocks(id, baseId, blocksCount)
{
  for (i = 1; i <= blocksCount; i++)
  {
    elm = document.getElementById(baseId + i);
    if (elm) elm.style.display = 'none';
  }

  elm = document.getElementById(baseId + id);
  if (elm) elm.style.display = 'block';

}

function switchImages(id, baseId, imgCount)
{
  for (i = 1; i <= imgCount; i++)
  {
    elm = document.getElementById(baseId + i);
    if (elm) elm.style.display = 'none';
  }

  elm = document.getElementById(baseId + id);
  if (elm) elm.style.display = 'inline';

}

function formatNumberAsCurrency(n)
{
  if ( isNaN(n) ) return "0 руб.";
  n = xRound( parseFloat(n), 2 );
  num = n.toString().split('.', 2);
  n = num[0].toString();
  res = "";
  k = 1;

  for ( i = n.length; i > 0; i-- )
  {
    if ( k++ % 3 == 1 ) res = " " + res;
    // res = n.[ i - 1 ] + res;
    res = n.substr(i - 1, 1) + res;
  }

  res += " руб.";
  if ( num[1] ) res += " " + num[1] + " коп.";
  return res;

} ////////////////////////////////////

function xRound(v, d)
{
  var f = Math.pow(10, d);
  return Math.round(v * f) / f;
}

function setOpacity(object, alpha)
{
  if (document.all) object.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + (alpha * 100) + ")";
  else object.style.opacity = alpha;
}
