function getCheckedId(radioObj) {
	if(!radioObj) return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked) return radioObj.id;
		else return "";
		for(var i = 0; i < radioLength; i++) {
			if(radioObj[i].checked) {return radioObj[i].id;}
		}
	return "";
}

function getListValue(name) {
	var obj = document.getElementById(name);
	if (obj != null) {
	   return obj.options[obj.selectedIndex].value;
	}
	return '';
}

var patron = new Array(2,2,4);
var patronHora = new Array(2,2);
var patron2 = new Array(1,3,3,3,3);
var dias = new Array('<replace id="uti.js.26">Domingo</replace>','<replace id="uti.js.20">Lunes</replace>','<replace id="uti.js.21">Martes</replace>','<replace id="uti.js.22">Miercoles</replace>','<replace id="uti.js.23">Jueves</replace>','<replace id="uti.js.24">Viernes</replace>','<replace id="uti.js.25">Sábado</replace>')
var meses = new Array('<replace id="uti.js.08">Enero</replace>','<replace id="uti.js.09">Febrero</replace>','<replace id="uti.js.10">Marzo</replace>','<replace id="uti.js.11">Abril</replace>','<replace id="uti.js.12">Mayo</replace>','<replace id="uti.js.13">Junio</replace>','<replace id="uti.js.14">Julio</replace>', '<replace id="uti.js.15">Agosto</replace>','<replace id="uti.js.16">Septiembre</replace>','<replace id="uti.js.17">Octubre</replace>','<replace id="uti.js.18">Noviembre</replace>','<replace id="uti.js.19">Diciembre</replace>');

function mascara(d,sep,pat,nums)
{
	if(d.valant != d.value){
		val = d.value;
		largo = val.length;
		val = val.split(sep)
		val2 = '';
		for(r=0;r<val.length;r++){
			val2 += val[r];	
		}
		if(nums){
			for(z=0;z<val2.length;z++){
				if(isNaN(val2.charAt(z))){
					letra = new RegExp(val2.charAt(z),"g");
					val2 = val2.replace(letra,"");
				}
			}
		}
		val = '';
		val3 = new Array();
		for(s=0; s<pat.length; s++){
			val3[s] = val2.substring(0,pat[s]);
			val2 = val2.substr(pat[s]);
		}
		for(q=0;q<val3.length; q++){
			if(q ==0){
				val = val3[q];
			}
			else{
				if(val3[q] != ""){
					val += sep + val3[q];
					}
			}
		}
		d.value = val;
		d.valant = val;
		}
	}

	/* Variables y funciones necesarias para cambiar el número de noches o fechas del viaje (será de la vuelta) de forma automática */
	var aFinMes = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); 

	/* diferencia entre dos fechas dependiendo el mes*/
	function days_between(date1, date2){
		var ONE_DAY = 1000 * 60 * 60 * 24;
		var date1_ms = date1.getTime();
		var date2_ms = date2.getTime();
		var difference_ms = (date1_ms - date2_ms);
		return Math.round(difference_ms/ONE_DAY);
	}
	
/* incrementa fechas */
function incDate(sFec0){  	
	var nDia = parseInt(sFec0.substr(0, 2), 10); 
	var nMes = parseInt(sFec0.substr(3, 2), 10); 
	var nAno = parseInt(sFec0.substr(6,4), 10); 
	nDia += 1; 
	if (nDia > finMes(nMes, nAno)){ 
		nDia = 1; 
		nMes += 1; 
		if (nMes == 13){ 
			nMes = 1; 
			nAno += 1; 
		} 
	}
	return makeDateFormat(nDia, nMes, nAno); 
}  

//Decrementar fecha
function decDate(sFec0){ 
	var nDia = Number(sFec0.substr(0, 2)); 
	var nMes = Number(sFec0.substr(3, 2)); 
	var nAno = Number(sFec0.substr(6,4)); 
	nDia -= 1; 
	if (nDia == 0){ 
		nMes -= 1; 
		if (nMes == 0){ 
			nMes = 12; 
			nAno -= 1; 
		} 
		nDia = finMes(nMes, nAno); 
	} 
	return makeDateFormat(nDia, nMes, nAno); 
} 
/* comprobacion si es fin de mes y año bisiesto */
function finMes(nMes, nAno)  { 
	return aFinMes[nMes - 1] + (((nMes == 2) && (nAno % 4) == 0)? 1: 0); 
} 

