function openPopUp(width,height,url,target){
		return window.open(url,target,'toolbar=0,location=0, directories=0, status=0, menubar=0, scrollbars=1, resizable=0, width=' + width + ', height=' + height + ', left=' + (screen.width - width)/2 + ', top=' + (screen.height-height)/2 );
}
function openPopUp2(width,height,url,target){
		return window.open(url,target,'toolbar=0,location=0, directories=0, status=0, menubar=0, scrollbars=0, resizable=0, width=' + width + ', height=' + height + ', left=' + (screen.width - width)/2 + ', top=' + (screen.height-height)/2 );
}
function popWin(){
        return openPopUp(550,400,'about:blank','__popWin');
}
function popPrint(){
        var width=550;var height=400;
        return window.open("about:blank",'__popPrint','toolbar=0,location=0, directories=0, status=0, menubar=0, scrollbars=0, resizable=0, width=' + width + ', height=' + height + ', left=' + (screen.width - width)/2 + ', top=' + (screen.height-height)/2 );
        
}
function op_m_i(url){
    openPopUp(392,450,'about:blank','_pimage');
}
function printO(obj){
    obj.focus();
    obj.print();
}

function comment_window(){
    openPopUp(550,530,'about:blank','_comment')
}
function rateover(o, r){
    var id = parseInt(o.id.replace('dorate',''));
    for(var i=1; i<=id;i++){
        document.getElementById('dorate'+i).src='/images/rate/ra.gif';
    }
}
function rateout(o, r){
    for(var i=1; i<=5;i++){
        if(i<=r)
            document.getElementById('dorate'+i).src='/images/rate/r1.gif';
        else
            document.getElementById('dorate'+i).src='/images/rate/r0.gif'
    }
}

function ajax_page(url, containerid){
    var page_request = false
    if (window.XMLHttpRequest) // if Mozilla, Safari etc
        page_request = new XMLHttpRequest()
    else if (window.ActiveXObject){ // if IE
        try {
            page_request = new ActiveXObject("Msxml2.XMLHTTP")
        } 
        catch (e){
            try{
                page_request = new ActiveXObject("Microsoft.XMLHTTP")
            }
            catch (e){}
        }
    }
    else
        return false;
        
    document.getElementById(containerid).innerHTML='<img src="/images/loading14.gif" align="absmiddle"/>'//loading.. text
    page_request.onreadystatechange=function(){
        load_page(page_request, containerid)
    }
    
    var cache_clear_param=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime();
    page_request.open('GET', url+cache_clear_param, true);
    page_request.send(null);
    
}

function load_page(page_request, containerid){
    if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
    document.getElementById(containerid).innerHTML=page_request.responseText
    //window.location.href.indexOf("http")==-1 : Not remote site
}

function dorate(id,point,sid){
    ajax_page('/backend/rating.aspx?news_id='+id+"&point="+point+"&sid="+sid,'rating_pad');
}
function badReport(id){
    ajax_page('/backend/CommentBadReport.aspx?id='+id,'cmt_bad_'+id);
    alert('Cảm ơn bạn đã thông báo cho chúng tôi');
}
function time_params(){
    var a = new Date();
    var ms = a.getMilliseconds();
    var s = a.getSeconds();
    var mn = a.getMinutes();
    var h = a.getHours();
    var d = a.getDay();
    var m = a.getMonth();
    var y = a.getFullYear();
    var t = y+'_'+m+'_'+d+'_'+h+'_'+mn+'_'+s+'_'+ms;
    return t;
}

function counter(id){
    document.write('<img src="/backend/counter.ashx?news_id='+id+'&sid='+time_params()+'" width="1" height="1" border="0" />');
}

function setCookie(c_name,value,expiredays)
{
	var exdate=new Date()
	exdate.setDate(exdate.getDate()+expiredays)
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate)
} 

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=")
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1 
    c_end=document.cookie.indexOf(";",c_start)
    if (c_end==-1) c_end=document.cookie.length
    return unescape(document.cookie.substring(c_start,c_end))
    } 
  }
return null
}

function checkCookie()
{
username=getCookie('username')
if (username!=null)
  {alert('Welcome again '+username+'!')}
else 
  {
  username=prompt('Please enter your name:',"")
  if (username!=null && username!="")
    {
    setCookie('username',username,365)
    }
  }
}

