function DoMenuClick(X,Y,MenuID) {

	MenuID.setX(X);
	MenuID.setY(Y);
	MenuID.show();
}

function DoMenuItemClick(MenuItemID,TextValue,FGID) {

	document.getElementById(MenuItemID).innerHTML=TextValue;hideCursorMenus();
	document.getElementById('updateicon_' + FGID).style.display='inline';
//	document.getElementById('updateheader').style.display='inline';
	document.getElementById('updatelink').style.display='inline';

}
function updatePageStats() {
	var cartSelectedCount = document.getElementById('cartSelectedCount');
	var pageSelectedCount = document.getElementById('pageSelectedCount');
	var cartTotalPrice = document.getElementById('cartTotalPrice');
	var pageTotalPrice = document.getElementById('pageTotalPrice');
	
	if ((cartSelectedCount != null) && (pageSelectedCount != null)) {
		document.getElementById('cartSelectedCount').innerHTML = document.getElementById('pageSelectedCount').innerHTML;
	}
	
	if ((cartTotalPrice != null) && (pageTotalPrice != null)) {
		document.getElementById('cartTotalPrice').innerHTML = document.getElementById('pageTotalPrice').innerHTML;
	}
}

function DoUpdateDataNow(SearchToken, SearchGroupGUID)
{

//	document.getElementById('updateicon_295').style.display='none';
//	document.getElementById('updateicon_394').style.display='none';
//	document.getElementById('updateicon_431').style.display='none';
//	document.getElementById('updateicon_320').style.display='none';
//	document.getElementById('updateicon_296').style.display='none';
//	document.getElementById('updateicon_294').style.display='none';
//	document.getElementById('updatelink').style.display='none';

	document.hrsearches.searchToken.value = SearchToken;
	document.hrsearches.sg.value = SearchGroupGUID;
	document.hrsearches.resetSearch.value = 'y';

	document.hrsearches.submit();

//	window.alert('Please wait while we update your data tables');

}

function CancelProfileEdit(strTableID)
{
	TableIDForEditMode = 0;
	ClearSearchProfileForm(strTableID);
}

function ClearSearchProfileForm(strTableID)
{
	var menuDiv;
	var menuTable;

	menuDiv = document.getElementById('Menu' + strTableID);
	menuTable = document.getElementById('MenuTable' + strTableID);

	menuDiv.style.display = 'none';
	menuDiv.innerHTML = '&nbsp;';

	menuTable.style.display = '';	
}

function ProcessProfileEditButton(SearchToken,SearchGroupGUID,strTableID)
{
	var menuTable;
	var answer; 
	menuTable = document.getElementById('MenuTable' + strTableID);

	if (TableIDForEditMode != 0)
	{
		if (answer = window.confirm('Another item is already being edited. Click Cancel to go back and finish editing your previous search. Or, you can click OK to undo previous edits and begin editing this search.'))
		{
			CancelProfileEdit(TableIDForEditMode);
			menuTable.style.display = 'none';
			TableIDForEditMode = strTableID;
			TransposeSpanToForm(SearchToken,SearchGroupGUID,strTableID);
		}
	}
	else
	{
		menuTable.style.display = 'none';
		TableIDForEditMode = strTableID;
		TransposeSpanToForm(SearchToken,SearchGroupGUID,strTableID);
	}
}

