function doNothing() {}

$(function () {
	$('#dialog').dialog({
		modal:true,
		autoOpen:false,
		resizable:false,
		draggable:false,
		closeOnEscape: false,
		open: function(event, ui) { $(this).find(".ui-dialog-titlebar-close").hide(); }
	});
	$('#error_dialog').dialog({
		width:800,
		autoOpen:false,
		draggable:false,
	});
	
	$('a.link').each(function () {
		var $this = $(this);
		$this.attr('link', $this.attr('href'));
	}).attr('href', 'javascript:doNothing()');
	
	$('a.link').click(function (e) {
		goTo($(this).attr('link'), $(this).attr('link'));
	});
	
	
	$("#uploadify").uploadify({
		'uploader'       : 'uploadify/scripts/uploadify.swf',
		'script'         : 'uploadify/scripts/uploadify.php',
		'cancelImg'      : 'uploadify/cancel.png',
		'folder'         : 'uploads',
		'queueID'        : 'fileQueue',
		'auto'           : true,
		'multi'          : false,
		onComplete: function (e, q, file, response) {
			$('#uploadify_filename').html(file.name);
			$('#uploadify_cp').val(response);
		}
	});
})

function goTo(link, section) {
	$('.content').hide();
	$(link).show();
	$('#main_menu a').removeClass('over').filter('[link='+section+']').addClass('over');
}

function submitForm() {
	var $form = $('#form');
	var errors = false;
	var msg = '';
	
	// Validating fields
	var $fields = $form.find('.mandatory');
	$fields.each(function () {
		var $this = $(this);
		var $text = $this.parent().children('b');
		if ( $this.val().length > 0 ) {
			$text.css('color', '');
		} else {
			$text.css('color', 'red');
			errors = true;
			msg += '<p>Le champs «'+$text.html()+'"» est obligatoire.</p>';
		}
	});
	
	// Validating radios
	var radios = [];
	$form.find(':input[type=radio][value=1]').each(function () {
		radios.push($(this).attr('name'));
	});
	for ( var i = 0 ; i < radios.length ; i ++ ) {
		var $rad = $form.find(':input[type=radio][name='+radios[i]+']');
		if ( $rad.filter(':checked').length ) {
			$($rad[0]).parent().prev().children('b').css('color', '');
		} else {
			$($rad[0]).parent().prev().children('b').css('color', 'red');
			errors = true;
		}
	}
	
	if ( !errors ) {
		$('#dialog').dialog('open');
		$.ajax({
			url:'ajax.php',
			type:'post',
			data:$form.serialize(),
			success: function () {
				$('#dialog').dialog('close');
				goTo('#confirmation', '#inscription')
			}
		})
	} else {
		$('#error_dialog').html(msg).dialog('open');
	}
}
