function LanguageSet(PageLang)
{
	setCookie('Profile.Lang', '/', '', PageLang);
	location.reload(1);
}

function SetCurrentOption(field, Current)
{
	for (var i=0; i < field.options.length; i++)
	{
		if (field.options[i].value==Current)
		{
			field.selectedIndex = i;
			return 1;
		}
	}

	if (field.options.length > 0)
	{
		field.selectedIndex = 0;
	}
	return 0;
}

function setCookie(Name, Path, Expires, Value)
{
	var cstr = Name.concat('=').concat(Value);
	
	if (Path=='')
		path='/';

	cstr=cstr.concat(';path=').concat(Path);


	if (Expires=='')
		Expires=(new Date(2020, 11, 14)).toGMTString();

	document.cookie=cstr.concat(';expires=').concat(Expires);
}

function getCookie(Name, Default)
{
	var cookie = document.cookie;
	var ir = 0, ie = 0, sf = '', i = 0, j = 0;
	Name = Name.toLowerCase();

	if (typeof(Default) == 'undefined')
		Default = '';

	if (cookie.length == 0)
		return Default;

	if ((ir = Name.indexOf('.')) == -1)
	{
		if (cookie.substr(0, Name.length + 1).toLowerCase() == Name.concat('='))
		{
			if ((ie = cookie.indexOf(';')) != -1)
			{
				cookie = cookie.substr(0, ie);
			}
		}
		else
		{
			if ((ie = cookie.toLowerCase().indexOf('; '.concat(Name).concat('='))) == -1)
				return Default;

			cookie = cookie.substr(ie + 2);

			if ((ie = cookie.indexOf(';')) != -1)
			{
				cookie = cookie.substr(0, ie);
			}
		}
		sf = ';';
	}
	else
	{
		if ((i=cookie.toLowerCase().indexOf(Name.concat('='))) != -1)
		{
			if ((j = cookie.indexOf(';', i)) > i + Name.length + 1)
			{
				return ReplaceAll(unescape(cookie.substr(i + Name.length + 1, j - i - Name.length - 1)), '+', ' ');
			}
			else
			{
				j = cookie.length;
				return ReplaceAll(unescape(cookie.substr(i + Name.length + 1, j - i - Name.length - 1)), '+', ' ');
			}
		}

		var Root = Name.substr(0, ir);
		Name = Name.substr(ir + 1);

		if (cookie.substr(0, Root.length + 1).toLowerCase() == Root.concat('='))
		{
			if ((ie = cookie.indexOf(';')) != -1)
			{
				cookie = cookie.substr(0, ie);
			}
		}
		else
		{
			if ((ie = cookie.toLowerCase().indexOf('; '.concat(Root).concat('='))) == -1)
				return Default;

			cookie = cookie.substr(ie + 2);

			if ((ie = cookie.indexOf(';')) != -1)
			{
				cookie = cookie.substr(0, ie);
			}
		}

		cookie = cookie.substr(Root.length + 1);
		sf = '&';
	}

	if (cookie.substr(0, Name.length + 1).toLowerCase() == Name.concat('='))
	{
		ir = Name.length + 1;
	}
	else
	{
		if ((ir = cookie.toLowerCase().indexOf('&'.concat(Name).concat('='))) == -1)
			return Default;

		ir+=Name.length + 2;
	}

	if ((ie=cookie.indexOf(sf, ir)) == -1)
	{
		return ReplaceAll(unescape(cookie.substr(ir)), '+', ' ');
	}
	else
	{
		return ReplaceAll(unescape(cookie.substring(ir, ie)), '+', ' ');
	}
}

function ReplaceAll(iStr, v1, v2)
{
	var i = 0, oStr = '', j = v1.length;

	while (i < iStr.length)
	{
		if (iStr.substr(i, j) == v1)
		{
			oStr+=v2;
			i+=j
		}
		else
		{
			oStr+=iStr.charAt(i);
			i++;
		}
	}

	return oStr;
}

function LTrim(iStr)
{
	while (iStr.charCodeAt(0) <= 32)
	{
		iStr=iStr.substr(1);
	}
	return iStr;
}

function RTrim(iStr)
{
	while (iStr.charCodeAt(iStr.length - 1) <= 32)
	{
		iStr=iStr.substr(0, iStr.length - 1);
	}
	return iStr;
}

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 UnicodeSet(iStr)
{
	for (i=0, oStr=''; i < iStr.length; i++)
	{
		switch ((j=iStr.charCodeAt(i)))
		{
		case 34:
			oStr=oStr.concat('&quot;');
			break;
		case 38:
			oStr=oStr.concat('&amp;');
			break;
		case 39:
			oStr = oStr.concat('&#39;');
			break;
		case 60:
			oStr = oStr.concat('&lt;');
			break;
		case 62:
			oStr = oStr.concat('&gt;');
			break;
		default:
			if (j < 32 || j > 127 || j==34 || j==39)
			{
				oStr=oStr.concat('&#').concat(j).concat(';');
			}
			else
			{
				oStr=oStr.concat(iStr.charAt(i)); 
			}
			break;
		}
	}
	
	return oStr;
}

