var resultatTexte = null;
var resultatLlistaOcup = null;
var resultatIntegrantOcup = null;
var resultatUsuariOcup = null;
var resultatContacteOcup = null;

function XMLHttpRequestXooco(){
	var xmlhttp=false;
	
	try{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}catch(e){
		try{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}catch(E){
			xmlhttp = false;
		}
	}
	
	if(!xmlhttp && typeof XMLHttpRequest != 'undefined'){
		xmlhttp = new XMLHttpRequest();
	}
	
	return xmlhttp;
}

//Editar la llista
function editarLlista(nomLlista, codSes){
	window.location="ompleDadesLlista.php?nomLlista="+ escape(nomLlista);
}

//Esborrem la llista
function esborrarLlista(nomLlista, frase_SegDesBorrLlista){
	if(confirm(frase_SegDesBorrLlista +': '+ nomLlista +' ?')){
		window.location="esborraLlista.php?nomLlista="+ escape(nomLlista);
	}
}

//Esborrem la llista preferida
function esborrarLlistaPreferida(nomLlista, frase_SegDesBorrLlista){
	if(confirm(frase_SegDesBorrLlista +': '+ nomLlista +' ?')){
		window.location="esborraLlistaPreferida.php?nomLlista="+ escape(nomLlista);
	}
}

//Enviem la llista a un amic
function enviarLlistaAAmic(nomLlista, frase_NoEsPodenEnviarLlistesPrivades, visivilitat){
	if (visivilitat == 'privada'){
		alert (frase_NoEsPodenEnviarLlistesPrivades);	
	}else{
		window.location="enviaAUnAmic.php?nomLlista="+ escape(nomLlista);
	}
}

//Esborrem la llista
function esborrarContacte(eMailContacte, frase_SegDesBorrContacte){
	if(confirm(frase_SegDesBorrContacte +': '+ eMailContacte +' ?')){
		window.location="esborraContacte.php?eMailContacte="+ eMailContacte;
	}
}

//Esborrem integrant
function esborrarIntegrant(nomIntegrant, frase_SegDesBorrIntegrant){
	var nomLlista = escape(document.getElementById('nomLlista').value);
	var defLlista = escape(document.getElementById('defLlista').value);
	var tagLlista = escape(document.getElementById('tagLlista').value);
	
	if(confirm(frase_SegDesBorrIntegrant +': '+ nomIntegrant +' ?')){
		window.location="esborraIntegrantALlista.php?nomLlista="+ nomLlista +"&nomIntegrant="+ nomIntegrant +"&defLlista="+ defLlista +"&tagLlista="+ tagLlista;		
	}
}

//Modificacio dades personals
function validarDades(frase_NoCoincideixenClaus, frase_SanDomplirTotsElsCamps){
	var clauUsu = document.getElementById('clauUsu').value;
	var repClauUsu = document.getElementById('repClauUsu').value;
	var eMailUsu = document.getElementById('eMailUsu').value;

	if (clauUsu != "" && repClauUsu != "" && eMailUsu != ""){
		if (clauUsu != repClauUsu) {
			alert(frase_NoCoincideixenClaus + '.');
		}else{
			document.formModificaUsuari.submit();
		}
	}else{
		alert(frase_SanDomplirTotsElsCamps + '.');
	}
}


