﻿/*********************************************************************************
* MemberToolbar
*********************************************************************************/
/**************************************
* constant
***************************************/
//toolbar
var CLASS_PANEL_MEMBER_LOGIN = ".panel_member_login";
var CLASS_PANEL_MEMBER_CONTROL = ".panel_member_control";
var CLASS_PANEL_MEMBER_NAME = ".member_name";

//Dialog
var ID_DLG_CONTAINER = "dialog_container";

//MemberLogin
var ID_MEMBERLOGIN_CONTAINER = "memberlogin_container";

//MemberRegister
var ID_MEMBERREGISTER_FORM = "memberregister_form";

/**************************************
* URL
***************************************/
var URL_MEMBER_LOGIN = "member_login.aspx";
var URL_MEMBER_LOGOUT = "member_logout.aspx";
var URL_MEMBER_REGISTER = "member_register.aspx";
var URL_MEMBER_UPDATE = "member_update.aspx";
var URL_MEMBER_GET = "member_get.aspx";
var URL_MEMBER_GETPWD = "member_getpwd.aspx";

var MemberToolbar = Object();
MemberToolbar.showLoginPanel = function(bFlag)
{
    var panel = $$(CLASS_PANEL_MEMBER_LOGIN);
    if (null != panel && panel.length > 0)
    {
        if (bFlag)
        {
            panel[0].show();
        }
        else 
        {
            panel[0].hide();            
        }
    }    
}
MemberToolbar.showControlPanel = function(bFlag)
{
    var panel = $$(CLASS_PANEL_MEMBER_CONTROL);
    if (null != panel && panel.length > 0)
    {
        if (bFlag)
        {
            panel[0].show();            
        }
        else
        {
            panel[0].hide();            
        }
    }    
}
MemberToolbar.showMemberName = function(name)
{
    var panel = $$(CLASS_PANEL_MEMBER_NAME);
    if (null != panel && panel.length > 0)
    {
        panel[0].innerHTML = name;
    }    
}

/*********************************************************************************
* Dialog
*********************************************************************************/
var Dialog = Class.create({
    initialize : function()
    {
        this.dlgContainer = null;
        this.dlgFrame = null;
        this.dlgToolbar = null;
        this.dlgMessage = null;
        this.dlgError = null;
        this.dlgDataTransStatus = null;
        this.dlgContent = null;
        this.dlgButtonClose = null;
    },
    open : function()
    {
        var caller = this;
        
        //create container
        this.dlgContainer = $(document.createElement("div"));
        this.dlgContainer.id = ID_DLG_CONTAINER;
        this.dlgContainer.style.width = Utility.getPageWidth() + "px";
        this.dlgContainer.style.height = Utility.getPageHeight() + "px"; 
        document.body.appendChild(this.dlgContainer);
        
        //create frame
        this.dlgFrame = $(document.createElement("div"));
        this.dlgFrame.addClassName("frame");
        this.dlgContainer.appendChild(this.dlgFrame);

        //create toolbar
        this.dlgToolbar = $(document.createElement("div"));
        this.dlgToolbar.addClassName("toolbar");
        this.dlgFrame.appendChild(this.dlgToolbar);
        
        this.dlgButtonClose = $(document.createElement("div"));
        this.dlgButtonClose.addClassName("btn_close");
        this.dlgButtonClose.onclick = function()
        {
            if (null != caller)
            {
                caller.close();
            }
        };
        this.dlgToolbar.appendChild(this.dlgButtonClose);
        
        //create message
        this.dlgMessage = $(document.createElement("div"));
        this.dlgMessage.addClassName("message");
        this.dlgMessage.hide();
        this.dlgFrame.appendChild(this.dlgMessage);
                
        //create data trans notifier
        this.dlgDataTransStatus = $(document.createElement("div"));
        this.dlgDataTransStatus.addClassName("datatransstatus");
        this.dlgDataTransStatus.innerHTML = "请稍候...";
        this.dlgDataTransStatus.hide();
        this.dlgFrame.appendChild(this.dlgDataTransStatus);

        //create content
        this.dlgContent = $(document.createElement("div"));
        this.dlgContent.addClassName("content");
        this.dlgContent.hide();
        this.dlgFrame.appendChild(this.dlgContent);                                

        //create errormsg
        this.dlgError = $(document.createElement("div"));
        this.dlgError.addClassName("error");
        this.dlgError.hide();
        this.dlgFrame.appendChild(this.dlgError);
    },
    close : function()
    {
        document.body.removeChild(this.dlgContainer);        

        this.dlgContainer = null;
        this.dlgFrame = null;
        this.dlgMessage = null;
        this.dlgError = null;
        this.dlgDataTransStatus = null;
        this.dlgContent = null;
        this.dlgButtonClose = null;
    },
    reportMsg : function(text)
    {
        if (null != this.dlgMessage)
        {
            this.dlgMessage.innerHTML = text;
            this.dlgMessage.show();
        }
    },
    showMsg : function()
    {
        if (null != this.dlgMessage)
        {
            this.dlgMessage.show();
        }
    },
    hideMsg : function()
    {
        if (null != this.dlgMessage)
        {
            this.dlgMessage.hide();
        }
    },
    reportError : function(text)
    {
        if (null != this.dlgError)
        {
            this.dlgError.innerHTML = text;
            new Effect.Appear(this.dlgError);
        }
    },
    showError : function()
    {
        if (null != this.dlgError)
        {
            new Effect.Appear(this.dlgError);
        }    
    },
    hideError : function()
    {
        if (null != this.dlgError)
        {
            new Effect.Fade(this.dlgError);
        }    
    },    
    showDataTransStatus : function()
    {
        if (null != this.dlgDataTransStatus)
        {
            this.dlgDataTransStatus.show();
        }
    },
    hideDataTransStatus : function()
    {
        if (null != this.dlgDataTransStatus)
        {
            this.dlgDataTransStatus.hide();
        }
    },
    appendContent : function(eDiv)
    {
        if (null != this.dlgContent)
        {
            this.dlgContent.appendChild(eDiv);
            this.dlgContent.show();
        }
    },
    showToolbar : function()
    {
        if (null != this.dlgToolbar)
        {
            this.dlgToolbar.show();
        }
    },
    hideToolbar : function()
    {
        if (null != this.dlgToolbar)
        {
            this.dlgToolbar.hide();
        }
    },
    showContent : function()
    {
        if (null != this.dlgContent)
        {
            this.dlgContent.show();
        }
    },
    hideContent : function()
    {
        if (null != this.dlgContent)
        {
            this.dlgContent.hide();
        }
    },
    hideAll : function()
    {
        this.dlgToolbar.hide();
        this.dlgMessage.hide();
        this.dlgDataTransStatus.hide();
        this.dlgContent.hide();
        this.dlgError.hide();
    }
});

