// JavaScript Document
//this js is used to populate and prepulate a set of three dependent drop down menus (select nodes) based on user input
//The drop downs can be prepopulated if specific query string values are present in the browser address bar.
//See query String Reader for more info --> that script calls into the function prePopulateDropDowns(), otherwise
//this script will process as normal starting with the window .load even below.
//v1.0 --------------12/11/08 casey stalnaker

addEvent(window, "load", initDropDownMenus);
//force no-cache when returning to page with anonymous funct.
window.onunload = function(){};


var dropDownTargetPath = "productClaimsForm.html";
var dropDownSpecies;
var dropDownStage;
var dropDownNeeds;

//globals for building query string
var selected_species;
var selected_life_stage;
var selected_needs;

//globals for prePopulatingDropDowns (only used from ProductClaimsForm.html
var selected_species_index;
var selected_life_stage_index;
var selected_needs_index;

//globals to reference the arrays we are working with
var targetStageArray = new Array();
var targetNeedsArray = new Array();

//this boolean is used in initDropDownMenus() and is only relevent when this js is used from a page 
//where drop down selections have already been made and redirect has happened.
var prePopulate=false;

//SPECIES
var speciesArray = new Array("Cat", "Dog");
var catLifeStages =  new Array("Kitten", "Adult", "Mature Adult");
var dogLifeStages =  new Array("Puppy", "Adult", "Mature Adult");

var cat_kitten_needs_Array = new Array("Healthy Development / Original","Healthy Development / Ocean Fish & Rice Recipe","Indoor", "Nature's Best Chicken & Brown Rice","Nature's Best  Ocean Fish & Brown Rice");
var cat_kitten_abbrev_Array = new Array("Kitten-Healthy-Development-Original.html","Kitten-Healthy-Development-Ocean-Fish-And-Rice-Recipe.html",	"Kitten-Indoor.html", "Natures-Best-Kitten-Chicken-And-Brown-Rice-Dinner.html", "Natures-Best-Kitten-Ocean-Fish-And-Brown-Rice-Dinner.html");

var cat_adult_needs_Array = new Array("Hairball Control",
					"Hairball Control Light",
					"Light",
					"Indoor",
					"Optimal Care Original",
					"Opitmal Care Ocean Fish",
					"Oral Care",					
					"Sensitive Skin",
					"Sensitive Stomach",
					"Nature's Best Chicken and Brown Rice",
					"Nature's Best Ocean Fish & Brown Rice");

var cat_adult_abbrev_Array = new Array("Feline-Adult-Hairball-Control.html", 
					"Feline-Adult-Hairball-Control-Light.html", 
					"Feline-Adult-Light.html",
					"Feline-Adult-Indoor.html",
					"Feline-Adult-Optimal-Care-Original.html",
					"Feline-Adult-Optimal-Care-Ocean-Fish-And-Rice-Recipe.html",
					"Feline-Adult-Oral-Care.html",
					"Feline-Adult-Sensitive-Skin.html", 
					"Feline-Adult-Sensitive-Stomach.html",
					"Natures-Best-Feline-Adult-Chicken-And-Brown-Rice-Dinner.html",
					"Natures-Best-Feline-Adult-Ocean-Fish-And-Brown-Rice-Dinner.html");

var cat_ma_needs_Array = new Array("Active Longevity  / Original", "Indoor","Hairball Control");
var cat_ma_abbrev_Array = new Array("Feline-Mature-Adult-Active-Longevity-Original.html","Feline-Mature-Adult-Indoor.html","Feline-Mature-Adult-Hairball-Control.html");

var dog_puppy_needs_Array = new Array("Healthy Development / Original","Lamb Meal & Rice Recipe","Large Breed","Lamb Meal & Rice Recipe Large Breed", "Small Bites", "Nature's Best Chicken and Brown Rice", "Nature's Best Lamb & Brown Rice");
var dog_puppy_abbrev_Array = new Array("Puppy-Healthy-Development-Original.html", "Puppy-Lamb-Meal-And-Rice-Recipe.html","Puppy-Large-Breed.html","Puppy-Lamb-Meal-And-Rice-Recipe-Large-Breed.html","Puppy-Small-Bites.html", "Natures-Best-Puppy-Chicken-And-Brown-Rice-Dinner.html", "Natures-Best-Puppy-Lamb-And-Brown-Rice-Dinner.html" );

