var delay_search = 1000;
var timeout=null;
var validated=false;
var completed=false;
function complete(){
	var type;
	$('.img_info').each(function(){
		file = $(this).attr('src');
		type =  file.substring(file.lastIndexOf('/')+1,file.lastIndexOf('.'));
		if(type=='exclamation') return false;
	});
	//alert(type);
	if(type == 'exclamation')return false; else return true;
}
$(document).ready(function(){
	if($('#mail_inscription').val()!="") {
			$('#mail_inscription').removeClass('oblig');
	}else{$('#mail_inscription').addClass('oblig');}
		
	$('#mail_inscription').change(function(event){
		if($('#mail_inscription').val()!="" ){
			$('#mail_inscription').removeClass('oblig');
		}else{$('#mail_inscription').addClass('oblig');}
	});
	
	$('#inscription_info').submit(function(){
       		if(checkError()==false) return false;
       });
       
       $('#cgv').click(function(){ 
		if(cbcgv()==true){
			 $('#cgv_img').html('');
			 $('#cgv_msg').html('');		 
		 }
		else{
			$('#cgv_img').html("<img class='img_info' border='0' style='float: left;' src='/_design/exclamation.gif'/>");
			$('#cgv_msg').html('Veuillez cocher la case SVP !');
		}	
	});
	function cbcgv(){
		var cgv = document.inscription_info.cgv;
		if(cgv.checked ) return true;
		else return false;
	}
	function checkError(){
		completed = complete();
		if(completed==true){
			if(cbcgv()==true){
				//$('#your_email_msg a').css({color:"#009a4e"});
			return true;
			}
			else{ 
				$('#cgv_img').html("<img class='' border='0' style='float: left;' src='/_design/exclamation.gif'/>");
				$('#cgv_msg').html('Veuillez cocher la case SVP !');
				alert('Detected incorrect information');
				return false;
			}
		}else {
			if(cbcgv()==true){
				$('#cgv_img').html('');
				$('#cgv_msg').html('');
				alert('Detected incorrect information');
				return false;
			}
			else{
				$('#cgv_img').html("<img class='' border='0' style='float: left;' src='/_design/exclamation.gif'/>");
				$('#cgv_msg').html('Veuillez cocher la case SVP !');
				alert('Detected incorrect information');
				return false;
			}
		}
	};
	
		$('#pseudo').keyup(function(){
       		$.validate();
    	});
    	/*$('#pseudo').blur(function(){ 
      	 	$.validate();
    	});*/
    	jQuery.validate = function (option){
    	
	    	var pseudo = $('#pseudo').val();
	    	var length = pseudo.length;
	    	
	    	if(length==0){
	    			if (timeout) clearTimeout(timeout);
							timeout = setTimeout(function(){
	    						$('#message_pseudo').html('');
	    			 }, delay_search);			
	    		    	
	    	}else{
			        if (length < 4){	
			          	if (timeout) clearTimeout(timeout);
							timeout = setTimeout(function(){
			        			$('#message_pseudo').html('<img class="img_info" src="/_design/exclamation.gif" />Votre pseudo doit faire plus de 4 lettres');
			        		 }, delay_search);
			       	}
			         	        
			        else if (length >= 4){  			
						if (timeout) clearTimeout(timeout);
							timeout = setTimeout(function(){
								$('#message_pseudo').html('<img class="img_info" src="/_design/tabs/loading.gif" />');
			            		$('#message_pseudo').html($.ajax({
												            url: $('#rep_ajax').val(),
												            type: "POST",
												    		data: "pseudo="+pseudo, 
												    		//timeout: 28000,
															//error: function(d,ms){
																//$("#frm_reload").submit();
															//},						    	           		
												    		async: false,
												    		success: function(mgs){  		
												    			//$('#message_pseudo').html
												    		}
												        	}).responseText);
			            	}, delay_search);
		    		}
		    	}
    	}    	
});

 