function UnicodeGet(iStr)
{
	for (i=0, oStr=''; i < iStr.length; )
	{
		if (iStr.charCodeAt(i)==38)
		{
			if (iStr.charCodeAt(i + 1)==35)
			{
				p=iStr.indexOf(';', i  + 2);
				if (p!=-1)
				{
					if (p - i <= 7)
					{
						if (isFinite(iStr.substr(i + 2, p - i - 2)))
						{
							oStr = oStr.concat(String.fromCharCode(iStr.substr(i + 2, p - i - 2)));
							i = p + 1;
							continue;
						}
					}
				}
			}
			else
			{
				p=iStr.indexOf(';', i  + 1);
				if (p!=-1)
				{
					switch (iStr.substr(i + 1, p - i - 1))
					{
					case 'amp':
						oStr = oStr.concat('&');
						i = p + 1;
						break;
					case 'quot':
						oStr = oStr.concat('"');
						i = p + 1;
						break;
					case 'lt':
						oStr = oStr.concat('<');
						i = p + 1;
						break;
					case 'gt':
						oStr = oStr.concat('>');
						i = p + 1;
						break;
					}
				}
			}
		}
	
	
		oStr=oStr.concat(iStr.charAt(i));
		i++;
	}
	
	return oStr;
}


function SetNameAndService(vIP, vName, vCallerID)
{
	var i, v1, v2, v3;

	if (vName == '1280')
	{
		if (SetCurrentOption(document.forms[0].QServ, '3'))
		{
			document.forms[0].QUser.value = vCallerID;
			SetCurrentOption(document.forms[0].QLoca, vCallerID.substr(0, 1));
			document.forms[0].QPass.focus();
		}
		return;
	}


	if (vName.substr(vName.length - 5).toUpperCase()=='@CARD')
	{
		if (SetCurrentOption(document.forms[0].QServ, '2'))
		{
			document.forms[0].QUser.value = vName;
			SetCurrentOption(document.forms[0].QLoca, '8');
			document.forms[0].QPass.focus();
		}
		return;
	}

	if (vName.substr(vName.length - 7).toUpperCase()=='@FPT.VN')
	{
		if (SetCurrentOption(document.forms[0].QServ, '1'))
		{
			document.forms[0].QUser.value = vName;
			SetCurrentOption(document.forms[0].QLoca, '4');
			document.forms[0].QPass.focus();
		}
		return;
	}

	if (vName.substr(vName.length - 11).toUpperCase()=='@HCM.FPT.VN')
	{
		if (SetCurrentOption(document.forms[0].QServ, '1'))
		{
			document.forms[0].QUser.value = vName;
			SetCurrentOption(document.forms[0].QLoca, '8');
			document.forms[0].QPass.focus();
		}
		return;
	}

	if (vName.substr(0, 5).toUpperCase()=='CARD/')
	{
		if (SetCurrentOption(document.forms[0].QServ, '2'))
		{
			document.forms[0].QUser.value = vName;
			SetCurrentOption(document.forms[0].QLoca, '4');
			document.forms[0].QPass.focus();
		}
		return;
	}

	v1 = vIP.substr(0, vIP.lastIndexOf('.'));
	i = v1.lastIndexOf('.') - 1;
	v2 = v1.substr(0, i + 1);
	v3 = parseInt(v1.substr(i + 2));

	if (v2 == '210.245')
	{
		if (v3 >= 0 && v3 <= 15)
		{
			SetCurrentOption(document.forms[0].QLoca, '4');				
		}
		else if (v3 >= 16 && v3 <= 31)
		{
			SetCurrentOption(document.forms[0].QLoca, '8');
		}

		if (SetCurrentOption(document.forms[0].QServ, '1'))
		{
			if ((document.forms[0].QUser.value = vName) == '')
			{
				document.forms[0].QUser.focus();
			}
			else
			{
				document.forms[0].QPass.focus();
			}
		}		
	}
}

function ReverseNameToService()
{
	document.Search.QUser.value = Trim(document.Search.QUser.value);

	if (document.Search.QUser.value.substr(document.Search.QUser.value.length - 5).toUpperCase()=='@CARD')
	{
		SetCurrentOption(document.Search.QLoca, '8');
		SetCurrentOption(document.Search.QServ, '2');
		return;
	}

	if (document.Search.QUser.value.substr(0, 5).toUpperCase()=='CARD/')
	{
		SetCurrentOption(document.Search.QLoca, '4');
		SetCurrentOption(document.Search.QServ, '2');
		return;
	}

	if (document.Search.QUser.value.substr(document.Search.QUser.value.length - 7).toUpperCase()=='@FPT.VN')
	{
		SetCurrentOption(document.Search.QLoca, '4');
		SetCurrentOption(document.Search.QServ, '4');
		return;
	}

	if (document.Search.QUser.value.substr(document.Search.QUser.value.length - 11).toUpperCase()=='@HCM.FPT.VN')
	{
		SetCurrentOption(document.Search.QLoca, '8');
		SetCurrentOption(document.Search.QServ, '4');
		return;
	}
}