var dog_adult_needs_Array = new Array("Advanced Fitness / Original",
					   "Lamb Meal & Rice Recipe",
					   "Large Breed",
					   "Lamb Meal & Rice Recipe / Large Breed",
					   "Small Bites",
					   "Lamb Meal & Rice Recipe / Small Bites",
					   "Light",
					   "Light Large Breed",
					   "Light Small Bites",
					   "Oral Care",
					   "Sensitive Skin",
					   "Sensitive Stomach",
					   "Nature's Best Chicken & Brown Rice",
					   "Nature's Best Lamb & Brown Rice",
					   "Nature's Best Chicken & Brown Rice Small Bites",
					   "Nature's Best Lamb & Brown Rice Small Bites");
var dog_adult_abbrev_Array = new Array("Canine-Adult-Advanced-Fitness-Original.html",
					   "Canine-Adult-Lamb-Meal-And-Rice-Recipe.html",
					   "Canine-Adult-Large-Breed.html",
					   "Canine-Adult-Lamb-Meal-And-Rice-Recipe-Large-Breed.html",
					   "Canine-Adult-Small-Bites.html",
					   "Canine-Adult-Lamb-Meal-And-Rice-Recipe-Small-Bites.html",
					   "Canine-Adult-Light.html",
					   "Canine-Adult-Light-Large-Breed.html",
					   "Canine-Adult-Light-Small-Bites.html",
					   "Canine-Adult-Oral-Care.html",
					   "Canine-Adult-Sensitive-Skin.html",
					   "Canine-Adult-Sensitive-Stomach.html",
					   "Natures-Best-Canine-Adult-Chicken-And-Brown-Rice-Dinner.html",
					   "Natures-Best-Canine-Adult-Lamb-And-Brown-Rice-Dinner.html",
					   "Natures-Best-Canine-Adult-Chicken-And-Brown-Rice-Small-Bites.html",
					   "Natures-Best-Adult-Lamb-And-Brown-Rice-Small-Bites.html");

var dog_ma_needs_Array = new Array("Active Longevity / Original","Large Breed",	"Small Bites");
var dog_ma_abbrev_Array = new Array("Canine-Mature-Adult-Active-Longevity-Original.html","Canine-Mature-Adult-Large-Breed.html","Canine-Mature-Adult-Small-Bites.html");

function clearDropDownMenu(menuObj)
{
	var myMenuObjLength = menuObj.length;
	for(var r=myMenuObjLength-1; r>=1; r--)
	{	
		menuObj.remove(r);
	}
	menuObj.selectedIndex = 0;
	menuObj.disabled = true;
}

function setSpecies(targ,selObj,restore)
{
	//hide needs when switching species
	clearDropDownMenu(dropDownStage);
	clearDropDownMenu(dropDownNeeds);
	selected_species_index = selObj.selectedIndex;
	//the below line breaks IE
	//populateStageDropDown(selObj.options[selected_species_index].value);
	//so....do it this way: reference that data you need in the appropriate array, based on selected index of the dropdown, NOT the VALUE of the dropdown
	var arrayValue = speciesArray[selected_species_index-1];
	populateStageDropDown(arrayValue);
}

function setStage(targ,selObj,restore)
{
	clearDropDownMenu(dropDownNeeds);
	//dropDownNeeds.selectedIndex = 0;
	selected_life_stage_index = selObj.selectedIndex;
	var arrayValue = targetStageArray[selected_life_stage_index-1]
	populateNeedsDropDown(arrayValue);
}

