	$(document).ready(function()
	{
	
	 // Set the focus on the fist text box
	$("#client").focus(); 
     
	controlButton();
	$("#ClientFrom").keyup(function() {
	    controlButton();
	});
	
    
	
	function controlButton() {
		var textEntered1 = true;
	    $("#client").each(function() {
	        if (textEntered1 && $(this).val().length == 0) {
	            textEntered1 = false;
	        }
		});
		
	    var textEntered2 = true;
	    $("#email").each(function() {
	        if (textEntered2 && $(this).val().length == 0) {
	            textEntered2 = false;
				textEntered3 = true;
	        }
		});
		
		var textEntered3 = true;
	    $("#pssd").each(function() {
	        if (textEntered3 && $(this).val().length == 0) {
	            textEntered3 = false;
				textEntered2 = true;
	        }
		});
		
		if (textEntered1 && textEntered2 && textEntered3 == true) {
	        $("#OKSubmit").attr("disabled", "");
		}
		else if (textEntered1 && textEntered3 && textEntered2 == true) {
	        $("#OKSubmit").attr("disabled", "");
	    }
	 	else {
	        $("#OKSubmit").attr("disabled", "disabled");
	    }
	}
    
	    
	
	 $("#ClientFrom").submit(function()
	 {
	 	
	 	//remove all the class add the messagebox classes and start fading
	 	$("#msgbox").removeClass().addClass('messagebox').text('Vérification...').fadeIn(1000);
	 	if($('#pssd').val() != '' && $('#email').val() == null){
			//check the username exists or not from ajax
		 	$.post("tocontrol.php",{ user_client:$('#client').val(),user_pssd:$('#pssd').val(),rand:Math.random() } ,function(data)
		    {
					if(data=='yes') //if correct login detail
				  	{
				  		$("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
							{ 
						  	//add message and change the class of the box and start fading
						  	$(this).html('Validation...').addClass('messageboxok').fadeTo(900,1,
		      	        		function()
						  			{ 

										//redirect to secure page
							 			document.location.replace("tocontrol.php?go=true");
									});
	    				});
				  	}
					
					else if(data=='echoue')
					{
						$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
							{ 
						  	//add message and change the class of the box and start fading
						  	$(this).html('Votre compte VSN ou mot de passe est erroné, recommencez...').addClass('messageboxerror').fadeTo(900,1);
							});
					}
				  	else 
				  	{

						$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
							{ 
						  	//add message and change the class of the box and start fading
						  	$(this).html('Votre code client ou e-mail est erroné, recommencez...').addClass('messageboxerror').fadeTo(900,1);
							});		
		      		}

	    	});
			
		}
		
		else if($('#email').val() != '' && $('#pssd').val() == null){
			//check the username exists or not from ajax
		 	$.post("tocontrol.php",{ user_client:$('#client').val(),user_email:$('#email').val(),rand:Math.random() } ,function(data)
		    {
					if(data=='yes') //if correct login detail
				  	{
				  		$("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
							{ 
						  	//add message and change the class of the box and start fading
						  	$(this).html('Validation...').addClass('messageboxok').fadeTo(900,1,
		      	        		function()
						  			{ 

										//redirect to secure page
							 			document.location.replace("tocontrol.php?go=true");
									});
	    				});
				  	}
					else if(data=='agences') //if correct login detail
				  	{
				  		$("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
							{ 
						  	//add message and change the class of the box and start fading
						  	$(this).html('Autorisation...').addClass('messageboxok').fadeTo(900,1,
		      	        		function()
						  			{ 

										//redirect to secure page
							 			document.location.replace("tocontrol.php?go=distributeur");
									});
	    				});
				  	}
					else if(data=='echoue')
					{
						$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
							{ 
						  	//add message and change the class of the box and start fading
						  	$(this).html('Votre compte VSN ou mot de passe est erroné, recommencez...').addClass('messageboxerror').fadeTo(900,1);
							});
					}
				  	else 
				  	{

						$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
							{ 
						  	//add message and change the class of the box and start fading
						  	$(this).html('Votre code client ou e-mail est erroné, recommencez...').addClass('messageboxerror').fadeTo(900,1);
							});		
		      		}

	    	});
			
		}
		else{
			$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
				{ 
			  	//add message and change the class of the box and start fading
			  	$(this).html('Veuillez remplir !').addClass('messageboxerror').fadeTo(900,1);
				});
		}
		return false; //not to post the  form physically
	});
	//now call the ajax also focus move from 
	//$("#email").show(blur(function()
	//{
	//	$("#ClientFrom").trigger('submit');
	//}));
});

