// JavaScript Document
$(document).ready(function(){

	if($('#signup').length)
	{
	
		$('#pays').change(function(){
			
			$('#day').removeAttr('disabled');
			$('#month').removeAttr('disabled');
			$('#year').removeAttr('disabled');	
		
		});
	
		$("#cat").change(function(){$('#search').removeAttr('disabled');});
		
		$("#search").change(function(){
			$('#min').removeAttr('disabled');
			$('#max').removeAttr('disabled');
		});
		
		$("#year").focus(function(){$('#ville').removeAttr('disabled');	});
		$("#max").focus(function(){$('#pays').removeAttr('disabled');	});
		$("#email").focus(function(){$('#pseudo').removeAttr('disabled');});
		$("#pseudo").focus(function(){$('#password').removeAttr('disabled');});
			
		$('#submit').click(function(){
			
			
			$.ajax({
				  url: "actions_public/take-signup.php",
				  global: false,
				  type: "POST",
				  cache: false,
				  data: ($('#signupForm').serialize()),
				  dataType: "json",
				  success: function(rep){
				
					$("#dialog-message" ).html(rep["msg"]);
					
					if(rep["status"] == "error")
					{											
						// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
						$( "#dialog" ).dialog( "destroy" );
					
						$( "#dialog-message" ).dialog({
							modal: true,
							title: "Une erreur est survenue !",
							buttons: {
								Ok: function() {								
									$( this ).dialog( "close" );
								}
							}
						});
					
					}
					else if (rep["status"] == "noerror")
					{
						//Sexy.info("Inscription r&eacute;ussie !<br>Vous allez recevoir un email de confirmation d'ici quelques minutes.");
						
						$.post("actions_public/take-login.php",{login: $("#pseudo").val(),pass:$("#password").val()});
						
						$( "#dialog-message" ).dialog({
							modal: true,
							title: "Inscription réussi !",
							buttons: {
								Ok: function() {
									document.location = "http://www.freemeet.net";
								}
							}
						});
						
						$("#signup input:enabled").val("");
					}
				   
				}});

			
		});
	
	}
	
	$("#cgu").click(function(){
		
		openDialog("cgu","Conditions générales d'utilisation",700,300);
	});
	
	
	//RETRIEVE PASSWORD FUNCTION
	
	$("#help").click(function(){
		
		var check = 0;
		
		$("#dialog-message" ).html("Entrez votre pseudo ou votre adresse email :<br><input type='text' id='infos'>");
		// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
		$( "#dialog" ).dialog( "destroy" );
	
		$( "#dialog-message" ).dialog({
			modal: true,
			width : 310,
			resizable : false,
			title : "Mot de passe perdu ?",
			buttons: {
				
				Envoyer: function() {
					
				if(check != 1)	
				{  
				  $.ajax({
				  url: "ajax/password.php",
				  global: false,
				  type: "POST",
				  cache: false,
				  data: ({search : $("#infos").val()}),
				  dataType: "json",
				  success: function(reply){
					  
					  if(reply["status"] == "ok")
					  {	
						check = 1;
						$(".ui-button-text").html("Fermer")
						
					  }
					  
					  $("#dialog-message" ).html(reply["message"]);
				  
				  }});
				}
				
				else
				{
					$( this ).dialog( "close" );
				}
			
			}
			
			}});
		
	});

});

function suggest(inputString,country){
		if(inputString.length == 0) {
			$('#suggestions').fadeOut();
		} else {
		$('#ville').addClass('load');
			$.get("ajax/signup.php", {search: ""+inputString+"", action: "villes", country: country}, function(data){
				if(data.length >0) {
					$('#suggestions').fadeIn();
					$('#suggestionsList').html(data);
					$('#ville').removeClass('load');
				}
			});
		}
	}

function fill(thisValue,city) {
	$('#ville').val(thisValue);
	$('#villeid').val(city);
	$('#email').removeAttr('disabled');
	$('#city').val(thisValue);
	$('#cityID').val(city);
	setTimeout("$('#suggestions').fadeOut();", 600);
}