/*colocacion del 0 para las fechas*/	 
function padNmb(nStr, nLen, sChr){ 
	var sRes = String(nStr); 
	for (var i = 0; i < nLen - String(nStr).length; i++) 
		sRes = sChr + sRes; 
	return sRes; 
} 

/*creacion del formato fecha --/--/---- */
function makeDateFormat(nDay, nMonth, nYear){ 
	var sRes; 
	sRes = padNmb(nDay, 2, "0") + "/" + padNmb(nMonth, 2, "0") + "/" + padNmb(nYear, 4, "20"); 
	return sRes; 
} 
//proceso que desmenuza la fecha y su hora del calendario para asignarlo a dos inputs	
function getFechaHora(cal)
{
	var rest= cal.value;
	document.getElementById(cal.id).value = cal.value.substr(0,10);
	if (cal.id=='inputRC_fecori')
	{
		document.getElementById('inputRC_horori').value = rest.substr(10,10);
	}	
	else if (cal.id=='inputRC_fecdes')
	{
		document.getElementById('inputRC_hordes').value = rest.substr(10,10);
	}	
}
		
function fromFecha(fechastr,hh,mm,ss) 
 {        
	 if ((fechastr != null) && (esFecha(fechastr))) 
	{
		return (new Date(Math.floor(fechastr.substr(8, 4)), Math.floor(fechastr.substr(3, 2)) - 1, Math.floor(fechastr.substr(0, 2)),hh,mm,ss));
	} 
	 else 
	  {
		return new Date();
	   }
}
		
function esFecha(fechastr) 
{
	var regexpfecha = /^([0-3][0-9])\/([0-1][0-9])\/([0-9][0-9][0-9][0-9])$/;
	return regexpfecha.test(fechastr);
}	
		
//modificamos el formato de fecha según tip
function toDate(tip,dia,mes,ano){
	var imes = parseInt(mes,10)-1;
	var strmes = meses[imes];
	var fecha = new Date(ano,imes,dia);
	var diasemana = fecha.getDay();
	var strdia = dias[diasemana]; 
	var fec='';
	if (tip==0)
		fec = strdia + ', ' + dia + ' de ' + strmes + ' del ' + ano;
	if (tip==1)
		fec = dia + ' ' + strmes + ' ' + ano;
	if (tip==2)
		fec = dia + ' de ' + strmes + ' del ' + ano;
	if (tip==3)
		fec = dia + ' de ' + strmes;
	return fec;		
}
        
var validos="abcçdefghijklmnñopqrstuvwxyzABCÇDEFGHIJKLMNÑOPQRSTUVWXYZ áéíóúüÁÉÍÓÚÜ./";
letri= new Array();
letri[0] = "T" ;letri[1] = "R" ;letri[2] = "W" ;
letri[3] = "A" ;letri[4] = "G" ;letri[5] = "M" ;
letri[6] = "Y" ;letri[7] = "F" ;letri[8] = "P" ;
letri[9] = "D" ;letri[10] = "X" ;letri[11] = "B" ;
letri[12] = "N" ;letri[13] = "J" ;letri[14] = "Z" ;
letri[15] = "S" ;letri[16] = "Q" ;letri[17] = "V" ;
letri[18] = "H" ;letri[19] = "L" ;letri[20] = "C" ;
letri[21] = "K" ;letri[22] = "E" ;

var letrasNIF="TRWAGMYFPDXBNJZSQVHLCKE";   
       
function validamail(EmailAddr) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(EmailAddr)){
		return (true)
	} else {
		alert('<replace id="uti.js.00">El E-mail introducido no es correcto</replace>');
		return (false);
	}
}                  	

function nombrevalido(texto, campo) {
	 if (texto.length>25) {
		 alert('<replace id="uti.js.01">El campo</replace> ' + campo + ' <replace id="uti.js.02">es demasiado largo, por favor redúzcalo a 25 carácteres máximo.</replace>');
		 return false;
	  }
	  
	  for (var i=0; i < texto.length; i++) {
		var letra = texto.charAt(i);
		if (validos.indexOf(letra)==-1) {
		alert('<replace id="uti.js.01">El campo</replace> ' + campo + ' <replace id="uti.js.03">contiene el carácter no válido</replace>: '+letra+'.\n<replace id="uti.js.04">Por favor, use sólo los carácteres válidos, que son</replace>:\n'+validos);
		return false;}
	   }
	  return true;
}	 
         
