// JavaScript Document

var speedMiPerMin  = 0;
var speedKmPerHr  = 0;
var speedMiPerHr  = 0;
var runningTimeMin  = 0;
var runningTimeSec  = 0;
var totalDistance = 0;
var LapDistMi = 0;
var LapDistMeters  = 0;
var LapTimeMin  = 0;
var theone = 0;

function calculateTotalTime() {
		//calculate speeds in mi/min and mi/hr, then display speed in mi/hr and pace in min/mi  
        var totalDistanceMiles = 0;
		var totalDistanceMeters = 0;

		//use the mi/min speed and running time to calculate the total distance traveled
		//then display in the appropriate TextBoxes
		
		if (document.frmTimeDistCalculator.optTotalMilesMeters[1].checked) {
			//alert("optTotalMilesMeters[1]");
			if (document.frmTimeDistCalculator.txtDistanceMeters.value == "")
			  {document.frmTimeDistCalculator.txtDistanceMeters.value = "0";}
			//make sure the put only numbers in the text boxes
			//if (!isNaN(document.frmTimeDistCalculator.txtDistanceMeters.value)) {
				totalDistanceMeters = document.frmTimeDistCalculator.txtDistanceMeters.value;
				totalDistanceMiles = kmToMiles(totalDistanceMeters / 1000);
				document.frmTimeDistCalculator.txtDistanceMi.value = decimalPlaces(totalDistanceMiles,2);
			//}
			//else {
			   //NaNFlag = true;
			//}
		}
		else {
		   //alert("optTotalMilesMeters[0]");
		   if (document.frmTimeDistCalculator.txtDistanceMi.value == "")
			  {document.frmTimeDistCalculator.txtDistanceMi.value = "0";}
		   //make sure the put only numbers in the text boxes
		   //if (!isNaN(document.frmTimeDistCalculator.txtDistanceMi.value)) {
				totalDistanceMiles = document.frmTimeDistCalculator.txtDistanceMi.value;
				totalDistanceMeters = milesToKm(totalDistanceMiles) * 1000;
				document.frmTimeDistCalculator.txtDistanceMeters.value = decimalPlaces(totalDistanceMeters,2);
			//}
			//else {
				//NaNFlag = true;
			//}
		}
		
		if (speedMiPerMin > 0) {
			
			runningTimeMin = totalDistanceMiles / speedMiPerMin;
			var runTimeMin = Math.floor(runningTimeMin);
			var runTimeSec = (runningTimeMin - runTimeMin) * 60;
			document.frmTimeDistCalculator.txtRunTimeMin.value = runTimeMin;			
			document.frmTimeDistCalculator.txtRunTimeSec.value = decimalPlaces(runTimeSec,2);
			
		}
		else {
			document.frmTimeDistCalculator.txtRunTimeMin.value = "Undefined";
		}
		return(0);
}

function calculateTotalDistance() {

		runningTimeMin = clockToDecimal(document.frmTimeDistCalculator.txtRunTimeMin.value, document.frmTimeDistCalculator.txtRunTimeSec.value);
		totalDistance = runningTimeMin * speedMiPerMin;
		document.frmTimeDistCalculator.txtDistanceMi.value = decimalPlaces(totalDistance,2);
		document.frmTimeDistCalculator.txtDistanceMeters.value = decimalPlaces(1000 * milesToKm(totalDistance),2);
		return(0);
}

