var nDummy = 0;
function dummy() {}

///////////////////////////////////////////////////////////////
// Http

function BzHttpPost(sUrl, sPost, pResFunction) {
	new Ajax.Request(sUrl, {
		method: "POST",
		parameters: sPost,
		onSuccess: function(httpObj) {
			if (pResFunction != null) pResFunction(httpObj.responseText);
		}
	});
}

///////////////////////////////////////////////////////////////
// Find ListBox

var BzOnFlbReturn = null;
var BzOnFlbEscape = null;
var BzOnFlbFocus = null;

function BzOnFlbClick(sInput, sListView) {
	var pInput = document.getElementById(sInput);
	var pListView = document.getElementById(sListView);
	var pList = document.getElementById(sListView + "_lst");
	pInput.value = pList.options[pList.selectedIndex].text;
	pListView.style.display = 'none';
	if (BzOnFlbReturn != null)
		BzOnFlbReturn();
}

function BzOnFlbClose(sListView) {
	document.getElementById(sListView).style.display = 'none';
}

function BzOnFlbList(sInput, sListView, arrValue, arrText) {
	var sBuf = "";
	var pInput = document.getElementById(sInput);
	var pListView = document.getElementById(sListView);

	var sOption = "";
	var sResult = "";

	var sValue = pInput.value;
	var nLen = sValue.length;
	if (nLen > 0) {
		var nOption = 0;
		if (isNaN(sValue)) {
			for (n=0; n<arrText.length; n++) {
				if (arrText[n].substr(0, nLen) == sValue) {
					sBuf += '<option value=' + arrValue[n] + '>';
					sBuf += arrValue[n] + ':' + arrText[n] + '<br>';
					sBuf += '</option>';
					if (nOption == 0) {
						sOption = arrText[n];
						sResult = arrValue[n] + ':' + arrText[n];
					} else
						sOption = "";
					nOption++;
				}
			}
		} else {
			for (n=0; n<arrValue.length; n++) {
				if (arrValue[n].substr(0, nLen) == sValue) {
					sBuf += '<option value=' + arrValue[n] + '>';
					sBuf += arrValue[n] + ':' + arrText[n] + '<br>';
					sBuf += '</option>';
					if (nOption == 0) {
						sOption = arrValue[n];
						sResult = arrValue[n] + ':' + arrText[n];
					} else
						sOption = "";
					nOption++;
				}
			}
		}
	} else {
		for (n=0; n<arrValue.length; n++) {
			sBuf += '<option value=' + arrValue[n] + '>';
			sBuf += arrValue[n] + ':' + arrText[n] + '<br>';
			sBuf += '</option>';
		}
	}
	if (sBuf.length > 0) {
		if (sValue.length > 0 && sValue == sOption) {
			pListView.style.display = 'none';
			pInput.value = sResult;
			pInput.focus();
		} else {
			pListView.innerHTML = "<select id=\"" + sListView + "_lst\" name=\"" + sListView + "_lst\" size=\"10\" onClick=\"BzOnFlbClick('" + sInput + "', '" + sListView + "');\" onKeyDown=\"BzOnFlbEnter('" + sInput + "', '" + sListView + "');\"" + ((BzOnFlbFocus != null) ? " onFocus=\"BzOnFlbFocus.focus();\"" : "") + ">" + sBuf + "</select>";
			pListView.style.display = 'inline';
		}
	} else
		pListView.style.display = 'none';
}

