var http = createRequestObject();

function createRequestObject() {
        var xmlhttp;
        try { xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); }
        catch(e) {
    try { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
        catch(f) { xmlhttp=null; }
  }
  if(!xmlhttp&&typeof XMLHttpRequest!="undefined") {
        xmlhttp=new XMLHttpRequest();
  }
        return  xmlhttp;
}

function trim(a) {
        return a.replace(/^s*(S*(s+S+)*)s*$/, "$1");
}

// + ------------------- +
// |     CHECK EMAIL     |
// + ------------------- +

function echeck(str) {

                var at="@"
                var dot="."
                var lat=str.indexOf(at)
                var lstr=str.length
                var ldot=str.indexOf(dot)
                if (str.indexOf(at)==-1){
                   alert("Địa chỉ E-mail không chính xác")
                   return false
                }

                if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
                        alert("Địa chỉ E-mail không chính xác")
                   return false
                }

                if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
                    alert("Địa chỉ E-mail không chính xác")
                    return false
                }

                 if (str.indexOf(at,(lat+1))!=-1){
                    alert("Địa chỉ E-mail không chính xác")
                    return false
                 }

                 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
                    alert("Địa chỉ E-mail không chính xác")
                    return false
                 }

                 if (str.indexOf(dot,(lat+2))==-1){
                    alert("Địa chỉ E-mail không chính xác")
                    return false
                 }

                 if (str.indexOf(" ")!=-1){
                    alert("Địa chỉ E-mail không chính xác")
                    return false
                 }

                 return true

        }

// + ------------------- +
// |       CONTACT       |
// + ------------------- +

function contact_vn_handleResponse() {
        try {
                if((http.readyState == 4)&&(http.status == 200)){
                        var response = http.responseText;
                        if (response) {
                                alert(response);
                        }
                        else {
                                alert ("Cảm ơn! Chúng tôi sẽ liên lạc sớm nhất có thể!");
                                window.location.href = 'contact.php';
                        }
                }
        }
        catch(e){}
        finally{}
}

function submit_contact_vn() {
        var name = document.getElementById("name").value;
        var email = document.getElementById("email").value;
        var phone = document.getElementById("phone").value;
        var content = document.getElementById("content").value;
        var title = document.getElementById("title").value;

                if(trim(name) == "" || trim(content) == "" || trim(phone) == "" || trim(email) == "" || trim(title) == "") {
                alert("Vui lòng nhập đầy đủ thông tin ở những vùng đánh dấu sao!");
                 } else {
                        if (echeck(email)) {
                        if (confirm("Bạn có chắc những thông tin trên là đúng?")) {
                                http.open('POST',  'source/submit_contact.php');
                                http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                                http.onreadystatechange = contact_vn_handleResponse;
                                http.send('name='+name+'&email='+email+'&phone='+phone+'&title='+title+'&content='+content+'&sid='+Math.random());
                           }
                         }
                     }

}

function reset_contact_vn() {
        if (confirm("Bạn có muốn xoá hết để làm lại?")) {
                document.getElementById("name").value ='';
                document.getElementById("email").value ='';
                document.getElementById("phone").value ='';
                document.getElementById("title").value ='';
                document.getElementById("content").value ='';
        }
}

// + ------------------- +
// |       CONTACT EN    |
// + ------------------- +

function contact_en_handleResponse() {
        try {
                if((http.readyState == 4)&&(http.status == 200)){
                        var response = http.responseText;
                        if (response) {
                                alert(response);
                        }
                        else {
                                alert ("Thank you! We'll reply as soon as possible!");
                                window.location.href = 'contact.php';
                        }
                }
        }
        catch(e){}
        finally{}
}

function submit_contact_en() {
        var name = document.getElementById("name").value;
        var email = document.getElementById("email").value;
        var phone = document.getElementById("phone").value;
        var content = document.getElementById("content").value;
        var title = document.getElementById("title").value;

                if(trim(name) == "" || trim(content) == "" || trim(phone) == "" || trim(email) == "" || trim(title) == "") {
                alert("Please submit all fields marked with star!");
                 } else {
                        if (echeck(email)) {
                        if (confirm("Do you want to continue?")) {
                                http.open('POST',  'source/submit_contact.php');
                                http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                                http.onreadystatechange = contact_en_handleResponse;
                                http.send('name='+name+'&email='+email+'&phone='+phone+'&title='+title+'&content='+content+'&sid='+Math.random());
                           }
                         }
                     }

}

