var validusername = false;
var validemail = false;
var validpassword = false;
var validpassword2 = false;
var validcheckbox = false;
function validateUsername() {
	var x = document.getElementById("username").value
	if (x==null || x=="") {
		document.getElementById("username-alert").className = 'alert alert-error';
		document.getElementById("username-alert").innerHTML = '<i class="fa fa-times"></i> Aquest camp és obligatori';
		validusername = false;
	}
	else if(x.length<4) {
		document.getElementById("username-alert").className = 'alert alert-error';
		document.getElementById("username-alert").innerHTML = "<i class='fa fa-times'></i> El nom d'usuari és massa curt";
		validusername = false;
	}
	else if(x.length>20) {
		document.getElementById("username-alert").className = 'alert alert-error';
		document.getElementById("username-alert").innerHTML = "<i class='fa fa-times'></i> El nom d'usuari és massa llarg";
		validusername = false;
	}
	else if(/^[a-zA-Z0-9_]+$/.test(x) == false) {
		document.getElementById("username-alert").className = 'alert alert-error';
		document.getElementById("username-alert").innerHTML = "<i class='fa fa-times'></i> El nom d'usuari no compleix les condicions";
		validusername = false;
	}
	else {
		document.getElementById("username-alert").className = 'alert';
		document.getElementById("username-alert").innerHTML = '<i class="fa fa-spinner fa-spin"></i>';
		$.ajax({
			type: "POST",
			url: "/checkUsername/",
			data: {'username': $('#username').val()},
			dataType: "text",
			success: function(response) {
				var response = $.parseJSON(response);
				if (response.success){
					document.getElementById("username-alert").className = 'alert alert-valid';
					document.getElementById("username-alert").innerHTML = "<i class='fa fa-check'></i>";
				}
				else{
					document.getElementById("username-alert").className = 'alert alert-error';
					document.getElementById("username-alert").innerHTML = "<i class='fa fa-times'></i> Aquest usuari ja existeix";
				}
			}
		});
		document.getElementById("username-alert").className = 'alert alert-valid';
		document.getElementById("username-alert").innerHTML = '<i class="fa fa-check"></i>';
		validusername = true;
	}
}
function validateEmail() {
	var x = document.getElementById("email").value
	var atpos = x.indexOf("@");
	var dotpos = x.lastIndexOf(".");
	if (x==null || x=="") {
		document.getElementById("email-alert").className = 'alert alert-error';
		document.getElementById("email-alert").innerHTML = '<i class="fa fa-times"></i> Aquest camp és obligatori';
		validemail = false;
	}
	else if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) {
		document.getElementById("email-alert").className = 'alert alert-error';
		document.getElementById("email-alert").innerHTML = '<i class="fa fa-times"></i> Aquesta direcció no es vàlida';
		validemail = false;
	}
	else {
		document.getElementById("email-alert").className = 'alert alert-valid';
		document.getElementById("email-alert").innerHTML = "<i class='fa fa-check'></i>";
		validemail = true;
	}
}
function validatePassword() {
	var x = document.getElementById("password").value
	if (x==null || x=="") {
		document.getElementById("password-alert").className = 'alert alert-error';
		document.getElementById("password-alert").innerHTML = '<i class="fa fa-times"></i> Aquest camp és obligatori';
		validpassword = false;
	}
	else if(x.length<8) {
		document.getElementById("password-alert").className = 'alert alert-error';
		document.getElementById("password-alert").innerHTML = "<i class='fa fa-times'></i> La contrasenya és massa curta";
		validpassword = false;
	}
	else if(x.length>30) {
		document.getElementById("password-alert").className = 'alert alert-error';
		document.getElementById("password-alert").innerHTML = "<i class='fa fa-times'></i> La contrasenya és massa llarga";
		validpassword = false;
	}
	else if(/^[a-zA-Z0-9_@.+-]+$/.test(x) == false) {
		document.getElementById("password-alert").className = 'alert alert-error';
		document.getElementById("password-alert").innerHTML = "<i class='fa fa-times'></i> La contrasenya no compleix les condicions";
		validpassword = false;
	}
	else {
		document.getElementById("password-alert").className = 'alert alert-valid';
		document.getElementById("password-alert").innerHTML = '<i class="fa fa-check"></i>';
		validpassword = true;
	}
}
function validatePassword2() {
	var y = document.getElementById("password").value
	var x = document.getElementById("password2").value
	if (x==null || x=="") {
		document.getElementById("password2-alert").className = 'alert alert-error';
		document.getElementById("password2-alert").innerHTML = '<i class="fa fa-times"></i> Aquest camp és obligatori';
		validpassword2 = false;
	}
	else if(x!=y) {
		document.getElementById("password2-alert").className = 'alert alert-error';
		document.getElementById("password2-alert").innerHTML = "<i class='fa fa-times'></i> La contrasenya no coincideix";
		validpassword2 = false;
	}
	else {
		document.getElementById("password2-alert").className = 'alert alert-valid';
		document.getElementById("password2-alert").innerHTML = '<i class="fa fa-check"></i>';
		validpassword2 = true;
	}
}
function validateCheckbox() {
	if(document.getElementById("condicions").checked) {
		validcheckbox = true;
		document.getElementById("condicions-alert").innerHTML = "";
	}
	else {
		validcheckbox = false;
		document.getElementById("condicions-alert").innerHTML = "És obligatori llegir i acceptar les condicions d'ùs.";
	}
}
function validateForm() {
	validateUsername();
	validateEmail();
	validatePassword();
	validatePassword2();
	validateCheckbox();
	if (validusername && validemail && validpassword && validpassword2 && validcheckbox) {
		return true;
	} else {
		alert("Hi ha camps que no són vàlids!");
		validateUsername();
		validateEmail();
		validatePassword();
		validatePassword2();
		validateCheckbox();
		return false;
	}
}