/*
 * Questions:
 *     get_adj_age... 5 to 9...?!
 */

 function bull_calc() {
     // Setup
 }
 
 bull_calc.prototype.raise_error = function( message ) {
     if (message === null) message = "Please enter a valid number.";
	 
     document.getElementById('result').innerHTML = message;
 
     return false;
 };
 
 bull_calc.prototype.isNumber = function( test ) {
    return (!isNaN(test) && test !== "");
 };

 bull_calc.prototype._adj_age = function (age) {
     var adjusted = 0;

     if( age < 1004 )
         adjusted = 2;
     else if( age < 1339 )
         adjusted = 3;
     else if( age < 1704 )
         adjusted = 4;
     else if( age < 3562 )
         adjusted = 5; // TODO... 5 to 9; ////// VAGUE SPECIFICATION
     else if( age < 3927 )
         adjusted = 10;
     else if( age < 4293 )
         adjusted = 11;
     else if( age < 4658 )
         adjusted = 12;
     else
         adjusted = 13;

     return adjusted;
 };

 /**
  * Adjusted Birth Weight
  *
  * @param bw  Birth Weight
  * @param age Age
  */
 bull_calc.prototype.adj_birth_weight = function( bw, age ) {
     var weight;
	 
	 if (!this.isNumber(bw) || !this.isNumber(age)) 
	     return this.raise_error("Please enter a valid number");

     return bw * 1 + this._adj_age(age * 1);
 };

 /**
  * 205 Day Wean Weight
  *
  * @param ww    Weaning Weight
  * @param bw    Birth Weight
  * @param w_age Weaning Age
  * @param age   Age
  */
 bull_calc.prototype.ww205 = function( ww, bw, w_age, age ) {
    var adjusted_age = this._adj_age(age);
	
	if (!this.isNumber(ww) || !this.isNumber(bw) || !this.isNumber(w_age) || !this.isNumber(age)) {
	     return this.raise_error("Please enter a valid number");
	}

    return 205 * (ww - bw) / w_age + bw + adjusted_age;
 };

 bull_calc.prototype.yearlingWeight365 = function( yw, ww, bw, w_age, age ) {
     return this._yearlingWeightX(yw, ww, bw, w_age, age, 365);
 };

 bull_calc.prototype.yearlingWeight452 = function( yw, ww, bw, w_age, age ) {
     return this._yearlingWeightX(yw, ww, bw, w_age, age, 452);
 };

 bull_calc.prototype.yearlingWeight550 = function( yw, ww, bw, w_age, age ) {
     return this._yearlingWeightX(yw, ww, bw, w_age, age, 550);
 };

 bull_calc.prototype._yearlingWeightX = function( yw, ww, bw, w_age, age, days ) {
     var ww205 = this.ww205(ww, bw, w_age, age);
	 
	 if (!this.isNumber(yw) || !this.isNumber(ww) || !this.isNumber(bw) || 
	     !this.isNumber(w_age) || !this.isNumber(age) || !this.isNumber(days)) {
		 
	     return this.raise_error("Please enter a valid number");
	 }

     return ((days * 1 - 205) * (yw * 1 - ww * 1)) / (days * 1 - 205) + ww205 * 1;
 };

 bull_calc.prototype.frameScore = function( mht, age, sex ) {
 
     if (!this.isNumber(mht) || !this.isNumber(age)) {
	     return this.raise_error(null);
	 }
 
     if (sex == 'M') { // Bull
         return -11.548 + (0.4878 * mht) - (0.0289 * age) + (0.00001947 * Math.pow(age * 1, 2)) + (0.0000334 * mht * age);
     } else { // Heifer
         return -11.7086 + (0.4723 * mht) - (0.0239 * age) + (0.0000146 * Math.pow(age * 1, 2)) + ( 0.0000759 * mht * age);
     } 
 };

 /*
N = num calves included in the cow's average
C = Avg weaning ratio for all calves the cow has produced

TODO
  */
 bull_calc.prototype.mppa = function (n, c) {
 
     if (!this.isNumber(n) || !this.isNumber(c)) {
	     return this.raise_error(null);
	 }
	 
     return 100 + ( n * 0.4 * (c - 100) ) / ( 1 + ((n - 1) * 0.4) );
 };

 bull_calc.prototype.pelvicArea365 = function(actual_pelvic, age_days, sex) { 
     var constant = sex == 'M' ? .25 : .27;
	 
	 if (!this.isNumber(actual_pelvic) || !this.isNumber(age_days)) {
	     return this.raise_error(null);
	 }

     return actual_pelvic * 1 + (constant * (365-age_days * 1));
 };

 bull_calc.prototype.scrotalCircum365 = function(breed, age, yr_sc) {
     var adj_age_factor = this._getFactor(breed);
	 
	 if (!this.isNumber(age) || !this.isNumber(yr_sc)) 
	     return this.raise_error(null);

     return yr_sc * 1 + ( (365 - age * 1) * adj_age_factor );
 };

 bull_calc.prototype._getFactor = function(breed) {
    switch( breed ) {
        case "angus":
            return 0.0374;
        case "red angus":
            return 0.0324;
        case "brangus":
            return 0.0708;
        case "charolais":
            return 0.0505;
        case "gelbvieh":
            return 0.0505;
        case "hereford":
            return 0.0425;
        case "polled hereford":
            return 0.0305;
        case "limousin":
            return 0.0590;
        case "salers":
            return 0.0574;
        case "simmental":
            return 0.0543;
        default:
            return -1;
    }
 };
 
 bull_calc.prototype.yield_grade = function(adj_fat, percent_fat, carc_weight, ribeye_area) {
     if (!this.isNumber(adj_fat) || !this.isNumber(percent_fat) || !this.isNumber(carc_weight) || !this.isNumber(ribeye_area)) {
	     return this.raise_error(null);
	 }
	 
	 return 2.50 + (2.5 * adj_fat) + (0.2 * percent_fat) + (0.0038 * carc_weight) - (0.32 * ribeye_area);
 }
 
 var calc = new bull_calc();

 /*
 test = new Array();
 test = [1003,1004,1005,1337,1338,1339,1702,1703,1704,3560,3561,3562,3925,3926,3927,4291,4292,4293,4656,4657,4658,4659];

 for (var i = 0; i < test.length; i++) {
     document.write(test[i] + " = " + calc._adj_age(test[i]) + "<br />");
 }*/

// </script>
