// JavaScript Document

/* ============================================================================================*/
/*
Style tables with striping
*/
/* ============================================================================================*/
var stripeTable = function()
{
	$(".stripeMe tr:even").addClass("alt");
	$(".stripeMe tr:not(.headerRow)").mouseover
	(
		function() 
		{
			$(this).addClass("over");
		}
	);
	$(".stripeMe tr").mouseout
	(
		function() 
		{
			$(this).removeClass("over");
		}
	);

	$(".stripeMeOdd tr:odd").addClass("alt");
	$(".stripeMeOdd tr:not(.headerRow)").mouseover
	(
		function() 
		{
			$(this).addClass("over");
		}
	);
	$(".stripeMeOdd tr").mouseout
	(
		function()
		{
			$(this).removeClass("over");
		}
	);

	$(".stripeMeWhite tr:even").addClass("alt");
	$(".stripeMeWhite tr:not(.headerRow)").mouseover
	(
		function() 
		{
			$(this).addClass("over");
		}
	);
	$(".stripeMeWhite tr").mouseout
	(
		function()
		{
			$(this).removeClass("over");
		}
	);
};

/* ============================================================================================*/
/*
Style summary tables with scrolling
*/
/* ============================================================================================*/
var styleSummaryTable = function(jsNumRows)
{
	if((jsNumRows) && (jsNumRows > 7))
	{
		$(".tableScroll").addClass("summaryBottom");
		$(".tableScroll").addClass("tableScroll_IE");
	}
	else
	{
		$(".stripeMe").addClass("summaryBottom");
	}
};

/* ============================================================================================*/
/*
Style summary tables with scrolling
*/
/* ============================================================================================*/
var styleReportTable = function(jsNumRows)
{
	if((jsNumRows) && (jsNumRows > 18))
	{
		$(".tableScroll").addClass("tableScroll_IE");
		$("#scrollBarBlank").show();
	}
	else
	{
		$("#scrollBarBlank").hide();
	}
	
	$(".removeBorders").css("border-right","none").css("border-left","none").css("border-top","none").css("border-bottom","none");
};

/* ============================================================================================*/
/*
This function gets the computer's local time.
*/
/* ============================================================================================*/

var getLocalDateTime = function()
{
	// get local date and time
	var currentDateTime = new Date();
	
	// format date information
	var currentMonth = currentDateTime.getMonth() + 1;
	var currentDate = currentDateTime.getDate();
	var currentYear = currentDateTime.getFullYear();
	
	var formattedDate = currentMonth + '/' + currentDate + '/' + currentYear;
	
	// format time information
	var currentHour = currentDateTime.getHours();
	var currentMinute = currentDateTime.getMinutes();
	var currentSecond = currentDateTime.getSeconds();
	var currentMillisecond = currentDateTime.getMilliseconds();
	
	var formattedTime = currentHour + ':' + currentMinute + ':' + currentSecond + '.' + currentMillisecond;
	
	var dateTime = formattedDate + ' ' + formattedTime;
	return dateTime;

};


/* ============================================================================================*/
/*
This function populates the txtDateTime field on a page with the user's computer's local time.
*/
/* ============================================================================================*/

var populateDateTimeFields = function(fieldToPopulate)
{
	// get local date and time
	var currentDateTime = new Date();
	
	// format date information
	var currentMonth = currentDateTime.getMonth() + 1;
	var currentDate = currentDateTime.getDate();
	var currentYear = currentDateTime.getFullYear();
	
	var formattedDate = currentMonth + '/' + currentDate + '/' + currentYear;
	
	// format time information
	var currentHour = currentDateTime.getHours();
	var currentMinute = currentDateTime.getMinutes();
	var currentSecond = currentDateTime.getSeconds();
	var currentMillisecond = currentDateTime.getMilliseconds();
	
	var formattedTime = currentHour + ':' + currentMinute + ':' + currentSecond + '.' + currentMillisecond;

	// set page field with formatted date and time
	var jsFieldToPopulate	=	document.getElementById(fieldToPopulate);
	if(jsFieldToPopulate)
	{
		jsFieldToPopulate.value = formattedDate + ' ' + formattedTime;
	}
};


/* ============================================================================================*/
/*
This function populates the State/Provinces based on the selection in the Country dropdown box.
*/
/* ============================================================================================*/

