// JavaScript Document

var ERROR_COLOR = '#ffcccc'; // The color to highlight controls that have not been answered correctly.

/*************************************************************************************************************************/

// A simple, general form validation function. Checks text boxes, text area's, password fields, radio buttons, checkboxes and selects (not multiple selections though). 
// Every input on the form must have a value or a selection UNLESS their 'display' style is set to none, or the input is DISABLED or the className is set to 'not_required'.
function validateForm(form)
{
	valid = true;
		
	// Loop through all controls on the form and turn them to white, in case they were changed to the error color.
  for (i = 0; i < form.length; i++)
  {			
		if (form[i].style.backgroundColor == ERROR_COLOR)
		{
			form[i].style.backgroundColor = "#fff";			
		}
	}
	
	// Loop through all controls on the form.
  for (i = 0; i < form.length; i++)
  {		 
    // Check control if it is 'text' or 'textarea' or password field.
    if ((form[i].type == 'text' || form[i].type == 'textarea' || form[i].type == 'password') && (form[i].style.display != "none" && !form[i].disabled  && form[i].className != 'not_required'))
    {
      var valueOfInput = form[i].value;
      var whitespace = " \t\n\r";
      
      // Check value is not null or empty.
      if ((valueOfInput == null) || (valueOfInput.length == 0))
      {
        valid = false;
        form[i].style.backgroundColor = ERROR_COLOR;
      }
			else // Check for at least one valid character.
			{
				validChar = false;
				
				// Search input value for chars that are not whitespace.
				for (j = 0; j < valueOfInput.length; j++)
				{
					var c = valueOfInput.charAt(j);
					if (whitespace.indexOf(c) == -1)  // A letter has been found!
					{
						validChar = true;
						break;
					}
				} // End of for loop.
				
				// See if a valid char was found, if not highlight field and set 'valid' to false.
				if (!validChar)
				{
					valid = false;
	        form[i].style.backgroundColor = ERROR_COLOR;
				}
			} // End of checking for some data in the field.
    } // End of if input is text or textarea.

    // Check that radio buttons and checkboxes have something checked within their group.
    else if ((form[i].type == 'radio' || form[i].type == 'checkbox') && form[i].style.display != "none" && form[i].className != 'not_required')
    {
      var groupName = form.elements[i].name;
      var numInGroup = form.elements[groupName.toString()].length;
      var lastItemIndex = i + numInGroup - 1;
			var selectionMade = false;
   
      // Search through this group for a checked item.
      for (j = i; j <= lastItemIndex; j++)
      { 
        if (form[j].checked)
        {
          selectionMade = true;
          break; 
        }
      } // End of for loop.
			
			// See if a selection was made, if not highlight inputs and set 'valid' to false.
			if (!selectionMade)
			{
				valid = false;
				
				for (k = i; k <= lastItemIndex; k++)
				{
					form[k].style.backgroundColor = ERROR_COLOR;
				}
			} // End of 'if' selection made.	
			
			// Increment i.
			i = lastItemIndex;
			
    } // End of if 'radio' or 'checkbox'.		
		
		// Check that an item has been selected from any lists in the form.
		else if (form[i].type == 'select-one' && form[i].style.display != "none" && form[i].className != 'not_required')
		{
			itemSelected = false;
			
			for (j = 0; j < form[i].options.length; j++)
			{
				// Check for a value.
				if (form[i].value != '')
				{
					itemSelected = true;
				}
			} // End of for loop.
			
			if (!itemSelected)
			{				
				form[i].style.backgroundColor = ERROR_COLOR;
				valid = false;
			}
		} // End of if form input is of type 'select'.
  } // End of looping through the controls in the form.

	// Check the valid var and if false, return a message.
	if (valid == false)
	{
		alert("Please fill in the higlighted fields before submitting the form.");
		return false;
	}

	return true;

} // End of validateForm().

/*************************************************************************************************************************/

// Show the rows of past events in the calendars.
function showPastEvents(calendarType)
{
	var tableRows = document.getElementsByTagName("tr");
	for (i = 0; i < tableRows.length; i++)
	{
		var tableRow = tableRows[i];
		var rowCalendarType = tableRow.id.split("_");
		
		if (rowCalendarType[0] == calendarType)
		{
			if (tableRow.style.display == "block")
			{
				tableRow.style.display = "none"
			}
			else
			{
				tableRow.style.display = "block"
			}
		} // End of checking for calendar_id
		
	} // End of loop.
	
} // End of showPastEvents().

/*************************************************************************************************************************/


