// /* Locations
// --------------------------------------------------*/
function getLocations(id) {
  $('courseSearch').style.display = 'none';
  $('courseSearchNote').style.display = 'none';
  $('locations').style.display = 'none';
//-------------------
/*
Update the courseSearchText box with different content when a county is selected.
*/
//-------------------
 $('courseSearchText').innerHTML = '<h2>Course Search</h2>\n';
 $('courseSearchText').innerHTML += '<p>Search for sport courses in your area by using the map above</p>\n';

  loader.toggle();
  var myAjax = new Ajax(
    "/ajax/html_locations.php",
    {
      method: "get",
      data: { county: id },
      update: "locations",
      onComplete: function() {
        $('countys').style.display = "none";
        showRegion();
        $$('.custom-tip').each(
          function(e) {
            e.style.display="none";
          }
        );
      },
      evalScripts: true
    }
  ).request();
}

function updateFee() {
  var myAjax = new Ajax(
    "/ajax/update_fee.php",
    {
      method: "get",
      data: {fee: $('fee').value},
      update: "total"
    }
  ).request();
}

/* Sports
--------------------------------------------------*/
function getSports(id,sport,clear) {
  var data2Send = {location: id};
  if (sport) {
    data2Send = {location: id, sport: sport};
  }
  $('results').addClass("spinner");
  var myAjax = new Ajax(
    "/ajax/html_sports.php",
    {
      method: "get",
      data: data2Send,
      update: "sports",
      onComplete: function() {
        $('sports').style.display = "block";
        $('courseSearchText').style.display = "none";
        $('results').removeClass("spinner"); 
        if (clear) { 
          $('results').innerHTML = '<p>Please select a sports tab above to display the relevent events.</p>'; 
        }
      },
      evalScripts: true
    }
  ).request();
}

function showSports(clear) {
  $('sports').style.display = "block";
  $('courseSearchText').style.display = "none";
  $('results').removeClass("spinner"); 
  if (clear) { 
    $('results').innerHTML = '<p>Please select a sports tab above to display the relevent events.</p>'; 
  }
}

/* Events
--------------------------------------------------*/

function getEvents(sport,location) {
  $('results').innerHTML = '';
  $('results').addClass("spinner");
  var myAjax = new Ajax(
    "/ajax/html_events.php?",
    {
      method: "get",
      data: { sport: sport, location: location },
      update: "results",
      onComplete: function() { 
        $('results').removeClass("spinner"); 
        getSports(location,sport);
      },
      evalScripts: true
    }
  ).request();
}


var loader = null;
var loading = 0;

function showhide()
{
  if (loading == 1)
  {
    setTimeout(loader.toggle(), 10000);
    loading = 0;
  }
}

function showSpinner()
{
  $('loading').removeClass("error");
  $('loading').innerHTML = '<span style="vertical-align: middle;">Loading...&nbsp;&nbsp;</span><img src="/images/ajax-loader.gif" style="vertical-align: middle;" />';
  loader.hide().toggle();
}

function tryme()
{
  showhide();
}

/* Login form
--------------------------------------------------*/
function login() {
  var myAjax = new Ajax("/ajax/login.php", {
    data: $("login"),
    update: "loginFeedback",
    method: "get",
    onComplete: function() {
      if (isIn(this.response.text, 'Logged in')) {
        location.reload();
      } else {
        $('loginFeedback').addClass("error"); 
        loginFeedback.hide().toggle();
      }
    } 
  }
).request();
}

/* Logout
--------------------------------------------------*/

function logout() {
  var myAjax = new Ajax('/ajax/logout.php', {
    onComplete: function() {
      window.location = "/";
    },
    evalScripts: true
  }).request();
}


/* isIn
--------------------------------------------------*/
function isIn(s, x) {
  if (s.toString().match(x.toString()) !== null) {
    return true;
  } 
  else {
    return false;
  }
}

/* Booking a course
--------------------------------------------------*/

//stage 1

