﻿// JScript File
var hdnSelectedRecordCountCltId = "";
var lblSelectedRecordCountCltId = "";
var currentActiveRow = null;
var defaultRowStyle = "grid_row";
var alternateRowStyle = "grid_alternating_row";
var activeRowStyle = "grid_active_row";
var selectedRowStyle = "";
var isCapslock = false;
var tagColIndex = -1;

function MM_swapImgRestore()
{ //v3.0
	var i, x, a = document.MM_sr; for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) x.src = x.oSrc;
}

function MM_preloadImages()
{ //v3.0
	var d = document; if (d.images)
	{
		if (!d.MM_p) d.MM_p = new Array();
		var i, j = d.MM_p.length, a = MM_preloadImages.arguments; for (i = 0; i < a.length; i++)
			if (a[i].indexOf("#") != 0) { d.MM_p[j] = new Image; d.MM_p[j++].src = a[i]; }
	}
}

function MM_findObj(n, d)
{ //v4.01
	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 = MM_findObj(n, d.layers[i].document);
	if (!x && d.getElementById) x = d.getElementById(n); return x;
}

function MM_swapImage()
{ //v3.0
	var i, j = 0, x, a = MM_swapImage.arguments; document.MM_sr = new Array; for (i = 0; i < (a.length - 2); i += 3)
		if ((x = MM_findObj(a[i])) != null) { document.MM_sr[j++] = x; if (!x.oSrc) x.oSrc = x.src; x.src = a[i + 2]; }
}

function UpdateSelectedRecordCount(objCheckBox)
{
	var hdnSelectedRecordCount = document.getElementById(hdnSelectedRecordCountCltId);
	var lblSelectedRecordCount = document.getElementById(lblSelectedRecordCountCltId);
	if (objCheckBox.checked)
	{
		hdnSelectedRecordCount.value = parseInt(hdnSelectedRecordCount.value) + 1;
	} else
	{
		hdnSelectedRecordCount.value = parseInt(hdnSelectedRecordCount.value) - 1;
	}
	lblSelectedRecordCount.innerHTML = hdnSelectedRecordCount.value;
}

/************************************************************************/

function CancelUpAndDownKeyEvent(e)
{
	var keyCode;
	var isValidEvent = true;
	if (!e)
	{
		e = window.event;
	}
	if (window.event)
	{// IE 
		keyCode = e.keyCode;
	} else if (e.which)
	{ // Netscape/Firefox/Opera
		keyCode = e.which;
	}

	switch (keyCode)
	{
		case 40:
		case 38:
			isValidEvent = false;
			break;
	}
	if (e.returnValue)
		e.returnValue = isValidEvent;
	return isValidEvent;
}

function getCheckbox(row, keyColIndex)
{
	var chk = null;
	var cell = row.cells[keyColIndex];
	for (var j = 0; j < cell.childNodes.length; j++)
	{
		var child = cell.childNodes[j];
		if (child.type)
		{
			if (child.type == "checkbox")
			{
				chk = child;
				break;
			}
		}
	}
	if (!chk)
	{
		for (var j = 0; j < cell.childNodes.length; j++)
		{
			var child = cell.childNodes[j];
			if (child.tagName == "SPAN")
			{
				for (var i = 0; i < child.childNodes.length; i++)
				{
					var spanChild = child.childNodes[i];
					if (spanChild.type == "checkbox")
					{
						chk = spanChild;
						break;
					}
				}
				break;
			}
		}
	}
	return chk;
}

function changeActiveRow(row, keyId, e)
{
	if (!e)
	{
		e = window.event;
	}
	var keyCode;
	if (window.event)
	{// IE 
		keyCode = e.keyCode;
	} else if (e.which)
	{ // Netscape/Firefox/Opera
		keyCode = e.which;
	}
	switch (keyCode)
	{
		case 40: // Down-Arrow Key
			if (row.nextSibling && row.nextSibling.tagName == "TR")
				changeRowStyle(row.nextSibling, keyId, e)
			break;
		case 38: // Up-Arrow Key
			if (row.rowIndex > 1)
				changeRowStyle(row.previousSibling, keyId, e)
			break;
		case 35: // End Key
			var table = row.parentNode;
			changeRowStyle(table.rows[table.rows.length - 1], keyId, e);
			break;
		case 36: // Home Key
			var table = row.parentNode;
			if (table.rows.length > 1)
				changeRowStyle(table.rows[1], keyId, e);
			break;
	}
}

