/**
 * Function to validate the login form
 */
function validateLoginForm(id) {
    $(id).validate({
        rules: {
            '_username': {
                required: true,
                email: true
            },
            '_password': {
                required: true
            }
        },
        messages: {
            '_username': {
                required: 'Email required',
                email: 'Enter a valid email address'
            },
            '_password': {
                required: 'Password required'
            }
        }
    });
}


/**
 * Function to validate the password request form
 */
function validateRequestForm(id) {
    $(id).validate({
        rules: {
            'username': {
                required: true,
                email: 5
            }
        },
        messages: {
            'username': {
                required: 'Email required',
                email: 'Enter a valid email address'
            }
        }
    });
}


/**
 * Function to validate the password reset form
 */
function validateResetForm(id) {
    $(id).validate({
        rules: {
            'fos_user_resetting_form[plainPassword][first]': {
                required: true,
                minlength: 5
            },
            'fos_user_resetting_form[plainPassword][second]': {
                required: true,
                equalTo: "#fos_user_resetting_form_plainPassword_first"
            }
        },
        messages: {
            'fos_user_resetting_form[plainPassword][first]': {
                required: 'Please enter your account\'s new password',
                minlength: 'Enter at least 5 characters'
            }
            ,
            'fos_user_resetting_form[plainPassword][second]': {
                required: 'Please confirm password',
                minlength: 'Enter at least 5 characters',
                equalTo: 'Passwords don\'t match'
            }
        }
    });
}

function validateExpertiseForm(id) {
    $(id).validate({
        rules: {
            'expertise[name]': {
                required: true
            }
        },
        messages: {
            'expertise[name]': {
                required: 'Name required'
            }
        }
    });
}

function validateSubSpecialityForm(id) {
    $(id).validate({
        rules: {
            'expertise[name]': {
                required: true
            }
        },
        messages: {
            'expertise[name]': {
                required: 'Name required'
            }
        }
    });
}


function validateAdminForm(id) {
    $(id).validate({
        rules: {
            'user[email]': {
                required: true,
                email: true,
                remote: Routing.generate('app_web_user_check_email')
            },
            'user[firstName]': {
                required: true,
                RESTRICTIONS: true
            },
            'user[lastName]': {
                required: true,
                RESTRICTIONS: true
            },
            'user[address]': {
                required: true,
                RESTRICTIONS: true
            },
            'user[postcode]': {
                required: true,
                RESTRICTIONS: true
            },
            'user[phone]': {
                number: true
            },
            'user[country]': {
                required: true
            },
            'user[plainPassword][first]': {
                required: true,
                minlength: 8
            },
            'user[plainPassword][second]': {
                required: true,
                equalTo: "#user_plainPassword_first"
            }
        },
        messages: {
            'user[firstName]': {
                required: "First name required"
            },
            'user[lastName]': {
                required: "Surname required"
            },
            'user[email]': {
                required: "Email address required",
                email: "Email address not valid",
                remote: "This email address already used"
            },
            'user[address]': {
                required: "Address required"
            },
            'user[postcode]': {
                required: "Post code required"
            },
            'user[country]': {
                required: "Select country"
            },
            'user[plainPassword][first]': {
                required: "Password required",
                minlength: "Password must be at least 8 characters"
            },
            'user[plainPassword][second]': {
                required: "Confirm password",
                equalTo: "Passwords do not match"
            }

        }
    });
}

function validateEditAdminForm(id) {
    $(id).validate({
        rules: {
            'user[email]': {
                required: true,
                email: true,
                remote: Routing.generate('app_web_user_check_email')
            },
            'user[firstName]': {
                required: true,
                RESTRICTIONS: true
            },
            'user[lastName]': {
                required: true,
                RESTRICTIONS: true
            },
            'user[address]': {
                required: true,
                RESTRICTIONS: true
            },
            'user[postcode]': {
                required: true,
                RESTRICTIONS: true
            },
            'user[phone]': {
                required: true,
                RESTRICTIONS: true,
                number: true
            },
            'user[country]': {
                required: true
            },
            'user[plainPassword][first]': {
                required: false,
                minlength: 8
            },
            'user[plainPassword][second]': {
                required: false,
                equalTo: "#user_plainPassword_first"
            }
        },
        messages: {
            'user[firstName]': {
                required: "First name required"
            },
            'user[lastName]': {
                required: "Surname required"
            },
            'user[email]': {
                required: "Email address required",
                email: "Email address not valid",
                remote: "This email address already used"
            },
            'user[address]': {
                required: "Address required"
            },
            'user[phone]': {
                required: "Phone number required"
            },
            'user[postcode]': {
                required: "Post code required"
            },
            'user[country]': {
                required: "Select country"
            },
            'user[plainPassword][first]': {
                minlength: "Password must be at least 8 characters"
            },
            'user[plainPassword][second]': {
                equalTo: "Passwords do not match"
            }

        }
    });
}

