
var vacancies = 0;

$(function() {
	changePlace();
    addFieldset();
    goNext();
	fancyBox();
})

function fancyBox(){
	$("a.fancybox").fancybox({
		'zoomSpeedIn':		300, 
		'zoomSpeedOut':	300, 
		'overlayShow':		false
	});
}

function goNext() {
    $('#gonext').attr('disabled', 'disabled');
    $('#f_terms input[type=checkbox]').attr('checked', false).click(function() {
        if ($('input[name="terms"]').attr('checked') && $('input[name="my_data"]').attr('checked')) {
            $(this).parents('form').find('#gonext').removeAttr('disabled');
        } else {
            $(this).parents('form').find('#gonext').attr('disabled', 'disabled');
        }
    });
}

function changePlace() { 
	var select = {
		place : '',
		date  : ''
	}
	$("select#place").change(function() {
		select.place = $('select#place option:selected').text(); 
		$.ajax({
			type: "GET",
			url:  "trip_php/date.php",
			data: "id="+$(this).val(),
			success: function(options){ 
				$('select#date').html(options);
				$('select#date:first').attr('selected', 'selected');
				$('select#date').change(function() { 
					$.ajax({
						type: "GET",
						url:  "trip_php/vacancies.php",
						data: "id="+$(this).val(),
						success: function(data) {
							var str = data.split(',');
							// get vacancies
							vacancies = str[0];
							$('#f_sum span').html(str[0]);
							// get price for the trip
							var price = str[1];
							// fill select options with number of valancies
							var opt = '<option value="0"></option>';
							for (var i = 1; i <= str[0]; i++) {
								opt += '<option value="'+i+'">'+i+'</option>';
							}
							$('#f_count select').html(opt);
							// calculate total price
							$('#f_count select').change(function() {
								var sum   = 0;	
								var total = 0;
								$('#f_count select').each(function() {
									if ($(this).attr('id') != 'room_count') {
										sum += parseInt($(this).val());
									}
								});
								// show total price if user chose less or equal numbers then "vacancy"
								if (sum <= vacancies) {
									total = sum * price; 
									$('#f_price span').html(total.toFixed(2) + ' zł ');
								}
							});
						}
					});
					select.date = $('select#date option:selected');
					if (select.date.val() != 0) {
						$('#f_sum h3').html(select.place + ' ' + select.date.text());
					}
				});
			}
		});
	});
}

function addFieldset() { 
	var my = {
		fieldset : '', // contain string of generated fieldset
		substr   : 0,  // how much fieldsets we must add or remove depends on selected options
		temp 	 : 0,  // total sum of all select options values
		number 	 : 0   // number of existing fieldsets 
	}
		
	$('#f_count select').change(function() {
		my.temp 	= 0;
		my.substr	= 0;
		my.number 	= 0;
		my.fieldset = '';
		
		$('#f_count select').each(function() {
			if ($(this).attr('id') != 'room_count') {
				my.temp += parseInt($(this).val());
			} 
		}); 
		// show additional fieldset if user chose less or equal then "vacancy"
		if (my.temp > vacancies) {
			alert('Dostepnych jest tylko '+vacancies+' miejsc');
		} else {
			my.temp = my.temp - 1;
			my.number = $('#u-data fieldset').length;  
			my.substr = my.temp - my.number;
			if (my.temp > my.number) {
				for (var i = 1; i <= my.substr; i++) {
					my.fieldset += '<fieldset class="f_data">';
					my.fieldset += '<legend>Dane osobowe</legend>';
					my.fieldset += '<h3>Dane uczestnika</h3>';
					my.fieldset += '<p><label>Imię:</label><input type="text" class="input" name="firstname[]" /></p>';
					my.fieldset += '<p><label>Nazwisko:</label><input type="text" class="input" name="lastname[]" /></p>';
					my.fieldset += '<p><label>Adres:</label><input type="text" class="input" name="address[]" /></p>';
					my.fieldset += '<p><label>Miejscowość:</label><input type="text" class="input" name="city[]" /></p>';
					my.fieldset += '<p><label>Kod:</label><input type="text" class="input" name="postal_code[]" /></p>';
					my.fieldset += '<p><label>PESEL:</label><input type="text" class="input" name="pesel[]" /></p>';
					my.fieldset += '<p><label>Email:</label><input type="text" class="input" name="email[]" /></p>';
					my.fieldset += '<p><label>Telefon:</label><input type="text" class="input" name="phone[]" /></p>';
					my.fieldset += '</fieldset>';
				}
				$('#u-data').append(my.fieldset);
			} else {
				my.j = 0;
				$('#u-data fieldset').each(function() {
					my.j += 1;
					if (my.j > my.temp) {
						$(this).remove();
					}
				});
			}
		}	
	});	
}
