//function that allows the user to start customizing funds
function startCustomization(selectedFundsList)
{
	//show customization panel
	showHideCustomizationPanel (true);
	
	//hide commit changes panel
	showHideCommitChangesPanel (false);
	
	//show all funds
	showFundRows ('[ALL]');
	
	//show checkboxes
	showHideCheckBoxes(false);
	
	//check marked selected funds
	checkMarkSelectedFunds(selectedFundsList);
}

//function to show dislaimers based on selected funds
function showDislaimers(selectedFundsList)
{
	//gather all inputs
	var x = document.getElementsByTagName('tr');
	var fundColTD = null;
	var fundColSups = null;
	var supVal = null;
	var curDisclaimer = null;
	var selectedFundsArray = selectedFundsList.split(";");
	//go through all tr's
	for (var i=0;i<x.length;i++)
	{
		//if it's a fund row
		if (x[i].id.substring(0,9) == 'FUND_ROW_')
		{
			var fundId = x[i].id.substring(9)
			//if we show all or the current fund matches the one we're working with
			if (selectedFundsList == '[ALL]' || findInFundList(fundId, selectedFundsArray))
			//MD Select Fund
			{
				//check if there is a subscript in the 
				//show dislaimer
				fundColTD = x[i].getElementsByTagName('td');
				
				//if the fund column exists
				if (fundColTD != null)
				{
					//alert("c");
					//check if it has any <sup> tags
					fundColSups = fundColTD[1].getElementsByTagName('sup');
					
					//go through all sups
					for (var j=0;j<fundColSups.length;j++)
					{
						//alert("sup!");
						//get first sub's value
						supVal = fundColSups[j].id;
						
						//get disclaimer
						curDisclaimer = document.getElementById('Disc_' + supVal);
						
						//if disclaimer exists
						if (curDisclaimer != null)
						{
							//show the disclaimer item
							curDisclaimer.className = 'unit_values_disclaimer shown';
						}
					}
				}
			}
		}
	}
}

//function to cancel changes
function stopCustomization(selectedFundsList)
{
	//hide the commit changes panel
	showHideCommitChangesPanel (true);
	
	//show customization panel
	showHideCustomizationPanel (false);
	
	//show only selected funds
	showFundRows (selectedFundsList);
	
	//hide checkboxes
	showHideCheckBoxes(true);
}

//function to show or hide customization panel
function showHideCustomizationPanel(isHide)
{
	document.getElementById('customizePanel').className = (isHide?  'hidden customizePanel':'shown customizePanel');
}

//function to show or hide commit changes panel
function showHideCommitChangesPanel(isHide)
{
	document.getElementById('saveCancelChangesPanel').className = (isHide?  'hidden saveCancelChangesPanel':'shown saveCancelChangesPanel');
}

//function to  show or hide checkboxes
function showHideCheckBoxes(isHide)
{
	var fromClassName = null;
	var toClassName = null;
	
	//if we want to hide checkboxes
	if (isHide)
	{
		fromClassName = 'chkbox shown';
		toClassName = 'chkbox hidden';
	}
	else
	{
		fromClassName = 'chkbox hidden';
		toClassName = 'chkbox shown';
	}
	
	//hide/show th's
	var y = document.getElementsByTagName('th');
	
	//go through all th's
	for (var i=0;i<y.length;i++)
	{
		if (y[i].className == fromClassName)
			y[i].className = toClassName;
	}
	
	//hide/show td's
	var x = document.getElementsByTagName('td');
	
	//go through all td's
	for (var i=0;i<x.length;i++)
	{
		if (x[i].className == fromClassName)
			x[i].className = toClassName;
	}
}

//function to place a check mark next to selected funds and unselect the rest
function checkMarkSelectedFunds(selectedFundsList)
{
	var selectedFundsArray = selectedFundsList.split(";");
	//gather all inputs
	var x = document.getElementsByTagName('input');
	
	//go through all inputs
	
	for (var i=0;i<x.length;i++)
	{
		//if it's a fund customization checkbox
		if (x[i].id.substring(0,10) == 'FUND_CKBX_')
		{
			var checked = false;
			if (selectedFundsList == '[ALL]'){
				checked = true;
			//if we have a list
			} else {
				var fundId = x[i].id.substring(10);
				checked = findInFundList(fundId, selectedFundsArray)
			}
			x[i].checked = checked;
		}
	}
}


