
function detecta(){
if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){ //test for Firefox/x.x or Firefox x.x (ignoring remaining digits);
 var ffversion=new Number(RegExp.$1) // capture x.x portion and store as a number
 if (ffversion>=3){
  //document.write("You're using FF 3.x or above")
  alert("You're using FF 3.x or above")
 }else if (ffversion>=2){
  //document.write("You're using FF 2.x")
  alert("You're using FF 2.x")
 }else if (ffversion>=1){
  //document.write("You're using FF 1.x")
  alert("You're using FF 1.x")}
}
else{
 //document.write("n/a")
  //alert("no es FF")
 }

if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
 var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
 if (ieversion>=8){
  //document.write("You're using IE8 or above")
  alert("You're using IE8 or above")
} else if (ieversion>=7){
  //document.write("You're using IE7.x")
   alert("You're using IE7.x")
}else if (ieversion>=6){
  //document.write("You're using IE6.x")
   alert("You're using IE6.x")
}else if (ieversion>=5){
  //document.write("You're using IE5.x")
  alert("You're using IE5.x")}
}
else{
 //document.write("n/a")
 // alert("no es Explorer")
}

//1) Detect IE5+ and Firefox (including NS6+)
if (document.getElementById)
alert("You are using a modern browser that supports the DOM")//2) Detect Firefox (and NS6+) exclusively
if (document.getElementById && !document.all)
alert("You are using Firefox or NS6+")//3) Detect Opera 6+ exclusively
if (window.opera && window.print)
alert("You are using Opera 6 or above")
}

function pepe(){
//	document.write("<B>Nombre de tu navegador: </B>" + navigator.appName + "<BR>")
//document.write("<B>Agente: </B>" + navigator.userAgent + "<BR>")
//document.write("<B>Versión: </B>" + navigator.appVersion + "<BR>")
alert("<B>Nombre de tu navegador: </B>" + navigator.appName + "<BR>"+"<B>Agente: </B>" + navigator.userAgent + "<BR>"+"<B>Versión: </B>" + navigator.appVersion + "<BR>")
}



function carrera(){
	this.hora;
	this.minu;
	this.seg;
	this.segtot;
	this.mintot;
	this.velockmho;
	this.velocmtsg;
	this.ritminkm;
	this.ritsegkm;
	this.ritsegmt;
}
function segundea(time){
	time.segtot = time.hora * 3600 + time.minu * 60 + time.seg * 1
	time.mintot = time.hora * 60 + time.minu * 1 + time.seg / 60
}

function calcSegTot(){
		segtot = hora * 3600 + minu * 60 + seg
}

function segs2mins(segs){
	hor = Math.round((segs/3600)-((segs%3600)/3600))
	segs = segs - hor * 3600
	min = Math.round((segs/60)-((segs%60)/60))
	seg = segs - min * 60
	seg = Math.round(seg *100) /100
	//alert("formattempo: "+hor+":"+min+":"+seg+"  desde "+segs)
	return formattempo(hor,min,seg)
}
function formattempo(hor,min,seg){
	txt = ""
	if(hor > 0) txt = hor+" Hs. "
	if(min > 0 || txt != "") txt += min+" Min. "
	if(seg > 0 || txt != "") txt += seg+" Segs."
	//alert("formattempo: "+hor+":"+min+":"+seg+"  y queda "+txt)
	return txt
}


var distanciaKm =0;
var distanciaMt=0;
var distanciaMll=0;
var carr=0;