function changeRowStyle(row, keyColIndex, e)
{
	var targ;
	if (!e)
	{
		e = window.event;
	}
	if (e)
	{
		if (e.target)
		{
			targ = e.target;
		} else if (e.srcElement)
		{
			targ = e.srcElement;
		}
		if (targ.nodeType == 3)
		{ // defeat Safari bug
			targ = targ.parentNode;
		}
	}
	if (row)
	{
		// BE If only one row tag doesn't work
		//if(currentActiveRow == null || (currentActiveRow != null && currentActiveRow.rowIndex != row.rowIndex)) {
		if (row.className != "State1" && row.className != "State2" && row.className != "State3")
		{
			if (keyColIndex != null)
			{
				if (currentActiveRow)
				{
					if (selectedRowStyle != "" && getCheckbox(currentActiveRow, keyColIndex).checked)
						currentActiveRow.className = selectedRowStyle;
					else
					{
						oDefaultClass = currentActiveRow.attributes.getNamedItem("defaultClass");
						if (oDefaultClass)
						{
							currentActiveRow.className = oDefaultClass.value;
						} else
						{
							if (currentActiveRow.rowIndex % 2 == 0)
								currentActiveRow.className = alternateRowStyle;
							else
								currentActiveRow.className = defaultRowStyle;
						}
					}
				}
				if (e && targ.type)
				{
					if (targ.type == "checkbox")
					{
						var chk = getCheckbox(row, keyColIndex);
						if (!chk.disabled)
							getCheckbox(row, keyColIndex).focus();
						else
							row.cells[0].setActive();
					}
				} else
				{
					var chk = getCheckbox(row, keyColIndex);
					if (chk && !chk.disabled)
						getCheckbox(row, keyColIndex).focus();
					else
						row.cells[0].setActive();
				}
			} else
			{
				if (currentActiveRow)
				{
					if (currentActiveRow.rowIndex % 2 == 0)
						currentActiveRow.className = alternateRowStyle;
					else
						currentActiveRow.className = defaultRowStyle;
				}
				if (!e || !e.srcElement || !e.srcElement.type)
					row.cells[0].setActive();
			}

			if (currentActiveRow && tagColIndex > 0 && row.cells.length > 0)
			{
				var tag = '';
				var hiddenField = document.getElementById(hdnTagCltId);
				if (hiddenField != null)
					tag = hiddenField.value;

				var cell = row.cells[tagColIndex];
				if (cell != null)
					UpdateTags(cell, tag);
			}

			currentActiveRow = row;
			row.className = activeRowStyle;
		}
		//}
	}
}

function Load()
{
	Resize();
}

