var formEvents = function () {
	var $form = $("#fEmpresa");
	$form.validate({
		rules: {
			email: {
				required: true,
				email	: true
			},
			conf_email: {
				required: true,
				email	: true,
				equalTo : "#email"
			},
			conf_password: {
				required: true,
				equalTo : "#password"
			},
			cif: {
				cifnif: true,
				required: true
			}
		},
		submitHandler: function(form) {
			if ($("#acepto").attr("checked")) {
				form.submit();
			} else {
				alert("Debes aceptar la condiciones de uso para poder continuar");
			}
 		}
	});
	return false;
}

var formEvents_ = function () {
	var form = document.getElementById("fEmpresa");
	
	var email_field = document.getElementById("email");
	var email_conf_field = document.getElementById("conf_email");
	
	var pwd = document.getElementById("password");
	var conf_pwd = document.getElementById("conf_password");
	
	email_field.onchange = function () {
		if (this.value != '') {
			var unique = 1;
			Email.check(this.value);
		}
	}
	
	email_conf_field.onchange = function () {
		if (this.value != '') {
			var unique = 1;
			Email.check(this.value);
		}
	}
	
	form.onsubmit = function () {
		var id = document.getElementById("id_usr").value;
		if (id != '') {
			//listaCampos = listaCampos.remove("password");
			//listaCampos = listaCampos.remove("conf_password");
			
			// calcula el valor de la etiqueta desde el label->for
			var labels = document.getElementsByTagName("LABEL");
			for (var i=0; i<labels.length; i++) {
				var targetname=labels[i].getAttribute('for')?labels[i].getAttribute('for'):labels[i].getAttribute('htmlFor');
				if (targetname == 'password') {
					listaEtiquetas.remove(labels[i].firstChild.data);
					listaCampos.remove(targetname);
				}
				if (targetname == 'conf_password') {
					listaEtiquetas.remove(labels[i].firstChild.data);
					listaCampos.remove(targetname);
				}
			}
		}
		if (email_field.value != email_conf_field.value) {
			// alert("El email y su confirmación no coinciden");
			Msg.show("El email y su confirmación no coinciden");
			return false;
		} else {
			if (conf_pwd.value != pwd.value) {
				Msg.show("La contraseña y su confirmación no coinciden");
				return false;
			} else {
				if (validaForm("formulario", listaCampos, listaEtiquetas, "Por favor, rellene:\n", ",\n")) {
					// acepta las condiciones?
					var acepto = document.getElementById("acepto");
					if (acepto.checked) {
						Msg.clear();
						this.submit();
					} else {
						Msg.show("Debes aceptar la condiciones de uso para poder continuar");
						return false;
					}
					
					this.submit();
				} else {
					return false;
				}
			}
		}
	}
	var nombre = document.getElementById("nombre");
	var apellidos = document.getElementById("apellidos");
	
	nombre.onkeyup = function () {
		var apellidos = document.getElementById("apellidos");
		if (this.value != '') {		
			updateH('subtitle', this.value+" "+apellidos.value);
		} else {
			updateH('subtitle', default_name);
		}
	}
	apellidos.onkeyup = function () {
		var nombre = document.getElementById("nombre");
		if (this.value != '') {		
			updateH('subtitle', nombre.value +" "+this.value);
		} else {
			updateH('subtitle', default_name);
		}
	}
}

var errorEvents = function () {
	alert("Se ha producido un error.\n\nPor favor, verifique que la dirección de email es correcta\ny que no la tiene ningún otro usuario.");
}

// Pop-up de ventana emergente
function load(file, name) {
	if (name == null) {
		name = 'window1'
	}
	// "/info/prot_datos.php"
	if (file != null) {
		var load = window.open(file,name,'height=480,width=700,scrollbars=1,resizable=1');
	}
}
