/*
nom1
cognom11
cognom21
adreca1
cp1
poblacio1
telefon1
email1
edat1
nom2
cognom12
cognom22
adreca2
cp2
poblacio2
telefon2
email2
edat2
entitat
altresentitats

		  
		document.getElementById("nom1")
		document.getElementById("cognom11")
		document.getElementById("cognom21")
		document.getElementById("adreca1")
		document.getElementById("cp1")
		document.getElementById("poblacio1")
		document.getElementById("telefon1")
		document.getElementById("email1")
		document.getElementById("edat1")
		document.getElementById("nom2")
		document.getElementById("cognom12")
		document.getElementById("cognom22")
		document.getElementById("adreca2")
		document.getElementById("cp2")
		document.getElementById("poblacio2")
		document.getElementById("telefon2")
		document.getElementById("email2")
		document.getElementById("edat2")
		document.getElementById("entitat").selectedIndex
		document.getElementById("altresentitats")
*/


var error;
var guardat;
var matriuok=new Array(20);
var parametres;

for (i=0;i<matriuok.length;i++){
	matriuok[i]=false;
}

/*Valida formulari inscripció*/

function valida(obj){
	error=0;
	guardat=0;
	if (document.getElementById('numinsc').value!=""){
		guardat=1;
		for (i=0;i<matriuok.length;i++){
			matriuok[i]=true;
		}
	}
	if (obj.name!="entitat"){
		if (obj.value.length==0){
			error=1;
		}else{
			switch (obj.id){
				case "nom1":  //0
				   matriuok[0]=true;
				break;
				case "cognom11":  //1
				   matriuok[1]=true;
				break;
				case "cognom21":   //2
				   matriuok[2]=true;
				break;
				case "adreca1":  //3
				   matriuok[3]=true;
				break;
				case "cp1":  //4
				   matriuok[4]=true;
				break;
				case "poblacio1":  //5
					matriuok[5]=true;
				break;
				case "telefon1":  //6
				   matriuok[6]=true;
				break;
				case "email1":  //7
				   matriuok[7]=true;
				break;
				case "edat1":  //8
					if(!(parseInt(obj.value,10)>=6 && parseInt(obj.value,10)<=14)){
						error=2;
					}else{
						matriuok[8]=true;
					}
				break;
				case "nom2":  //9
				   matriuok[9]=true;
				break;
				case "cognom12":  //10
				   matriuok[10]=true;
				break;
				case "cognom22":  //11
				   matriuok[11]=true;
				break;
				case "adreca2":  //12
				   matriuok[12]=true;
				break;
				case "cp2":  //13
				   matriuok[13]=true;
				break;
				case "poblacio2":  //14
				   matriuok[14]=true;
				break;
				case "telefon2":  //15
				   matriuok[15]=true;
				break;
				case "email2": //16
				   matriuok[16]=true;
				break;
				case "edat2":  //17
					if (parseInt(document.getElementById("edat1").value,10)==6 && parseInt(obj.value,10)<18){
				  	    error=4;
						break;
				  	}
					if(!(parseInt(obj.value,10)>=6 && parseInt(obj.value,10)<=14)){
						document.getElementById("selent").style.display="none";
						document.getElementById("noment").innerHTML="Parella mixta";
						document.getElementById("identitat").value=25;
						document.getElementById("noment").style.display="block";
					    matriuok[18]=true;
					    matriuok[19]=true;
					}else{
						document.getElementById("selent").style.display="block";
						document.getElementById("noment").style.display="none";
					    matriuok[18]=false;
					    matriuok[19]=false;
					}
					matriuok[17]=true;
				break;
				case "altresentitats": //19
				   if (obj.value!="Nom de l'entitat/escola"){
					    matriuok[18]=true;
					    matriuok[19]=true;
				   }else{
				 		error=3;
				 		document.getElementById("entitat").selectedIndex=0;
				   }
				break;
			}
		}
	}else{  //18
		document.getElementById("selent").style.display="none";
		if (obj.options[obj.selectedIndex].value==100){
			document.getElementById("nomaltresent").style.display="block";
			document.getElementById("altresentitats").focus();
			matriuok[18]=false;
			matriuok[19]=false;
		}else{
			document.getElementById("noment").style.display="block";
			document.getElementById("noment").innerHTML=obj.options[obj.selectedIndex].text
			        + "<br />(<a href='#' class='textpetit' onclick='canvient()'>Feu clic si no est\u00E0 b\u00E9</a>)";
			matriuok[18]=true;
			matriuok[19]=true;
		}
		document.getElementById("identitat").value=obj.options[obj.selectedIndex].value;
		}
	if(document.getElementById("gestio").value!=1){
		for (i=0;i<matriuok.length;i++){
			if (matriuok[i]==false && error==0){error=1;}
		}
	}else{
		if (error==1){error=0;}
	}
	if (error==0){
		document.getElementById("prevbotons").style.display="none";
		if (guardat==1){
			document.getElementById("botons2").style.display="block";
		}else{
			document.getElementById("botons").style.display="block";
		}
	}else{
		document.getElementById("prevbotons").style.display="block";
		document.getElementById("botons").style.display="none";
		document.getElementById("botons2").style.display="none";
		switch (error){
			case 2:
			   alert ("L'edat del primer integrant ha de ser d'entre 6 i 14 anys");
			   document.getElementById(obj.name).value="";
			   document.getElementById(obj.name).focus();
			break;
			case 3:
				document.getElementById("selent").style.display="block";
				document.getElementById("nomaltresent").style.display="none";
			    alert("Seleccioneu l'entitat/escola\nTamb\u00E9 podeu participar com INDEPENDENTS.");
			break;
			case 4:
			   	document.getElementById(obj.name).value="";
			   	document.getElementById(obj.name).focus();
			    alert("Els nois i noies de 6 anys tindran per parella sempre un major d'edat (parella mixta).");
			break;
		}
	}
}