function isMail(Cadena) {
	Punto = Cadena.substring(Cadena.lastIndexOf('.') + 1, Cadena.length);			// Cadena del .com
	Dominio = Cadena.substring(Cadena.lastIndexOf('@') + 1, Cadena.lastIndexOf('.')); 	// Dominio @lala.com
	Usuario = Cadena.substring(0, Cadena.lastIndexOf('@'));					// Cadena lalala@
	Reserv = "@/º'\"+*{}\<>?¿[]áéíóú#·¡!^*;,:";						// Letras Reservadas

	// Añadida por El Codigo para poder emitir un alert en funcion de si email valido o no
	valido = true;


	// verifica qie el Usuario no tenga un caracter especial
	for (var Cont=0; Cont<Usuario.length; Cont++) {
		X = Usuario.substring(Cont,Cont+1)
		if (Reserv.indexOf(X)!=-1)
                	valido = false;
	}

	// verifica qie el Punto no tenga un caracter especial
	for (var Cont=0; Cont<Punto.length; Cont++) {
		X=Punto.substring(Cont,Cont+1)
		if (Reserv.indexOf(X)!=-1)
			valido = false;
	}

	// verifica qie el Dominio no tenga un caracter especial
	for (var Cont=0; Cont<Dominio.length; Cont++) {
		X=Dominio.substring(Cont,Cont+1)
		if (Reserv.indexOf(X)!=-1)
			valido = false;
		}

	// Verifica la sintaxis básica.....
	if (Punto.length<2 || Dominio <1 || Cadena.lastIndexOf('.')<0 || Cadena.lastIndexOf('@')<0 || Usuario<1) {
		valido = false;
	}

	// Añadido por El Código para que emita un alert de aviso indicando si email válido o no
	if (valido) {
		return ('OK');	//cambiar por return true para hacer el submit del formulario en caso de validacion correcta
	} else {
		return ('KO');
	}
}


function validarDadesRegistre(frase_AquestUsuariEstaRepetit, frase_SanDomplirTotsElsCamps, frase_ADacceptarElsTermesDus, frase_NoSadmetenCaractersEspecials, frase_eMailNoValid){
	var nomUsu = escape(document.getElementById('nomUsu').value);
	var eMailUsu = document.getElementById('eMailUsu').value;
	var respostaUsu = document.getElementById('respostaUsu').value;

	if ((nomUsu.indexOf('%22',0) == -1) && (nomUsu.indexOf('%27',0) == -1) && (nomUsu.indexOf('%5C',0) == -1) && (nomUsu.indexOf('%u',0) == -1)){
		if (document.getElementById('acceptoCondicionsCkBox').checked){
			if (nomUsu != "" && eMailUsu != "" && respostaUsu != ""){
				if (resultatUsuariOcup.indexOf("Error!",0) == "-1"){
					if (isMail(eMailUsu)== 'OK'){
						document.formCreaUsuari.submit();
					}else{
						alert(frase_eMailNoValid + '.');			
					}
				}else{
					alert(frase_AquestUsuariEstaRepetit + '.');			
				}
			}else{
				alert(frase_SanDomplirTotsElsCamps + '.');
			}
		}else{
			alert(frase_ADacceptarElsTermesDus + '.');
		}
	}else{
		alert(frase_NoSadmetenCaractersEspecials + '.');
	}
}

function validarEnviamentCorreu(frase_SanDomplirTotsElsCamps, frase_eMailNoValid){
	var elTeuNom = escape(document.getElementById('elTeuNom').value);
	var eMail = document.getElementById('eMail').value;
	var textMissatge = document.getElementById('textMissatge').value;
	
	if (elTeuNom != "" && eMail != "" && textMissatge != ""){
		if (isMail(eMail) == 'OK'){
			document.enviaMail.submit();
		}else{
			alert(frase_eMailNoValid + '.');			
		}
	}else{
		alert(frase_SanDomplirTotsElsCamps + '.');
	}
}

//Guardar la llista
function guardarLlista(frase_NomLlistaEstaRepetit, frase_NecesitaNomPerGuardarLlista, frase_NoSadmetenCaractersEspecials){
	var nomLlista = escape(document.getElementById('nomLlista').value);
	var visivilitatLlista = document.getElementById('visivilitatLlista').value;
	var tipusLlista = document.getElementById('tipusLlista').value;
	
	document.getElementById('nomLlista').disabled = false;  //Habilitem que puguin seleccionar una altre llista
	
	if ((nomLlista.indexOf('%22',0) == -1) && (nomLlista.indexOf('%27',0) == -1) && (nomLlista.indexOf('%5C',0) == -1) && (nomLlista.indexOf('%u',0) == -1)){
		if (nomLlista != ""){
			if (resultatLlistaOcup.indexOf("Error!",0) == "-1"){
				if (visivilitatLlista == 'tag_privada'){
					var accio="creaLlista.php?visivilitatLlista=privada&tipusLlista=" + tipusLlista;
				}else{
					var accio="creaLlista.php?visivilitatLlista=publica&tipusLlista=" + tipusLlista;
				}
	
				document.formAfegeixLlistaPublica.action = accio;
				document.formAfegeixLlistaPublica.submit();
			}else{
				alert(frase_NomLlistaEstaRepetit + '.');
			}		
		}else{
			alert(frase_NecesitaNomPerGuardarLlista + '.');
		}
	}else{
		alert(frase_NoSadmetenCaractersEspecials + '.');
	}			
}