function populateStageDropDown(whatSpecies)
{
	//alert('populateStageDropDown');
	selected_species = whatSpecies.toLowerCase() + "_";
	dropDownStage.disabled = false;	
	//destroy 
	targetStageArray = null;
	//alert(selected_species);
	switch(selected_species)
	{
		case "cat_" :
			targetStageArray = catLifeStages;
		break;
		case "dog_" :
			targetStageArray = dogLifeStages;
		break;
		default:
			alert("no species found :"+ selected_species );
		break;
	}
	
	for(var s=1; s<=targetStageArray.length; s++)
	{	
		dropDownStage.options[ s ] = new Option( targetStageArray[s-1], targetStageArray[s-1]);
	}
}

function setNeeds(targ,selObj,restore)
{
	selected_needs_index = selObj.selectedIndex;
	
    var myAbbrevString = String(selected_species + selected_life_stage + "abbrev_Array");
	var myNeedsString = String(myAbbrevString.replace(/abbrev/, "needs"));
	//should return an array
	var myAbbrevArray = eval(myAbbrevString);
	var myNeedsArray = eval(myNeedsString);
	var needsLength = myNeedsArray.length;
	
	var arrayValue = myNeedsArray[selected_needs_index -1]
	selected_needs = arrayValue;
	
	//switch
	for(var n=0; n<needsLength; n++)
	{
		if(myNeedsArray[n] == selected_needs)
		{
		//switch to abbreviated need and append to query string
		selected_needs = myAbbrevArray[n];
		redirectWithQueryString();
		return;
		}
	}
}

function populateNeedsDropDown(whatLifeStage)
{
	//shift to lower
	selected_life_stage = whatLifeStage.toLowerCase() + "_";
	//destroy 
	targetNeedsArray = null;
	
	switch(selected_species)
	{
		case "cat_":
			switch(selected_life_stage)
			{
				case "kitten_" :
					targetNeedsArray  = cat_kitten_needs_Array;
				break;
				case "adult_" :
					targetNeedsArray  = cat_adult_needs_Array;
				break;
				case "mature adult_" :
					targetNeedsArray  = cat_ma_needs_Array;
					//rewrite the selected_life_stage global, replacing space with _
					selected_life_stage = "ma_";
				break;
				case "ma_" :
					targetNeedsArray  = cat_ma_needs_Array;
				break;
				default:
				alert("populateNeedsDropDown() --> could not locate CAT lifeStage! : value = "+selected_life_stage);;
			}
		
		break;
		case   "dog_":
			switch(selected_life_stage)
			{
				case "puppy_" :
					targetNeedsArray  = dog_puppy_needs_Array;
				break;
				case "adult_" :
					targetNeedsArray  = dog_adult_needs_Array;
				break;
				case "mature adult_" :
					targetNeedsArray  = dog_ma_needs_Array;
					//rewrite the selected_life_stage global, replacing space with _
					selected_life_stage = "ma_";
				break;
				case "ma_" :
					targetNeedsArray  = dog_ma_needs_Array;
				break;
				default:
				alert("populateNeedsDropDown() --> could not locate DOG lifeStage! : value = "+selected_life_stage);
			}
		break;
		default:
		alert("selected_species not found");
	}
	for(var s=1; s<=targetNeedsArray.length; s++)
	{	
		
		dropDownNeeds.options[ s ] = new Option(targetNeedsArray[s-1], targetNeedsArray[s-1]);
	}
	//show dropdown
	dropDownNeeds.disabled = false;
}

function redirectWithQueryString()
{
	var queryString = String(selected_needs);
	try
	{
		//alert(myAbbrevString);
		//window.location = String(myAbbrevString);
		window.location = String(queryString);
	}
	catch (e)
	{
		alert("dropDownData.js || redirectWithQueryString() - Redirect Error : \n" + e);
	}
}

