
function refreshParent(id, idcasteller) {
	if (id != null && id != 0){
		var location = window.opener.location.href;
		var i = window.opener.location.href.indexOf('idadreca');
		if (i != -1){
			if (idcasteller != null && idcasteller != 0){
				window.opener.location.href = location.substring(0, i) + "idadreca=" + id + "&id=" + idcasteller;
			}else{
				window.opener.location.href = location.substring(0, i) + "idadreca=" + id;
			}
		}else{
			if (idcasteller != null && idcasteller != 0){
				window.opener.location.href = window.opener.location.href + "?idadreca=" + id + "&id=" + idcasteller;
			}else{
				window.opener.location.href = window.opener.location.href + "?idadreca=" + id;
			}
		}
	}else{
		if (idcasteller != null && idcasteller != 0){
			window.opener.location.href = window.opener.location.href;
		}else{
			window.opener.location.href = window.opener.location.href + "?id=" + idcasteller;
		}
	}

	if (window.opener.progressWindow){
		window.opener.progressWindow.close()
	}
	window.close();
}

function refreshParentCastells(id) {
	if (id != null && id != 0){
		var location = window.opener.location.href;
		var i = window.opener.location.href.indexOf('id');
		if (i != -1){
			window.opener.location.href = location.substring(0, i) + "id=" + id;
		}else{
			window.opener.location.href = window.opener.location.href + "?id=" + id;
		}
	}else{
		window.opener.location.href = window.opener.location.href;
	}

	if (window.opener.progressWindow){
		window.opener.progressWindow.close()
	}
	window.close();
}

function refreshParentActuacio(id) {
	if (id != null && id != 0){
		var location = window.opener.location.href;
		var i = window.opener.location.href.indexOf('?');
		if (i != -1){
			window.opener.location.href = location.substring(0, i) + "?idActuacio=" + id;
		}else{
			window.opener.location.href = window.opener.location.href + "?idActuacio=" + id;
		}
	}else{
		window.opener.location.href = window.opener.location.href;
	}

	if (window.opener.progressWindow){
		window.opener.progressWindow.close()
	}
	window.close();
}

function refreshParentFiltre(filtre) {
	if (filtre != null && filtre != 0){
		var location = window.opener.location.href;
		var i = window.opener.location.href.indexOf('?');
		if (i != -1){
			window.opener.location.href = location.substring(0, i) + "?filtre=" + filtre;
		}else{
			window.opener.location.href = window.opener.location.href + "?filtre=" + filtre;
		}
	}else{
		window.opener.location.href = window.opener.location.href;
	}

	if (window.opener.progressWindow){
		window.opener.progressWindow.close()
	}
	window.close();
}

//----------------------------------------------------------
// Validació dels formats de data dels formularis (dd/mm/aaa)
//----------------------------------------------------------
function validaDataFormulari(mdata){
	if (mdata.charAt(2) != '/' || mdata.charAt(5) != '/' || mdata.length != 10){
		alert("el format de la data ha de ser el següent:\n\ndd/mm/aaaa\n\non dd és el dia(2 xifres), mm és el mes(2 xifres) i aaaa és l'any(4 xifres)");
		return false;
	}else{
		dia = parseInt(mdata.substring(0,2), 10);
		mes = parseInt(mdata.substring(3,5), 10);
		any = parseInt(mdata.substring(6,10), 10);
		if (isNaN(dia) || isNaN(mes) || isNaN(any) || dia < 1 || dia > 31 || mes < 1 || mes > 12){
			alert("la data és incorrecta, revisa-la");
			return false;
		}
	}
	return true;
}

//----------------------------------------------------------
// Validació dels formats d'hora dels formularis (hh:mm)
//----------------------------------------------------------
function validaHoraFormulari(hora){
	try{
		if (hora.charAt(2) != ':' || hora.length != 5){
			alert("el format de l'hora ha de ser el següent:\n\nhh:mm\n\non hh és l'hora(2 xifres) i mm són els minuts(2 xifres)");
			return false;
		}else{
			h = parseInt(hora.substring(0,2), 10);
			m = parseInt(hora.substring(3,5), 10);
			if (h < 0 || h > 23 || m < 0 || h > 59){
				alert("l'hora és incorrecta, revisa-la");
				return false;
			}
		}
	}catch(e){
		return false;
	}
	return true;
}

function validarEmail(valor) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor)){
		return true;
	} else {
		alert("L'adreça de mail és incorrecta.");
		return false;
	}
}

function validarDNI(valor){
	dni = valor.substring(0,valor.length-1);
	let = valor.charAt(valor.length-1);

	if (!isNaN(let)) {
		alert("el dni no és correcte");
		return false;
	}else{
		cadena = "TRWAGMYFPDXBNJZSQVHLCKET";
		posicion = dni % 23;
		letra = cadena.substring(posicion,posicion+1);

		if (letra != let.toUpperCase()){
			alert("el dni no és correcte");
			return false;
		}
	}

	return true;
}

function validarTelefon(valor){
	if (valor.length != 9 || isNaN(valor) || valor.charAt(0) != '9'){
		alert("el número de telèfon és incorrecte");
		return false;
	}
	
	return true;
}

function validarTelefonMobil(valor){
	if (valor.length != 9 || isNaN(valor) || valor.charAt(0) != '6'){
		alert("el número de telèfon mòbil és incorrecte");
		return false;
	}
	
	return true;
}