/*Cancia l'entitat unco escollida*/

function canvient(){
	matriuok[18]=false;
	matriuok[19]=false;
	document.getElementById("selent").style.display="block";
	document.getElementById("noment").style.display="none";
	document.getElementById("nomaltresent").style.display="none";
	document.getElementById("prevbotons").style.display="block";
	document.getElementById("botons").style.display="none";
	document.getElementById("nomaltresent").value="";
}

/*Inscriu, corregeix o esborra un registre de parelles*/

function inscriu(tipus){
	if (parseInt(document.getElementById('edat2').value,10)>=18){
		categoria=1;
	}else{
		categoria=0;
	}
	if (tipus=="DELETE"){
		txt="Elimino la inscripci\u00F3?"
	}else{
		txt="Guardo la inscripci\u00F3.\nRecordeu imprimir-vos el comprovant per la caixa."
	}
	if(document.getElementById("gestio").value==1){
		comfirmat=true;
	}else{
		comfirmat=confirm(txt);
	}
	
	if (comfirmat){
		if (document.getElementById("identitat").value!=""){
		document.getElementById("botons2").style.display="block";
		document.getElementById("prevbotons").style.display="none";
		document.getElementById("botons").style.display="none";
		parametres="tipus="+tipus+"&" +
		           "taula=parelles&";
     	if (document.getElementById("gestio").value!=1){
			parametres+="numcamps=23&";
     	}else{
     		if (document.getElementById("vdorsal").value==""){
				parametres+="numcamps=28&";
     		}else{
	 			parametres+="numcamps=30&";
     		}
     	}
		parametres+="nomcamp0=nom1&valcamp0=" + document.getElementById('nom1').value + "&tpcamp0=text&" +
					"nomcamp1=cognom11&valcamp1=" + document.getElementById('cognom11').value + "&tpcamp1=text&" +
					"nomcamp2=cognom21&valcamp2=" + document.getElementById('cognom21').value + "&tpcamp2=text&" +
					"nomcamp3=adreca1&valcamp3=" + document.getElementById('adreca1').value + "&tpcamp3=text&" +
					"nomcamp4=cp1&valcamp4=" + document.getElementById('cp1').value + "&tpcamp4=text&" +
					"nomcamp5=poblacio1&valcamp5=" + document.getElementById('poblacio1').value + "&tpcamp5=text&" +
					"nomcamp6=telefon1&valcamp6=" + document.getElementById('telefon1').value + "&tpcamp6=text&" +
					"nomcamp7=email1&valcamp7=" + document.getElementById('email1').value + "&tpcamp7=text&" +
					"nomcamp8=edat1&valcamp8=" + document.getElementById('edat1').value + "&tpcamp8=int&" +
					"nomcamp9=nom2&valcamp9=" + document.getElementById('nom2').value + "&tpcamp9=text&" +
					"nomcamp10=cognom12&valcamp10=" + document.getElementById('cognom12').value + "&tpcamp10=text&" +
					"nomcamp11=cognom22&valcamp11=" + document.getElementById('cognom22').value + "&tpcamp11=text&" +
					"nomcamp12=adreca2&valcamp12=" + document.getElementById('adreca2').value + "&tpcamp12=text&" +
					"nomcamp13=cp2&valcamp13=" + document.getElementById('cp2').value + "&tpcamp13=text&" +
					"nomcamp14=poblacio2&valcamp14=" + document.getElementById('poblacio2').value + "&tpcamp14=text&" +
					"nomcamp15=telefon2&valcamp15=" + document.getElementById('telefon2').value + "&tpcamp15=text&" +
					"nomcamp16=email2&valcamp16=" + document.getElementById('email2').value + "&tpcamp16=text&" +
					"nomcamp17=edat2&valcamp17=" + document.getElementById('edat2').value + "&tpcamp17=int&" +
					"nomcamp18=identitat&valcamp18=" + document.getElementById("identitat").value + "&tpcamp18=int&" +
					"nomcamp19=altresentitats&valcamp19=" + document.getElementById('altresentitats').value + "&tpcamp19=text&" +
					"nomcamp20=idmarxa&valcamp20=" + idmarxa + "&tpcamp20=int&" +
					"nomcamp21=data&valcamp21=" + dataSQL(Date()) + "&tpcamp21=text&" +
					"nomcamp22=Categoria&valcamp22=" + categoria + "&tpcamp22=int";
     	if (document.getElementById("gestio").value==1){
     		if(document.getElementById("ganyota1").checked){
	     		vganyota1=1;
     		}else{
	     		vganyota1=0;
     		}
     		if(document.getElementById("ganyota2").checked){
	     		vganyota2=1;
     		}else{
	     		vganyota2=0;
     		}
     		switch (document.getElementById("cobrat").selectedIndex){
     			case 0:
     			    var imp=0;
     			break;
     			case 1:
     			    var imp=parseInt(preu,10);
     			break;
     			case 2:
     			    var imp=parseInt(preusortida,10);
     			break;
     			case 3:
     			    var imp=parseInt(preu,10);
     			break;
     			case 4:
     			    var imp=0;
     			break;
     		}
     		
		    var fpag=document.getElementById("cobrat")[document.getElementById("cobrat").selectedIndex].value;
		    var vsituacio=document.getElementById("estat")[document.getElementById("estat").selectedIndex].value;

		    if (parseInt(vganyota1,10)+parseInt(vganyota2,10)==2){
		    	imp=0;
		    }else if(parseInt(vganyota1,10)+parseInt(vganyota2,10)==1){
		    	imp-=parseInt(preu,10)/2;
		    }
     		if (document.getElementById("vdorsal").value==""){
	     		parametres+="&nomcamp23=Ganyota1&valcamp23=" + vganyota1 + "&tpcamp23=int"
     		           +"&nomcamp24=Ganyota2&valcamp24=" + vganyota2 + "&tpcamp24=int"
     		           +"&nomcamp25=import&valcamp25=" + imp + "&tpcamp25=int"
     		           +"&nomcamp26=FormaPagament&valcamp26=" + fpag + "&tpcamp26=int"
     		           +"&nomcamp27=estat&valcamp27=" + vsituacio + "&tpcamp27=int";
     		}else{
	     		parametres+="&nomcamp23=Ganyota1&valcamp23=" + vganyota1 + "&tpcamp23=int"
     		           +"&nomcamp24=Ganyota2&valcamp24=" + vganyota2 + "&tpcamp24=int"
     		           +"&nomcamp25=import&valcamp25=" + imp + "&tpcamp25=int"
     		           +"&nomcamp26=FormaPagament&valcamp26=" + fpag + "&tpcamp26=int"
     		           +"&nomcamp27=Dorsal&valcamp27=" + document.getElementById("vdorsal").value + "&tpcamp27=int"
     		           +"&nomcamp28=estat&valcamp28=" + vsituacio + "&tpcamp28=int"
     		           +"&nomcamp29=HoraRecDorsal&valcamp29=" + document.getElementById('HoraRecDorsal').value + "&tpcamp29=text";

     		}
     		          
     	}
		if (tipus=="UPDATE" || tipus=="DELETE"){
			parametres+="&nomId=idParella&valId="+ document.getElementById('numinsc').value;
		}
     	clientSideInclude('zvars', 'scripts/consultes.php?'+parametres);
        if (tipus=="INSERT"){
			guardat=1;
        	document.getElementById('numinsc').value=document.getElementById('nou_id').value;
        }
     	if (document.getElementById("gestio").value==1){
			clientSideInclude('submenu', 'html/sbmgestio.php?menu=inscrits');
			//clientSideInclude('cos', 'html/inscripciocorrec.php?gestio=1&numins='+document.getElementById('numinsc').value);
			clientSideInclude('cos', 'html/inscripciocorrec.php?gestio=1');
			document.getElementById('nom1').focus();
		}else{
			imprimir();
		}
	}else{
		alert("S'ha de triar una entitat!!!");
	}
	}
}

