﻿
function SetFocusMenu(id, css)
{
    var obj = document.getElementById(id);
    if (obj) obj.className = id;
    var obj1 = document.getElementById('f' + id);    
    if (obj1) obj1.className = id;
}

var DayInMilliseconds = 24 * 60 * 60 * 1000;
var isSelectedDay = false;
var TodaysDate = new Date();
var SelectedDate = TodaysDate;
var OriginalDate = TodaysDate;
var ColumnDayMapping = Array( 1, 2, 3, 4, 5, 6, 0 ); 
var previousYear = SelectedDate.getFullYear();		

 function ShowCalendar(pYear, pMonth, pMonthDay)
 {
 	
 	var obj1 = document.getElementById("pnText");
 	if (obj1) obj1.innerHTML = "Tháng " + pMonth +"/" +pYear;
 	if (!isSelectedDay)
 	{
 	    var objNgay =document.getElementById("lblNgay");
 	    if (objNgay) objNgay.innerHTML = pMonthDay + "/" + pMonth + "/" + pYear;		 	    
 	}
 	isSelectedDay = false;
 	pYear  = parseInt( pYear );  pYear = (isNaN( pYear ) || pYear < 1599 || pYear > 2999) ? TodaysDate.getFullYear() : pYear;
 	pMonth = parseInt( pMonth ); pMonth = isNaN( pMonth ) ? (TodaysDate.getMonth() + 1) : pMonth;
 	pMonthDay = parseInt( pMonthDay ); pMonthDay = isNaN( pMonthDay ) ? TodaysDate.getDate() : pMonthDay;
    
	previousYear = pYear;

 	SelectedDate = new Date();
	SelectedDate.setFullYear( pYear, pMonth - 1, pMonthDay );
	var FirstDayOfMonth = new Date(), FirstDayOfNextMonth = new Date(), LastDayOfMonth = new Date();
 	FirstDayOfMonth.setFullYear( pYear, pMonth - 1, 1 );

 	if(pMonth < 12) FirstDayOfNextMonth.setFullYear( pYear, pMonth, 1 );
 	else FirstDayOfNextMonth.setFullYear( pYear + 1, 0, 1 );

 	LastDayOfMonth.setTime( FirstDayOfNextMonth.getTime() - DayInMilliseconds );

 	var dayNo = 0;
 	for( var r = 0; r < 6; r++ )
 	{
 		for(var c = 0; c < 7; c++)
 		{
 			var td = document.getElementById("GridTD_" + r + "_" + c);
 			td.className = ""; 			
 			td.innerHTML = "&nbsp;";			
			td.style.backgroundColor = "";			
			td.style.color = "black";
			td.style.fontWeight = "normal";
 			if( dayNo == 0 )
 				if ( FirstDayOfMonth.getDay() == ColumnDayMapping[c] )
 					dayNo = 1;

 			if( dayNo > 0 && dayNo <= LastDayOfMonth.getDate() )
 			{
 				td.innerHTML = "<a href='javascript:void(0);' onclick='javascript:ShowCalendar("+pYear+","+pMonth+","+dayNo+")'>"+ dayNo + "</a>";

 				if( (TodaysDate.getFullYear() == pYear) && (TodaysDate.getMonth() == pMonth - 1) && (TodaysDate.getDate() == dayNo) )
 				{
					td.style.fontWeight = "bold";
 				}
                
 				if( pMonthDay == dayNo )
 				{
					td.className = "selecteddate"
 				}
 						 				
 				dayNo++;
 			}
 		}
 	}
 	AjaxRequestURL("idContent","/Pages/SinhNhatSao.aspx?day=" + pMonth+'/'+ pMonthDay+ '/' + pYear );
 }


 function formatDate( dateObject )
 {
 	var pMonth = dateObject.getMonth() + 1;
 	var pMonthDay = dateObject.getDate();
 	var pYear = dateObject.getFullYear();

 	
 	var disp_Month = (pMonth < 10) ? "0" + pMonth : pMonth;
 	var disp_Day = (pMonthDay  < 10) ? "0" + pMonthDay  : pMonthDay;
 	document.frmDateSelection.month.selectedIndex = pMonth - 1;
 	document.frmDateSelection.year.value = pYear;
 	return disp_Day + "/" + disp_Month + "/" + pYear;
 }


 function MonthOrYearChanged(i)
 {		    
    isSelectedDay = true;
    var month = document.getElementById('hdMonth').value ;		    
    var year = document.getElementById('hdYear').value;
    month = parseInt(month) + parseInt(i);		    
    if (month < 1 ) 
    {
        month = 12;
        year = parseInt(year) - 1
    }
    else if (month > 12 ) 
    {
        month = 1;
        year = parseInt(year) + 1
    }
    document.getElementById('hdMonth').value = month;
    document.getElementById('hdYear').value = year;
	ShowCalendar(year , month , -1 );
 }

