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 ='';
        }
}

// + ----------------------- +
// |        CHANGE IMG       |
// + ----------------------- +

function changeIt(imageName,objName)
{
        var obj = document.getElementById(objName);

        var imgTag = "<img src='"+imageName+"' border='0' />";

        obj.innerHTML = imgTag;

        return;
}

// + ------------------- +
// |       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';
                }
        }
}

// + ----------------------- +
// |        CART RESET       |
// + ----------------------- +

function cart_reset_vn() {
        if (confirm("Bạn có muốn xoá hết để làm lại?")) {
                document.getElementById("full_name").value ='';
                document.getElementById("address").value ='';
                document.getElementById("phone").value ='';
                document.getElementById("mobile").value ='';
                document.getElementById("email").value ='';
                document.getElementById("des").value ='';
                document.getElementById("d_full_name").value ='';
                document.getElementById("d_address").value ='';
                document.getElementById("d_date").value ='';
                document.getElementById("fr_time").selectedIndex = '0';
                document.getElementById("to_time").selectedIndex = '0';
                document.getElementById("day").selectedIndex = '0';
                document.getElementById("month").selectedIndex = '0';
                document.getElementById("year").selectedIndex = '0';
        }
}

// + ------------------- +
// |       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;
        } else {
                document.getElementById("d_full_name").value = '';
                document.getElementById("d_address").value = '';
        }
}


// + ------------------- +
// |    SEARCH PRODUCT   |
// + ------------------- +
function find_product() {
        var kw = document.getElementById("kw").value;
                if (trim(kw) == "" || trim(kw) == "Mã SP hoặc từ khoá") {
                alert("Vui lòng nhập từ khoá cần tìm");
                return false;
                } else {
                window.location.href = 'search.php?kw='+kw;
                }
}
