function setFocus() {
   if(typeof(document.forms[0].elements[0]) != 'undefined')
      document.forms[0].elements[0].focus(); 
}

function cleanError(id)
{
    var handle= document.getElementById(id); 
    if(handle) handle.innerHTML = "&nbsp;"; 
}
 
function registerUser()
{
    cleanError("regError"); 
    var email = document.regForm.email.value;
    var name = document.regForm.realname.value;
    var password1 = document.regForm.password1.value;
    var password2 = document.regForm.password2.value;

    if(password1 != password2)
    {
        $("#regError").html("错误: 二次输入密码不一样");
        return;
    }
    if(password1.length < 6)
    {
        $("#regError").html("错误: 新密码至少要有6个字符");
        return;
    }

    setTimeout(function(){_registerUser(); }, 1000);
}

function _registerUser()
{
    var email = document.regForm.email.value;
    var name = document.regForm.realname.value;
    var password1 = document.regForm.password1.value;
    var password2 = document.regForm.password2.value;

    $("#submitImageBtn").show(); 
    $("#submitBtn").hide(); 

    var salt = password2.substr(Math.floor(password2.length/2),password2.length);
    var md5hex = hex_hmac_md5(password2, salt);

    var url = "/account/add_user.php";

    var params = "email="+encodeURIComponent(email) + "&password="+md5hex + "&name=" + encodeURIComponent(name);

    $.ajax({
           type: "POST",
           url: url, 
           data: params,
           contentType: 'application/x-www-form-urlencoded',
           success: function(p) { registerUserSuccess(); }, 
           error: function(p) { registerUserFailure(p); }
    });
}

function registerUserSuccess()
{
    $("#submitImageBtn").hide(); 
    $("#submitBtn").show(); 
    window.location="/account/welcome_user.php"; 
}

function registerUserFailure(p)
{
    $("#submitImageBtn").hide(); 
    $("#submitBtn").show(); 
    $("regError").html(p.responseText); 
}
    
function userLogon(mainpage)
{
    $("#loginError").html(); 
    $("#submitImageBtn").show(); 
    $("#submitBtn").hide(); 

    setTimeout(function(){_userLogon(mainpage); }, 1000);
}

function _userLogon(mainpage)
{
    var email = document.loginForm.email.value; 
    var password = document.loginForm.password.value; 
    var rememberme = document.loginForm.rememberme.checked; 
    var seed = document.loginForm.seed.value; 
    var salt = password.substr(Math.floor(password.length/2),password.length); 
    var md5hex1 = hex_hmac_md5(password, salt); 
    var md5hex2 = hex_hmac_md5(md5hex1, seed);
    var url = "/account/logon_user.php"; 

    var params = "email="+email + "&password="+md5hex2; 
    if(rememberme)
        params += "&rememberme=" + rememberme; 

    $.ajax({
           type: "POST", 
           url: url, 
           data: params,
           contentType: 'application/x-www-form-urlencoded',
           success: function(p) { userLogonSuccess(mainpage)},
           error: function(p) { userLogonFailure(p); }
    }); 
}

function userLogonSuccess(mainpage)
{
    $("#submitImageBtn").hide(); 
    $("#submitBtn").show(); 
    window.location=mainpage; 
}

function userLogonFailure(p)
{
    $("#submitImageBtn").hide(); 
    $("#submitBtn").show(); 
    $("#loginError").html(p.responseText); 
}

function updateUserPerm(element, uid)
{
    var rule = element.value; 
    var url = "/account/updaterule.php?uid=" + encodeURIComponent(userid) + "&rule=" + encodeURIComponent(rule); 
    $.ajax({
           type: "GET",
           url: url,
           success: function(p) { userLogonSuccess(mainpage)},
           error: function(p) { userLogonFailure(p); }
    });
}

function showChangePassword()
{
    var id1 = document.getElementById("showPasswordChange");
    var id2 = document.getElementById("showPasswordForm");
    if(id2.style.display != "none")
    {
      id2.style.display = "";
      id1.innerHTML = "hide password";
    }
    else
    {
      id2.style.display = "none";
      id1.innerHTML = "change password";
    }
}

function updateProfile()
{
    cleanError("updateProfileError"); 

    var name = document.updateProfileForm.realname.value;
    
    if(document.updateProfileForm.oldpassword.value.length == 0 &&
       (document.updateProfileForm.newpassword1.value.length != 0 ||
       document.updateProfileForm.newpassword1.value.length != 0))
    {
        document.getElementById("updateProfileError").innerHTML="错误: 如更改密码，请填写旧密码";
        return;
    }

    if(document.updateProfileForm.oldpassword.value.length != 0)
    {

       var oldpass = document.updateProfileForm.oldpassword.value;
       var newpass1 = document.updateProfileForm.newpassword1.value;
       var newpass2 = document.updateProfileForm.newpassword2.value;

       if(newpass1 != newpass2)
       {
           document.getElementById("updateProfileError").innerHTML="错误: 二次输入新密码不一样";
           return;
       }

       if(newpass1.length < 6)
       {
           document.getElementById("updateProfileError").innerHTML="错误: 新密码至少要有6个字符";
           return;
       }
    }

    $("#submitImageBtn").show(); 
    $("#submitBtn").hide(); 
    setTimeout(function(){_updateProfile(); }, 1000);
}

