function QueryString(key)
{
    var strQueryString = window.location.search.substring(1);
    var values = strQueryString.split('&');
    for (i = 0; i < values.length; i++)
    {
        var value = values[i].split('=');
        if (value[0] == key)
        {
            return value[1];
        }
    }
    return '';
}

function RefreshImage(imgId, url)
{
    var chartImage = document.getElementById(imgId);
    
    var currentDateTime = new Date();
    
    chartImage.src = url + '?' + currentDateTime.getDate() + currentDateTime.getTime();
}

function fixNumber(str)
{
    var temp = strim(str); // trim
    
    if (temp.indexOf('.') > 0)
    {
        temp = temp.substring(temp.indexOf('.') + 1);
        
        if (temp.length == 3)
        {
            str = str.replace(',', '#');
            str = str.replace('.', ',');
            str = str.replace('#', '.');
        }
    }
    
    return str;
}

function FormatNumber(value, displayZero)
{
    if (value == '') return (displayZero ? '0' : '');
    try
    {
        var number = parseFloat(value);
        value = FormatNumber1(number, 2, '.', ',');
        return (value);
    }
    catch (err)
    {
        return (displayZero ? '0' : '');
    }
}

function FormatNumber1(number, decimals, decimalSeparator, thousandSeparator) 
{
    var number = number.toFixed(decimals);
    
    var temp = number.toString();
    
    var f = temp.substr(temp.length - decimals, decimals);
    
    while (f != '' && f.charAt(f.length - 1) == '0') f = f.substr(0, f.length - 1);
    
    if (f != '') f = decimalSeparator + f;
    
    var t = temp.substr(0, temp.length - 3);
    
    if (thousandSeparator != '' && t.length > 3) 
	{
		h = t;
		t = '';
		
		for (j = 3; j < h.length; j += 3) 
		{
			i = h.slice(h.length - j, h.length - j + 3);
			t = thousandSeparator + i +  t + '';
		}
		
		j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
		t = j + t;
	}
	
	temp = t + f;
	
    return temp.replace('-,', '-');;
}

function String2Float(value)
{
    return (value != '' ? parseFloat(value) : 0);
}

function Reload()
{
    window.location.reload();
}

function ltrim(str){
    try
    {
        if (str)
        {
            return str.toString().replace(/^\s+/, '');
        }
        else
        {
            return '';
        }
    }
    catch (ex)
    {
        return str;
    }
}
function rtrim(str) {
    try
    {
        if (str)
        {
            return str.replace(/\s+$/, '');
        }
        else
        {
            return '';
        }
    }
    catch (ex)
    {
        return str;
    }
}
function trim(str) {
    try
    {
        if (str)
        {
            return str.replace(/^\s+|\s+$/g, '');
        }
        else
        {
            return '';
        }
    }
    catch (ex)
    {
        return str;
    }
}

function ScrollObject(instanceName, containerId, scrollContentId, delay, speed)
{
    this.InstanceName = instanceName;
    
    this.Container = null;
    this.ScrollContent = null;
    
    this.Delay = delay;
    this.Speed = speed;
    
    this.IsScrolling = true;
    
    this.InitScroll = function()
    {
        this.Container = document.getElementById(containerId);
        this.ScrollContent = document.getElementById(scrollContentId);
        
        this.ScrollContent.left = this.Container.offsetWidth;

        if (window.opera || navigator.userAgent.indexOf("Netscape/7") != -1)
        { //if Opera or Netscape 7x, add scrollbars to scroll
            //this.ScrollContent.style.width = this.Container.offsetWidth + "px";
            //this.ScrollContent.style.overflow = "scroll";
            setTimeout(this.InstanceName + '.Scroll()', this.Delay);
        }
        else
        {
            setTimeout('setInterval("' + this.InstanceName + '.Scroll()", ' + this.Delay + ')', this.Delay);
        }
    }
    
    this.Scroll = function()
    {
        if (this.IsScrolling)
        {
            if (parseInt(this.ScrollContent.style.left) > (this.ScrollContent.offsetWidth * (-1) + 8))
            {
                this.ScrollContent.style.left = parseInt(this.ScrollContent.style.left) - this.Speed + "px";
            }
            else
            {
                this.ScrollContent.style.left = parseInt(this.Container.offsetWidth) + 8 + "px";
            }
        }
        
        if (window.opera || navigator.userAgent.indexOf("Netscape/7") != -1)
        {
            setTimeout(this.InstanceName + '.Scroll()', this.Delay);
        }
    }
}
function checkContactForm(f){	
//email
	var regex = /^(([\-\w]+)\.?)+@(([\-\w]+)\.?)+\.[a-zA-Z]{2,4}$/;
	if(f.email.value=="" || f.email.value==null){
		window.alert('Bạn chưa nhập email');
		f.email.focus();
		return false;
	}
	if (!regex.test(f.email.value)){
		window.alert('Email của bạn chưa đúng');
		f.email.focus();
		return false;
	}
	if(f.title.value=="" || f.title.value==null){
		window.alert("Bạn chưa nhập tiêu đề");
		f.title.focus();
		return false;
	}
	if(f.content.value=="" || f.content.value==null){
		window.alert("Bạn chưa nhập nội dung");
		f.content.focus();
		return false;
	}
	return true;
}