/*********************************************************************************
* Member Login
*********************************************************************************/
var WndMemberLogin = Class.create(Dialog, {
    initialize : function($super) 
    {
        $super();
        this.inputEmail = null;
        this.inputPwd = null;
    },
    open : function($super)
    {
        $super();
        var caller = this;
        
        //create container
        var dlgContainer = document.createElement("div");
        dlgContainer.id = ID_MEMBERLOGIN_CONTAINER;
        
        //create login table
        var table = document.createElement("table");
        table.setAttribute("align", "center");               
        
        //create tbody
        var tbody = document.createElement("tbody");
        table.appendChild(tbody);
        
        //create email row
        var row;
        var cell;        
        var nRowIndex = 0;
        var nCellIndex = 0;
        
        //create input email row
        nCellIndex = 0;
        row = tbody.insertRow(nRowIndex);
        dlgContainer.appendChild(table);

        cell = row.insertCell(nCellIndex);        //email label
        cell.setAttribute("align", "right");
        cell.appendChild(document.createTextNode("Email: "));        

        ++nCellIndex;
        cell = row.insertCell(nCellIndex);        //email input 
        this.inputEmail = $(document.createElement("input"));
        this.inputEmail.type = "text";
        this.inputEmail.className = "inputEmail";
        cell.appendChild(this.inputEmail);

        //create pwd row
        ++nRowIndex;
        nCellIndex = 0;
        row = tbody.insertRow(nRowIndex);        

        cell = row.insertCell(nCellIndex);                  //pwd label
        cell.setAttribute("align", "right");
        cell.appendChild(document.createTextNode("密码: "));        

        ++nCellIndex;        
        cell = row.insertCell(nCellIndex++);
        this.inputPwd = $(document.createElement("input")); //pwd input
        this.inputPwd.type = "password";
        this.inputPwd.className = "inputPwd";
        cell.appendChild(this.inputPwd);
        
        //create submit button
        var divFooter = $(document.createElement("div"));
        divFooter.addClassName("footer");        
        dlgContainer.appendChild(divFooter);

        var buttonSubmit = document.createElement("input");
        buttonSubmit.type = "button";
        buttonSubmit.value = "登录";
        buttonSubmit.onclick = function() {
            if (null != caller) {
                caller.onSubmitClick();
            }
        };
        divFooter.appendChild(buttonSubmit);
        
        this.appendContent(dlgContainer);
        this.reportMsg("请输入您的Email地址和密码");
        this.inputEmail.focus();
    },
    close : function($super)
    {
        this.inputEmail = null;
        this.inputPwd = null;
        $super();        
    },
    onSubmitClick : function()
    {
        var caller = this;

        //validate input
        if (Validator.isEmpty(this.inputEmail.value))
        {
            this.reportError("请输入您的Email");
            this.inputEmail.focus();
            return;
        }
        if (Validator.isEmpty(this.inputPwd.value))
        {
            this.reportError("请输入您的密码");
            this.inputPwd.focus();
            return;
        }
        
        //send login data
        this.hideAll();
        this.showDataTransStatus();
        new Ajax.Request(URL_MEMBER_LOGIN, {
            method : "post",
            parameters : {
                email : this.inputEmail.value,
                pwd : this.inputPwd.value
            },
            onSuccess : function(transport) {
                caller.parseResponseData(transport.responseText);
            },
            onFailure : function(transport) {
                caller.onFailure("网络故障");
            }
        });
    },
    parseResponseData : function(responseText)
    {
        if (null == responseText || Validator.isEmpty(responseText)) 
        {
            this.onFailure("未知错误");
            return;
        }
        
        var response = null;
        try {
            response = responseText.evalJSON();
        }
        catch (exp)
        {
            this.onFailure("未知错误");
            return;
        }
        
        if (response.flag != 1)
        {
            this.onFailure(response.errorMsg);
        }
        else 
        {
            this.onSuccess(response.errorMsg);
        }
    },
    onSuccess : function(responseText)
    {
        this.close();
        MemberToolbar.showLoginPanel(false);
        MemberToolbar.showControlPanel(true);
        MemberToolbar.showMemberName(responseText);
    },
    onFailure : function(text)
    {
        var strErrorMsg;
        if (null == text || text.length <= 0)
        {
            strErrorMsg = "未知原因";
        }
        else 
        {
            strErrorMsg = text;
        }

        this.hideDataTransStatus();
        this.showToolbar();
        this.showMsg();
        this.showContent();
        this.reportError("登录失败：" + strErrorMsg);
        this.inputEmail.focus();
    }
});