//Afegim integrant a la vista i a la llista
function afegeixIntegrant(frase_NomIntegrantEstaRepetit, frase_NomLlistaEstaRepetit, frase_PrimerAsignarNomIntegrant, frase_PrimerAsignarNomLlista, frase_NoSadmetenCaractersEspecials){
	var pos_url = 'afegeixIntegrantALlista.php';
	var nomLlista = escape(document.getElementById('nomLlista').value);

	if ((nomLlista.indexOf('%22',0) == -1) && (nomLlista.indexOf('%27',0) == -1) && (nomLlista.indexOf('%5C',0) == -1) && (nomLlista.indexOf('%u',0) == -1)){
		var defLlista = escape(document.getElementById('defLlista').value);
		var tagLlista = escape(document.getElementById('tagLlista').value);
		var tipusLlista = document.getElementById('tipusLlista').value;
		var visivilitatLlista = document.getElementById('visivilitatLlista').value;
		var nomIntegrant = escape(document.getElementById('nomIntegrant').value);
		//var comentariIntegrant = escape(document.getElementById('comentariIntegrant').value);

		if (visivilitatLlista == 'tag_privada'){
			visivilitatLlista = 'privada';
		}else{
			visivilitatLlista = 'publica';
		}
	
		var comentariIntegrant ='';
		var req = XMLHttpRequestXooco();
	
		try {
			document.formLlista.tipusLlista.disabled = true;  //Deshabilitem que puguin seleccionar una altre llista
			document.getElementById('nomLlista').disabled = true;  //Deshabilitem que puguin seleccionar una altre llista
			document.getElementById('LayerBotoGravaLlistaPagUsu').style.visibility ='visible';
		}
		catch (e) {
		}
		
		if (tipusLlista != 'tag_llistaFotos'){
			comentariIntegrant = escape(document.getElementById('comentariIntegrant').value);
		}
		
		if (nomLlista != ""){
			if (nomIntegrant != ""){
				if (resultatLlistaOcup.indexOf("Error!",0) == "-1"){
					if (resultatIntegrantOcup.indexOf("NO",0) == "-1"){
						if (req) {
							req.onreadystatechange = function() {
								if (req.readyState == 4 && (req.status == 200 || req.status == 304)) {
									document.getElementById('resultatIntegrants').innerHTML = req.responseText;
								}
							}
							req.open('POST', pos_url +'?nomLlista='+ nomLlista +'&defLlista='+ defLlista +'&tagLlista='+ tagLlista +'&nomIntegrant='+ nomIntegrant +'&comentariIntegrant='+ comentariIntegrant +'&tipusLlista='+ tipusLlista +'&visivilitatLlista='+ visivilitatLlista, true);
				
							req.send(null);
							document.getElementById('nomIntegrant').value = "";
							document.getElementById('comentariIntegrant').value = "";
						}
					}else{
						alert(frase_NomIntegrantEstaRepetit + '.');
					}
				}else{
					alert(frase_NomLlistaEstaRepetit + '.');
				}
			}else{
			alert(frase_PrimerAsignarNomIntegrant + '.');
		}
		}else{
			alert(frase_PrimerAsignarNomLlista + '.');
		}
	}else{
		alert(frase_NoSadmetenCaractersEspecials + '.');
	}		
}


