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");
}


// + ------------------- +
// |      REGISTER       |
// + ------------------- +

function reg_handleResponse() {
        try {
                if((http.readyState == 4)&&(http.status == 200)){
                        var response = http.responseText;
                        if (response) {
                                document.getElementById("reg_loading").innerHTML = response;
                                document.getElementById("reg_loading").style.display = "block";
                        }
                        else {
                                alert ("Đăng kí thành công! Chúng tôi sẽ liên lạc trong thời gian sớm nhất!");
                                window.location.href = 'register.php';
                        }
                }
        }
        catch(e){}
        finally{}
}

function submit_register() {
        var contact_name = document.getElementById("contact_name").value;
        var company = document.getElementById("company").value;
        var email = document.getElementById("email").value;
        var address = document.getElementById("address").value;
        var phone = document.getElementById("phone").value;
        var fax = document.getElementById("fax").value;
        var product = document.getElementById("product").value;
        var content = document.getElementById("content").value;

                if(trim(contact_name) == "" || trim(email) == "" || trim(phone) == "" || trim(product) == "" || trim(content) == "") {
                alert("Vui lòng nhập đầy đủ thông tin ở những vùng được đánh dấu sao");
                 } else {
                        if (confirm("Bạn có chắc những thông tin trên là chính xác chưa?")) {
                                http.open('POST',  'submit_app.php');
                                http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                                http.onreadystatechange = reg_handleResponse;
                                http.send('contact_name='+contact_name+'&company='+company+'&email='+email+'&address='+address+'&phone='+phone+'&fax='+fax+'&product='+product+'&content='+content+'&sid='+Math.random());
                           }

                     }

}

function reset() {
        if (confirm("Bạn có chắc muốn xoá hết thông tin để điền lại không?")) {
                document.getElementById("contact_name").value ='';
                document.getElementById("company").value ='';
                document.getElementById("email").value ='';
                document.getElementById("address").value ='';
                document.getElementById("phone").value ='';
                document.getElementById("fax").value ='';
                document.getElementById("product").value ='';
                document.getElementById("content").value ='';
        }
}

function sndReq(vote,id_num,ip_num,units) {
        var theUL = document.getElementById('unit_ul'+id_num); // the UL
        http.open('get', 'rpc.php?j='+vote+'&q='+id_num+'&t='+ip_num+'&c='+units);
        http.onreadystatechange = ratting_handleResponse;
        http.send(null);
}

function ratting_handleResponse() {
  if(http.readyState == 4){
                if (http.status == 200){

        var response = http.responseText;
        var update = new Array();

        if(response.indexOf('|') != -1) {
            update = response.split('|');
            changeText(update[0], update[1]);
        }
                }
    }
}

function changeText( div2show, text ) {
    // Detect Browser
    var IE = (document.all) ? 1 : 0;
    var DOM = 0;
    if (parseInt(navigator.appVersion) >=5) {DOM=1};

    // Grab the content from the requested "div" and show it in the "container"
    if (DOM) {
        var viewer = document.getElementById(div2show);
        viewer.innerHTML = text;
    }  else if(IE) {
        document.all[div2show].innerHTML = text;
    }
}

// + ------------------- +
// |      CONTACT        |
// + ------------------- +

function contact_handleResponse() {
        try {
                if((http.readyState == 4)&&(http.status == 200)){
                        var response = http.responseText;
                        if (response) {
                                alert(response);
                        }
                        else {
                                alert ("Đã nhận thông tin của quý khách! Chúng tôi sẽ liên lạc trong thời gian sớm nhất!");
                                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(email) == "" || trim(phone) == "" || trim(content) == "") {
                alert("Vui lòng nhập đầy đủ thông tin ở những vùng được đánh dấu sao");
                 } else {
                        if (confirm("Bạn có chắc những thông tin trên là chính xác chưa?")) {
                                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ó chắc muốn xoá hết thông tin để điền lại không?")) {
                document.getElementById("contact_name").value ='';
                document.getElementById("name_com").value ='';
                document.getElementById("email").value ='';
                document.getElementById("address").value ='';
                document.getElementById("phone").value ='';
                document.getElementById("content").value ='';
        }
}

function close_announce () {
        document.getElementById("announcement").style.display = "none";
}

function dosearch() {
        var kw = document.getElementById("text_search").value;
        if (trim(kw) == "") {
        alert ("Vui lòng nhập chuỗi kí tự cần tìm");
        return false;
        } else {
        window.location.href = 'search.php?act=result&kw='+kw;
        return false;
        }
}
function trim(a) {
        return a.replace(/^s*(S*(s+S+)*)s*$/, "$1");
}

var dayarray=new Array("Chủ nhật","Thứ 2","Thứ 3","Thứ 4","Thứ 5","Thứ 6","Thứ 7")
var montharray=new Array("01","02","03","04","05","06","07","08","09","10","11","12")

function getthedate(){
var mydate=new Date()
var year=mydate.getYear()
if (year < 1000)
year+=1900
var day=mydate.getDay()
var month=mydate.getMonth()
var daym=mydate.getDate()
if (daym<10)
daym="0"+daym
var hours=mydate.getHours()
var minutes=mydate.getMinutes()
var seconds=mydate.getSeconds()
var dn="AM"
if (hours>=12)
dn="PM"
if (hours>12){
hours=hours-12
}
if (hours==0)
hours=12
if (minutes<=9)
minutes="0"+minutes
if (seconds<=9)
seconds="0"+seconds
//change font size here
var cdate="<small><font color='000000' face='Arial'><b>"+dayarray[day]+", "+daym+"/"+montharray[month]+"/"+year+" "+hours+":"+minutes+":"+seconds+" "+dn
+"</b></font></small>"
if (document.all)
document.all.clock.innerHTML=cdate
else if (document.getElementById)
document.getElementById("clock").innerHTML=cdate
else
document.write(cdate)
}
if (!document.all&&!document.getElementById)
getthedate()
function goforit(){
if (document.all||document.getElementById)
setInterval("getthedate()",1000)
}