/*********************************************************************************
* MemberLogout
*********************************************************************************/
var WndMemberLogout = Class.create(Dialog, {
    open : function($super)
    {        
        $super();
        var caller = this;
        
        this.hideAll();
        this.showDataTransStatus();
        
        new Ajax.Request(URL_MEMBER_LOGOUT, {
            onSuccess : function(transport) 
            {
                if (null != caller)
                {
                    caller.parseResponseData(transport.responseText);
                }
            },
            onFailure : function(transport) 
            {
                if (null != caller)
                {
                    caller.onFailure("网络故障");                
                }
            }
        });
    },
    parseResponseData : function(responseText)
    {
        if (null == responseText || responseText.length <= 0)
        {
            this.onFailure("网络故障");
            return;
        }
        
        var response = null;
        try {
            response = responseText.evalJSON();
        }
        catch (exp)
        {
            this.onFailure("未知错误");
            return;
        }
        
        if (response.flag != 1) 
        {
            this.onFailure(response.errorMsg);
        }
        else
        {
            this.onSuccess();
        }
    },
    onSuccess : function()
    {
        this.close();
        MemberToolbar.showLoginPanel(true);
        MemberToolbar.showControlPanel(false);        
    },
    onFailure : function(text)
    {
        var strErrorMsg;
        if (null == text || text.length <= 0)
        {
            strErrorMsg = "未知原因";
        }
        else 
        {
            strErrorMsg = text;
        }

        this.hideDataTransStatus();
        this.reportError("退出失败：" + strErrorMsg);
    }
});

/*********************************************************************************
* MemberRegister
*********************************************************************************/
var WndMemberRegister = Class.create(Dialog, {
    initialize : function($super) {
        $super();
        this.form = null;
        this.inputEmail = null;
        this.inputPwd = null;
        this.inputName = null;
        this.btnSubmit = null;
    },
    open : function($super) {
        $super();
        var caller = this;
        
        //create form
        this.form = $(document.createElement("form"));
        this.form.id = ID_MEMBERREGISTER_FORM;
        
        //create table
        var table = document.createElement("table");
        table.setAttribute("align", "center");
        this.form.appendChild(table);
        
        var tbody = document.createElement("tbody");
        table.appendChild(tbody);
        
        var row, cell;
        var nRowIndex = 0;
        var nCellIndex = 0;
        
        //create email row
        row = tbody.insertRow(nRowIndex);
        
        cell = row.insertCell(nCellIndex);
        var labelEmail = document.createTextNode("* EMail:");   //label email
        cell.appendChild(labelEmail);
        
        ++ nCellIndex;
        cell = row.insertCell(nCellIndex);
        this.inputEmail = document.createElement("input");       //input email
        this.inputEmail.type = "text";
        this.inputEmail.name = "email";
        this.inputEmail.className = "inputEmail";
        cell.appendChild(this.inputEmail);
        
        //create password row
        ++nRowIndex;
        nCellIndex = 0;        
        row = tbody.insertRow(nRowIndex);
        
        cell = row.insertCell(nCellIndex);
        var labelPwd = document.createTextNode("* 密码:");    //label pwd
        cell.appendChild(labelPwd);
        
        ++nCellIndex;
        cell = row.insertCell(nCellIndex);
        this.inputPwd = document.createElement("input");       //input pwd
        this.inputPwd.type = "password";
        this.inputPwd.name = "pwd";
        this.inputPwd.className = "inputPwd";
        cell.appendChild(this.inputPwd);
        
        //create name row
        ++nRowIndex;
        nCellIndex = 0;        
        row = tbody.insertRow(nRowIndex);
        
        cell = row.insertCell(nCellIndex);
        var labelName = document.createTextNode("* 姓名:");   //label name
        cell.appendChild(labelName);
        
        ++nCellIndex;
        cell = row.insertCell(nCellIndex);
        this.inputName = document.createElement("input");      //input name
        this.inputName.type = "text";
        this.inputName.name = "name";
        this.inputName.className = "inputName";
        cell.appendChild(this.inputName);
        
        //create footer
        var divFooter = $(document.createElement("div"));
        divFooter.addClassName("footer");
        this.form.appendChild(divFooter);
        
        this.btnSubmit = document.createElement("input");
        this.btnSubmit.type = "button";
        this.btnSubmit.value = "注册";
        this.btnSubmit.onclick = function() {
            if (null == caller) {
                throw new Error("invalid caller!");
            }
            
            caller.onSubmit();
        }
        divFooter.appendChild(this.btnSubmit);        
        
        this.appendContent(this.form);
        this.reportMsg("请输入您的Email, 密码，姓名");
        this.inputEmail.focus();
    },
    close : function($super)
    {
        this.form = null;
        this.inputEmail = null;
        this.inputPwd = null;
        this.inputName = null;
        this.btnSubmit = null;

        $super();
    },
    validateForm : function()
    {
        if (Validator.isEmpty(this.inputEmail.value)) {
            this.reportError("请输入您的Email.");
            this.inputEmail.focus();
            return false;
        }
        if (!Validator.isEmail(this.inputEmail.value)) {
            this.reportError("请输入有效的Email.");
            this.inputEmail.focus();
            return false;
        }        
        if (Validator.isEmpty(this.inputPwd.value)) {
            this.reportError("请输入您的密码.");
            this.inputPwd.focus();
            return false;            
        }
        if (Validator.isEmpty(this.inputName.value)) {
            this.reportError("请输入您的姓名.");
            this.inputName.focus();
            return false;            
        }
        
        return true;        
    },
    onSubmit : function()
    {
        if (!this.validateForm()) 
        {
            return;
        }
        
        this.hideAll();
        this.showDataTransStatus();
        var caller = this;
        new Ajax.Request(URL_MEMBER_REGISTER, {
            method : "post",
            parameters : $(ID_MEMBERREGISTER_FORM).serialize(),
            onSuccess : function(transport) {
                if (null == caller)
                {
                    throw new Error("invalid caller!");
                }
                caller.parseResponseData(transport.responseText);
            },
            onFailure : function(trasport) {
                if (null == caller)
                {
                    throw new Error("invalid caller!");
                }
                caller.onFailure("网络故障");                
            }
        });
    },
    parseResponseData : function(responseText)
    {        
        if (null == responseText || responseText.length <= 0)
        {
            this.onFailure("网络故障");
            return;
        }
        
        var response = null;
        try {
            response = responseText.evalJSON();
        }
        catch (exp)
        {
            this.onFailure("未知错误");
            return;
        }

        if (response.flag != 1)
        {
            this.onFailure(response.errorMsg);
        }
        else
        {
            this.onSuccess();
        }
    },
    onSuccess : function()
    {
        MemberToolbar.showLoginPanel(false);
        MemberToolbar.showControlPanel(true);
        MemberToolbar.showMemberName(this.inputName.value);
        this.close();
    },
    onFailure : function(text)
    {
        var strErrorMsg;
        if (null == text || text.length <= 0)
        {
            strErrorMsg = "未知原因";
        }
        else 
        {
            strErrorMsg = text;
        }
        
        this.showToolbar();
        this.hideDataTransStatus();
        this.showMsg();
        this.showContent();
        this.reportError("注册失败：" + strErrorMsg);
        this.inputEmail.focus();
    }
});

