﻿//加载用户信息
$().ready(function(){
    if(getPageName().toLowerCase() == 'aion-user-setting'){
        loadUserInfo ();
    }
});

var loadUserInfo = function(){

    $.get(g_url.user,{backinfo:'true'},function (data){
    
    var list = $.xml2json(data).Table;
       
        if(list ){
            $('#span_email').html(list.Email);
            $('#span_accountname').html(list.CustomerID);
            if(list .Gender=="M")
            {
               $('#male').attr("checked",true);
            }
            else if(list .Gender=="F")
            {
               $('#female').attr("checked",true);
            }
            if(list .CustomerName!="0")
            {
                $('#txt_customername').attr("value",list .CustomerName);
            }
            $('#txt_oldpwd').attr("title",list .LoginPassword);
            if(list .BornDate!="0")
            {
                var year=list .BornDate.substr(0,4);
                var month=list .BornDate.substr(4,2);
                var day=list .BornDate.substr(6,8);

                $('#txt_birthdate').val(month+"/"+day+"/"+year);
            }
            if(list .Address!="0")
            {
                $('#txt_address').val(list .Address);
            }
            if(list .PostNo!="0")
            {
                $('#txt_postcode').val(list .PostNo);
            }
            if(list .City!="0")
            {
                $('#txt_city').val(list .City);
            }
            if(list .Province!="0")
            {
                $('#txt_province').val(list .Province);
            }
            if(list .Telephone!="0")
            {
                $('#txt_tel').val(list .Telephone);
            }
            if(list .QQ!="0")
            {
                $('#txt_qq').attr("value",list .QQ);
            }
            if(list .Nation!="0")
            {
                $('#txt_country').attr("value",list .Nation);
            }
        }  
        else 
        {
            $('#span_email').html("<span class=\"red\">Unknown User</span>");
            $('#span_accountname').html("<span class=\"red\">Unknown User</span>");
        }        
    })
}

//更新用户
var updataUserInfo = function (){

    var gender= "0";
    if($('#male').attr('checked'))
    {
        gender = "0" ;
    }
    else 
    {
        gender ="1" ;
    }

    if(validate("ul_userinfo")){
         var xmlString = "<userinfo>";
         xmlString+="<rpass>" + escape($("#txt_oldpwd").attr('title')) + "</rpass>";
         xmlString+="<oldpass>" + escape($("#txt_oldpwd").val()) + "</oldpass>";
         xmlString+="<newpass>" + escape($("#txt_newpwd1").val()) + "</newpass>";
         xmlString+="<customername>" + escape($("#txt_customername").val()) + "</customername>";
         xmlString+="<borndate>" + escape($("#txt_birthdate").val()) + "</borndate>";
         xmlString+="<gender>" + escape(gender) + "</gender>";
         xmlString+="<icq>" + escape($('#txt_qq').val()) + "</icq>";
         xmlString+="<telephone>" + escape($('#txt_tel').val()) + "</telephone>";
         xmlString+="<nation>" + escape($('#txt_country').val()) + "</nation>";
         xmlString+="<province>" + escape($('#txt_province').val()) + "</province>";
         xmlString+="<city>" + escape($('#txt_city').val()) + "</city>";
         xmlString+="<postno>" + escape($('#txt_postcode').val()) + "</postno>";
         xmlString+="<address>" + escape($('#txt_address').val()) + "</address>";
         xmlString+="<paypalemail>" + escape($('#txt_paypalemail').val()) + "</paypalemail>";
         xmlString+="</userinfo>";

         $.post(g_url.user,{updateXmlStr:xmlString},function (data){
            if(Number(data) == 1)
            {
                alert ("update your infomation success!");
            }
            else if(Number(data) == 0)
            {
                alert ("Sorry, you password wrong! update failure!");
            }          
            else 
            {
                alert("Your login is loss!Please login again!");
            }  
         })
    }
}

