// v.1.0.6

function changed_child_age(){
  age = $("age").value;
  $("child_age").value = age;
  update_formula_amount_options(age);
  update_milk_amount_options(age);
  update_grain_amount_options(age);
  update_veg_amount_options(age);
  update_fruit_amount_options(age);
  update_meat_amount_options(age);
  update_water_amount_options(age);
  $('submission_buton').setStyle({display: "inline"});
}

zero_to_ten_oz_options= '<option value="0">select</option><option value="0">0 oz</option> <option value="1">1 oz</option> <option value="2">2 oz</option> <option value="3">3 oz</option> <option value="4">4 oz</option> <option value="5">5 oz</option> <option value="6">6 oz</option> <option value="7">7 oz</option> <option value="8">8 oz</option> <option value="9">9 oz</option> <option value="9">10 oz</option> '
zero_to_ten_tbsp_options= '<option value="0">select</option><option value="0">0 tbsp</option> <option value="1">1 tbsp</option> <option value="2">2 tbsp</option> <option value="3">3 tbsp</option> <option value="4">4 tbsp</option> <option value="5">5 tbsp</option> <option value="6">6 tbsp</option> <option value="7">7 tbsp</option> <option value="8">8 tbsp</option> <option value="9">9 tbsp</option> <option value="9">10 tbsp</option> '
zero_to_ten_servings_options='<option value="0">select</option><option value="0">0 servings</option> <option value="1">1 serving</option> <option value="2">2 servings</option> <option value="3">3 servings</option> <option value="4">4 servings</option> <option value="5">5 servings</option> <option value="6">6 servings</option> <option value="7">7 servings</option> <option value="8">8 servings</option> <option value="9">9 servings</option> <option value="9">10 servings</option> '

function update_formula_amount_options(age){
  field_name= "formula_amount" 
  options= $(field_name)
  options.replace(' \
    <select name="'+field_name+'" id="'+field_name+'"> \
    	<option value="0">select</option> \
      <option value="15">0 - 15 oz</option> \
      <option value="16">16 oz</option> \
      <option value="17">17 oz</option> \
      <option value="18">18 oz</option> \
      <option value="19">19 oz</option> \
      <option value="20">20 oz</option> \
      <option value="21">21 oz</option> \
      <option value="22">22 oz</option> \
      <option value="23">23 oz</option> \
      <option value="24">24 oz</option> \
      <option value="25">25 oz</option> \
      <option value="26">26 oz</option> \
      <option value="27">27 oz</option> \
      <option value="28">28 oz</option> \
      <option value="29">29 oz</option> \
      <option value="30">30 oz</option> \
      <option value="31">31 oz</option> \
      <option value="32">32 oz</option> \
      <option value="33">33 oz</option> \
      <option value="34">34 oz</option> \
      <option value="35">35 oz</option> \
    </select>');
}
function update_milk_amount_options(age){
  field_name= "milk_amount" 
  options= $(field_name)
  options.replace('<select name="'+field_name+'" id="'+field_name+'">' + zero_to_ten_servings_options + '</select>');
}
function update_grain_amount_options(age){
  field_name= "grain_amount" 
  options= $(field_name)
  if(age < 12){
    options.replace('<select name="'+field_name+'" id="'+field_name+'">' + zero_to_ten_tbsp_options + '</select>');
  } else {
    options.replace('<select name="'+field_name+'" id="'+field_name+'">' + zero_to_ten_servings_options + '</select>');
  }
}
function update_veg_amount_options(age){
  field_name= "veg_amount" 
  options= $(field_name)
  if(age < 12){
    options.replace('<select name="'+field_name+'" id="'+field_name+'">' + zero_to_ten_tbsp_options + '</select>');
  } else {
    options.replace('<select name="'+field_name+'" id="'+field_name+'">' + zero_to_ten_servings_options + '</select>');
  }
}
function update_fruit_amount_options(age){
  field_name= "fruit_amount" 
  options= $(field_name)
  if(age < 12){
    options.replace('<select name="'+field_name+'" id="'+field_name+'">' + zero_to_ten_tbsp_options + '</select>');
  } else {
    options.replace('<select name="'+field_name+'" id="'+field_name+'">' + zero_to_ten_servings_options + '</select>');
  }
}
function update_meat_amount_options(age){
  field_name= "meat_amount" 
  options= $(field_name)
  if(age < 12){
    options.replace('<select name="'+field_name+'" id="'+field_name+'">' + zero_to_ten_tbsp_options + '</select>');
  } else {
    options.replace('<select name="'+field_name+'" id="'+field_name+'">' + zero_to_ten_servings_options + '</select>');
  }

}
function update_water_amount_options(age){
  field_name= "water_amount"
  options= $(field_name)
  options= $(field_name)
  options.replace('<select name="'+field_name+'" id="'+field_name+'">' + zero_to_ten_oz_options + '</select>');  
//  if(age > 3){
//    options.replace('<select name="'+field_name+'" id="'+field_name+'">' + zero_to_ten_oz_options + '</select>');
//  } else {
//    options.replace('<select name="'+field_name+'" id="'+field_name+'"><option value="0">0 oz</option></select>');
//  }
  
}

