var clientPC = navigator.userAgent.toLowerCase();
var is_ie = ((clientPC.indexOf("msie") != -1) && (clientPC.indexOf("opera") == -1));

function estaPausa() {
	var res=false;
	if (typeof(ent_pausada)!='undefined' && ent_pausada)
		res=true;
	return res;
}

var inicioPausa;
function perteneceLista2(valor,lista) {
        var res=false;
        pos =(";"+lista+";").indexOf(";"+valor+";");
        if (pos!=-1) {
                res=true;
        }
        return res;
}

function pausar(cod_estudio) {
	var nodoPausa = window.parent.getElement("id_pausa1");
	if (estaPausa()) {
		var finPausa = new Date ();
		var diff = finPausa-inicioPausa;
		//Establezco como tiempo de pausas estos segundos.
		var nodo=window.parent.document.getElementById('intervalo_pausados');
		nodo.value=parseInt(nodo.value)+diff;

		ent_pausada=false;
		nodoPausa.innerHTML="<img src='img/pausa.png' alt='Pausar'>";
		window.location.href="do.php?op=continuar&cod_estudio="+cod_estudio+"&diff="+diff;
	} else {
		inicioPausa = new Date ();
		ent_pausada=true;
		nodoPausa.innerHTML="<img src='img/play.png' alt='Continuar'>";
	}
}

function procesarMultiple(obj,nomBase,idBase,sCodiExclu){
	var aTemp = new Array();
	var valorVble = document.getElementById("VARMUL_"+nomBase).value;

	// agafem valor original
	if (valorVble=="&" || valorVble=="-")
		valorVble="";	
	if (valorVble!="")
		aTemp = valorVble.split(';');
	// checkbox
	if (obj.type=='checkbox' || obj.type=='radio'){
		// si marque un radio borre la informació que puga haver
		if (obj.type=='radio')
			aTemp = new Array();
		// si marque un checkbox borre la informació dels radio que puga haver
		else {
			// si te codi exclu busquem per a vore si estan i els esborrem
			if (sCodiExclu!=''){
				var aBorrar = new Array();
				aBorrar = sCodiExclu.split(';');
				for (j=0;j<aBorrar.length;j++){
					// llegim el valor excluyente
					try {
						var valorExclu = document.getElementById(idBase+aBorrar[j]).value;
					} catch(error){
						break;
					}
					// mire si ja està en el vector				
					for (i=0;i<aTemp.length;i++){
						if (aTemp[i]==valorExclu){
							aTemp.splice(i,1);
							break;
						}
					}
				}
			}
		}
		 
		// llegim el valor seleccionat
		var valorItem = obj.value;
		// mire si ja està en el vector
		var pos=-1;
		for (i=0;i<aTemp.length;i++){
			if (aTemp[i]==valorItem){
				pos = i;
				break;
			}
		}
		// si ja està en el vector l'esborre
		if (pos!=-1)
			aTemp.splice(pos,1);
		// si no l'afegisc
		else
			aTemp.push(valorItem);
	}	
	// res
	else
		return;

	// establisc els elements seleccionats
	document.getElementById("VARMUL_"+nomBase).value=aTemp.join(';')
}

