function pushToOptSel(prefixId, index)
{
	var tbxState = document.getElementById(prefixId + 'tbOptSel');
	var opt = document.getElementById(prefixId + 'Cb' + index);
	var optArray;
	if (tbxState.value.length > 0)
		optArray  = tbxState.value.split(',');
	else
		optArray = new Array();
	var maxSelOpt = eval(prefixId + 'MaxOpt');
	
	if (maxSelOpt == -1 || optArray.length < maxSelOpt)
	{
		optArray[optArray.length] = index;
		opt.checked = true; 		
		if (tbxState.value.length > 0)
			tbxState.value += ",";
		tbxState.value += index;
	}
	if (maxSelOpt != -1 && optArray.length >= maxSelOpt)
		SetDisableAttribute(prefixId, true);	
}
			
function removeFromOptSel(prefixId, index)
{
	var tbxState = document.getElementById(prefixId + 'tbOptSel');
	var optArray  = tbxState.value.split(',');
	var maxSelOpt = eval(prefixId + 'MaxOpt');
	if (maxSelOpt != -1 && optArray.length == maxSelOpt)
		SetDisableAttribute(prefixId, false);
	for(var i = 0; i < optArray.length; i++)
		if(optArray[i] == index) 
			break;
	if (i <  optArray.length)
	{
		for(var j = i + 1; j < optArray.length; j++)
		{
			optArray[j-1] = optArray[j];
		}
		optArray.length = optArray.length -1;
		tbxState.value = "";
		for(i = 0; i < optArray.length; i++)
		{
			if (tbxState.value.length > 0)
					tbxState.value += ",";
			tbxState.value += optArray[i];
		}
	}
	document.getElementById(prefixId + 'Cb' + index).checked = false; 		 
}

function findValue(prefixId, index)
{
	var optArray  = document.getElementById(prefixId + 'tbOptSel').value.split(',');
	for(var i = 0; i < optArray.length; i++)
	{
		if(optArray[i] != '')
		{
			if (optArray[i] == index)
			{
				return true;
			}
		}
	}
	return false;
}

function inspectOnClick(prefixId, index)
{
	var optClicked = document.getElementById(prefixId + 'Cb'+ index);
	var textbox = document.getElementById(prefixId + 'Tb'+ index);
	var isChecked = findValue(prefixId, index);
	if (!isChecked)
	{
		//is checking in a new item
		if (textbox != null  && !textbox.disabled)
			textbox.focus();
		pushToOptSel(prefixId ,index); 						
	}
	else
	{
		//is checking out an  item selected
		if(textbox != null) //have it got an textbox associated ?
		{
			//yes, it has.
			if (textbox.value == "")// is blank the textbox associated ?
			{
				//remove it from item selecteds options
				removeFromOptSel(prefixId ,index);
			}
			else
			{
				//set focus on textbox associated and inhibit the cheking out
				textbox.focus();
				optClicked.checked = true;
			}
		}
		else
		{
			//remove it from item selecteds options
			removeFromOptSel(prefixId ,index);
		}
	}
}

function SetDisableAttribute(prefixId, isDisabling)
{
	var Opts = document.getElementsByTagName('INPUT');
	if (isDisabling)
	{
		for(var j=0; j < Opts.length; j++)
		{
			if (Opts[j].id.indexOf(prefixId) >= 0)
			{
				if (Opts[j].type == 'checkbox')
				{  
					if (!Opts[j].checked)
						Opts[j].disabled = true;
				}
				else
					if (Opts[j].type == 'text')
					{
						if(Opts[j].id.indexOf(prefixId + 'tbOptSel') == -1 && Opts[j].id.indexOf(prefixId + 'tbViewState') == -1)
						{
							var option =  document.getElementById(Opts[j].id.replace('Tb','Cb')); 
							if ( option != null && !option.checked)
							{
								Opts[j].disabled = true;
							}
						}
					}
			}
		}
		Opts = document.getElementsByTagName('SPAN');
		for(j=0; j < Opts.length; j++)
		{
			if (Opts[j].id.indexOf(prefixId) >= 0)
			{
				checkBoxId = Opts[j].id.replace('Lb','Cb'); 
				if (!document.getElementById(checkBoxId).checked)
				{
					Opts[j].disabled = true;
				}	
			}
		}
		
	}
	else
	{
		for(var j=0; j < Opts.length; j++)
		{
			if (Opts[j].id.indexOf(prefixId) >= 0)
			{
				Opts[j].disabled = false;
			}
		}
		Opts = document.getElementsByTagName('SPAN');
		for(j=0; j < Opts.length; j++)
		{
			if (Opts[j].id.indexOf(prefixId) >= 0)
			{
				Opts[j].disabled = false;		
			}
		}
	}
}

function tbxLostFocus(tbId, index)
{	
	var textbox = document.getElementById(tbId);
	if (textbox.value == "")
	{
		removeFromOptSel( tbId.substring(0,tbId.indexOf("Tb")), index);
	}
}