function getDateObject( dateString )
{
	var arr = Array();
	var valid = false;
	var day = -1, month = -1, year = -1;

	
	if( dateString.indexOf("/") > 0 )
	{
		valid = true;
		arr   = dateString.split("/");
		day = parseInt(arr[0],10);
		if(arr.length > 0) month = parseInt(arr[1],10);
		if(arr.length > 1) year  = parseInt(arr[2],10);
	}
	
	else if( dateString.indexOf("-") > 0 )
	{
		valid = true;
		arr   = dateString.split("-");
		day = parseInt(arr[2],10);
		if(arr.length > 0) month = parseInt(arr[1],10);
		if(arr.length > 1) year  = parseInt(arr[0],10);
	}

	if(! valid ) return TodaysDate;

	day = parseInt(arr[0],10);
	if(arr.length > 0) month = parseInt(arr[1],10);
	if(arr.length > 1) year  = parseInt(arr[2],10);

	var theDate = new Date();

	theDate.setFullYear( year, month - 1, day );
	
	return theDate;
}


function Initialize( fromDateObj )
{
	previousYear = SelectedDate.getFullYear();
	
				    
		var validSelected = false;
		try
		{					
			if( typeof(SelectedDate) == "object" )
			{
				validSelected = true;
			}
			else
			{
				SelectedDate = getDateObject( SelectedDate );
				validSelected = true;
			}
		}
		catch( ex )
		{				    
			validSelected = false;
		}
		if( ! validSelected ) SelectedDate = TodaysDate;				
		OriginalDate = SelectedDate;										
	ShowCalendar( SelectedDate.getFullYear(), SelectedDate.getMonth() + 1, SelectedDate.getDate() );						
}	


function ShowAvatar()
{
    var divavatar = document.getElementById("divavatar");
    if (divavatar)
    {
        divavatar.style.display = "";    
        var str = '';
        for (var i = 0 ; i <=20; i++)
        {
            str += '<img onclick="ChoseAvatar(this)" style="cursor: pointer; width:50px;" src="http://img.2sao.vietnamnet.vn/Images/Avatar/' + (i < 10 ? '0' + i : i) + '.jpg" />';
        }   
        document.getElementById("divIcon").innerHTML = str;
    }
}

function ChoseAvatar(ctr)
{
    var hiddenAvatar = eval("hidAvatar");
    hiddenAvatar.value = ctr.src;
    document.getElementById("imgAvatar").src = ctr.src;
}