function doTimeDistCalc() {
		
		//function doit() is the main function for calculations
		
		var NaNFlag = false;  //for flagging non nummeric values
		
		//fill the text boxes with zeros if they are blank
		
		
		document.frmTimeDistCalculator.txtSpeedMiPerHr.value = " ";
		document.frmTimeDistCalculator.txtSpeedKmPerHr.value = " ";
        document.frmTimeDistCalculator.txtPace.value = " ";
		document.frmTimeDistCalculator.txtPaceKm.value = " ";
		document.frmTimeDistCalculator.txtPace400.value = " ";
		
		
		if (document.frmTimeDistCalculator.txtLapTimeMin.value == "") {document.frmTimeDistCalculator.txtLapTimeMin.value = "0";}
		if (document.frmTimeDistCalculator.txtLapTimeSec.value == "") {document.frmTimeDistCalculator.txtLapTimeSec.value = "0";}
		//if (document.frmTimeDistCalculator.txtRunTimeMin.value == "") {document.frmTimeDistCalculator.txtRunTimeMin.value = "0";}
		//if (document.frmTimeDistCalculator.txtRunTimeSec.value == "") {document.frmTimeDistCalculator.txtRunTimeSec.value = "0";}
		//if (document.frmTimeDistCalculator.txtDistanceMi.value == "") {document.frmTimeDistCalculator.txtDistanceMi.value = "0";}
		//if (document.frmTimeDistCalculator.txtDistanceMeters.value == "") {document.frmTimeDistCalculator.txtDistanceMeters.value = "0";}
		//make sure the put only numbers in the text boxes
        if  ((isNaN(document.frmTimeDistCalculator.txtLapTimeMin.value)) ||  
	       	(isNaN(document.frmTimeDistCalculator.txtLapTimeSec.value))) // || 
   	    	//(isNaN(document.frmTimeDistCalculator.txtRunTimeMin.value)) || 
        	//(isNaN(document.frmTimeDistCalculator.txtRunTimeSec.value))) 
	    {
           NaNFlag = true;
        }
		     
        if (document.frmTimeDistCalculator.optDistTime[0].checked) { 
			document.frmTimeDistCalculator.txtDistanceMi.value = " ";
           	document.frmTimeDistCalculator.txtDistanceMeters.value = " ";
			if (document.frmTimeDistCalculator.txtRunTimeMin.value == "") {document.frmTimeDistCalculator.txtRunTimeMin.value = "0";}
			if (document.frmTimeDistCalculator.txtRunTimeSec.value == "") {document.frmTimeDistCalculator.txtRunTimeSec.value = "0";}
			if  ((isNaN(document.frmTimeDistCalculator.txtRunTimeMin.value)) ||  
				(isNaN(document.frmTimeDistCalculator.txtRunTimeSec.value))) 
			{
			   NaNFlag = true;
			}
		}
		else {
			document.frmTimeDistCalculator.txtRunTimeMin.value = " ";
           	document.frmTimeDistCalculator.txtRunTimeSec.value = " ";
			if (document.frmTimeDistCalculator.txtDistanceMi.value == "") {document.frmTimeDistCalculator.txtDistanceMi.value = "0";}
			if (document.frmTimeDistCalculator.txtDistanceMeters.value == "") {document.frmTimeDistCalculator.txtDistanceMeters.value = "0";}
			if (document.frmTimeDistCalculator.optTotalMilesMeters[0].checked) {
				if (isNaN(document.frmTimeDistCalculator.txtDistanceMi.value)) {NaNFlag = true;}
				document.frmTimeDistCalculator.txtDistanceMeters.value = " ";
			}
			else {
				if (isNaN(document.frmTimeDistCalculator.txtDistanceMeters.value)) {NaNFlag = true;}
				document.frmTimeDistCalculator.txtDistanceMi.value = " ";				
			}				
		}
		        
		if (document.frmTimeDistCalculator.optMilesMeters[1].checked) {
            if (document.frmTimeDistCalculator.txtLapDistMeters.value == "")
			  {document.frmTimeDistCalculator.txtLapDistMeters.value = "0";}
            //make sure the put only numbers in the text boxes
			if (!isNaN(document.frmTimeDistCalculator.txtLapDistMeters.value)) {
                LapDistMeters = document.frmTimeDistCalculator.txtLapDistMeters.value;
                LapDistMi = kmToMiles(LapDistMeters / 1000);
                document.frmTimeDistCalculator.txtLapDistMi.value = decimalPlaces(LapDistMi,2);
            }
			else {
               NaNFlag = true;
            }
        }
		else {
           if (document.frmTimeDistCalculator.txtLapDistMi.value == "")
			  {document.frmTimeDistCalculator.txtLapDistMi.value = "0";}
           //make sure the put only numbers in the text boxes
		   if (!isNaN(document.frmTimeDistCalculator.txtLapDistMi.value)) {
                LapDistMi = document.frmTimeDistCalculator.txtLapDistMi.value;
                LapDistMeters = milesToKm(LapDistMi) * 1000;
                document.frmTimeDistCalculator.txtLapDistMeters.value = decimalPlaces(LapDistMeters,2);
			}
         	else {
           		NaNFlag = true;
           	}
       }
		//tell them sarcastically that they have entered a non-numeric character
		if (NaNFlag) {
			myAlert("What kind of number is that?");
		}
		
        //get the lap time in decimal form from the min and sec TextBoxes
       LapTimeMin = clockToDecimal(document.frmTimeDistCalculator.txtLapTimeMin.value, document.frmTimeDistCalculator.txtLapTimeSec.value);
		
				//calculate speeds in mi/min and mi/hr, then display speed in mi/hr and pace in min/mi  
        if ((LapTimeMin <= 0)  || NaNFlag) {

            //the runner is infinitely fast
            document.frmTimeDistCalculator.txtSpeedMiPerHr.value = "Undefined";
			document.frmTimeDistCalculator.txtSpeedKmPerHr.value = "Undefined";
            document.frmTimeDistCalculator.txtPace.value = "Undefined";
			document.frmTimeDistCalculator.txtPaceKm.value = "Undefined";
			document.frmTimeDistCalculator.txtPace400.value = "Undefined";     
        	if (document.frmTimeDistCalculator.optDistTime[0].checked) { 
				document.frmTimeDistCalculator.txtDistanceMi.value = "Undefined";
            	document.frmTimeDistCalculator.txtDistanceMeters.value = "Undefined";
			}
			else {
				document.frmTimeDistCalculator.txtRunTimeMin.value = "Undefined";
            	document.frmTimeDistCalculator.txtRunTimeSec.value = "Undefined";		
			}	
			
		}
		else {
            //calculate the speed
            speedMiPerMin = (LapDistMi / LapTimeMin);
            speedMiPerHr = speedMiPerMin * 60;
			document.frmTimeDistCalculator.txtSpeedMiPerHr.value = decimalPlaces(speedMiPerHr,2); // + " mi/hr";
            document.frmTimeDistCalculator.txtSpeedKmPerHr.value = decimalPlaces(milesToKm(speedMiPerHr),2);
			document.frmTimeDistCalculator.txtPace.value = calcPace(LapTimeMin, LapDistMi);
			document.frmTimeDistCalculator.txtPaceKm.value = calcPace(LapTimeMin, milesToKm(LapDistMi));
			document.frmTimeDistCalculator.txtPace400.value = calcPace(LapTimeMin, 2.5 * milesToKm(LapDistMi));           
			
			
			if (document.frmTimeDistCalculator.optDistTime[0].checked) { 
				calculateTotalDistance();
			}
			else {
				calculateTotalTime();		
			}
		}
		
		
        
        return(0);
}  //end doit()