function procesarExclu(idBase,index,sCodiExclu){
	if (typeof(sCodiExclu)=="undefined" || sCodiExclu=="")
		return;						
		
	aCodExclu= sCodiExclu.split(';');
	numId = index;
	// si és excluyente
	var esExclu=false;
	var x;
	for (x in aCodExclu){
		if (aCodExclu[x]==numId){
			esExclu=true;
			break;
		}
	}
	// si és excluyente desmarque la resta de excluyentes
	if (esExclu){
		for (x in aCodExclu){
			if (aCodExclu[x]!=numId){								
				document.getElementById(idBase+aCodExclu[x]).checked=false;
			}
		}
	} else { // si no ho és, deshabilite els excluyentes
		// si estàn habilitats
		if (document.getElementById(idBase+aCodExclu[0]).disabled==false){
			// els deshabilite
			for (x in aCodExclu){	
				var item = document.getElementById(idBase+aCodExclu[x]);
				item.checked=false;
				item.disabled=true;
			}
		} else { // si no estàn habilitats
			// comprove si hi han items no excluyentes marcats		
			var marcats=false;			
			for (x=1;document.getElementById(idBase+x)!=null;x++){
				if (document.getElementById(idBase+x).checked){
					marcats=true;
					break;
				}
			}
			if (!marcats){
				// els habilite
				for (x in aCodExclu){	
					var item = document.getElementById(idBase+aCodExclu[x]);
					item.disabled=false;
				}
			}								
		}							
	}						
}

	function eliminaResultados(item, npatron) {
		str="";
		valor="";
		if (item.checked) {
			for (i=0;i<document.formulario.length;i++) {
				var nodo = document.formulario[i];
				var patron = npatron + "-";
				if (nodo.name!=null) {
					if (nodo.name.substr(0,patron.length)==patron) {
						//alert(nodo.name+"_"+nodo.checked+"_"+nodo.disabled);
						if (!nodo.disabled) {
							if (nodo!=item)
								nodo.checked=false;
						}
					}
				}
			}
		}
	}
	var esRadio=false;

	var form_submitted = false;

	function submit_form ( )
	{
	  if ( form_submitted )
	  {
		//alert ( "Your form has already been submitted. Please wait..." );
		return false;
	  }
	  else
	  {
		form_submitted = true;
		return true;
	  }
	}


	function enviarFormularioPagina() {
		if (comprobarVariable() && submit_form())
			document.formulario.submit();
	}

	function enviarFormularioPaginaAnterior() {
		document.formulario.prev.value="1";
		enviarFormularioPagina()
	}

	function enviarFormularioIndice(valor) {
		document.formulario.indice.value=valor;
		enviarFormularioPagina()
	}

	function submitMaxLength(maxlength,area){
		if(area.value.length>=maxlength)
			enviarFormularioPagina();
	}

	function focoA(item){
		var tmp=document.getElementsByName(item);
		tmp[0].focus();
	}

	function selectFocoA(select, valores, destinos){
		var a_valores = valores.split(',');
		var a_destinos = destinos.split(',');
		var salt = false;

		var countSel = select.options.length;
		var countVal = a_valores.length;
		for (i=countSel-1;i>=0;i--){
			if (select.options[i].selected){
				for (j=countVal-1;j>=0;j--){
					if (select.options[i].value==a_valores[j]){
						var tmp=document.getElementsByName(a_destinos[j]);
						tmp[0].focus();
						salt=true;
						break;
					}
				}
				break;
			}
		}
		return salt;
	}
	
	function procesarSemiAbierta(vble, codigo, vbleSemiAb){
		if (vbleSemiAb!=null){
			obj = document.getElementById(vbleSemiAb);
			// select
			if (obj!=null && obj.type.substr(0,6)=='select'){
				for (i=1;i<999;i++){
					if (obj.options[i].value==codigo){
						if (vble.value=='')
							obj.options[i].selected=false;
						else
							obj.options[i].selected=true;
						break;
					}
				}
			} else { // radio / checkbox
				var pulsa;
				for (i=1;i<999;i++){
					pulsa = document.getElementById('pulsa_'+i);
					if (pulsa==null)
						break;
					if (pulsa.value==codigo){
						if (vble.value==''){
							if (pulsa.checked==true){
								pulsa.checked=false;
								pulsa.onclick();
							}							
						}else{
							if (pulsa.checked==false){
								pulsa.checked=true;
								pulsa.onclick();
							}							
						}
						break;
					}
				}
			}
		}
	}
	
	var foco = '';

	function keypress() {
		if (is_ie) document.onkeydown = keypress_ie;
   		else document.onkeypress = keypress_ff;
	}


	function keypress_ff(e){
		if (document.getElementById('capa_alert').style.visibility == 'visible' || document.getElementById('capa_desactivar').style.visibility == 'visible')
			return;
		if (estaPausa()) {
			e.returnValue=false;
			return false;
		}
		var numero=-1;

		if (esRadio==null) esRadio=false;
		if (e.which==13 && (!e.shiftKey)) {
			enviarFormularioPagina();
			e.returnValue=false;
			return false;
		} else if (((e.keyCode==27 && e.shiftKey) || e.keyCode==27) && irAnterior) {
			enviarFormularioPaginaAnterior();
			e.returnValue=false;
			return false;
		}
		if (document.formulario.bloque.value!="1" && esRadio && (document.formulario.semiab.value!="1" || (document.formulario.semiab.value=="1" && (foco!="text" && foco!="textarea")))) {
			var msg = getElement("msg");
			//if (msg!=null) msg.innerHTML=event.keyCode+";"+event.altKey+";"+event.ctrlKey+";"+event.shiftKey;

			var shift = e.shiftKey;
			var ctrl = (!e.altKey && e.ctrlKey);
			var alt = (e.altKey && !e.ctrlKey);
			var altgr = (e.altKey && e.ctrlKey);

			// teclat estandar (alfanumèric) + numpad (normal,ctrl,alt,altgr)
			if (e.which!=0){
				var valor = e.which;
				if (valor>=48 && valor<=57) {
					numero = valor-48;	
					if (ctrl) // 20_29
						numero = 20+numero;
					else if (alt) // 30_39
						numero = 30+numero;
					else if (altgr) // 47_49
						numero = 40+numero;			
				} else if (shift){ // 10_19
					switch (valor){
						case 61: numero = 10; break;//0
						case 33: numero = 11; break; //1
						case 34: numero = 12; break; //2
						case 183: numero = 13; break; //3
						case 36: numero = 14; break; //4
						case 37: numero = 15; break; //5
						case 38: numero = 16; break; //6
						case 47: numero = 17; break; //7
						case 40: numero = 18; break; //8
						case 41: numero = 19; break; //9							
					}
				} else { // 41_46
					switch (valor){
						case 124: numero = 41; break;//1
						case 64: numero = 42; break; //2
						case 35: numero = 43; break; //3
						case 126: numero = 44; break; //4
						case 8364: numero = 45; break; //5
						case 172: numero = 46; break; //6
					}
				}
			} else if (e.keyCode!=0){ // numpad (shift)
				var valor = e.keyCode;
				switch (valor){
					case 45: numero = 10; break;//0
					case 35: numero = 11; break; //1
					case 40: numero = 12; break; //2
					case 34: numero = 13; break; //3
					case 37: numero = 14; break; //4
					case 12: numero = 15; break; //5
					case 39: numero = 16; break; //6
					case 36: numero = 17; break; //7
					case 38: numero = 18; break; //8
					case 33: numero = 19; break; //9							
				}
			}		
			
//alert(numero+' '+e.which+' '+e.keyCode+'\r\nmays: '+e.shiftKey+'\r\nctrl: '+e.ctrlKey+'\r\nalt: '+e.altKey+'\r\naltgr: '+(e.altKey && e.ctrlKey));

			if (numero!=-1) {
				if (numero==0 && (!shift) && (!ctrl) && (!alt) && (!altgr)) {
					for (i=1;i<999;i++) {
						var nodo = getElement("pulsa_"+i);
						if (nodo!=null)
							nodo.checked=false;
						else
							break;						
					}
				} else {					
					var nodo = getElement("pulsa_"+numero);
					if (nodo!=null) {
						if (nodo.type=="radio" && !nodo.disabled) {
							nodo.checked=true;
							var evt = document.createEvent('MouseEvents');
							evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
							nodo.dispatchEvent(evt);
						}else if (nodo.type=="checkbox") {
							if (!nodo.disabled) {
								nodo.checked=!nodo.checked;								
								if ((typeof(nodo.onclick) != "undefined"))
									nodo.onclick();
							}
						}
					}
				}
			}
			//if (msg!=null) msg.innerHTML=event.keyCode+";"+event.altKey+";"+event.ctrlKey+";"+event.shiftKey+"===>"+numero;
			e.returnValue=false;
			return false;
		}
	}


	function keypress_ie() {
		if (document.getElementById('capa_alert').style.visibility == 'visible' || document.getElementById('capa_desactivar').style.visibility == 'visible')
			return;
		if (estaPausa()) {
			event.returnValue=false;
			return false;
		}
		var numero=-1;
		if (esRadio==null) esRadio=false;
		if (event.keyCode==13 && (!event.shiftKey)) {
			enviarFormularioPagina();
			event.returnValue=false;
			return false;
		} else if ((event.keyCode==27 && event.shiftKey) || event.keyCode==27) {
			enviarFormularioPaginaAnterior();
			event.returnValue=false;
			return false;
		}
		if (document.formulario.bloque.value!="1" && esRadio && (document.formulario.semiab.value!="1" || (document.formulario.semiab.value=="1" && foco!="text"))) {
			var msg = getElement("msg");
			//if (msg!=null) msg.innerHTML=event.keyCode+";"+event.altKey+";"+event.ctrlKey+";"+event.shiftKey;


			if (event.keyCode>=48 && event.keyCode<=57) {
				numero = event.keyCode-48;
			} else if (event.keyCode>=96 && event.keyCode<=105) {
				numero = event.keyCode-96;
			} else if (event.keyCode==45) numero=10;
			else if (event.keyCode==35) numero=11;
			else if (event.keyCode==40) numero=12;
			else if (event.keyCode==34) numero=13;
			else if (event.keyCode==37) numero=14;
			else if (event.keyCode==12) numero=15;
			else if (event.keyCode==39) numero=16;
			else if (event.keyCode==36) numero=17;
			else if (event.keyCode==38) numero=18;
			else if (event.keyCode==33) numero=19;

			if (numero!=-1) {
				if (numero==0 && (!event.altKey) && (!event.shiftKey) && (!event.ctrlKey) ) {
					for (i=1;i<999;i++) {
						var nodo = getElement("pulsa_"+i);
						if (nodo!=null)
							nodo.checked=false;
						else
							break;						
					}
				} else {
					if (event.altKey && event.ctrlKey) {
						numero = 40+numero;
					} else if (event.ctrlKey) {
						numero = 20+numero;
					} else if (event.altKey) {
						numero = 30+numero;
					} else if (event.shiftKey) {
						numero = 10+numero;
					}
					var nodo = getElement("pulsa_"+numero);
					if (nodo!=null) {

						if (nodo.type=="radio" && !nodo.disabled){ 
							nodo.checked=true;
							nodo.fireEvent("onclick");
						}else if (nodo.type=="checkbox") {
							if (!nodo.disabled) {
								nodo.checked=!nodo.checked;
								nodo.fireEvent("onclick");
							}
						}
					}
				}
			}
			//if (msg!=null) msg.innerHTML=event.keyCode+";"+event.altKey+";"+event.ctrlKey+";"+event.shiftKey+"===>"+numero;
			event.returnValue=false;
			return false;
		}
	}

function comprobarmarca(rdbtn,valor1,valor2){
	if(valor1===valor2){
		rdbtn.checked=false;
		return true;
	}
	rdbtn.checked=true;
	return false;
}

function disable_item_timer(id, timer){
	document.getElementById(id).style.visibility='hidden';
	setTimeout('enable_item("'+id+'")', timer);	
}

function enable_item(id){
	document.getElementById(id).style.visibility='visible';
}
