/** JS FILE FOR USER CREATION PROCESS **/
/** USERNAME **/
$('#user_username').blur(function () {
	if (($(this).val().length < 6) || ($(this).val().length > 25)) {
		$('#user_username_error').html("Tu Usuario debe tener entre 6 y 25 caracteres.");
		$('#user_username_error').show();
		$('#td_username').addClass('td_bckgrd_error');		
	} else {		
		if ($(this).val().match(/^[a-zA-Z0-9]+$/i) == null) {
			$('#user_username_error').html("Tu usuario puede contener solo números y letras.");
			$('#user_username_error').show();
			$('#td_username').addClass('td_bckgrd_error');			
		} else {
			$('#user_username_error').html("<span class=\"loading_text\">Verificando si tu usuario ya existe...</a>");
			$('#user_username_error').show();
			$('#td_username').removeClass('td_bckgrd_error');
			$.ajax({ url: "/signup/check_username_availability", cache: false, data: "username=" + $(this).val(),
				success: function(html){
					if (html == "false") {
						$('#user_username_error').html("El Usuario que ingresaste no esta disponible, intentá con otro.");
						$('#user_username_error').show();
						$('#td_username').addClass('td_bckgrd_error');						
					} else {
						$('#user_username_error').html("");
						$('#td_username').removeClass('td_bckgrd_error');
					}
				}
			});
		}
	}
});

/** EMAIL **/
$('#user_email').blur(function () {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	if (!pattern.test($(this).val())) {
		$('#user_email_error').html("El E-mail ingresado no es válido.");
		$('#user_email_error').show();
		$('#td_email').addClass('td_bckgrd_error');		
	} else {
		$('#user_email_error').html("<span class=\"loading_text\">Verificando si tu dirección de e-mail ya existe...</a>");
		$('#user_email_error').show();
		$('#td_email').removeClass('td_bckgrd_error');		
		$.ajax({ url: "/signup/check_email_availability", cache: false, data: "email=" + $(this).val(),
			success: function(html){
				if (html == "false") {
					$('#user_email_error').html("El E-mail que ingresaste ya existe, intentá con otro, o recupera tu contraseña <a href=\"/recuperar_contrasena\">acá</a>.");
					$('#user_email_error').show();
					$('#td_email').addClass('td_bckgrd_error');					
				} else {
					$('#user_email_error').html("");
					$('#td_email').removeClass('td_bckgrd_error');
				}
			}
		});
	}
});

/** PASSWORD **/
$('#user_password').blur(function () {
	if ($(this).val().length < 6) {
		$('#user_password_error').html("Tu Contraseña debe tener como mínimo 6 caracteres.");
		$('#user_password_error').show();
		$('#td_password').addClass('td_bckgrd_error');		
	} else {
		$('#user_password_error').html("");
		$('#td_password').removeClass('td_bckgrd_error');		
	}
});

/** FULL NAME **/
$('#user_full_name').blur(function () {
	if ($(this).val().length < 6) {
		$('#user_full_name_error').html("Tenés que ingresar tu Nombre y Apellido.");
		$('#user_full_name_error').show();
		$('#td_full_name').addClass('td_bckgrd_error');		
	} else {
		$('#user_full_name_error').html("");
		$('#td_full_name').removeClass('td_bckgrd_error');		
	}
});

/** FULL NAME **/
$('#user_full_name').blur(function () {
	if ($(this).val().length < 6) {
		$('#user_full_name_error').html("Tenés que ingresar tu Nombre y Apellido.");
		$('#user_full_name_error').show();
		$('#td_full_name').addClass('td_bckgrd_error');		
	} else {
		$('#user_full_name_error').html("");
		$('#td_full_name').removeClass('td_bckgrd_error');		
	}
});

/** BIRTHDAY **/
function calculate_age() {
	var birthday = new Date($('#user_birthday_1i').val(), $('#user_birthday_2i').val() - 1, $('#user_birthday_3i').val());
	var today = new Date();
	var age =  Math.floor((( today - birthday ) / 86400000) / 365);
	return age;
}
$('#user_birthday_1i').change(function () {	
	if (calculate_age() < 18) {
		$('#user_birthday_error').html("Tenés que ser mayor de edad para registrarte al Club.com.py");
		$('#user_birthday_error').show();
		$('#td_birthday').addClass('td_bckgrd_error');		
	} else {
		$('#user_birthday_error').html("");
		$('#td_birthday').removeClass('td_bckgrd_error');
	}
});
$('#user_birthday_2i').change(function () {	
	if (calculate_age() < 18) {
		$('#user_birthday_error').html("Tenés que ser mayor de edad para registrarte al Club.com.py");
		$('#user_birthday_error').show();
		$('#td_birthday').addClass('td_bckgrd_error');		
	} else {
		$('#user_birthday_error').html("");
		$('#td_birthday').removeClass('td_bckgrd_error');
	}
});
$('#user_birthday_3i').change(function () {	
	if (calculate_age() < 18) {
		$('#user_birthday_error').html("Tenés que ser mayor de edad para registrarte al Club.com.py");
		$('#user_birthday_error').show();
		$('#user_birthday_error').show();
		$('#td_birthday').addClass('td_bckgrd_error');		
	} else {
		$('#user_birthday_error').html("");
		$('#td_birthday').removeClass('td_bckgrd_error');
	}
});

/** DNI **/
$('#user_dni').blur(function () {
	if (!$(this).val()) {
		$('#user_dni_error').html("Tenés que ingresar tu Número de Cédula.");
		$('#user_dni_error').show();
		$('#td_dni').addClass('td_bckgrd_error');		
	} else {
		$('#user_dni_error').html("");
		$('#td_dni').removeClass('td_bckgrd_error');		
	}
});

/** CAPTCHA **/
$('#reload_captcha').click(function() {
	document.images['captcha'].src='/captcha/generate?dummy=' + Math.floor(Math.random()*65535);
	return false;
});