function Resize()
{
	var tbcTopMenu = document.getElementById('tbcTopMenu');
	var pnlContent = document.getElementById('ctl00_pnlContentMain');
	var tbcContent = document.getElementById('ctl00_tbcContentMain');
	var tbcStatusbar = document.getElementById('tbcStatusbar');
	var tblMenuCtrl = document.getElementById('ctl00_tbcLeftMenu');
	var navBar = document.getElementById('ctl00_NavBar1');
	var tbrContentTitle = document.getElementById('ctl00_tbrContentTitle');
	var tbrContentToolbar = document.getElementById('ctl00_tbrContentToolbar');
	var tbrContentAction = document.getElementById('ctl00_tbrContentAction');
	var hdnContentMainDefaultWidth = document.getElementById('ctl00_hdnContentMainDefaultWidth');
	var hdnContentMainDefaultHeight = document.getElementById('ctl00_hdnContentMainDefaultHeight');

	if (pnlContent)
	{
		//if (false) {
		var offsetWidth = 0;
		var offsetHeight = 0;
		var scrollbarRightWidth = 0;
		var scrollbarLeftHeight = 0;

		if (document.all)
		{
			if (!window.opener)
				scrollbarRightWidth = 17;
			scrollbarLeftHeight = 0;
			offsetWidth = document.body.offsetWidth;
			offsetHeight = document.body.offsetHeight;
		} else
		{
			scrollbarRightWidth = 23;
			scrollbarLeftHeight = 8;
			offsetHeight = window.innerHeight;
			offsetWidth = window.innerWidth;
		}

		offsetWidth -= scrollbarRightWidth;
		offsetHeight -= scrollbarLeftHeight;

		if (tbcTopMenu)
			offsetHeight = offsetHeight - parseInt(tbcTopMenu.offsetHeight);

		if (tbcStatusbar)
			offsetHeight = offsetHeight - parseInt(tbcStatusbar.offsetHeight);

		if (tbrContentAction)
		{
			offsetHeight = offsetHeight - parseInt(tbrContentAction.offsetHeight);
			offsetHeight = offsetHeight - 1; /* border */
		}

		if (tbrContentTitle)
		{
			offsetHeight = offsetHeight - parseInt(tbrContentTitle.offsetHeight);
			offsetHeight = offsetHeight - 1; /* border */
		}

		if (tbrContentToolbar)
		{
			offsetHeight = offsetHeight - parseInt(tbrContentToolbar.offsetHeight);
			offsetHeight = offsetHeight - 1; /* border */
		}

		if (navBar)
		{
			tblMenuCtrl.style.width = parseInt(navBar.offsetWidth);
		}

		if (tblMenuCtrl)
		{
			offsetWidth = offsetWidth - parseInt(tblMenuCtrl.offsetWidth);
		}

		if (offsetHeight < 200)
			offsetHeight = 200;

		if (offsetWidth < 320)
			offsetWidth = 320;

		offsetWidth = offsetWidth - 8 /* border */;
		offsetHeight = offsetHeight - 8 /* border */;

		if (hdnContentMainDefaultWidth)
			hdnContentMainDefaultWidth.value = offsetWidth;
		if (hdnContentMainDefaultHeight)
			hdnContentMainDefaultHeight.value = offsetHeight;

		pnlContent.style.width = offsetWidth;
		pnlContent.style.height = offsetHeight;
		tbcContent.style.height = offsetHeight;
		if (parseInt(tbcContent.offsetHeight) > parseInt(pnlContent.offsetHeight))
			pnlContent.style.height = parseInt(tbcContent.offsetHeight);

	}
}
/************************************************************************/

function pageLoad()
{
	Resize();
}

/************************************************************************/

function PopupWindow(sURL, sWinName, iWidth, iHeight, bIsScrollbars, bIsResizable, bIsFullScreen, iTop_)
{

	iWidth = Math.round(iWidth);
	iHeight = Math.round(iHeight);

	iLeft = Math.round((window.screen.width - iWidth) / 2);
	iTop = Math.round((window.screen.height - iHeight) / 2);
	if (iTop_ >= 0) iTop = iTop_;

	bIsScrollbars = bIsScrollbars ? '1' : '0';
	bIsResizable = bIsResizable ? '1' : '0';
	bIsFullScreen = bIsFullScreen ? '1' : '0';

	sExt = 'toolbar=no,menubar=no,directories=no,location=no,titlebar=no,status=no,channelmode=no';
	sExt += ',scrollbars=' + bIsScrollbars + ',resizable=' + bIsResizable;
	//sExt += ',fullscreen=' + bIsFullScreen;
	sExt += ',top=' + iTop + ',left=' + iLeft + ',width=' + iWidth + ',height=' + iHeight;

	//alert(sExt);
	var obj = window.open(sURL, sWinName, sExt, false);
	if (obj)
		obj.focus();
	return obj;

}
/************************************************************************/

function OpenWizard(tableName, ctlFieldId, ctlValueId)
{
	var ctlValue = document.getElementById(ctlValueId).value;
	var ctlField = document.getElementById(ctlFieldId);
	var fieldName = ctlField.value;
	var title = ctlField.options[ctlField.selectedIndex].innerText;
	PopupWindow("Wizard.aspx?tableName=" + tableName + "&fieldName=" + fieldName + "&ctlValueId=" + ctlValueId + "&ctlValue=" + ctlValue + "&title=" + title, "Wizard", 520, 320);
}

function OpenWizardHelp(ctlFieldId)
{
	var ctlField = document.getElementById(ctlFieldId);
	var helpResId = ctlField.options[ctlField.selectedIndex].attributes.getNamedItem("helpResId");
	if (helpResId)
	{
		PopupWindow("WizardHelp.aspx?resId=" + helpResId.value, "WizardHelp", 520, 320);
	}
}

