$(function()
 {
   var msgmail = $('input[name=msgmail]');
  /*---------------------------------------------------------------------------------------*/
  $('#sendpm').click( function() {
      if($('#sendpmbox').toggle().is(':visible'))
         msgmail.focus();
      return false;
    });
  $('#closesendpm').click( function() {
      $('#sendpmbox').fadeOut('slow');
      return false;
    });
  /*---------------------------------------------------------------------------------------*/
  $('#sendmsg').submit(function()
    {
     $('#sendprivmsg').get(0).disabled=true;
    });
  var alertmsg = $('#alertmsg');
  msgmail.blur(function()
    {
     var mail = $(this).val();
     if(""==mail) return true;

     r1 = /(@.*@)|(\.\.)|(@\.)|(^\.)/;
     r2 = /^.+\@[a-z0-9\-\.]+\.([a-z]{2,4})$/i;

     if(r1.test(mail) || !r2.test(mail))
        alertmsg.html('Вы ошиблись при вводе e-mail').show();
     else
        alertmsg.hide();
    });

   var msglen   = $('#msgtextlen');
   var msgtext  = $('textarea[name=msgtext]');
   var max    = parseInt(msglen.attr('max'));
   function calctalen()
     {
      var plen=$(this).val().length;

      msglen.val(max-plen);

      if(plen>max)
         {
          alertmsg.html('Вы привысили допустимую длину в '+max+' символов').show();
          $(this).val( $(this).val().substr(0,max+1) );
          msglen.val(max-$(this).val().length);
         }
      else
         alertmsg.hide();
     }
   msgtext.bind('keydown', calctalen).bind('keyup', calctalen).bind('change', calctalen);
   msgtext.blur(function()
     {
      var MsgText ='';
      var ThisText = $(this).val();
      var MsgFlag = false;

      ThisText = ThisText.replace(/ +/g,' ');
      ThisText = $.trim(ThisText);
      $(this).val(ThisText);

      var text1 = ThisText;
      ThisText = ' '+ThisText+' ';

      ThisText = ThisText.replace(/http.*?\s/ig,'');
      ThisText = ThisText.replace(/www.*?\s/ig,'');
      ThisText = ThisText.replace(/[\s,;][-\.0-9a-zA-Z]+\.(ru|com|net|biz|su|info|org)\S*[\s\.,;:]/ig,'');
      ThisText =  $.trim(  ThisText );

      if( text1!=ThisText)
        {
         MsgFlag = true;
         MsgText += '<li>Указывать веб сайт запрещено</li>';
         $(this).val(ThisText);
        }
      if( '' == ThisText)
        {
         MsgFlag = true;
         MsgText += '<li>Поле не заполнено</li>';
        }
      else
      if( ThisText == ThisText.toUpperCase() )
        {
         MsgFlag = true;
         MsgText += '<li>Нельзя набирать текст полностью в верхнем регистре (БОЛЬШИМИ буквами)</li>';
         $(this).val( ThisText.toLowerCase() )
        }
      if( ThisText.length > max )
        {
         lenbox.show();
         MsgFlag = true;
         MsgText += '<li>Превышен лимит длины сообщения '+max+'</li>';
        }
      if( ''==MsgText)
         alertmsg.hide();
      else
         MsgText = '<ul>'+MsgText+'</ul>';
      if( MsgFlag && ''!=MsgText)
        {
         alertmsg.html( MsgText );
         alertmsg.fadeIn("slow");
        }
     });
  /*---------------------------------------------------------------------------------------*/
   msglen.focus(function(){msgtext.focus();});
  /*---------------------------------------------------------------------------------------*/
  var content = document.getElementById('content'), posttitle = $('.posttitle',content), posttext = $('.posttext',content);
  var replObj = {'а':'a','е':'e','о':'o','р':'p','с':'c','х':'x','К':'K','Е':'E','Н':'H','М':'M','А':'A','Е':'E','О':'O','Р':'P','С':'C','Х':'X'};
  posttitle.html(posttitle.html().replace(/[аеорсхКЕНМАЕОРСХ]/g, function(x){return replObj[x];}));
  posttext.html(posttext.html().replace(/[аеорсхКЕНМАЕОРСХ]/g, function(x){return replObj[x];}));
  /*---------------------------------------------------------------------------------------*/
    $('.pboximg',content).click(function(clk){
      var targ = clk.target,imgs=$('img',this),targInd = imgs.index( targ );
      if(!$(targ).is('img'))
         return false;
      showimgbox(targInd, imgs.eq(0).attr('src'), $(this).attr('imgbox').split(','));
      return false;
    });
  /*---------------------------------------------------------------------------------------*/
 });