function findInFundList(fundId, fundArray){
	var found = false;
	for(var item=0; item < fundArray.length; item++){
		if (fundArray[item] == fundId){
			//found it
			found = true;
			break;
		}
	}
	return found;
}

//function to show all fund rows or only the ones selected
function showFundRows(selectedFundsList)
{
	var selectedFundsArray = selectedFundsList.split(";");
	//gather all inputs
	var x = document.getElementsByTagName('tr');
	
	//go through all tr's
	for (var i=0;i<x.length;i++)
	{
		//if it's a fund row
		if (x[i].id.substring(0,9) == 'FUND_ROW_')
		{
			//if we show all or the current fund matches the one we're working with
			
			var fundId = x[i].id.substring(9);	
			if (selectedFundsList == '[ALL]' || findInFundList(fundId, selectedFundsArray))
				x[i].className = 'shown';
			else
				x[i].className = 'hidden';
		}
	}
}

//startup function
function initalizeCustomizedFunds(selectedFundsList,showList,scrollList)
{
	//show customized funds
	showFundRows (selectedFundsList);
	
	//show disclaimers
	showDislaimers (selectedFundsList);
	
	//if we need to show the list on startup and scroll to it
	if (showList)
	{
		//show selected funds
		document.getElementById('unitvalues-panel').className = 'Shown';
		//document.getElementById('unitvalues-panel').scrollTo();
		//(document.getElementById('unitvalues-panel'));
		
		if (scrollList)
			ScrollToElement(document.getElementById('unitvalues-panel'));
	}
}

//idea borrowed from: http://radio.javaranch.com/pascarello/2005/01/09/1105293729000.html
function ScrollToElement(theElement){
	
	//var selectedPosX = 0;
	var selectedPosY = 0;
	
	//while the element exists
	while(theElement != null){
		//get x and y offset positions of the current element
		//selectedPosX += theElement.offsetLeft;
		selectedPosY += theElement.offsetTop;
		//assign the element to its offset parent
		theElement = theElement.offsetParent;
	}
	
	//scroll the window to element location
	window.scrollTo(0,selectedPosY);
}

//function to submit form
function submitForm()
{
	var frm = document.getElementById('unitPriceFrm');
	
	if (frm != null)
		frm.submit();
}

//function to move to next available date
function moveNextDate()
{
	var currDate = document.getElementById('currDate');
	var nextDate = document.getElementById('nextDate');
	var direction = document.getElementById('moveDirection');
	var datePicked = document.getElementById('datePicked');
	
	if (currDate != null && nextDate != null && direction != null && datePicked != null)
	{
		//set current date to be the same as the next date
		currDate.value = nextDate.value;
		
		//set move direction to be next
		direction.value = "next";
		
		//set date picked to no
		datePicked.value = "No";
		
		//submit the form
		submitForm();
	}
}

//function to move to previous available date
function movePreviousDate()
{
	var currDate = document.getElementById('currDate');
	var prevDate = document.getElementById('prevDate');
	var direction = document.getElementById('moveDirection');
	var datePicked = document.getElementById('datePicked');
	
	if (currDate != null && prevDate != null && direction != null && datePicked != null)
	{
		//set current date to be the same as the next date
		currDate.value = prevDate.value;
		
		//set move direction to be previous
		direction.value = "previous";
		
		//set date picked to no
		datePicked.value = "No";
		
		//submit the form
		submitForm();
	}
}

//function to select a new date
function selectDate(aDate)
{
	var currDate = document.getElementById('currDate');
	var direction = document.getElementById('moveDirection');
	var datePicked = document.getElementById('datePicked');
	
	if (currDate != null && direction != null && datePicked != null)
	{
		//set current date to be the same as the given one
		currDate.value = aDate;
		
		//set move direction to be previous
		direction.value = "previous";
		
		//set date picked to yes
		datePicked.value = "Yes";
		
		//submit the form
		submitForm();
	}
}

//function that is triggered when a user selects a date from the date picker
function datePickerClosed(fld)
{
	if (fld == null)
		return;
	
	//select a date
	selectDate (fld.value);
}
