var timesHovered = 0;
var nonCompetitorAvailable = true;
var noEquipment = false;

function showSubmenu(id, el) {
  $('#menu a').removeClass('clicked');
  $('#menu li').removeClass('bordered');
	
	tokens = el.parent().attr('id').split('-');	
	
  timesHovered++;
  el.parent().parent().one('mouseout', function(){
     timer = setTimeout('doMouseOut(' + tokens[1] + ')', 500);
  });
  el.addClass('clicked');
  el.parent().parent().addClass('forceshow');
  el.parent().addClass('bordered');
  $('#transparentbg').addClass('forceshow');
  $('> a', el.parent().parent().parent()).addClass('forcehovered');
  $('#submenu > div').hide();
  $('body').append('<div class = "submenulink"></div>');
  var of = el.offset();
  $('.submenulink').css({top : of.top - 1, left : of.left + 230 });
  $('#' + id).show();
  $('#submenu').fadeIn(400);
  
}

function showProductText(id) {
  $('.productside > div').hide();
  $('#' + id).show(); 
  $('.rightside, .news_main').jScrollPane();
}

function showNewsSection(id) {
	$('#news_container span').hide();
	$('#' + id).show();
	$('.rightside, .news_main').jScrollPane();
}

function showNew(id) {
	$('.news_main > div').hide();
	$('#new-' + id).show();
}

function doMouseOut(item_id) {
  
  if(!isIn) {
	  	 timesHovered--;
	  	 if(timesHovered == 0)
	  	 {
	         $('#menu ul').removeClass('forceshow');
	         $('.submenulink').remove();  
	         $('> a', $('#menuitem-' + item_id).parent().parent()).removeClass('forcehovered');
	 		 $('#menuitem-' + item_id + ' a').removeClass('clicked');
	 		 $('#menuitem-' + item_id).removeClass('bordered');
	 		 $('#transparentbg').removeClass('forceshow');
	 		 $('#transparentbg').hide();
	         $('#submenu').fadeOut(400);  
	  	 }
  }
  
}

function forgotPassword()
{
	if($('#email').val().length > 0)
	{
		$.ajax({
			url : 'api/forgot',
			type : 'post',
			data : { email : $('#email').val() },
			dataType: 'json',
			success : function(data) {
				if(data.success == 1) {
					alert('New password has been sent to your email address!');
				}
				else {
					alert('Could not find entered email address!');
				}
			}
		});
	}
	else 
	{
		alert('Please, enter your email address!');
	}
}

function reservationRemoveAddon(addonId, reservationId)
{
	$.ajax({
		url : '/api/removeAddon',
		type : 'post',
		data : { id : addonId, reservation_id : reservationId },
		dataType : 'json',
		success : function(data) {
			if(data.success == 1) {
				showForm(4);
			}
		}		
	});
}

function addPerson()
{
	$.ajax({
		url : '/api/addPerson',
		type : 'post',
		data : $('#bookingForm1').serialize(),
		dataType : 'json',
		success : function(data) {
			if(data.success == 1) {
				window.location = '/reservation?goStep=4';
			}
		}
	});
}