/**
 * Function to init the uploader
 * */
function initImageUploader( url ) {

    //pluploader function
    var uploader = new plupload.Uploader({
        runtimes : 'html5,flash,silverlight,html4',
        browse_button : 'pickfiles', // you can pass in id...
        container: document.getElementById('imageContainer'), // ... or DOM Element itself
        url : '' + url + '',
        flash_swf_url: "/bundles/appadmin/pluploader/Moxie.swf",
        silverlight_xap_url: "/bundles/appadmin/pluploader/Moxie.xap",
        multi_selection: false,

        filters : {
            max_file_size : '100mb',
            mime_types: [
                {title : "Image files", extensions : "jpg,gif,png,jpeg"}
            ]
        },

        init: {
            PostInit: function() {
                document.getElementById('filelist').innerHTML = '';
            },

            FilesAdded: function(up, files) {
                uploader.start();
                $("#filelist").html("");
                plupload.each(files, function(file) {
                    //document.getElementById('filelist').innerHTML += '<div id="' + file.id + '">' + file.name + ' (' + plupload.formatSize(file.size) + ') <b></b></div>';

                    file_html = '<div id="' + file.id + '" class="row">' +
                        '<div class="col-md-6">' + file.name + ' (' + plupload.formatSize(file.size) + ')</div>' +
                        '<div class="col-md-6">' +
                        '<div class="progress progress-striped active">' +
                        '<div class="progress-bar progress-bar-success"  role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%">' +
                        '<span class="sr-only">0% Complete</span>' +
                        '</div>' +
                        '</div>' +
                        '</div>' +
                        '</div>'
                    document.getElementById('filelist').innerHTML += file_html;

                });
//                while(up.files.length > 1) {
//                    up.removeFile(up.files[0]);
//                }
                up.refresh();
            },

            UploadProgress: function(up, file) {
                //document.getElementById(file.id).getElementsByTagName('b')[0].innerHTML = '<span>' + file.percent + "%</span>";
                var elem = $(document).find('#'+file.id+' .progress-bar');
                $(elem).attr('aria-valuenow', file.percent);
                $(elem).css('width', file.percent+'%');
            },

            Error: function(up, err) {
                document.getElementById('filelist').innerHTML += "\nError #" + err.code + ": " + err.message;
            }
        }
    });
    uploader.init();
    //Show uploaded file
    uploader.bind('FileUploaded', function(up, file, info) {
        var obj = $.parseJSON(info.response);

        var html = '<div class="imageContainer">' +
            '<img src="../../../..'+ obj.file_path +'" />' +
            '</div>';

        $("#imagesContainer").html(html);

        $("#imageId").val(obj.file_id);
        $("#showError").addClass('hidden');

    });

}

/**
 *
 * @param expertise
 * @param subspecialities
 * Function to get subspecialities and select already selected ones.
 */

function filterForm(expertise){
    if ( expertise === "" ) {
        $("#filtered").html("");
    } else {
        $.ajax({
            type: "POST",
            url: Routing.generate('advertise_subspeciality'),
            data: { 'expertise': expertise, subspecialities: $('#advertise_subspeciality').val() },
            dataType: 'json',
            success: function (resp) {
                $("#filtered").html(resp.content).removeClass('hidden');

                $('#advertise_subspeciality').select2().val(resp.subspecialities).trigger("change");

            }
        });
    }

}

/**
 * Function to validate advertise form
 */
function validateAdvertiseForm(id) {
    $(id).validate({
        rules: {
            'advertise[name]': {
                required: true
            },
            'advertise[link]': {
                required: true
            },
            'advertise[dateRange]': {
                required: true
            }
        },
        messages: {
            'advertise[name]': {
                required: 'Ad name required'
            },
            'advertise[link]': {
                required: 'Link required'
            },
            'advertise[dateRange]': {
                required: 'Slelect date range'
            }
        },
        submitHandler: function(form) {

            $("#showError").addClass('hidden');

            if ( $("#imageId").val() == 0) {
                $("#showError").removeClass('hidden');
                return false;
            }
            form.submit();
            return false;
        }
    });
}