//Afegim integrant a la vista i a la llista
function afegeixIntegrantAmbLlistaVerificada(frase_NomIntegrantEstaRepetit, frase_PrimerAsignarNomIntegrant, frase_PrimerAsignarNomLlista){
	var pos_url = 'afegeixIntegrantALlista.php';
	var nomLlista = escape(document.getElementById('nomLlista').value);
	var defLlista = escape(document.getElementById('defLlista').value);
	var tagLlista = escape(document.getElementById('tagLlista').value);
	var tipusLlista = document.getElementById('tipusLlista').value;
	var visivilitatLlista = document.getElementById('visivilitatLlista').value;
	var nomIntegrant = escape(document.getElementById('nomIntegrant').value);

	var comentariIntegrant ='';
	var req = XMLHttpRequestXooco();

	if (tipusLlista != 'tag_llistaFotos'){
		comentariIntegrant = escape(document.getElementById('comentariIntegrant').value);
	}
	

	if (nomLlista != ""){	
		if (nomIntegrant != ""){
			if (resultatIntegrantOcup.indexOf("NO",0) == "-1"){
				if (req) {
					req.onreadystatechange = function() {
						if (req.readyState == 4 && (req.status == 200 || req.status == 304)) {
							document.getElementById('resultatIntegrants').innerHTML = req.responseText;
						}
					}
					req.open('POST', pos_url +'?nomLlista='+ nomLlista +'&defLlista='+ defLlista +'&tagLlista='+ tagLlista +'&nomIntegrant='+ nomIntegrant +'&comentariIntegrant='+ comentariIntegrant +'&tipusLlista='+ tipusLlista+'&visivilitatLlista='+ visivilitatLlista, true);
		
					req.send(null);
					document.getElementById('nomIntegrant').value = "";
					document.getElementById('comentariIntegrant').value = "";
				}
			}else{
				alert(frase_NomIntegrantEstaRepetit + '.');
			}
		}else{
			alert(frase_PrimerAsignarNomIntegrant + '.');
		}
	}else{
		alert(frase_PrimerAsignarNomLlista + '.');
	}
}

//Afegim contacte al usuari
function afegeixContacte(frase_NomContacteEstaRepetit, frase_TotContacteNecesitaEMail){
	var pos_url = 'afegeixContacte.php'; 
	var pos_url2 = 'refrescaContacte.php';
	var eMailContacte = document.getElementById('eMailContacte').value;
	var nomContacte = escape(document.getElementById('nomContacte').value);
	var req = XMLHttpRequestXooco();
	var req2 = XMLHttpRequestXooco();
	
	if (eMailContacte != ""){
	  	if (resultatContacteOcup.indexOf("NO",0) == "-1"){
			if (req) {
					req.onreadystatechange = function() {
				if (req.readyState == 4 && (req.status == 200 || req.status == 304)) {
					document.getElementById('LayerResultatContactes').innerHTML = req.responseText;
				}
				}
				req.open('POST', pos_url +'?eMailContacte='+ eMailContacte +'&nomContacte='+ nomContacte,true);
	
				req.send(null);
				document.getElementById('nomContacte').value = "";
				document.getElementById('eMailContacte').value = "";
			}

			if (req2) {
					req2.onreadystatechange = function() {
				if (req2.readyState == 4 && (req2.status == 200 || req2.status == 304)) {
					document.getElementById('LayerResultatContactesALlista').innerHTML = req2.responseText;
				}
				}
				req2.open('POST', pos_url2  ,true);
	
				req2.send(null);
			}

		}else{
		alert(frase_NomContacteEstaRepetit + '.');
	  	}
	}else{
		alert(frase_TotContacteNecesitaEMail + '.');
	}
}

//Ocultar camp password
function ocultarCampPassword(){
	var pos_url = 'usuariOMail.php';	
	var req = XMLHttpRequestXooco();	
	var oblitPass = 'true';
	
	if (document.getElementById('passUsu').disabled == true){
		document.getElementById('passUsu').disabled = false;
		oblitPass = 'true';
	}else{
		document.getElementById('passUsu').disabled = true;
		oblitPass = 'false';
	}
	
	if (req) {
			req.onreadystatechange = function() {
		if (req.readyState == 4 && (req.status == 200 || req.status == 304)) {
			document.getElementById('ContenidorUsuari').innerHTML = req.responseText;
			resultatUsuariOcup = req.responseText;
		}
		}
		req.open('GET', pos_url + '?oblitPass=' + oblitPass,true);
		req.send(null);
	}
	
}