/*********************************************************************************
* MemberUpdate
*********************************************************************************/
var WndMemberUpdate = Class.create(Dialog, {
    initialize : function($super)
    {
        $super();
        
        this.form = null;
        this.inputEmail = null;
        this.inputPwd = null;
        this.inputName = null;
    },
    open : function($super)
    {
        $super();
        
        //fetch data
        this.hideAll();
        this.showDataTransStatus();        
        var caller = this;
        new Ajax.Request(URL_MEMBER_GET, {
            onSuccess : function(transport) {
                caller.parseResponseDataGet(transport.responseText);
            },
            onFailure : function(trasport) {
                caller.onFailureGet("网络故障");
            }
        });
    },
    parseResponseDataGet : function(responseText)
    {
        if (null == responseText || responseText.length <= 0)
        {
            this.onFailureGet("网络错误");
            return;
        }
        
        var response = null;
        try {
            response = responseText.evalJSON();
        }
        catch (exp)
        {
            this.onFailureGet("未知错误");
            return;
        }

        if (response.flag != 1) 
        {
            this.onFailureGet(response.errorMsg);
        }
        else 
        {
            this.onSuccessGet(responseText);
        }
    },
    onSuccessGet : function(responseText)
    {
        this.showToolbar();
        this.showMsg();
        this.showContent();
        this.hideDataTransStatus();
        
        var member = responseText.evalJSON();
        this.createForm(member);
    },
    onFailureGet : function(text) 
    {
        var strErrorMsg;
        if (null == text || text.length <= 0)
        {
            strErrorMsg = "未知原因";
        }
        else 
        {
            strErrorMsg = text;
        }
        
        this.showToolbar();
        this.hideDataTransStatus();
        this.reportError("读取信息失败：" + strErrorMsg);
        this.inputEmail.focus();        
    },
    createForm : function(member)
    {
        //create form
        this.form = $(document.createElement("form"));
        this.form.id = ID_MEMBERREGISTER_FORM;
        
        //create table
        var table = document.createElement("table");
        table.setAttribute("align", "center");
        this.form.appendChild(table);
        
        var tbody = document.createElement("tbody");
        table.appendChild(tbody);
        
        var row, cell;
        var nRowIndex = 0;
        var nCellIndex = 0;
        
        //create email row
        row = tbody.insertRow(nRowIndex);
        
        cell = row.insertCell(nCellIndex);
        var labelEmail = document.createTextNode("* EMail:");   //label email
        cell.appendChild(labelEmail);
        
        ++ nCellIndex;
        cell = row.insertCell(nCellIndex);
        this.inputEmail = document.createElement("input");       //input email
        this.inputEmail.type = "text";
        this.inputEmail.name = "email";
        this.inputEmail.value = member.email;        
        this.inputEmail.className = "inputEmail";
        cell.appendChild(this.inputEmail);
        
        //create password row
        ++nRowIndex;
        nCellIndex = 0;        
        row = tbody.insertRow(nRowIndex);
        
        cell = row.insertCell(nCellIndex);
        var labelPwd = document.createTextNode("* 密码:");    //label pwd
        cell.appendChild(labelPwd);
        
        ++nCellIndex;
        cell = row.insertCell(nCellIndex);
        this.inputPwd = document.createElement("input");       //input pwd
        this.inputPwd.type = "password";
        this.inputPwd.name = "pwd";
        this.inputPwd.value = member.pwd;
        this.inputPwd.className = "inputPwd";
        cell.appendChild(this.inputPwd);
        
        //create name row
        ++nRowIndex;
        nCellIndex = 0;        
        row = tbody.insertRow(nRowIndex);
        
        cell = row.insertCell(nCellIndex);
        var labelName = document.createTextNode("* 姓名:");   //label name
        cell.appendChild(labelName);
        
        ++nCellIndex;
        cell = row.insertCell(nCellIndex);
        this.inputName = document.createElement("input");      //input name
        this.inputName.type = "text";
        this.inputName.name = "name";
        this.inputName.value = member.name;
        this.inputName.className = "inputName";
        cell.appendChild(this.inputName);
        
        //create footer
        var divFooter = $(document.createElement("div"));
        divFooter.addClassName("footer");
        this.form.appendChild(divFooter);
        
        this.btnSubmit = document.createElement("input");
        this.btnSubmit.type = "button";
        this.btnSubmit.value = "修改";
        this.btnSubmit.owner = this;
        this.btnSubmit.onclick = function() {
            this.owner.onSubmit();
        }
        divFooter.appendChild(this.btnSubmit);        
        
        this.appendContent(this.form);
        this.reportMsg("您可以在这里修改登录Email, 密码，姓名");
        this.inputEmail.focus();
    },
    validateForm : function()
    {
        if (Validator.isEmpty(this.inputEmail.value)) {
            this.reportError("请输入您的Email.");
            this.inputEmail.focus();
            return false;
        }
        if (!Validator.isEmail(this.inputEmail.value)) {
            this.reportError("请输入有效的Email.");
            this.inputEmail.focus();
            return false;
        }        
        if (Validator.isEmpty(this.inputPwd.value)) {
            this.reportError("请输入您的密码.");
            this.inputPwd.focus();
            return false;            
        }
        if (Validator.isEmpty(this.inputName.value)) {
            this.reportError("请输入您的姓名.");
            this.inputName.focus();
            return false;            
        }
        
        return true;        
    },
    onSubmit : function()
    {
        if (!this.validateForm())
        {
            return;
        }
        
        this.hideAll();
        this.showDataTransStatus();
        var caller = this;
        new Ajax.Request(URL_MEMBER_UPDATE, {
            method : "post",
            parameters : $(ID_MEMBERREGISTER_FORM).serialize(),
            onSuccess : function(transport) {
                caller.parseResponseDataUpdate(transport.responseText);
            },
            onFailure : function(trasport) {
                caller.onFailureUpdate("网络故障");                
            }
        });
    },
    parseResponseDataUpdate :function(responseText)
    {
        if (null == responseText || responseText.length <= 0)
        {
            this.onFailureUpdate("网络故障");
            return;
        }
        
        var response = null;
        try {
            response = responseText.evalJSON();
        }
        catch (exp)
        {
            this.onFailureUpdate("未知错误");
            return;
        }
        
        if (response.flag != 1)
        {
            this.onFailureUpdate(response.errorMsg);
        }
        else
        {
            this.onSuccessUpdate();
        }
    },
    onSuccessUpdate : function()
    {
        MemberToolbar.showLoginPanel(false);
        MemberToolbar.showControlPanel(true);
        MemberToolbar.showMemberName(this.inputName.value);
        this.close();
    },
    onFailureUpdate :function(text)
    {
        var strErrorMsg;
        if (null == text || text.length <= 0)
        {
            strErrorMsg = "未知原因";
        }
        else 
        {
            strErrorMsg = text;
        }
        
        this.hideDataTransStatus();
        this.showToolbar();
        this.showMsg();
        this.showContent();
        this.reportError("修改失败：" + strErrorMsg);
        this.inputEmail.focus();
    }
});

