var comptador=0;
var error=0;
var mtecles=new Array(48,49,50,51,52,53,54,55,56,57,96,97,98,99,100,101,102,103,104,105);
var	confirma=true;

function mascara(e,control){
var trobat=false;
(e.keyCode) ? k=e.keyCode : k=e.which;
for (i=0;i<=mtecles.length;i++){
	if(k==mtecles[i]) trobat=true;
}
//if (document.getElementById("botrepassar").value!=1){
	if (trobat==false){
		if(document.getElementById("r"+control).value.length>comptador){
			document.getElementById("r"+control).value=document.getElementById("r"+control).value.substr(0,document.getElementById("r"+control).value.length-1);
		}
	}else{
		comptador=document.getElementById("r"+control).value.length;
	}
/*}else{
	if (control>0 && k==40){
	 	ctrl=control-1;
	 	document.getElementById("r"+ctrl).focus();
	}else{
		document.getElementById("r"+control).value=document.getElementById("r"+control).value.substr(0,document.getElementById("r"+control).value.length-1);
	}
}*/
}

function fcontrol(control){
var ctrlantH="";
var ctrlantM="";
var ctrlH=0;
var ctrlant=0;
error=0;
if(document.getElementById("r"+control).value.length!=0){
	if (control==0){
		if(InStr(document.getElementById("r"+control).value, ".")!=-1){
			ctrlantH=Left(document.getElementById("r"+control).value,
		                    InStr(document.getElementById("r"+control).value,"."));
			ctrlantM=Mid(document.getElementById("r"+control).value,
		                    InStr(document.getElementById("r"+control).value,".")+1);
		}else{
			error=1;
		}
		if(parseInt(ctrlantH,10)<0 	
		||parseInt(ctrlantH,10)>12
		||isNaN(parseInt(ctrlantH,10))
		||parseInt(ctrlantM,10)<0 	
		||parseInt(ctrlantM,10)>=60
		||isNaN(parseInt(ctrlantM,10))){

			error=1;

		}else{
			document.getElementById("r"+control).value=formatHora(ctrlantH,ctrlantM);
		}
	}else{
		if (document.getElementById("r"+control).value.length>2
		  || parseInt(document.getElementById("r"+control).value,10)>=60
		  || parseInt(document.getElementById("r"+control).value,10)<0
		  || isNaN(parseInt(document.getElementById("r"+control).value),10)){

			error=2;
	
		}else{	
			ctrlant=control-1;
			ctrlantH=Left(document.getElementById("r"+ctrlant).value,
	                    InStr(document.getElementById("r"+ctrlant).value,":"));
			ctrlantM=Mid(document.getElementById("r"+ctrlant).value,
	                    InStr(document.getElementById("r"+ctrlant).value,":")+1);
			if (parseInt(document.getElementById("r"+control).value,10)<parseInt(ctrlantM,10)){
				ctrlH=parseInt(ctrlantH,10)+1;
			    document.getElementById("p"+control).innerHTML=Math.abs(matriuTemps[ctrlant][0]-((60-parseInt(ctrlantM,10))+parseInt(document.getElementById("r"+control).value,10)));
			    document.getElementById("m"+control).innerHTML=((60-parseInt(ctrlantM,10))+parseInt(document.getElementById("r"+control).value,10));
				document.getElementById("r"+control).value=formatHora(ctrlH,document.getElementById("r"+control).value);
			}else{
			    document.getElementById("p"+control).innerHTML=Math.abs(matriuTemps[ctrlant][0]-(parseInt(document.getElementById("r"+control).value,10)-parseInt(ctrlantM,10)));
			    document.getElementById("m"+control).innerHTML=(parseInt(document.getElementById("r"+control).value,10)-parseInt(ctrlantM,10));
				document.getElementById("r"+control).value=formatHora(ctrlantH,document.getElementById("r"+control).value);
			}
			total(control);
			desempat1();
			desempat2();
		}
	}
	
	//Tractament d'errors
	if(error!=0){
		switch (error){
			case 1:
			  if (document.getElementById("r"+control).value!="No presentat"
			      && document.getElementById("r"+control).value!="Desqualificat"){
		    	alert(document.getElementById("r"+control).value
			    	 + " No sembla una hora.\nRecorda que el format a de ser HH.MM");
			  }
			break;
			case 2:
			  if (document.getElementById("r"+control).value!="No presentat"
			      && document.getElementById("r"+control).value!="Desqualificat"){
			     alert(document.getElementById("r"+control).value
				      + " No semblen minuts.\nRecorda que el format a de ser MM de 0 a 59");
			  }
			break;
			}
	    document.getElementById("r"+control).focus();
	}
}
}