function bookCourse(id) {
  $('contentMid').addClass("spinner");
  var myAjax = new Ajax(
    "/ajax/ajax_book_course.php",
    {
      method: "get",
      data: { course: id },
      update: "course",
      onComplete: function() {
        showBooking();
      },
      evalScripts: true
    }
  ).request();
}

function showEvents() {
  $('contentMid').removeClass("spinner");
}

//stage 2

function chooseChildren() {
  $('contentMid').addClass("spinner");
  var myAjax = new Ajax(
    "/ajax/ajax_medical_records.php",
    {
      method: "get",
      data: "chooseChild",
      update: "course",
      onComplete: function() {
        alertContents2();
      },
      evalScripts: true
    }
  ).request();
}

function alertContents2() {
  $('contentMid').removeClass("spinner");
}

// =================================

// data  you can write parameters here.  Can be a querystring, an object or a Form element.
// onComplete  function to execute when the ajax request completes.
// update  $(element) to insert the response text of the XHR into, upon completion of the request.
// evalScripts   boolean; default is false.  Execute scripts in the response text onComplete.  When the response is javascript the whole response is evaluated.
// evalResponse  boolean; default is false.  Force global evalulation of the whole response, no matter what content-type it is.
// Example

// var myAjax = new Ajax(url, {method: 'get'}).request();

var Ajafx = Ajax.extend({

  options: {
    data: null,
    update: null,
    onComplete: null,
    evalScripts: true,
    method: "get",
    evalResponse: false
  },

  initialize: function(url, options){
    this.addEvent('onSuccess', this.onComplete);
    this.setOptions(options);
    this.options.data = this.options.data || this.options.postBody;
    if (!['post', 'get'].contains(this.options.method)){
      this._method = '_method=' + this.options.method;
      this.options.method = 'post';
    }
    this.parent();
    this.setHeader('X-Requested-With', 'XMLHttpRequest');
    this.setHeader('Accept', 'text/javascript, text/html, application/xml, text/xml, */*');
    this.url = url;
    showSpinner();
  }
});



// =========================
var loginFeedback = null;

/* Registration form - Mootools
--------------------------------------------------*/
window.onDomReady(function() {
  var Tips2 = new Tips($$('.tips'), {
    initialize:function(){
      this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 500, wait: false}).set(0);
    },
    
    onShow: function(toolTip) {
      this.fx.start(1);
    },
    
    onHide: function(toolTip) {
      this.fx.start(0);
    },
    
    className: 'custom'
  });

  loader = new Fx.Slide('loading').hide();
  $('loading').style.display = "block";
  
  if ($('loginFeedback')) {
    loginFeedback = new Fx.Slide('loginFeedback');
    loginFeedback.hide();
    $('loginFeedback').style.display = 'block';
  }

  if ($("registration")) {
    $("registration").addEvent('submit', function(e) {
      /**
      * Prevent the submit event
      */
      new Event(e).stop();

      /**
      * This empties the feedback and shows the spinning indicator
      */
      var feedback = $('feedback').empty().addClass('ajax-loading');

      /**
      * send takes care of encoding and returns the Ajax instance.
      * onComplete removes the spinner from the log.
      */
      this.send({
        update: feedback,
        evalScripts: true,
        onComplete: function() {
          feedback.removeClass('ajax-loading');
        }
      });
      new SmoothScroll().scrollTo(0,0);
      
      return false;
    });
  }

});

function submitRegistration() {
  var feedback = $('feedback').empty().addClass('ajax-loading');

  $("registration").send({
    update: feedback,
    evalScripts: true,
    onComplete: function() {
      feedback.removeClass('ajax-loading');
    }
  });
  new SmoothScroll().scrollTo(0, 0);

}

var slideyBooking = null;

function addChildrenToCourse() {
  var myAjax = new Ajax(
    "/book.php",
    {
      method: "post",
      data: $('bookCourse'),
      update: "bookingForm"
    }
  ).request();
  new SmoothScroll().scrollTo(0,400);
}