function leeDistKm(form){
	if(isNaN(form.distkm.value)) {
		limpiadist(form)
		alert("Debe introducir un valor numèrico")
		return false
	}else{
		distanciaKm = parseFloat(form.distkm.value)
		distanciaMt = distanciaKm * 1000
		distanciaMll = distanciaKm /  1.604
		renderdist(form)
		return true
	}
}
function leeDistMt(form){
	if(isNaN(form.distmt.value)) {
		limpiadist(form)
		alert("Debe introducir un valor numèrico")
		return false
	}else{
		distanciaMt = parseFloat(form.distmt.value)
		distanciaKm = distanciaMt / 1000
		distanciaMll = distanciaKm /  1.604
		renderdist(form)
		return true
	}
}
function leeDistMll(form){
	if(isNaN(form.distmll.value)) {
		limpiadist(form)
		alert("Debe introducir un valor numèrico")
		return false
	}else{
		distanciaMll = parseFloat(form.distmll.value)
		distanciaKm = distanciaMll * 1.604
		distanciaMt = distanciaKm * 1000
		renderdist(form)
		return true
	}
}

function calcula(form){
	if(distanciaKm==""){
		alert("Debe introducir una Distancia")
		return
	}
	ho = form.horas.value
	mi = form.minus.value
	sg = form.segs.value
	sgt = form.sgstot.value
	if(ho==0 && mi==0 && sg==0 && sgt==0){
		 alert("Debe introducir un tiempo")
		 return
	}
	if(ho==0 && mi==0 && sg==0){
		//calcula en base a sgt
		//renderComboTiempo(form,sgt)
			ho = Math.round((sgt/3600)-((sgt%3600)/3600))
			sgt = sgt - ho * 3600
			mi = Math.round((sgt/60)-((sgt%60)/60))
			sg = sgt - mi * 60
			sg = Math.round(sg *100) /100
			//poner en combos
			form.horas.options[ho].selected = true
			form.minus.options[mi].selected = true
			form.segs.options[sg].selected = true
	}
	carr = new carrera() 
//	alert("Calculo "+distanciaKm+" km en "+ho+" "+mi+" "+sg)
	carr.hora = ho
	carr.minu = mi
	carr.seg = sg
	segundea(carr)
	form.sgstot.value = carr.segtot
	//alert(carr.segtot+" segundos")
	carr.velockmho = distanciaKm * 3600 / carr.segtot
	carr.velockmho = Math.round(carr.velockmho *100) /100
	carr.velocmtsg = distanciaMt / carr.segtot
	carr.velocmtsg = Math.round(carr.velocmtsg *100) /100
	//alert(distanciaKm+" / "+carr.segtot+" = "+carr.velockmho)
	form.velkmh.value = carr.velockmho
	form.velmsg.value = carr.velocmtsg
	carr.ritsegkm = carr.segtot / distanciaKm
	carr.ritsegmt = carr.ritsegkm / 1000
	carr.ritsegkm = Math.round(carr.ritsegkm *100) /100
	carr.ritsegmt = Math.round(carr.ritsegmt *100) /100
	carr.ritminkm = segs2mins(carr.ritsegkm)
	form.ritmkm.value = carr.ritminkm
	form.ritsmt.value = carr.ritsegmt
}
//Calculador de tiempo total y velocidades a partir de RITMO y DIST
function calcTiempoVeloc(form){
	//alert(form.name)
	if(leeVeloc(form)){
		hrs = distanciaKm / velKmH 		//minutos totales
		sgt = hrs * 3600
		renderComboTiempo(form,sgt)
		calcula(form)
	}
	
}
var velKmH = 0		//	Minutos/Km
var velMtS = 0		//	Segs/Metro


function leeVeloc(form){
//	alert("Ritmo: campo = "+form.ritmkm.value+", parseFloat="+parseFloat(form.ritmkm.value)+", isNaN = "+isNaN(form.ritmkm.value))
	//distanciaMll = document.getElementById('distmll').value
	//poner indexOf('S') xq parseFloat lee 5 en '5Min'
	if(isNaN(form.velkmh.value) || parseFloat(form.velkmh.value) <= 0 || form.velkmh.value == "") {
		limpiVel(form)
		alert("Debe introducir un valor numèrico de Velocidad\nEj: 5.3")
		return false
	}else{
		velKmH = parseFloat(form.velkmh.value)
		//velMtS = parseFloat(form.velmsg.value)
		return true	}
}
//..................................
function limpiVel(form){
	velKmH = 0
	velMtS = 0
	form.velkmh.value = ""
	form.velmsg.value = ""
}
//------- RITMO -> calcula resto
function renderComboTiempo(form,sgt){
			ho = Math.round((sgt/3600)-((sgt%3600)/3600))
			sgt = sgt - ho * 3600
			mi = Math.round((sgt/60)-((sgt%60)/60))
			sg = sgt - mi * 60
			sg = Math.round(sg)
			//poner en combos
			form.horas.options[ho].selected = true
			form.minus.options[mi].selected = true
			form.segs.options[sg].selected = true	
}

