/**
 * Created by IOAN on 11/14/2015.
 */
$(document).ready(function(){

    $('#login-submit').on('click', function(e){

        if($('#login-form')[0].checkValidity()) {
            e.preventDefault();
            $('#login-form').trigger('submit');
        }
    });


    $('#login-form').on('submit', function(e){


        if($('#login-form')[0].checkValidity()) {
            e.preventDefault();
            var data_remote = $(this).data('remote');
            if(data_remote == "local") {
                var form_data = $('#login-form').serializeObject();
                form_data['csurl'] = encodeURIComponent(full_uri + "amodules/account/login_process.php");
                form_data['baseurl'] = baseurl;
                //console.log(form_data);
                $.ajax({
                    url: baseurl + '/proxy.php',
                    data: form_data,
                    type: 'post',
                    dataType: 'json',
                    cache: false,
                    success: function (r) {
                        if (r.result == 1) {
                            if (r.hasOwnProperty('flashm')) {
                                $.post(baseurl + '/flashm.php', {'flashm': r.flashm});
                            }
                            window.redirectLink = r.link;
                            window.setTimeout(function () {
                                var rlink = window.redirectLink;
                                delete(window.redirectLink);
                                document.location.href = rlink;
                            }, 1000);

                        }
                        else {
                            $('.alert-error').removeClass('hidden').html(r.error);
                        }

                    }
                });
            }
            else {
                return true;
            }
        }



    });


});