//注册用户
var registerAccount = function (){

    if(validate('regsiterpanel')){
              
        var gender= "0";
        if($('#male').attr('checked'))
        {
            gender = "0" ;
        }
        else 
        {
            gender ="1" ;
        }
        
        var xmlString = "<userinfo>";
        xmlString += "<customerid>" + escape($('#txt_customerid').val()) + "</customerid>";
        xmlString += "<customername>" + escape($('#txt_customername').val()) + "</customername>";
        xmlString += "<loginpassword>" + escape($('#txt_password').val()) + "</loginpassword>";
        xmlString += "<borndate>" + escape($('#txt_borndate').val()) + "</borndate>";
        xmlString += "<gender>" + escape(gender) + "</gender>";
        xmlString += "<icq>" + escape($('#txt_icq').val()) + "</icq>";
        xmlString += "<email>" + escape($('#txt_email').val()) + "</email>";
        xmlString += "<telephone>" + escape($('#txt_telephone').val()) + "</telephone>";
        xmlString += "<mobilecall></mobilecall>";
        xmlString += "<address>" + escape($('#txt_address').val()) + "</address>";
        xmlString += "<city>" + escape($('#txt_city').val()) + "</city>";
        xmlString += "<province>" + escape($('#txt_province').val()) + "</province>";
        xmlString += "<postno>" + escape($('#txt_postcode').val()) + "</postno>";
        xmlString += "<nation>" + escape($('#txt_country').val()) + "</nation>";
        xmlString += "<introduce>" + escape($('#introduce').val()) + "</introduce>";
        xmlString += "<guild>" + escape($('#enterguild').val()) + "</guild>";
        xmlString += "</userinfo>";
        
        $.post(g_url.user,{registerXmlStr:xmlString},function (data){
            if(Number(data) > 0)
            {
                alert ("Congratulations!!!\n\nYou account has been successfully created!\n\nPlease login again this account!")
                window.open("default.aspx", "_self", "");
            }
            else if(Number(data) == -10)
            {
                alert ("Sorry! please refresh this page and try again!");
            }          
            else 
            {
                alert("Sorry! A user already exists with this account or mailbox!");
            }  
         })
    }
}

//判断注册邮箱是否存在
function IsExistsEmail(acc, email){

    $(".red", $('li_email').parent()).remove();
    if(validate("li_email")){
    
        $.get(g_url.user,{acc:acc ,email:email},function (data){
        
            if(Number(data) == 0)
            {
                $('#txt_email').after("<span class=\"red\">It already exists!</span>");
            }
        })
    }
}

//判断注册账号是否存在
function IsExistsAcc(acc, email){

    $(".red", $('li_accountname').parent()).remove();
    if(validate("li_accountname")){
    
        $.get(g_url.user,{acc:acc ,email:email},function (data){            
        
            if(Number(data) == 0)
            {
                $('#txt_customerid').after("<span class=\"red\">It already exists!</span>");
            }
        })
    }
}

//Important Information Select
function ii_Changed(me) 
{
    if (me == "1") 
    {
        $('#i1').show();
        $('#i2').hide();
    }
    else 
    {
        $('#i2').show();
        $('#i1').hide();
    }
}

//修改用户密码
function findUserPwd(){

    if(validate('ul_usr_info')){
    
        var xmlString="<info>";
        xmlString+="<customerid>" + escape($('#tb_usr_name').val()) + "</customerid>";
        xmlString+="<email>" + escape($('#tb_usr_mail').val()) + "</email>";
        xmlString+="<newpass>" + escape($('#tb_usr_pwd').val()) + "</newpass>";
        xmlString+="</info>";
        
        $.post(g_url.user,{passwordxmlStr:xmlString},function (data){
            if(Number(data) == 1)
            {
                $('#tb_usr_pwd').val("");
                $('#tb_usr_rpwd').val("");
                alert ("update your password success!")
            }       
            else 
            {
                alert("Sorry! update your password failure!");
            }  
         })
    }
}

//判断邮箱是否存在
function FIsExistsEmail(acc, email){

    $(".red", $('span_email').parent()).remove();
    if(validate("span_email")){
    
        $.get(g_url.user,{acc:acc ,email:email},function (data){
        
            if(Number(data) == 1)
            {          
                $('#tb_usr_mail').after("<span class=\"red\">Sorry,this mailbox not exists!!</span>");
            }
        })
    }
}

//判断账号是否存在
function FIsExistsAcc(acc, email){

    $(".red", $('span_accountname').parent()).remove();
    if(validate("span_accountname")){
    
        $.get(g_url.user,{acc:acc ,email:email},function (data){            

            if(Number(data) == 1)
            {
                $('#tb_usr_name').after("<span class=\"red\">Sorry,this account is not exists!</span>");
            }
        })
    }
}