var DurItemPos = {IntValue:0, DispValue:1, mfCtrl:2};
function ddigit2(n) {return (n>9)? ""+n : "0"+n;}

function DurLoop(ctrlName, isDown) //mei
{
	DurChange(ctrlName, isDown);
	gTTret = setTimeout('DurLoop("'+ctrlName+'",'+isDown+')', gTTmsec);
	gTTmsec = 100;
}

function DurKeyDN(ctrlName)
{
	var code = event.keyCode;
	if(code == 38 || code == 40) // up
	{
		DurChange(ctrlName, (code == 40) ? 1 : 0);
		event.returnValue = false;
		return;
	}

	if(code == 8 || //back
		code == 9 ||  //tab
		code == 46 || //delete
		code == 37 || //left
		code == 39 //right
		)
		return;
	if(!((code >= 48 && code <= 57) || (code >= 96 && code <= 105)))
	{
		event.returnValue = false;
		return;
	}
}

function SetDurFocus(ctrlName)
{
	var obj = document.getElementsByName(ctrlName);
	obj[DurItemPos.DispValue].select();
}

function DurChange(ctrlName, isDown)
{
	var obj = document.getElementsByName(ctrlName);
	var tmp = parseInt(obj[DurItemPos.IntValue].value);
	if(isDown == 0)
	  tmp = (tmp < 100) ? tmp+1 : 100;
	else
		tmp = (tmp > 1) ? tmp-1 : 1;

	SetDispValue(obj, tmp);
	obj[DurItemPos.IntValue].value = tmp;
	obj[DurItemPos.DispValue].select();
	if(obj[DurItemPos.mfCtrl].value != "")
	{
		var obj2 = document.getElementById(obj[DurItemPos.mfCtrl].value);
		obj2.value = 1;
	}
}

function SetDispValue(obj, nValue)
{
	if(nValue == 100)
	  obj[DurItemPos.DispValue].value = "PERMANENT";
	else
	  obj[DurItemPos.DispValue].value = ddigit2(nValue) + " SEC";
}

function SetDurValue(ctrlName, nValue)
{
	var obj = document.getElementsByName(ctrlName);
	nValue = (nValue < 1) ? 1 : ((nValue > 100) ? 100 : nValue);
	SetDispValue(obj, nValue);
	obj[DurItemPos.IntValue].value = nValue;	
}

function GetDurValue(ctrlName)
{
	var obj = document.getElementsByName(ctrlName);
	return parseInt(obj[DurItemPos.IntValue].value);
}

function disableDurCtrl(ctrlName, bDisable)
{
	var obj = document.getElementsByName(ctrlName);
	for(i=0;i<obj.length;i++)
		obj[i].disabled = bDisable;
}

function HideDurCtrl(ctrlName, bHide)
{
	var obj = document.getElementById("tbl"+ctrlName);
	obj.style.visibility = bHide ? "hidden" : "visible";
}

function AddDurationCtrl(ctrlName, nValue, mfCtrl) // ctrlName, default value, modify flag control
{
	var tblName = "tbl"+ctrlName;
  var tmHtml = '<table cellspacing="0" id='+tblName+' cellpadding="0" style="border:0px ridge"><tr><td>';
  tmHtml += '<table cellspacing="0" cellpadding="0" bordercolor=#B5CBD0 style="border:1px ridge; font-weight: bold; font-size: 12px; font-family: Arial; height: 18px">';
  tmHtml += '<td><input name='+ctrlName+' type=hidden value="0"></td><tr>';
  tmHtml += '<td rowspan=2><input name='+ctrlName+' type=text value="0" readonly style="font-size: 12px; font-family: Arial; width:80px; border-width:0px;" onfocus="SetDurFocus(\''+ctrlName+'\')" onkeydown="DurKeyDN(\''+ctrlName+'\')"></td>';
  tmHtml += '<td rowspan=2><input name='+ctrlName+' type=hidden value="' + mfCtrl + '"></td>';
  
  //mei
  tmHtml += '<td rowspan="2"><input name='+ctrlName+' type=text style="font-size: 12px; font-family: Arial; width: 22px; border-width:0px" onfocus="SetTTpos(\''+ctrlName+'\', 6)" onkeydown="gNewSysTime=0; TTDNam(\''+ctrlName+'\');"></td>';
  tmHtml += '<td height="50%"><input name='+ctrlName+' type=image src=up.gif onmousedown="gTTmsec=500;DurLoop(\''+ctrlName+'\', 0);"></td>';
  tmHtml += '<tr><td height="50%"><input name='+ctrlName+' type=image src=down.gif onmousedown="gTTmsec=500;DurLoop(\''+ctrlName+'\', 1);"></td></tr>';
  tmHtml += '<td><input name='+ctrlName+' type=hidden value="3"></td>';
  tmHtml += '<td><input name='+ctrlName+' type=hidden value="' + mfCtrl + '"></td>';
  
  tmHtml += '</tr></table>';
  tmHtml += '</td></tr></table>';
  document.write(tmHtml);
	var obj = document.getElementsByName(ctrlName);
	obj[DurItemPos.DispValue].maxLength = 10;
	SetDispValue(obj, nValue);
	obj[DurItemPos.IntValue].value = nValue;
}
