function hightLight(oChk)
{
	while (oChk.tagName!="TR")
	{
		oChk = oChk.parentElement;
	}
	oChk.className = "H";
}
function lowLight(oChk)
{
	var nID;
	nID = oChk.opt;
	while (oChk.tagName!="TR")
	{
		oChk = oChk.parentElement;
	}
	oChk.className = 'row' + nID;
}
function toggleLight(oChk)
{
	if (oChk.name != "chkSelectAll")
	{
		if (oChk.checked)
			hightLight(oChk);
		else
			lowLight(oChk);
	}
}
function restoreSelected(slct, id)
{
	var i
	for(i = 0; i < slct.length; i++)
	{
		if(id == slct.options[i].value)
		{
			slct.selectedIndex = i;
			break;
		}
	}
	return (i < slct.length);
}

function setFocus(obj)
{
	if (!obj.disabled)
		obj.focus();
}
function setFocusEx(objArr, index)
{
	var myindex = 0;
	if(index)
		myindex = index;
		
	var obj = getAtIndex(objArr, myindex);
	setFocus(obj);
}

function selectAll(objCheckArray, objRadioArray, checked, dim, subindex)
{
	var i = 0;
	if(objCheckArray)
	{
		if(objCheckArray.length)
		{
			for(i = 0; i < objCheckArray.length; i++)
			{
				if(objCheckArray[i].CheckGroup)
				{
					if(!objCheckArray[i].disabled)
					{
						objCheckArray[i].checked = checked;
							
						if(null == objRadioArray)
							rowColorCheck(i, objCheckArray[i], dim, subindex);
						else
							rowColorCheckRadio(i, objCheckArray[i], objRadioArray[i], subindex);
							
					}
				}
			}
		}
		else
		{
			if(objCheckArray.CheckGroup)
			{
				if(!objCheckArray.disabled)
				{
					objCheckArray.checked = checked;
							
					if(null == objRadioArray)
						rowColorCheck(i, objCheckArray, dim, subindex);
					else
						rowColorCheckRadio(i, objCheckArray, objRadioArray, subindex);
							
				}
			}
		}
		setDirty(true);
	}
}

function selectAllGrouped(objCheckArray, objRadioArray, CheckGroup, checked, dim, subindex)
{
	var i = 0;
	// update all the children of this group node to be like it
	// update the parent according to the status of it and all its sublings
	
	if(objCheckArray.length)
	{
		for(i = 0; i < objCheckArray.length; i++)
		{
			if(objCheckArray[i].CheckGroup == CheckGroup)
			{
				if(!objCheckArray[i].disabled)
				{
					objCheckArray[i].checked = checked;
					if(null == objRadioArray)
						rowColorCheck(i, objCheckArray[i], dim, subindex);
					else
						rowColorCheckRadio(i, objCheckArray[i], objRadioArray[i], subindex);
								
					selectAllGrouped(objCheckArray, objRadioArray, objCheckArray[i].value.toString() +  CheckGroup.toString(), checked, dim, subindex)
				}
			}
		}
	}
	else
	{
		if(objCheckArray.CheckGroup)
		{
			if(!objCheckArray.disabled)
			{
				objCheckArray.checked = checked;
						
				if(null == objRadioArray)
					rowColorCheck(i, objCheckArray, dim, subindex);
				else
					rowColorCheckRadio(i, objCheckArray, objRadioArray, subindex);
						
			}
		}
	}
	setDirty(true);
}

function getObjIndex(objarr, obj)
{
	var index
	for(index = 0; index<objarr.length; index++)
	{
		if(objarr[index] == obj)
		{
			return index;
		}
	}
	return -1;
}

function getCheckedList(arrObj, Level)
{
	var i, j = 0;
	var list = new Array();
	
	if(arrObj.length)
	{
		for(i = 0; i < arrObj.length; i++)
		{
			if (arrObj[i].checked)
			{
				if (Level)
				{
					if(arrObj[i].Level==Level)
					{
						list[j++] = arrObj[i];
					}
				}
				else
				{
					list[j++] = arrObj[i];
				}
			}
		}
	}
	else
	{
		if (arrObj.checked)
		{
			list[0] = arrObj;
		}
	}
	return list;
}

function getAtIndex(arrObj, index)
{
	var obj;
	if(arrObj)
	{
		if(arrObj.length)
			obj = arrObj[index]
		else
			obj = arrObj;
	}
	return obj;
}

var g_useStrips = true;
var g_useColors = false;

