/**

-- selectAllOptions --

formElement = name of form option list to select all in

Selects all options in an option select field on submit
Field must all multiple selections

*/

	function selectAllOptions(formElement) {
 
		var i;
	
	  	for(i = 0; i < formElement.length; i++) {
		    	formElement.options[i].selected = true;
	 	} 
	  	
		return;
		
	 }
 
/**

-- addItemToList --

theForm = form that input elements exist in

Adds start date, end date, start time, and end time fields to a 
larger option select list

*/

	function addItemToList(theForm) {
 
	 	var selectLength;
	 	var newSelect;
	 	var theStartDate;
	 	var theEndDate;
	 	var theStartTime;
	 	var theEndTime;
		var theTimeTBA;
		
		// Validate Field Contents

			//--------- FIX FORMATTING

			// format start date
			if(notEmpty(theForm.bulletinStartDate.value)==true){
				theForm.bulletinStartDate.value = formatDate(theForm.bulletinStartDate.value)
			}


			// format end date
			if(notEmpty(theForm.bulletinEndDate.value)==true){
				theForm.bulletinEndDate.value = formatDate(theForm.bulletinEndDate.value)
			}

				//--------------------------------

			// Check for startDate
			if(notEmpty(theForm.bulletinStartDate.value)==false){
				alert("A Start Date is required");
				theForm.bulletinStartDate.focus();
				return false;
			}

			// Check formatting on Start Date
			if ((notEmpty(theForm.bulletinStartDate.value)==true) && (validateDate(theForm.bulletinStartDate.value)==false)){
				alert("The Start Date must be correctly formatted");
				theForm.bulletinStartDate.focus();
				return false;
			}	

			// Check formatting on End Date
			if ((notEmpty(theForm.bulletinEndDate.value)==true) && (validateDate(theForm.bulletinEndDate.value)==false)){
				alert("The End Date must be correctly formatted");
				theForm.bulletinEndDate.focus();
				return false;
			}	
	
			// End Date cannot occur before Start Date
			//if ((((notEmpty(theForm.bulletinEndDate.value)==true) && (validateDate(theForm.bulletinEndDate.value)==true)) && ((notEmpty(theForm.bulletinStartDate.value)==true) && (validateDate(theForm.bulletinStartDate.value)==true))) && (theForm.bulletinEndDate.value) < (theForm.bulletinStartDate.value)) 
			//{
			//	alert("The End Date cannot occur before the Start Date");
			//	theForm.bulletinEndDate.focus();
			//	return false;
			//}

			// Check formatting on Start Time
			if ((notEmpty(theForm.bulletinStartTime.value)==true) && (validateTime(theForm.bulletinStartTime.value)==false)){
				alert("The Start Time must be correctly formatted");
				theForm.bulletinStartTime.focus();
				return false;
			}
			
			// Check formatting on End Time
			if ((notEmpty(theForm.bulletinEndTime.value)==true) && (validateTime(theForm.bulletinEndTime.value)==false)){
				alert("The End Time must be correctly formatted");
				theForm.bulletinEndTime.focus();
				return false;
			}

			// If End Date is not empty or on same day as start date, then the end time must be greater
			//if ((notEmpty(theForm.bulletinEndTime.value)==true) && ((formatTimeAsMilitary(theForm.bulletinEndTime.value)) < (formatTimeAsMilitary(theForm.bulletinStartTime.value))) {
			//	alert("The End Time must occur after the Start Time");
			//	theForm.bulletinEndTime.focus();
			//	return false;
			//}

		// Get Field Contents
 		theStartDate = theForm.bulletinStartDate.value;
 		theEndDate = theForm.bulletinEndDate.value;
 		theStartTime = theForm.bulletinStartTime.value;
 		theEndTime = theForm.bulletinEndTime.value;
		theTimeTBA = false //theForm.bulletinTimeTBA.checked;		
 		
		// Place new data in option select list
	 	selectLength = theForm.bulletinDates.options.length;
	 	newSelect = selectLength + 1;

 		theForm.bulletinDates.length = newSelect;

		// Build text and value for list
		var theSelectText;
		var theSelectValue;

		theSelectText = theStartDate

		if ((theEndDate != '') && (theEndDate != theStartDate)) {
			theSelectText = theSelectText + ' - ' + theEndDate
		}

		if (theStartTime != '') { 
			theSelectText = theSelectText + ', '
			if ((theEndTime == '') || (theEndTime == theStartTime)) {
				theSelectText = theSelectText + 'at ' + theStartTime
			} else {
				theSelectText = theSelectText + 'from ' + theStartTime + ' to ' + theEndTime
			}
		} else {
			if(theTimeTBA==true){
				theSelectText = theSelectText + ', Time To Be Announced'
				theTimeTba = true
			}
		}

		theSelectValue = theStartDate + '+' + theEndDate + '+' + theStartTime + '+' + theEndTime + '+' + theTimeTBA;
		
		//alert(theSelectValue)

 		theForm.bulletinDates.options[(newSelect-1)].text = theSelectText
 		theForm.bulletinDates.options[(newSelect-1)].value = theSelectValue
		
		// Reset Fields
 		theForm.bulletinStartDate.value = '';
 		theForm.bulletinEndDate.value = '';
 		theForm.bulletinStartTime.value = '';
 		theForm.bulletinEndTime.value = '';
		//theForm.bulletinTimeTBA.checked = false;

		//theForm.bulletinStartDate.value = formatDate(theStartDate,"MM/DD/YY");
		//return true;

		return; 
	
 	}
 