function showForm(id) {
  if( id > Reservation.lastStep + 1) {
	  alert('Please, finish the preceeding forms first!');
  }
  else {
	  
	  $('form > div').hide();
	  $('#step-' + id).show(200);
	  $('.product_menu li a').removeClass('clicked');
	  $('#link-' + id).addClass('clicked');
	  
	  
	  if(id == 4) {  
		  
		$.ajax({
			type : 'post',
			url : '/api/itinerary',
			data : {},
			dataType : 'json',
			success : function(data) {
				
				var htmlToInsert = '<p>Customer: <br />';
				for(i in data.persons) {
					htmlToInsert += '<strong>' + data.persons[i]['name'] + ', ' + data.persons[i]['address'] + '</strong><br />';
				}
				htmlToInsert += 'Party of: ' + data.party + ', Confirmation No.: ' + data.refno +'</p>';
				htmlToInsert += '<p class = "itineraryOptions">'
				if(data.noCompetitor != 'yes')
				{
					htmlToInsert += '<a class = "button" href = "/reservation/competitor/' + data.programs[0].id + '">Add Competitor</a>';
				}
				if(data.nonCompetitor == 'yes')
				{
					htmlToInsert += '<a href = "javascript: void(0);" onclick = "addNonCompetitor();" class = "button">Add Non-Competitor</a>';
				}
				htmlToInsert += '</p>';
				htmlToInsert += '<div class = "clearer"></div>';
				for(i in data.programs)
				{
				htmlToInsert += '<p class = "options"><span>EUR ' + data.programs[i].price + '</span></p>';
				htmlToInsert += '<p><strong>' + data.programs[i].name + '</strong><br>' + data.programs[i].dates + '</p>';
				}
				
				htmlToInsert += '<p class = "itineraryOptions"><a class = "button" href = "javascript: void(0);" onclick = "helpMe(\'new\')">Add Program</a></p>';
				
				if(!noEquipment)
				{
					htmlToInsert += '<p><strong>Equipment:</strong><br />'
					htmlToInsert += 'Ronan Pensec Travel Cycling Jersey, Size: ' + data.jersey ;
				}
				
				if(data.bikes.carbon > 0)
				{
					htmlToInsert += '<br />Carbon Rental Bike(s) - ' + data.bikes.carbon + 'x EUR ' + data.bike_carbon +  '';
				}
				if(data.bikes.aluminium > 0)
				{
					htmlToInsert += '<br />Aluminium Rental Bike(s) - ' + data.bikes.aluminium + 'x EUR ' + data.bike_aluminium + '';
				}
				htmlToInsert += '</p>';
				
				
				
				for(i in data.addons) {
					htmlToInsert += '<p class = "options"><img src = "/images/itinerary_delete.png" alt = "Delete" title = "Delete" onclick = "reservationRemoveAddon(' + data.addons[i]['id'] + ', ' + data.addons[i]['reservation_id'] + ');" /><img src = "/images/itinerary_new.png" alt = "Edit" title = "Edit" onclick = "showForm(1);"/><span>EUR ' + data.addons[i]['price'] + '</span></p>';
					htmlToInsert += '<p><strong>' + data.addons[i]['name'] + '</strong><br />' + data.addons[i]['dates'] + '</p>';
				}
				
				htmlToInsert += '<p><strong>Coupon Code</strong><br /><input type = "text" name = "coupon_code" id = "coupon_code" value = "enter your coupon code" class = "" style = "margin-top: 10px;" /><br /><a href = "javascript: void(0);" onclick = "validateCoupon();" class = "button" style = "width: 90px; margin-top: 10px;">Use Code</a></p>';
				
				htmlToInsert += '<p class = "options">EUR <strong>' + data.total + '</strong></p><p><strong>Total</strong>:</p>';
				htmlToInsert+= '<p class = "options">EUR ' + data.balance + '</p><p>Balance to pay:</p>';
				
				$('#itinerary > div').html(htmlToInsert);
				
				$('#itinerary > div').jScrollPane();
			}
		});		  
	    
	  }
	  
  }
}

var thumbPage = null;

var nextPage = null;
var previousPage = null
var lastPage = null;

function updateThumbnails(pageNo) {
	
	$.ajax({
		url : 'media/thumbnails',
		data : { page:pageNo },
		dataType : 'json',
		type : 'post',
		success : function(data) {
			var htmlToInsert = '';
			for(i in data.files) {
				htmlToInsert += '<div><img src = "/files/' + data.files[i]['path'] + '" alt = "news image" width = "100" height = "70" /><a href = "/gallery"><span>' + data.files[i]['heading'] + '</span><br /></a></div>';
			}
			$('#imgNews').html(htmlToInsert);
			$('#thumbnailsCounter').html('Items ' + (pageNo * 3 - 2) + '-' + pageNo * 3 + ' of ' + data.count);
			if(data.last > pageNo)
				nextPage = pageNo + 1;
			else
				nextPage = pageNo;
			if(pageNo > 1)
				previousPage = pageNo - 1;
			else
				previousPage = 1;
			lastPage = data.last;
		}
	})
	
}