function enableStrips(val)
{
	g_useStrips = val;
}
function rowColor(row, arrObj, index, dim)
{
	var ret;
	if(row)
	{
		var obj = getAtIndex(arrObj, index);
		if(obj)
		{
			ret = obj.checked;
			if(g_useColors)
			{
				if(obj.checked)
				{
					if(dim)
						row.className = 'rowDim';
					else
						row.className = 'rowSel';
				}
				else
				{
					if(g_useStrips)
						row.className = 'row' + index % 2;
					else
						row.className = '';
				}
			}
		}
	}
	return ret;
}

function getTR(index, subindex)
{
	var obj = null;
	
	var trRow = eval('trRow' + index);
	if(trRow)
		obj = getAtIndex(trRow, subindex);
		
	return obj;
}

function rowColorCheck(index, chk, dim, subindex)
{
	var ret
	var trRow = getTR(index, subindex);
	if(trRow)
		ret = rowColor(trRow, chk, index, dim);
		
	return ret;
}

function rowColorCheckRadio(index, rad, chk, subindex)
{
	var obj = getAtIndex(rad, index);
	if(!obj.checked)
	{
		var trRow = getTR(index, subindex);
		rowColor(trRow, chk, index, true);
	}
}

function rowColorRadioCheck(index, lastIndex, rad, chk, subindex)
{
	if(index == lastIndex)
		return;
	var trRow = getTR(index, subindex);
	rowColor(trRow, rad, index, false);
	if(null != lastIndex)
	{
		trRow = getTR(lastIndex, subindex);
		rowColor(trRow, chk, lastIndex, true);
	}
}
//////////////////////////////////////

function orderOpt(obj, dir)
{
	if (-1 == obj.selectedIndex)
		return false;
		
	var newIndex = -1;
	if('up' == dir)
	{
		if(obj.selectedIndex > 0)
		newIndex = obj.selectedIndex-1;
	}
	else if(obj.selectedIndex < obj.length - 1)
		newIndex = obj.selectedIndex + 2;
	
	if(-1 != newIndex)
	{
		obj.insertBefore(obj.options[obj.selectedIndex], obj.options[newIndex]);
		return true;
	}
	return false;
}

function moveOpt(oSrc, oDst, all)
{
	if(all)
	{
		var i;
		for (i = 0; i < oSrc.length; i++)
		{
			if(moveSingleOpt(oSrc, oDst, i))
				i--;
		}

	}
	
	else
	{
		for (i = 0; i < oSrc.length; i++)
		{
			if(oSrc.options[i].selected)
			{
				if(moveSingleOpt(oSrc, oDst, i))
					i--;
			}
		}
	}
}

