/**
 * Created by Systemy Informatyczne nestSoft
 * User: slawomir.zytko@nestsoft.pl
 * Date: 8/17/11
 * Time: 9:49 AM
 */
$(document).ready(function() {
    $("#profileLink").click(function() {
        $("#profileFormDialog").dialog('open');
    });

    function updateTips( t ) {
        tips
            .html("<strong>" + t + "</strong>" )
            .addClass( "ui-state-highlight" );
//        setTimeout(function() {
//            tips.removeClass( "ui-state-highlight", 500 );
//        }, 5000 );
        tips.effect("fadeIn", {}, 1000);
    }

    function checkLength( o, n, min, max ) {
        if ( o.val().length > max || o.val().length < min ) {
            o.addClass( "ui-state-error" );
            updateTips( "Length of " + n + " must be between " +
                min + " and " + max + "." );
            return false;
        } else {
            o.removeClass("ui-state-error");
            return true;
        }
    }

    function checkRegexp( o, regexp, n ) {
        if ( !( regexp.test( o.val() ) ) ) {
            o.addClass( "ui-state-error" );
            updateTips(n );
            return false;
        } else {
            o.removeClass("ui-state-error");
            return true;
        }
    }
    function checkRequired(o, n) {
        if (o.val().length == 0) {
            o.addClass("ui-state-error");
            updateTips("Field " + n + " is required");
            return false;
        } else {
            o.removeClass("ui-state-error");
            return true;
        }
    }
    var password = $( "#profile_password" ),
    oldpassword = $("#profile_old_password"),
    repassword = $("#profile_repassword"),
    firstname = $("#profile_firstname"),
    lastname = $("#profile_lastname"),
    address = $("#profile_address"),
    phone = $("#profile_phone"),
    allFields = $( [] ).add( password ).add(oldpassword).add(repassword).add(firstname).add(lastname).add(address).add(phone),
    tips = $( ".validateTips" );
    $("#profile_loading").hide();
    $("#profileFormDialog").dialog({
        autoOpen: false,
        height: 650,
        width: 400,
        modal: true,
        buttons: {
            "Save": function() {
                $("#profile_loading").show();
                var bValid = true;
                allFields.removeClass( "ui-state-error" );

                if (oldpassword.val() > 0) {
                    bValid = bValid && checkLength( password, "password", 6, 25 );
                    if (password.val() != repassword.val()) {
                        updateTips("Passwords are not equal");
                        bValid = false;
                    }
                }

                bValid = bValid && checkRequired(firstname, "firstname");
                bValid = bValid && checkRequired(lastname, "lastname");
                bValid = bValid && checkRequired(address, "address");
                bValid = bValid && checkRequired(phone, "phone");

                if ( bValid ) {
                    $.ajax({
                        url: '/auth/profile',
                        type: 'post',
                        dataType: 'json',
                        data: $("#profileForm").serialize(),
                        success: function(data) {
                            if (data.result == 1) {
                                updateTips('Your profile has been saved');
                            } else {
                                switch (data.result) {
                                    case 'WRONG_OLD_PASSWORD' :
                                        updateTips("Incorrect old password");
                                        break;
                                    case 'WRONG_NEW_PASSWORD' :
                                        updateTips('Incorrect new password');
                                        break;
                                    case 'WRONG_PASSWORD_NOT_EQUAL' :
                                        updateTips("Password are not equal");
                                        break;
                                    default:
                                        updateTips("Failed to change profile. Try once again.");
                                        break;
                                }
                            }
                        }
                    });
                }
                $("#profile_loading").hide();
            },
            Cancel: function() {
                updateTips("");
                $( this ).dialog( "close" );
            }
        },
        close: function() {
            //allFields.val( "" ).removeClass( "ui-state-error" );
        }
    });
});