var getStateProvinces = function(jsStatesByCountry,jsIdCountry,jsLocCountry,countryFieldId,stateFieldId)
{
	if(countryFieldId)
	{
		var jsCountryFieldId = countryFieldId;
	}
	else
	{
		var jsCountryFieldId = 'txtCountry';
	}

	if(stateFieldId)
	{
		var jsStateFieldId = stateFieldId;
	}
	else
	{
		var jsStateFieldId = 'txtState_Province';
	}

	// set txtCountry hidden field
	if(jsLocCountry)
	{
		var numCountries = jsLocCountry.length;
		for(var i=0;i<numCountries;i++)
		{
			if(jsLocCountry[i][0] == jsIdCountry)
			{
				document.getElementById(jsCountryFieldId).value = jsLocCountry[i][1];
				var countryCodeField = document.getElementById('txtCountry_Code');
				if(countryCodeField)
				{
					countryCodeField.value = jsLocCountry[i][2];
				}
				break;
			}
		}
	}
	
	// get State/Province dropdown box
	var stateOptions = document.getElementById(jsStateFieldId);
	if(stateOptions)
	{
		stateOptions.options.length = 0;
	
		// set first option to Select
		var newOption=document.createElement('option');
		newOption.value='';
		newOption.text='Select';
		try
		{
			stateOptions.add(newOption,null); // standards compliant
		}
		catch(ex)
		{
			stateOptions.add(newOption); // IE only
		}

		// set second option to dashes
		newOption=document.createElement('option');
		newOption.value='';
		newOption.text='-----';
		try
		{
			stateOptions.add(newOption,null); // standards compliant
		}
		catch(ex)
		{
			stateOptions.add(newOption); // IE only
		}

		if (jsStatesByCountry)
		{
			for(i=0;i<jsStatesByCountry.length;i++)
			{
				newOption=document.createElement('option');
				newOption.value=jsStatesByCountry[i];
				newOption.text=jsStatesByCountry[i];
				try
				{
					stateOptions.add(newOption,null); // standards compliant
				}
				catch(ex)
				{
					stateOptions.add(newOption); // IE only
				}
			}
		}
	}

};


/* ============================================================================================*/
/*
This function populates the Loc State/Provinces, for the Census source type in the Source and 
Citation pages, based on the selection in the Country dropdown box.
*/
/* ============================================================================================*/

var getLocStateProvinces = function(jsStatesByCountry,jsIdCountry,jsLocCountry)
{
	// set txtLocCountry hidden field
	if(jsLocCountry)
	{
		var numCountries = jsLocCountry.length;
		for(var i=0;i<numCountries;i++)
		{
			if(jsLocCountry[i][0] == jsIdCountry)
			{
				document.getElementById('txtLocCountry').value = jsLocCountry[i][1];
				break;
			}
		}
	}
	
	// get Loc State/Province dropdown box
	var stateOptions = document.getElementById('txtLocState_Province');
	stateOptions.options.length = 0;
	
	// set first option to Select
	var newOption=document.createElement('option');
	newOption.value='';
	newOption.text='Select';
	try
	{
		stateOptions.add(newOption,null); // standards compliant
	}
	catch(ex)
	{
		stateOptions.add(newOption); // IE only
	}

	// set second option to dashes
	newOption=document.createElement('option');
	newOption.value='';
	newOption.text='-----';
	try
	{
		stateOptions.add(newOption,null); // standards compliant
	}
	catch(ex)
	{
		stateOptions.add(newOption); // IE only
	}

	if (jsStatesByCountry)
	{
		for(i=0;i<jsStatesByCountry.length;i++)
		{
			newOption=document.createElement('option');
			newOption.value=jsStatesByCountry[i];
			newOption.text=jsStatesByCountry[i];
			try
			{
				stateOptions.add(newOption,null); // standards compliant
			}
			catch(ex)
			{
				stateOptions.add(newOption); // IE only
			}
		}
	}
	
};


/* ============================================================================================*/
/*
This function returns the idCountry based on the country name.
*/
/* ============================================================================================*/

var getIdCountry = function(jsLocCountry,jsCountryName)
{
	var returnIdCountry = 0;
	
	if(jsLocCountry)
	{
		var numCountries = jsLocCountry.length;
		for(var i=0;i<numCountries;i++)
		{
			if(jsLocCountry[i][1] == jsCountryName)
			{
				returnIdCountry = jsLocCountry[i][0];
				break;
			}
		}
	}
	
	return returnIdCountry;
};


