﻿DomLoaded.load(InitGuestbook);
//初始化留言本
function InitGuestbook(){
    //添加textarea拉伸功能
    AddTextAreaSizeBar();
    InitGuestForm.call($fm("MessageBox"));
    InitGuestList.call($("guestbook"));
}
//初始化留言本表单
function InitGuestForm(){
    this.table = $a.call(this,"table");
    SetFormMode.call(this);
    BatchRunFun.call(this, SetEachRegex);
    this.onsubmit = GuestFormSubmit;
}
//设置表单模式
function SetFormMode(){
    if(typeof(this["validate"]) == "undefined")
        this.mode = "validate";//已注册验证模式
    else if(this["nickname"].disabled)
        this.mode = "login";//登录未验证模式
    else if(this["already"] && this["already"].checked)
        this.mode = "logout";//已注册未登录模式
    else
        this.mode = "normal";//默认模式
}
function SetEachRegex(){
    NormalEvent.call(this);
    switch(this.name){
        case "already":
            BindAlready.call(this);
            break;
        case "validate":
            BindValidate.call(this);
            break;
        case "nickname":
            BindNickname.call(this);
            break;
        case "account":
            BindAccout.call(this);
            break;
        case "recordkey":
            BindRecordkey.call(this);
            break;
        case "message":
            BindMessage.call(this);
            break;
        default:
            break;
    }
    SetNormalEvent.call(this);
}
//绑定默认事件
function NormalEvent(){
    this.onfocus = function(){
        this.form.table.className = "message choose";
    }
    this.onblur = function(){
        this.form.table.className = "message";
    }
    if(this.type == "text" || this.type == "password")
        SetRelate.call(this);
}
//设置默认事件
function  SetNormalEvent(){
    if((this.type == "text" || this.type == "password")
        && this.value != "" && this.name != "nickname")
            this.value = "";
}
//添加对象关系
function SetRelate(){
    var obj = this.parentNode.parentNode;
    var oSpan = document.createElement("span");
    this.Relate = oSpan;
    this.IsCorrect = false;
    obj.appendChild(oSpan);
    this.setAttribute("autocomplete","off");
}
//绑定登录开始
function BindAlready(){
    this.onclick = function(){
        if(this.checked)
            this.form["account"].Open();
        else
            this.form["account"].Hidden();
    }
}
//绑定帐号
function BindAccout(){
    this.value = "";
    this.Open = function(){
        this.SetStyle("", "none", "logout");
        SetResultBG.call(this.form["nickname"], "","");
        this.form["nickname"].onchange();
    }
    this.Hidden = function(){
        this.SetStyle("none", "", "normal");
        this.form["nickname"].onchange();
    }
    this.SetStyle = function(mStyle, oStyle, modeName){
        SetTableStyle.call([this,this.form["recordkey"]], mStyle);
        SetTableStyle.call([this.form["validate"]], oStyle);
        this.form.mode = modeName;
    }
    this.Return = ReturnLoginAccount;
    this.CheckFunction = CheckToServer;
    DefaultChange.call(this);
}
function SetTableStyle(sStyle){
    for(var i=0,j=this.length;i<j;i++){
        this[i].parentNode.parentNode.parentNode.style.display = sStyle;
    }
}
//绑定验证码
function BindValidate(){
    $("validateImg").onclick = refreshImg;
    this.Relate.innerHTML = "If you're not authenticated, please input!";
    this.Return = returnValidate;
    this.CheckFunction = CheckToServer;
    DefaultChange.call(this);
}
//绑定昵称
function BindNickname(){
    if(this.form.mode == "normal" && this.value != ""){
        if(!CheckLength.call(this,3,25))
            SetResultBG.call(this, "error","Required length is 3-25 characters.");
        else if(/\W+/.test(this.value))
            SetResultBG.call(this, "error","The Nickname only support (a-zA-Z_0-9).");
        else
            this.IsCorrect = true;
    }
    this.onchange = function(){
        if(!CheckLength.call(this,3,25)){
            SetResultBG.call(this, "error","Required length is 3-25 characters.");
        }else if(/\W+/.test(this.value)){
            SetResultBG.call(this, "error","The Nickname only support (a-zA-Z_0-9).");
        }else if(CheckNull.call(this)){
            if(this.form.mode == "normal")
                CheckToServer.call(this);
            else
                SetResultBG.call(this, "correct","Ok!");
        }else{
            SetResultBG.call(this, "","");
        }
    }
}
//绑定密码
function BindRecordkey(){
    this.CheckFunction = CheckRecordkeyLength;
    DefaultChange.call(this);
}
function DefaultChange(){
    this.onchange = function(){
        if(CheckNull.call(this))
            this.CheckFunction.call(this);
    }
}
//绑定留言信息
function BindMessage(){
    this.Relate = $("MaxChars");
    this.onkeyup = function(){
        this.Relate.innerHTML = this.value.length;
    }
    this.onkeydown = function(e){
        var Keys;
	    if(!e)Keys = window.event.keyCode;	
	    else Keys = e.which;
        if(this.value.length > 1499 & Keys != 8 && Keys != 46)
            return false;
    }
    this.CheckFunction = CheckMessageLength;
    DefaultChange.call(this);
}
//检查留言长度
function CheckMessageLength(){
    if(!CheckLength.call(this,10,1500))
        SetResultBG.call(this, "error","The required length for message is 10-1500 characters.");
    else
        SetResultBG.call(this, "correct","Ok!");
}
//服务器端检查
function CheckToServer(){
    var URL = "/member/validate.xhtml";
    var postStr = "action="+ this.name + "&value=" + this.value;
    var Total = new ajax.$x(URL,"POST",returnStatus,CheckError,postStr,this,false);
    SetResultBG.call(this, "loading","Validating, wait please...");
}
//设置Form提交
function GuestFormSubmit(){
    switch(this.mode){
        case "normal":
            this.InputText = [this["nickname"], this["validate"]];
            break;
        case "logout":
            this.InputText = [this["nickname"], this["account"], this["recordkey"]];
            break;
        case "login":
            this.InputText = [this["validate"]];
            break;
        case "validate":
            this.InputText = [];
            break;
        default:
            break;
    }
    this.InputText.push(this["message"]);
    if(!RegisterSubmit.call(this))
        return false;
}
//初始化留言本列表
function InitGuestList(){
    this.QuoteTo = $f("MessageBox","message");
    BatchRunFun.call($as.call(this, "dl"), SetListEvent);
}
function SetListEvent(){
    this.QuoteLink = $a.call(this, "a", 1);
    this.Floor = $a.call(this, "em").innerHTML;
    this.Owner = GetOwner.call($a.call(this, "dt"));
    var oObj = this;
    this.QuoteLink.onclick = function(){
        oObj.parentNode.QuoteTo.value += "[Quote id=" + oObj.Floor + "]" + oObj.Owner + "[/Quote]\r\n";
        oObj.parentNode.QuoteTo.onchange();
    }
}
//得到昵称
function GetOwner(){
    var regex = new RegExp("([^> \r\n]*)<span>","ig");
    regex.test(this.innerHTML)
    return RegExp.$1;
}
