(function($){
  $(function(){
  
  /*var warningText = "Внимание! Сайт работает в аварийном режиме. Вы видите спасенную копию от 11.02.2010. Работы по восстановлению и переходу сайта в рабочий режим будут продолжаться до 2 апреля. Все будет хорошо!";
  
  if(!(window.location.href.indexOf('udivland.ru')))
    $('<div id="attention">'+warningText+' <a href="#">Закрыть</a></div>')
      .css({
        position: 'absolute',
        top: '0',
        left: '0',
        width: '70%',
        textAlign: 'center',
        padding: '10px 15%',
        backgroundColor: '#000',
        opacity: '0.5',
        color: '#fff',
        zIndex: '1000'
      }).appendTo($('body')).find('a').css({
        color: '#fff',
        position: 'absolute',
        top: '10px',
        right: '15px'
      })
      .click(function() {
        $(this).parents('div#attention').hide();
        return false;
      });*/
  
  // Для всех форм, мы разрешаем их отправку только тогда, когда все ОБЯЗАТЕЛЬНЫЕ
  // поля заполнены
  $("form").submit(function(event){
  
    var Items = $(".required", $(this));
    var submitedForm = this;
    
    var Radios = {};
    var Fields = new Array();
  
    Items.each(function(){
    
      if($(this).css("display") == "none")
        return;
              
      if(this.type == "text"
         && $(this).hasClass("not-valid-email") ){
      
        alert("Поле \"" + $(this).attr("title") + "\" должно иметь верное значение!");
        this.focus();   
        return false;  
      }
  
      // radio кнопки
      if( this.type == "radio" ) {
  
        if( !Radios[$(this).attr("name")] ){
          Radios[$(this).attr("name")] = {
            name: $(this).attr("name"),
            title: $(this).attr("title"),
            firstItem: this,
            values: new Array()
          };
        }
  
        Radios[$(this).attr("name")].values[$(this).attr("value")] = $(this).attr("text");
  
        if( $(this).attr("checked") )
          Radios[$(this).attr("name")].value = $(this).val();
      }
      else if( !$(this).val()
               && $.inArray(this.tagName.toLowerCase(), ["input", "textarea", "select"]) >= 0
               && $.inArray(this.type, ["checkbox", "hidden", "submit", "button"]) == -1 ) {
  
        var Regs = $(this).attr("title").split(/\s*\|\s*/);
  
        var Title = $(this).attr("name");
  
        if( Regs[0] )
          Title = Regs[0];
  
        Fields.push(Title);
  
        if( Fields.length == 1 )
          this.focus();
      }
    });
  
    $.each(Radios, function(){
  
      if( !$(this).val() ) {
  
        var Title = $(this).attr("name");
  
        var Regs = $(this).attr("title").split(/\s*\|\s*/);
  
        if( Regs[0] )
          Title = Regs[0];
  
        Fields.push(Title);
  
        //if( Fields.length == 1 )
          //this.focus(Radios[i].firstItem);
      }
    });
    
    //alert(Fields);
  
    if( Fields.length ) {
  
      alert("Пожалуйста, заполните пол" + (Fields.length == 1 ? "е":"я:") + " «" + Fields.join("», «") + "».");
  
      return false;
    }
  
    return true;
  });
  
});
})(jQuery);