//Calculador de tiempo total y velocidades a partir de RITMO y DIST
function calcTiempoRitmo(form){
	//alert("calcTiempoRitmo")
	if(leeRitmo(form)){
		sgt = ritmoKm * distanciaKm		//Segundos totales
		renderComboTiempo(form,sgt)
		calcula(form)
	}
}
//ritmkm
var ritmoKm = 0		//	segundos/Km
var ritmoMt = 0		//	Segs/Metro


function leeRitmo(form){
//	alert("Ritmo: campo = "+form.ritmkm.value+", parseFloat="+parseFloat(form.ritmkm.value)+", isNaN = "+isNaN(form.ritmkm.value))
	//distanciaMll = document.getElementById('distmll').value
	//poner indexOf('S') xq parseFloat lee 5 en '5Min'
	if(form.velmin.value == 0 && form.velseg.value == 0) {
		//limpiRit(form)
		alert("Debe introducir Minutos/Segundos")
		return false
	}else{
		ritmoKm = parseInt(form.velmin.value) * 60 + parseInt(form.velseg.value)
	//alert("leo "+form.velmin.value+" y "+form.velseg.value+" = "+ritmoKm)
		return true	}
}

//..................................
function limpiRit(form){
	ritmoKm = 0
	ritmoMt = 0
	form.ritmkm.value = ""
	form.ritsmt.value = ""
}

function limpia(form){
	limpiadist(form)
}
function limpiadist(form){
		distanciaKm=""
		distanciaMt=""
		distanciaMll=""
		form.distkm.value = ""
		form.distmt.value = ""
		form.distmll.value = ""	
}
function limpiatiempo(form){
			form.horas.options[0].selected = true
			form.minus.options[0].selected = true
			form.segs.options[0].selected = true
			form.sgstot.value = 0
}
function renderdist(form){
		form.distmt.value = redon2(distanciaMt)
		form.distkm.value = redon2(distanciaKm)
		form.distmll.value = redon2(distanciaMll)	
}
function redon2(esto){
	return Math.round(esto *100) /100
}
///////////////////////////////////////////////////////////////////////////////
// IMC
 function calcimc(form){
 	var imc = 0;
 	altor = form.alto.value
 	pesor = form.peso.value
	if(isNaN(parseInt(altor)) || isNaN(parseInt(pesor))) {
		alert("Debe introducir valores numèricos en Peso y Altura")
	}else{
		aux = altor * altor / 10000
		imc = pesor / aux
		imc = Math.round(imc *100) /100
		form.imc.value = imc
	  min = aux * 18.5 
	  max = aux * 25 
	  min = Math.round(min *100) /100
	  max = Math.round(max *100) /100
		form.ideal.value = min + " y " + max + " Kilos"
	}
}
//PASADAS
//Obj.tiempo

function tempo(){
	this.segs=0
	this.mins=0
	this.hors=0
	this.segtot=0
	this.msg=0
}
//Obj. 1 pasada
function pasa(){
	this.dist						// distancia de la pasada
	this.x100						// x ciento del esfuerzo
	this.times					// obj. tempo
}

function pasada(){
	this.dist
	this.al100
	this.pasas
}
function planpasadas(){
	this.dist							//distancia del test = 1000mts
	this.al100						//obj. tiempo del test al 100%
	this.xcentajes				//coleccion %s del plan
	this.xcenmsg					//mensaje visible
	this.xdist						//coleccion de distancias del plan
	this.xdistmsg					//msg distancias
	this.pasas						//matriz de objetos pasa()
}