function doClickTimTin(event, id, isclick)
{
    var textSearch = document.getElementById(id);       
      
    var _url = "/Pages/Search.aspx?key=";
    
    if (isclick)
    {
        var strSearch = textSearch.value;
        strSearch = encodeURIComponent(strSearch);  
        if(document.all) 
            {
                window.location.href = _url + escape(strSearch);
            }
            else 
            {
                document.location.href = _url + escape(strSearch);
            }
    }
    
    var key;    
    if(window.event)
    {
        key = window.event.keyCode;
        if (key == 13)
        {
            
            var strSearch = textSearch.value;
            strSearch = encodeURIComponent(strSearch);  
            if(document.all) 
            {
                window.location.href = _url + escape(strSearch);
            }
            else 
            {
                document.location.href = _url + escape(strSearch);
            }
            return false;
         }
     }
     else
     {
        key = event.which;
        if (key == 13)
        {
             var strSearch = textSearch.value;
            strSearch = encodeURIComponent(strSearch);  
            if(document.all) 
            {
                window.location.href = _url + escape(strSearch);
            }
            else 
            {
                document.location.href = _url + escape(strSearch);
            }
            return false;
        }
     }
 }
 
 function AdvanceSearch(event, id, isclick)
{
    var textSearch = $ID(id);  
    var catclient = $ID("hidCategory");     
    var fdate = $ID('fdate') != null ? $ID('fdate').value : "";
    var edate = $ID('edate') != null ? $ID('edate').value : "";
    var sort = $ID('ddlsort') != null ? $ID('ddlsort').value : 0;
    var cat = 0;
    if (catclient != null && $ID(catclient.value)) cat = $ID(catclient.value).value;
    var type = $ID('type') != null ? ($ID('type').checked ? 1 : 0) : 0 ;
    var rows = $ID('rows') != null ? $ID('rows').value : 100;   
    var _url = "/Pages/QASearch.aspx" + 
               "?rows="  + rows +
               "&type="  + type +
               "&cat="  + cat +
               "&sort="  + sort +
               "&fdate="  + fdate +
               "&edate="  + edate +
               "&key=";
               
           var strSearch = textSearch.value;    
           if (strSearch == "Tìm kiếm trên mục Hỏi đáp") {strSearch = ""};
    if (isclick)
    {
        
        strSearch = encodeURIComponent(strSearch);          
        if(document.all) 
            {
                window.location.href = _url + escape(strSearch);
            }
            else 
            {
                document.location.href = _url + escape(strSearch);
            }
    }
    
    var key;    
    if(window.event)
    {
        key = window.event.keyCode;
        if (key == 13)
        {
            
            strSearch = encodeURIComponent(strSearch);  
            if(document.all) 
            {
                window.location.href = _url + escape(strSearch);
            }
            else 
            {
                document.location.href = _url + escape(strSearch);
            }
            return false;
         }
     }
     else
     {
        key = event.which;
        if (key == 13)
        {
             var strSearch = textSearch.value;
            strSearch = encodeURIComponent(strSearch);  
            if(document.all) 
            {
                window.location.href = _url + escape(strSearch);
            }
            else 
            {
                document.location.href = _url + escape(strSearch);
            }
            return false;
        }
     }
     return true;
 }

function ClearThacmac(obj)
{
    var isfirst = obj.getAttribute("isfirst");   
    if (!isfirst){
        obj.value = "";
        obj.setAttribute("isfirst","true");
    }
    
         
}

function LoadImage(id, src) 
{
    id.src = src;
    id.onerror = null;
}

function AjaxRequestURL(div,url)
{var obj = document.getElementById(div);if (obj)
{obj.innerHTML = "<img src='/images/loading.gif'/>Đang tải dữ liệu..."} var xmlHttp;try{if (!document.all){xmlHttp=new XMLHttpRequest()}else{try{xmlHttp=new ActiveXObject("Msxml2.XMLHTTP")}catch (e){xmlHttp=new ActiveXObject("Microsoft.XMLHTTP")}}}catch (e){alert("Your browser does not support AJAX!");return false};xmlHttp.onreadystatechange=function(){if(xmlHttp.readyState==4)
{
    if (obj) 
        obj.innerHTML =xmlHttp.responseText
}
};xmlHttp.open("GET",url,true);xmlHttp.send(null)}

function GetOptionValue(id)
{
    var obj = document.getElementById(id)
    return obj.options[obj.selectedIndex].value;
}
function ViewByDate(ngay, thang, nam)
{
    var n = GetOptionValue(ngay);
    var t = GetOptionValue(thang);
    var nam = GetOptionValue(nam);
    var hidUsing = document.getElementById("hidUsing")
    var prefix = hidUsing != null ? document.getElementById("hidUsing").value : "";
    if (prefix != "")
    {
        location.href = prefix.replace('$', n + '-' + t + '-' + nam);
    }
    
}

function $ID(id)
{
    return document.getElementById(id);
}

function CommentSend(name1, mail1, type1, content1)
{
    var name = $ID(name1).value;
    var mail = $ID(mail1).value;
    var type = $ID(type1).value;
    var content = $ID(content1).value;    
    var img = document.getElementById('imgAjax');
    if (name == "") {alert("Bạn chưa nhập tên"); $ID(name1).focus(); return;}
    img.src = "/Pages/QAForm/QnA.aspx?name=" + encodeURIComponent(name) +
              "&mail=" +  mail +
              "&type=" + type +
              "&content=" + encodeURIComponent(content);
    $ID(name1).value = "";
    $ID(mail1).value = "";
    $ID(content1).value = "";
    alert("Gửi hoặc đăng tin đồn thành công");
} 

