﻿var objMoiNhat=document.getElementById('divNewest');
var objDocNhieuNhat=document.getElementById('divMostView');
var liNewest = document.getElementById('liNewest');
var liMostView = document.getElementById('liMostView');
var hrefMoiNhat = document.getElementById('hrefMoiNhat');
var hrefDocNhieuNhat = document.getElementById('hrefDocNhieuNhat');

if(objDocNhieuNhat!=null)
    objDocNhieuNhat.style.display = 'none';
if(liNewest!=null) 
    liNewest.className = "kt_xemnhieunhat_selected";
if(liMostView!=null)     
    liMostView.className = "kt_xemnhieunhat_deselected";
if(hrefMoiNhat!=null)         
    hrefMoiNhat.className = "kt_moinhat";
if(hrefDocNhieuNhat!=null)     
    hrefDocNhieuNhat.className = "kt_xemnhieunhat";

function getTab(id)
{
    if  (id==1)
    {
        hrefMoiNhat.className = "kt_moinhat";
        hrefDocNhieuNhat.className = "kt_xemnhieunhat";
        liNewest.className = "kt_xemnhieunhat_selected";
        liMostView.className = "kt_xemnhieunhat_deselected";
        if(objDocNhieuNhat!=null) 
            objDocNhieuNhat.style.display = 'none';
        if(objMoiNhat.style.display == 'none')
        { 
            objMoiNhat.style.display = '';
            return; 
        }
    }
    if (id==2)
    {
        hrefMoiNhat.className = "kt_xemnhieunhat";
        hrefDocNhieuNhat.className = "kt_moinhat";
        liNewest.className = "kt_xemnhieunhat_deselected";
        liMostView.className = "kt_xemnhieunhat_selected";
        objMoiNhat.style.display = "none";
        if(objDocNhieuNhat.style.display == "none")
        { 
            objDocNhieuNhat.style.display = "";
            return; 
        }
    }
}
function ShowFrameComment(News_ID)
{   
    var url = "/Ajax/FormComment.aspx?NewsID=" + News_ID;
    var trListComment = document.getElementById("trListComment");
    var trComment = document.getElementById("trComment");
    if (trListComment) {
        trListComment.style.display = "none";
        var ctr = document.getElementById("anchorList");
        trListComment.className = "tab_off_yk";
        ChangeImageBackground(ctr.parentNode.parentNode.parentNode.parentNode,"","off");
    }
    if (trComment){
         trComment.style.display = "";  
         var ctr = document.getElementById("anchorSend");      
         ChangeImageBackground(ctr.parentNode.parentNode.parentNode.parentNode,"re_x","on");
         trComment.className = "tab_on_yk";
    }
    var frameComment = document.getElementById("frameComment");
    if (frameComment && frameComment.src != url) frameComment.src = url;
  
}
function showListComment()
{
    var trListComment = document.getElementById("trListComment");
    var trComment = document.getElementById("trComment");
    if (trListComment)trListComment.style.display = "";   
    if (trComment) trComment.style.display = "none";   
    if (trListComment) {        
        trListComment.className = "tab_on_yk"
        var ctr = document.getElementById("anchorList");
        ChangeImageBackground(ctr.parentNode.parentNode.parentNode.parentNode,"re_x","on");
    }
    if (trComment) {      
        trComment.className = "tab_off_yk";
         var ctr = document.getElementById("anchorSend");
        ChangeImageBackground(ctr.parentNode.parentNode.parentNode.parentNode,"","off");
    }
}
function ChangeImageBackground(ctr,css, status)
{
    ctr.className = css;
    
    var imgs = ctr.getElementsByTagName("img");
    for (var i = 0 ; i < imgs.length; i++)
    {   
        if (status == 'on') {
            imgs[i].src = imgs[i].src.replace("_off_" ,"_on_");
           
        }
        else {
            imgs[i].src = imgs[i].src.replace("_on_" ,"_off_");
            
       }
    }
    if (status == "on")
    {
        ctr.className = "active_comment";        
         
    }
    else
    {
        ctr.className = "inactive_comment";
    }
}
function checkMail(entry)
{
    var x = entry.value;
    var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (filter.test(x)) {return true;}
    else {return false;}
}
function get_radio_value (radio_array)
{
  var i;
  for (i = 0; i < radio_array.length; i++)
   if (radio_array[i].checked)
    return radio_array[i].value;
  return null;
}

function Vote () 
{
    var vote = document.getElementsByName('vote');
    var id = get_radio_value(vote);
    var LeftPos=(screen.width)?(screen.width-600)/2:100;
    var TopPos=(screen.height)?(screen.height-250)/2:100;  
    
    if(id!=null)
        window.open("/VoteResult.aspx?VoteItem_ID=" + id,null,"height=250,width=600,status=no, resizable=no,scrollbar=no,toolbar=no,location=no,menubar=no,Left= "+LeftPos+",top="+TopPos);
    else
        alert("Bạn hãy lựa chọn ý kiến của mình!");
}

function ShowVoteResult () {
    var LeftPos=(screen.width)?(screen.width-600)/2:100;
    var TopPos=(screen.height)?(screen.height-250)/2:100;  
    window.open("/VoteResult.aspx",null,"height=250,width=600,status=no, resizable=no,scrollbar=no,toolbar=no,location=no,menubar=no,Left= "+LeftPos+",top="+TopPos);
}