/**

-- removeItemFromList

theForm = form that option elements exist in

Removes a specific option from the select list

*/

	function removeItemFromList(theForm) {

		if ((theForm.bulletinDates.selectedIndex != -1) && (theForm.bulletinDates.options[theForm.bulletinDates.selectedIndex].value != '')) {

		 	var numToRemove;
		 	var selectLength;
		 	var newSelect;
		
	 		selectLength = theForm.bulletinDates.options.length;
	 		newSelect = selectLength - 1;
	 		numToRemove = theForm.bulletinDates.selectedIndex;
	 
	 		theForm.bulletinDates.options[numToRemove] = null;
		
		}
	 
	 }

/**

-- modifyItemInList

theForm = form that option elements exist in

Allows modification of specific item in list
***) Alerts user?
1) removes item from list
2) places item in fields

*/

	function modifyItemInList(theForm) {

		if ((theForm.bulletinDates.selectedIndex != -1) && (theForm.bulletinDates.options[theForm.bulletinDates.selectedIndex].value != '')) {
    		
			var response = confirm('Date information to be modified will be placed in the fields above, writing over any previously existing data. Do you wish to perform this action?');

	     		if (response) {
				
				// Get Field Contents
	 			theSelectedDate = theForm.bulletinDates.selectedIndex;
	 			theDateValues = theForm.bulletinDates.options[theSelectedDate].value;	
				//alert(theDateValues)	
				theDateValues = theDateValues.split('+')
	
				// Set Fields
	 			theForm.bulletinStartDate.value = theDateValues[0];
	 			theForm.bulletinEndDate.value = theDateValues[1];
	 			theForm.bulletinStartTime.value = theDateValues[2];
	 			theForm.bulletinEndTime.value = theDateValues[3];
				if(theDateValues[4]!="false"){
					//alert("not false");
					//theForm.bulletinTimeTBA.checked = true;
				} else {
					//alert("is false");
					//theForm.bulletinTimeTBA.checked = false;
				}			

				// remove Item from list	
				removeItemFromList(theForm)
			
			} 
		}
	}
 
