var conn1_ch = false;
var conn2_ch = false;

$(document).ready(function(){
 
function init_form(){

    if(!conn1_ch){
      $('#conn1').attr('value', 'Телефон');
    }
    $('#conn1').click(function(){
      if(!conn1_ch){
        var st = $(this).attr("value");
        if (st == "Телефон"){
          $(this).attr("value","");
        }
      }
    }).change(function(){
      conn1_ch=true;
    }).blur(function(){
      if(!conn1_ch){
        $(this).attr('value', 'Телефон');
      }
    });

    if(!conn2_ch){
      $('#conn2').attr('value', 'e-mail');
    }
    $('#conn2').click(function(){
      if(!conn2_ch){
        var st = $(this).attr("value");
        if (st == "e-mail"){
          $(this).attr("value","");
        }
      }
    }).change(function(){
      conn2_ch=true;
    }).blur(function(){
      if(!conn2_ch){
        $(this).attr('value', 'e-mail');
      }
    });

}

 init_form();

  $('#formmail').submit(function(){
    if(!conn1_ch){$('#conn1').attr('value', '');}
    if(!conn2_ch){$('#conn2').attr('value', '');}
    var params = $(this).serialize();
    $(this).hide();
    //html('<p style="width:100%;padding:120px 10px;text-align:center;">...&nbsp;подождите идёт отправка сообщения&nbsp;...</p>');
    $('input[name=formsubmit]').attr('disabled','1');
    
    $.post('/feedback',params,function(ret){

      $('.imput_error').hide();
      $('input[name=formsubmit]').attr('disabled','');
      if( ret.stat == '1' ){
	    for ( keyVar in ret.err ) {
	      $('#err_'+ret.err[keyVar]['id']).html(ret.err[keyVar]['msg']).show();
	      $('input[name='+ret.err[keyVar]['id']+']').css('background','#f3b6b6').focus(function(){$('#err_'+$(this).attr('name')).hide();$(this).css('background','#ffffff');});
	      $('textarea[name='+ret.err[keyVar]['id']+']').css('background','#f3b6b6').focus(function(){$('#err_'+$(this).attr('name')).hide();$(this).css('background','#ffffff');});
	    }
	  $('#formmail').show();

      }else{

        // Форма отправилась отлично
        send_message('Спасибо за сообщение'); 
	$('#obr_svjaz').html('Спасибо за сообщение');
	$('.default_sendmail').remove();

      }
    },'json');
    return false;
 });

// вызывает латбокс с сообщением
function send_message(text){
    if ( $('#message_overlay').length )
    {
    } else {
        var html = '<div id="message_overlay" class="content"><div class="close"></div><p id="ms_mesage"></p></div>';
        $('body').append(html);
    }
    $('#ms_mesage').html(text);   
    
    $("#message_overlay").overlay({
        expose: '#000',
        closeOnClick: false,
        api: true
    }).load();
}
 
 });
