$(document).ready(function() {
  $("a.cv_add_lang").live(
      'click',
      function() {
        var select_p_obj = $("div#cv_jazyky p.jazyk:last");
        var cloned_p_obj = select_p_obj.clone();
        var select_j_obj = select_p_obj
            .children("select[name='jazyk_id[]']");
        var selected_j_opt_val = select_j_obj.children(
            'option:selected').val();
        if (selected_j_opt_val != '0') {
          cloned_p_obj.children("select[name='jazyk_id[]']").children(
              "option[value='" + selected_j_opt_val + "']").remove();
        }
        cloned_p_obj.children("select[name='ju_id[]']").children(
            "option[value='0']").attr('selected', true);
        select_p_obj.children("a.cv_add_lang").remove();
        cloned_p_obj.insertAfter("div#cv_jazyky p.jazyk:last");
        return false;
      });
  $("input.ddv").focus(
      function() {
        if (!$(this).attr('default_value')
            || $(this).attr('default_value') == $(this).val()) {
          $(this).attr('default_value', $(this).val());
          $(this).val('');
        }
      });
  $("input.ddv").blur(function() {
    var value = $(this).val();
    var default_value = $(this).attr('default_value');
    if (value.length == 0 && default_value && default_value.length > 0) {
      $(this).val(default_value);
    }
  });
  $(".fields_box_opt, .fields_box_obl")
      .each(
          function() {
            var block_position = $(this).css('position');
            if (block_position == 'static') {
              $(this).css('position', 'relative');
            }
            if ($.browser.msie && parseInt($.browser.version) <= 6
                && $(this).css('float') == 'none') {
              var height = $(this).height();
              $(this).css('height', height + 'px');
            }
            $(this)
                .append(
                    '<div class="corner lt"></div><div class="corner lb"></div><div class="corner rt"></div><div class="corner rb"></div>');
          });
  $('div.js_alert_msg').each(function() {
    if (!$(this).attr('showed')) {
      var content = $(this).html();
      var js_content = content.replace(/\\n/, '\n');
      alert(js_content);
      $(this).attr('showed', '1');
    }
  });

});

function kalk_zp_refresh() {
  var typ_odberatela = $(":radio[name='typ_odberatela']:checked").val();
  var obec_id = $("#kalk_zp_obec").val();
  var spotreba = $("#kalk_zp_spotreba").val();
  var qs = 'ajax=ajax&form_sent=1';
  qs += '&typ_odberatela=' + encodeURIComponent(typ_odberatela);
  qs += '&obec_id=' + encodeURIComponent(obec_id);
  qs += '&spotreba=' + encodeURIComponent(spotreba);
  $.getJSON('/includes/kalkulacka-zalohovych-plateb.inc.php?' + qs, function(
      data) {
    $("#kalk_zp_doporuceny_tarif").val(data.typ_odberatela + data.tarif);
    $("#kalk_zp_opc").val(data.opc);
    $("#kalk_zp_mep").val(data.mep);
    $("#kalk_zp_result_container").html(data.html);
  });
  return false;
}
function vyber_certifikovaneho_partnera() {
  var segment = $(":radio[name='segment']:checked").val();
  var qs = 'ajax=ajax&form_sent=1';
  $(":checkbox[name='segmenty[]']:checked").each(function() {
    qs += '&segmenty[]=' + encodeURIComponent($(this).val());
  });
  $("input[name='okresy[]']:checked").each(function() {
    qs += '&okresy[]=' + encodeURIComponent($(this).val());
  });
  $("input[name='spotrebice[]']:checked").each(function() {
    qs += '&spotrebice[]=' + encodeURIComponent($(this).val());
  });
  $("input[name='cinnosti_zakladni[]']:checked").each(function() {
    qs += '&cinnosti_zakladni[]=' + encodeURIComponent($(this).val());
  });
  $("input[name='cinnosti[]']:checked").each(function() {
    qs += '&cinnosti[]=' + encodeURIComponent($(this).val());
  });
  $.getJSON('/includes/vyber-certifikovaneho-partnera.inc.php?' + qs, function(
      data) {
    $("#vcp_result_container").html(data.html);
  });
  return false;
}
function vyber_servisneho_partnera() {
  var qs = 'ajax=ajax&form_sent=1';
  $("input[name='okresy[]']:checked").each(function() {
    qs += '&okresy[]=' + encodeURIComponent($(this).val());
  });
  $("input[name='spotrebice_kat[]']:checked").each(function() {
    qs += '&spotrebice_kat[]=' + encodeURIComponent($(this).val());
  });
  $("input[name='spotrebice[]']:checked").each(function() {
    qs += '&spotrebice[]=' + encodeURIComponent($(this).val());
  });
  $.getJSON('/includes/vyber-servisneho-partnera.inc.php?' + qs,
      function(data) {
        $("#vsp_result_container").html(data.html);
      });
  return false;
}
