$(document).ready(function(){
	$('#Seleccion').click(function(){
		if($(this).attr('value') == 'La solidaritat i el voluntariat'){
			$('#Seleccion2').css('visibility','visible');
			$('#label_subtematica').css('visibility','visible');
			$('#bloc3').css('display','block');
		}
		else{
			$('#Seleccion2').css('visibility','hidden');
			$('#label_subtematica').css('visibility','hidden');
			$('#bloc3').css('display','none');
		}
	});
});

/* Generar Captcha */
function generar(){
	var x = document.getElementById("letras")
	var y = Math.round(Math.random()*(6-1)+1);
	x.src = "img/img"+y+".gif";
	init();
	}
	
/* Validar Formulario */

var validationConfig = [
	{ref:'centre_gran',datamask:/^[a-z,A-Z,0-9,.,\u00B4,\u00B7,\u0095,\u002D,;,(,),:, ,\u00e1,\u00e9,\u00ed,\u00f3,\u00fa,\u00f1,\u00e0,\u00c0,\u00e8,\u00c8,\u00f2,\u00d2,\u00ef,\u00cf,\u00fc,\u00dc,\u00e7,\u00c7,\u00c1,\u00c9,\u00cd,\u00d3,\u00da,\u00d1,\u0027]{1,50}$/,msg:'Introduce el nombre del centro (solo letras y n\u00fameros)'},
	{ref:'dia_inici',datamask:/^[0-9]{2}$/,msg:'Introduce el d\u00eda de inicio (solo 2 n\u00fameros sin espacio) Ej: 05'},
	{ref:'mes_inici',datamask:/^[0-9]{2}$/,msg:'Introduce el mes de inicio (solo 2 n\u00fameros sin espacio) Ej: 07'},
	{ref:'any_inici',datamask:/^[0-9]{4}$/,msg:'Introduce el a\u00f1o de inicio (solo 4 n\u00fameros sin espacio) Ej: 2010'},
	{ref:'dia_final',datamask:/^[0-9]{2}$/,msg:'Introduce el d\u00eda de finalizaci\u00f3n (solo 2 n\u00fameros sin espacio) Ej: 05'},
	{ref:'mes_final',datamask:/^[0-9]{2}$/,msg:'Introduce el mes de finalizaci\u00f3n (solo 2 n\u00fameros sin espacio) Ej: 07'},
	{ref:'any_final',datamask:/^[0-9]{4}$/,msg:'Introduce el a\u00f1o de finalizaci\u00f3n (solo 4 n\u00fameros sin espacio) Ej: 2010'},
	{ref:'num_sessions',datamask:/^[0-9]{1,3}$/,msg:'Introduce el n\u00famero de sesiones realizadas (m\u00e1ximo 3 n\u00fameros sin espacio) Ej: 15'},
	{ref:'num_voluntaris',datamask:/^[0-9]{1,3}$/,msg:'Introduce el n\u00famero de voluntarios participantes (m\u00e1ximo 3 n\u00fameros sin espacio) Ej: 4'},
	{ref:'num_nens',datamask:/^[0-9]{1,3}$/,msg:'Introduce el n\u00famero de ni\u00f1os participantes (m\u00e1ximo 3 n\u00fameros sin espacio) Ej: 112'},
	{ref:'nousTemes',datamask:/^[a-z,A-Z,0-9,.,\u00B4,\u00B7,\u0095,\u002D,;,(,),:,\u003f,\u00bf, ,\u00e1,\u00e9,\u00ed,\u00f3,\u00fa,\u00f1,\u00e0,\u00c0,\u00e8,\u00c8,\u00f2,\u00d2,\u00ef,\u00cf,\u00fc,\u00dc,\u00e7,\u00c7,\u00c1,\u00c9,\u00cd,\u00d3,\u00da,\u00d1,\u0027]+$/i,msg:'No puede quedar vacio ni con los caracteres /, <, >, "'},
	{ref:'mesOpinions',datamask:/^[a-z,A-Z,0-9,.,\u00B4,\u00B7,\u0095,\u002D,;,(,),:,\u003f,\u00bf, ,\u00e1,\u00e9,\u00ed,\u00f3,\u00fa,\u00f1,\u00e0,\u00c0,\u00e8,\u00c8,\u00f2,\u00d2,\u00ef,\u00cf,\u00fc,\u00dc,\u00e7,\u00c7,\u00c1,\u00c9,\u00cd,\u00d3,\u00da,\u00d1,\u0027]+$/i,msg:'No puede quedar vacio ni con los caracteres /, <, >, "'},
	{ref:'captcha',datamask:/^[UW4X,3XXA,2X4H,A6KN,K6A4]{4}$/,msg:'Introduce los caracteres del captcha (las letras van siempre en may\u00fasculas) Ej: A3B4'}
]

