﻿User = {
    name: '',
    email: '',

    Validate: function () {
        User.name = E('txtUserName').value;
        User.email = E('txtUserEmail').value;
        if (Validate.IsEmpty(User.name) || !Validate.IsAlphaAndSpace(User.name) || !Validate.IsLengthBetween(User.name, 3, 70) || User.name == 'nome')
            return false;
        if (!Validate.IsEmail(User.email))
            return false;

        return true;
    },

    //    function Button1_onclick() {
    //var service = new SandwichServices.CostService();
    //service.CostOfSandwiches(3, onSuccess, null, null);
    //}

    //function onSuccess(result){
    //alert(result);
    //}

    Post: function () {
        if (User.Validate()) {
            E('spanValidationText').style.display = 'none';

            //Check If the E-mail is already registered
            var service = new Ajax.UserService();
            service.CheckEmail(User.email, function (result) {
                if (!result) {
                    E('spanValidationEmailText').style.display = 'none';
                    //Register user
                    service.Create(User.name, User.email, function () {
                        E('spanSuccessText').style.display = '';
                        E('txtUserName').value = '';
                        E('txtUserEmail').value = '';
                        tracker('/cadastro/sucesso');
                    }, null, null);
                } else {
                    E('spanSuccessText').style.display = 'none';
                    E('spanValidationEmailText').style.display = '';
                }
            }, null, null);

        } else {
            E('spanSuccessText').style.display = 'none';
            E('spanValidationText').style.display = '';
        }
    }
}