/*********************************************************************************
* Member Get Pwd
*********************************************************************************/
var WndMemberGetPwd = Class.create(Dialog, {
    initialize : function($super)
    {
        $super();
        
        this.inputEmail = null;
    },
    open : function($super)
    {
        $super();

        var caller = this;
        
        //create container
        var divContainer = $(document.createElement("div"));
        divContainer.id = "membergetpwd_container";
        
        //create table
        var table = document.createElement("table");
        divContainer.appendChild(table);
        
        var tbody = document.createElement("tbody");
        table.appendChild(tbody);
        
        //create form
        var nRowIndex = 0;
        var nCellIndex = 0;
        var row, cell;

        row = table.insertRow(nRowIndex);                       //label email
        cell = row.insertCell(nCellIndex);
        cell.appendChild(document.createTextNode("EMail: "));
        
        ++nCellIndex;                                           //input email
        cell = row.insertCell(nCellIndex);
        this.inputEmail = document.createElement("input");
        this.inputEmail.type = "text"; 
        cell.appendChild(this.inputEmail);
        
        //create footer
        var divFooter = $(document.createElement("div"));
        divFooter.addClassName("footer");
        
        var btnSubmit = document.createElement("input");
        btnSubmit.type = "button";
        btnSubmit.value = "确定";
        btnSubmit.onclick = function()
        {
            caller.onSubmit();
        }
        divFooter.appendChild(btnSubmit);
        divContainer.appendChild(divFooter);
        
        this.reportMsg("请输入您的登录Email，单击[确定]找回密码");
        this.appendContent(divContainer);
        this.inputEmail.focus();
    },
    validateForm : function()
    {
        if (Validator.isEmpty(this.inputEmail.value))
        {
            this.reportError("请输入您的登录Email");
            this.inputEmail.focus();
            return false;
        }
        if (!Validator.isEmail(this.inputEmail.value)) {
            this.reportError("请输入有效的Email.");
            this.inputEmail.focus();
            return false;
        }        
        
        return true;
    },
    onSubmit : function()
    {
        if (!this.validateForm())
        {
            return;
        }
        
        this.hideAll();
        this.showDataTransStatus();
        var caller = this;        
        new Ajax.Request(URL_MEMBER_GETPWD, {
            method : "post",
            parameters : {
                email : caller.inputEmail.value
            },
            onSuccess : function(transport)
            {
                caller.parseResponseData(transport.responseText);
            },
            onFailure : function(transport)
            {
                caller.onFailure("网络故障");
            }
        });
    },
    parseResponseData : function(responseText)
    {
        if (null == responseText || responseText.length <= 0)
        {
            this.onFailureUpdate("网络故障");
            return;
        }
        
        var response = null;
        try {
            response = responseText.evalJSON();
        }
        catch (exp)
        {
            this.onFailure("未知错误");
            return;
        }
        
        if (response.flag != 1)
        {
            this.onFailure(response.errorMsg);
        }
        else
        {
            this.onSuccess(response.errorMsg);
        }        
    },
    onSuccess : function(text)
    {
        if (null == text || text.length <= 0)
        {
            this.close();
            return;
        }
        
        this.hideDataTransStatus();
        this.showToolbar();
        this.reportMsg(text);
    },
    onFailure : function(text)
    {
        var strErrorMsg;
        if (null == text || text.length <= 0)
        {
            strErrorMsg = "未知原因";
        }
        else 
        {
            strErrorMsg = text;
        }
        
        this.hideDataTransStatus();
        this.showToolbar();
        this.showMsg();
        this.showContent();
        this.reportError(strErrorMsg);
        this.inputEmail.focus();    
    }    
});

/*********************************************************************************
* Bookmark Panel
*********************************************************************************/
/***************************************
* ID
****************************************/
var ID_BM_DATATRANSSTATUS = "bm_datatransstatus";
var ID_BM_MSG = "bm_msg";
var ID_BM_ERROR = "bm_error";

var ID_BM_PANEL_ADD = "bm_panel_add";
var ID_BM_FORM_ADD = "bm_form_add";

var ID_BM_PANEL_VIEW = "bm_panel_view";

/***************************************
* URL
****************************************/
var URL_BM_ADD = "Bookmark_Add.aspx";
var URL_BM_VIEW = "Bookmark_View.aspx";
var URL_BM_DELETE = "Bookmark_Delete.aspx";

/***************************************
* Bookmark panel
****************************************/
var Bookmark = new Object();

