//*****************************************************************************
//*
//*		POPCLIENT.JS
//*
//*		Javascript functions to support PopUp Example
//*
//*		Requires: display_utils.js, utility.js
//*
//*		(C) 2004-2007 Carboni Software, LLC
//*
//*
//*****************************************************************************

		//*****************************************************************
		//*
		//*  Collapse Functions
		//*
		//*****************************************************************
		
		//*****************************************************************
		//* Global variables
		var collapsePops = new Hashtable();
		var collapseArea = new Hashtable();
		var collapseDups = new Hashtable();
		var collapseCnts = new Hashtable();
		var btnStrtImage = "";
		var btnDupsImage = "";
		var btnRemvImage = "";
		var btnDoneImage = "";
		var btnStrtImageDis = "";
		var btnDupsImageDis = "";
		var btnRemvImageDis = "";
		var btnDoneImageDis = "";
		var btnStrtImageHvr = "";
		var btnDupsImageHvr = "";
		var btnRemvImageHvr = "";
		var btnDoneImageHvr = "";
		var GroupNames = new Hashtable();
		var NewGroupNames = new Hashtable();

		//*****************************************************************
		//* function loadCollapsePop
		//*
		//*		string colPopID : identifies this collapse popup
		//*		array  colPopItemIDs : items displayed in check list
		//*		bool   colDups : sets condition of Dups button enabled
		//*		array  btnImages : button images for collapse popup
		//*		array  gNames : previously existing group names
		//*		string colGroupListID : identifies the group name list area
		//*		array  gRItems : items in groups to remove from list 
		//*
		//*		-- Sets initial information for the collapse popup.  Item
		//*		ID's are added to hashtable, button images are assigned,
		//*		group names are initialized, and display state is saved
		//*		for Start Over.
		//*
		function loadCollapsePop(colPopID, colPopItemIDs, colDups, btnImages, gNames, colGroupListID, gRItems)
		{
			collapsePops.put(colPopID, colPopItemIDs);
			
			btnStrtImage = btnImages[0];
			btnDupsImage = btnImages[1];
			btnRemvImage = btnImages[2];
			btnDoneImage = btnImages[3];
			btnStrtImageHvr = btnImages[4];
			btnDupsImageHvr = btnImages[5];
			btnRemvImageHvr = btnImages[6];
			btnDoneImageHvr = btnImages[7];
			btnStrtImageDis = btnImages[8];
			btnDupsImageDis = btnImages[9];
			btnRemvImageDis = btnImages[10];
			btnDoneImageDis = btnImages[11];

			var objColGroupList = getObject(colGroupListID);
			var gn = new Array();			
			if (gNames != null)
			{
				for (i=0; i<gNames.length; i++)
				{
					gn[i] = gNames[i];
					objColGroupList.innerHTML += "<p><b>&nbsp;&nbsp;" + gNames[i].toString().substring(1) + "</b><br></p>";
				}
			}
			GroupNames.put(colPopID, gn);
						
			NewGroupNames.put(colPopID, new Array());
			
			var objColItems = getObject(colPopID);
			if (objColItems)
			{
				collapseArea.put(colPopID, objColItems.parentNode.innerHTML);
			}
			
			collapseDups.put(colPopID, colDups);
			collapseCnts.put(colPopID, 0);
			
			if (gRItems != null)
			{
				for (i=0; i<gRItems.length; i++)
				{
					var objColItem = getObject(gRItems[i]);
					if (objColItem)
					{
						objColItem.checked = false;
						// parent1=<span>, parent2=<td>, parent3=<tr>
						var objParent = objColItem.parentNode.parentNode.parentNode;
						objParent.parentNode.removeChild(objParent);
					}
				}
			}
		}
		
		//*****************************************************************
		//* function EnableButton
		//*
		//*		object objBtn : the button to enable
		//*		string btnImg : image to set for enabled button
		//*		string btnImgHvr : image to set for mouseover
		//*
		function EnableButton(objBtn, btnImg, btnImgHvr)
		{
			objBtn.disabled = false;
			objBtn.src = btnImg;
			//objBtn.onmouseover = function() { objBtn.src = btnImgHvr; objBtn.style.cursor = 'pointer';};
			//objBtn.onmouseout = function () { objBtn.src = btnImg; objBtn.style.cursor = 'default';};
		}
		
		//*****************************************************************
		//* function DisableButton
		//*
		//*		object objBtn : the button to disable
		//*		string btnImgDis : image to set for disabled button
		//*
		function DisableButton(objBtn, btnImgDis)
		{
			objBtn.disabled = true;
			objBtn.src = btnImgDis;
			objBtn.onmouseover = null;
			objBtn.onmouseout = null;
		}
		
		//*****************************************************************
		//* function onCollapseItemChecked
		//*
		//*		event  eventObj : event object that fired this call
		//*		string colPopID : identifies the collapse list
		//*		string colItemID : identifies the collapse list check item
		//*		string colPopTxtBoxID : identifies the group name text box
		//*		string colBtn1ID : identifies the collapse dups button
		//*		string colBtn2ID : identifies the collapse remove button
		//*
		//*		-- called when item in collapse list is selected or de-
		//*		selected.  If one or more items are selected, the text
		//*		box to enter group name is enabled, and collapse buttons
		//*		are enabled if allowed.
		//*
		function onCollapseItemChecked(eventObj, colPopID, colItemID, colPopTxtBoxID, colBtn1ID, colBtn2ID)
		{
			var checkCount = collapseCnts.get(colPopID);
			var colPopItems = collapsePops.get(colPopID);
			var objColTxtBox = getObject(colPopTxtBoxID);
			var objColBtn1 = getObject(colBtn1ID);
			var objColBtn2 = getObject(colBtn2ID);
			var bCollapseDups = collapseDups.get(colPopID);
			
			if (!objColTxtBox)
			{
				alert('No GroupName text box!');
				return;
			}

			var objColItem = getObject(colItemID);
			if (objColItem)
			{
				if (objColItem.checked)
				{
					checkCount++;
				}
				else
				{
					checkCount--;
				}
			}
			
			collapseCnts.put(colPopID, checkCount);
			
			if (checkCount < 0)
				alert('Invalid checkCount: Please alert development staff!');
			
			if (checkCount > 0)
			{
				objColTxtBox.disabled = false;
				if (bCollapseDups)
					EnableButton(objColBtn1, btnDupsImage, btnDupsImageHvr);
				else
					DisableButton(objColBtn1, btnDupsImageDis);
				EnableButton(objColBtn2, btnRemvImage, btnRemvImageHvr);
			}
			else
			{
				objColTxtBox.disabled = true;
				DisableButton(objColBtn1, btnDupsImageDis);
				DisableButton(objColBtn2, btnRemvImageDis);
			}
		}
		
		//*****************************************************************
		//* function onCollapseItemChecked
		//*
		//*		event  eventObj : event object that fired this call
		//*		string colPopID : identifies the collapse list
		//*		string colPopTxtBoxID : identifies the group name text box
		//*		string colBtn1ID : identifies the collapse dups button
		//*		string colBtn2ID : identifies the collapse remove button
		//*
		//*		-- This function was added as a workaround due to a bug in
		//*		Mozilla browsers (i.e. Firefox).  The bug is a failure 
		//*		to fire an event on the checkbox when the label of the
		//*		checkbox is clicked on.  The label click event will turn
		//*		the checkbox selection on or off, so the event needs to
		//*		be captured within the entire list.  The only difference
		//*		with the entire list event is that the checkbox is not
		//*		actually selected (or de-selected) yet.
		//*
		function onCollapseListChecked(eventObj, colPopID, colPopTxtBoxID, colBtn1ID, colBtn2ID)
		{
			if ((ie) || (eventObj.target.attributes["for"] == null))
				return;
			
			var colItemID = eventObj.target.attributes["for"].value;
			var checkCount = collapseCnts.get(colPopID);
			var colPopItems = collapsePops.get(colPopID);
			var objColTxtBox = getObject(colPopTxtBoxID);
			var objColBtn1 = getObject(colBtn1ID);
			var objColBtn2 = getObject(colBtn2ID);
			var bCollapseDups = collapseDups.get(colPopID);
			
			if (!objColTxtBox)
			{
				alert('No GroupName text box!');
				return;
			}

			var objColItem = getObject(colItemID);
			if (objColItem)
			{
				if (objColItem.checked)
					checkCount--;
				else
					checkCount++;
			}
			
			collapseCnts.put(colPopID, checkCount);
			
			if (checkCount < 0)
				alert('Invalid checkCount: Please alert development staff!');
			
			if (checkCount > 0)
			{
				objColTxtBox.disabled = false;
				if (bCollapseDups)
					EnableButton(objColBtn1, btnDupsImage, btnDupsImageHvr);
				else
					DisableButton(objColBtn1, btnDupsImageDis);
				EnableButton(objColBtn2, btnRemvImage, btnRemvImageHvr);
			}
			else
			{
				objColTxtBox.disabled = true;
				DisableButton(objColBtn1, btnDupsImageDis);
				DisableButton(objColBtn2, btnRemvImageDis);
			}
		}
		
		
		//*****************************************************************
		//* function onCollapseStartOver
		//*
		//*		string colPopID : identifies the collapse list
		//*		string colPopTxtBoxID : identifies the group name text box
		//*		string colBtnStartID : identifies the start over button
		//*		string colBtn1ID : identifies the collapse dups button
		//*		string colBtn2ID : identifies the collapse remove button
		//*		string colGroupListID : identifies the group name list area
		//*		string colHiddenListID : identifies the hidden area of group names
		//*
		//*		-- called when Start Over button is pressed.  Resets all
		//*		values to initial settings, and restores the check box area.
		//*
		function onCollapseStartOver(colPopID, colPopTxtBoxID, colBtnStartID, colBtn1ID, colBtn2ID, colGroupListID, colHiddenListID)
		{
			var objColTxtBox = getObject(colPopTxtBoxID);
			var objColStartBtn = getObject(colBtnStartID);
			var objColBtn1 = getObject(colBtn1ID);
			var objColBtn2 = getObject(colBtn2ID);
			var objColGroupList = getObject(colGroupListID);
			var objColHiddenList = getObject(colHiddenListID);

			collapseCnts.put(colPopID, 0);
			
			GroupNames.put(colPopID, new Array());
			NewGroupNames.put(colPopID, new Array());
			objColGroupList.innerHTML = "";
			objColHiddenList.defaultValue = "";
			objColHiddenList.value = "!!";
			objColHiddenList.disabled = false;
			
			if (!objColTxtBox)
			{
				//alert('No GroupName text box!');
				return;
			}

			var objColItems = getObject(colPopID);
			if (objColItems)
			{
				var objDiv = objColItems.parentNode;
				if (objDiv)
					objDiv.innerHTML = collapseArea.get(colPopID);
			}
			
						
			var colPopItems = collapsePops.get(colPopID);
			if (colPopItems != null)
			{
				for (i=0; i<colPopItems.length; i++)
				{
					var objColItem = getObject(colPopItems[i]);
					if (objColItem)
					{
						objColItem.checked = false;
					}
				}
			}
			
			objColTxtBox.disabled = true;
			EnableButton(objColStartBtn, btnStrtImage, btnStrtImageHvr);
			DisableButton(objColBtn1, btnDupsImageDis);
			DisableButton(objColBtn2, btnRemvImageDis);
		}
		
		//*****************************************************************
		//* function onCollapseDups
		//*
		//*		string colPopID : identifies the collapse list
		//*		string colPopTxtBoxID : identifies the group name text box
		//*		string colBtn1ID : identifies the collapse dups button
		//*		string colBtn2ID : identifies the collapse remove button
		//*		string colGroupListID : identifies the group name list area
		//*		string colHiddenListID : identifies the hidden area of group names
		//*
		//*		-- called when Collapse Dups button is pressed.  Gets all
		//*		selected items and assigns them to the group name entered.
		//*		The items are added as an array in the hidden group name
		//*		area.  The group name is prefixed with $D to indicate that
		//*		these items should be duplicated.  The new group name is also
		//*		added to the visible group list.
		//*
		function onCollapseDups(colPopID, colPopTxtBoxID, colBtn1ID, colBtn2ID, colGroupListID, colHiddenListID)
		{
			var objColTxtBox = getObject(colPopTxtBoxID);
			var objColBtn1 = getObject(colBtn1ID);
			var objColBtn2 = getObject(colBtn2ID);
			var objColGroupList = getObject(colGroupListID);
			var objColHiddenList = getObject(colHiddenListID);
			
			var txtGroupName = objColTxtBox.value;
			txtGroupName = trimString(txtGroupName);
			
			if (txtGroupName == "")
			{
				alert('Please enter a Group Name');
				return;
			}
			
			if (!isGroupNameValid(colPopID, txtGroupName))
				return;

			var ngLength = NewGroupNames.get(colPopID).length;
			NewGroupNames.get(colPopID)[ngLength] = txtGroupName;
			
			objColGroupList.innerHTML += "<p><b>&nbsp;&nbsp;" + txtGroupName + "</b><br></p>";
			
			objColHiddenList.value += "$D" + txtGroupName + "[";
			var colPopItems = collapsePops.get(colPopID);
			if (colPopItems != null)
			{
				for (i=0; i<colPopItems.length; i++)
				{
					var objColItem = getObject(colPopItems[i]);
					if (objColItem)
					{
						if (objColItem.checked)
						{
							if (objColItem.parentNode.attributes[objColItem.parentNode.attributes.length-1].name.toUpperCase() == "DEFAULTVALUE")
								objColHiddenList.value += objColItem.parentNode.attributes[objColItem.parentNode.attributes.length-1].value;
							else
								objColHiddenList.value += objColItem.parentNode.defaultValue;
							objColHiddenList.value += ",";
							objColItem.checked = false;
						}
					}
				}
			}
			objColHiddenList.value += "]\r\n";
			objColHiddenList.disabled = false;
			
			objColTxtBox.value = "";
			objColTxtBox.disabled = true;
			DisableButton(objColBtn1, btnDupsImageDis);
			DisableButton(objColBtn2, btnRemvImageDis);

			collapseCnts.put(colPopID, 0);
		}
		
		//*****************************************************************
		//* function onCollapseRemove
		//*
		//*		string colPopID : identifies the collapse list
		//*		string colPopTxtBoxID : identifies the group name text box
		//*		string colBtn1ID : identifies the collapse dups button
		//*		string colBtn2ID : identifies the collapse remove button
		//*		string colGroupListID : identifies the group name list area
		//*		string colHiddenListID : identifies the hidden area of group names
		//*
		//*		-- called when Collapse Remove button is pressed.  Gets all
		//*		selected items and assigns them to the group name entered.
		//*		The items are added as an array in the hidden group name
		//*		area.  The group name is prefixed with $R to indicate that
		//*		these items should be removed.  The new group name is also
		//*		added to the visible group list.
		//*
		function onCollapseRemove(colPopID, colPopTxtBoxID, colBtn1ID, colBtn2ID, colGroupListID, colHiddenListID)
		{
			var objColTxtBox = getObject(colPopTxtBoxID);
			var objColBtn1 = getObject(colBtn1ID);
			var objColBtn2 = getObject(colBtn2ID);
			var objColGroupList = getObject(colGroupListID);
			var objColHiddenList = getObject(colHiddenListID);
			
			var txtGroupName = objColTxtBox.value;
			txtGroupName = trimString(txtGroupName);
			
			if (txtGroupName == "")
			{
				alert('Please enter a Group Name');
				return;
			}
			
			if (!isGroupNameValid(colPopID, txtGroupName))
				return;

			var ngLength = NewGroupNames.get(colPopID).length;
			NewGroupNames.get(colPopID)[ngLength] = txtGroupName;
			
			objColGroupList.innerHTML += "<p><b>&nbsp;&nbsp;" + txtGroupName + "</b><br></p>";
			
			objColHiddenList.value += "$R" + txtGroupName + "[";
			var colPopItems = collapsePops.get(colPopID);
			if (colPopItems != null)
			{
				for (i=0; i<colPopItems.length; i++)
				{
					var objColItem = getObject(colPopItems[i]);
					if (objColItem)
					{
						if (objColItem.checked)
						{
							if (objColItem.parentNode.attributes[objColItem.parentNode.attributes.length-1].name.toUpperCase() == "DEFAULTVALUE")
								objColHiddenList.value += objColItem.parentNode.attributes[objColItem.parentNode.attributes.length-1].value;
							else
								objColHiddenList.value += objColItem.parentNode.defaultValue;
							objColHiddenList.value += ",";
							objColItem.checked = false;
							// parent1=<span>, parent2=<td>, parent3=<tr>
							var objParent = objColItem.parentNode.parentNode.parentNode;
							objParent.parentNode.removeChild(objParent);
						}
					}
				}
			}
			objColHiddenList.value += "]\r\n";
			objColHiddenList.disabled = false;
			
			objColTxtBox.value = "";
			objColTxtBox.disabled = true;
			DisableButton(objColBtn1, btnDupsImageDis);
			DisableButton(objColBtn2, btnRemvImageDis);

			collapseCnts.put(colPopID, 0);
		}
		
		//*****************************************************************
		//* function isGroupNameValid
		//*
		//*		string GroupName : group name to validate
		//*
		//*		returns: true if GroupName is valid, otherwise false.
		//*
		//*		-- checks for illegal characters in group name, and
		//*		determines whether or not group name was already used.
		//*
		function isGroupNameValid(colPopID, GroupName)
		{
			if ((GroupName.indexOf("$") != -1) ||
				(GroupName.indexOf("<") != -1) ||
				(GroupName.indexOf(">") != -1) ||
				(GroupName.indexOf("?") != -1) ||
				(GroupName.indexOf("/") != -1) ||
				(GroupName.indexOf("\\") != -1) ||
				(GroupName.indexOf("[") != -1) ||
				(GroupName.indexOf("]") != -1) ||
				//(GroupName.indexOf(",") != -1) ||
				//(GroupName.indexOf(".") != -1) ||
				(GroupName.indexOf("{") != -1) ||
				(GroupName.indexOf("}") != -1) ||
				(GroupName.indexOf("|") != -1) ||
				(GroupName.indexOf("%") != -1) ||
				(GroupName.indexOf("&") != -1) ||
				(GroupName.indexOf("#") != -1))
			{
				var errString = "Group Name cannot contain special characters '$','<','>','?','/','\\','[',']','{','}','|','%','&','#'";
				alert(errString);
				return false;
			}

			var gn = GroupNames.get(colPopID);			
			for (i=0; i<gn.length; i++)
			{
				if (GroupName == gn[i].toString().substring(1))
				{
					alert('Group Name already exists.  Please enter a different name.');
					return false;
				}
			}
			
			var ngn = NewGroupNames.get(colPopID);
			for (i=0; i<ngn.length; i++)
			{
				if (GroupName == ngn[i])
				{
					alert('Group Name already exists.  Please enter a different name.');
					return false;
				}
			}
			
			return true;
		}

