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 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 ='';
        }
}

function search() {
        var kw = document.getElementById("kw").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;
        }
}

// + ------------------- +
// |   REFRESH CAPTCHA   |
// + ------------------- +

function refreshimg() {
        http.open('POST',  '/source/refresh_captcha.php');
        http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        http.onreadystatechange = refreshimg_handleResponse;
        http.send('sid='+Math.random());
}

function refreshimg_handleResponse() {
        try {
                if((http.readyState == 4)&&(http.status == 200)){
                        var response = http.responseText;
                        if (response) {
                                document.getElementById("captcha_image").innerHTML = response;
                                document.getElementById("captcha_image").style.display = "block";
                        }
                }
        }
        catch(e){}
        finally{}
}

// + ------------------- +
// |     SUBMIT COMMENT  |
// + ------------------- +

function submit_comment() {
        var type = document.getElementById("type").value;
        var post_id = document.getElementById("post_id").value;
        var name = document.getElementById("name").value;
        var email = document.getElementById("email").value;
        var content = document.getElementById("content").value;
        var title = document.getElementById("title").value;
        var captcha = document.getElementById("captcha").value;  
        if(trim(name) == "" || trim(content) == "" || trim(email) == "" || trim(title) == "")
                alert("Vui lòng nhập đầy đủ thông tin!");
        else if(trim(captcha) == "")
                alert("Vui lòng nhập mã bảo vệ");
        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_comment.php');
                http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                http.onreadystatechange = submit_comment_handleResponse;
                http.send('type='+type+'&post_id='+post_id+'&name='+name+'&email='+email+'&title='+title+'&content='+content+'&captcha='+captcha+'&sid='+Math.random());
                }
                }
        }
}

function submit_comment_handleResponse() {
        try {
                if((http.readyState == 4)&&(http.status == 200)){
                        var response = http.responseText;
                        if (response) {
                                alert(response);
                        } else {
                                alert("Cảm ơn bạn đã gửi comment. Tất cả dữ liệu sẽ được kiểm tra trước khi được hiển thị!");
                                url = window.location.href;
                                url = url.replace("#","");
                                window.location.href = url;
                        }
                }
        }
        catch(e){}
        finally{}
}

// + ------------------- +
// |   COMMENT DETAIL    |
// + ------------------- +

function comment_detail(obj,field) {
        var hideshow = document.getElementById(obj);
                if(hideshow.style.display != "none") {
                        hideshow.style.display = "none";
                        document.getElementById(field).innerHTML = '<img src=\"/templates/img/down.png\" border=\"0\">';
                }
                else {
                        hideshow.style.display = "block";
                        document.getElementById(field).innerHTML = '<img src=\"/templates/img/up.png\" border=\"0\">';
                }
        return false;
}

// + ------------------- +
// |     GO TO PAGE      |
// + ------------------- +

function go_to_page(type,post_id,pg) {
        http.open('POST',  'go_page.php');
        http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        http.onreadystatechange = go_to_page_handleResponse;
        http.send('type='+type+'&post_id='+post_id+'&pg='+pg+'&sid='+Math.random());
}

function go_to_page_handleResponse() {
        try {
                if((http.readyState == 4)&&(http.status == 200)){
                        var response = http.responseText;
                        if (response) {
                                document.getElementById("comment_post").innerHTML = response;
                                document.getElementById("comment_post").style.display = "block";
                        }
                }
        }
        catch(e){}
        finally{}
}

// + ------------------- +
// |     POOL SUBMIT     |
// + ------------------- +
function submit_vote(id) {
        var tt_opt = document.getElementsByName(id).length;
        var opt = document.getElementsByName(id);
        var c= "";
        for(var i = 0; i < tt_opt; i++) {
                if(opt[i].checked) {
                        var c = opt[i].value;
                }
        }
        if (trim(c) == "") {
        alert ("Vui lòng chọn câu trả lời trước khi bình chọn!");
        } else {
                        http.open('POST',  '/source/submit_vote.php');
                        http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                        http.onreadystatechange = voted_handleResponse;
                        http.send('pool_id='+id+'&opt_id='+c+'&sid='+Math.random());
        }

}

function voted_handleResponse() {
        try {
                if((http.readyState == 4)&&(http.status == 200)){
                        var response = http.responseText;
                        if (response) {
                                alert(response);
                        }
                        else {
                                alert ("Đã nhận ý kiến của bạn! Cảm ơn!");
                                window.location.href = '/';
                        }
                }
        }
        catch(e){}
        finally{}
}

// + ------------------- +
// |       SEARCH        |
// + ------------------- +

function search() {
        var kw = document.getElementById("kw").value;
        if (trim(kw) != "") {
                window.location.href = '/search/simple/'+kw+'.html';
                return false;
        } else {
                alert("Vui lòng nhập từ khoá cần tìm");
                return false;
        }
}

// + ------------------- +
// |   ADVANCE SEARCH    |
// + ------------------- +

function advance_search() {
        var kw_adv = document.getElementById("kw_adv").value;
        var type = document.getElementById("type").value;
        if (trim(kw_adv) != "" || trim(kw_adv) == "Nhập từ khoá cần tìm") {
                window.location.href = '/search/advance/'+type+'/'+kw_adv+'.html';
                return false;
        } else {
                alert("Vui lòng nhập từ khoá cần tìm");
                return false;
        }
}

// + ------------------- +
// |      BOOKMARK       |
// + ------------------- +

function bookmark(url,title){
  if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4)) {
  window.external.AddFavorite(url,title);
  } else if (navigator.appName == "Netscape") {
    window.sidebar.addPanel(title,url,"");
  } else {
    alert("Press CTRL-D (Netscape) or CTRL-T (Opera) to bookmark");
  }
}

// + ------------------- +
// |  SEND LINK VIA Y!M  |
// + ------------------- +
function send_yahoo(content){
        var name=prompt("Vui lòng nhập nick Y!M người nhận","Nhập nick ở đây");
        if (trim(name) == "" || trim(name) == "Nhập nick ở đây") {
                alert("Bạn chưa nhập nick người nhận, vui lòng thử lại");
        } else {
                window.location = 'ymsgr:sendIM?'+name+'&m='+content+':  '+ window.top.location +'';
        }
}

// + ------------------- +
// |   INSERT EMOTION    |
// + ------------------- +
function AddText(NewCode) {
        document.getElementById('content').value+=NewCode
        document.getElementById('content').focus();
}

function insertemot (emotcode) {
        var emot="[emot]"+emotcode+"[/emot]";
        AddText(emot);  
}
