﻿var fIsRTL = false;
var fSearchOn = false;
var fSearchFocus = false;
var strNonSearchString = 'Tra bằng tiếng Việt';
var fEnteredText = false;
function SelectAction(oSrchFrm, index)
{
	if ('undefined' == typeof(oSrchFrm) || null == oSrchFrm)
		return false;

	var tbQuery = null;
	if(!_searchInput) tbQuery = document.getElementById('frmSearch_tbQueryStr');
	else tbQuery = _searchInput;

	oSrchFrm.linhvucid.value = index;
	oSrchFrm.tu_khoa.value = tbQuery.value;

	if (!FChkSrchTxt(tbQuery.value) || !fEnteredText){
		alert('Bạn chưa gõ gì vào ô tìm kiếm.');
		return false;
	}

	return true;
}

function FSEnt(e)
{
	return e&&e.keyCode==13&&e.shiftKey;
}

function FChkSrchTxt(wz)
{
	var ws=/^[\s]*$/;
	return wz&&!ws.test(wz);
}

function FocSrchTxt(o)
{
	if(_searchInput!='undefined'&&_searchInput!=o) fEnteredText = false;
	
	if (o && o.value)
	{
		if (strNonSearchString != null && strNonSearchString != 'undefined' && o.value == strNonSearchString && !fEnteredText)
		{
			o.value = '';
		}
		else
		{
			o.select();
		}
		setSearchOnColor(o);
		fEnteredText=true;
	}
}

function FTrySearch(e, strFormName, index)
{
	fSearchFocus = false;
	searchclose();

	var oSrchFrm = document.getElementById(strFormName);

	if ('undefined' != typeof(oSrchFrm) && null != oSrchFrm && SelectAction(oSrchFrm, index))
		{

		try {external.AutoCompleteSaveForm(oSrchFrm);} catch (err) {}
		oSrchFrm.submit();
		}

	if (e && e.stopPropagation)
		e.stopPropagation(); 

	return false;
}

function FEntTextbox(e, strFormName, index)
{
	if (e && 13 == e.keyCode)
		return FTrySearch(e, strFormName, index);

	return true;
}

function StrEncodeUrlComponent(strUrlComponent)
{
	if (typeof(strUrlComponent) == "undefined" ||
		null == strUrlComponent)
		return "";

	if (typeof(encodeURIComponent) != "undefined")
		return StrEncodeUrlComponent55(strUrlComponent);

	return StrEncodeUrlComponent50(strUrlComponent);
}

function StrEncodeUrlComponent55(strUrlComponent)
{
	return encodeURIComponent(strUrlComponent);
}

function StrEncodeUrlComponent50(strUrlComponent)
{
	var i=0;
	var strEncoded = "";
	for (i=0; i < strUrlComponent.length; i++)
		{
		var chr = strUrlComponent.charAt(i);
		var iChr = strUrlComponent.charCodeAt(i);

		if (iChr < 128)
			{
			if ('!' == chr || '\'' == chr || '(' == chr || 	')' == chr || '~' == chr)
				strEncoded += chr;
			else if (0 == iChr)
				strEncoded += "%00";
			else if (127 == iChr)
				strEncoded += "%7F";
			else if ('+' == chr)
				strEncoded += "%2B";
			else if ('/' == chr)
				strEncoded += "%2F";
			else if ('@' == chr)
				strEncoded += "%40";
			else
				strEncoded += escape(chr);
			}
		else if (iChr < 2048)
			{
			strEncoded += "%";
			strEncoded += ( (iChr >> 6) + 192).toString(16).toUpperCase();

			strEncoded += "%";
			strEncoded += ( (iChr & 63) + 128).toString(16).toUpperCase();
			}
		else 
			{
			strEncoded += "%";
			strEncoded += ( (iChr >> 12) + 224).toString(16).toUpperCase();

			strEncoded += "%";
			strEncoded += ( ((iChr >> 6) & 63) + 128).toString(16).toUpperCase();

			strEncoded += "%";
			strEncoded += ( (iChr & 63) + 128).toString(16).toUpperCase();
			}
		}

	return strEncoded;
}

function StrReplaceParam(strUrl, strParam, strNewValue)
{
	var strUpperUrl = strUrl.toUpperCase();
	var strUpperParam = strParam.toUpperCase();

	var iStart = strUpperUrl.indexOf('?' + strUpperParam + "=");
	if (iStart < 0)
		iStart = strUpperUrl.indexOf('&' + strUpperParam + "=");

	if (iStart < 0)
		return strUrl;

	var iEnd = strUpperUrl.indexOf('&', iStart+1);

	if (iEnd < 0)
		iStart--;

	var strRet = strUrl.substring(0, iStart+1);

	if (iEnd >= 0)
		strRet += strUrl.substring(iEnd+1, strUrl.length);

	if (typeof(strNewValue) != 'undefined' &&
		null != strNewValue && strNewValue.length > 0)
		{
		var strSeparator = strRet.indexOf('?') < 0 ? '?' : '&';
		if (strRet.charAt(strRet.length-1) == strSeparator)
			strSeparator=''; 
		strRet += strSeparator + strParam + '=' + strNewValue;
		}

	return strRet;
}