/* ============================================================================================*/
/*
This function sets the Country for first render.
*/
/* ============================================================================================*/

var setCountry = function(jsIdCountry,jsCountryFieldId)
{
	var countryOptions = document.getElementById(jsCountryFieldId);

	if(countryOptions)
	{
		if(jsIdCountry >= 1)
		{
			var countryOptionsLength = countryOptions.options.length;
			for(i=0; i<countryOptionsLength; i++)
			{
				if(countryOptions.options[i].value == jsIdCountry)
				{
					countryOptions.options[i].selected = true;
					break;
				}
			}
		}
		
		else
		{
			countryOptions.options[0].selected = true;
		}
	}

};


/* ============================================================================================*/
/*
This function sets the state/province dropdown box value to the given state/province name.
*/
/* ============================================================================================*/

var setStateProvince = function(jsStateProvinceName,jsStateFieldId)
{
	var stateOptions = document.getElementById(jsStateFieldId);

	if(stateOptions)
	{
		if(jsStateProvinceName > '' )
		{
			var stateOptionsLength = stateOptions.options.length;
			for(i=0; i<stateOptionsLength; i++)
			{
				if(stateOptions.options[i].value == jsStateProvinceName)
				{
					stateOptions.options[i].selected = true;
					break;
				}
			}
		}
		else
		{
			stateOptions.options[0].selected = true;
		}
	}
};


/* ============================================================================================*/
/*
This function sets the given hidden field to the given value.
*/
/* ============================================================================================*/

var setHiddenField = function(jsFieldID,jsValue)
{
	var fieldToSet = document.getElementById(jsFieldID);
	if(fieldToSet)
	{
		fieldToSet.value = jsValue;
	}
};


/* ============================================================================================*/
/*
This function sets the given hidden field to the value from a given field on the parent page.
*/
/* ============================================================================================*/

var setHiddenFieldFromParent = function(jsFieldID,jsParentFormFieldName)
{
	var parentFormFieldName	=	self.parent.document.getElementById(jsParentFormFieldName);
	if(parentFormFieldName)
	{
		var parentFormFieldValue	=	parentFormFieldName.value;
		var fieldToSet				=	document.getElementById(jsFieldID);
		
		if(fieldToSet)
		{
			fieldToSet.value = parentFormFieldValue;
		}
		
	}
	
};


/* ============================================================================================*/
/*
This function sets the style:display attribute on element.
*/
/* ============================================================================================*/

var toggleVisibility = function(jsElementID,jsIsVisible)
{
	var showElement	=	false;		// default is to hide element
	if(jsIsVisible)
	{
		showElement	=	jsIsVisible;
	}

	// set visibility on given element
	if(showElement == true)
	{
		$("#"+jsElementID).show();
	}
	else
	{
		$("#"+jsElementID).hide();
	}

};


/* ============================================================================================*/
/*
This function sets the checked attribute on a checkbox or radio button input field.
*/
/* ============================================================================================*/

var setCheckboxRadio = function(jsFieldID,jsValue)
{
	var checkedValue	=	false;
	if(jsValue == 1)
	{
		checkedValue	=	true;
	}
	
	var fieldToSet = document.getElementById(jsFieldID);
	if(fieldToSet)
	{
		fieldToSet.checked = jsValue;
	}
};


/* ============================================================================================*/
/*
This function sets the disabled attribute on a field.
*/
/* ============================================================================================*/

var setDisabled = function(jsFieldID,jsValue)
{
	var disabled	=	'';
	if(jsValue == 1)
	{
		disabled	=	'disabled';
	}
	
	var fieldToSet = document.getElementById(jsFieldID);
	if(fieldToSet)
	{
		fieldToSet.disabled = disabled;
	}
};


/******************************************************************************/
/* Populate data based on selection                                           */
/******************************************************************************/
var updateDisplayData = function(elementId,displayDataName)
{
	$("#" + elementId).load(displayDataName);
};


