var flag = false;

function GetExpiryDate(DayCount)
{
	var UTCstring;
	Today = new Date();
	nomilli=Date.parse(Today);
	Today.setTime(nomilli+DayCount*24*60*60*1000);
	UTCstring = Today.toUTCString();
	return UTCstring;
}
	
function SetCookie(name,value,duration)
{
	CookieString=name+"="+escape(value)+";EXPIRES="+GetExpiryDate(duration);
	document.cookie=CookieString;
}

function GetCookie(CookieName) 
{
	var CookieString=""+document.cookie;
	var index1=CookieString.indexOf(CookieName);
	if (index1==-1 || CookieName=="") return ""; 
	var index2=CookieString.indexOf(';',index1);
	if (index2==-1) index2=CookieString.length; 
	return unescape(CookieString.substring(index1+CookieName.length+1,index2));
}

function CheckAll(State)
{
	for (var i=0; i < document.frmSelect.elements.length; i++)
		if (document.frmSelect.elements[i].type == 'checkbox')
			document.frmSelect.elements[i].checked = State;
}

function BuildSelectList(RemoveComma)
{
	var strSelectList="";
	for (var i=0; i < document.frmSelect.elements.length; i++)
		if (document.frmSelect.elements[i].type == 'checkbox')
			if (document.frmSelect.elements[i].checked)
				strSelectList += "," + document.frmSelect.elements[i].value;				
	if (RemoveComma)	strSelectList=strSelectList.substring(1);
	return strSelectList;
}
function View()
{
	flag = true;	
	SetCookie('SelectList', BuildSelectList(0), 2);
	flag = true;
}
function ViewHCM()
{
	flag = true;	
	SetCookie('SelectListHCM', BuildSelectList(0), 2);
}
function ViewHN()
{
	flag = true;
	SetCookie('SelectListHN', BuildSelectList(0), 2);
}
function ViewOTC()
{
	flag = true;	
	SetCookie('SelectListOTC', BuildSelectList(0), 2);
}
function LoadSelecteList()
{		
	var str=GetCookie("SelectList");
	for (var i=0; i < document.frmSelect.elements.length; i++)
		if (document.frmSelect.elements[i].type == 'checkbox')
			if (str.indexOf(","+document.frmSelect.elements[i].value)>=0)
				document.frmSelect.elements[i].checked = true;
}
function LoadSelectListHCM()
{	
	var str=GetCookie("SelectListHCM");
	for (var i=0; i < document.frmSelect.elements.length; i++)
		if (document.frmSelect.elements[i].type == 'checkbox')
			if (str.indexOf(","+document.frmSelect.elements[i].value)>=0)
				document.frmSelect.elements[i].checked = true;
}
function LoadSelectListHN()
{
	var str=GetCookie("SelectListHN");
	for (var i=0; i < document.frmSelect.elements.length; i++)
		if (document.frmSelect.elements[i].type == 'checkbox')
			if (str.indexOf(","+document.frmSelect.elements[i].value)>=0)
				document.frmSelect.elements[i].checked = true;
}
function LoadSelectListOTC()
{
	var str=GetCookie("SelectListOTC");
	for (var i=0; i < document.frmSelect.elements.length; i++)
		if (document.frmSelect.elements[i].type == 'checkbox')
			if (str.indexOf(","+document.frmSelect.elements[i].value)>=0)
				document.frmSelect.elements[i].checked = true;
}
function Trim(iStr)
{
	while (iStr.charCodeAt(0) <= 32)
	{
		iStr=iStr.substr(1);
	}

	while (iStr.charCodeAt(iStr.length - 1) <= 32)
	{
		iStr=iStr.substr(0, iStr.length - 1);
	}

	return iStr;
}

function Left(str, n)
{
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}


function Right(str, n)
{
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

function DeleteRows()
{
	var i;
	var dtab = document.getElementById('tblContent');
	for (i = dtab.rows.length - 1; i >= 2; i--)
	{
		dtab.deleteRow(dtab.rows[i].rowIndex);		
	}
}

function ContactUs()
{
	open('/ContactUs/Contact.htm','Toasoan','height=340px,width=400px,status=no,toolba=no,location=no');
}

function formatNumber(iVal)
{	
	var iStr=iVal.toString();
	var aStr='';
	var oStr='';
	var rPos=0;

	while (((rPos=iStr.indexOf(',')) >= 0))
				iStr=iStr.substr(0, rPos) + iStr.substr(rPos + 1);

	if ((rPos=iStr.indexOf('.')) >= 0)
	{
				pStr=iStr.substr(rPos + 1);
				iStr=iStr.substring(0, rPos);
	}
	else
	{
				pStr='';
	}

	if (iStr.substr(0, 1)=='-')
	{
				aStr='-';
				iStr=iStr.substr(1);
	}

	while (iStr.length > 0)
	{
				oStr=(oStr.length==0) ? iStr.substr(iStr.length - 3) : iStr.substr(iStr.length - 3) + ',' + oStr;
				iStr=iStr.substr(0, iStr.length - 3);
	}

	return (pStr=='') ? aStr + oStr : aStr + oStr + '.' + pStr;	
}

function FormatCurrency(num, currencyCode, isReplace, justFormat) {
    if (num == null)
        return "";
    var num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num))
        num = "0";
    var 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++) {
        switch (Trim(currencyCode.toLowerCase())) {
            case 'en-us':
                {
                    num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
                    break;
                }
            case 'vi-vn':
                {
                    num = num.substring(0, num.length - (4 * i + 3)) + '.' + num.substring(num.length - (4 * i + 3));
                    break;
                }
        }
    }
    var res = '0';
    switch (Trim(currencyCode.toLowerCase())) {
        case 'en-us':
            {
                if (justFormat != null && justFormat == true) {
                    if (isReplace == false)
                        res = (((sign) ? '' : '-') + num + '.' + cents);
                    else
                        res = (((sign) ? '' : '-') + num);
                }
                else {
                    if (isReplace == false)
                        res = (((sign) ? '' : '-') + '$' + num + '.' + cents);
                    else
                        res = (((sign) ? '' : '-') + '$' + num);
                }
                break;
            }
        case 'vi-vn':
            {
                if (justFormat != null && justFormat == true) {
                    if (isReplace == false)
                        res = (((sign) ? '' : '-') + num + ',' + cents);
                    else
                        res = (((sign) ? '' : '-') + num);
                }
                else {
                    if (isReplace == false)
                        res = (((sign) ? '' : '-') + num + ',' + cents + '<u>d</u>');
                    else
                        res = (((sign) ? '' : '-') + num + '<u>d</u>');
                }
                break;
            }
    }
    return res;
}

/***********************************************************************************************/


