var vengoDeBloque=false;
var resumen=false;
var aUnEnBl=false;
var aUnEnCl=false;// se asigna un curso en un ciclo
var validarCodi=true;
var bloqueVacio=false;
function ComprobacionRellenoActividadesEnCicloSubmit()
{         
	var result= ComprovacionRellenoActividadesEnCiclo('submit');
	//alert("resumen" +resumen);
	//alert("aUnEnCl "+aUnEnCl);
         if(aUnEnCl && resumen ) this.form.submit();
}
function ComprovacionRellenoActividadesEnCiclo(obvsString) 
{         
	  //  aUnEnCl 
	     
	     aUnEnCl=false;
	   if(obvsString.indexOf("submit")!=-1)	
	   {      //alert("aqui se aprieta submit");   
              
           	
              	validaCodiCentre();
           	    if (!validarCodi) 
               {
                  alert('El codi de centre es incorrecte');
                  var doc1;
                  doc1 = document.getElementById('VARCHAR_refCentre');
                  doc1.focus();
                  return false;
                }
                resumen=true;
           	//alert(bloques.length);
           	var doc2;
           	var b=false;// para un bloque si no se ha rellenado ninguna actividad, retorna false
           	var f=1;
           	var b1=true
           	//alert(x<bloques.length);
           	 while (f<bloques.length) 
                {
                 vengoDeBloque=false;
                if (!(bloques[f][0]==-1)){
                //alert("vengo de ciclo");
                 b=ComprovacionRellenoActividadesEnBloque(f);}
                
                 
                f=f+1;  
                }// end while
              //  alert(aUnEnCl);
                // alert("ya se ha recorrido todas las actividades");   
     	       if(!aUnEnCl )
                {
	            alert("has de seleccionar com a mínim una activitat");
	            doc2 = document.getElementById('VARCHAR_obsv');
	            doc2.focus();
	            return false;
                }

	   }  //end if
	   //alert('todo correcto pal servidor');
	   return b;
}

//cuando vengo de bloque: dodnevengo=false
// esta funcion comprueva un bloque, si no esta rellenado retorna true
// si esta bien rellenado retorna true
// si esta mal rellenado retorna false