function reset_contact_en() {
        if (confirm("Do you want to clear all fields")) {
                document.getElementById("name").value ='';
                document.getElementById("email").value ='';
                document.getElementById("phone").value ='';
                document.getElementById("title").value ='';
                document.getElementById("content").value ='';
        }
}



// + ------------------- +
// |       CHECKOUT      |
// + ------------------- +

function checkout() {
        if (confirm("Bạn có muốn tiến hành thanh toán không?")) {
                var iNumberOrdered = GetCookie("NumberOrdered");
                if ( iNumberOrdered == null ) { iNumberOrdered = 0; }
                if ( iNumberOrdered == 0 ) {
                alert ("Bạn chưa chọn sản phẩm nào");
                } else {
                window.location.href = 'product.php?act=checkout';
                }
        }
}

// + ----------------------- +
// |      CHECKOUT EN        |
// + ----------------------- +

function checkout_en() {
        if (confirm("Do you want to checkout?")) {
                var iNumberOrdered = GetCookie("NumberOrdered");
                if ( iNumberOrdered == null ) { iNumberOrdered = 0; }
                if ( iNumberOrdered == 0 ) {
                alert ("You didn't select product");
                } else {
                window.location.href = 'product.php?act=checkout';
                }
        }
}

// + ----------------------- +
// |     RESET BOOKROOM      |
// + ----------------------- +

function cart_reset() {
        if (confirm("Bạn có chắc muốn xoá hết thông tin để điền lại không?")) {
                document.getElementById("name").value ='';
                document.getElementById("address").value ='';
                document.getElementById("city").value ='';
                document.getElementById("country").value ='';
                document.getElementById("phone").value ='';
                document.getElementById("fax").value ='';
                document.getElementById("email").value ='';
                document.getElementById("des").value ='';
        }
}

// + ----------------------- +
// |     RESET BOOKROOM EN   |
// + ----------------------- +

function cart_reset_en() {
        if (confirm("Do you want to clear all fields?")) {
                document.getElementById("name").value ='';
                document.getElementById("address").value ='';
                document.getElementById("city").value ='';
                document.getElementById("country").value ='';
                document.getElementById("phone").value ='';
                document.getElementById("fax").value ='';
                document.getElementById("email").value ='';
                document.getElementById("des").value ='';
        }
}

// + ------------------- +
// |    EMAIL AVAILABLE  |
// + ------------------- +

function check_email() {
        email = document.getElementById("email").value;
        if(     trim(email) == "")
                alert("Bạn chưa nhập E-mail");
        else {
                if (echeck(email)) {
                http.open('POST',  'source/check_email.php');
                http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                http.onreadystatechange = checkemail_handleResponse;
                http.send('email='+email+'&sid='+Math.random());
                }

        }
        return false;
}

function checkemail_handleResponse() {
        try {
                if((http.readyState == 4)&&(http.status == 200)){
                        var response = http.responseText;
                        if (response) {
                                document.getElementById("checkemail_loading").innerHTML = response;
                                document.getElementById("checkemail_loading").style.display = "block";
                        }
                }
        }
        catch(e){}
        finally{}
}

// + ------------------- +
// |    USER AVAILABLE   |
// + ------------------- +

function check_user() {
        user = document.getElementById("user_name").value;
        if(     trim(user) == "")
                alert("Bạn chưa nhập Tài khoản");
        else {
                http.open('POST',  'source/check_user.php');
                http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                http.onreadystatechange = checkuser_handleResponse;
                http.send('user='+user+'&sid='+Math.random());

        }
        return false;
}

function checkuser_handleResponse() {
        try {
                if((http.readyState == 4)&&(http.status == 200)){
                        var response = http.responseText;
                        if (response) {
                                document.getElementById("checkuser_loading").innerHTML = response;
                                document.getElementById("checkuser_loading").style.display = "block";
                        }
                }
        }
        catch(e){}
        finally{}
}