function moveSingleOpt(oSrc, oDst, index)
{
	// check for max count
	if(oDst.MaxCount)
	{
		if((oDst.length >= oDst.MaxCount) && (0 < oDst.MaxCount))
			return false;
	}

	var opt = oSrc.options[index];
	
	var headerExist = false;
	var thisHeader = null;
	var dublicate = false;
	var nodBefore = null;
	
	// check if it is valid to move
	if(oDst.CheckList && opt.CheckValue)
	{
		var arrList = oDst.CheckList.split(',')
		var x;
		for(x = 0; x<arrList.length; x++)
		{
			if (arrList[x] == opt.CheckValue)
				break;
		}
		if(x == arrList.length)
			return false;
	}
	
	// special handle for the negative items that to be deleted only and not moved to the DST
	if(opt.value < 0)
	{
		opt.removeNode();
		return true;
	}
	
	if(opt.GroupHead)
	{
		for(i= oDst.options.length - 1; i >= 0; i--)
		{
			var curOpt = oDst.options[i]; 
			if(curOpt.GroupHead)
			{
				if(curOpt.value == opt.value)
				{
					headerExist = true;
					thisHeader = curOpt;
					break;		
				}
			}
		}
		dublicate = true;
	}
	else
	{		
		var i
		// scan from the end
		for(i= oDst.options.length - 1; i >= 0; i--)
		{
			var curOpt = oDst.options[i]; 
			// special handle here for the negative items
			//if(curOpt.value < 0)
			//{
			//	opt.removeNode();
			//}
			if(curOpt.GroupHead)
			{
				if(curOpt.value == opt.GroupValue)
				{
					headerExist = true;
					if(i < oDst.options.length - 1)
					{
						nodBefore = oDst.options[i+1];
						break;		
					}
				}
			}
			//else 
			else if(curOpt.GroupValue)		// this is the added check to prevent adding the node before the last and making it be added at  the last (end)
			{
				if(curOpt.GroupValue == opt.GroupValue)
				{
					headerExist = true;
					if(i < oDst.options.length - 1)
					{
						nodBefore = oDst.options[i+1];
						break;
					}
				}
			}
		}
	}
		
	if(dublicate)
	{
		
		if(!headerExist)
		{
			var newOpt = dublicateNode(opt);
			oDst.insertBefore(newOpt, nodBefore);
			newOpt.innerText = opt.innerText;
		}
		
		////////////////////
		//it is a header node to move all its childs
		for(i = 0; i < oSrc.options.length; i++)
		{
			var curOpt = oSrc.options[i]; 
			if(!(curOpt.GroupHead))
			{
				if(curOpt.GroupValue == opt.value)
				{
					oDst.insertBefore(oSrc.options[i], null);
					i--;
				}
			}
		}			
		opt.removeNode();
	}
	else
	{
		if(!headerExist)
		{
			for(i= oSrc.options.length - 1; i >= 0; i--)
			{
				var curOpt = oSrc.options[i]; 
				if(curOpt.GroupHead)
				{
					if(curOpt.value == opt.GroupValue)
					{
						break;
					}
				}
			}
			if(i >= 0 )
			{
				var newOpt = dublicateNode(curOpt);
				oDst.insertBefore(newOpt, nodBefore);
				newOpt.innerText = curOpt.innerText;
			}
		}
		
		var childcount = 0;
		// if this is the last child remove the head
		for(i= oSrc.options.length - 1; i >= 0; i--)
		{
			var curOpt = oSrc.options[i]; 
			if(curOpt.GroupHead)
			{
				if(curOpt.value == opt.GroupValue)
				{
					thisHeader = curOpt;
				}
			}
			else
			{
				if(curOpt.GroupValue == opt.GroupValue)
				{
					childcount++;
				}
			}
		}
		if(childcount <2)
		{
			if(thisHeader)
				thisHeader.removeNode();
		}
		
		var newIndex = oDst.insertBefore(opt, nodBefore);
		//oDst.selectedIndex = -1;
	}
	return true;
}

function dublicateNode(nod)
{
	var newNod = document.createElement("OPTION")
	
	if(nod.GroupValue)
		newNod.setAttribute('GroupValue', nod.GroupValue);
		
	if(nod.GroupHead)
		newNod.setAttribute('GroupHead', nod.GroupHead);
	
	newNod.name = nod.name;
	newNod.id = nod.id;
	newNod.value = nod.value;
	newNod.text = nod.text;
	newNod.style.color = nod.style.color;
	newNod.style.background = nod.style.background;
	
	return newNod;
}


function dublicateNode2(nod)
{
	var newNod = document.createElement("OPTION")
	var i;
	for(i = 0; i < nod.attributes.length; i++)
	{
		var att = nod.attributes[i].name;
		att.value = nod.attributes[i].value;
		newNod.setAttribute(att.name, att.value, 0);
	}
	//newNod.style = nod.style;
	newNod.text = nod.text;
	newNod.innerText = nod.innerText
	return newNod;
}

function orderOptex(oObj, dir)
{
	//oObj = window.form1.slctDst
	if (-1 != oObj.selectedIndex)
	{
		var newindex = -1
		var oFrom, oTo
		if ('up' == dir)
		{
			if(0 < oObj.selectedIndex)
			{
				newindex = oObj.selectedIndex - 1
			}
		}
		else
		{
			if(oObj.length - 1 > oObj.selectedIndex)
			{
				newindex = oObj.selectedIndex + 1
			}
		}
		if (-1 != newindex)
		{
			oFrom = oObj.options[oObj.selectedIndex]
			oTo = oObj.options[newindex]
				
			tmpValue = oTo.value;
			tmpText = oTo.text;
			
			oTo.value = oFrom.value;
			oTo.text = oFrom.text;
			
			oFrom.value = tmpValue;
			oFrom.text = tmpText;
			
			oObj.selectedIndex = newindex;
		}
	}
}

//////////////////////////////
function makeList(slct, att)
{
	var oarrItem = slct.options;
	var len = oarrItem.length;
	var ItemArray = ''
	for(i = 0; i < len; i++)
	{
		if(att)
			ItemArray += oarrItem[i].getAttribute(att);
		else
			ItemArray += oarrItem[i].value;
		if(i < len - 1)
			ItemArray +=  ',';
	}
	return ItemArray;
}

