function chooseSelectBoxValue(formName, formElement, value) {
  elem = document.forms[formName].elements[formElement];
  if (elem) {
    opts = elem.options;
    for (i=0; i<opts.length; i++) {
      if (value == opts[i].value) {
        elem.selectedIndex = i;
        opts[i].seleted = true;
        break;
      }
    }
  }
}

function askDelete() {
  return confirm('Подтвердите операцию');
}

function toggleCompareBlockItem(cb, idToProcess, itemTitle, detailsURL) {
  if (cb.checked) {
    if ($('#compareList li').size() < 4) {
      $.post('/action.php?component=Shop&action=addCompareItem',
       {'itemId': idToProcess},
       function (data, textStatus) {
          if (textStatus == 'success') {
            if (data.result == 'ok') {
              $('#compareLink').before('<li id="compareItem'+idToProcess+'"><a href="'+detailsURL+'">'+itemTitle+'</a></li>');
              $('#compareLink').show();
            }
          }
       },
       'json'
      )
    } else {
      cb.checked = false;
    }
  } else {
    $.post('/action.php?component=Shop&action=deleteCompareItem', {'itemId': idToProcess});
    $('#compareItem'+idToProcess).remove();
    if ($('#compareList li').size() == 1) {
      $('#compareLink').hide();
    }
  }
}

function addToBookMarks(itemId) {
  $.post('/action.php?component=Bookmarks&action=addBookmark',
    {'itemId': itemId},
    function (data, textStatus) {
      if (textStatus == 'success') {
        if (data.result == 'ok') {
          $("#bookmarksLink").colorBlend([{toColor:"white", param:"color", cycles:1, duration: 1000}]);
          $("#bookmarksLink span").html(data.count); 
        }
      }
    },
    'json'
  )
}

  function updateTB(tbId, value) {
    tbI = document.getElementById(tbId);
    if (value != '') {
      tbI.src = '/resize/width/100' + value.replace(/http:\/\/[^\/]+/gi,'');
      tbI.style.display = 'inline';
    } else {
      tbI.style.display = 'none';
    }
  }

  function renderFMControl(inputName, inputValue) {
    document.writeln(
      '<input type="text" name="' + 
      inputName + 
      '" value="' + 
      inputValue +
      '" id="FM' + 
      inputName + 
      '" onchange="updateTB(\'TB'+ 
      inputName +
      '\', this.value)" /><br />');
    document.writeln('<button onclick="return setFile(\'FM' + inputName + '\');" class="button">[выбрать]</button>&nbsp;');
    document.writeln('<button onclick="document.getElementById(\'FM' + inputName + '\').value = \'\'; updateTB (\'TB' + inputName + '\', \'\');" class="button">[очистить]</button>');
    document.writeln('<img style="display: none" id="TB'+ inputName + '" /><hr />');
    
    updateTB('TB'+inputName, inputValue);
  }

  function setFile(elementId) {
    var win = window.open('/lib/ajaxfilemanager/ajaxfilemanager.php?editor=form&elementId='+elementId, 'ajaxFileImageManager', 'width=782,height=440');   
    return false;
  }
  
function handleNoNewPost() {
  $('.newPost').each(function() {
    this.disabled = true;
    this.checked = false;
  })
}

function handleNewPost() {
  $('.newPost').each(function() {
    this.disabled = false;
  })
}

function submitIPPForm(ipp) {
  document.forms['ippForm'].elements[0].value=ipp;
  document.forms['ippForm'].submit();
  return false;
}

function processSupportForm(itemId, questionTypeIndex, group) {
  theForm = document.forms['requestForm'];
  theForm.elements[group + 'questionType'].selectedIndex = questionTypeIndex;
  theForm.elements[group + 'questionType'].onchange();
  theForm.elements[group + 'itemId'].value = itemId;
  return false;
}