// + ------------------- +
// |      REGISTER       |
// + ------------------- +
function user_register_handleResponse() {
        try {
                if((http.readyState == 4)&&(http.status == 200)){
                        var response = http.responseText;
                        if (response) {
                                alert(response);
                        }
                        else {
                                alert ("Cảm ơn! Tài khoản của bạn đã được tạo. Vui lòng kiểm tra email để lấy mật khẩu!");
                                window.location.href = 'index.php';
                        }
                }
        }
        catch(e){}
        finally{}
}

function user_register() {
        user_name = document.getElementById("user_name").value;
        email = document.getElementById("email").value;
        email2 = document.getElementById("email2").value;
        full_name = document.getElementById("full_name").value;
        address = document.getElementById("address").value;
        phone = document.getElementById("phone").value;
        if(trim(user_name) == "" || trim(email) == "" || trim(email2) == "" || trim(full_name) == "" || trim(address) == "" || trim(phone) == "") {
                alert("Vui lòng nhập đầy đủ thông tin trong các vùng in đậm");
                return false;
        } else if (email != email2) {
                alert("Xác nhận email không chính xác");
                return false;
        } else {
                http.open('POST',  'source/register.php');
                http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                http.onreadystatechange = user_register_handleResponse;
                http.send('user_name='+user_name+'&email='+email+'&full_name='+full_name+'&address='+address+'&phone='+phone+'&sid='+Math.random());
                return false;
        }
}

// + ----------------------- +
// |      RESET REGISTER     |
// + ----------------------- +

function user_register_reset() {
        if (confirm("Bạn muốn xoá hết để làm lại?")) {
                document.getElementById("user_name").value ='';
                document.getElementById("email").value ='';
                document.getElementById("email2").value ='';
                document.getElementById("full_name").value ='';
                document.getElementById("address").value ='';
                document.getElementById("phone").value ='';
        }
        return false;
}

// + ------------------- +
// |        LOGIN        |
// + ------------------- +

function login_handleResponse() {
        try {
                if((http.readyState == 4)&&(http.status == 200)){
                        var response = http.responseText;
                        if (response) {
                                alert(response);
                        } else {
                        alert("Đăng nhập thành công!");
                        url = window.location.href;
                        url = url.replace("#","");
                        window.location.href = url;
                        }
                }
        }
        catch(e){}
        finally{}
}

function user_login() {
        var user = document.getElementById("user").value;
        var pwd = document.getElementById("pwd").value;
        if(     trim(user) == "" ||     trim(pwd) == "" || trim(user) == "Tài khoản đăng nhập" || trim(pwd) == "xxxxxx") {
                alert("Vui lòng nhập email & mật khẩu để đăng nhập");
                return false;

        } else {
                http.open('POST',  'source/login.php');
                http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                http.onreadystatechange = login_handleResponse;
                http.send('user='+user+'&pwd='+pwd+'&sid='+Math.random());
                return false;
        }
        return false;
}

// + ------------------- +
// |     UPDATE INFO     |
// + ------------------- +
function user_info_handleResponse() {
        try {
                if((http.readyState == 4)&&(http.status == 200)){
                        var response = http.responseText;
                        if (response) {
                                alert(response);
                        }
                        else {
                                alert ("Thông tin đã được cập nhật!");
                                url = window.location.href;
                                url = url.replace("#","");
                                window.location.href = url;
                        }
                }
        }
        catch(e){}
        finally{}
}

function user_info() {
        user_name = document.getElementById("user_name").value;
        email = document.getElementById("email").value;
        full_name = document.getElementById("full_name").value;
        address = document.getElementById("address").value;
        phone = document.getElementById("phone").value;
        s = document.getElementsByName("sex");
        if (s[0].checked) sex = s[0].value;
        if (s[1].checked) sex = s[1].value;
        if(trim(user_name) == "" || trim(email) == "" || trim(full_name) == "" || trim(address) == "" || trim(phone) == "") {
                alert("Vui lòng nhập đầy đủ thông tin trong các vùng in đậm");
                return false;
        } else {
                http.open('POST',  'source/update_info.php');
                http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                http.onreadystatechange = user_info_handleResponse;
                http.send('user_name='+user_name+'&email='+email+'&full_name='+full_name+'&address='+address+'&phone='+phone+'&sid='+Math.random());
                return false;
        }
        return false;
}

// + ----------------------- +
// |    RESET UPDATE INFO    |
// + ----------------------- +