function configpasadas(form){
//alert("configpasadas")
	pp = new planpasadas()
	pp.dist = 1000
	pp.al100 = getTempo(0,form.mins.value,form.segus.value)	
	pp.xcentajes = cargax100(form)	//new Array(90,80,70,60)
	pp.xdist = cargadist(form);	//new Array(800,600,400,200,100)
	calcpasadas(pp)
	renderplanpasadas(form,pp)
}
//metodo gallego
function cargadist(form){
	arr = new Array()
	if(form.d3000.checked == true) arr.push(3000)
	if(form.d2000.checked == true) arr.push(2000)
	if(form.d1500.checked == true) arr.push(1500)
	if(form.d1000.checked == true) arr.push(1000)
	if(form.d900.checked == true) arr.push(900)
	if(form.d800.checked == true) arr.push(800)
	if(form.d700.checked == true) arr.push(700)
	if(form.d600.checked == true) arr.push(600)
	if(form.d500.checked == true) arr.push(500)
	if(form.d400.checked == true) arr.push(400)
	if(form.d300.checked == true) arr.push(300)
	if(form.d200.checked == true) arr.push(200)
	if(form.d100.checked == true) arr.push(100)
	return arr
}
function cargax100(form){
	arr = new Array()
//alert("cargax100: "+form.p100.checked)
	if(form.p100.checked == true) arr.push(100)
	if(form.p90.checked == true) arr.push(90)
	if(form.p80.checked == true) arr.push(80)
	if(form.p75.checked == true) arr.push(75)
	if(form.p70.checked == true) arr.push(70)
	if(form.p60.checked == true) arr.push(60)
	if(form.p50.checked == true) arr.push(50)
	/*if(document.getElementById('p40').checked == true) arr.push(40)
	if(document.getElementById('p30').checked == true) arr.push(30)
	if(document.getElementById('p20').checked == true) arr.push(20)
	if(document.getElementById('p10').checked == true) arr.push(10)*/
	return arr
}

function calcpasadas(pp){
	//alert("calcpasadas "+pp.xcentajes.length)
	pp.pasas = new Array()
	for(i=0;i<pp.xdist.length;i++){
		pp.pasas[i] = new Array()
		for(j=0;j<pp.xcentajes.length;j++){
			t = new tempo()
			t.segtot = Math.round(pp.al100.segtot * 100 * pp.xdist[i] / (pp.dist * pp.xcentajes[j])) //tiempo100%/90%  * 800/1000
			//alert(t.segtot+": "+i+","+j)
			segs2time(t)
			pp.pasas[i][j] = t
		}
	}
	//alert("calcpasadas")
}


//alert("213/60= "+(213/60)+" y el resto 213%60= "+(213%60)+"   horas: "+((213/60)-((213%60)/60)))
//segs2time(new tempo())

function segs2time(t){
	segst = t.segtot
	hor = Math.round((segst/3600)-((segst%3600)/3600))
	segst = segst - hor * 3600
	min = Math.round((segst/60)-((segst%60)/60))
	segst = segst - min * 60
	seg = Math.round(segst *100) /100
	t.hors = hor
	t.mins = min
	t.segs = seg
	t.msg = formattempo(hor,min,seg)
	//alert("segs2time: "+hor+":"+min+":"+seg+"  desde 213  "+t.segtot)
	//alert(t.msg)
}

function getTempo(h,m,s){
	tt = new tempo()
	tt.hors = h
	tt.mins = m
	tt.segs = s
	caltempo(tt)
	tt.msg = formattempo(tt.hors,tt.mins,tt.segs)
	return tt	
}
function showtempo(t){
	txt = t.msg+"\nSegundos: "+t.segtot
	alert(txt)
}
function caltempo(time){
	time.segtot = time.hors * 3600 + time.mins * 60 + time.segs * 1
}
////////////////////////////////////
//  Renderizado
var iniTable = "<table border='0' align='center'>"
var finTable = "</table>"
var iniTr = "<tr>"
var finTr = "</tr>"
var tdColor = "<td bgcolor="						//aca color '#55ccdd'
var tdColor2 = " align='center'>"				//aca contenido
var tdfin = "</td>"
color1 = '#77ccdd' //cuadro vacio
color2 = '#cc88dd' //header
color3 = '#eeaa88' //col. head linea datos
color4 = '#eedd99' // linea datos