//Chequejem que el nom el usuari no estigui ocupat
function chk_usuari(){
	var pos_url = 'comprovaDisponivilitatUser.php';
	var nom = escape(document.getElementById('nomUsu').value);
	var req = XMLHttpRequestXooco();
	
	if (req) {
			req.onreadystatechange = function() {
		if (req.readyState == 4 && (req.status == 200 || req.status == 304)) {
			document.getElementById('resultat').innerHTML = req.responseText;
			resultatUsuariOcup = req.responseText;
		}
		}
		req.open('GET', pos_url +'?nomUsu='+nom,true);
		req.send(null);
	}
}

//Chequejem que el nom de la llista no estigui ocupat
function chk_nomLlista(){
	var pos_url = 'comprovaDispoNomLlista.php';
	var nomLlista = escape(document.getElementById('nomLlista').value);
	var req = XMLHttpRequestXooco();
	
	if (req) {
			req.onreadystatechange = function() {
		if (req.readyState == 4 && (req.status == 200 || req.status == 304)) {
			document.getElementById('LayerCheckLlistaLliure').innerHTML = req.responseText;
			resultatLlistaOcup = req.responseText;
		}
		}
		req.open('POST', pos_url +'?nomLlista='+ nomLlista,true);
		req.send(null);
	}
}

//Chequejem que el nom del integrant no estigui ocupat
function chk_IntegrantOcupat(){
	var pos_url = 'comprovaDispoNomIntegrant.php';
	var nomIntegrant = escape(document.getElementById('nomIntegrant').value);
	var nomLlista = escape(document.getElementById('nomLlista').value);
	var req = XMLHttpRequestXooco();
	
	if (req) {
			req.onreadystatechange = function() {
		if (req.readyState == 4 && (req.status == 200 || req.status == 304)) {
			document.getElementById('resultatIntegrantOcupat').innerHTML = req.responseText;
			resultatIntegrantOcup = req.responseText;
		}
		}
		req.open('POST', pos_url +'?nomIntegrant='+ nomIntegrant +'&nomLlista='+ nomLlista,true);
		req.send(null);
	}
}

//Chequejem que el eMail del contacte no estigui ocupat
function chk_ContacteOcupat(){
	var pos_url = 'comprovaDispoeMailContacte.php';
	var eMailContacte = document.getElementById('eMailContacte').value;
	var req = XMLHttpRequestXooco();
	
	if (req) {
			req.onreadystatechange = function() {
		if (req.readyState == 4 && (req.status == 200 || req.status == 304)) {
			document.getElementById('resultatContacteOcupat').innerHTML = req.responseText;
			resultatContacteOcup = req.responseText;
		}
		}
		req.open('POST', pos_url +'?eMailContacte='+ eMailContacte,true);
		req.send(null);
	}
}


//Mostra la llista de integrants
function mostraLlistaIntegrants(){
	var pos_url = 'mostraLlistaIntegrants.php';
	var nomLlista = escape(document.getElementById('nomLlista').value);
	var req = XMLHttpRequestXooco();
	

	if (req) {
			req.onreadystatechange = function() {
		if (req.readyState == 4 && (req.status == 200 || req.status == 304)) {
			document.getElementById('resultatIntegrants').innerHTML = req.responseText;
		}
		}
		req.open('POST', pos_url +'?nomLlista='+ nomLlista,true);

		req.send(null);
	}

}

function mostraTipusLlista(){
	var pos_url = 'mostraComentariLlista.php';
	var nomLlista = escape(document.getElementById('nomLlista').value);
	var req = XMLHttpRequestXooco();
	
	if (req) {
			req.onreadystatechange = function() {
		if (req.readyState == 4 && (req.status == 200 || req.status == 304)) {
			document.getElementById('afegeixComFotVid').innerHTML = req.responseText;
		}
		}
		req.open('POST', pos_url +'?nomLlista='+ nomLlista,true);

		req.send(null);
	}
}