Bookmark.showMsg = function()
{
    new Effect.Appear(ID_BM_MSG, {duration:1});
}
Bookmark.hideMsg = function()
{
    new Effect.Fade(ID_BM_MSG, {duration:2});
}
Bookmark.reportMsg = function(text)
{
    $(ID_BM_MSG).innerHTML = text;
    this.showMsg();
    setTimeout(Bookmark.hideMsg, 3000);
}

Bookmark.showError = function()
{
    new Effect.Appear(ID_BM_ERROR, {duration:1}); 
}
Bookmark.hideError = function()
{
    new Effect.Fade(ID_BM_ERROR, {duration:2}); 
}
Bookmark.reportError = function(text)
{
    $(ID_BM_ERROR).innerHTML = text;
    this.showError();
    setTimeout(Bookmark.hideError, 3000);
}

Bookmark.showDatatransStatus = function()
{
    $(ID_BM_DATATRANSSTATUS).show();
}
Bookmark.hideDatatransStatus = function()
{
    $(ID_BM_DATATRANSSTATUS).hide();
}

Bookmark.showPanelAdd = function()
{
    var element = $(ID_BM_PANEL_ADD);    
    if (null != element)
    {        
        element.show();
    }
}
Bookmark.hidePanelAdd = function()
{
    new Effect.Fade(ID_BM_PANEL_ADD, {duration:1});
}

Bookmark.showPanelView = function()
{
    new Effect.Appear(ID_BM_PANEL_VIEW, {duration:1});
    $(ID_BM_PANEL_VIEW).scrollTo();
}
Bookmark.hidePanelView = function()
{
    new Effect.Fade(ID_BM_PANEL_VIEW, {duration:1});
}

/*********************************************************************************
* Bookmark Add
*********************************************************************************/
var PanelBookmarkAdd = Class.create({
    initialize : function(nId, strName)
    {
        this.id = nId;
        this.name = strName;
        this.panelContainer = null;
        this.inputName = null;
    },
    open : function()
    {
        if (null != $(ID_BM_FORM_ADD))
        {
            Bookmark.showPanelAdd();
            return;
        }
        
        //get member info
        var caller = this;        
        Bookmark.showDatatransStatus();
        new Ajax.Request(URL_MEMBER_GET,{
            onSuccess : function(transport) 
            {
                caller.parseResponseDataCheckLogin(transport.responseText);
            },
            onFailure : function(transport)
            {
                caller.onFailureCheckLogin("网络故障");
            }
        });
    },
    close : function()
    {
        if (null != this.panelContainer) 
        {
            $(ID_BM_PANEL_ADD).removeChild(this.panelContainer);
        }
        
        this.panelContainer = null
        this.inputName = null;
    },
    parseResponseDataCheckLogin : function(responseText)
    {
        if (null == responseText || responseText.length <= 0)
        {
            this.onFailureCheckLogin("网络错误");
            return;
        }
        
        var response = null;
        try {
            response = responseText.evalJSON();
        }
        catch (exp)
        {
            this.onFailureCheckLogin("未知错误");
            return;
        }

        if (response.flag != 1) 
        {
            this.onFailureCheckLogin("请先登录或注册");
        }
        else
        {
            this.onSuccessCheckLogin(responseText);
        }
    },
    onSuccessCheckLogin : function()
    {
        Bookmark.hideDatatransStatus();
        this.createForm();        
    },
    onFailureCheckLogin : function(text)
    {
        var strErrorMsg;
        if (null == text || text.length <= 0)
        {
            strErrorMsg = "未知原因";
        }
        else 
        {
            strErrorMsg = text;
        }
        
        Bookmark.hideDatatransStatus();
        Bookmark.reportError(strErrorMsg);
    },
    createForm : function()
    {
        var caller = this;
        this.panelContainer = $(document.createElement("div"));
        this.panelContainer.id = ID_BM_FORM_ADD;
        //create title
        var title = $(document.createElement("div"));
        title.appendChild(document.createTextNode("请输入书签名:"));
        this.panelContainer.appendChild(title);
        
        //create input name box
        this.inputName = $(document.createElement("input"));
        this.inputName.type = "text";
        this.inputName.value = this.name;
        this.panelContainer.appendChild(this.inputName);        
        
        //create footer bar
        var divFooter = $(document.createElement("div"));
        divFooter.addClassName("footer");
        this.panelContainer.appendChild(divFooter);
        
        //create submit button
        var btnSubmit = $(document.createElement("input"));
        btnSubmit.type = "button";
        btnSubmit.value = "添加";
        btnSubmit.onclick = function(){
            caller.onSubmit();
        }
        divFooter.appendChild(btnSubmit);
        
        //create cancel button
        var btnCancel = $(document.createElement("input"));
        btnCancel.type = "button";
        btnCancel.value = "取消";
        btnCancel.addClassName("btnCancel");
        btnCancel.onclick = function(){
            caller.close();
        }
        divFooter.appendChild(btnCancel);        

        $(ID_BM_PANEL_ADD).appendChild(this.panelContainer);       
        Bookmark.showPanelAdd();
        this.inputName.focus();
    },
    onSubmit : function()
    {
        if (Validator.isEmpty(this.inputName.value)) {
            Bookmark.reportError("请输入书签名");
            this.inputName.focus();
            return;
        }
        
        var caller = this;
        Bookmark.showDatatransStatus();
        Bookmark.hidePanelAdd();
        new Ajax.Request(URL_BM_ADD,{
            method : "post",
            parameters : {
                id : caller.id, 
                name : caller.inputName.value
            },
            onSuccess : function(transport)
            {
                caller.parseResponseDataSubmit(transport.responseText);
            },
            onFailure : function(transport)
            {                
                caller.onFailureSubmit("网络故障");
            }
        });
    },
    parseResponseDataSubmit : function(responseText)
    {
        if (null == responseText || responseText.length <= 0)
        {
            this.onFailureSubmit("网络错误");
            return;
        }

        var response = null;
        try {
            response = responseText.evalJSON();
        }
        catch (exp)
        {
            this.onFailureSubmit("未知错误");
            return;        
        }        
        
        if (response.flag != 1) 
        {
            this.onFailureSubmit(response.errorMsg);
        }
        else
        {
            this.onSuccessSubmit(responseText);
        }
    },
    onSuccessSubmit : function(text)
    {
        Bookmark.hideDatatransStatus();
        Bookmark.reportMsg("书签添加成功");
        this.close();
    },
    onFailureSubmit : function(text)
    {
        var strErrorMsg;
        if (null == text || text.length <= 0)
        {
            strErrorMsg = "未知原因";
        }
        else 
        {
            strErrorMsg = text;
        }
        
        Bookmark.hideDatatransStatus();
        Bookmark.reportError(strErrorMsg);
        this.close();
    }
});