function BzOnFlbKeyDown(sInput, sListView, arrValue, arrText) {
	var pInput = document.getElementById(sInput);
	var pListView = document.getElementById(sListView);
	switch (event.keyCode) {
	case 13: // VK_RETURN
		var pListView = document.getElementById(sListView);
		if (pListView.style.display != 'none') {
			var pOptions = document.getElementById(sListView + "_lst").options;
			if (pOptions.length > 0 && pOptions.selectedIndex >= 0) {
				var pInput = document.getElementById(sInput);
				pInput.value = pOptions[pOptions.selectedIndex].text;
			}
			pListView.style.display = 'none';
		}
		if (BzOnFlbReturn != null)
			BzOnFlbReturn();
		break;
	case 27: // VK_ESCAPE
		if (BzOnFlbEscape != null)
			BzOnFlbEscape();
		pListView.style.display = 'none';
		break;
	case 37: // VK_LEFT
		break;
	case 39: // VK_RIGHT
		break;
	case 38: // VK_UP
		if (pListView.style.display != "none") {
			var pOptions = document.getElementById(sListView + "_lst").options;
			if (pOptions.length > 0 && pOptions.selectedIndex > 0)
				pOptions.selectedIndex--;
		} else if (pInput.value.length > 0)
			BzOnFlbList(sInput, sListView, arrValue, arrText);
		break;
	case 40: // VK_DOWN
		if (pListView.style.display != "none") {
			var pOptions = document.getElementById(sListView + "_lst").options;
			if (pOptions.length > 0 && pOptions.selectedIndex < pOptions.length - 1)
				pOptions.selectedIndex++;
		//} else if (pInput.value.length > 0)
		} else
			BzOnFlbList(sInput, sListView, arrValue, arrText);
		break;
	case 33: // VK_PAGEUP
		if (pListView.style.display != "none") {
			var pOptions = document.getElementById(sListView + "_lst").options;
			if (pOptions.length > 0 && pOptions.selectedIndex > 0) {
				if (pOptions.selectedIndex > 10)
					pOptions.selectedIndex -= 10;
				else
					pOptions.selectedIndex = 0;
			}
		} else if (pInput.value.length > 0)
			BzOnFlbList(sInput, sListView, arrValue, arrText);
		break;
	case 34: // VK_PAGEDN
		if (pListView.style.display != "none") {
			var pOptions = document.getElementById(sListView + "_lst").options;
			if (pOptions.length > 0 && pOptions.selectedIndex < pOptions.length - 1) {
				if (pOptions.selectedIndex < pOptions.length - 11)
					pOptions.selectedIndex += 10;
				else
					pOptions.selectedIndex = pOptions.length - 1;
			}
		} else if (pInput.value.length > 0)
			BzOnFlbList(sInput, sListView, arrValue, arrText);
		break;
	case 36: // VK_HOME
		if (pListView.style.display != "none") {
			var pOptions = document.getElementById(sListView + "_lst").options;
			if (pOptions.length > 0 && pOptions.selectedIndex > 0)
				pOptions.selectedIndex = 0;
		} else if (pInput.value.length > 0)
			BzOnFlbList(sInput, sListView, arrValue, arrText);
		break;
	case 35: // VK_END
		if (pListView.style.display != "none") {
			var pOptions = document.getElementById(sListView + "_lst").options;
			if (pOptions.length > 0 && pOptions.selectedIndex < pOptions.length - 1)
				pOptions.selectedIndex = pOptions.length - 1;
		} else if (pInput.value.length > 0)
			BzOnFlbList(sInput, sListView, arrValue, arrText);
		break;
	}
}

function BzOnFlbKeyUp(sInput, sListView, arrValue, arrText) {
	var pInput = document.getElementById(sInput);
	switch (event.keyCode) {
	case 13: // VK_RETURN
	case 27: // VK_ESCAPE
	case 37: // VK_LEFT
	case 39: // VK_RIGHT
	case 38: // VK_UP
	case 40: // VK_DOWN
	case 33: // VK_PAGEUP
	case 34: // VK_PAGEDN
	case 36: // VK_HOME
	case 35: // VK_END
		break;
	default:
		if (pInput.value.length > 0)
			BzOnFlbList(sInput, sListView, arrValue, arrText);
		break;
	}
}

///////////////////////////////////////////////////////////////
// Focus Module

function BzOnFocus(pFocus) {
	pFocus.style.imeMode = 'active';
	pFocus.select();
}

function BzOnDateFocus(pFocus) {
	pFocus.style.imeMode = 'inactive';
	pFocus.select();
}

function BzOnNumericFocus(pFocus) {
	pFocus.value = BzGetNumeric(pFocus);
	nDummy = eval(pFocus.value);
	pFocus.style.imeMode = 'inactive';
	pFocus.select();
}

function BzOnNumericBlur(pBlur, nDecimal) {
	pBlur.value = BzNumberFormat(BzGetNumeric(pBlur), nDecimal);
}

function BzOnDisableEnter() {
	if (event.keyCode == 13) event.keyCode = 9;
}

function BzSetNumericColor(pView, nValue) {
	if (nValue > 0)
		pView.style.color = 'black';
	else if (nValue < 0)
		pView.style.color = 'red';
	else
		pView.style.color = 'silver';
}

function BzGetNumeric(pObject) {
	var sValue = pObject.value;
	if (sValue.length > 0)
		sValue = sValue.replace(/[^0-9\-\.]/g, "");
	if (isNaN(sValue) || sValue.length <= 0)
		sValue = 0;
	else if (sValue.length >= 2 && sValue.substr(0, 1) == '0')
		sValue = eval(sValue);
	return parseFloat(sValue);
}

///////////////////////////////////////////////////////////////
// Check Module

function BzClickSelectAll() {
	var allChecked = frmView.sel_all.checked;
	for (n=0; n<frmView.length; n++) {
		var e = frmView.elements[n];
		if (e.type == 'checkbox' && e.name == 'sel_id[]' && e.checked != allChecked) e.checked = allChecked;
	}
}

function BzClickSelect() {
	var bAll = true;
	for (n=0; n<frmView.length; n++) {
		var e = frmView.elements[n];
		if (e.type == 'checkbox' && e.name == 'sel_id[]' && e.checked == false) {
			bAll = false;
			break;
		}
	}
	frmView.sel_all.checked = bAll;
}

