$(document).ready(function() {
        var docheight = $(document).height();
        $("#menu_container").height(docheight);

        $(window).resize(function() {
                var winheight = $(window).height();
                if(winheight > docheight){
                    $("#menu_container").height(winheight);
                } else {
                    $("#menu_container").height(docheight);
                }
        });

        $('#select-all').click(function(event) {
                if(this.checked) {
                    // Iterate each checkbox
                    $(':checkbox').each(function() {
                            this.checked = true;
                    });
                }else{
                    $(':checkbox').each(function() {
                            this.checked = false;
                    });
                }
        });
        $('#inverse').click(function(event) {
                $(':checkbox').not('#select-all').each(function() {
                        if(this.checked == false){
                            this.checked=true;
                        }else{
                            this.checked=false;
                        }
                });
        });


        $("#delete").click(function(event){

                var $form = $("form");
                event.preventDefault();
                if(confirm("Are you sure you want to delete?")){



                    $("#saveResult").fadeIn("slow");

                    $("#saveResult").html("Deleting...");

                    var datas = $form.serialize();
                    var  url = $form.attr( 'action' );

                    $.ajax({
                            url: url,
                            type: 'POST',
                            dataType: 'html',
                            data: datas,
                            success: function(response, textStatus, XMLHttpRequest) {
                                if (!response){

                                    $("#saveResult").html("There was an error!").delay(2000).fadeOut("slow");
                                    return false;
                                }
                                else {
                                    $("#saveResult").html(response).delay(2000).fadeOut("slow");
                                    if(response == 'Deleted!'){
                                        $("input[type=checkbox]:checked").not("#select-all").each(function(){
                                                $(this).closest('tr').fadeOut();
                                        });
                                    }
                                }
                            },
                            error: function(XMLHttpRequest, textStatus, errorThrown) {
                                $("#saveResult").html("There was an error!");
                                if (typeof console != 'undefined')
                                    console.dir(XMLHttpRequest);
                                return false;
                            }
                    });


                }
        });


        $("#save").click(function(){

           $("#generateForm").trigger("submit");
        });

});


        $("#generateForm").submit(function(event){
                var $form = $("#generateForm");
                var myvalidator = $('#generateForm').bValidator();
                event.preventDefault();
                if(myvalidator.data('bValidator').isValid()) {


                    $("#saveResult").fadeIn("slow");

                    $("#saveResult").html("Submitting...");

                    var datas = $form.serialize();
                    var  url = $form.attr( 'action' );
                    $.ajax({
                            url: url,
                            type: 'POST',
                            dataType: 'html',
                            enctype: 'multipart/form-data',
                            data: datas,
                            success: function(response, textStatus, XMLHttpRequest) {
                                if (!response){

                                    $("#saveResult").html("There was an error!").delay(2000).fadeOut("slow");
                                    return false;
                                }
                                else {
                                    if(response == 'Created!'){
                                        $("#save").hide();
                                    }
                                    $("#saveResult").html(response).delay(2000).fadeOut("slow");


                                }
                            },
                            error: function(XMLHttpRequest, textStatus, errorThrown) {
                                $("#saveResult").html("There was an error!");
                                if (typeof console != 'undefined')
                                    console.dir(XMLHttpRequest);
                                return false;
                            }
                    });

                }
        });

jQuery(document).ready(function($) {
        $('.ConfirmDelete').click(function() {
                return confirm('Delete record?');
        });
});
