$(document).ready(function()
{
	// Redirect membership sign-up depending on type
	$('#applyformembership').submit(function() {
		var link = $('#membershiptype').val();
		var form_url = $(this).attr("action");     
		$(this).attr("action", form_url+link+'/');
	});
	
	// Default country selections to Ireland
	$('select#country').each(function()
	{
		$(this).val('431');
	});	
	$('select#business_country').each(function()
	{
		$(this).val('431');
	});
		
	// Country drop downs have any previous value put into the 'title' attribute.	
	$('select[title]').each(function()
	{
		if ($(this).attr('title') != '')
			$(this).val($(this).attr('title'));
	});
	
	// On profile forms, hide none relevant sections.
	var isCorporate = $('form.profile input#corporate').val() == '1';
	var isSupplier = $('form.profile input#supplier').val() == '1';
	var isConsultant = $('form.profile input#consultant').val() == '1';
	
	if (!isConsultant)
	{
		$('form.profile tr.consultant').hide();
	}
	
	if (!isSupplier)
	{
		$('form.profile tr.supplier').hide();
	}
	
	// FAQ Form
	var FAQ_LABEL_NAME =  'your name';
	var FAQ_LABEL_EMAIL =  'email address';
	var FAQ_LABEL_TITLE =  'sum it up with a short title';
	var FAQ_LABEL_MESSAGE = 'your message';
	
	// Initialise labels
	var faq_name = ($('form#faqform input#name').val());
	var faq_email = ($('form#faqform input#email').val());
	var faq_title =  ($('form#faqform input#title').val());
	var faq_message = ($('form#faqform textarea#message').html());

	if (faq_name == '')
	 $('form#faqform input#name').val(FAQ_LABEL_NAME);
	 
	if (faq_email == '')
	 $('form#faqform input#email').val(FAQ_LABEL_EMAIL);
	 
	if (faq_title == '')
	 $('form#faqform input#title').val(FAQ_LABEL_TITLE);
	 
	if (faq_message == '')
	 $('form#faqform textarea#message').html(FAQ_LABEL_MESSAGE);
	
	$('form#faqform input#name').focus(function() {
	  if ($(this).val() == FAQ_LABEL_NAME)
		$(this).val('');
	});
	
	$('form#faqform input#name').blur(function() {
	  if ($(this).val() == '')
		$(this).val(FAQ_LABEL_NAME );
	});
	
	$('form#faqform input#email').focus(function() {
	  if ($(this).val() == FAQ_LABEL_EMAIL)
		$(this).val('');
	});
	
	$('form#faqform input#email').blur(function() {
	  if ($(this).val() == '')
		$(this).val(FAQ_LABEL_EMAIL );
	});
	
	$('form#faqform input#title').focus(function() {
	  if ($(this).val() == FAQ_LABEL_TITLE)
		$(this).val('');
	});
	
	$('form#faqform input#title').blur(function() {
	  if ($(this).val() == '')
		$(this).val(FAQ_LABEL_TITLE);
	});
	
	$('form#faqform textarea#message').focus(function() {
	  if ($(this).html() == FAQ_LABEL_MESSAGE)
		$(this).html('');
	});
	
	$('form#faqform textarea#message').blur(function() {
	  if ($(this).html() == '')
		$(this).html(FAQ_LABEL_MESSAGE);
	});
	
	$('form#faqform input#submit').click(function() {
	
	  var name = ($('form#faqform input#name').val());
	  var email = ($('form#faqform input#email').val());
	  var title =  ($('form#faqform input#title').val());
	  var message = ($('form#faqform textarea#message').html());
	  
	  if (name == FAQ_LABEL_NAME)
		 $('form#faqform input#name').val('');	 
		 
	  if (email == FAQ_LABEL_EMAIL)
		 $('form#faqform input#email').val('');
		 
	  if (title == FAQ_LABEL_TITLE)
		 $('form#faqform input#title').val('');
		 
	  if (message == FAQ_LABEL_MESSAGE)
		 $('form#faqform textarea#message').html('');
	});
	
	// default faq form question type
	$('form#faqform li#question input').trigger('click');
	
	/* Debug Form
	$('form#faqform ').submit(function() {
		 
			var data = $(this).serialize();
			alert(data);
			
			return false;
	}); */
	
	/*--------------------------------------------------------------------------------------------------------------*/
	/*-------------- Events reg form -------------------------------------------------------------------------------*/
	

	$("input[name=attending]").change(function() {
		checkField();
	});

	
	function checkField() {
		var n = $("input[name=attending]:checked").length;
		var v = $("input[name=attending]:checked").val();		
		var message1 = (v == 1) ? "Number of additional attendees<span>*</span>" : "Number of attendees I'm booking for <span>*</span>";		
		var message2 = (v == 1) ? 'Your contact and billing info' : 'Billing and booking contact info only';		
		if (n) { 
			$("tr#hiddenData").show();
			$("th.mainContact").html(message2);		
			$("th.nop").html(message1);	
			$('span#note').show();	
		}
		if (v == 0) {
			$("#numberOfPeople option[value='0']").remove();
			$('span#note').hide();
		} else {
			if ( ! $("#numberOfPeople option[value='0']").length) {
				$("#numberOfPeople").prepend('<option value="0">Only Me</option>');
				$("#numberOfPeople option[value='0']").attr("selected","selected");
				$('span#note').show();
			}
		}
    }
	checkField();
	
	
	$("select[name=numberOfPeople]").change(function() {
		var v = $(this).val();		
		if (v == 0) {		
			alert('The step 2 will be skipped');
			
		}
		return false;
	});
	
	
	$("#reg_event_step1_button").click(function() {
	

		
		$.cookie('test_cookie', 'cookie_value', { path: '/' });
		if (!($.cookie('test_cookie') == 'cookie_value')) {
			alert("You need to enable cookies for this site to use the booking form!");
			return false;
		}



		
		
		
	
		var trigger;
		var n = $("input[name=attending]:checked").length;
		if (!n) {
			alert('Please select if you are attending this event yourself or just booking for others?');
			return false;
		}

		$('form#RegForm').map(function(){ 
			array = $.makeArray(this.elements);
			trigger = 0;
			for (var i = 0;i<array.length;i++) {
				
				if ( (array[i].name != 'discount_code') && (array[i].name != 'hi_robot') && (array[i].name != 'addressline2')) {
					//console.log(array[i].name +' = '+ array[i].value);
					if ((array[i]).value == '') {
						trigger += 1;
					} 
				}
			}
		});
		if (trigger == 0) {
			var nop = parseFloat($("select[name=numberOfPeople]").val());
			var a = parseFloat($("input[name=attending]:checked").val());		
			var count = nop + a;	
			if (count == 0) {
				alert('There must be at least one attendend!');
				return false;
			}
			
			if (confirm('Number of attendees: '+count+ ' \nNOTE: This step cannot be undone!')) {
				return true;
			} else {
				return false;
			}			
		} else {
			return true;	
		}
	

	});
	
	$('#reg_event_step3_button').click(function() {
		var v = $("select[name=gotoid]").val();
		if (v == '') {
			alert('Please choose payment option!');
			return false;
		}
	});
	
	
});