/******************************************************************************/
/* Return integer width of given string value                                 */
/******************************************************************************/
var getWidth = function(elementClass)
{
	// set local variables
	var returnWidth		=	0;
	var listOfClasses	=	[];
	
	listOfClasses	=	elementClass.split(' ');
	
	for(i=0;i<=listOfClasses.length;i++)
	{
		// evaluate input and set local variable
		if(listOfClasses[i]			==	'width_xTny')
		{
			returnWidth		=	returnWidth + 25;
		}
		else if(listOfClasses[i]	==	'width_Tny')
		{
			returnWidth		=	returnWidth + 50;
		}
		else if(listOfClasses[i]	==	'width_Sml')
		{
			returnWidth		=	returnWidth + 80;
		}
		else if(listOfClasses[i]	==	'width_Med')
		{
			returnWidth		=	returnWidth + 100;
		}
		else if(listOfClasses[i]	==	'width_Std')
		{
			returnWidth		=	returnWidth + 150;
		}
		else if(listOfClasses[i]	==	'width_Lrg')
		{
			returnWidth		=	returnWidth + 200;
		}
		else if(listOfClasses[i]	==	'width_xLrg')
		{
			returnWidth		=	returnWidth + 300;
		}
		else if(listOfClasses[i]	==	'width_Chk_JustCenter')
		{
			returnWidth		=	returnWidth + 51;
		}
		else if(listOfClasses[i]	==	'citationIcon')
		{
			returnWidth		=	returnWidth + 30;
		}
	}
	
	return returnWidth;
};


/******************************************************************************************/
/*
This function resets the page to show the Multimedia section.
*/
/******************************************************************************************/
var postRender = function()
{
	location.href='#idMultimediaList';
};


/******************************************************************************/
/* Deletes session variables when closing Edit Events Details                 */
/******************************************************************************/
var deleteEditDetailVariables = function()
{
	// set proxy information
	var genericFunctions	=	new genericFunctionsProxy();
	genericFunctions.setHTTPMethod("POST");
	genericFunctions.deleteEditDetailsVariables();
};


/******************************************************************************/
/* check valid email address                                                  */
/******************************************************************************/

var	isValidEmailAddress = function (jsEmailAddress)
{
	var regString		=	/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
	var matchedString	=	jsEmailAddress.match(regString);
	if (matchedString)
	{
		return true;
	}
	else
	{
		return false;
	}
};


/*==================================================================================================*/
/* This function looks through a list of radio buttons with the name equal to the input variable.   */
/* If one of the radio buttons is checked, its value is returned.  If no radio buttons are checked, */
/* the value of zero (0) is returned.                                                               */
/*                                                                                                  */
/* Inputs:                                                                                          */
/*		radioButtonName	-	The value of the name attribute of the list of radio buttons to check   */
/*                                                                                                  */
/* Outputs:                                                                                         */
/*		returnValue		-	If a radio button is checked, the value attribute of the checked radio  */
/*							button is returned.                                                     */
/*                                                                                                  */
/*							If no radio button is checked, zero (0) is returned.                    */
/*==================================================================================================*/
var getRadioValue	=	function(radioButtonName)
{
	// set return variable
	var returnValue		=	0;

	// get array of radio buttons
	var radioButtonArray	=	document.getElementsByName(radioButtonName);
	if (radioButtonArray)
	{
		// loop through radio buttons to see if any are selected
		for (var nextButton = 0; nextButton < radioButtonArray.length; nextButton++)
		{
			var button	=	radioButtonArray.item(nextButton);
			if (button.checked	==	true)
			{
				// set returnValue to the value of the checked button
				returnValue		=	button.value;
				break;
			}
		}
		
	}
	
	return returnValue;
	
};


/*==================================================================================================*/
/* This function formats the date in a Coldfusion cfgrid based on the column number sent in.        */
/*                                                                                                  */
/* Inputs:                                                                                          */
/*		gridName			-	The name of the cfgrid to format.                                   */
/*		arrDateGridColumn	-	An array of column numbers of the cfgrid to format.                 */
/* 								Column numbering starts at zero.                                    */
/*                                                                                                  */
/* Outputs:                                                                                         */
/*		None.                                                                                       */
/*==================================================================================================*/
var setDateRenderer	=	function(gridName,arrDateGridColumn)
{
	// get a pointer to the cfgrid
	mygrid = ColdFusion.Grid.getGridObject(gridName);
	
	// get a structure of the columns
	cm = mygrid.getColumnModel();
	
	// format the appropriate column
	for(nextCol=0; nextCol < arrDateGridColumn.length; nextCol++)
	{
		cm.setRenderer(arrDateGridColumn[nextCol], Ext.util.Format.dateRenderer('M d, Y'));
	}
	mygrid.reconfigure(mygrid.getDataSource(),cm);

};
		