/*Buida el formulari*/
function buida(){
	if(confirm("Netejo els formulari sense fer la inscripci\u00F3?")){
    	buidaform();
	}
}

/*Buida el formulari i restaura zona botons*/
function buidaform(){
	for (i=0;i<document.forms.length;i++){
		document.forms[i].reset();
	}
	document.getElementById("prevbotons").style.display="block";
	document.getElementById("botons2").style.display="none";
	document.getElementById("botons").style.display="none";
	document.getElementById("selent").style.display="block";
	document.getElementById("noment").style.display="none";
	document.getElementById("nomaltresent").style.display="none";
	for (i=0;i<matriuok.length;i++){
		matriuok[i]=false;
	}
	error=0;
    guardat=0;
    parametres="";
    document.getElementById('numinsc').value="";
}
	

/*demana a la funcio inscriu que corregeixi un registre*/
function corregir(){
	inscriu("UPDATE");
	//if (document.getElementById("gestio").value==1){
	//	clientSideInclude('submenu', 'html/sbmgestio.php?menu=inscrits');
	//}
}

/*demana a la funcio inscriu que elimini un registre*/
function eliminar(){	
	inscriu("DELETE");
	buidaform();
	if (document.getElementById("gestio").value==1){
		clientSideInclude('submenu', 'html/sbmgestio.php?menu=inscrits');
	}
}


/* imprimeix la inscripció*/
function imprimir(){
	var adr="html/inscripcio_rebut.php?"
	           + parametres
	           + "&numinsc="+document.getElementById('numinsc').value
	           + "&edicio="+edicio
	           + "&datamarxa="+datamarxa;
    window.open( adr ,'Inscripcio','width=600,height=400,scrollbars=1,status=1');
    
}


/*marca com ganyota la forma de pagament*/
function pagaGanyota(){
	if (document.getElementById("ganyota1").checked
	  && document.getElementById("ganyota2").checked){
		document.getElementById("cobrat").selectedIndex=4;
	}else{
		document.getElementById("cobrat").selectedIndex=0;
	}
}
