var checkEmail = function () {
	var emailField = dijit.byId("email_address");
	var old_email = dojo.byId("old_email").value;
	if (emailField.value != old_email && emailField.value != '') {
		dojo.xhrGet( {
			url: "./ajax/validateUniqueEmail.php?email=" + emailField.value,
			handleAs: "json",
			handle: emailValidationHandler
		});
	}
}

function emailValidationHandler(response) {
	// Clear any error messages that may have been displayed
	dijit.byId("email_address").displayMessage();
	// console.log(response.error);
	switch(response.error) {
		case "2":
			dijit.byId("email_address").displayMessage("Ja existeix un usuari amb aquest email");
			break;
		case "1":
			dijit.byId("email_address").displayMessage("L'adreça d'email no és vàlida");
			break;
	}
}

var registerEvents = function () {
	var form = document.getElementById("fRegister");
	var country = document.getElementById("country_id");
	var state = document.getElementById("state_id");
	var city = document.getElementById("city_id");
	
	if (form != null) {
		form.onsubmit = function () {
			if (validaForm("user_registration", listaCampos, listaEtiquetas, "Si us plau, omple:\n", ",\n")) {
				// si els camps state i city estan inhabilitats, els habilita
				if (city != null) {
					city.disabled = false;
				}
				if (state != null) {
					state.disabled = false;
				}
				this.submit();	
			}
			return false;
		}
	}
	// llista de provincies
	
	if (country != null) {
		country.onchange = function () {
			var ajax = new Ajax.Request('./ajax/getStateList.php?country='+country.value, {method: 'get', onComplete: stateListHandler});
		}
	}
	
	// llista de muncipis
	
	if (state != null) {
		state.onchange = function () {
			var ajax = new Ajax.Request('./ajax/getCityList.php?state='+state.value, {method: 'get', onComplete: cityListHandler});
		}
	}
	
}