function nifvalido(texto, campo) {
	texto=texto.toUpperCase();
	var ultimochar=texto.charAt(texto.length-1);
	var Q=(Math.abs(texto.substr(0,texto.length-1))%23);
	if (letrasNIF.indexOf(ultimochar)==-1 || letri[Q]!=ultimochar) {
		alert('<replace id="uti.js.01">El campo</replace> '+campo+' <replace id="uti.js.05">es erróneo, por favor revise ese campo.</replace>');
		return false;
	}
	return true;
}

function documentovalido(doc,tipdoc) {
	if (tipdoc == 'D') {
		if (nifvalido(doc,'DNI/NIF')) return true;
	} else if (tipdoc == 'C' || tipdoc == 'T') {
		var caracteres_validos = "abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZ0123456789";
		for (var i=0; i < doc.length; i++) {
			var letra = doc.charAt(i);
			if (caracteres_validos.indexOf(letra) == -1) {
				alert('<replace id="uti.js.06">El campo Nº de Documento contiene el carácter no válido</replace>: '+letra);
				return false;
			}
		}
		return true;
	} else {
		alert('<replace id="uti.js.07">Indique el tipo de documento</replace>');
	}
	return false;
}

function isNumber (valor) {
	var valido = /^([0-9])+$/	
	if (!valido.test (valor))
		return false
	return true;
}

function esNumero(cadena){
/*
* Finalidad: devuelve si la cadena está formada por números o no.
* Entradas:  la cadena a validar.
* Salida:    TRUE si son todos números, false en caso contrario.
*/

	var bCaracter=false;
	var i=0;
	
	while(!bCaracter && i<cadena.length){
		if("0123456789".indexOf(cadena.charAt(i))==-1)
			bCaracter=true;
		else 
			i++;
	}
	
	if(cadena.length==0) return false;
	else return !bCaracter; 
}

function esNIFCorrecto(dni) {
 /*
* Finalidad: devuelve si un NIF es correcto o no.
* Entradas:  la cadena a validar.
* Salida:    TRUE si es correcto, false en caso contrario.
*/
  var bRetorno = true;
  if(dni.length<=9){
	  let = dni.substr(dni.length-1,1).toUpperCase();	
	  numero = dni.substr(0,dni.length-1);
	  numero = numero % 23;
	  letra='TRWAGMYFPDXBNJZSQVHLCKET';
	  letra=letra.substring(numero,numero+1);	  	
	  if (letra!=let) bRetorno = false;
   } else bRetorno = false;
  return bRetorno;
}

function esCIFCorrecto(sCIF){
/*
* Finalidad: realiza todas las validaciones para asegurarnos que es un CIF correcto.
* Entradas:  la cadena a validar.
* Salida:    TRUE si es correcto, false en caso contrario.
*/
   var bRetorno=false;
   sCIF = sCIF.toUpperCase();
	if(sCIF.length==9){
			//es un CIF o un NIE(extranjeros)
		 if("ABCDEFGHPQSKLMYX".indexOf(sCIF.substr(0,1))!=-1){
				if(sCIF.charAt(0)=="X" || sCIF.charAt(0)=="T" || sCIF.charAt(0)=="A"){
					bRetorno=esNIFCorrecto("0" + sCIF.substr(1,8));
				}else if(sCIF.charAt(0)=="Y"){
				      bRetorno=esNIFCorrecto("1" + sCIF.substr(1,8));	
				}else{
					if(esNumero(sCIF.substr(1,7))){
						var suma = parseInt(sCIF.substr(2,1),10) +
							parseInt(sCIF.substr(4,1),10) +
							parseInt(sCIF.substr(6,1),10);	

						for(var n=0; n<4; n++){
							suma+=((2*parseInt(sCIF.substr((2*n)+1,1),10))%10 +
								Math.floor((2*parseInt(sCIF.substr((2*n)+1,1),10))/10));
						}
						
						var control = 10 - (suma % 10);
						
						if("PQS".indexOf(sCIF.substr(0,1))!=-1){
							bRetorno=(sCIF.substr(8,1)==String.fromCharCode(64 + control));	
						}else{
							if(esNumero(sCIF.substr(8,1))){
								if(control==10)
									control=0;
								bRetorno=(parseInt(sCIF.substr(8,1),10)==control);	
							}
						}
						
					}
				}
			}
	}
	return bRetorno;
  }