function doClickTimTin(event)
{
    var textSearch = document.getElementById('txtSearch');
    
	var strSearch = textSearch.value;
    strSearch = strSearch.replace("?", "");
	strSearch = encodeURIComponent(strSearch)
	var url = "/home/search.htm?key=" + strSearch;
	
    var key;
    
    if(window.event)
    {
        key = window.event.keyCode;
        
        if (key == 13)
        {
            if(textSearch.value!="")
            {   
                document.location.href = url;
                event.keyCode = 0;                        
                return false;
            }
            else
            {
                alert("Bạn chưa nhập từ khóa tìm kiếm !");
                return false;
            }
        }    
    }
    else
    {
        key = event.which;    

        if (key == 13)
        {
            if(textSearch.value!="")
            {
                window.location.href = url;
                return false;
            }
            else
            {
                alert("Bạn chưa nhập từ khóa tìm kiếm !");
                
                return false;
            }
        }
    }
}

function OnSearchClick()
{
    var textSearch = document.getElementById("txtSearch");
    if(textSearch.value!="")
    {
        var strSearch = textSearch.value;
        strSearch = strSearch.replace("?", "");
        var _url = "/home/search.htm?key=" + encodeURIComponent(strSearch);                
        if(document.all) {
            window.location.href = _url;
        }
        else {
            document.location.href = _url;
        }
        return true;
    }
    else
    {
        alert("Bạn chưa nhập từ khóa tìm kiếm!");
        return false;
    }
} 
function CheckComment()
{
    var txtName = document.getElementById("txtFullName");
    var txtContent = document.getElementById("txtContents");
    var txtEmail=document.getElementById("txtEmail");
    
    if(txtName.value=="")
    {
        alert("Bạn chưa nhập họ tên !");
        txtName.focus();
        return false;
    }
    if (checkMail(txtEmail)==false)
    {
        alert("Địa chỉ mail không đúng");
        txtEmail.value="";
        txtEmail.focus();
        return false;
    }   
    if(txtContent.value=="")
    {
        alert("Bạn chưa nhập nội dung !");
        txtContent.focus();
        return false;
    }
   return true;
}
function OpenWindowSendMail()
{
    var LeftPos=(screen.width)?(screen.width-600)/2:100;
    var TopPos=(screen.height)?(screen.height-500)/2:100; 
    window.open("/Ajax/News/SentMail.aspx?link="+location.href ,"","left=" + LeftPos + ",top=" + TopPos + ",width=" + 510 + ",height=" + 300 + " ");
}
function PageInfo(pageCount, divPage,loadFunction,curPage)
{
    this.PageCount = Math.floor(pageCount);
    this.CurrentPage = 1;
    this.divPage = divPage;
    this.loadFunction = loadFunction;
    this.hidCurrent = curPage;
}

function ResetPage(hidCoutNews,divPage,loadFunction,curPage)
{   
    var hidCount=document.getElementById(hidCoutNews);
    var count;
    if (hidCount) {count = hidCount.value;}  
    var hidCur = document.getElementById(curPage);
    if (hidCur.value == '') hidCur.value = 1;    
    return new PageInfo(count,divPage,loadFunction,curPage);     
}

function BuildPaggingAjax(page,index)
{   
    s = ""; 
    page.CurrentPage = index;
    var div = document.getElementById(page.divPage);
    div.style.display = (page.PageCount <= 1) ? "none" : "";	
    div.innerHTML = "";    
    if (1 != page.CurrentPage)
	    s += CreateLinkAjax(div,1,"Đầu",page.loadFunction,page.CurrentPage);
	
    if (page.PageCount <= 4)
    {   
	    for (var i = 1; i <= page.PageCount ; i++) 
	    {  
		    s += CreateLinkAjax(div,i,i,page.loadFunction,page.CurrentPage); 
	    }
    }  
    else 
    {		  
        if (page.CurrentPage == 1)
        {
            for (var i = 1 ; i <= 5; i++)
		    {				
			    s += CreateLinkAjax(div,i,i,page.loadFunction,page.CurrentPage);
		    }
        }
        
        if (page.CurrentPage > 1 && page.CurrentPage < page.PageCount)
        {
            if (page.CurrentPage > 2)
            {
               s +=  CreateLinkAjax(div,page.CurrentPage - 2,"Trước",page.loadFunction,page.CurrentPage);
            }
            
            for (var i = (page.CurrentPage - 1); i <= (page.CurrentPage + 1); i++)
            {                        
                s += CreateLinkAjax(div,i,i,page.loadFunction,page.CurrentPage)
            }
            
            if (page.CurrentPage <= page.PageCount - 2)
            {
                s += CreateLinkAjax(div,page.CurrentPage + 2,"Sau",page.loadFunction,page.CurrentPage);
            }
        }
        if (page.CurrentPage == page.PageCount)
        {
    	    s += CreateLinkAjax(div,page.CurrentPage - 3,page.CurrentPage - 3,page.loadFunction,page.CurrentPage);

            for (var i = page.PageCount - 2 ; i <= page.PageCount; i++)
		    {				
			    s += CreateLinkAjax(div,i,i,page.loadFunction,page.CurrentPage);
		    }
        }

    }
    if (page.PageCount != page.CurrentPage)
	    s += CreateLinkAjax(div,page.PageCount,"Cuối",page.loadFunction,page.CurrentPage);	
	div.innerHTML = s;
}

function CreateLinkAjax(div,i,text,loadFunction,currpage)
{	
    var url = location.href.replace(".htm","");
    var arr = url.split('/');
  
    var s = "";
    if (i != currpage)
        s = "<span><a class=\"topmenukinhtehoptacs\" href=\"#\" value=\"" + i + "\" onclick=\"" + loadFunction + "(" + i + ")\"><b>" + text + "</b></a></span> &nbsp;";
    else
        s = "<span><span>" + text + "</span></span> &nbsp;";        

    return s;
}