/*********************************************************************************
* Bookmark View
*********************************************************************************/
var PanelBookmarkView = Class.create({
    open : function()
    {
        var caller = this;
        
        Bookmark.showDatatransStatus();
        Bookmark.showPanelView();
        new Ajax.Updater(ID_BM_PANEL_VIEW, URL_BM_VIEW, {
            onComplete : function()
            {
                Bookmark.hideDatatransStatus();
            },
            onFailure : function ()
            {
                caller.onFailure("网络故障");
            }
        });
    },
    onFailure : function(text)
    {
        Bookmark.hidePanelView();
        Bookmark.reportError(text);
    }
});

/*********************************************************************************
* Bookmark Delete
*********************************************************************************/
var PanelBookmarkDelete = Class.create({
    initialize : function(nId, strName)
    {
        this.id = nId;
        this.name = strName;
    },
    open : function()
    {
        if (null == this.id)
        {
            throw new Error("书签id无效!");
        }
        
        var caller = this;
        Bookmark.hidePanelView();
        Bookmark.showDatatransStatus();
        new Ajax.Request(URL_BM_DELETE, {
            method : "post",
            parameters : {
                id : caller.id
            },
            onSuccess : function(transport)
            {
                caller.parseResponseData(transport.responseText);
            },
            onFailure : function(transport)
            {
                caller.onFailure("网络故障");
            }
        });
    },
    parseResponseData : function(responseText)
    {
        if (null == responseText || responseText.length <= 0)
        {
            this.onFailure("网络故障");
            return;
        }

        var response = null;
        try {
            response = responseText.evalJSON();
        }
        catch (exp)
        {
            this.onFailure("未知错误");
            return;        
        }        
        
        if (response.flag != 1) 
        {
            this.onFailure(response.errorMsg);
        }
        else
        {
            this.onSuccess();
        }        
    },
    onSuccess : function()
    {
        Bookmark.hideDatatransStatus();
        clientBookmarkView();
        Bookmark.reportMsg("[" + this.name + "] 已删除");
    },
    onFailure : function(text)
    {
        var strErrorMsg;
        if (null == text || text.length <= 0)
        {
            strErrorMsg = "未知原因";
        }
        else 
        {
            strErrorMsg = text;
        }
        
        Bookmark.hideDatatransStatus();
        Bookmark.reportError("书签删除失败:" + strErrorMsg);
        clientBookmarkView();
    }
});

/*********************************************************************************
* Comment
*********************************************************************************/
//id
var ID_COMMENT_MSG = "comment_msg";
var ID_COMMENT_ERROR = "comment_error";
var ID_COMMENT_DATATRANSSTATUS = "comment_datatransstatus";

var ID_PANEL_COMMENT_ADD = "panel_comment_add";
var ID_FORM_COMMENT_ADD = "form_commenent_add";
var ID_BTNSUBMIT_COMMENT_ADD = "btnsubmit_commenent_add";

var ID_PANEL_COMMENT_VIEW = "panel_comment_view";

//url
var URL_ARTICLECOMMENT_ADD = "articlecomment_add.aspx";
var URL_ARTICLECOMMENT_VIEW = "articlecomment_view.aspx";
/***************************************
* Comment System
****************************************/
var Comment = new Object();

Comment.showPanelAdd = function()
{
    var element = $(ID_PANEL_COMMENT_ADD);
    if (null != element)
    {
        element.show();
        element.scrollTo();
    }
}
Comment.hidePanelAdd = function()
{
    $(ID_PANEL_COMMENT_ADD).hide();
}

Comment.showPanelView = function()
{
    new Effect.Appear(ID_PANEL_COMMENT_VIEW, {duration:1});
}
Comment.hidePanelView = function()
{
    new Effect.Fade(ID_PANEL_COMMENT_VIEW, {duration:1});
}

Comment.showDatatransStatus= function()
{
    $(ID_COMMENT_DATATRANSSTATUS).show();
}
Comment.hideDatatransStatus = function()
{
    $(ID_COMMENT_DATATRANSSTATUS).hide();
}

Comment.showMsg = function()
{
    new Effect.Appear(ID_COMMENT_MSG, {duration:1});
}
Comment.hideMsg = function()
{
    new Effect.Fade(ID_COMMENT_MSG, {duration:1});
}
Comment.reportMsg = function(text)
{
    $(ID_COMMENT_MSG).innerHTML = text;
    this.showMsg();
    setTimeout(Comment.hideMsg, 3000);
}

Comment.showError = function()
{
    new Effect.Appear(ID_COMMENT_ERROR, {duration:1});
}
Comment.hideError = function()
{
    new Effect.Fade(ID_COMMENT_ERROR, {duration:1});
}
Comment.reportError = function(text)
{
    $(ID_COMMENT_ERROR).innerHTML = text;
    this.showError();
    setTimeout(Comment.hideError, 3000);
}

