﻿
// this function gets the cookie, if it exists
function GetCookie( 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 SetCookie( name, value, expires, path, domain, secure ) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );

	if ( expires )
	{
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + //expires.toGMTString()
		( ( path ) ? ";path=" + path : "" ) + 
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

// this deletes the cookie when called
function DeleteCookie( name, path, domain ) {
	if ( GetCookie( name ) ) document.cookie = name + "=" +
			( ( path ) ? ";path=" + path : "") +
			( ( domain ) ? ";domain=" + domain : "" ) +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}


// Quick link
var isShowQuickLink = false;
function showQuickLink()
{
    isShowQuickLink = !isShowQuickLink;
	var divObj = document.getElementById( "QuickLink" );
	//var imgObj = document.getElementById( "imgQickLinkArrow" );
	var labQuickLink = document.getElementById( "labQuickLink" );
	
	if( isShowQuickLink )
	{
	    divObj.style.visibility = "visible";
	    //imgObj.src = "Images/down-arrow.gif";
	    //labQuickLink.style.fontWeight = "bold";
	}
	else
	{
	    divObj.style.visibility = "hidden";
	    //imgObj.src = "Images/right-arrow.gif";
	}
}

// Fit image by width
function FitWidthById( imageId, fixWidth, count )
{
        var image = document.getElementById(imageId);
        if( image.height == 0 || image == null )
        {
            if( count < 20 /* 1/2 s */ ) // Sau 10 giây mà vẫn chưa load xong. Tự động hủy việc co ảnh
            {
                var countPlus = count + 1;
                setTimeout("FitWidthById( '"+ imageId +"', "+ fixWidth +", "+ countPlus +" )", 500 );
            }else
            {
                image.width = fixWidth;
                image.height = fixWidth;
            }
            return;
        }else
        if( image.height > image.width )
        {
            image.width = (fixWidth * image.width) / image.height;
            image.height = fixWidth;
        }
}

// ------------------------ String--------------------------
// Removes leading whitespaces
function LTrim( value ) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}

// Removes ending whitespaces
function RTrim( value ) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}

// Removes leading and ending whitespaces
function trim( value ) 
{
	return LTrim(RTrim(value));
}

// ------------------------ Textbox focus -------------------
function TextboxOnFocus(textboxObj, value )
{
    if( textboxObj.value == value )
    {
        textboxObj.value = "";
        textboxObj.style.color = '#000';
    }
}

function TextboxOnBlur(textboxObj, value)
{
    if( textboxObj.value == "" )
    {
        textboxObj.value = value;
        textboxObj.style.color = '#BBB';
    }
}

//-------------- Div -----------------
function ShowDiv( divId )
{
    var div = document.getElementById( divId );
    if( div.style.visibility == "visible" )
    {
        div.style.visibility = "hidden";
        div.style.display="none";
    }else
    {
        div.style.visibility = "visible";
        div.style.display="block";
    }
}
