//============================================================+
// File name   : sprint_distribution_ita.js                                   
// Begin       : 2000-08-17                                    
// Last Update : 2003-11-20                                    
//                                                             
// Description : Sprint races distribution                                    
//                                                             
//                                                             
// Author: Nicola Asuni                                        
//                                                             
// (c) Copyright:                                              
//               Tecnick.com S.r.l.                            
//               Via Ugo Foscolo n.19                          
//               09045 Quartu Sant'Elena (CA)                  
//               ITALY                                         
//               www.tecnick.com                               
//               info@tecnick.com                              
//============================================================+

var AverageSpeed_ms=0;
var AverageSpeed_kmh=0;
var f0=0;
var rowcolor = new Array("#ffffcc","#ccffcc"); // Colori per le righe (alternati)

// --------------------------------------------------------------------------
function RoundCent(num)
{
 return( Math.round(num * 100)/100 );
}

// --------------------------------------------------------------------------
function PrintPads(num)
{
 var pads = 0;
 var tempnum = Math.round((num - Math.floor(num))*100);
 if (tempnum==0) pads=2;
 else 	{
        tempnum = tempnum/10;
 		tempnum = Math.round((tempnum - Math.floor(tempnum))*10);
		if (tempnum==0) pads=1;
 		}
 
 return(pads);
}

// --------------------------------------------------------------------------
function ClearALL()
{
document.Run_Rhythm_Form.Distance.value = "100";
document.Run_Rhythm_Form.Time.value = "";
document.Run_Rhythm_Form.Reac.value = "0.15";
document.Run_Rhythm_Form.Tc.value = "1.2";
document.Run_Rhythm_Form.K.value = "0.005";
return;
}

// --------------------------------------------------------------------------
function Check_distance()
{
  if(document.Run_Rhythm_Form.Distance.value.length < 1)
   {
    alert("Attenzione: occorre inserire la distanza in metri.");
	return(0);
   }
 else if(isNaN(document.Run_Rhythm_Form.Distance.value))
   {
    alert("Attenzione: la distanza deve essere un numero intero.");
    document.Run_Rhythm_Form.Distance.value = "";
	return(0);
   }
  else if((document.Run_Rhythm_Form.Distance.value<10)||(document.Run_Rhythm_Form.Distance.value>250))
   {
    alert("Attenzione: la distanza inserita non è regolare (10<Distanza<250).");
    document.Run_Rhythm_Form.Distance.value = "";
	return(0);
   }
 return(1);
}

// --------------------------------------------------------------------------
function Check_time()
{
  if(document.Run_Rhythm_Form.Time.value.length < 1)
   {
    alert("Attenzione: occorre inserire il tempo in secondi.");
	return(0);
   }
 else if(isNaN(document.Run_Rhythm_Form.Time.value))
   {
    alert("Attenzione: il tempo deve essere un numero.\nUsate il punto come separatore decimale.");
    document.Run_Rhythm_Form.Time.value = "";
	return(0);
   }
  else if((document.Run_Rhythm_Form.Time.value<1)||(document.Run_Rhythm_Form.Time.value>40))
   {
    alert("Attenzione: il tempo inserito non è regolare (1<Tempo<40).");
    document.Run_Rhythm_Form.Time.value = "";
	return(0);
   }
 return(1);
}

// --------------------------------------------------------------------------
function Check_reaction()
{
  if(document.Run_Rhythm_Form.Reac.value.length < 1)
   {
    alert("Attenzione: occorre inserire il tempo di reazione in secondi.");
	return(0);
   }
 else if(isNaN(document.Run_Rhythm_Form.Reac.value))
   {
    alert("Attenzione: il tempo di reazione deve essere un numero.");
    document.Run_Rhythm_Form.Reac.value = "";
	return(0);
   }
  else if((document.Run_Rhythm_Form.Reac.value<0)||(document.Run_Rhythm_Form.Reac.value>0.4))
   {
    alert("Attenzione: il tempo di reazione inserito non è regolare (0<Tempo<0.4).");
    document.Run_Rhythm_Form.Reac.value = "";
	return(0);
   }
 return(1);
}
// --------------------------------------------------------------------------
function Check_Tc()
{
  if(document.Run_Rhythm_Form.Tc.value.length < 1)
   {
    alert("Attenzione: occorre inserire la costante Tc in [m/sec^3].");
	return(0);
   }
 else if(isNaN(document.Run_Rhythm_Form.Tc.value))
   {
    alert("Attenzione: Tc deve essere un numero.");
    document.Run_Rhythm_Form.Tc.value = "";
	return(0);
   }
  else if((document.Run_Rhythm_Form.Tc.value<=0)||(document.Run_Rhythm_Form.Tc.value>=4))
   {
    alert("Attenzione: la Tc inserita non è regolare  (0<Tc<4).");
    document.Run_Rhythm_Form.Tc.value = "";
	return(0);
   }
 return(1);
}