function canviaTipusLlista(){
	var pos_url = 'afegeixComentariLlista.php';
	var tipusLlista = document.getElementById('tipusLlista').value;
	var req = XMLHttpRequestXooco();
	
	if (req) {
			req.onreadystatechange = function() {
		if (req.readyState == 4 && (req.status == 200 || req.status == 304)) {
			document.getElementById('afegeixComFotVid').innerHTML = req.responseText;
		}
		}
		req.open('POST', pos_url +'?tipusLlista='+ tipusLlista,true);

		req.send(null);
	}
}

function canviaVisivilitatLlista(){
	var visivilitatLlista = document.getElementById('visivilitatLlista').value;

	if (visivilitatLlista == 'tag_privada'){
		document.getElementById('VerificaLliuCon').checked = true;
		document.getElementById('LayerTextVisivilitatLlista').style.visibility ='hidden';
		document.getElementById('tabContactes').style.visibility ='visible';
	}else{
		document.getElementById('LayerTextVisivilitatLlista').style.visibility ='visible';
	}
}

function canviaVisivilitatTabContacte(){
	var VerificaLliuCon = document.getElementById('VerificaLliuCon').checked;
	
	if (VerificaLliuCon == false){
		document.getElementById('tabContactes').style.visibility ='hidden';
	}else{
		document.getElementById('tabContactes').style.visibility ='visible';
	}
}

function afegeixAFavorits(nomLlista, nomUsu, frasePerAfegirFavoritsRegPrimer, fraseLlistaAfegida){
	var pos_url = 'afegeixLlistaPreferits.php';
	
	if (nomUsu == 'anonim'){
		alert(frasePerAfegirFavoritsRegPrimer + '.');
	}else{
		var req = XMLHttpRequestXooco();
	
		if (req) {
			req.open('POST', pos_url +'?nomLlista='+ nomLlista + "&nomUsu=" + nomUsu,true);
			req.send(null);
			alert(fraseLlistaAfegida + '.');
		}
	}
}

function contactesTotsOn(){
	for (i=0; i<document.getElementById('formAfegeixLlistaPublica').elements.length; i++){
		if (document.getElementById('formAfegeixLlistaPublica').elements[i].type == "checkbox"){
			document.getElementById('formAfegeixLlistaPublica').elements[i].checked = 1;
		}
	}
}

function contactesTotsOff(){
	for (i=0; i<document.getElementById('formAfegeixLlistaPublica').elements.length; i++){
		if (document.getElementById('formAfegeixLlistaPublica').elements[i].type == "checkbox"){
			document.getElementById('formAfegeixLlistaPublica').elements[i].checked = 0;
		}
	}
}

//Mostra la llista de integrants
function mostraLlistaIntegrantsVotacio(capcalera){
	var pos_url = 'mostraLlistaIntegrantsVotacio.php';
	var req = XMLHttpRequestXooco();

	if (req) {
		req.onreadystatechange = function() {
			if (req.readyState == 4 && (req.status == 200 || req.status == 304)) {
				document.getElementById('LayerResultat').innerHTML = req.responseText;
			}
		}
		req.open('POST', pos_url +'?capcalera='+ capcalera ,true);
		req.send(null);
	}

}

//Chequejem que el nom de la llista no estigui ocupat
function valoraVot(idVot, tipusBoto, idLlista, passUsuari){
	var pos_url = 'valorarVots.php';
	var req = XMLHttpRequestXooco();

	if (req) {
		req.onreadystatechange = function() {
			if (req.readyState == 4 && (req.status == 200 || req.status == 304)) {
				document.getElementById('LayerResultat').innerHTML = req.responseText;
				resultatTexte = req.responseText;
			}
		}
		req.open('POST', pos_url +'?idVot='+ idVot +'&tipusBoto='+ tipusBoto +'&idLlista='+ idLlista+'&passUsuari='+ passUsuari, true);
		req.send(null);
	}
}