function analyse_child_nutrition(){
  // location in servings array 
  formula= 0;
  milk   = 1;
  grain  = 2;
  veg    = 3;
  fruit  = 4;
  meat   = 5;
  water  = 6;
  
  var servings=new Object();
  //           formula, milk, grain, veg, fruit, meat, water
  servings['0'] = ["21+", "0", "0",  "0",  "0",  "0",  "0" ]
  servings['4'] = ["24+", "0", "3+", "0",  "0",  "0",  "4+"]
  servings['6'] = ["24+", "0", "4+", "1+", "1+", "0",  "4+"]
  servings['8'] = ["24+", "0", "4+", "4+", "4+", "1+", "4+"]
  servings['10']= ["20+", "1+", "4+", "4+", "8+", "2+", "4+"]
  servings['12']= ["0+", "2+","6+", "3+", "2+", "2+", "4+"]

  set= servings[$("child_age").value]
  analyse_formula_amount( set[formula], "formula" );
  analyse_milk_amount( set[milk], "milk" );
  analyse_grain_amount( set[grain], "grain" );
  analyse_veg_amount( set[veg], "veg" );
  analyse_fruit_amount( set[fruit], "fruit" );
  analyse_meat_amount( set[meat], "meat" );
  analyse_water_amount( set[water], "water" );
  
  // remove submission button
  $("submission_buton").remove();
  $("submission_again_buton").setStyle({display: "inline"});
  
  
  // remove age selector
  $("subTitle").replace('\
  <div id="subTitle">\
    <h3>Ok, now see where Baby does well, and where she may need extra helpings.</h3>\
    <p>Then download our <a href="http://www.babyzone.com/baby/nutrition-chart.pdf">month-by-month nutrition chart.</a></p>\
  </div>')
  
  window.scrollTo(0,0);
}

function analyse_amount_vs_serving(amount,serving){ 
  // alert(amount+" <=> "+serving)
  if(serving.endsWith('+')){
    serving= serving.gsub(/\+/, '')
    if(amount < serving) { // too little
      return -1
    } else if(amount >= serving) { // just right
      return 0
    }
  } else {
    if(amount < serving) { // too little
      return -1
    } else if(amount == serving) { // just right
      return 0
    } else if(amount > serving) { // too much
      return 1
    }
  }
}


function analyse_amount(servings, fieldname, not_enough_msg, enough_msg, too_much_msg){
  switch( analyse_amount_vs_serving($(fieldname+"_amount").value, servings) ) {
  case -1:
    $(fieldname+"_title").replace('<span class="wrong" id="'+fieldname+'_title"> = NOT ENOUGH</span>')
    $(fieldname+"_text").replace('<p id="'+fieldname+'_text">'+ not_enough_msg +'</p>')
    break;
  case 0:
    $(fieldname+"_title").replace('<span class="good" id="'+fieldname+'_title"> = ENOUGH</span>')
    $(fieldname+"_text").replace('<p id="'+fieldname+'_text">'+ enough_msg +'</p>')
    break;
  case 1:
    $(fieldname+"_title").replace('<span class="wrong" id="'+fieldname+'_title"> = TOO MUCH</span>')
    $(fieldname+"_text").replace('<p id="'+fieldname+'_text">'+ too_much_msg +'</p>')
    break;
  }
}