function _updateProfile()
{
    var name = document.updateProfileForm.realname.value; 

    var params = "name=" + encodeURIComponent(name);
    var url = "/account/update_profile.php";
    if(document.updateProfileForm.oldpassword.value.length != 0)
    {

       var oldpass = document.updateProfileForm.oldpassword.value; 
       var newpass1 = document.updateProfileForm.newpassword1.value; 
       var newpass2 = document.updateProfileForm.newpassword2.value; 

       var salt = oldpass.substr(Math.floor(oldpass.length/2),oldpass.length);
       var md5hex1 = hex_hmac_md5(oldpass, salt);
       salt = newpass2.substr(Math.floor(newpass2.length/2),newpass2.length);
       var md5hex2 = hex_hmac_md5(newpass2,salt);

       params += "&oldpassword="+md5hex1 + "&newpassword="+md5hex2;

    }
    $.ajax({
       type: "POST",
       url: url,
       data: params,
       contentType: 'application/x-www-form-urlencoded',
       success: function(p) { 
             $("#submitImageBtn").hide();
             $("#submitBtn").show();
             $("#updateProfileError").html("<span style=\"color:green\">修改成功</span>"); 
       },
       error: function(p) { 
             $("#submitImageBtn").hide();
             $("#submitBtn").show();
             $("#updateProfileError").html(p.responseText); 
       }
    });
}

function postComment()
{
    var topic = document.postCommentForm.topic.value; 
    var detail = document.postCommentForm.detail.value; 
    var params = "topic=" + encodeURIComponent(topic) + "&detail=" + encodeURIComponent(detail); 

    if(topic.length < 0)
    {
        $("#postCommentError").html("错误: 没有标题");
        return;
    }

    if(detail.length < 0)
    {
        $("#postCommentError").html("错误: 没有内容");
        return;
    }

    if(typeof(document.postCommentForm.realname) != 'undefined')
    {
       var name = document.postCommentForm.realname.value;
       var email = document.postCommentForm.email.value;
       params += "&name="+name + "&email="+encodeURIComponent(email); 
    }

    if(document.postCommentForm.sticky)
    {
       var sticky = document.postCommentForm.sticky.checked; 
       var locked = document.postCommentForm.locked.checked; 
       if(sticky)
          params += "&sticky=" + sticky; 
       if(locked)
          params += "&locked=" + locked; 
    }

    $("#submitBtn").attr("disabled","disabled");

    var url = "/forum/add_topic.php";
    $.ajax({
           type: "POST",
           url: url,
           data: params,
           contentType: 'application/x-www-form-urlencoded',
           success: function(p) { $("#submitBtn").removeAttr("disabled"), window.location = "/forum/main.php"; },
           error: function(p) { $("#submitBtn").removeAttr("disabled"); $("#postCommentError").html(p.responseText); }
     });
}


function addAnswer()
{
    var a_answer = document.addAnswerForm.a_answer.value; 
    var id = document.addAnswerForm.id.value; 

    if(a_answer.length < 0)
    {
        $("#addAnswerError").html("错误: 没有内容");
        return;
    }

    var params = "id=" + id + "&a_answer=" + encodeURIComponent(a_answer); 
    if(document.addAnswerForm.realname)
    {
       var name = document.addAnswerForm.realname.value;
       var email = document.addAnswerForm.email.value;
       params += "&name="+encodeURIComponent(name) + "&email="+encodeURIComponent(email);
    }

    $("#submitBtn").attr("disabled","disabled");
    var url = "/forum/add_answer.php";

    $.ajax({
           type: "POST",
           url: url,
           data: params,
           contentType: 'application/x-www-form-urlencoded',
           success: function(p) { $("#submitBtn").removeAttr("disabled"), window.location = "/forum/view_topic.php?id=" + id; },
           error: function(p) { $("#submitBtn").removeAttr("disabled"); $("#addAnswerError").html(p.responseText); }
     });

}

function updateTopic()
{
    var id = document.updateTopicForm.id.value; 
    var topic = document.updateTopicForm.topic.value; 
    var detail = document.updateTopicForm.detail.value; 
    var params = "id=" + id + "&topic=" + encodeURIComponent(topic) + "&detail=" + encodeURIComponent(detail); 

    if(topic.length < 0)
    {
        $("#updateTopicError").html("错误: 没有标题");
        return;
    }

    if(topic.length < 0)
    {
        $("#updateTopicError").html("错误: 没有内容");
        return;
    }

    if(document.updateTopicForm.sticky)
    {
       var sticky = document.updateTopicForm.sticky.checked; 
       var locked = document.updateTopicForm.locked.checked; 
       if(sticky)
          params += "&sticky=" + sticky; 
       if(locked)
          params += "&locked=" + locked; 
    }

    $("#submitBtn").attr("disabled","disabled");
    var url = "/forum/update_topic.php";

    $.ajax({
           type: "POST",
           url: url,
           data: params,
           contentType: 'application/x-www-form-urlencoded',
           success: function(p) { $("#submitBtn").removeAttr("disabled"), $("#updateTopicError").html("<span style=\"color:green\">修改成功</span>");},
           error: function(p) { $("#submitBtn").removeAttr("disabled"); $("#updateTopicError").html(p.responseText); }
     });
}

function updateAnswer()
{
    var q_id = document.updateAnswerForm.q_id.value; 
    var a_id = document.updateAnswerForm.a_id.value; 
    var answer = document.updateAnswerForm.answer.value; 
    var params = "q_id=" + q_id + "&a_id="+a_id + "&answer=" + encodeURIComponent(answer);

    if(answer.length < 0)
    {
        $("#updateTopicError").html("错误: 没有内容");
        return;
    }

    $("#submitBtn").attr("disabled","disabled");
    var url = "/forum/update_answer.php";
    $.ajax({
           type: "POST",
           url: url,
           data: params,
           contentType: 'application/x-www-form-urlencoded',
           success: function(p) { $("#submitBtn").removeAttr("disabled"), $("#updateAnswerError").html("<span style=\"color:green\">修改成功</span>");},
           error: function(p) { $("#submitBtn").removeAttr("disabled"); $("#updateAnswerError").html(p.responseText); }
     });

}

