function goNextRegistrationStep()
 {
    //downloadPartView("/profile/profile.php", "registration", {userType: $('input[name=userType]:checked').val() }, "POST");
   var userType = $('input[name=userType]:checked').val();
   placePreloader("registration");
   $.ajax({
      url : "/profile/profile.php",
      data : {userType: userType},
      cache : false,
      type: "POST",
      error : function(XMLHttpRequest, textStatus, errorThrown){
         //console.log or alert error
         alert("Ошибка: " + errorThrown);
      },
      success: function(data){
         //alert("Test: " + data);
         $("#registration").html(data);
         secondStepInit(data);
         return false;
      }
   });
   return false;
 }
 
 jQuery.extend(jQuery.validator.messages, {
        remote: "Этот емайл уже зарегистрирован."
 });
 
 function secondStepInit(data)
 {
   $("#profileForm").validate({
      submitHandler: function(form) {
              $("#btnSubmit").replaceWith("Регистрирую... ");
              form.submit();
              return false;
      },
      rules: {
         email: {
           required: true,
           email: true,
           maxlength: 100,
           remote: {
               url: "checkemail.php",
               type: "post",
               data: {
                 email: function() {
                   return $("#email").val();
                 },
                 userID: function() {
                   return $("#userID").val();
                 }
               }
             }
         },
         password: {
           required: true,
           minlength: 6,
           maxlength: 20
         },
         passwordConfirmation: {
           required: true,
           equalTo: "#password"
         },
         surname: {
            required: true,
            minlength: 3,
            maxlength: 50
         },
         name: {
            required: true,
            minlength: 3,
            maxlength: 30
         },
         sex: "required",
         yearStart: {
            required: true
         },
         yearFinish: {
            required: true
         },
         fack: {
            required: true
         },
         filial: {
            required: true
         },
         filePhoto: {
            accept: "jpg"
         }
      }
   });
   
   $("#dialogYears").dialog({
      width:600,
      title: "Внимание!",
      modal: true,
      buttons: {
         Ok: function() {
              $(this).dialog('close');
         }
      }
   });
   $("#dialogYears").dialog('close');
   
   
   //Birthday data
   $('#birthday').datepick( {
      minDate: '-100Y',
      maxDate: '-17Y',
      yearRange: '-100:+100',
      currentText: '',
      showOn: 'both',
      showStatus: true,
      buttonImageOnly: true,
      buttonImage: '/js/jquery/datepick/calendar.gif'
   });
   
   //Copy email
   $("#email").keyup(function() { syncEmail() } );
   $("#email").blur(function() { syncEmail() } );
   
   //Years for start and graduation
   var yearControl = 4;
   $("#isGraduated").click(function() {
      if ($("#isGraduated").attr("checked")) {
         var yearCurrent = new Date().getFullYear();
         var yearStarted = $("#yearStart").val();
         var yearDiff = yearCurrent - yearStarted;
         if (yearDiff < yearControl) {
            $("#isGraduated").removeAttr("checked");
            $("#dialogYears").html("<p>Вы не можете закончить университет за " + yearDiff + " год(а)!<br />Соответственно Вы не можете быть выпускником и выбрать дату окончания университета.<br /></br />Подсказка: Проверьте год поступления в университет.</p>");
            $("#dialogYears").dialog('open');
            //$.blockUI({ message: "Вы не можете закончить университет за " + (yearDiff) + " год(а)!\nК сожалению выбрать дату окончания университета не возможно в таком случае.\n\nПодсказка: Проверьте год поступления в университет." }); 
            //alert("Вы не можете закончить университет за " + (yearDiff) + " год(а)!\nК сожалению выбрать дату окончания университета не возможно в таком случае.\n\nПодсказка: Проверьте год поступления в университет.");
         }
         else {
            $("#yearFinish").removeAttr("disabled");
            formatYearFinish(yearCurrent, yearStarted);
            $('#yearFinish').focus();
         } 
      }
      else {
         $("#yearFinish").attr("disabled","disabled");
      }
   });
   
   $("#yearStart").change(function() {
      var yearCurrent = new Date().getFullYear();
      var yearStarted = $("#yearStart").val();
      var yearDiff = yearCurrent - yearStarted;
      if (yearDiff < yearControl) {
         $("#isGraduated").removeAttr("checked");
         $("#yearFinish").attr("disabled","disabled");
      }
      else {
         formatYearFinish(yearCurrent, yearStarted);   
      }
   });
   
   function formatYearFinish(yearCurrent, yearStarted) {
      $('#yearFinish').children().remove();
      $('#yearFinish').append('<option></option>');
      for (var i=(parseInt(yearStarted) + yearControl); i<=yearCurrent; i++) {
         $('#yearFinish').append('<option value="' + i + '">' + i + '</option>');
      }
      $('#yearFinish').find('option:first').attr("selected","selected")
   }
   
   $("#email").focus();
 }

 function syncEmail()
 {
  if ($("#email").val() != "") {
   $("#contactEmail").text($("#email").val());
  }
 }
 
 function saveNewMessage()
 {
     $("#btnSendMessage").replaceWith("Публикую...");
     $.ajax({
         url : "/startpage/newMessageForm.php",
         data : { message: $("#message").val() },
         cache : false,
         type: "POST",
         beforesend: function() {
             placePreloader("communication");
         },
         error : function(XMLHttpRequest, textStatus, errorThrown){
            //console.log or alert error
            alert(errorThrown);
         },
         success: function(data){
            getCommunicationFirstPage();
         }
     });

     return false;
 }
 
 $(document).ready(function(){
    $("#userTypeForm").validate({
        submitHandler: function() {
            goNextRegistrationStep()
        }
    });
   
   $("#listOfFilials").dialog({
      width:600,
      title: "Список филиалов доступных для регистрации",
      modal: true,
      buttons: {
         Ok: function() {
              $(this).dialog('close');
         }
      }
   });
   $("#listOfFilials").dialog('close');
   
    $("#popupListOfFilials").click(function(){
      $("#listOfFilials").dialog('open');
    });
 })