function initDropDownMenus()
{
	//alert('initDropDownMenus');
	dropDownSpecies =  document.food_form.species;
	dropDownStage = document.food_form.stage;
	dropDownNeeds = document.food_form.needs;
	
	dropDownSpecies.options[0] = new Option("Please Select Species", "Select Species");	
	dropDownSpecies.options[0].disabled = "disabled";
	dropDownNeeds.options[0] = new Option("Please Select A Special Need", "Select Needs");	
	dropDownStage.options[0] = new Option("Please Select Life Stage", "Life Stage");
	
	if(prePopulate)
	{
		//open up dropdown values and get ready to prepopuluate with Query String values
		dropDownSpecies.disabled = false;
		dropDownStage.disabled = false;
		dropDownNeeds.disabled = false;
		//alert('prepopulate inside init');
	}
	else
	{	
		//disable all but the first dropdown and get ready for interaction
		dropDownSpecies.selectedIndex = 0;
		dropDownSpecies.disabled = false;
		clearDropDownMenu(dropDownStage);
		clearDropDownMenu(dropDownNeeds);
	}		

}
	
	
	
//$(document).ready(function(){	
function prePopulateDropDowns()
{
	//alert('prepopulate');
	prePopulate = true;
	initDropDownMenus();
	
	selected_species = document.getElementById('current_species').value;
	selected_life_stage = document.getElementById('current_lifestage').value;
	//need to strip off trailing _ after round trip from the queryStringReader script to this script 
	//selected_species = selected_species.substring(0, selected_species.lastIndexOf("_"));
	//selected_life_stage = selected_life_stage.substring(0, selected_life_stage.lastIndexOf("_"));
	//alert(selected_species);
	//var selected_species = document.getElementsById('current_species');
	//var selected_life_stage = document.getElementsById('current_lifestage');
	
	
	
	populateStageDropDown(selected_species);
	populateNeedsDropDown(selected_life_stage);

	var myAbbrevString = String(selected_species + selected_life_stage + "abbrev_Array");
	var myAbbrevNeedsArray = eval(myAbbrevString);
	var speciesArrayLength = speciesArray.length;
	var stageArrayLength = targetStageArray.length;
	
	
	
	var needsArrayLength = targetNeedsArray.length;

	for(var s=0; s<speciesArrayLength; s++)
	{
		if(String(speciesArray[s].toLowerCase() + "_") == selected_species.toLowerCase())
		{
			selected_species_index = s+1;
			continue;
		}
	}

	for(var l=0; l<stageArrayLength; l++)
	{
		if(String(targetStageArray[l].toLowerCase() + "_") == selected_life_stage.toLowerCase())  
		{
			selected_life_stage_index = l+1;
			continue;
		}
		//special exception - cant simply match with lowerCase shift b/c of compound word
		else if(selected_life_stage == "ma_")
		{	
			if(targetStageArray[l] == "Mature Adult")
			{
				selected_life_stage_index = l+1;
				continue;
			}
		}
	}
	
	
	
	
	var selected_needsURL = (document.location.href);
	lastSlash = selected_needsURL.lastIndexOf("/")+1;
	lastChar = selected_needsURL.length;
	//alert('about to loop');
	selected_needs = "";
	for (w=lastSlash;w<lastChar;w++)
		{
			selected_needs = selected_needs + (selected_needsURL.charAt(w));
			//alert('loop');
		}
	//selected_needs = selected_needsURL.lastIndexOf("/");
		
	//alert(selected_needsURL);
	//alert(lastSlash);
	//alert(lastChar);
	//alert(selected_needs);
	
	
	
	
	
	for(var n=0; n<needsArrayLength; n++)
	{	
		if(myAbbrevNeedsArray[n] == selected_needs)
		{
			selected_needs_index = n+1;
			continue;
		}
	}

	dropDownSpecies.selectedIndex = selected_species_index;
	dropDownStage.selectedIndex =  selected_life_stage_index;
	dropDownNeeds.selectedIndex = selected_needs_index;
//});
 }

function addEvent(elm, evType, fn, useCapture)
// addEvent and removeEvent
{
	if (elm.addEventListener){
		elm.addEventListener(evType, fn, useCapture);
		return true;
	} else if (elm.attachEvent){
		var r = elm.attachEvent("on"+evType, fn);
		return r;
	} else {
		//alert("Handler could not be removed");
	}
} 

//$(document).ready(function(){
	//		prePopulateDropDowns()			   
		//				   });
