$(document).ready(function() {
  // прячем кнопку
  $('#sbmtbtn').css('visibility', 'hidden');
  
  // удаляем hidden`ы
  $('input[name=type]').remove();
  $('input[name=dimension]').remove();
  $('input[name=thickness]').remove();
  
  // создаем действие для всех select`ов
  $($('select')[0]).prepend('<option value="0">' + Lng.select + '</option>');
  var opts = $($('select')[0]).children('option');
  $(opts[0]).attr('selected', 'selected');
  $('select').bind('change', function(e) { onChange(this); });
});

function onChange(obj) {
  //var nextSiblings = $(obj).nextAll('select');
  var parentsDivs = $(obj).parent('div').nextAll('div');
  for(var i=0; i<parentsDivs.length; i++) {
    var nextSibling = $(parentsDivs[i]).children('select');
    if(nextSibling) {
      nextSibling.children().remove();
      nextSibling.attr('disabled', 'disabled');
      nextSibling.append('<option value="0" selected="selected">' + Lng.empty + '</option>');
    }
  }
  if(typeof nextSibling == 'undefined') {
    $('#sbmtbtn').css('visibility', 'visible');
  } else {
    $('#sbmtbtn').css('visibility', 'hidden');
  }
  
  /*
  if(nextSiblings.length) {
    nextSiblings.children().remove();
    nextSiblings.attr('disabled', 'disabled');
    nextSiblings.append('<option value="0" selected="selected">' + Lng.empty + '</option>');
  } else {
    $('#sbmtbtn').css('visibility', 'visible');
  }
  */
  var sId = 0;
  var childrens = $(obj).children('option');
  childrens.each(function() { if(this.selected) { sId = parseInt(this.value); } });
  
  if($(obj).nextAll('select').length || !sId) {
    $('#sbmtbtn').css('visibility', 'hidden');
  }
  
  if(sId && typeof nextSibling != 'undefined') {
    var XMLdata = $.ajax({
      url: "/~services:calculation@type=" + $(obj).attr('name') + ",id=" + sId,
      async: true,
      dataType: "xml",
      complete: function(XMLHttpRequest, textStatus){
        if (textStatus == "error") {
          alert("Невозможно получить данные");
        }
        else {
        }
      },
      success: function(XMLHttpRequest, textStatus){
        fillSiblingSelect(XMLHttpRequest, obj);
      },
      error: function(XMLHttpRequest, textStatus, errorThrown){
        alert("Ошибка при обработке данных: " + textStatus);
      }
    });
  }
}

function fillSiblingSelect(request, obj) {
  var rootElement = request.documentElement;
  if(rootElement.nodeName == 'error') { 
    alert(rootElement.firstChild.firstChild.nodeValue);
    return false; 
  }
  
  if(rootElement.hasChildNodes()) {
    //Explorer($(obj).parent('div'));
    var nextSibling = $($($(obj).parents('div')[0]).nextAll('div')[0]).children('select');
    if(!nextSibling.length) { nextSibling = $($($(obj).parents('div')[0]).nextAll('div')[1]).children('select'); }
    //alert($($($(obj).parents('div')[0]).nextAll('div')[0]).children().length);
    if(nextSibling.length) { 
      nextSibling.removeAttr('disabled');
      nextSibling.children('option').remove();
      nextSibling.append('<option value="0">' + Lng.select + '</option>');
      for(var e = rootElement.firstChild; e; e = e.nextSibling) {
        nextSibling.append('<option value="' + parseInt(e.getAttribute('id')) + '">' + e.firstChild.nodeValue + '</option>');
      }
    } else {
      $('#sbmtbtn').css('visibility', 'visible');
    }
  } 
}

function Explorer(obj)
{
  var msg = '';
  var i = 0;
  for(var p in obj) { ++i; msg += p + " "; if(i%5==0) { msg += "\n"; } } 
  alert(msg);
}

