// Javascript code for the children details popup
//

$(document).ready(function() {

	$("#children_dialog").dialog({
		bgiframe: true,
		autoOpen: false,
		modal: true,
		width: 428,
		draggable: false,
		resizable: false,
		buttons: {
			'OK': function() {
				var valid = true;
				
				var ages = new Array();
				var sexs = new Array();
				$("#children_dialog table tr:gt(0)").each(function(i, domEle) {
					
					var ageTxt = $(domEle).find('input[type="text"]');	
					var txt = $(ageTxt).val();
					
					if(txt.search(/^\d\d?$/) == -1) {
						valid = false;
						$(ageTxt).addClass('error');
					}
					else {
						ages.push(txt);
					}
					
					if(valid) {
						var gender = 'F';
						$(this).children('input[type="radio"]').each(function (index, domEle) {
							gender = domEle.checked == true ? domEle.value : gender2;
						});
						sexs.push(gender);
					}
				});
				
				if(valid) {
					$("#age_children_hidden").val(ages.join(","));
					$("#gender_children_hidden").val(sexs.join(","));
					$(this).dialog('close');
				}
				
			// End of OK button
			},
			'Annuler': function() {
				$('#nb_children_select').val(0);
				$(this).dialog('close');
			}
		},
		close: function() {
			$('#blocmilieu').removeClass('hidden');
		}
	});


	// Open Children dialog
	$('#nb_children_select').change(function() {
		if($(this).val() > 0) {
			$('#blocmilieu').addClass("hidden");
			
			var nbChildren = $(this).val();
			
			var header='<table id="children_table"><tr><th>Enfants</th><th>Sexe</th><th>Age</th></tr>';
			$('#children_dialog form').html(header);
			
			for(i=1; i<=nbChildren; i++) {
				
				var oddRow = "";
				if(i%2 == 1) {
					oddRow = ' class="odd"';
				}
			
				var row = 	'<tr' + oddRow + '><td>' + i + '</td>' +
							'<td><input value="M" name="gender_child' + i + '" type="radio"/>un garçon<br/>' +
							'<input value="F" name="gender_child' + i + '" type="radio" checked="checked" />une fille</td>' + 
							'<td><input type="text" name="age_child' + i + '"/> ans</td></tr>';
				
				$('#children_dialog table').append(row);
			}
			$('#children_dialog form').append('</table>');
			
			$('#children_dialog').dialog('open');
		}
		return false;
	});

});