function ShowUpdateIconAndLink(iconID,updateLinkID)
{
	var iconDiv
	var linkSpan

	iconDiv = document.getElementById(iconID);
	linkSpan = document.getElementById(updateLinkID);
	iconDiv.style.display = 'inline';

	if (linkSpan.style.display == 'none')
	{
		linkSpan.style.display = 'inline';
	}
	
	var frmElements = document.getElementById("hrsearches").elements;
	var fNoCriteriaSpecified = true;
	var searchBreadthOption = document.getElementById("searchBreadth");
		
	for(var i=0; i < frmElements.length; i++)
	{	
		if ((frmElements.item(i).name != "searchBreadth") && (frmElements.item(i).name != "searchMethod") && 
			(frmElements.item(i).type == "select-one") && (frmElements.item(i).value.length > 0))
		{
			fNoCriteriaSpecified = false;
			break;
		}
	}
	
	var searchBreadthItems = searchBreadthOption.options;
	if (fNoCriteriaSpecified)
	{
		// remove exacth and close match options
		var filteredSearchBreadthOptions = searchBreadthOption.innerHTML;
		
		for (var j=0; j < searchBreadthItems.length; j++)
		{
			if ((searchBreadthItems.item(j).value == "0") || (searchBreadthItems.item(j).value == "1"))
			{
				filteredSearchBreadthOptions = filteredSearchBreadthOptions.replace(searchBreadthItems.item(j).outerHTML, '');
				searchBreadthOption.removeChild(searchBreadthItems.item(j));
				j = j - 1;
			}
		}
	}
	else
	{
		if (searchBreadthItems.length == searchBreadthArray.length + 1)
		{
			return;
		}
	
		// rebuild options	
		var option;
		for (var j=0; j < searchBreadthItems.length; j++)
		{
			option = searchBreadthItems.item(j);
			if (option.value != "-1")
			{
				searchBreadthOption.removeChild(option);	
				j = j - 1;
			}
		}
		
		for (var j=0; j < searchBreadthArray.length; j++)
		{
			option = document.createElement("OPTION");
			option.value= '' + j + '';
			option.text=searchBreadthArray[j];
			searchBreadthItems.add(option);
		}
	}
	
}

function AbbreviateTerm(term)
{
	var newTerm;
	newTerm = term;

	//need to create a little dictionary. for now, just these few:

	if (newTerm.match(' / '))
	{
		newTerm = replaceSubstring(newTerm, ' / ', '/');
	}
	if (newTerm.match('Exchange'))
	{
		newTerm = replaceSubstring(newTerm, 'Exchange', 'Exch.');
	}
	if (newTerm.match('Organization'))
	{
		newTerm = replaceSubstring(newTerm, 'Organization', 'Org.');
	}
	if (newTerm.match('State'))
	{
		newTerm = replaceSubstring(newTerm, 'State', 'St.');
	}
	if (newTerm.match('Government'))
	{
		newTerm = replaceSubstring(newTerm, 'Government', 'Govt.');
	}
	if (newTerm.match('Years'))
	{
		newTerm = replaceSubstring(newTerm, 'Years', 'Yrs.');
	}
	if (newTerm.match('Center'))
	{
		newTerm = replaceSubstring(newTerm, 'Center', 'Ctr.');
	}
	if (newTerm.match('Islands'))
	{
		newTerm = replaceSubstring(newTerm, 'Islands', 'Islds.');
	}
	if (newTerm.match('Company'))
	{
		newTerm = replaceSubstring(newTerm, 'Company', 'Co.');
	}
	if (newTerm.match('District'))
	{
		newTerm = replaceSubstring(newTerm, 'District', 'Dist.');
	}
	return newTerm;
}

function confirmApplySubscriptions(fBuy)
{
	var i = 0;
	var selectedSearch = 0;
	var quotaRemaining = 0;
	var fSubmit = false;
	var checkedSearches = null;
	
	if (document.hrsearches.Srch != null)
	{
		checkedSearches = document.hrsearches.Srch;
		for (i = 0; i < checkedSearches.length; ++i)
		{
			if (checkedSearches[i].checked)
			{
				selectedSearch = selectedSearch + 1;
			}
		}
		
		quotaRemaining = parseInt(document.hrsearches.quotaRemaining.value);
	}
	
	if ((selectedSearch > 0) && (quotaRemaining > 0)) 
	{
		if (selectedSearch > quotaRemaining)
		{
			if (!fBuy)
			{
				alert('Your subscription(s) cover ' + quotaRemaining + ' report(s), but you have checked ' + selectedSearch + '. Please choose only up to ' + quotaRemaining + ' report(s), or purchase additional subscription(s).');
			}	
			else
			{
				fSubmit = true;
			}	
		}
		else
		{
			if(confirm('Confirm: Your subscription(s) will be applied to the ' + selectedSearch + ' checked report(s).'))
			{
				fSubmit = true;
			}
		}
	}
	else
	{
		fSubmit = true;
	}
	
	if (fSubmit)
	{
		if (fBuy)
		{
			document.hrsearches.buyNow.value = 'y';
		}
		else
		{
			document.hrsearches.applySubs.value = 'y';
		}
		document.hrsearches.submit();
	}
}
