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");
}

// + ------------------- +
// |      CONTACT        |
// + ------------------- +

function contact_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() {
        var contact_name = document.getElementById("contact_name").value;
        var email = document.getElementById("email").value;
        var phone = document.getElementById("phone").value;
        var content = document.getElementById("content").value;

                if(trim(contact_name) == "" || trim(content) == "") {
                alert("Vui lòng nhập đầy đủ thông tin ở những vùng đánh dấu sao!");
                 } else {
                        if (confirm("Bạn có chắc những thông tin trên là đúng?")) {
                                http.open('POST',  'submit_contact.php');
                                http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                                http.onreadystatechange = contact_handleResponse;
                                http.send('contact_name='+contact_name+'&email='+email+'&phone='+phone+'&content='+content+'&sid='+Math.random());
                           }

                     }

}

function reset_contact() {
        if (confirm("Bạn có muốn xoá hết để làm lại?")) {
                document.getElementById("contact_name").value ='';
                document.getElementById("email").value ='';
                document.getElementById("phone").value ='';
                document.getElementById("content").value ='';
        }
}

function checkout() {
        if (confirm("Bạn có muốn thanh toán đơn hàng không?")) {
                window.location.href = 'product.php?act=checkout';
        }
}

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 ='';
        }
}

function search() {
        var kw = document.getElementById("kw").value;
        window.location.href = 'search.php?kw='+kw;
}

var t_id = setInterval(animate,20);
var pos=0;
var dir=2;
var len=0;

function animate()
{
var elem = document.getElementById("progress");
if(elem != null) {
if (pos==0) len += dir;
if (len>32 || pos>79) pos += dir;
if (pos>79) len -= dir;
if (pos>79 && len==0) pos=0;
elem.style.left = pos;
elem.style.width = len;
}
}

function remove_loading() {
this.clearInterval(t_id);
var targelem = document.getElementById("loader_container");
targelem.style.display='none';
targelem.style.visibility='hidden';
var t_id = setInterval(animate,60);
}
