﻿function TAccount() {
    var self = this;
    this.logininfo = new Object();
    this.logininfo.posturl = "";
    this.logininfo.returnurl = "";

    this.reginfo = new Object();
    this.reginfo.posturl = ""; //注册提交地址
    this.reginfo.redirecturl = ""; //注册成功后的激活地址

    this.activeinfo = new Object();
    this.activeinfo.posturl = ""; //激活提交地址
    this.activeinfo.redirecturl = ""; //激活成功后的转向地址

    this.userinfo = new Object();
    this.userinfo.posturl = ""; //保存用户信息地址
    this.userinfo.savedomainurl = ""; //保存个性化域名的地址
    this.userinfo.changepasswordurl = ""; //保存密码地址
    this.userinfo.saveskinurl = ""; //保存皮肤的地址
    this.userinfo.sendactivemailurl = ""; //发送激活邮件的地址
    this.userinfo.canelmobileurl = ""; //取消手机绑定
    
    
    //登录函数，failedcallback失败后执行的函数
    this.login = function (username, password, keepstatus, failedcallback) {
        $.ajax({
            url: self.logininfo.posturl,
            data: "username=" + username + "&password=" + encodeURIComponent(password) + "&keepstatus=" + keepstatus + "&t=" + (new Date()).getTime(),
            type: 'post',
            dataType: "json",
            timeout: 30000,
            error: function () {
                app.alert('提交失败');
            },
            success: function (data) {
                if (data.ret == 1) {
                    var url = data.url;
                    if (url == "") url = self.logininfo.returnurl;
                    if (url == "") url = "/";
                    location = url;
                } else {
                    failedcallback(data.ret);
                }
            }
        });
    }

//注册
    this.reg = function (username, password, vcode, invitecode) {
        $.ajax({
            url: self.reginfo.posturl,
            data: "username=" + escape(username) + "&password=" + encodeURIComponent(password) + "&vcode=" + vcode + "&invitecode=" + escape(invitecode) + "&t=" + (new Date()).getTime(),
            type: 'post',
            dataType: "json",
            timeout: 30000,
            error: function () {
                app.alert('提交失败');
            },
            success: function (data) {
                if (data == 1) {
                    location = self.reginfo.redirecturl;
                } else {
                    app.alert(data);
                }
            }
        });
    }


/*激活用户*/
    this.active = function (username, nickname, sex, country, province, city, section) {
        $.ajax({
            url: self.activeinfo.posturl,
            data: "username=" + encodeURIComponent(username) + "&nickname=" + encodeURIComponent(nickname) + "&sex=" + encodeURIComponent(sex) + "&country=" + country + "&province=" + province + "&city=" + city + "&section=" + section + "&t=" + (new Date()).getTime(),
            type: 'post',
            dataType: "json",
            timeout: 30000,
            error: function (err) {
                alert('提交失败:' + err);
            },
            success: function (data) {
                if (data == 1) {
                    location = self.activeinfo.redirecturl;
                } else {
                    alert(data);
                    $('#nickname').focus();
                }

            }
        });
    }


/*保存用户信息*/
    this.saveuserinfo = function (nickname, sex, country, province, city, section, birthday_year, birthday_month, birthday_day, blogurl, description) {
        $.ajax({
            url: self.userinfo.posturl,
            data: "nickname=" + encodeURIComponent(nickname) + "&sex=" + encodeURIComponent(sex) + "&country=" + encodeURIComponent(country) + "&province=" + province + "&city=" + city + "&section=" + section + "&birthday_year=" + birthday_year + "&birthday_month=" + birthday_month + "&birthday_day=" + birthday_day + "&blogurl=" + encodeURIComponent(blogurl) + "&description=" + encodeURIComponent(description) + "&t=" + (new Date()).getTime(),
            type: 'post',
            dataType: "json",
            timeout: 30000,
            error: function () {
                app.alert('提交失败');
            },
            success: function (data) {
                if (data == 1) {
                    app.alert("保存成功");
                } else {
                    app.alert(data);
                    $('#nickname').focus();
                }

            }
        });
    }

//保存域名
this.savedomain = function (domain) {
    $.ajax({
        url: self.userinfo.savedomainurl,
        data: "domain=" + encodeURIComponent(domain),
        type: 'post',
        dataType: "json",
        timeout: 30000,
        error: function () {
            app.alert('提交失败');
        },
        success: function (data) {
            if (data == 1) {
                app.alert("设置成功");
                location.reload();
            } else {
                app.alert(data);
            }

        }
    });
}

this.changepassword = function (nowpassword, password) {
    $.ajax({
        url: self.userinfo.changepasswordurl,
        data: "nowpassword=" + encodeURIComponent(nowpassword) + "&password=" + encodeURIComponent(password),
        type: 'post',
        dataType: "json",
        timeout: 30000,
        error: function () {
            app.alert('提交失败');
        },
        success: function (data) {
            if (data == 1) {
                app.alert("操作成功");
            } else {
                app.alert(data);
            }

        }
    });
}

this.saveskin = function (skinid) {
    $.ajax({
        url: self.userinfo.saveskinurl,
        data: "skinid=" + skinid,
        type: 'post',
        dataType: "json",
        timeout: 30000,
        error: function () {
            app.alert('提交失败');
        },
        success: function (data) {
            if (data == 1) {
                app.sucess("保存完成");
            } else {
                app.alert(data);
            }

        }
    });
}

this.sendactivemail = function (username) {
    $.ajax({
        url: self.userinfo.sendactivemailurl,
        data: "username=" + encodeURIComponent(username),
        type: 'post',
        dataType: "json",
        timeout: 30000,
        error: function () {
            app.alert('提交失败');
        },
        success: function (data) {
            if (data == 1) {
                app.alert("激活邮件已经发出，请进入您的邮箱查看！");
            } else {
                app.alert(data);
            }

        }
    });
}


this.cancelmobile = function () {
    app.confirm('确定要取消绑定吗？', function () {
        $.ajax({
            url: self.userinfo.cancelmobileurl,
            type: 'post',
            dataType: "json",
            timeout: 30000,
            error: function () {
                app.alert('提交失败');
            },
            success: function (data) {
                if (data == 1) {
                    //app.alert("您的手机已经取消绑定");
                    location.reload();
                } else {
                    app.alert(data);
                }
            }
        })
    });
}

}//end class
