/**
 * @author filldelanit
 */

/******************************************************************
 * Mostrar i ocultar una capa donat l'identificador de l'element
 ******************************************************************/
function mostrar(nomCapa)
{
	document.getElementById(nomCapa).style.display="block";
}
function ocultar(nomCapa)
{
	document.getElementById(nomCapa).style.display="none";
}

/******************************************************************
 * Afegir i eliminar elements d'un formulari donat:
 * obj: element abans el qual s'inserirà el nou div
 * id: identificador del contenidor on es vol afegir el div
 * text_treure: text del botó eliminar
 ******************************************************************/
var num=0;//<---- variable global... lleeeeeig, s'ha d'arreglar

function afegir_input(obj,id,text_treure) 
{
/*
The NAME attribute cannot be set at run time on elements dynamically 
created with the createElement method. To create an element with a name 
attribute, include the attribute and value when using the createElement method.
*/
	num++;
	var id_subcontenidor = 'div'+num;
	var subcontenidor = document.createElement('div');
	subcontenidor.id = id_subcontenidor;
	
	if(navigator.appName=="Microsoft Internet Explorer")
	{
		var str0 = "obres_detall";
		var str = "<input name=" + str0 + " />";
		var element = document.createElement(str);
	}
	else
	{
		var element = document.createElement('input');
		element.setAttribute('name','obres_detall');
	}
	
	element.setAttribute('type','input');
	subcontenidor.appendChild(element);
	
	//Amb la seqüència \u posem el valor en unicode
	element = document.createTextNode('\u00a0\u00a0');
	subcontenidor.appendChild(element);		
	element = document.createElement('input');
	element.setAttribute('type','button');
	element.setAttribute('value',text_treure);
	element.setAttribute('id','div'+num);
	element.setAttribute('name','div'+num);
	element.onclick = function () {eliminar_input(this.id,id)};
	subcontenidor.appendChild(element);
	var contenidor = document.getElementById(id);
	contenidor.insertBefore(subcontenidor,obj);
	/*
	 * Si es vol afegir al final del contenidor, fer servir:
	 * 
	 * contenidor.appendChild(contenidor);
	 */
}

function eliminar_input(id_subcontenidor, id_contenidor) 
{
  var contenidor = document.getElementById(id_contenidor);
  contenidor.removeChild(document.getElementById(id_subcontenidor));
}

/******************************************************************
 * Mostrar els tabs d'artistes (tants com s'han sel·leccionat)
 ******************************************************************/

function mostrar_artistes()
{ 
	if (document.getElementById('obra_individual').checked || document.getElementById('projecteIndividual').checked)
	{
		var cad_artista = 'li_artista1';
		document.getElementById(cad_artista).style.display="block";
	}
	else
	{
		if(document.getElementById('obra_colectiva').checked){
			var i_num_participants = document.getElementById('num_participants').value;
		}else if(document.getElementById('projecteGrup').checked){
			var i_num_participants = document.getElementById('num_participantsP').value;			
		}
		if (i_num_participants == 'mes') 
		{
			i_num_participants = 1;
			document.getElementById('notificacio_mes_deu').style.display = "block";
		}
		var i_contador = 1;
		for (i_contador; i_contador <= i_num_participants; i_contador++) {
			cad_artista = 'li_artista' + i_contador;
			document.getElementById(cad_artista).style.display = "block";
		}
	}
}

/******************************************************************
 * Ocultar els tabs d'artistes
 ******************************************************************/

function ocultar_artistes()
{
	var i_num_participants = 10;
	var i_contador = 1;
	for (i_contador; i_contador <= i_num_participants; i_contador++) 
	{
		var cad_artista = 'li_artista'+ i_contador;
		document.getElementById(cad_artista).style.display="none";
	}
	document.getElementById('notificacio_mes_deu').style.display = "none";
}

/******************************************************************
 * Recollim tota la informació del formulari
 ******************************************************************/

function recolectar_obra()
{
	var str_post;
	
	var titol = document.getElementById('titol').value;
	str_post = "titol=" + encodeURI(titol);
	
	var tecnica = document.getElementById('tecnica').value;
	if (tecnica == 'altres')
	{
		tecnica = document.getElementById('especfTecnica').value;
	}
	str_post+= "&tecnica=" + encodeURI(tecnica);
	
	var crtl = document.getElementsByName('disciplina');
    for (i = 0; i < crtl.length; i++)
	{
		var radio = crtl[i];
		if (radio.checked)
		{
			var tipus_obra = radio.value;
			
		}
	}
	str_post+= "&disciplina=" + encodeURI(tipus_obra);
	var disciplina = document.getElementsByName('disciplina');
	if (disciplina[4].checked) 
	{
		var obres_detall = document.getElementsByName('obres_detall');
		str_post+= "&titol_obres=";
		for (i=0;i<obres_detall.length;i++)
		{
			var detall_obra = obres_detall[i].value;
			str_post+= encodeURI(detall_obra) + '---';
		}
		var material = document.getElementById('material_tipus2').value;
		str_post+= "&material=" + encodeURI(material);
		var observacions = document.getElementById('observacions_tipus2').value;
		str_post+= "&observacions=" + encodeURI(observacions);
		var durada = 0;;
		str_post+= "&durada=" + encodeURI(durada);
	}
	else
	{
		var espai = document.getElementById('espai').value;
		str_post+= "&espai=" + encodeURI(espai);
		var durada = document.getElementById('durada').value;
		str_post+= "&durada=" + encodeURI(durada);
		var material = document.getElementById('material_tipus1').value;
		str_post+= "&material=" + encodeURI(material);
		var observacions = document.getElementById('comentaris_tipus1').value;
		str_post+= "&observacions=" + encodeURI(observacions);
	}
	
	return str_post;
}

