//required ajaxcommon.js file
var isLogin = false;
var strSend = '';
function ajax_login()
{
    if(!GetE("email").value)
    {
         //SendErrorInfo('101', 'Hãy nh?p email');
    }
    isLogin = true;
    //get my account block info
    strSend = "?email=" + GetE("email").value + "&passwd=" + GetE("passwd").value + '&login';
    sendData(login_OnResponsed, "ajax-login.php" + strSend, true, null);
    // for wishlist
    strSend = "?email=" + GetE("email").value + "&passwd=" + GetE("passwd").value + '&wishlist';
}

function wishlist_OnResponsed(xmlHttp)
{
    if(xmlHttp)
    {
        if(GetE("wishlist_block")) GetE("wishlist_block").innerHTML = xmlHttp.responseText;
    }
}

function ajax_logout()
{
    isLogin = false;
    sendData(login_OnResponsed, "ajax-login.php?mylogout", true, null);
}

function getTopLoginText()
{
    if(isLogin)
    {
        return '<a href="my-account.php">Tài khoản của bạn</a>';
    }
    return '<a href="my-account.php">Đăng ký</a>';
}

function login_OnResponsed(xmlHttp)
{
    if(xmlHttp)
    {
        var err = GetError(xmlHttp);
        if(err)
        {
            if(err[1].indexOf('mail') >= 0)
                GetE('email').focus();
            else if(err[1].indexOf('khẩu') >= 0)
                GetE('passwd').focus();
            SendErrorInfo(err[0], err[1]);
            return;
        }
        GetE("block_my_account").innerHTML = xmlHttp.responseText;
        GetE("top_account").innerHTML = getTopLoginText();
        if(!isLogin) 
        {
            if(GetE("wishlist_block")) GetE("wishlist_block").innerHTML = '';
        }
        else
        {
            //get mywishlist block info
            sendData(wishlist_OnResponsed, "ajax-login.php" + strSend, true, null);
        }
    }
}