function validateCoupon() {
	
	$.ajax({
		url : 'api/validatecoupon',
		data : { code : $('#coupon_code').val() },
		dataType : 'json',
		type : 'post',
		success : function(data) {
			
			if(data.success == 1) {
				alert('Coupon accepted, updating your price...!');
				showForm(4);
			}
			if(data.success == 2) {
				alert('We are sorry, but the code you have entered is not valid!');
			}
			if(data.success == 3) {
				alert('We are sorry, but you have ran out of uses for the code entered!');
			}
			
		}
	});
	
}

var isIn = false;
var timer = '';
var timer2 = '';

$(document).ready(function(){

  $('.slideshow .controls img').click(function(){
      var tokens = $(this).attr('id').split('-');
      $('img', $(this).parent()).each( function() {
        var tokens2 = $(this).attr('id').split('-');
        $(this).attr('src', './images/slideshow-' + tokens2[2] + '-ina.png');
      });
      $(this).attr('src', './images/slideshow-' + tokens[2] + '-a.png');
      $('.images img', $(this).parent().parent()).hide();
      $('#slideshow-img-' + tokens[2]).fadeIn(600);
  });
  
  $('#submenu').bind('mouseenter', function(){
     isIn = true;
  });
  
  $('#submenu').bind('mouseleave', function(){
     isIn = false;
     $('#menu ul').removeClass('forceshow');
     $('#menu a').removeClass('forcehovered');
     $('#menu a').removeClass('clicked');
     $('#menu li').removeClass('bordered');
     $('#transparentbg').removeClass('forceshow');
     if($('#menu li ul:visible').length < 1) {
    	 $('#transparentbg').removeClass('hide');
     }
     $('#transparentbg').hide();
     $('.submenulink').remove();
     $('#submenu').fadeOut(400);
  });
  
  
  
  $('#menu > li').bind('mouseenter', function(){
      if($('ul', this).length > 0) {
        $('#transparentbg').show();
        $('> a', this).addClass('hovered');
        $('ul', $(this)).show();
      }
  }).bind('mouseleave', function(){
      if($('ul', this).length > 0) {
        if(!$('#transparentbg').hasClass('forceshow')) {
            $('#transparentbg').hide();
        }
        $('> a', this).removeClass('hovered');
        $('ul', $(this)).hide();
      }
  });
  
  $('#menu > li > ul > li').hover(function(){
    $(this).css('border', '1px solid #0054A6');
  }, function(){
    $(this).css('border', '1px solid #c8cacB');
  });
  
  $('.rightside, .news_main').jScrollPane();
  
  $('.reservation_menu li').hover(function(){
      $(this).css('background-image', 'url("./images/reservation_bg_hover.png")');
  }, function(){
      $(this).css('background-image', 'url("./images/reservation_bg.png")');
  });
  
  $('#campaigns a').click(function(){
    $('#campaigns a').removeClass('clicked');
    $('.campaign').hide();
    $(this).addClass('clicked');
    var tokens = $(this).attr('id').split('-');
    $('#campaigns-bg').show();
    $('#campaign-' + tokens[2]).fadeIn(500);
    $('#campaigns-holder').one('mouseleave', function(){
        timer2 = setTimeout("$('#campaign-"+ tokens[2] + "').fadeOut(500); $('#campaigns-bg').hide(); $('#campaigns a').removeClass('clicked');", 20000);
    });
  });

  $('#slideshow-img-1').load(function(){ 
	  $(this).show(); 
  });
  
  $('#loginForm').submit(function(){
	  $.ajax({
			type : 'post',
			url : '/api/login',
			data : $(this).serialize(),
			dataType : 'json',
			success : function(data)
			{
				if(data.success == 1)
					{
						window.location = '/reservation';
					}
				else 
					{
					alert('Wrong email address / password!');
					}
			}
		});
  });
  
  $('#reservation_experience').change(function(){
	  $.ajax({
		  type: 'post',
		  url : '/api/product',
		  data : {id:$(this).val()},
		  dataType : 'json',
		  success : function(data)
		  {
			 $('#starting_point').val(data.starting_point);
			 $('#starting_date').val(data.starting_date);
			 $('#finishing_point').val(data.finishing_point);
			 $('#finishing_date').val(data.finishing_date);
			 $('#duration').val(data.duration);
			 var htmlToInsert = '<option value = "">select to add</option>';
			
			 $('#addon').html(htmlToInsert);
			 
			 htmlToInsert = '';
			 
			 for(i in data.addons)
			 {
				 htmlToInsert += '<div><img src = "/files/' + data.addons[i]['img'] + '" /><h1>' + data.addons[i]['name'] + '</h1><p>' + data.addons[i]['summary'] +'</p><div class = "clearer"></div><p class = "right" style = "margin-top: -32px; margin-right: 120px;"><select id = "addon_' + data.addons[i]['id'] + '"><option value = "1">1 Ticket</option><option value = "2">2 Tickets</option><option value = "3">3 Tickets</option></select></p><div class = "clearer"></div><a class = "button" href = "javascript: void(0);" onclick = "addAddon(' + data.addons[i]['id'] + ')">Add Option</a><span class = "price">€ ' + data.addons[i]['price'] + ' / person</span><div class = "clearer"></div></div>';
			 }
			 
			 $('#clue_addons').html(htmlToInsert);
			 
			 if(data.rate_non == '' || data.rate_non == null || data.rate_non == 'null')
			 {
				 	nonCompetitorAvailable = false;
				 	$('#sharingWith option[value="non competitor"]').hide();
			 }
			 else
			 {
				 	nonCompetitorAvailable = true;
				 	$('#sharingWith option[value="non competitor"]').show();
			 }
			 
			 if(data.no_rental == 'yes')
			 {				 
				 	$('#rentalBike').attr('disabled', 'disabled'); 
			 }
			 else
			 {
				 	$('#rentalBike').attr('disabled', false); 
			 }
			 
			 if(data.no_equip == 'yes')
			 {
				 $('#room').val('single');
				 $('#room').parent().hide();
				 $('label[for=travellingPeople]').html('Number of tickets <sup>*</sup>');
				 $('label', $('#special').parent()).html('Special needs');
				 $('#sharingWith').parent().hide();
				 $('#nameOf2nd').parent().hide();
				 $('#nameOf3rd').parent().hide();
				 noEquipment = true;
			 }
			 else 
			 {
				 $('#room').parent().show();
				 $('label[for=travellingPeople]').html('Number of people travelling <sup>*</sup>');
				 $('label', $('#special').parent()).html('Special needs/Dietary requirements');
				 $('#sharingWith').parent().show();
				 $('#nameOf2nd').parent().show();
				 $('#nameOf3rd').parent().show();
				 noEquipment = false;
			 }
			 
			 if(data.rate_single == '' || data.rate_single == null || data.rate_single == 'null') $('#room option[value=single]').hide();			 
			 if(data.rate_double == '' || data.rate_double == null || data.rate_double == 'null') $('#room option[value=double]').hide();
			 if(data.rate_twin == '' || data.rate_twin == null || data.rate_twin == 'null') $('#room option[value=twin]').hide();
			 if(data.rate_tripple == '' || data.rate_tripple == null || data.rate_tripple == 'null') $('#room option[value=tripple]').hide();
			 
			 $('#room option').show();
			 
			 if(data.na_single == 'yes') $('#room option[value=single]').hide();
			 if(data.na_double == 'yes') $('#room option[value=double]').hide();
			 if(data.na_twin == 'yes') $('#room option[value=twin]').hide();
			 if(data.na_tripple == 'yes') $('#room option[value=tripple]').hide();
			 
			 $('#room').val($('#room option:visible').val());
			 $('#room').trigger('change');
			 
		  }		  
	  });
  });
  
  
  
  $('#rentalBike').click(function(){
	  if($('#rentalBike:checked').val() == 'yes') {
		  $('#rentABike').removeClass('invisible');
	  }
	  else {
		  $('#rentABike').addClass('invisible');
	  }
  });
  
  $('#rentalBike').change(function(){
	  if($('#rentalBike:checked').val() == 'yes') {
		  $('#rentABike').removeClass('invisible');
	  }
	  else {
		  $('#rentABike').addClass('invisible');
	  }
  });
  
  $('#travellingPeople').keyup(function(){
	  if(noEquipment)
	  {
		
		  if($(this).val() > 10){
			  $('#errors').html('<p>The maximum number of tickets per booking is 10!</p>');
			  errorMe();
			  $(this).val(10);
			  return true;
		  }
		  
	  }
	  else {
			  
		  if($(this).val() > 3 && $('#room').val() == 'tripple') {
			  $('#errors').html('<p>The maximum number of people travelling per this booking is 3!</p>');
			  errorMe();
			  $(this).val(3);
			  return true;
		  }
		  else {
			  if($(this).val() > 2 && ($('#room').val() == 'double' || $('#room').val() == 'twin')) {
				  $('#errors').html('<p>The maximum number of people travelling per this booking is 2!</p>');
				  errorMe();
				  $(this).val(2);
				  return true;
			  }
			  else {
				  if($(this).val() > 1 && $('#room').val() == 'single') {
					  $('#errors').html('<p>The maximum number of people travelling per this booking is 1!</p>');
					  errorMe();
					  $(this).val(1);
					  return true;
				  }
			  }
		  }
		  
	  }
  });
  
  $('#rentalBike').trigger('change');
  
  $('#reservation_experience').trigger('change');
  
  $('#room').change(function(){
	  if($(this).val() != 'single')
	  {
		  $('#sharing_with').removeClass('invisible');
		  $('#second_party').removeClass('invisible');
	  		if($('#room').val() == 'tripple') {
	  			$('#third_party').removeClass("invisible");
	  		}
	  		else {
	  		}
	  }
	  else 
	  {
		  $('#sharing_with').addClass('invisible');
		  $('#second_party, #third_party').addClass('invisible');
	  }
  });
  
  $('#room').trigger('change');
 
  
  $('#addon').click(function(){
	  helpMe('addon');
	  return false;
  });
  
  $('#contactForm').submit(function(){
	  if($('#privacy:checked').val() != 1) {
		  alert('You have not agreed with the privacy policy!');
	  }
	  else {
		  $.ajax({
			 type : 'post',
			 url: '/api/contact',
			 data : $(this).serialize(),
			 dataType : 'json',
			 success : function(data)
			 {
			  	  if(data.success == 1) {
			  		  alert('Thank you for your inquiry. We will get back to you soon!');
			  	  }
			  	  else {
			  		  alert('Please fill all the required fields!');
			  	  }
			 }
		  });
	  }
	  return false; 
  });
  
  $('#sharingWith').change(function(){
	  	if($(this).val() == 'competitor')
	  	{
	  		helpMe('noOfPeople');
	  	}
	  	else {
		  	if($(this).val() == 'non competitor')
		  	{
		  		if($(this).val() != 0) {
			  		$('#second_party').removeClass('invisible');
			  		if($('#room').val() == 'tripple') {
			  			$('#third_party').removeClass("invisible");
			  		}
			  		else {
			  		}
			  	}
			  	else {
			  		$('#second_party').addClass('invisible');
			  		if($('#room').val() == 'tripple') {
			  			$('#third_party').addClass("invisible");
			  		}
			  	}
		  	}
		  	else {	
		  		$('#travellingPeople').val(1);
		  	}
	  	}

  });
  
  var htmlToAppend = $('#clueContainer').html();
  $('#clueContainer').remove();
  $('#stage').append('<div id = "clueContainer">' + htmlToAppend  + '</div>');
  
  htmlToAppend = $('#errorContainer').html();
  $('#errorContainer').remove();
  $('#stage').append('<div id = "errorContainer">' + htmlToAppend  + '</div>');
  
  var realHeight;
  var realWidth;
  
	 if( typeof( window.innerWidth ) == 'number' ) {
	   realHeight = window.innerHeight;
	   realWidth = window.innerWidth;
	 }
	 else {
	   realHeight = document.documentElement.clientHeight;
	   realWidth = document.documentElement.clientWidth;
	 }
  
  $('#clueContainer').css({ position:'fixed', left: ((realWidth / 2) - 325) + 'px', top: ((realHeight / 2) - 200) + 'px' });
  $('#errorContainer').css({ position:'fixed', left: ((realWidth / 2) - 325) + 'px', top: ((realHeight / 2) - 200) + 'px' });
  
  
  
});

