var ClubLabores = new Class.create();

ClubLabores.prototype = {
	initialize : function(){
		$$('a.labor').each(function(item){
			Event.observe(item, 'click', this.mostrarLaborEvt.bind(this));
		}.bind(this));

		Event.observe($('submitForm'), 'click', this.validarForm.bind(this));

		Event.observe($('formularioLabores'), 'click', function(){
			$('loadingClub').hide();
			$('mensajeClub').hide();
			$('formularioClub').show();
			$('textoClub').show();
			$('textoClub2').show();
			$('divFormLabores').show();
		});

		Event.observe($('idPais'), 'change', function(){
			if ($('idPais').options[$('idPais').selectedIndex].value == 'es'){
				$('provincia').disabled = false;
				idProvinciaSel = '<?=$carrito->idProvincia?>';
				if (idProvinciaSel != ''){
					for(cont = 0; cont < $('provincia').options.length; cont++){
						if($('provincia').options[cont].value == idProvinciaSel){
							$('provincia').selectedIndex = cont;
						}
					}
				}else{
					$('provincia').selectedIndex = 0;
				}
			}else{
				for(cont = 0; cont < $('provincia').options.length; cont++){
					if ($('provincia').options[cont].value == 100){
						$('provincia').selectedIndex = cont;
					}
				}
				$('provincia').disabled = true;
			}
		});
	},

	mostrarLaborEvt : function(evt){
		elemento = Event.element(evt);
		mostrarLabor(elemento.getAttribute("idLabor"));
	},

	validarForm : function(){
		if(!$('condiciones').checked){
			alert($('condiciones').getAttribute("errMsg"));
			$('condiciones').focus();
			return false;
		}

		if(!$('condiciones2').checked){
			alert($('condiciones2').getAttribute("errMsg"));
			$('condiciones2').focus();
			return false;
		}

		if($('nombre').value == ''){
			alert($('nombre').getAttribute('errMsg'));
			$('nombre').focus();
			return false;
		}

		if($('apellidos').value == ''){
			alert($('apellidos').getAttribute('errMsg'));
			$('apellidos').focus();
			return false;
		}

		if($('direccion').value == ''){
			alert($('direccion').getAttribute('errMsg'));
			$('direccion').focus();
			return false;
		}

		if($('cp').value == ''){
			alert($('cp').getAttribute('errMsg'));
			$('cp').focus();
			return false;
		}

		if($('poblacion').value == ''){
			alert($('poblacion').getAttribute('errMsg'));
			$('poblacion').focus();
			return false;
		}

		if($('imagen').value == ''){
			alert($('imagen').getAttribute('errMsg'));
			$('imagen').focus();
			return false;
		}

		if(!validarMail($('email').value)){
			alert($('email').getAttribute('errMsg'));
			$('email').focus();
			return false;
		}

		if(($('imagenExt').value.toLowerCase() != 'jpg') && ($('imagenExt').value.toLowerCase() != 'jpeg')){
			alert($('imagenExt').getAttribute('errMsg'));
			return false;
		}

		$('formLabores').submit();
		$('formularioClub').hide();
		$('textoClub').hide();
		$('textoClub2').hide();
		$('loadingClub').show();
	}
}

function ocultarFormLabores(){
	$('divFormLabores').hide();
	$('loadingClub').hide();
	$('mensajeClub').hide();
}

function finalFormLabores(){
	$('loadingClub').hide();
	$('mensajeClub').show();
}

Event.observe(window,"load",function(){ new ClubLabores(); });