function user_info_reset() {
        if (confirm("Bạn muốn xoá hết để làm lại?")) {
                document.getElementById("full_name").value ='';
                document.getElementById("address").value ='';
                document.getElementById("phone").value ='';  
        }
        return false;
}

// + ------------------- +
// |    USER UPDATE PWD  |
// + ------------------- +

function user_update_pwd() {
        var old_pwd = document.getElementById("old_pwd").value;
        var new_pwd = document.getElementById("new_pwd").value;
        var new_pwd2 = document.getElementById("new_pwd2").value;

        if (trim(old_pwd) == "" || trim(old_pwd) == "" || trim(old_pwd) == "") alert ("Vui lòng nhập đầy đủ thông tin");
        else if (old_pwd == new_pwd2) alert("Mật khẩu cũ & mới không thể giống nhau được");
        else if (new_pwd != new_pwd2) alert("Xác nhận mật khẩu không chính xác");
        else {
                var url="source/update_pwd.php";
                url=url+"?act=change_pass&old_pwd="+old_pwd+"&new_pwd="+new_pwd;
                url=url+"&sid="+Math.random();
                http.open("GET",url,true);
                http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                http.onreadystatechange = user_update_pwd_handleResponse;
                http.send(null);
        }
}

function user_update_pwd_handleResponse() {
        try {
                if((http.readyState == 4)&&(http.status == 200)){
                        var response = http.responseText;
                        if (response) {
                                alert(response);
                        } else {
                        alert ("Thay đổi mật khẩu thành công! Bạn có thể sử dụng mật khẩu mới để đăng nhập!");
                        window.location.href = 'index.php';
                        }
                }
        }
        catch(e){}
        finally{}
}

// + ----------------------- +
// |    RESET UPDATE PASS    |
// + ----------------------- +

function user_update_pwd_reset() {
        if (confirm("Bạn muốn xoá hết để làm lại?")) {
                document.getElementById("old_pwd").value ='';
                document.getElementById("new_pwd").value ='';
                document.getElementById("new_pwd2").value ='';
        }
        return false;
}

// + ------------------- +
// |        LOGOUT       |
// + ------------------- +

function logout_handleResponse() {
        try {
                if((http.readyState == 4)&&(http.status == 200)){
                        var response = http.responseText;
                        if (response) {
                        } else {
                        alert ("Đăng xuất thành công!");
                        url = window.location.href;
                        url = url.replace("#","");
                        window.location.href = url;
                        }
                }
        }
        catch(e){}
        finally{}
}

function logout() {
        if (confirm("Bạn có chắc chắn muốn thoát khỏi hệ thống không?")) {
                        var url="source/logout.php";
                        url=url+"?sid="+Math.random();
                        http.open("GET",url,true);
                        http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                        http.onreadystatechange = logout_handleResponse;
                        http.send(null);
        }
}

// + ------------------- +
// |      FORGOT PASS    |
// + ------------------- +

function forgot_pwd() {
        email = document.getElementById("email").value;
        if(     trim(email) == "") {
        alert("Bạn chưa nhập đầy đủ thông tin");
        return false;
               } else {
               if (echeck(email)) {
                http.open('POST',  'source/forgot_pwd.php');
                http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                http.onreadystatechange = forgot_pwd_handleResponse;
                http.send('act=sendkey&email='+email+'&sid='+Math.random());
                return false;
                }
        }

}

function forgot_pwd_handleResponse() {
        try {
                if((http.readyState == 4)&&(http.status == 200)){
                        var response = http.responseText;
                        if (response) {
                                alert(response);
                        } else {
                        alert ("Vui lòng kiểm tra email để lấy mật khẩu mới!");
                        window.location.href = 'index.php';
                        }
                }
        }
        catch(e){}
        finally{}
}

// + ------------------- +
// |        LOGIN        |
// + ------------------- +

function login_handleResponse() {
        try {
                if((http.readyState == 4)&&(http.status == 200)){
                        var response = http.responseText;
                        if (response) {
                                alert(response);
                        } else {
                        alert("Đăng nhập thành công!");
                        url = window.location.href;
                        url = url.replace("#","");
                        window.location.href = url;
                        }
                }
        }
        catch(e){}
        finally{}
}