function OpenHelp()
{
	PopupWindow("Help.aspx", "Help", 520, 320);
}

function OpenWacthInstructionDetail(wtwId)
{
	PopupWindow("WatchInstructionDetail.aspx?wtwId=" + wtwId, "WatchInstructionDetail", 640, 480, false, true, false);
}

function OpenPopupDevice(blbId, tmbNum)
{
	if (!tmbNum)
		PopupWindow("PopupDevice.aspx?blbId=" + blbId + "&width=620&height=480", "WacthInstructionDevice", 640, 480, false, false, false);
	else
		PopupWindow("PopupDevice.aspx?regId=" + blbId + "&tmbNum=" + tmbNum + "&width=620&height=480", "WacthInstructionDevice", 640, 480, false, false, false);
}

function OpenWacthInstructionReport(wtwId)
{
	PopupWindow("WatchInstructionReport.aspx?wtwId=" + wtwId, "WatchInstructionReport", 960, 690, false, true, false);
}

function OpenWizardNice(ctlValueId)
{
	var ctlValue = document.getElementById(ctlValueId).value;
	PopupWindow("WizardNice.aspx?ctlValueId=" + ctlValueId + "&ctlValue=" + ctlValue, "WizardNice", 620, 360);
}

function SearchInstructionToRepository(srwId)
{
	PopupWindow("SearchInstructionToRepository.aspx?srwId=" + srwId, "SearchInstructionToRepository", 620, 360);
}

function ShowTagsDescription(moduleName)
{
	PopupWindow("TagsDescription.aspx?moduleName=" + moduleName, "ShowTagsDescription", 620, 360);
}

function ExportQueryResults(moduleName)
{
	PopupWindow("ExportQueryResults.aspx?moduleName=" + moduleName, "ExportQueryResults", 400, 300);
}

function DownloadQueryResults(moduleName)
{
	PopupWindow("DownloadQueryResults.aspx?moduleName=" + moduleName, "DownloadQueryResults", 400, 300);
}

function ProfileDetailsPopUp(p_UserId)
{
	var iWidth = 960;
	PopupWindow("ProfileDetails.aspx?UserId=" + p_UserId, "ProfileDetails", iWidth, 480, false, true);
}

function SetValue(value, ctlValueId)
{
	window.opener.document.getElementById(ctlValueId).value = value;
}

function ShowNiceDescription(ctlNicId, ctlLabelId)
{
	document.getElementById(ctlLabelId).innerHTML = document.getElementById(ctlNicId).value;
}

function SynchronizeHiddenField(ctlCblId, ctlHdnId)
{
	var inputArray = document.getElementsByTagName('input');
	var valueList = "";
	for (var inputIndex = 0; inputIndex < inputArray.length; inputIndex++)
	{
		var input = inputArray[inputIndex];
		if (input.type == "checkbox")
		{
			if (input.id.indexOf(ctlCblId) > -1)
			{
				if (input.checked)
				{
					if (input.nextSibling)
					{
						if (input.nextSibling.tagName == 'LABEL')
						{
							valueList += (valueList != "") ? ',' : '';
							valueList += input.nextSibling.innerHTML;
						}
					}
					else
					{
						valueList += (valueList != "") ? ',' : '';
						valueList += input.value;
					}
				}
			}
		}
	}
	document.getElementById(ctlHdnId).value = valueList;
}

function PageNumber_KeyDown(e)
{
	var keyCode;
	var isValidKey = true;
	if (!e)
	{
		e = window.event;
	}
	if (window.event)
	{// IE 
		keyCode = e.keyCode;
	} else if (e.which)
	{ // Netscape/Firefox/Opera
		keyCode = e.which;
	}
	if (keyCode == 20)
		isCapslock = !isCapslock;

	isValidKey = (
		keyCode == 8 || keyCode == 13 || keyCode == 16 ||
		keyCode == 17 || keyCode == 20 || keyCode == 40 ||
		(keyCode >= 48 && keyCode <= 57 && !isNaN((String.fromCharCode(keyCode)))) ||
		(keyCode >= 96 && keyCode <= 105)
		)

	if (window.event)
		event.returnValue = isValidKey;

	return isValidKey;
}