$(document).ready(function(){
    $("#searchForm").submit(function(e){
      search();
      $("#scrollToResult").trigger("click");
      e.preventDefault;
      return false;
    });
    
    //Auto scrolling to anchor
    //Got from http://beski.wordpress.com/2009/04/21/scroll-effect-with-local-anchors-jquery/
    $(".scroll").click(function(event){
        //prevent the default action for the click event
        event.preventDefault();
 
        //get the full url - like mysitecom/index.htm#home
        var full_url = this.href;
 
        //split the url by # and get the anchor target name - home in mysitecom/index.htm#home
        var parts = full_url.split("#");
        var trgt = parts[1];
 
        //get the top offset of the target anchor
        var target_offset = $("#"+trgt).offset();
        var target_top = target_offset.top;
 
        //goto that anchor by setting the body scroll top to anchor top
        $('html, body').animate({scrollTop:target_top}, 1500);
    });

    $("#surname").focus();
})

function search()
{
   placePreloader("searchResult");
   name = $("#name").val();
   surname = $("#surname").val();
   secondName = $("#secondName").val();
   yearStart = $("#yearStart").val();
   yearFinish = $("#yearFinish").val();
   fack = $("#fack").val();
   filial = $("#filial").val();
   group = $("#group").val();
   obshaga = $("#obshaga").val();
   
   $.ajax({
      url : "/search/actionSearch.php",
      data : {name: name, surname: surname, secondName: secondName, yearStart: yearStart, yearFinish: yearFinish, fack: fack, filial: filial, group: group, obshaga: obshaga},
      cache : false,
      type: "POST",
      error : function(XMLHttpRequest, textStatus, errorThrown){
         //console.log or alert error
         //alert(errorThrown);
      },
      success: function(data){
         $("#searchResult").html(data);
         return false;
      }
   });
   return false;
}