function StrAddUniqueParam(strUrl, strParam, strNewValue)
{
	if ((null == strUrl) || (null == strParam) || (null == strNewValue))
		return strUrl;

	var strRet = StrReplaceParam(strUrl, strParam)

	var strSeparator = strRet.indexOf('?') < 0 ? '?' : '&';
	return strRet + strSeparator + strParam + "=" + strNewValue;
}

function StrGetCookie(tName)
{
	return GetCookie(tName, "na");
}

function mDeleteCookie(tName)
{
	var exp = new Date();
	exp.setDate (exp.getDate() -10);
	mSetCookie(tName, "", false, exp);
}

var gstrOneShotCookie;

function GetOneShotCookie()
{
	if ("undefined" == typeof(gstrOneShotCookie))
	{
		gstrOneShotCookie = GetCookie("awsval", "");
		mDeleteCookie("awsval");
	}

	if (typeof(gstrOneShotCookie) == "undefined" || null == gstrOneShotCookie)
		gstrOneShotCookie = "";

	return gstrOneShotCookie;
}

function StrGetArgumentValue(strQueryName)
{
	var reQuery = new RegExp("[\\?&]" + strQueryName + "=([^&]*)", "i");

	if (!reQuery.test(location))
		return null;

	return reQuery.exec(location)[0].substring(strQueryName.length + 2);
}

function offset(object, alignment) {
	var nOffset = 0;

	if (object == null)
		return nOffset;

	while (object.offsetParent) {
		nOffset += ((alignment == 'x') ? object.offsetLeft : object.offsetTop);
		object = object.offsetParent;
	}

	return nOffset;
}

function setNonSearchString(searchBox)
{
	if (searchBox != null && searchBox != 'undefined' && searchBox.value == '')
	{
		searchBox.value = strNonSearchString;
		setSearchOffColor(searchBox);
		fEnteredText = false;
	}
}

function setSearchOffColor(searchBox)
{
	searchBox.className = 'cdsearchbox cdSearchBoxOffColor';
}

function setSearchOnColor(searchBox)
{
	searchBox.className = 'cdsearchbox cdSearchBoxOnColor';
}

function delayedclose()
{
	fSearchFocus = false;
	window.setTimeout('searchclose()', 180);
}

function setSearchFocus()
{
	fSearchFocus = true;
}

function searchclose()
{
	var searchdrop = document.getElementById('cdsearchoutcdsdrop');
	if (searchdrop != null && searchdrop != 'undefined')
	{
		if (fSearchFocus == false)
		{
			searchdrop.style.display = 'none';
			fSearchOn = false;
		}
	}
}

function searchout()
{
	var searchdrop = document.getElementById('cdsearchoutcdsdrop');
	if (searchdrop != null && searchdrop != 'undefined' && fSearchOn == true)
	{
		searchdrop.style.display = 'none';
		fSearchOn = false;
	}
	else
	{
		var buttonleft = document.getElementById('cdsdropleft');
		var buttonmiddle = document.getElementById('cdsdrop');

		if (buttonleft != null && buttonleft != 'undefined' &&
			buttonmiddle != null && buttonmiddle != 'undefined' && 
			searchdrop != null && searchdrop != 'undefined')

		{
			var newleft = offset(buttonleft, 'x');
			var newtop = offset(buttonleft, 'y');

			searchdrop.style.display = 'none';
			searchdrop.style.position = 'absolute';

			if (fIsRTL)
			{				
				newleft = offset(buttonmiddle, 'x');
				searchdrop.style.left = newleft + 'px';
			}
			else
			{			
				searchdrop.style.left = newleft + 'px';
			}
			searchdrop.style.top = newtop + buttonmiddle.offsetHeight + 'px';

			searchdrop.style.display = 'block';

			fSearchOn = true;
		}
	}
}

function resizeSearchBox()
{
	var input = document.getElementById('frmSearch_tbQueryStr');
	var container = document.getElementById('tdSearchBox');
	if (input != null && input != 'undefined' && container != null && container != 'undefined')
	{
		var conwidth = container.offsetWidth;
		input.style.width = conwidth - 10 + 'px';

		if (input.value != strNonSearchString)
		{
			fEnteredText = true;
			setSearchOnColor(input);
		}
	}
}