/*function desempat1(){
	document.getElementById("d1").innerHTML=0;
	for (i=0;i<=matriuTemps.length;i++){
		if(parseInt(document.getElementById("p"+i).innerHTML)==0
		&& document.getElementById("p"+i).innerHTML!=""){
			document.getElementById("d1").innerHTML=parseInt(document.getElementById("d1").innerHTML,10)+1
		}
	}
}*/

function desempat1(){ 
document.getElementById("d1").innerHTML=""; 
document.getElementById("d3").innerHTML=""; 
var mpenalit=new Array(matriuTemps.length); 
var resultat=0; 
var punts=0; 
for (i=1;i<=matriuTemps.length;i++){
    mpenalit[i]=nz(parseInt(document.getElementById("p"+i).innerHTML,10)); 
} 
mpenalit.sort(function(a,b){return a - b}); 
for (i=0;i<matriuTemps.length;i++){
	var poten=matriuTemps.length-(i+1);
    resultat+=mpenalit[i]*Math.pow(2,poten);
    if (mpenalit[i]<10){
    	document.getElementById("d3").innerHTML+="0"+mpenalit[i];
    }else{
    	document.getElementById("d3").innerHTML+=mpenalit[i];
    }
    document.getElementById("d1").innerHTML=resultat; 
} 
} 





function desempat2(){
var txt=""; 
var txtnom=""; 
j=1;
for (i=0;i<matriuTemps.length;i++){
	if(isNaN(parseInt(document.getElementById("p"+j).innerHTML,10))){
		punts="--";
		nom="--";
	}else{
		punts=formatDosDigits(parseInt(document.getElementById("p"+j).innerHTML,10))
		nom=document.getElementById("c"+j).innerHTML;
	} 
matriudesempat[matriuTemps[i][2]]=punts; 
matriudesempattxt[matriuTemps[i][2]]=nom;
j++;
}
for(i=0;i<matriuTemps.length;i++){ 
            txt+=matriudesempat[i]; 
            txtnom+=matriudesempattxt[i]; 
}
document.getElementById("d4").innerHTML=txtnom; 
document.getElementById("d2").innerHTML=txt; 
}



function neteja(control){
	if (control==0 && document.getElementById("r"+control).value!=""){
		confirma=confirm("Aquesta parrella ja te entrats els resultats\n\n\nVols eliminar-los?\n\n");
	}else{
		confirma=true;
	}
	if (confirma){
		if(document.getElementById("r"+control).value!=""){
			for (i=control;i<=matriuTemps.length;i++){
		     	document.getElementById("r"+i).value="";
				comptador=document.getElementById("r"+control).value.length;
				document.getElementById("m"+i).innerHTML="&nbsp;";
				document.getElementById("p"+i).innerHTML="&nbsp;";
			 }
			 total(control-1);
		}else{
			comptador=document.getElementById("r"+control).value.length;
		}
		desempat1();
		desempat2();
	}
}


function total(control){
var j=1;
document.getElementById("tm").innerHTML=0;
document.getElementById("tp").innerHTML=0;
for (i=1;i<=control;i++){ 
				document.getElementById("tm").innerHTML=parseInt(document.getElementById("tm").innerHTML,10)
				                                       +parseInt(document.getElementById("m"+i).innerHTML,10);
				document.getElementById("tp").innerHTML=parseInt(document.getElementById("tp").innerHTML,10)
				                                       +parseInt(document.getElementById("p"+i).innerHTML,10);
	}
}



function fsalta(control){
var ctrl=control+1;
var t=document.getElementById("r"+ctrl);
var digits=0;
if(control==0){
	digits=5;
	if(document.getElementById("r"+control).value.length==2){
		document.getElementById("r"+control).value +=".";
		comptador=document.getElementById("r"+control).value.length;
	}
}else{
	digits=2;
}
if (digits-document.getElementById("r"+control).value.length<=0){
	fcontrol(control);
	if (!t) {
			if(error==0) document.getElementById("botguardar").focus();
	}else{
			if(error==0) t.focus();
	}
}
}
var seledicio="";
var categoria="I";
var selidmarxa="";

function valorsllistat(obj){
	if (obj.name=="categoria"){
		categoria=obj.options[obj.selectedIndex].value;
	}else{
		selidmarxa=obj.options[obj.selectedIndex].value;
		seledicio=obj.options[obj.selectedIndex].text;
	}
}