function GuiBinhLuan(name1, mail1,content1)
{
    if (Get_Cookie('lancuoi') == 1) {alert('Bạn hãy chờ 30 giây nữa để được gửi tiếp bình luận'); return;}
    var name = $ID(name1).value;
    var mail = $ID(mail1).value;    
    var content = $ID(content1).value;
    var newsid =  $ID("hidNewsID").value;
    var avatar =  $ID("imgAvatar").src;    
    var img = new Image();
    if (name == "") {alert("Bạn chưa nhập tên"); $ID(name1).focus(); return;}
    img.src = "/Pages/QAForm/Comment.aspx?name=" + encodeURIComponent(name) +
              "&mail=" +  encodeURIComponent(mail) +              
              "&newsid=" +  newsid +    
              "&avatar=" +  escape(avatar) +    
              "&content=" + encodeURIComponent(content);
    $ID(name1).value = "";
    $ID(mail1).value = "";
    $ID(content1).value = "";
    Set_Cookie('lancuoi',1, 30);
    alert("Bạn đã gửi bình luận thành công");
} 




function setHomepage() 
 { 
	if (document.all) 
     { 
         document.body.style.behavior='url(#default#homepage)'; 
		document.body.setHomePage('http://2sao.net'); 
   
     }     
 } 
 
 
function Get_Cookie( name ) {

var start = document.cookie.indexOf( name + "=" );
var len = start + name.length + 1;
if ( ( !start ) &&
( name != document.cookie.substring( 0, name.length ) ) )
{
return null;
}
if ( start == -1 ) return null;
var end = document.cookie.indexOf( ";", len );
if ( end == -1 ) end = document.cookie.length;
return unescape( document.cookie.substring( len, end ) );
}

function Set_Cookie( name, value, expires)
{
var today = new Date();
today.setTime( today.getTime() );
if ( expires )
{
    expires = expires * 1000;
}
var expires_date = new Date( today.getTime() + (expires) );
document.cookie = name + "=" +escape( value ) + ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) ;
}

function BuildContent(p, p_2, p_3, p_4, p_5)
        {
            return "<style>\nbody{background: #" + p + " url(http://static.2sao.vn/design/home/" + p_2 + "/" + p_2 +
                    "-background2.jpg) no-repeat scroll center top;}\n.title_home,.title_home:active{ color:#" + p_3 + "}\n .title_home:hover { color:#" + p_4 + "  !important;}\n.content, #subcontent{background: transparent url(http://static.2sao.vn/design/home/" + p_2 + "/" + p_2 + "-bg2.gif) repeat-y left top;}\n.header300 {background:url(http://static.2sao.vn/design/home/" + p_2 +"/0"+ p_2 +
                    ".gif) no-repeat scroll left top;}\n.xemtiep300 a span {background-color:#" + p_5 +
                        ";}\n.box_hoidap_title {background:url(http://static.2sao.vn/design/home/0" + p_2 +
                    ".gif) no-repeat scroll left -135px;}\n</style>";
        }