// --------------------------------------------------------------------------
function Check_K()
{
  if(document.Run_Rhythm_Form.K.value.length < 1)
   {
    alert("Attenzione: occorre inserire la costante K in sec.");
	return(0);
   }
 else if(isNaN(document.Run_Rhythm_Form.K.value))
   {
    alert("Attenzione: la costante K deve essere un numero.");
    document.Run_Rhythm_Form.K.value = "";
	return(0);
   }
  else if((document.Run_Rhythm_Form.K.value<=0)||(document.Run_Rhythm_Form.K.value>=1))
   {
    alert("Attenzione: la costante K inserita non è regolare (0<K<1).");
    document.Run_Rhythm_Form.K.value = "";
	return(0);
   }
 return(1);
}


// --------------------------------------------------------------------------
function Calculate_distribution()
{
if (Check_distance() && Check_time() && Check_Tc() && Check_K())
	{
	var i=0;
	var k=0;
	var t=0; //time variable
	var d=0; //d(t) distance function of time
	var v=0; //v(t) speed function of time
	var dprec=-10; //previous distance
	var tprec=0; //previous time
	var S = RoundCent(document.Run_Rhythm_Form.Distance.value); //total distance
	var R = RoundCent(document.Run_Rhythm_Form.Reac.value); //reaction time [sec]
	var Tf = RoundCent(document.Run_Rhythm_Form.Time.value) - R; // total time without reaction
	var K = document.Run_Rhythm_Form.K.value; //costant [s]
	var Tc = document.Run_Rhythm_Form.Tc.value; //costant [m/sec^3]
	var adjust=0;
	
AverageSpeed_ms = RoundCent(S/Tf);
AverageSpeed_kmh = RoundCent(AverageSpeed_ms * 3.6);

f0 = S*(K-(1/Tc))/((1/K)*(Math.exp(-K*Tf)-1)+Tc*(1-Math.exp(-Tf/Tc)));
	
	RhythmWindow=window.open("null.htm","RhythmWindow","menubar=yes,scrollbars=yes,resizable=yes,width=468,height=468'");
	RhythmWindow.focus();
	
	RhythmWindow.document.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><title>Distribuzione ideale per gare di velocit&agrave;  rettilinee</title></head><body>');

	RhythmWindow.document.write('<h3>Distribuzione ideale per gare di velocit&agrave; rettilinee</h3>');
  
  RhythmWindow.document.write('<p>');
  RhythmWindow.document.write('Distanza totale: <b>'+S+' m</b><br>');
  RhythmWindow.document.write('Tempo totale: <b>'+RoundCent(document.Run_Rhythm_Form.Time.value)+' sec</b><br>');
  RhythmWindow.document.write('Tempo di reazione: <b>'+R+' sec</b><br>');
  RhythmWindow.document.write('Velocit&agrave; media: <b>'+AverageSpeed_ms+' m/sec ('+AverageSpeed_kmh+' Km/h)</b><br>');

 RhythmWindow.document.write('Tc: <b>'+Tc+' sec</b> ; ');
 RhythmWindow.document.write('K: <b>'+K+' m/sec<sup>3</sup></b><br>');
  RhythmWindow.document.write('</p>');

  //legenda
  RhythmWindow.document.write('<table width="400" border="0" cellspacing="0" cellpadding="0"><tr>');
  RhythmWindow.document.write('<td align="left"><font color="#FF0000">Distanza [m]</font></td>');
  RhythmWindow.document.write('<td align="right"><font color="#0000FF">Velocit&agrave; [m/sec]</font></td>');
  RhythmWindow.document.write('</tr>');
  RhythmWindow.document.write('<tr><td colspan="2" align="center">');
  
  //grafico
  RhythmWindow.document.write('<applet code="speedgraph.class" codebase="../../shared/java" name="speedgraph" width="400" height="250" >');
    RhythmWindow.document.write('<param name="Distance" value="'+S+'">');
    RhythmWindow.document.write('<param name="Time" value="'+RoundCent(document.Run_Rhythm_Form.Time.value)+'">');
    RhythmWindow.document.write('<param name="Reaction" value="'+R+'">');
    RhythmWindow.document.write('<param name="Tc" value="'+Tc+'">');
    RhythmWindow.document.write('<param name="K" value="'+K+'">');
RhythmWindow.document.write('</applet>');
  
    RhythmWindow.document.write('</td></tr>');
  RhythmWindow.document.write('<tr><td colspan="2" align="center">Tempo [sec]</td></tr>');
  RhythmWindow.document.write('</table><br><br>');

  //tabella
  RhythmWindow.document.write('<table border="1" cellspacing="2" cellpadding="2" align="center">');
  RhythmWindow.document.write('<tr bgcolor="#95caff"><th>Distanza<br>[m]</th><th>Tempo<br>[sec]</th><th>T. diff.<br>[sec]</th></tr>');

  //Print table rows
  for(t=0; t<=(Tf+R); t+=0.01) 
	 {
	  adjust=0;
	  d = RoundCent((f0/(K-(1/Tc)))*((1/K)*(Math.exp(-K*t)-1)+Tc*(1-Math.exp(-t/Tc))));
  
	  //If a distance multiple of 10 is reached...
	  if ( (d>=(dprec+10)) || (t>=(Tf+R)) )
	  		{	
	if (((dprec+10) - RoundCent((f0/(K-(1/Tc)))*((1/K)*(Math.exp(-K*(t-0.01))-1)+Tc*(1-Math.exp(-(t-0.01)/Tc)))))<(d-(dprec+10)))
{adjust=0.01}
	  		RhythmWindow.document.write('<tr bgcolor="'+rowcolor[k++]+'">');
			
			//distance
			d = Math.round(d);
			RhythmWindow.document.write('<td align="right">'+d+'</td>');
			
			//progressive time
	  		RhythmWindow.document.write('<td align="right">'+RoundCent(t+R-adjust)); 
			if(PrintPads(RoundCent(t+R-adjust))==2) {RhythmWindow.document.write('.00');}
			else {if(PrintPads(RoundCent(t+R-adjust))==1) {RhythmWindow.document.write('0');}}
			RhythmWindow.document.write('</td>');
			
			//partial time
			RhythmWindow.document.write('<td align="right">'+RoundCent(t+R-adjust-tprec));
			if(PrintPads(RoundCent(t+R-adjust-tprec))==2) {RhythmWindow.document.write('.00');}
			else {if(PrintPads(RoundCent(t+R-adjust-tprec))==1) {RhythmWindow.document.write('0');}}
			RhythmWindow.document.write('</td></tr>');
			
			if (k>1) k=0;
			dprec=d;
			tprec=RoundCent(t+R-adjust);
			}
	 }
	 
  RhythmWindow.document.write('</table><br>');
  
 RhythmWindow.document.write('<script language="JavaScript" type="text/javascript">function CloseInfo(){self.close()};</script>');
  RhythmWindow.document.write('<form><center><INPUT TYPE="button" NAME="Close" VALUE="Chiudi" onClick="CloseInfo()"></center></form>');

  var today = new Date();
  RhythmWindow.document.write('<hr><center><font size="1">www.atletix.net - Copyright &copy; 1998-'+ today.getFullYear() +', <a href="http://www.tecnick.com" target="newwin">Tecnick.com s.r.l.</a> Tutti i diritti riservati.</font></center>');

  RhythmWindow.document.write('</body></html>');
  
  RhythmWindow.document.close();
	
	}
return;
}

// --------------------------------------------------------------------------
ClearALL();
// --------------------------------------------------------------------------