function analyse_formula_amount(servings, fieldname){
  analyse_amount(servings, fieldname, 
    'Keep Baby drinking! Your child relies on this liquid nutrition right now. Read on to learn how to maximize breast or bottlefeeding sessions.', 
    'Yes! Your child is receiving the adequate amount of formula or breast milk necessary to continue growing healthily. Check out our feeding guides for even more pointers!',
    'too_much' )
  $(fieldname+"_class").addClassName('leftBoxExtended')
  $(fieldname+"_box").replace('\
  <ul>\
		<li><a href="http://www.babyzone.com/baby/feeding_nutrition/breastfeeding ">Breastfeeding Guide</a></li>\
		<li><a href="http://www.babyzone.com/baby/feeding_nutrition/formula">Formula Feeding 101</a></li> <br/>\
		<li class="liLast"><a href="http://www.babyzone.com/baby/feeding_nutrition/a2606/1#bm2"><strong>Breast Milk &amp; Formula Breakdown</strong></a></li>\
	</ul>')
}

function analyse_milk_amount(servings, fieldname){
  analyse_amount(servings, fieldname, 
    "Keep that milk coming! Besides breast milk or formula, whole cow's milk provides calcium and vitamins for your growing child. See why in these feeding guides.", 
    "Right on! Looks like you know that babies can start eating dairy by 10 months, drinking cow's milk after their first birthday, and relying on the calcium and vitamins in it throughout toddlerhood. Keep up the good work!",
    "Oops! Before 10 months, the only liquids babies need to thrive are water and breast milk or formula. Learn why in these feeding guides." )
  $(fieldname+"_class").addClassName('leftBoxExtended')
  $(fieldname+"_box").replace('\
	<ul>\
		<li><a href="http://www.babyzone.com/baby/feeding_nutrition/formula/a2463">When to Switch to Cow\'s Milk</a></li>\
		<li><a href="http://www.babyzone.com/toddler/nutrition_first_foods/a2544">Wait to Offer Skim Milk!</a></li><br/>\
		<li class="liLast"><a href="http://www.babyzone.com/baby/feeding_nutrition/a2606/2#bm4"><strong>Cow\'s Milk Requirements</strong></a></li>\
	</ul>')
}

function analyse_grain_amount(servings, fieldname){
  analyse_amount(servings, fieldname, 
    'Well, you\'re close: Offer Baby a few more grains in his or her diet to maintain energy levels. Stay on top of Baby\'s needs below.', 
    'Excellent! Your baby is receiving the right amount of cereal and grains to maintain his or her energy levels. Read on for more feeding tips.',
    'Uh-oh! Introducing complex solids at six months of age, rather than much earlier, may help to reduce your child\'s risk of later food allergies. Read on to learn more.' )
  $(fieldname+"_class").addClassName('leftBoxExtended')
  $(fieldname+"_box").replace('\
	<ul>\
		<li><a href="http://www.babyzone.com/quizzes/quiz.asp?quizid=98">Quiz: Is Baby Ready for Solids?</a></li>\
		<li><a href="http://www.babyzone.com/baby/feeding_nutrition/solids">When &amp; How to Introduce Solid Foods  </a></li><br/>\
		<li class="liLast"><a href="http://www.babyzone.com/baby/feeding_nutrition/a2606/2#bm6"><strong>Cereal &amp; Grain Specifics by Age</strong></a></li>\
	</ul>')
}

function analyse_veg_amount(servings, fieldname){
  analyse_amount(servings, fieldname, 
    'Almost there! Keep offering Baby an array of veggies, including a variety of colors and textures. Learn why and how in these feeding guides.', 
    'Way to go: Your baby is receiving enough veggies to keep his or her growth and health on track. Read on for more feeding tips.',
    'Whoops! Introducing complex solids at six months of age, rather than much earlier, may help to reduce your child\'s risk of later food allergies. Learn more in these feeding guides.' )
  $(fieldname+"_class").addClassName('leftBoxExtended')
  $(fieldname+"_box").replace('\
	<ul>\
		<li><a href="http://www.babyzone.com/baby/feeding_nutrition/a1441">Introducing Veggies (and Other First Solids)</a></li>\
		<li><a href="http://www.babyzone.com/mom_dad/home_food/cooking_entertaining/a1541">Kids and Veggies: A Better Way</a></li><br/>\
		<li class="liLast"><a href="http://www.babyzone.com/baby/feeding_nutrition/a2606/3#bm8"><strong>Veggie Necessities, Month by Month</strong></a></li>\
	</ul>')
}