function RandomTheme(){
        
             
            var arrayStyle = new Array(      
            BuildContent("1b1b1b","01","6f6f6f","a70244","bebebe"),  
            BuildContent("95dbd2","33","111111","976427","5f9c94"),	                        
			BuildContent("111111","24","111111","c81856","3b1244"),	
			BuildContent("95dbd2","33","111111","976427","5f9c94"),			  			
			BuildContent("1b1b1b","01","6f6f6f","a70244","bebebe"),	
			BuildContent("95dbd2","33","111111","976427","5f9c94"),				  		
            BuildContent("2b110e","03","683a26","7f9802","b0763d"),
            BuildContent("95dbd2","33","111111","976427","5f9c94"),	
			BuildContent("1b1b1b","01","6f6f6f","a70244","bebebe"),
			BuildContent("95dbd2","33","111111","976427","5f9c94"),	
            BuildContent("726461","05","634c53","91a078","726461"),
            BuildContent("95dbd2","33","111111","976427","5f9c94"),	
            BuildContent("1b1b1b","01","6f6f6f","a70244","bebebe"),
            BuildContent("95dbd2","33","111111","976427","5f9c94"),	
			BuildContent("111111","24","111111","c81856","3b1244"),
			BuildContent("95dbd2","33","111111","976427","5f9c94"),	
            BuildContent("1b1b1b","01","6f6f6f","a70244","bebebe"), 
            BuildContent("95dbd2","33","111111","976427","5f9c94"),	
			BuildContent("411e00","11","892823","a3632a","ab322c"),
			BuildContent("95dbd2","33","111111","976427","5f9c94"),	
			BuildContent("1b1b1b","01","6f6f6f","a70244","bebebe"),	
			BuildContent("95dbd2","33","111111","976427","5f9c94"),	
            BuildContent("2f064e","12","74349c","e09400","e09400"),
            BuildContent("95dbd2","33","111111","976427","5f9c94"),	
			BuildContent("1b1b1b","01","6f6f6f","a70244","bebebe"),
			BuildContent("95dbd2","33","111111","976427","5f9c94"),	
			BuildContent("3a7516","13","3a8010","5db89e","ffaf2e"),
			BuildContent("95dbd2","33","111111","976427","5f9c94"),	
			BuildContent("1b1b1b","01","6f6f6f","a70244","bebebe"),
			BuildContent("95dbd2","33","111111","976427","5f9c94"),	
			BuildContent("111111","24","111111","c81856","3b1244"),			
			BuildContent("452632","14","91204d","e4844a","91204d"),		
			BuildContent("95dbd2","33","111111","976427","5f9c94"),	
			BuildContent("1b1b1b","01","6f6f6f","a70244","bebebe")
            );           
            var n = parseInt(Math.random() * 34);           
            document.write(arrayStyle[n]);
 }
   
     
function CheckVersion()
{
    if (navigator.userAgent.indexOf("Firefox")!=-1)
    {
        document.write("<style>body{background:#ac8c5d url(/design/cine/dienanh_bg2ie.jpg) no-repeat center top;}</style>");
    }
}
            
            
function ShowText(id)
{
    var first = id.getAttribute("first");
    if (first == "1")
    {
        id.value = "";
        id.setAttribute("first", "");
    }
}
     
function $Vid(id)
{
    return $ID(id) != null ? $ID(id).value : "";
}
function LogSite()
{
    var newsid = $Vid('newsId');
    var catid = $Vid('catId');
    var news_title = $Vid('newsTitle');
    var cat_title = $Vid('catTitle');
    Log_AssignValue(newsid, news_title,catid,cat_title );
}       

function SynDIV(div1,div2)
{
    var m1 = $ID(div1).offsetHeight;
    var m2 = $ID(div2).offsetHeight;   
   
        if (m1 > m2) {$ID(div2).style.height = m1 +"px";} 
        else {$ID(div1).style.height = m2 + "px";}
     
}
function $F(id)
{
    return $(id) != null ? $(id).value : "";
}


function share_facebook(){    u = location.href;    t = document.getElementById("newsTitle").value;    window.open("http://www.facebook.com/share.php?u=" + encodeURIComponent(u) + "&t=" + encodeURIComponent(t));}


function SaoView()
{
    var catId = document.getElementById("catId");
    var loc = "/Pages/VoteResult.aspx?VoteId=" + voteId + "&Cat_ID=" + catId.value;
     window.open(loc,null,"height=200,width=400,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes");
}
function SaoSelect(obj)
{
    document.getElementById("hidSelect").value = obj.value;    
}
function SaoBC()
{ 
    var catId = document.getElementById("catId");
    var other = document.getElementById("others").value;    
    var item = document.getElementById("hidSelect").value;
    if (item == "")
    {
        alert("Bạn chưa chọn ý kiến!");
        return ;
    }
    if (item != "" && parseInt(item) == 0 && other == "")
    {
        alert("Bạn chưa nhập ý kiến riêng!");
        document.getElementById("others").focus();
        return;
    }
    var loc = "/Pages/VoteResult.aspx?VoteId=" + voteId + "&Cat_ID=" + catId.value + "&VoteItemID=" + item + "&other=" + encodeURIComponent(other);
    window.open(loc,null, "height=400,width=500,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes");
}