var TRUE = 1;
var FALSE = 0;

/******************************************************************************
 format is a generic positive number decimal formatting function.  Give it an
 expression and the desired number of decimal places to the right of the
 decimal place and it will format the result of the expression as a string.
 ******************************************************************************/
function format(expr, decplaces) {
  // Raise expr. vy power of 10 times decplaces; round to an integer, convert
  // to a string.
  var str = "" + Math.round(eval(expr) * Math.pow(10, decplaces));
  
  // Pad small value strings with zeros to the left of rounded number.
  while(str.length <= decplaces) {
    str = "0" + str; 
  }
  
  // Establish location of decimal point.
  var decpoint = str.length - decplaces;
  
  // Assemble final result and return it.
  return str.substring(0, decpoint) + "." + str.substring(decpoint, str.length);
}

/******************************************************************************
 midScoresSum will return the sum of the two middle scores from a list of four
 scores.
 ******************************************************************************/
function midScoresSum(score1, score2, score3, score4) {
  var array = new Array(score1, score2, score3, score4);
  
  bubbleSort(array);
  
  return array[1] + array[2];
}

/******************************************************************************
 getQualifiers places all of the individual qualifiers from the competitors
 array into the qualifiers array.
 ******************************************************************************/
function getQualifiers(competitors, qualifiers) {
	for(var i = 0; i < competitors.length; i++) {
	  // Traverse through entire competitors array.
	  if(competitors[i].indiv) {
	    // The i-th competitor qualified individually for the event; copy her to
	    // the qualifiers array.
	    qualifiers[qualifiers.length] = competitors[i];
	  }
	}
}

/******************************************************************************
 assignPlaces will assign places to competitors.  compArray is an array of
 competitors in descending order by score, then competition order.
 ******************************************************************************/
function assignPlaces(compArray) {
	var place;
  
  if((compArray[0].score == "&nbsp;") || (compArray[0].score == "") || (compArray[0].score == -1)) {
    // Nobody has a score, do nothing.
  }
  else {
    // At least one competitor has scored; assign places.
    for(var i = 0; i < compArray.length; i++) {
      if(i == 0) {
        // place must be 1.
        place = 1;
      }
      else {
        // place must be calculated, in case of ties or no score.
        if((compArray[i].score == "&nbsp;") || (compArray[i].score == "") || (compArray[i].score == -1)) {
        	// Since compArray is ordered, compArray[i] through
        	// compArray[compArray.length - 1] do not have a score.
          // Do not assign anymore numeric places; exit the for loop.
          break;
        }
        else if(compArray[i].score == compArray[i - 1].score) {
        	/*** This if clause entered for program readability. ***/
        	// compArray[i] is tied with compArray[i - 1]; place is
        	// compArray[i - 1].place.
          place = compArray[i - 1].place;
        }
        else if(compArray[i].score != compArray[i - 1].score) {
        	// compArray[i] is not tied with compArray[i - 1]; place is (i + 1).
          place = i + 1;
        }
      }
      compArray[i].place = place;
    }
  }
}

/******************************************************************************
 getFullName will return the full name of the competitor.
 ******************************************************************************/
function getFullName() {
  return this.fName + " " + this.lName;
}

/******************************************************************************
 numToYear will convert a number to the abbreviated high school year.
 ******************************************************************************/
function numToYear(num) {
  switch(num) {
    case 1:
      return "Fr.";
      break;
    case 2:
      return "So.";
      break;
    case 3:
      return "Jr.";
      break;
    case 4:
      return "Sr.";
      break;
    default:
      return "N/A";
      break;
  }
}

/******************************************************************************
 numToYear0 will convert a number to the abbreviated high school year.
 ******************************************************************************/
function numToYear0(num) {
  switch(num) {
    case 0:
      return "Fr.";
      break;
    case 1:
      return "So.";
      break;
    case 2:
      return "Jr.";
      break;
    case 3:
      return "Sr.";
      break;
    default:
      return "N/A";
      break;
  }
}

/******************************************************************************'
 bubbleSort will sort array in the descending order.  array is a simple array.
 ******************************************************************************/
function bubbleSort(array) {
	for(var i = 0; i < (array.length - 1); i++) {
		for(var j = i + 1; j < array.length; j++) {
			if(array[i] < array[j]) {
				var dummy = array[i];
				array[i] = array[j];
				array[j] = dummy;
			}
		}
	}
}

/******************************************************************************
 scoreSort will perform a bubble sort on compArray, sorting by the scores in
 descending order.  compArray is an array of competitor objects.
 ******************************************************************************/
function scoreSort(compArray) {
  for(var i = 0; i < (compArray.length - 1); i++) {
    for(var j = i + 1; j < compArray.length; j++) {
      if(compArray[i].score < compArray[j].score) {
        var dummy = compArray[i];
        compArray[i] = compArray[j];
        compArray[j] = dummy;
      }
    }
  }
}

/******************************************************************************
 inRange returns true if score is within the valid range of gymnastics scores,
 otherwise it returns false.
 ******************************************************************************/
function inRange(score) {
  if((score >= 0) && (score <= 10)) {
    return true;
  }
  else {
    return false;
  }
}

/******************************************************************************
 inBigRange returns true if score is within the valid range of gymnastics
 scores, otherwise it returns false.
 ******************************************************************************/
function inBigRange(score) {
  if((score >= 0) && (score <= 40)) {
    return true;
  }
  else {
    return false;
  }
}

/******************************************************************************
 scorify will return score in the proper format for a gymnastics score, if
 score is in the valid range for scores.
 ******************************************************************************/
function scorify(score) {
  if(inRange(score)) {
  	// The scores are entered and within range.
  	return format(score, 3);
  }
  else {
  	// The scores are out of range.
  	return "&nbsp;";
  }
}

/******************************************************************************
 scorifyBig will return score in the proper format for a gymnastics score, if
 score is in the valid range for scores.
 ******************************************************************************/
function scorifyBig(score) {
  if(inBigRange(score)) {
  	// The scores are entered and within range.
  	return format(score, 3);
  }
  else {
  	// The scores are out of range.
  	return "&nbsp;";
  }
}

/******************************************************************************
 scorifyTeam will return score in the proper format for a gymnastics score.
 ******************************************************************************/
function scorifyTeam(score) {
  if(score >= 0) {
  	// The scores are entered and within range.
  	return format(score, 3);
  }
  else {
  	// The scores are out of range.
  	return "&nbsp;";
  }
}