/* RESERVATION */

var Reservation = {
		
		lastStep : 0
		
};

function formStep1()
{
	
	var passed = true;
	
	$('#errors').html('');
	
	if($('#reservation_experience').val().length == 0)
	{
		passed = false;
		$('#errors').append('<p>Please select an experience or event!</p>');
	}
	
	if($('#travellingPeople').val().length == 0) {
		passed = false;
		$('#errors').append('<p>How many people will your booking compromise?</p>');
	}
	
	if(passed) {
		$.ajax({
			type : 'post',
			url : '/api/step1',
			data : $('#bookingForm1').serialize(),
			dataType : 'json',
			success : function(data)
			{
				if(data.success == 1)
					{
						if(noEquipment)
						{
							Reservation.lastStep = 2;
							showForm(3);	
						}
						else 
						{
							Reservation.lastStep = 1;
							showForm(2);
						}
					}
				else 
					{
					alert('Please, fill out all the marked fields and make sure your passwords are matching!');
					}
			}
		});
	}
	else {
		errorMe();
	}

}

function competitorStep1()
{
	Reservation.lastStep = 1;
	showForm(2);
}

function formStep2()
{
	
	$.ajax({
		type : 'post',
		url : '/api/step2',
		data : $('#bookingForm2').serialize(),
		dataType : 'json',
		success : function(data)
		{
			if(data.success == 1)
				{
					Reservation.lastStep = 2;
					showForm(3);
				}
			else 
				{
					alert('Please, fill out all the marked fields!');
				}
		}
	});

}

