﻿var listKytu = new Array("a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "m", "n", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "2", "3", "4", "5", "6", "7", "8", "9");
function CreateCaptcha(StingLenght, idImgElement, idHiddenCode) {
    var temp = '';
    for (var i = 0; i < StingLenght; i++) {
        temp += listKytu[Math.floor(Math.random() * 31)];
    }
    var urlimage = "http://thongtingia.com/captcha.aspx?codeString=" + temp;
    $('#' + idImgElement).attr('src', urlimage);
    $('#' + idImgElement).attr('style', 'width: auto; height: auto;');
    $('#' + idHiddenCode).val(temp);
}

$(document).ready(function () {
    $("#txtname, #txtpass")
 .keypress(function (e) {
     var keyCode = e.keyCode || e.which;

     if (keyCode == 13)
         $("#btnLogin").trigger("click");
 });
});

function getData() {
    var username = document.getElementById('txtname').value;
    var pass = document.getElementById('txtpass').value;
    var passEncode = $.base64Encode(pass);
    var typeCookie = 0;

    var UserFilter = /^[a-z]+[0-9a-z_]{3,32}$/;
    var ReturnUser = UserFilter.test(username);
    if (ReturnUser == false) {
        jAlert('Sai tên đăng nhập hoặc mật khẩu', 'Thông báo');
        return false;
    }
    if ($('#typeCooki').attr('checked') == true) {
        typeCookie = 1;
    }

    $.ajax({
        type: "POST",
        //  data: "{username:'" + username + "',passEncode:'" + passEncode + "',typeCookie:'" + typeCookie + "'}",
        data: "{username:'" + username + "',passEncode:'" + passEncode + "'}",
        contentType: "application/json; charset=utf-8",
        url: "/WebServices/WS_Utility.asmx/SvLogin",
        dataType: "json",
        success: function (data) {
            data = data.d;
            if (data == "Success") {
                var CurrentUrl = window.location;
                window.location.href = CurrentUrl;
                return false;
            }
            else if (data == "Fail") {
                jAlert('Sai tên đăng nhập hoặc mật khẩu', 'Thông báo');
                return false;
            }
            else if (data == "Nochar") {
                jAlert('Bạn phải nhập đầy đủ thông tin đăng nhập', 'Thông báo');
                return false;
            }
            else if (data == "Fail2") {
                jAlert('Bạn đăng nhập không thành công', 'Thông báo');
                return false;
            }
            else if (data == "Block") {
                jAlert('Tài khoản của bạn đã bị khóa vĩnh viễn', 'Thông báo');
                return false;
            }
            else {
                var stBand;
                stBand = data;
                jAlert(stBand, 'Thông báo');
                return false;
            }
        }
    });
}

function doLogin() {
    var username = $('#txtname').val();
    var pass = $('#txtpass').val();
    username = $.trim(username);
    pass = $.trim(pass);
    if (username == '' || pass == '') {
        jAlert('Bạn phải nhập đầy đủ thông tin đăng nhập', 'Thông báo');
        return false;
    }
    else {
        var passEncode = $.base64Encode(pass);
        $('body').mask();
        $.ajax({
            type: "POST",
            data: "{username:'" + username + "',passEncode:'" + passEncode + "'}",
            contentType: "application/json; charset=utf-8",
            url: "/WebServices/WS_Utility.asmx/SvLogin",
            dataType: "json",
            success: function (data) {
                $("body").unmask();
                data = data.d;
                if (data == "Success") {
                    window.location.href = '/BackEnd/BE_base.aspx';
                    return false;
                }
                else if (data == "Fail") {
                    jAlert('Sai tên đăng nhập hoặc mật khẩu.', 'Thông báo');
                    return false;
                }
                else if (data == "Nochar") {
                    jAlert('Bạn phải nhập đầy đủ thông tin đăng nhập', 'Thông báo');
                    return false;
                }
                else if (data == "Fail2") {
                    jAlert('Bạn đăng nhập không thành công.', 'Thông báo');
                    return false;
                }
                else if (data == "Block") {
                    jAlert('Tài khoản của bạn đã bị khóa vĩnh viễn.', 'Thông báo');
                    return false;
                }
                else {
                    jAlert(data, 'Thông báo');
                    return false;
                }
            }
        });
    }
}

function doLogoff() {
    $.ajax({
        type: "POST",
        data: "",
        contentType: "application/json; charset=utf-8",
        url: "/WebServices/WS_Utility.asmx/SvLogoff",
        dataType: "json",
        success: function (data) {
            data = data.d;
            if (data == "LogoffSuccess") {
                window.location.href = "/BackEnd/BE_login.aspx";
                return false;
            }
        }
    });
}

function doRegister() {
    var fullname = $('#txtfull_name').val();
    fullname = $.trim(fullname);
    if (fullname == '') {
        jAlert('Bạn phải nhập Họ và tên', 'Thông báo');
        return false;
    }
    var email = $('#txtemail').val();
    email = $.trim(email);
    if (email == '') {
        jAlert('Bạn phải nhập Email', 'Thông báo');
        return false;
    }
    var EmailFilter = /^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;
    var ReturnVal = EmailFilter.test(email);
    if (ReturnVal == false) {
        jAlert('Email bạn nhập không đúng định dạng', 'Thông báo');
        return false;
    }

    var username = $('#txtname_user').val();

    username = $.trim(username);
    if (username == '') {
        jAlert('Bạn phải nhập tên đăng nhập', 'Thông báo');
        return false;
    }

    var UserFilter = /^[a-z]+[0-9a-z_]{3,32}$/;
    var ReturnUser = UserFilter.test(username);
    if (ReturnUser == false) {
        jAlert('Tên truy cập phải từ 4->32 ký tự, là ký tự từ a->z và từ 0->9, ký tự _, viết liền, viết không dấu, ký tự đầu tiên không được là số', 'Thông báo');
        return false;
    }


    var password = $('#txtpassword').val();
    password = $.trim(password);
    if (password == '') {
        jAlert('Bạn phải nhập Password', 'Thông báo');
        return false;
    }

    var filterPass = /^.{8,50}$/;
    var ReturnPass = filterPass.test(password);
    if (ReturnPass == false) {
        jAlert('Mật khẩu mới phải từ 8-50 ký tự', 'Thông báo');
        return false;
    }

    var re_password = $('#txtre_password').val();
    re_password = $.trim(re_password);
    if (re_password == '') {
        jAlert('Bạn phải nhập lại Mật khẩu', 'Thông báo');
        return false;
    }

    if (password != re_password) {
        jAlert('Mật khẩu bạn nhập chưa giống nhau', 'Thông báo');
        return false;
    }

    var mobile = $('#txtmobile').val();
    mobile = $.trim(mobile);
    if (mobile == '') {
        jAlert('Bạn phải nhập số điện thoại', 'Thông báo');
        return false;
    }
    var Password_userEncode = $.base64Encode(password);

    var security = $('#txtsecurity').val();
    security = $.trim(security);
    if (security == '') {
        jAlert('Bạn phải nhập mã an toàn', 'Thông báo');
        return false;
    }

    var valid_security = $('#HiddenCode').val();

    if (security != valid_security) {
        jAlert('Bạn vui lòng nhập lại mã an toàn', 'Thông báo');
        return false;
    }

    if ($('#accept').attr('checked') == false) {
        jAlert('Bạn phải đồng ý với những điều khoản tại Thông tin giá', 'Thông báo');
        return false;
    }

    $('body').mask();
    $.ajax({
        type: "POST",
        data: "{FullName:'" + fullname + "',Email:'" + email + "',Name_user:'" + username + "',Password_userEncode:'" + Password_userEncode + "',Phone:'" + mobile + "'}",
        contentType: "application/json; charset=utf-8",
        url: "/WebServices/WS_Utility.asmx/ServiceDangky",
        dataType: "json",
        success: function (data) {
            data = data.d;
            $('body').unmask();
            if (data == 'Success') {
                var host = window.location.hostname;
                host = 'http://' + host + '/RegisterSuccess.aspx';
                window.location.href = host;
                return false;
            }
            else if (data == 'ExistName') {
                jAlert('Tên truy cập đã được đăng ký, bạn vui lòng chọn một tên đăng nhập khác', 'Thông báo');
                return false;
            }
            else if (data == 'ExistEmail') {
                jAlert('Email đã được đăng ký, bạn vui lòng chọn một Email khác để đăng ký', 'Thông báo');
                return false;
            }
            else if (data == 'NoFull') {
                jAlert('Bạn vui lòng nhập đầy đủ thông tin đăng ký', 'Thông báo');
                return false;
            }
            else {
                jAlert('Bạn đăng ký tài khoản không thành công trên Thông tin giá', 'Thông báo');
                return false;
            }
        }
    });
}

function doResetPassword() {
    var old_pass = $('#old_password').val();
    old_pass = $.trim(old_pass);
    if (old_pass == "") {
        jAlert('Bạn phải nhập mật khẩu cũ', 'Thông báo');
        return false;
    }

    var new_pass = $('#new_password').val();
    new_pass = $.trim(new_pass);
    if (new_pass == "") {
        jAlert('Bạn phải nhập mật khẩu mới', 'Thông báo');
        return false;
    }

    var filterPass = /^.{8,50}$/;
    var ReturnPass = filterPass.test(new_pass);
    if (ReturnPass == false) {
        jAlert('Mật khẩu mới phải từ 8-50 ký tự', 'Thông báo');
        return false;
    }

    var renew_password = $('#renew_password').val();
    renew_password = $.trim(renew_password);
    if (renew_password == "") {
        jAlert('Bạn phải nhập lại mật khẩu mới', 'Thông báo');
        return false;
    }

    if (new_pass != renew_password) {
        jAlert('Hai mật khẩu phải giống nhau', 'Thông báo');
        return false;
    }

    var old_passEncode = $.base64Encode(old_pass);
    var new_passEncode = $.base64Encode(new_pass);

    $('body').mask();
    $.ajax({
        type: "POST",
        data: "{old_passEncode:'" + old_passEncode + "',new_passEncode:'" + new_passEncode + "'}",
        contentType: "application/json; charset=utf-8",
        url: "/WebServices/WS_Utility.asmx/SvChangePassword",
        dataType: "json",
        success: function (data) {
            $("body").unmask();
            data = data.d;
            if (data == "Success") {
                $('#old_password').val('');
                $('#new_password').val('');
                $('#renew_password').val('');
                jAlert('Bạn đã khôi phục mật khẩu thành công', 'Thông báo');
                return false;
            }
            else if (data == "OldFail") {
                jAlert('Mật khẩu cũ bạn nhập không đúng vui lòng nhập lại', 'Thông báo');
                return false;
            }
            else if (data == "Nochar") {
                jAlert('Bạn vui lòng nhập lại', 'Thông báo');
                return false;
            }
            else {
                jAlert('Bạn khôi phục mật khẩu thất bại', 'Thông báo');
                return false;
            }
        }
    });
}

function doCancelChangePass() {
    $('#old_password').val('');
    $('#new_password').val('');
    $('#renew_password').val('');
    $("#old_password").focus();
}

function doUpdateInfoMember() {
    var full_name = $('#full_name').val();
    full_name = $.trim(full_name);
    if (full_name == '') {
        jAlert('Bạn phải nhập họ tên', 'Thông báo');
        return false;
    }
    var address = $('#address').val();
    var birthday = $('#birthday').val();
    var cmt_num = $('#cmt_num').val();
    var phone = $('#phone').val();


    var fax = $('#fax').val();
    var mobile = $('#mobile').val();
    var yahooid = $('#yahooid').val();
    var skypeid = $('#skypeid').val();

    var website = $('#website').val();
    var ttlh = $('#ttlh').val();

    $('body').mask();
    $.ajax({
        type: "POST",
        data: "{FullName :'" + full_name + "',Address :'" + address + "',NgaySinh :'" + birthday + "',CMT :'" + cmt_num + "',HomePhone :'" + phone + "',Fax :'" + fax + "',Phone :'" + mobile + "',Yahoo :'" + yahooid + "',Skype :'" + skypeid + "',Website :'" + website + "',TtLienHe :'" + ttlh + "'}",
        contentType: "application/json; charset=utf-8",
        url: "/WebServices/WS_Utility.asmx/Sv_UpdateInfoMember",
        dataType: "json",
        success: function (data) {
            $("body").unmask();
            data = data.d;
            if (data == "Success") {
                jAlert('Bạn đã cập nhật thông tin cá nhân thành công.', 'Thông báo');
                return false;
            }
            else if (data == "SessionNull") {
                jAlert('Bạn vui lòng đăng nhập lại để thực hiện chức năng ', 'Thông báo');
                return false;
            }
            else if (data == "NoFullName") {
                jAlert('Bạn phải nhập họ tên', 'Thông báo');
                return false;
            }
            else {
                jAlert('Bạn cập nhật thông tin cá nhân không thành công !', 'Thông báo');
                return false;
            }
        }
    });
}

function doCancelInfoMember() {
    window.location.href = "/BackEnd/BE_updateInfo.aspx";
}