function addfav()
{
    if (window.sidebar) {
        window.sidebar.addPanel(document.title, location.href,"");
    } else if( window.external ) { // IE Favorite
        window.external.AddFavorite(location.href,document.title); }
    else if(window.opera && window.print) {
        return true; }
    else {
        alert("Chức năng này không được hỗ trợ cho trình duyệt bạn đang sử dụng!");
    }
   
}

function gotoRef(url){
    var _ref = "returnUrl=" + escape(location.href);
    _ref = (url.indexOf("?")!=-1) ? "&" + _ref : "?" + _ref;
    location.href = url + _ref;
}
//String format method String.Format("abc {0}",val)
String.Format = function( text ){
    if ( arguments.length <= 1 )
    {
        return text;
    }
    var tokenCount = arguments.length - 2;
    for( var token = 0; token <= tokenCount; token++ )
    {
        text = text.replace( new RegExp( "\\{" + token + "\\}", "gi" ), arguments[ token + 1 ] );
    }
    return text;
};
//Currency string format
String.Currency = function(num) {
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num)) num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10) cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
        num = num.substring(0,num.length-(4*i+3)) + '.'+ num.substring(num.length-(4*i+3));
    return num;// + '.' + cents);
}
//Get position {top,left} of html element
function getOffset(el) {
    var _x = 0;
    var _y = 0;
    while( el && !isNaN( el.offsetLeft ) && !isNaN( el.offsetTop ) ) {
        _x += el.offsetLeft - el.scrollLeft;
        _y += el.offsetTop - el.scrollTop;
        el = el.offsetParent;
    }
    return { top: _y, left: _x };
}
//Set pos of an element to other element
function setOverEL(frId, toId){
    var obj = document.getElementById(frId);
    var src = document.getElementById(toId);
    var _h=0;
    var _w=0;
    if(obj.offsetHeight)
    {
        _h=obj.offsetHeight;
    }
    else if(obj.style.pixelHeight)
    {
        _h=obj.style.pixelHeight;
    }    
    if(obj.offsetWidth)
    {
        _w=obj.offsetWidth;
    }
    else if(obj.style.pixelWidth)
    {
        _w=obj.style.pixelWidth;
    }    
    var pos = getOffset(src);
    src.style.width = _w+'px';
    src.style.height= _h+'px';
    //alert('change size '+toId+' to ('+_w+','+_h+') and \nmove ' + frId + '('+obj.style.top+','+obj.style.left+') to ' + toId + '('+pos.top+','+pos.left+')');
    obj.style.top = pos.top+'px';
    obj.style.left = pos.left+'px';
    var t = window.setTimeout("setOverEL('"+frId+"','"+toId+"')",1000);
}
//Url.encode & Url.decode
var Url = {
 
	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},
 
	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}

function HelpTextID(id, txt){    
    var ob = document.getElementById(id);
    if(ob) HelpText(ob, txt);
}

function HelpText(ob, txt){    
    if(ob.value==null || ob.value=='')
        ob.value=txt;
    ob.onblur = function(){
        if(ob.value=='' || ob.value==null)
            ob.value=txt;
    }        
    ob.onfocus = function(){
        if(ob.value==txt){
            ob.value='';
        }
    }
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /([a-zA-Z0-9]+)([a-zA-Z0-9]{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + '.' + '$2');
	}
	return x1;
}

function addCommasPhone(nStr)
{
	var newValue = nStr.replace(/\D/g,'');
	newValue = newValue.substring(0,3) + "-" + newValue.substring(3,6) + "-" + newValue.substring(6);
	return newValue;
}

function friendlyFormat(div_id,str_number){
	/*Convert tu 1000->1.000*/
	var mynumber=1000;	
	/*str_number = str_number.replace(/\./g,"");*/
	document.getElementById(div_id).innerHTML = addCommas(str_number);
}


function GetWH(id, type){
    var obj = document.getElementById(id);
    var _h=0;
    var _w=0;
    if(obj.offsetHeight)
    {
        _h=obj.offsetHeight;
    }
    else if(obj.style.pixelHeight)
    {
        _h=obj.style.pixelHeight;
    }    
    if(obj.offsetWidth)
    {
        _w=obj.offsetWidth;
    }
    else if(obj.style.pixelWidth)
    {
        _w=obj.style.pixelWidth;
    }
    if(type=='w') return _w;
    if(type='h') return _h;
    return 0;
}   