/**
 * @mc émafbroadcast
 * 2008 05 25
 * GRANGENEUVE, LE MAGAsIN
 * 
 * fichier js contenant les opérations ajax et jquery pour la 
 * gestion du formulaire
 */
$(document).ready(function() {
	$('#loginregister').blur(function(){
		var valuE=$('#loginregister').val();
		$.post('ajax/loginInfo.php', {
							val: valuE
						}, // envoi en post les valeurs
 							function(c){ // update div
								$('#info2').html(c);// innerhtml
								//$('#warning').html($('#warning').attr("alt"));
								
						});// function c et $.post
	});//end loginregister
	
	$('#pagePROFIL 	#passwordConfirm').blur(function(){
		if($('#password').val()!=($('#passwordConfirm').val())){
			
			$('#info1').html($('#info1').html()+'<br /><span>Les mots de passe ne concordent pas !</span>'); // innerhtml
			
		}else{
			$('#info1').html('');
		}
	
		
	});
	
	var error = new Array();
	
	$('#register').submit(function(){
		$('.jGrowl-notification').hide();
		error = new Array();
		var errorTxt='';
			function checkField(field){
				var f=$(field);
				var l=$('label[for="'+f.attr('id')+'"]').html().substring(0,$('label[for="'+f.attr('id')+'"]').html().length-2);
				
				if(f.val()==''){
					error.push(l);
					$(field+','+'label[for="'+f.attr('id')+'"]').addClass('error');
				}else{
					$(field+','+'label[for="'+f.attr('id')+'"]').removeClass('error');
				}
				if(l.toLowerCase()=='email' ){
					if (!(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(f.val()))) {
						error.push(l + ' non valide');
					}
				}
			
			}
			
		
			checkField('#loginregister');
			checkField('#name');
			checkField('#firstName');
			checkField('#phone');
			checkField('#email');
			checkField('#npa');
			checkField('#city');
		
			$.each(error,function(index,txt){
					errorTxt += '<li>'+txt+'</li>';
			})
			if(errorTxt!=''){
				errorTxt='Veuillez contrôler le(s) champ(s) suivant(s):<ul>'+errorTxt+'</ul>';
				$.jGrowl(errorTxt,{
					sticky: true
				});
				return false;
			};
			if($('#warning').attr("alt")=='false'){
								
				return false;
			};	
			
	});
	
	$('#pagePROFIL #register').submit(function(){
		if($('#password').val()!=($('#passwordConfirm').val())){
			
			$('#info1').html('Les mots de passe ne concordent pas !'); // innerhtml
			return false;
			
		}else{
			if (error.length == 0) {
				$('#shaPwd').val($.sha1($('#passwordConfirm').val()));
				$('#password,#passwordConfirm').val('');
			}else{
				return false;
			}
		}
	});
	
	$('#pageModPROFIL #register').submit(function(){
		if ($('#password').val() != '') {
			if ($('#newPassword').val() == $('#passwordConfirm').val() && $('#newPassword').val()!='') {
				if (error.length == 0) {
					$('#shaPwd').val($.sha1($('#password').val()));
					$('#shaPwdNew').val($.sha1($('#newPassword').val()));
					$('#password,#passwordConfirm,#newPassword').val('');
				}else{
					return false;
				}
			}
			else {
				$('#passwordConfirm,label[for="passwordConfirm"],#newPassword,label[for="newPassword"]').addClass("error");
				$('#info1').html('Les mots de passe ne concordent pas !'); // innerhtml
				return false;
			}
		}
	});
})