function competitorStep2()
{
	Reservation.lastStep = 2;
	showForm(3);
}

function formStep3()
{
	
	var passed = true;
	
	$('#errors').html('');
	
	if($('#salutation').val().length == 0)
	{
		passed = false;
		$('#errors').append('<p>Please tell us how to address you!</p>');
	}
	
	if($('#firstName').val().length == 0)
	{
		passed = false;
		$('#errors').append('<p>Please tell us your first name!</p>');
	}
	
	if($('#lastName').val().length == 0)
	{
		passed = false;
		$('#errors').append('<p>Please tell us your last name!</p>');
	}
	
	if($('#email').val().length == 0)
	{
		passed = false;
		$('#errors').append('<p>Please provide a valid email address!</p>');
	}
	
	if($('#confirm_email').val() != $('#email').val())
	{
		passed = false;
		$('#errors').append('<p>The email addresses you entered don’t match!</p>');
	}
	
	if($('#homePhone').val().length == 0)
	{
		passed = false;
		$('#errors').append('<p>Please provide your home phone number!</p>');
	}
	
	if($('#workPhone').val().length == 0)
	{
		passed = false;
		$('#errors').append('<p>Please provide your work or cell phone number!</p>');
	}
	
	if($('#address').val().length == 0)
	{
		passed = false;
		$('#errors').append('<p>Please provide your street address!</p>');
	}
	
	if($('#city').val().length == 0)
	{
		passed = false;
		$('#errors').append('<p>Please tell us what city you live in!</p>');
	}
	
	if($('#state').val().length == 0)
	{
		passed = false;
		$('#errors').append('<p>What state (U.S.) or province you live in!</p>');
	}
	
	if($('#zip').val().length == 0)
	{
		passed = false;
		$('#errors').append('<p>Please tell us your ZIP or postal code!</p>');
	}
	
	if($('#country').val().length == 0)
	{
		passed = false;
		$('#errors').append('<p>Please us the country you live in!</p>');
	}
	
	if($('#mf').val().length == 0)
	{
		passed = false;
		$('#errors').append('<p>Please tell us your gender!</p>');
	}
	
	if($('#age').val().length == 0)
	{
		passed = false;
		$('#errors').append('<p>Please tell us your age!</p>');
	}
	
	if($('input[name="reservation[ridingExperience]"]:checked').length == 0)
	{
		passed = false;
		$('#errors').append('<p>Please tell us about your experience as a rider!</p>');
	}
	
	if($('#password').val().length == 0)
	{
		passed = false;
		$('#errors').append('<p>Please select a password for your account!</p>');
	}
	
	if($('#password').val() != $('#confirm_password').val())
	{
		passed = false;
		$('#errors').append('<p>The passwords you entered don’t match!</p>');
	}
	
	if($('#hear').val().length == 0)
	{
		passed = false;
		$('#errors').append('<p>Please tell us how you found us!</p>');
	}
	
	if(passed)
	{
		$.ajax({
			type : 'post',
			url : '/api/step3',
			data : $('#bookingForm3').serialize(),
			dataType : 'json',
			success : function(data)
			{
				if(data.success == 1)
					{
						Reservation.lastStep = 3;
						showForm(4);
					}
				else 
					{
					  $('#errors').html('<p>Your email address is already registered with our system. Please sign in to your existing account to book your experience online.</p>');
					  errorMe();
					  $(this).val(10);
					  return true;
					}
			}
		});
	}
	else
	{
		errorMe();
	}

}