function renderplanpasadas(form,pp){
//alert("renderplanpasadas")
	txt = iniTable + iniTr + tdColor + color1 + tdColor2 + tdfin 
	for(j=0;j<pp.xcentajes.length;j++){
		txt += tdColor + color2 + tdColor2 + "&nbsp;" + pp.xcentajes[j] + "%&nbsp;" + tdfin
	}
	txt += finTr
	for(i=0;i<pp.xdist.length;i++){
		txt += iniTr
		txt += tdColor + color3 + tdColor2 + "&nbsp;" + pp.xdist[i] + "&nbsp;" + tdfin
		for(j=0;j<pp.xcentajes.length;j++){
			txt += tdColor + color4 + tdColor2 + "&nbsp;" + pp.pasas[i][j].msg + "&nbsp;" + tdfin
		}
		txt += finTr
	}
	txt += finTable
//alert(txt)
	document.getElementById('pasas').innerHTML = txt
}

function initCombosVel(){
	document.getElementById('tiempop').innerHTML = armaSelect('horas','',30)+"&nbsp;Horas&nbsp;&nbsp;"+armaSelect('minus','',60)+"&nbsp;Minutos&nbsp;&nbsp;"+armaSelect('segs','',60)+"&nbsp;Segsundos"
	document.getElementById('cr_min').innerHTML = armaSelect('velmin','',60)+"&nbsp;Mins.&nbsp;"+armaSelect('velseg','',60)+"&nbsp;Segs."
}

//arma HTML de Select numerico de 0 a max
function armaSelect(nombre,oncambio,max){
	var txt = "<select name='"+nombre+"' onchange='"+oncambio+"'>"
	for(i=0;i < max;i++){
		txt += getOption(i,i,0)
	}
	txt += "</select>"
//alert(txt)
	return txt
}

function getOption(val,texto,sel){
	var txt = "<option value='"+val+"' "
	if(sel) txt += "selected"
	txt += ">"+texto+"</option>"
	return txt
}

function inicia(){
	//alert("empesamo")
	initCombosVel()
}

//Descular tecla : se llama: onkeypress="return validarDigito(this,event)"
 function validarDigito(obj,e) {

	var keynum
	var keychar
	var numcheck
	var stringTotal = obj.value;
	
	
	if(window.event) // IE
	{
		keynum = e.keyCode
	}
	else if(e.which) // Netscape/Firefox/Opera
	{
		keynum = e.which
	}
	
	keychar = String.fromCharCode(keynum);
	numcheck = /\d/;
	
	if ( (keynum >= 48 && keynum <= 57) || (keynum == 46))  {
		return true;
	}
	else{
		alert('El caracter no es valido');
	 	return false;
	}
 }

function contar255(name) {
  var text = document.getElementById(name);
  n = text.value.length;
  t = 255;
  if (n > t) {
     text.value = text.value.substring(0, t);
     alert("La cantidad de Caracteres Actual es la Máxima Permitida");
  }
 }

 function popUpear() {
		winWidth = 800;
		winHeight= 400;
		leftPosition = (screen.width) ? (screen.width- winWidth)/2 : 0;
		topPosition = (screen.height) ? (screen.height- winHeight)/2 : 0;

		fullUrl = "../xxx/eso.html"
		
		settings =	'height=' +winHeight+ ', width=' +winWidth+ 
					', top=' +topPosition+ ', left=' +leftPosition+ 
					', scrollbars=yes, resizable=yes, status=yes';
		window.name = "Prestadores";
		window.open(fullUrl,"",settings);
 }