function llistat(tipus,ordre){
	if (tipus!="Dorsals"){
		seledicio=document.getElementById("edicio")[document.getElementById("edicio").selectedIndex].text;
		selidmarxa=document.getElementById("edicio")[document.getElementById("edicio").selectedIndex].value;
	}
	if(tipus=="Classificacio" || tipus=="Entitats" || tipus=="Temps"){
		var vurl='html/cosiframe.php?tipus='+tipus
     	                                    +'&seledicio='+seledicio
	                                        +'&selidmarxa='+selidmarxa
	                                        +'&categoria='+categoria;
	
	}else{
		var vurl='html/cosiframe.php?tipus='+tipus;
	}
    if (ordre!=undefined){
    	vurl+='&sordre='+ordre;
    }
	clientSideInclude('cos', vurl);
}

function portatemps(){
	if (typeof(matriuTemps)=="undefined"){
		portajavascript('html/temps.php');
	}
}
function omple(obj){
	if (obj.name=="dorsal"){
		crida('cos','coscontrols.php?numctrl='+matriuTemps.length+'&dorsal=',obj);
	}else{
		crida('cos','coscontrols.php?numctrl='+matriuTemps.length+'&idparella=',obj);
	}
		ompletemps();
		document.getElementById('r0').focus();
}

function ompletemps(){
		for(i=0;i<matriuTemps.length;i++){matriudesempat[i]='--';}
		for(i=0;i<matriuTemps.length;i++){matriudesempattxt[i]='--';}
		var j=1;
		for (i=0;i<matriuTemps.length;i++){
			document.getElementById('e'+j).innerHTML=matriuTemps[i][0];
			document.getElementById('c'+j).innerHTML=matriuTemps[i][3];
			j++;
			}
}

function ompleproper(obj){
	properdorsal=parseInt(obj.value,10)+1;
	clientSideInclude('cos', 'html/coscontrols.php?numctrl='+matriuTemps.length+'&dorsal='+properdorsal);
	ompletemps();
	document.getElementById('r0').focus();
}

function classifica(){
 tempstxt="";
 for (i=0;i<=matriuTemps.length;i++){
 	tempstxt+=document.getElementById('r'+i).value;
 	if (i<matriuTemps.length){
 		tempstxt+=";";
 	}
 }
 minutstxt=";";
 for (i=1;i<=matriuTemps.length;i++){
 	minutstxt+=document.getElementById('m'+i).innerHTML;
 	if (i<matriuTemps.length){
 		minutstxt+=";";
 	}
 }

 puntstxt=";";
 for (i=1;i<=matriuTemps.length;i++){
 	puntstxt+=document.getElementById('p'+i).innerHTML;
 	if (i<matriuTemps.length){
 		puntstxt+=";";
 	}
 }
parametres="tipus=UPDATE&" +
           "taula=parelles&" +
           "numcamps=8&" +
           "nomcamp0=tempstxt&valcamp0=" + tempstxt + "&tpcamp0=text&" +
   		   "nomcamp1=minutstxt&valcamp1=" + minutstxt + "&tpcamp1=text&" +
		   "nomcamp2=puntstxt&valcamp2=" + puntstxt + "&tpcamp2=text&" +
		   "nomcamp3=desempat1&valcamp3=" + document.getElementById('d1').innerHTML + "&tpcamp3=int&" +
		   "nomcamp4=desempat11&valcamp4=" + document.getElementById('d3').innerHTML + "&tpcamp4=text&" +
		   "nomcamp5=desempat2&valcamp5=" + document.getElementById('d2').innerHTML + "&tpcamp5=text&" +
		   "nomcamp6=minuts&valcamp6=" + document.getElementById('tm').innerHTML + "&tpcamp6=int&" +
		   "nomcamp7=punts&valcamp7=" + document.getElementById('tp').innerHTML + "&tpcamp7=int&" +
		   "nomId=idParella&valId="+ document.getElementById('idparellaidt').value;

	clientSideInclude('zvars', 'scripts/consultes.php?'+parametres);
	objdorsal=document.getElementById("dorsalint");
	ompleproper(objdorsal);
}


function fclasdefinitiva(obj){
	if (!obj.checked){
		if (confirm("Passos la classificació a definitiva?")){
		    clientSideInclude('zvars', 'scripts/clasdefinitiva.php');  	
		}else{
			alert("No he fet res");
			obj.checked=true;
		}
	}
}