function competitorStep3()
{
	
	$.ajax({
		type : 'post',
		url : '/api/competitor',
		data : $('#bookingForm1').serialize(),
		dataType : 'json',
		success : function(data)
		{
			if(data.success == 1)
				{
					window.location = '/reservation'
				}
			else 
				{
					alert('Please, fill out all the marked fields!');
				}
		}
	});
	
}

function newStep3(config)
{
	$.ajax({
		type : 'post',
		url : '/api/new?config=' + config,
		data : $('#bookingForm1').serialize(),
		dataType : 'json',
		success : function(data)
		{
			if(data.success == 1)
				{
					window.location = '/reservation'
				}
			else 
				{
					alert('Please, fill out all the marked fields!');
				}
		}
	});
}

function addNonCompetitor()
{	
	if(parseInt($('#travellingPeople').val()) == 1) {
		$('#travellingPeople').val( parseInt($('#travellingPeople').val()) + 1 );
		$('#second_party').removeClass('invisible');
		showForm(1);
	}
	else {
	if(parseInt($('#travellingPeople').val()) == 2) {
		$('#travellingPeople').val( parseInt($('#travellingPeople').val()) + 1 );
		$('#third_party').removeClass('invisible');
		showForm(1);
	}
	else {
	if(parseInt($('#travellingPeople').val()) > 2) {
		alert('You cannot add more than 2 non-competitors!');
	}
	}
	}
}

function helpMe(section)
{
	$('#transparentbg').show();
	$('#clueContainer').show();
	$('#clue_' + section).show();
	$('#clue_' + section + ' div').show();
}

function errorMe()
{
	$('#transparentbg').show();
	$('#errorContainer').show();
}

function closeHelp()
{
	$('#transparentbg').hide();
	$('#clueContainer').hide();
	$('#clueContainer div').hide();
}

function closeError()
{
	$('#transparentbg').hide();
	$('#errorContainer').hide();
}

function addAddon(addon_id)
{
	  $.ajax({
		  type: 'post',
		  url : '/api/addAddon',
		  data : {id:addon_id, number:$('#addon_' + addon_id).val()},
		  dataType : 'json',
		  success : function(data)
		  {
			  if(data.success == 1) {
				  alert('Additional option was successfully added to your itinerary!')
				  closeHelp();
			  }
		  }
	  });
}