function main_login() {
        var user = document.getElementById("main_user").value;
        var pwd = document.getElementById("main_pwd").value;
        if(     trim(user) == "" ||     trim(pwd) == "") {
                alert("Vui lòng nhập email & mật khẩu để đăng nhập");
                return false;

        } else {
                http.open('POST',  'source/login.php');
                http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                http.onreadystatechange = login_handleResponse;
                http.send('user='+user+'&pwd='+pwd+'&sid='+Math.random());
                return false;
        }
        return false;
}

// + ------------------- +
// |       CHECKOUT      |
// + ------------------- +

function checkout_vn() {
        if (confirm("Bạn có muốn tiến hành thanh toán không?")) {
                var iNumberOrdered = GetCookie("NumberOrdered");
                if ( iNumberOrdered == null ) { iNumberOrdered = 0; }
                if ( iNumberOrdered == 0 ) {
                alert ("Bạn chưa chọn sản phẩm nào");
                } else {
                window.location.href = 'product.php?act=checkout';
                }
        }
}

// + ------------------- +
// |       COPY DATA     |
// + ------------------- +
function copy_data() {
        if (document.getElementById("check").checked) {
                document.getElementById("d_full_name").value = document.getElementById("full_name").value;
                document.getElementById("d_address").value = document.getElementById("address").value;
                document.getElementById("d_ward").value = document.getElementById("ward").value;
                document.getElementById("d_city").selectedIndex = document.getElementById("city").selectedIndex;
        } else {
                document.getElementById("d_full_name").value = '';
                document.getElementById("d_address").value = '';
                document.getElementById("d_ward").value = '';
                document.getElementById("d_city").selectedIndex = '0';
        }
}

// + ------------------- +
// |    SEARCH PRODUCT   |
// + ------------------- +
function find_product() {
        var kw = document.getElementById("kw").value;
        var type = document.getElementById("type").value;
        var price = document.getElementById("fr_price").value;
                if (trim(kw) == "" || trim(kw) == "Mã SP hoặc từ khoá") {
                window.location.href = 'search.php?act=result&type='+type+'&price='+price;
                return false;
                } else {
                window.location.href = 'search.php?act=result&kw='+kw+'&type='+type+'&price='+price;
                return false;
                }
}

// + ----------------------- +
// |        POPUP PRO IMG    |
// + ----------------------- +

function popup(url,wdname,width,height)
{
        if (width == null)  { width  = 200; }   // default width
        if (height == null) { height = 400; }   // default height
        newwin=window.open(url,wdname,'fullscreen=no,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width='+width+',height='+height);
        if (document.all)
        {
                newwin.moveTo(0,0);
                newwin.focus();
        }
}

function view_gallery(id,width,height) {
        popup('gallery.php?act=gallery&id='+id,'gift',width,height);
}

// + ------------------- +
// |      SUBMIT POST    |
// + ------------------- +

function submit_topic_vn() {
                var title = document.getElementById("title").value;
                var short_des = document.getElementById("short_des").value;
                var content = document.getElementById("content").value;
                if(trim(title) == "" || trim(short_des) == "") {
                        alert("Vui lòng nhập các vùng có đánh dấu sao");
                        return false;
                } else {
                document.form["post_topic"].submit();
                }
}

// + ------------------------- +
// |         NEW CAPTCHA       |
// + ------------------------- +

function refreshimg() {
        http.open('POST',  'new_captcha.php');
        http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        http.onreadystatechange = refreshimg_handleResponse;
        http.send('null');
}

function refreshimg_handleResponse()
{
        if((http.readyState == 4)&&(http.status == 200)){
                var response = http.responseText;
                document.getElementById('captchaimage').innerHTML = response;
        }
}

// + ------------------- +
// |         SEARCH      |
// + ------------------- +
function find_search_en() {
        var kw = document.getElementById("kw").value;
                if (trim(kw) == "") {
                alert("Please submit keyword");
                return false;
                } else {
                window.location.href = 'search.php?act=result&kw='+kw;
                return false;
                }
}

// + ------------------- +
// |         SEARCH      |
// + ------------------- +
function find_search_vn() {
        var kw = document.getElementById("kw").value;
                if (trim(kw) == "") {
                alert("Vui lòng nhập từ khoá cần tìm");
                return false;
                } else {
                window.location.href = 'search.php?act=result&kw='+kw;
                return false;
                }
}
