$(function() {
	
	var name = $("#name"),
		email = $("#email"),
		password = $("#password"),
		allFields = $([]).add(name).add(email).add(password),
		tips = $("#validateTips");

	function updateTips(t) {
		tips.text(t).effect("highlight",{},1500);
	}

	function verifiermail(mail) { 
		if(mail!=""){ 
			if ((mail.indexOf("@")>=0)&&(mail.lastIndexOf(".")>mail.indexOf("@"))) { 
				return true ;
			} else { 
				return false ;
			} 
		} else { 
			return false; 
		} 
	}

	function checkLength(o,n,min,max) {
		if ( o.val().length > max || o.val().length < min ) {
			o.addClass('ui-state-error');
			updateTips("Le champ " + n + " doit contenir au moins "+min+" caracteres.");
			return false;
		} else {
			return true;
		}
	}

	function checkRegexp(o,regexp,n) {
		if ( !( regexp.test( o.val() ) ) ) {
			o.addClass('ui-state-error');
			updateTips(n);
			return false;
		} else {
			return true;
		}
	}

        $("input[name='email']").keyup(function() {
           $(this).val($(this).val().toLowerCase());
        });
	
	$("#dialog").dialog({
		bgiframe: true,
		autoOpen: false,
		height: 280,
		width:400,
		modal: true,
		buttons: {
			"S'identifier": function() {
				var bValid = true;
				allFields.removeClass('ui-state-error');

				
				bValid = bValid && checkLength(email,"email",6,80);
				bValid = bValid && checkLength(password,"password",4,99);

				
				// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
				bValid = bValid && checkRegexp(email,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"Format de l'email non-valide");
				bValid = bValid && checkRegexp(password,/^([0-9a-zA-Z])+$/,"Caracteres autorises pour le mot de passe : a-z, A-Z, 0-9");
				
				if (bValid) {
					$("#Identification").submit();
					$(this).dialog('close');
				}
			},
			"Annuler": function() {
				$(this).dialog('close');
			}
		},
		close: function() {
			allFields.val('').removeClass('ui-state-error');
		}
	});
	
	$("#menu").corner("3px");
	$("th").corner("4px");
	$("td").corner("4px");
	$(".info").corner("4px");
	$(".info2").corner("4px");
	$("#contenu_site").corner("round 8px").parent().css('padding', '4px').corner("round 10px")
	
	$('#logIn').click(function() {
		$('#dialog').dialog('open');
	});
	$('#logIn2').click(function() {
		$('#dialog').dialog('open');
	});
	
	$('#newC').submit(function() {
		var email = $("[name='email']").attr('value');
		var mdp = $("[name='mdp']").attr('value');
		
		if ($("#cgu").is(':checked')) {
			
		} else {
			alert('Veuillez accepter les conditions generales d\'utilisation');
			return false;
		}
		if (!mdp || mdp.length < 4) {
			alert('Mot de passe trop court !');
			return false;
		}
		if (!verifiermail(email)) {
			//alert('Votre adresse mail est invalide');
			//return false;
		}
		if ($("#document_file").val() == "") {
			alert('Veuillez joindre votre cv pour completer votre dossier');
			return false;
		}
	});
	
	$('#newCD').submit(function() {
		if ($("#cgu").is(':checked')) {
			
		} else {
			alert('Veuillez accepter les conditions generales d\'utilisation');
			return false;
		}
	});

	$('#new_mdp_email').hide();
	
	$('#newmdp').click(function() {
		$('#new_mdp_email').toggle();
	});

	$('#sendmail').click(function() {
		$('#resmail').load('ajax/sendmail.php',{EMAIL:$('#valemail').val()});
	});

	$('#go_ann').click(function() {
		var noann = $('#no_ann').attr('value');

		if (noann > 0) {
			window.location.href = "http://www.mybeautifuljob.com/candidature/index.php?annonce=" + noann + "";
		} else {
			alert("Veuillez renseigner un numero d'annonce !");
		}						
	});

	$('#filtrage_region').change(function() {
		var noreg = $('#filtrage_region').attr('value');

		$('#filtre_region').attr('value',noreg);
		$("#filtre_reg").submit();
	});
});