function recolectar_artistes()
{	
	var str_post =""; 
	var dni = document.getElementsByName('sDni');
	var nom = document.getElementsByName('sNom');
	var nom_artistic = document.getElementsByName('sNomArt');
	var cognom1 = document.getElementsByName('sCog1');
	var cognom2 = document.getElementsByName('sCog2');
	var adreca = document.getElementsByName('sAdreca');
	var poblacio = document.getElementsByName('sPoblacio');
	var cp = document.getElementsByName('iCp');
	var data_naix = document.getElementsByName('data');
	var email = document.getElementsByName('email');
	var telefon = document.getElementsByName('tel');
	var web = document.getElementsByName('web');
	
	if (document.getElementById('obra_individual').checked || document.getElementById('projecteIndividual').checked)
	{
		i_num_participants = 1;
	}
	else
	{
		if (document.getElementById('obra_colectiva').checked){	i_num_participants = document.getElementById('num_participants').value;}
		else if (document.getElementById('projecteGrup').checked){ i_num_participants = document.getElementById('num_participantsP').value;}
		if (i_num_participants=='mes')
		{
			i_num_participants=1;
		}	
	}
		
    for (i = 0; i < i_num_participants; i++)
	{
		str_post+= "&dni_"+i+"=" + encodeURI(dni[i].value);
		str_post+= "&nom_"+i+"=" + encodeURI(nom[i].value);
		str_post+= "&nom_artistic_"+i+"=" + encodeURI(nom_artistic[i].value);
		str_post+= "&cognom1_"+i+"=" + encodeURI(cognom1[i].value);
		str_post+= "&cognom2_"+i+"=" + encodeURI(cognom2[i].value);
		str_post+= "&adreca_"+i+"=" + encodeURI(adreca[i].value);
		str_post+= "&poblacio_"+i+"=" + encodeURI(poblacio[i].value);
		str_post+= "&cp_"+i+"=" + encodeURI(cp[i].value);
		str_post+= "&data_naix_"+i+"=" + encodeURI(data_naix[i].value);
		str_post+= "&email_"+i+"=" + encodeURI(email[i].value);
		str_post+= "&telefon_"+i+"=" + encodeURI(telefon[i].value);
		str_post+= "&web"+i+"=" + encodeURI(web[i].value);
	}
	
	return str_post;
}

/******************************************************************
 * Donem d'alta les dades del formulari
 ******************************************************************/
function alta_dades()
{
	//Donem d'alta l'obra
	var num_participants = 1;
	var dades_obra = recolectar_obra();
	var dades_artistes = recolectar_artistes();
	if (!document.getElementById('obra_individual').checked)
	{
		num_participants = document.getElementById('num_participants').value;	
	}
	alta('alta.php',dades_obra+"&num_participants="+num_participants+"&"+dades_artistes);
}

function alta_dades_projecte()
{
	//Donem d'alta l'obra
	var num_participants = 1;
	var proj = document.getElementById('elProjecte').value;
	dades_projecte = "dades_projecte=" + encodeURI(proj);
	var dades_artistes = recolectar_artistes();
	if (!document.getElementById('projecteIndividual').checked)
	{
		num_participants = document.getElementById('num_participantsP').value;	
	}
	alta('altaProjecte.php',dades_projecte+"&num_participants="+num_participants+"&"+dades_artistes);
}

function alta(url, parametres)
{
	//alert("parametres:"+parametres);
	var http_request;
	try 
	{
		// Opera 8.0+, Firefox, Safari
		http_request = new XMLHttpRequest();
	} 
	catch (e) 
	{
		// Internet Explorer
		try 
		{
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) 
		{
			try 
			{
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e) 
			{
				//No s'ha pogut crear una instància de XMLHTTP
				alert("Error en el teu navegador!. Si us plau, notifica el problema a l'administrador.");
				return false;
			}
		}
	}
	//var contenidor = document.getElementById('confirmacio');
	//http_request.open('POST', url, false);
	http_request.open('POST', url, true);
	http_request.onreadystatechange = function(){
		if (http_request.readyState == 4) 
		{
			//alert("hola "+http_request.responseText);
			if (http_request.responseText=='1')
			{
				document.getElementById('ok').style.display='block';
				document.getElementById('nook').style.display='none';
				document.getElementById('esperant').style.display='none';
			}
			else
			{
				document.getElementById('nook').style.display='block';
				document.getElementById('ok').style.display='none';
				document.getElementById('esperant').style.display='none';
			}
		} 
		else 
		{
			document.getElementById('nook').style.display='none';
			document.getElementById('ok').style.display='none';
			document.getElementById('esperant').style.display='block'
		}
	}
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded;UTF-8");
	http_request.setRequestHeader("Content-length", parametres.length);
	http_request.setRequestHeader("Connection", "close");
	http_request.send(parametres);
}

function escriure_confirmacio($str_confirmacio)
{
	document.getElementById('confirmacio').innerHTML = "$str_confirmacio";
}
   
