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 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 (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_handleResponse;
                                http.send('name='+name+'&email='+email+'&phone='+phone+'&title='+title+'&content='+content+'&sid='+Math.random());
                           }

                     }

}

function reset_contact() {
        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 ='';
        }
}


// + ------------------- +
// |       SEARCH        |
// + ------------------- +

function do_search() {
                var kw = document.getElementById("kw").value;
                if(trim(kw) == "") {
                        alert("Vui lòng nhập từ khoá cần tìm");
                        return false;
                } else {
                document.form.submit();
                }
}