function BzCheckSelect(sTitle) {
	var bChecked = false;
	for (n=0; n<frmView.length; n++) {
		var e = frmView.elements[n];
		if (e.type == 'checkbox' && e.name == 'sel_id[]' && e.checked == true) {
			bChecked = true;
			break;
		}
	}
	if (!bChecked) {
		alert(sTitle + "Not selected.");
		return false;
	}
	return true;
}

function BzCheckValue(pTextBox, sTitle)
{
	if (pTextBox.value == "") {
		alert(sTitle + " is required.\nInput your data.");
		pTextBox.focus();
		return false;
	}
	return true;
}

function BzCheckNumericValue(pTextBox, sTitle)
{
	pTextBox.value = BzGetNumeric(pTextBox);
	if (pTextBox.value == "" || pTextBox.value == "0") {
		alert(sTitle + "is required.\nInput your numeric data.");
		pTextBox.focus();
		return false;
	}
	return true;
}

function BzNumberFormat(nSource, nDecimal)
{
	if (nDecimal > 0) {
		var nTmp = Math.pow(10, nDecimal+1);
		var nData = (nTmp * nSource) / 10;
		nTmp /= 10;
		nSource = Math.floor(nData) / nTmp;
	}
	var sSign = '';
	var sSource = nSource.toString();
	var sDecimal = '';
	if (sSource.substring(0, 1) == '-') {
		sSign = '-';
		sSource = sSource.substring(1, sSource.length);
	}
	var arrSource = sSource.split('.');
	for (var n=0; n<arrSource.length; n++) {
		if (n == 0)
			sSource = arrSource[n];
		else if (n == 1)
			sDecimal = arrSource[n];
		else
			break;
	}
	if (sSource.length > 3) {
		for (var n=sSource.length-3; n>0; n-=3)
			sSource = sSource.substring(0, n) + ',' + sSource.substring(n, sSource.length);
	}

	if (nDecimal > 0) {
		sSource += '.' + sDecimal;
		for (var n=sDecimal.length; n<nDecimal; n++) sSource += '0';
	}
	return sSign + sSource;
}

var g_pPopup = null;
function BzViewPopup(sName, sHref, nWidth, nHeight, bScroll)
{
	try {
		g_pPopup.location.href = sHref;
		g_pPopup.focus();
	} catch (e) {
		var sParam = "toolbar=no";
		sParam += ",location=no";
		sParam += ",directories=no";
		sParam += ",status=no";
		sParam += ",menubar=no";
		sParam += ",scrollbars=";
		if (bScroll)
			sParam += "yes";
		else
			sParam += "no";
		sParam += ",resizable=no";
		sParam += ",width=" + nWidth;
		sParam += ",height=" + nHeight;
		sParam += ",left=" + (screen.width / 2 - nWidth / 2);
		sParam += ",top=" + (screen.height / 2 - nHeight / 2);
		g_pPopup = window.open(sHref, sName, sParam);
	}
}

var sOldColor = "";
var sOldBgColor = "";
function BzSetPointer(pObject, sColor, sBgColor)
{
	sOldColor = pObject.style.color;
	sOldBgColor = pObject.style.backgroundColor;
	pObject.style.color = sColor;
	pObject.style.backgroundColor = sBgColor;
	pObject.style.cursor = "hand";
}

function BzSetOldPointer(pObject)
{
	pObject.style.color = sOldColor;
	pObject.style.backgroundColor = sOldBgColor;
	pObject.style.cursor = "default";
}

///////////////////////////////////////////////////////////////
// Images Module

function BzPreloadImages() { //v3.0
	var d = document;
	if (d.images) {
		if (!d.g_pBzPre)
			d.g_pBzPre = new Array;
		var i, j = d.g_pBzPre.length, a = BzPreloadImages.arguments;
		for (i=0; i<a.length; i++) {
			if (a[i].indexOf("#") != 0) {
				d.g_pBzPre[j] = new Image;
				d.g_pBzPre[j++].src = a[i];
			}
		}
	}
}

function BzSwapImgRestore() { //v3.0
	var i, x, a = document.g_pBzSwp;
	for (i=0; a&&i<a.length&&(x = a[i])&&x.oSrc; i++)
		x.src = x.oSrc;
}

function BzFindObj(n, d) { //v3.0
	var p, i, x;
	if (!d)
		d = document;
	if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
		d = parent.frames[n.substring(p+1)].document;
		n = n.substring(0, p);
	}
	if (!(x = d[n]) && d.all)
		x = d.all[n];
	for (i=0; !x&&i<d.forms.length; i++)
		x = d.forms[i][n];
	for (i=0; !x&&d.layers&&i<d.layers.length; i++)
		x = BzFindObj(n, d.layers[i].document);
	return x;
}

function BzSwapImage() { //v3.0
	var i, j = 0, x, a = BzSwapImage.arguments;
	document.g_pBzSwp = new Array;
	for (i=0; i<(a.length-2); i+=3) {
		if ((x = BzFindObj(a[i])) != null) {
			document.g_pBzSwp[j++] = x;
			if (!x.oSrc)
				x.oSrc = x.src;
			x.src = a[i+2];
		}
	}
}
