﻿var urlpercorso;

function ajaxSave(form, action, messaggio, container,caller) {
    var current = 0;
    $('label.required').append('&nbsp;<strong>*</strong>&nbsp;');



    $.validator.addMethod("selectRequired", function (value, element) {
        var $element = $(element)
        if (value == 0)
            return false;
        return true;
    }, $.validator.messages.required);

    $.validator.addMethod("alphanumericnospace", function (value, element) {
        return this.optional(element) || !/^\d\w{0,}$/i.test(value);
    }, "Lettere o numeri no spazi o caratteri accentati");


    $.validator.addMethod("customurl", function (value, element) {
        return this.optional(element) || /^(\/[\w-]+\.)+[a-z]{2,6}$/i.test(value);
    }, "url");

    $.validator.addMethod("emailRequired", function (value, element) {
        validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;

        var email = $(element).attr("value");
        if (email.search(validRegExp) == -1)
            return false;
        return true;
    }, "Indirizzo e-mail errato");

    $.validator.addMethod("equalRequired", function (value, element) {
        var pass = $("#newuser_password").attr("value");
        var verifica = $(element).attr("value");
        if (verifica == '') return false;
        if (pass != verifica)
            return false;
        return true;
    }, $.validator.messages.required);

    $.validator.addMethod("equalchangeRequired", function (value, element) {
        var pass = $("#nuova").attr("value");
        var verifica = $(element).attr("value");
        if (verifica == '') return false;
        if (pass != verifica)
            return false;
        return true;
    }, $.validator.messages.required);

    $.validator.addMethod("imageRequired", function (value, element) {
        return value > 0;

    }, $.validator.messages.required);
    $(caller).click(function () {
        var validator = $(form).validate({
            errorClass: "warning",
            onkeyup: false,
            onblur: false,

            submitHandler: function (former) {

                $.post(action, $(form).serialize(), function (data, textStatus, xhr) {
                    $(container).html("<p class='risposta'>"+data+"</p>");
                });
                //                if (type == "createassociazione") {
                //                    $.post(action, $("#" + form).serialize(), function (data, textStatus, xhr) {
                //                        if ((data != 'ok'))
                //                            $("#" + containermod).html(data);
                //                        else {
                //                            CloseEdit();
                //                            $("#" + container).load(action2);
                //                        }
                //                    });
                //                }
                //                else if (type == "createglossario") {
                //                    $.post(action, $("#" + form).serialize(), function (data, textStatus, xhr) {
                //                        if (data != 'ok')
                //                            $("#" + containermod).html(data);
                //                        else {
                //                            CloseEdit();
                //                            $("#" + container).load(action2);
                //                        }
                //                    });
                //                }
                //                // ControllerAction('createassociazione', type, action, action2, containermod, container, form, "#"+caller);
                //                else {
                //                    $.post(action, $("#" + form).serialize(), function (data, textStatus, xhr) {
                //                        if (data != 'ok')
                //                            $("#" + container).html(data);
                //                        else {
                //                            CloseEdit();
                //                            $("#" + container).load(action2);
                //                        }
                //                    });
                //                }

            },
            errorPlacement: function (label, element) {
                // position error label after generated textarea
                if (element.is("textarea")) {
                    label.insertAfter(element); //??
                } else {
                    label.insertAfter(element)
                }
            }
        });
        if (validator != null) {
            validator.focusInvalid = function () {
                // put focus on tinymce on submit validation

                if (this.settings.focusInvalid) {
                    try {
                        var toFocus = $(this.findLastActive() || this.errorList.length && this.errorList[0].element || []);

                        if (toFocus.is("textarea")) {
                            if (tinyMCE.get(toFocus.attr("id")) != undefined)
                                tinyMCE.get(toFocus.attr("id")).focus();
                        } else {
                            toFocus.filter(":visible").focus();

                        }
                    } catch (e) {
                        // ignore IE throwing errors when focusing hidden elements
                    }
                }
            }
        }
    });
}