/**

-- usePrevContact

theForm = form that option elements exist in

Places Option List Data in contact info fields

*/

	function usePrevContact(theForm) {
	
		userData = theForm.contactSelect.options[theForm.contactSelect.selectedIndex].value;

		switch (userData){
			
			case '-- Select Existing Contact Information --' :
		
				theForm.bulletinContactName.value="";
				theForm.bulletinContactPhone.value="";
				theForm.bulletinContactEmail.value="";
				theForm.bulletinContactFax.value="";
				break;

			case '-- Use My Contact Information --' :

				theForm.bulletinContactName.value=userData[0];
				theForm.bulletinContactPhone.value=userData[1];
				theForm.bulletinContactEmail.value=userData[2];
				theForm.bulletinContactFax.value=userData[3];
				break;	

			default:

				userData = userData.split('*&*');
	
				theForm.bulletinContactName.value=userData[0];
				theForm.bulletinContactPhone.value=userData[1];
				theForm.bulletinContactEmail.value=userData[2];
				theForm.bulletinContactFax.value=userData[3];
		}

	}




/**

-- moveItemInList

theForm = form that option elements exist in
sourceList = this list
targetList = list to move objects to

Removes a specific option from the selected list and moves it to the list specified
Finishes by calling CleanSelect to remove empty options from the sourceList

*/

	function moveItemInList(sourceList,targetList) {

	  	for(i = 0; i < sourceList.options.length; i++) {

		    	if (sourceList.options[i].selected && sourceList.options[i].value != "") { 

					var newData = new Option();

				// Get Data - Get Field Contents

	 	 			var tempValueData = sourceList.options[i].value.split("+");
		  			var tempTextData = sourceList.options[i].text.split(" (");

					newData.value = tempValueData[0];
					newData.text = tempTextData [0];
	 		
				// Add Data - Place new data in option select list
			 		
					targetList.options[targetList.options.length] = newData;
	
				// Remove Data
		 
		 			sourceList.options[i].text = "";
		 			sourceList.options[i].value = "";
	
				// Deselect
				
					sourceList.options[i].selected = false;

				
				// If the sourcelist is folderUsers -- clear the checkboxes and remove permissions
				// data from the user value - 

				if(sourceList.name=="folderUsers"){

					//clearPerms(this.form);
					//permCheck(sourceList,sourceList.form);
					//trace("blah");

				}
	
			}

		}

		CleanSelect(sourceList);

	}

/**

-- CleanSelect

sourceList = this list

Removes empty options from the sourceList


*/

	function CleanSelect(sourceList)  {
	
		for(var i=0; i < sourceList.options.length; i++) {
	
			if(sourceList.options[i].value == "")  {

				for(var j=i; j < sourceList.options.length-1; j++)  {

					sourceList.options[j].value = sourceList.options[j+1].value;
					sourceList.options[j].text = sourceList.options[j+1].text;
				}
	
				var ln = i;	
				break;

			}
		
		}
		
		if(ln < sourceList.options.length)  {
			
			sourceList.options.length -= 1;
			
			CleanSelect(sourceList);
  	 	}
	}

/**

-- showHideFolderAttrib --

formElement = name of form option list

Loops through the form and hides the unselected divs, while selecting the selected item

*/

	function showHideFolderAttrib(formElement){
 
		var i;
	

	  	for(i = 0; i < formElement.length; i++) {

			if(formElement.options[i].value!=0 && formElement.options[i].value!=""){
				
				varDivName = formElement.options[i].text;
			
				if(formElement.options[i].selected == true){
					showLayer(varDivName);
					}else{
					hideLayer(varDivName);
					}	    	
	 			} 
			
			}
		}

/**

-- clearFolderTypeData --

attributeNumber = attributeNumber to clear

Clears data from specific attribute when creating or modifying a folder type

*/

	function clearFolderTypeData(attributeNumber, theForm){
 		
		var nameField = "attributeName" + attributeNumber ;
		var typeField = "attributeType" + attributeNumber ;
		var dataField = "attributeData" + attributeNumber ;
		var reqField = "attributeReq" + attributeNumber ; 

		if((theForm[nameField].value=='')&&(theForm[typeField].selectedIndex==0)&&(theForm[dataField].value=='')&&(theForm[reqField][1].checked=true)){
		
		} else {

			if(confirm('Are you sure you wish to clear this Attribute data?')){
					
				theForm[nameField].value = '';
				theForm[typeField].selectedIndex = 0;
				theForm[dataField].value = '';
				theForm[reqField][1].checked = true;

				}
			}	
		}