function init() {
	if (!document.getElementById || !document.getElementsByTagName || !document.createElement || !document.createTextNode) {
		alert('You need a new browser!');
		return;
	}
	document.getElementsByTagName('form')[0].onsubmit = function(){return validateForm();};
}

function validateForm() {
	var datos,errorMsg,el,ol,anchor;
	var errorCount = 0;
	var captcha_count = 0;
	datos = document.getElementById('datos');
	
	// we might have to reset the error messages
	if  (document.getElementById('error-message')) {
		datos.removeChild(document.getElementById('error-message'));
		var ps = document.getElementsByTagName('input');
		for (i = 0;i < ps.length;i++) {
			if (ps[i].className == 'error'){
				ps[i].className = '';
				ps[i].style.border = '#656565 1px solid';
				ps[i].style.padding = '1px 0px 1px 0px';
			}
		}
		var pt = document.getElementsByTagName('textarea');
		for (i = 0;i < pt.length;i++) {
			if (pt[i].className == 'error'){
				pt[i].className = '';
				pt[i].style.border = '#656565 1px solid';
				pt[i].style.padding = '1px 0px 1px 0px';
			}
		}
	}
		
	// validate and create messages
	errorMsg = newEl('div',{id:'error-message'});
	
	el = newEl('h2');
	el.appendChild(newEl('a',{id:'error-name',name:'error-name',href:'#datos',title:'Vuelve al inicio del formulario'},'Por favor, verifica los siguientes errores:'));
	errorMsg.appendChild(el);
			
	ol = newEl('ol');
	if(document.getElementById('Seleccion').value == ""){
		el = newEl('li');
		anchor = newEl('a',{href:'#'+document.getElementById('Seleccion').id},'Selecciona una campanya');
		anchor.onclick = function(){document.getElementById(this.href.split('#')[1]).focus();return false;};
		el.appendChild(anchor);
		ol.appendChild(el);
		document.getElementById('Seleccion').className = 'error';
		document.getElementById('Seleccion').style.border = '2px solid #ff0000';
		document.getElementById('Seleccion').style.padding = '4px 0px 4px 0px';
		errorCount++;
	}else if(document.getElementById('Seleccion').className == 'error'){
				document.getElementById('Seleccion').className = '';
				document.getElementById('Seleccion').style.border = '#656565 1px solid';
				document.getElementById('Seleccion').style.padding = '1px 0px 1px 0px';
			}
	for (var i in validationConfig)
	{	
		var obj = document.getElementById(validationConfig[i].ref)
		var val = obj.value.match(validationConfig[i].datamask);

		
		if (val == null) {
			el = newEl('li');
			anchor = newEl('a',{href:'#'+validationConfig[i].ref},validationConfig[i].msg);
			anchor.onclick = function(){document.getElementById(this.href.split('#')[1]).focus();return false;};
					
			el.appendChild(anchor);
			ol.appendChild(el);
					
			if (document.getElementById(validationConfig[i].ref)){
				document.getElementById(validationConfig[i].ref).className = 'error';
				document.getElementById(validationConfig[i].ref).style.border = '2px solid #ff0000';
				document.getElementById(validationConfig[i].ref).style.padding = '4px 0px 4px 0px';}
			errorCount++;
		}
				
	}
		
		
	
	if (errorCount > 0){
		errorMsg.appendChild(ol)
		datos.insertBefore(errorMsg,datos.getElementsByTagName('form')[0]);
		document.getElementById('error-name').focus();
		return false;
	} else {
	    return true;
	}
}
		
function newEl(name,atts,txt) {
	var e = document.createElement(name);
	if (atts != null && atts != 'undefined') {
		for (var i in atts) {
		 	e.setAttribute(i,atts[i]);
		}
	}
	if (typeof txt == 'string') {
		e.appendChild(document.createTextNode(txt));
	}
	return e;	
}
