
$(function(){
       $("#nojava").hide();
       $("#shedule_text").hide();
       $("#shedule_id").show();
       if($("#performance_id").val() < 1){
       	$("#shedule_id").attr("disabled","disabled");
       }
       if($("#shedule_id").val() < 1){
       	$("#fio").attr("disabled","disabled");
       	$("#kol").attr("disabled","disabled");
       	$("#email").attr("disabled","disabled");
       	$("#tel").attr("disabled","disabled");
       	$("#dop").attr("disabled","disabled");
       	$("#submit").attr("disabled","disabled");
       }
  $("#shedule_id").click(function(){
       if($("#shedule_id").val() > -1){
       	$("#fio").removeAttr("disabled");
       	$("#kol").removeAttr("disabled");
       	$("#email").removeAttr("disabled");
       	$("#tel").removeAttr("disabled");
       	$("#dop").removeAttr("disabled");
       	$("#submit").removeAttr("disabled");
		}
	});
  $("#submit").click(function(){
       doSubmit();
       return false;
       });
  $(".loading").css({
       "color": "red",
       "font-weight": "bold",
       "margin": "10px",
       "padding": "10px 0 0 20px",
       "width": "100px",
       "height": "50px",
       "display": "none",
       "background": "url(/img/ajax-loader-snake.gif) no-repeat 0% 50%"
       });
  $("select#performance_id").change(function(){
   if($(this).val()>-1){
       $("#load").show();
       $("#shedule_id").attr("disabled","disabled");
       $("#fio").attr("disabled","disabled");
       $("#kol").attr("disabled","disabled");
       $("#email").attr("disabled","disabled");
       $("#tel").attr("disabled","disabled");
       $("#dop").attr("disabled","disabled");
       $("#submit").attr("disabled","disabled");
	$("#aperf").html('<a href="/performance/?id=' + $(this).val() + '">Подробнее о спектакле</a>')
    $.getJSON("/ajax/tickets.html",{performance_id: $(this).val(), date_from: $("#date_from").val(), date_to: $("#date_to").val()}, function(j){
      $("#load").hide();
      var options = '';
      for (var i = 0; i < j.length; i++) {
        options += '<option value="' + j[i].oId + '">' + j[i].oVal + '</option>';
      }
      $("select#shedule_id").html(options);
      if(j.length > 0){
       $("#no_tickets").hide();
       $("#shedule_id").removeAttr("disabled");
       $("#fio").removeAttr("disabled");
       $("#kol").removeAttr("disabled");
       $("#email").removeAttr("disabled");
       $("#tel").removeAttr("disabled");
       $("#dop").removeAttr("disabled");
       $("#submit").removeAttr("disabled");
       } else        $("#no_tickets").show();
       })
    }
  })
})


function doSubmit(){
       var no_error = true;
       var perf = $("#performance_id").val();
       if (perf == -1) {
              $("#eperformance").show();
              $("#performance_id").focus();
              no_error = false;
              } else $("#eperformance").hide();      
       var shed = $("#shedule_id").val();
       if (shed == -1) {
              $("#eshedule").show();
              $("#shedule_id").focus();
              no_error = false;
              } else $("#eshedule").hide();
       var fio = $("input#fio").val();
       if (fio == "") {  
              $("#efio").show();
              $("input#fio").focus();
              no_error = false;
       } else $("#efio").hide();

       var kol = $("input#kol").val();
       if (kol == "") {  
              $("#ekol").show();
              $("input#kol").focus();  
              no_error = false;
       } else $("#ekol").hide();

       var tel = $("input#tel").val();
       if (tel == "") {  
              $("#etel").show();
              $("input#tel").focus();  
              no_error = false;
       } else $("#etel").hide();

       if(no_error){

              var dataString = 'send=1' 
                                   + '&performance_id=' + perf
                                   + '&shedule_id=' + shed
                                   + '&kol=' + kol
                                   + '&email=' + escape($("#email").val()) 
                                   + '&tel=' + escape($("#tel").val()) 
                                   + '&fio=' + escape(fio) 
                                   + '&dop=' + escape($("#dop").val()) 
              //+ $("form#order").serialize());
              $.ajax({
                     type: "GET",
                     url: "/ajax/tickets.html",
                     data: dataString,
                     beforeSend: function(){
                            $("#submit").attr("disabled","disabled");
                            $("#submit").val("Заказ отправляется...");
                            },
                     error: function(){
                            alert('На сервере произошка ошибка.\nПожалуйста, попробуйте еще раз.');
                            $("#submit").val("Сделать заказ");
                            $("#submit").removeAttr("disabled");
                            },
                     success: function(data, textStatus){
                            $('#t_order').html("<h1>Ваш заказ отправлен.</h1>");
                            document.location = '?sended=1';
                            }
                     });
              }
 
       return no_error;       
       }
       