/***************************************
* Article Comment Add
****************************************/
var PanelArticleCommentAdd = Class.create({
    initialize : function(id, email)
    {
        this.id = id;
        this.email = email;
        
        this.outer = null;
        this.inputEmail = null;
        this.inputText = null;
    },
    open : function()
    {
        var caller = this;
        
        if (null != document.getElementById(ID_FORM_COMMENT_ADD))
        {
            Comment.showPanelAdd();
            return;
        }
        
        //get container
        var divContainer = $(ID_PANEL_COMMENT_ADD);        
        
        //create outer
        this.outer = document.createElement("div");        
        divContainer.appendChild(this.outer);
        
        //create form
        var form = document.createElement("form");
        form.id = ID_FORM_COMMENT_ADD;
        this.outer.appendChild(form);
        
        //create table
        var table = document.createElement("table");
        table.align = "center";
        form.appendChild(table);
        
        var tbody = document.createElement("tbody");
        table.appendChild(tbody);
        
        var nRowIndex = 0;
        var nCellIndex = 0;
        var row, cell;
        
        row = tbody.insertRow(nRowIndex);       //label email
        cell = row.insertCell(nCellIndex);        
        cell.appendChild(document.createTextNode("Email: "));
        
        ++nCellIndex;
        cell = row.insertCell(nCellIndex);        
        this.inputEmail = document.createElement("input");
        this.inputEmail.type = "text";
        this.inputEmail.value = this.email;
        cell.appendChild(this.inputEmail);
        
        ++nRowIndex;
        nCellIndex = 0;
        row = tbody.insertRow(nRowIndex);
        cell = row.insertCell(nCellIndex);
        cell.appendChild(document.createTextNode("问题："));
        
        ++nCellIndex;
        cell = row.insertCell(nCellIndex);
        this.inputText = document.createElement("textarea");
        this.inputText.cols = "40";
        this.inputText.rows = "5";        
        cell.appendChild(this.inputText);
                                
        //add footer
        var divFooter = document.createElement("div");
        divFooter.className = "footer";
        this.outer.appendChild(divFooter);
        
        var btnSubmit = document.createElement("input");
        btnSubmit.type = "button";
        btnSubmit.value = "发送";
        btnSubmit.onclick = function() {
            caller.onSubmit();
        }
        divFooter.appendChild(btnSubmit);
        
        var btnCancel = document.createElement("input");
        btnCancel.type = "button";
        btnCancel.value = "取消";
        btnCancel.className = "btnCancel";
        btnCancel.onclick = function() {
            caller.close();
        }
        divFooter.appendChild(btnCancel);
                                
        Comment.showPanelAdd();
        if (Validator.isEmpty(this.email))
        {
            this.inputEmail.focus();
        }
        else 
        {
            this.inputText.focus();
        }
    },
    close : function()
    {
        $(ID_PANEL_COMMENT_ADD).removeChild(this.outer);
    },
    validateForm : function()
    {
        if (Validator.isEmpty(this.inputText.value))
        {
            Comment.reportError("请输入问题");
            this.inputText.focus();
            return false;
        }
        
        return true;
    },
    onSubmit : function()
    {
        if (!this.validateForm())
        {
            return;
        }

        Comment.hidePanelAdd();
        Comment.showDatatransStatus();
        var caller = this;        
        new Ajax.Request(URL_ARTICLECOMMENT_ADD,{
            method : "post",
            parameters : {
                id : caller.id,
                email : encodeURI(caller.inputEmail.value),
                text : encodeURI(caller.inputText.value)
            },
            onSuccess : function(transport)
            {
                caller.parseResponseData(transport.responseText);
            },
            onFailure : function(transport)
            {
                caller.onFailure("网络故障");
            }
        });        
    },
    parseResponseData : function(responseText)
    {
        if (null == responseText || responseText.length <= 0)
        {
            this.onFailure("网络故障");
            return;
        }

        var response = null;
        try {
            response = responseText.evalJSON();
        }
        catch (exp)
        {
            this.onFailure("未知错误");
            return;        
        }        
        
        if (response.flag != 1) 
        {
            this.onFailure(response.errorMsg);
        }
        else
        {
            this.onSuccess(response.errorMsg);
        }            
    },
    onSuccess : function(text)
    {
        Comment.hideDatatransStatus();
        Comment.hidePanelAdd();
        Comment.reportMsg(text);
        this.close();        
    },
    onFailure : function(text)
    {
        Comment.hideDatatransStatus();
        Comment.showPanelAdd();
        Comment.reportError(text);
    }
});

/***************************************
* Article Comment View
****************************************/
var PanelArticleCommentView = Class.create({
    initialize : function(id)
    {
        if (null == id) 
        {
            throw new Error("PanelArticleCommentView : param id is invalid");
        }
        this.id = id;
    },
    open : function()
    {
        Comment.showDatatransStatus();
        var caller = this;
        
        new Ajax.Updater(ID_PANEL_COMMENT_VIEW, URL_ARTICLECOMMENT_VIEW, {
            method : "post",
            parameters : {
                id : caller.id
            },
            onComplete : function() {
                Comment.hideDatatransStatus();
            },
            onSuccess : function() {
                Comment.showPanelView();
            },
            onFailure : function() {
                Comment.reportError(text);
            }
        });
    }
});

/*********************************************************************************
* interface
*********************************************************************************/
function clientMemberLogin()
{
    var wnd = new WndMemberLogin();
    wnd.open();
}
function clientMemberLogout()
{
    Bookmark.hidePanelView();
    Bookmark.hidePanelAdd();
    
    var wnd = new WndMemberLogout();
    wnd.open();
}
function clientMemberRegister()
{
    var wnd = new WndMemberRegister();
    wnd.open();
}
function clientMemberUpdate()
{
    var wnd = new WndMemberUpdate();
    wnd.open();
}
function clientMemberGetPwd()
{
    var wnd = new WndMemberGetPwd();
    wnd.open();
}

function clientBookmarkAdd(nId, strName)
{
    Bookmark.hidePanelView();
    var panel = new PanelBookmarkAdd(nId, strName);
    panel.open();
}
function clientBookmarkView()
{
    Bookmark.hidePanelAdd();
    var panel = new PanelBookmarkView();
    panel.open();
}
function clientBookmarkDelete(nId, strName)
{
    var panel = new PanelBookmarkDelete(nId, strName);       
    panel.open();
}

function clientArticleCommentAdd(id, email)
{
    var panel = new PanelArticleCommentAdd(id, email);
    panel.open();
}
function clientArticleCommentView(id)
{
    var panel = new PanelArticleCommentView(id);
    panel.open();
}