function ShowBCMASubMembership(bShow)
{
	document.getElementById('BCMAMemberSub').style.display = bShow ? "block" : "none";
}

function ShowBCMAMemberSubTab(lnk, idx)
{
	// get the currently selected tab
	var curIdx = 1;
	var curTab=null;
		
	var list = document.getElementById('BCMAMemberSubTab'); // get the list container
	for (var i=0; i<list.childNodes.length; i++)
	{
		if (list.childNodes[i].className=="BCMAMemberSubTabItem") // found a tab that corresponds to an item
		{	
			if (list.childNodes[i].childNodes[0].className=="BCMAMemberSubTabLink") // not the selected tab
				curIdx++;
			else // found the selected tab, mark it
			{
				curTab = list.childNodes[i].childNodes[0];
				break;
			}
		}
	}
	
	if (curTab!=null)
	{
		var strErr="";
		var bChangeTab = true;
		var curProfile = document.getElementById('BCMAMemberSub' + curIdx);
		
		// get the inputs and validate them
		var inputs = curProfile.getElementsByTagName("INPUT");
				
		// if any information has been entered, validate
		if (inputs[0].value!="" || inputs[1].value!="" || inputs[2].value!="")
		{
			if (inputs[0].value=="")
				strErr = "You have not entered a name!";
			else if (!IsValidBCMAMemberEmail(inputs[1].value))
				strErr = "You have not entered a valid email!";
			
			if (strErr!="") 
			{
				strErr += " Press OK to skip (changes will not be saved)";
				bChangeTab = confirm(strErr);
			}
		}
				
		if (bChangeTab)
		{
			curProfile.style.display = "none"; // hide the current profile
			var newProfile = document.getElementById('BCMAMemberSub' + idx);
			newProfile.style.display = "block"; // show the requested profile
			curTab.className = "BCMAMemberSubTabLink"; // set the current link to not selected
			lnk.className = "BCMAMemberSubTabLinkSelected"; // set the requested link to selected
			lnk.blur();
			
			/* There's an annoying IE bug that is losing my background image on the delete button when tabs
			   are changed.  So far the only way I can get around it is by readding the button every time.
			*/
			if (document.all)
			{
				var oldDelete = newProfile.childNodes[newProfile.childNodes.length-1];
				
				if (oldDelete.className=="BCMAMemberSubDelete")
				{
					var newDelete = oldDelete.cloneNode(true);
					oldDelete.replaceNode(newDelete);
				}
			}
		}
		
	}	
}

function IsValidBCMAMemberEmail(address)
{
	// regular expression to validate the e-mail address
	var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
			
	return re.test(address);
}

function FormatBCMAMemberSubInput(obj)
{
	obj.value = obj.value.replace(/^\s+/g, ""); // remove leading spaces
	obj.value = obj.value.replace(/\s+$/g, ""); // remove trailing spaces
}

function DeleteBCMAMemberSub(idx)
{
	if (confirm("Member will be deleted when you save the profile!  Are you sure?"))
	{
		var curProfile = document.getElementById('BCMAMemberSub' + idx);
		var memberID = "";
			
		// get the inputs and validate them
		var inputs = curProfile.getElementsByTagName("INPUT");
	
		for (var i=0; i<inputs.length; i++)
		{
			if (inputs[i].type!="button") 
			{	
				//store the id if there is one
				if (inputs[i].name.indexOf("SubID")!=-1) memberID = inputs[i].value;
				inputs[i].value = "";
			}
		}
		
		// get a handle to the list of members to delete
		var delList = document.getElementById('txtBCMAMemberSubDelete');
		
		// store the member id into a comma delimited list
		if (delList.value.indexOf(",")==-1) delList.value += ",";
		delList.value += memberID + ",";
	}
}