function analyse_fruit_amount(servings, fieldname){
  analyse_amount(servings, fieldname, 
    'So close! Keep offering Baby an array of yummy fruit, including a variety of colors and textures. Our food tips and tricks below can help!', 
    'Awesome! Baby sure is getting enough fruit in his or her daily diet, and you\'re to thank! Check out these other feeding tips.',
    'Oops! Introducing complex solids at six months of age, rather than much earlier, may help to reduce your child\'s risk of later food allergies. Read on to learn more.' )
  $(fieldname+"_class").addClassName('leftBoxExtended')
  $(fieldname+"_box").replace('\
	<ul>\
		<li><a href="http://www.babyzone.com/baby/feeding_nutrition/a2451">What to Put in That Sippy Cup</a></li>\
		<li><a href="http://www.babyzone.com/askanexpert/answer.asp?qid=112">Fruit Juice in Toddler\'s Diet? </a></li><br/>\
		<li class="liLast"><a href="http://www.babyzone.com/baby/feeding_nutrition/a2606/4#bm10"><strong>Fruit Amounts, Explained</strong></a></li>\
	</ul>')
}

function analyse_meat_amount(servings, fieldname){
  analyse_amount(servings, fieldname, 
    'A few more servings of protein will do your child loads of good: Remember, protein is what helps develop muscle for your growing baby. Below, learn about how much to offer.', 
    'Yes! You\'re offering your baby the right amount of protein for his or her growing body. Learn even more smart feeding hints below.',
    'Uh-oh! Introducing complex solids at six months of age, rather than much earlier, may help to reduce your child\'s risk of later food allergies. Learn more in these feeding guides.' )
  $(fieldname+"_class").addClassName('leftBoxExtended')
  $(fieldname+"_box").replace('\
	<ul>\
		<li><a href="http://parentzone.babyzone.com/recipes/Recipe.asp?Recipeid=10409&title=Baby\'s%20Meat%20and%20Potatoes">Recipe: Baby\'s Meat & Potatoes</a></li>\
		<li><a href="http://www.babyzone.com/mom_dad/home_food/a1496">Raising Vegetarian Babies</a></li><br/>\
		<li class="liLast"><a href="http://www.babyzone.com/baby/feeding_nutrition/a2606/5#bm12"><strong>Meat & Protein Needs by Age</strong></a></li>\
	</ul>')
}

function analyse_water_amount(servings, fieldname){
  analyse_amount(servings, fieldname, 
    'Keep that water coming! Even at this young age, Baby needs regular drinks of water. See below to learn how much your child needs.', 
    'Right on! Looks like your baby is getting properly hydrated, which is so important for his or her health. Learn more about why water is great for Baby below.',
    'Whoops! Babies under four months old don\'t need to drink water, as their bodies benefit most from breast milk or formula right now. Learn more about your child\'s water needs below.')
  $(fieldname+"_class").addClassName('leftBoxExtended')
  $(fieldname+"_box").replace('\
  <ul>\
		<li><a href="http://www.babyzone.com/baby_toddler_preschooler_health/photos_top_health_worries/4">When Can You Offer Baby Water?</a></li>\
		<li><a href="http://www.babyzone.com/askanexpert/answer.asp?qid=15746">Newborns\' Water Intake: What to Know</a></li><br/>\
		<li class="liLast"><a href="http://www.babyzone.com/baby/feeding_nutrition/a2606/6#bm14"><strong>Water Requirements, Explained</strong></a></li>\
	</ul>')
}


document.observe('dom:loaded', function(){
	//if we've got a postBack, show the results
	if($('postBack'))
	{
		analyse_child_nutrition();
	}

	else
	{
	  $('age').replace('\
	  <select id="age" onchange="changed_child_age();"> \
			<option selected>select</option> \
			<option value="0">0 to 4 months</option> \
			<option value="4">4 to 6 months</option> \
			<option value="6">6 to 8 months</option> \
			<option value="8">8 to 10 months</option> \
			<option value="10">10 to 12 months</option> \
			<option value="12">12 to 24 months</option> \
		</select>')
	}	

})