// function to show many guests - triggered by dropdown menu (Total in Group)
function show_multi_ag() {
	the_form = document.getElementById('fm-form');
	group_total = the_form.Gtotal.value;
	number_of_additional = group_total - 1;
	// loop and hide all of the fieldsets first
	for (i=1; i<=20; i++) {
		document.getElementById('ag_'+i).style.display = "none";
		// do we need to clear the form inputs first
	}
	// loop and show necessary fieldsets
	for (i=1; i<=number_of_additional; i++) {
		document.getElementById('ag_'+i).style.display = "block";
	}
	// loop and hide all of the "remove this person" links
	for (i=1; i<=20; i++) {
		document.getElementById('ag_remove_'+i).style.visibility = "hidden";
		// do we need to clear the form inputs first
	}
	// show just the last "remove this person" link
	// also the "Terms and Conditions" paragraph, if there are additional guests
	if (number_of_additional >=1) {
		document.getElementById('ag_remove_'+number_of_additional).style.visibility = "visible";
		document.getElementById('tandc').style.display = "block";
	}
	
	if (group_total == 20) {
		document.getElementById('add_additional').style.display = "none";
	} else {
		document.getElementById('add_additional').style.display = "block";
	}
}		

// function to add one additional guest - triggered by link
function show_single_ag() {
	the_form = document.getElementById('fm-form');
	group_total = the_form.Gtotal.value;
	number_of_additional = group_total - 1;
	// the one to add is 1 more than the current number_of_additional
	next_one_to_add = number_of_additional + 1;
	document.getElementById('ag_'+next_one_to_add).style.display = "block";
	// hide the previous "remove this person" link
	previous_ref = next_one_to_add -1;
	if (previous_ref > 0 ) {
		document.getElementById('ag_remove_'+previous_ref).style.visibility = "hidden";
	}
	// show just the current "remove this person" link
	document.getElementById('ag_remove_'+next_one_to_add).style.visibility = "visible";
	// Great. Now update the dropdown menu (Total in Group)
	the_form.Gtotal.selectedIndex = next_one_to_add;
	// And show the the Terms and Conditions paragraph
	document.getElementById('tandc').style.display = "block";
	
	if (group_total == 19) {
		document.getElementById('add_additional').style.display = "none";
	} else {
		document.getElementById('add_additional').style.display = "block";
	}
}

// function to remove one additional guest - triggered by link
function remove_person(ref) {
	the_form = document.getElementById('fm-form');
	// clear fields first
	clear_ag_fields(ref);
	// the hide the fieldset, and also update the dropdown menu (Total in Group)
	setTimeout(function () {
		hide_single_ag(ref);
		the_form.Gtotal.selectedIndex = (ref-1);
		document.getElementById('add_additional').style.display = "block";
		}, 300);
	// lastly, to hide the current "remove this person" link and show the previous one
	document.getElementById('ag_remove_'+ref).style.visibility = "hidden";
	previous_ref = ref -1;
	if (previous_ref > 0 ) {
		document.getElementById('ag_remove_'+previous_ref).style.visibility = "visible";
	}
	if (ref == 1) {
		// hide the Terms and Conditions paragraph
		document.getElementById('tandc').style.display = "none";
	}
	
}
function hide_single_ag(ref) {
	// hide the fieldset
	document.getElementById('ag_'+ref).style.display = "none";
}
function clear_ag_fields(ref) {
	document.getElementById('AG_fn'+ref).value = "";
	document.getElementById('AG_ln'+ref).value = "";
	document.getElementById('AG_age'+ref).value = "";
}


// ========================================================= //
// Function to check mandatory fields when form is submitted
// ========================================================= //

function verifyform() {
	var msg = "";
	var empty_fields = "";
	if (document.getElementById('Gfirst').value.length < 1) {
		empty_fields += "\n -       your first name";
		document.getElementById('Gfirst').className = 'warning';
	}
	
	if (document.getElementById('Glast').value.length < 1) {
		empty_fields += "\n -       your last name";
		document.getElementById('Glast').className = 'warning';
	}
	
	if (document.getElementById('Gaddr1').value.length < 1) {
		empty_fields += "\n -       your address";
		document.getElementById('Gaddr1').className = 'warning';
	}
	
	if (document.getElementById('Gaddr2').value.length < 1) {
		empty_fields += "\n -       your address (cont.)";
		document.getElementById('Gaddr2').className = 'warning';
	}
	
	if (document.getElementById('Gtown').value.length < 1) {
		empty_fields += "\n -       your town/city";
		document.getElementById('Gtown').className = 'warning';
	}
	
	if (document.getElementById('Gpost').value.length < 1) {
		empty_fields += "\n -       your postcode/zip";
		document.getElementById('Gpost').className = 'warning';
	}
	
	if (document.getElementById('GtelD').value.length < 1) {
		empty_fields += "\n -       your daytime phone number";
		document.getElementById('GtelD').className = 'warning';
	}
	
	if (document.getElementById('Gemail').value.length < 1) {
		empty_fields += "\n -       your email address";
		document.getElementById('Gemail').className = 'warning';
	}
	
	


	if (empty_fields.length > 1) {
		msg  = "=====   Ski Royal   ===== \n\n";
		msg += "In order for us to process your booking we need:  \n" + empty_fields + "\n";
		alert(msg);
		return false;
	} else {
		return true;
	}	
	
}


// Function to change warning class to nothing when user goes back to fill in missing fields
function backoffwarning(ref) {
	document.getElementById(ref).className = 'inp';
}