// cuando vengo de ciclo: dondevento= true
// esta funcion comprueva un bloque, si no esta rellenado comunica que no hay bloque rellenado
// si esta bien rellenado retorna true
// si esta mal rellenado retorna false
function ComprovacionRellenoActividadesEnBloque(m)
{         
              //alert(m); 
              //alert(actividades[1]); 
              //alert(parseInt(actividades[1])-parseInt(numB));
              
            aUnEnBl=false; 
            var aUnEnAct;// se asigna un curso en una actividad
            var x= 1;// cada x es una actividad
            //  para una actividad, cuando un curso está marcado: ya no miramos los siguientes
             var y=1;
           //  alert(m); 
           //alert(bloques[m].length);
           while ( x<bloques[m].length ) //recorrido por todas las actividades del bloque
           {
         // alert(x);
          // alert("DENTRO WHILE");
            aUnEnAct = false;
          //    alert(aUnEnAct + " es aUnEnAct y  bloques["+m+"]["+x+"] es " + bloques[m][x]);
             //alert('actividad '+x +' = '+bloques[m][x]);
              
           	 while (y<=6 && (!aUnEnAct  ))//recorrido por todas los cursos de actividad 
                 {   
                  //alert('curso '+y + ' de actividad: '+ "bloques["+m+"]["+x+"]");
                  //alert(document.getElementById(bloques[m][x]+'-Allistes'+y));  
                   //alert(document.getElementById(actividades[x]+'-Allistes'+y).checked);       
	           if( document.getElementById(bloques[m][x]+'-Allistes'+y)!=null && document.getElementById(bloques[m][x]+'-Allistes'+y).checked)
	           {
		           
		           aUnEnCl=true;    //vale true cuando ya has marcado un curso en un ciclo
		           aUnEnBl=true;  //vale true cuando ya has marcado un curso en un bloque
		           aUnEnAct=true;  //vale true cuando ya has marcado un curso en una actividad
		          
		        //alert(' el curso '+y + ' de actividad: '+bloques[m][x] + ' ha sido marcado');            
		           var doc2;
		           var regEx;
		           doc2 = document.getElementById(bloques[m][x]+'-VARCHAR_mestre');
				   if(doc2.value.length == 0)
				   {     resumen=false;
					alert("Has d\'introduir el nom del mestre/a");
					
					doc2.focus();
					return false;
		           }
		            regEx = /^([a-zA-ZñÑçÇ àèéíòóúüïáÀÁÈÉÍÒÓÚÜÏ]+)$$/;
				   if(!regEx.test(doc2.value))
				   {     resumen=false;
					 alert("El nom del mestre/a introduït no es vàlid, han de ser lletres");
					 
					 doc2.focus();
					 return false;
				   }
				   doc2 = document.getElementById(bloques[m][x]+'-INTEGER_grups');
				   if(doc2.value.length == 0)
				   {    resumen=false;
				   //alert(resumen);
					alert("Has d\'introduir el nombre de grups");
					
					doc2.focus();
					return false;
		           }
		           regEx = /^[0-9]{1,2}$/;
				   if(!regEx.test(doc2.value))
				   {     resumen=false;
					 alert("El nombre de grups introduït no es vàlid, han de ser digits");
					 
					 doc2.focus();
					 return false;
				   }
				   doc2 = document.getElementById(bloques[m][x]+'-INTEGER_alumnes');
				   if(doc2.value.length == 0)
				   {     resumen=false;
					alert("Has d\'introduir el nombre d'alumnes");
					
					doc2.focus();
					return false;
		           }
		           regEx = /^[0-9]{1,3}$/;
				   if(!regEx.test(doc2.value))
				   {       resumen=false;
					 alert("El nombre d'alumnes introduït no es vàlid, han de ser digits");
					
					 doc2.focus();
					 return false;
				   }
                }  //end if
                 var doc3;
               // alert('curso: '+y + ', almenosUnEnActiv: '+aUnEnAct);  
              if(y==6 && !aUnEnAct){
                //alert(' ningun  curso entre 0 y '+y + ' de actividad '+x +' : '+actividades[x] + ' ha sido marcado'); 
				   if(document.getElementById(bloques[m][x]+'-VARCHAR_mestre').value.length != 0
				   || document.getElementById(bloques[m][x]+'-INTEGER_grups').value.length != 0
				   || document.getElementById(bloques[m][x]+'-INTEGER_alumnes').value.length != 0
				   || document.getElementById(bloques[m][x]+'-VARCHAR_obs').value.length != 0)
				   {
				   // alert('curso '+y + ' de actividad: '+"bloques["+m+"]["+x+"]");
				//    alert(document.getElementById(bloques[m][x]+'-VARCHAR_obs').value.length);
					resumen=false;
					alert("Has de seleccionar com a mínim un curs");
					
					doc3=document.getElementById(bloques[m][x]+'-VARCHAR_mestre');
					doc3.focus();
					return false;
		           }
                }
                
                 // alert("DENTRO WHILE");
              // alert("Y:" + y);
             

                
                 y++;
              } //end while ))  //recorrido por todas los cursos de actividad 
                var doc1;
              var w=0;
              while (w<bloques_opcionales.length ){
                    if(bloques[m][x]==bloques_opcionales[w]){
                    var numSelect=0; 
                           for (var o=1;o<=6;o++)
                           {  
                                if( document.getElementById(bloques[m][x]+'-Allistes'+o)!=null && document.getElementById(bloques[m][x]+'-Allistes'+o).checked )
	                     	{
		                   numSelect++;    
	                        }//end if 
	                  } //end for
	                  if(numSelect>1){
	                  resumen=false;
	                    alert('A l´activitat ' +bloques[m][x]+ ' només es pot seleccionar un curs');
		                         doc1=document.getElementById(bloques[m][x]+'-VARCHAR_mestre'); 
		                         
		                         doc1.focus();  
			                 return false;

	                  }           
                    } // end if
                     w++;
              }// end while


             y=1;
                      
          x++; 
             
             // alert("end while" +y );        
           
          //   alert("NEW X" +x );           
          }    // end while   
          /// aqui llega cuando no esta rellenado        
     if(aUnEnBl==true && vengoDeBloque==true) {
      // alert("rellenado bien  ");
      return true;}
      
     if(aUnEnBl==false && vengoDeBloque==true) {
     //alert("no hay almenos un curso de ese bloque seleccionado, y vengo de bloque: no pasa nada");
     return true;}
     
     if(aUnEnCl==true && aUnEnBl==false && vengoDeBloque==false){
     // alert("hay almenos un curso de ese ciclo seleccionado, vengo de ciclo: bienpero ha de funcionar correcion campos ");
       return true;}

     
      else if(aUnEnBl==false &&  aUnEnCl==false && vengoDeBloque==false) {
     //alert("no hay almenos un curso de ese ciclo seleccionado, vengo de ciclo: mal se ha re rellnar");
     return false;}
       
       else if(aUnEnBl==true &&  aUnEnCl==true && vengoDeBloque==false) {
     //alert("hay almenos un curso de ese ciclo y bloque seleccionado, vengo de ciclo: bien pero ha de funcionar correcion campos");
     return true;}
     
     else if(aUnEnBl==false &&  aUnEnCl==true && vengoDeBloque==false) {
     ///alert("caso imposible");
     return true;}
     
          //if(dondeVengo==false) return  true               
    
}


 function validaCodiCentre()
	{
	// alert("validar codi centre");
            validarCodi=false;
            var doc;
            doc = document.getElementById('INTEGER_centre');
       
            var doc1;
            doc1 = document.getElementById('VARCHAR_refCentre');
                
            if (doc.value.length != 0 && doc1.value.length != 0 ){
             
                 if (   doc1.value == "08028795" && parseInt(doc.value) == 1
                     || doc1.value == "08035623" && parseInt(doc.value) == 2                   
                     || doc1.value == "08039896" && parseInt(doc.value) == 3
                     || doc1.value == "08028904" && parseInt(doc.value) == 4
                     || doc1.value == "08062626" && parseInt(doc.value) == 5
                     || doc1.value == "08028813" && parseInt(doc.value) == 6
                     || doc1.value == "08028758" && parseInt(doc.value) == 7
                     || doc1.value == "08036411" && parseInt(doc.value) == 8
                     || doc1.value == "08028746" && parseInt(doc.value) == 9
                     || doc1.value == "08028783" && parseInt(doc.value) == 10    
                     || doc1.value == "08053650" && parseInt(doc.value) == 11
                     || doc1.value == "08038351" && parseInt(doc.value) == 12
                     || doc1.value == "08056584" && parseInt(doc.value) == 13    
                     || doc1.value == "08037292" && parseInt(doc.value) == 14
                     || doc1.value == "08036317" && parseInt(doc.value) == 15
                     || doc1.value == "08069323" && parseInt(doc.value) == 16
                     || doc1.value == "08059949" && parseInt(doc.value) == 17                      
                     || doc1.value == "08063278" && parseInt(doc.value) == 18
                     || doc1.value == "08062663" && parseInt(doc.value) == 19
                     || doc1.value == "08069712" && parseInt(doc.value) == 20    
                     || doc1.value == "08028849" && parseInt(doc.value) == 21
                     || doc1.value == "08045549" && parseInt(doc.value) == 22
                     || doc1.value == "08031757" && parseInt(doc.value) == 23    
                     || doc1.value == "08053157" && parseInt(doc.value) == 24
                     || doc1.value == "08044715" && parseInt(doc.value) == 25
                     || doc1.value == "08043504" && parseInt(doc.value) == 26
                     || doc1.value == "23000000" && parseInt(doc.value) == 35
                     || doc1.value == "88888888" && parseInt(doc.value) == 35   
                     || doc1.value == "08058726" && parseInt(doc.value) == 28
                     || doc1.value == "08059007" && parseInt(doc.value) == 29
                     || doc1.value == "08057394" && parseInt(doc.value) == 30    
                     || doc1.value == "08052037" && parseInt(doc.value) == 31
                     || doc1.value == "08028886" && parseInt(doc.value) == 32
                     || doc1.value == "24000000" && parseInt(doc.value) == 35    
                     || doc1.value == "08028850" && parseInt(doc.value) == 34                             
                    ){
                     //alert('El codi de centre es correcte');
                     validarCodi = true;
                }
                 else {
                     //alert('El codi de centre es incorrecte');
		     
                }

            }
            return validarCodi;
        }