
function ajaxConfirm(message, url, options)
{
  var answer = confirm(message);
  if (answer)
  {
    new Ajax.Request(url, options);
  } 
  return false;
}

function toggleChildDOBValidation(checkbox)
{
  form = LiveValidationForm.getInstance(checkbox.form);
  month = findLiveValidation(form.fields, 'child_dob_month');
  day = findLiveValidation(form.fields, 'child_dob_day');
  year = findLiveValidation(form.fields, 'child_dob_year');
  
  if(checkbox.checked)
  {
    month.add( Validate.Presence, {failureMessage: 'Required.'} ).validate();
    day.add( Validate.Presence, {failureMessage: 'Required.'}  ).validate();
    year.add( Validate.Presence, {failureMessage: 'Required.'}  ).validate();
  } else {
    month.removeAllValidations();
    day.removeAllValidations();
    year.removeAllValidations();
  }
  
}

function findLiveValidation(fields, element)
{
  for( var i = 0, len = fields.length; i < len; i++ )
  {
    if (fields[i].element.id == element) return fields[i];
  }
  
}

function toggleEventSignupFields()
{
  if ($('event_signup_type').value == 'team') {
    $('signup_team').show();
  } else {
    $('signup_team').hide();
  }
}

/*
 * FORM LIST FUNCTIONS
 */

function addFormList(form)
{
  new Ajax.Updater('form_list', form.action + '&format=js',
                    {parameters: Form.serialize(form),
                     insertion: 'bottom',
                     evalScripts: true}
                  );
  return false;
}

function deleteFormList(link)
{
  var answer = confirm('Are you sure?');
  if (answer)
  {
    new Ajax.Request(link.href + '&format=js')
    Element.up(link, 'li').hide();
  }
  return false;
}

function editFormList(form)
{
  content = Element.up(form, 'li').down('.content')
  
  new Ajax.Updater(content, form.action + '&format=js',
                    {parameters: Form.serialize(form),
                     evalScripts: true}
                  );
  toggleFormListEdit(form);
  return false;
}

function toggleFormListEdit(link)
{
  Element.up(link, 'li').down('.actions').toggle();
  Element.up(link, 'li').down('.content').toggle();
  Element.up(link, 'li').down('.form').toggle();
  return false
}

function openFormListEdit(link)
{
  Element.up(link, 'li').down('.actions').hide();
  Element.up(link, 'li').down('.content').hide();
  Element.up(link, 'li').down('.edit_form').show();
  return false
}

function closeFormListEdit(link)
{
  Element.up(link, 'li').down('.actions').show();
  Element.up(link, 'li').down('.content').show();
  Element.up(link, '.edit_form').hide();
  return false;
}

/*
 * Event question
 */

function addEventQuestionOption(link)
{
  list = Element.previous(link);
  content = '<li>' + list.down('.hidden').innerHTML + '</li>';
  list.insert(content);
  Sortable.create(list);
}

function toggleEventQuestionOptions(selection)
{

  selection = Element.extend(selection);

  options = selection.up('form').down('.event_question_options_container');

  
  if (selection.value == 'text')
  {
    options.hide();
  } else {
    options.show();
  }
}

/*
 * REGISTRATION SCRIPTS
 */

function swapParticipant(check)
{
  check = Element.extend(check);
  form = check.up('form');
  url = form.action + '&participant_id=' + check.value;
  new Ajax.Request(url);
  
  ul = check.up('ul');
  if (ul.id == 'selected_participants')
  {
	  check.checked = false;
		
    other_container = $('available_participants');
  } else {
    other_container = $('selected_participants');
  }
  li = check.up('li');
  other_container.insert(li);
}

function toggleCheckboxByClass(box, class_name)
{
  elements = $$('input[type=checkbox].' + class_name);
  elements.each(function(el) {
    el.checked = box.checked;
  });
}

function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("#");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  } 
}

function toggleAdvancedSearch(array) {
	count=0;
	for (i=0;i<array.length;i++) {
		if ($(array[i]).value != 0) {
			count++;
		}
		if (count>0 || getQueryVariable("advanced") == 'true') {
			$("advanced_search").style.display = 'block';
			$("basic_search_link").style.display = 'block';
			$("advanced_search_link").style.display = 'none';
		} else {
			$("advanced_search").style.display = 'none';
			$("basic_search_link").style.display = 'none';
			$("advanced_search_link").style.display = 'block';
		}
		
	}
}


function resetSearchForm()
{
		
	$('date_month').selectedIndex = 0;
	$('date_day').selectedIndex = 0;
	$('date_year').selectedIndex = 0;
	$('signup_type').selectedIndex = 0;
	$('order').selectedIndex = 0;
	$('gender').selectedIndex = 0;
	$('category').selectedIndex = 0;

	$('fee').value = '';
	$('max_miles').value = '';
	$('location').value = '';
	$('terms').value = '';
	$('age_low').value = '';
	$('age_high').value = '';
}
















