var field;
var list;
var init = false;///Have the ArrayList and Field variables been initialized?

function SaveAll(){
	this.Initialize();
	var parent = document.getElementById('bev_month_table');///GET THE TBODY REF
	var children = parent.getElementsByTagName('input');
	//for(var p = 0; p<children; p++){
		for(var i = 0; i<children.length; i++){
			var child = children[i];
			if(child.className == 'cat_check'){
				if(child.checked){
					this.AddSelection(child);
				}
				else{
					this.HideSelector(child);
				}
			}
		}
	//}
	this.Save();
} 

/***********************************************************
This method is called when a checkbox is checked/unchecked 
from within the server control.
***********************************************************/
function SelectionChanged(element){
	this.SaveAll();
	/*if(!init)
		this.Initialize();
	var questionID = this.GetQuestionID(element);
	var drinkID = this.GetDrinkID(element);
	var uid = this.GetUID(element);
	if(element.checked == false){
		this.RemoveSelection(element);
		this.Save();
		return;
	}	
	if(!this.AllowSelection(element)){
		element.checked = false;
		return;
	}	
	this.AddSelection(element);
	this.Save();
	*/
}
/***********************************************************
This method is called by the dropdown elements when a beverage 
type is chosen. The current selection within the arraylist is
updated (simply by being removed and added again).
***********************************************************/
function UpdateSelection(element){
		this.SaveAll();
		/*this.RemoveSelection(element);
		this.AddSelection(element);
		this.Save();
		*/
}
/***********************************************************
Return the type of drink the question refers to.
***********************************************************/
function GetDrinkType(questionID){
	if(questionID == '1' || questionID == '6')
		return 'beverage';	
	return 'brand';
}
/***********************************************************
Remove the current selection from the collection and
reset the selector to be hidden.
***********************************************************/
function RemoveSelection(element){
	var questionID = this.GetQuestionID(element);
	var drinkID = this.GetDrinkID(element);
	var uid = this.GetUID(element);
	this.list.RemoveItemByID(uid);
	var selector = this.GetSelector(element);
	if(selector != null)
		selector.style.display = 'none';
}
/***********************************************************
Add the current selection to the collection and
set the selector to be visible.
***********************************************************/
function AddSelection(element){
	var questionID = this.GetQuestionID(element);
	var drinkID = this.GetDrinkID(element);
	var uid = this.GetUID(element);	
	var selector = this.GetSelector(element);
	var currentSelection = '';
	if(selector != null)
		currentSelection = selector.options[selector.selectedIndex].text;			
	Write(currentSelection);
	var p = new Property(questionID,drinkID,currentSelection,GetDrinkType(questionID));
	this.list.AddItem(p);
	if(selector != null)
		this.GetSelector(element).style.display = 'block';
}
/***********************************************************
Return the dropdown drink type element related to the currently
selected element.
***********************************************************/
function GetSelector(element){
	var questionID = this.GetQuestionID(element);
	var drinkID = this.GetDrinkID(element);
	var selectElement = document.getElementById('q_'+questionID+'_s_'+drinkID);
	return selectElement;
}
/***********************************************************
Return the dropdown drink type element related to the currently
selected element, and set it to hidden if it exists;
***********************************************************/
function HideSelector(element){
	var questionID = this.GetQuestionID(element);
	var drinkID = this.GetDrinkID(element);
	var selectElement = document.getElementById('q_'+questionID+'_s_'+drinkID);
	if(selectElement != null)
		selectElement.style.display = 'none';
}
/***********************************************************
Return the checkbox element related to the currently
selected dropdown.
***********************************************************/
function GetCheckbox(element){
	var questionID = this.GetQuestionID(element);
	var drinkID = this.GetDrinkID(element);
	var checkElement = document.getElementById('q_'+questionID+'_b_'+drinkID);
	return checkElement;
}
/***********************************************************
This method checks the currently selected items and returns
a boolean value specifying whether or not the selection can be
made. This is because only one question allows for multiple answers.
***********************************************************/
function AllowSelection(element){
	var questionID = this.GetQuestionID(element);
	var drinkID = this.GetDrinkID(element);
	var uid = this.GetUID(element);
	if(questionID != '2' && questionID != '4' && questionID != '1')
		return true;
	var items = this.list.Items();
	var count = items.length;	
	for(var i = 0; i<count; i++){
		if(items[i].QuestionID == questionID)
			return false;
	}
	return true;
}
/***********************************************************
This method parses the specific questionID from the ID 
attribute of a given element.
***********************************************************/
function GetQuestionID(element){
	var id = element.id;
	var split = id.split('_');
	return split[1];	
}
/***********************************************************
This method parses the specific drinkID from the ID 
attribute of a given element.
***********************************************************/
function GetDrinkID(element){
	var id = element.id;
	var split = id.split('_');
	return split[split.length-1];	
}
/***********************************************************
This method parses the uniqueID (aka UID) from the ID 
attribute of a given element. This is done by combining the
questionID and drinkID.
***********************************************************/
function GetUID(element){
	var questionID = this.GetQuestionID(element);
	var drinkID = this.GetDrinkID(element);
	return questionID+':'+drinkID;
}
function SetSelected(questionID,drinkID,type){
/*	if(!init)
		this.Initialize();
	var uid = questionID+':'+drinkID;
	Write(uid);
	if(type != "" && type != null){
		var selectElement = document.getElementById('q_'+questionID+'_s_'+drinkID);
		for(var i = 0; i<selectElement.length; i++){
			if(selectElement.options[i].text == type){
				selectElement.options[i].selected = true;
				break;
			}
		}
	}
	var checkElement = document.getElementById('q_'+questionID+'_b_'+drinkID);
	if(checkElement != null){
		checkElement.checked = true;
		this.AddSelection(checkElement);	
	}
	
	this.Save();
	*/
	var checkElement = document.getElementById('q_'+questionID+'_b_'+drinkID);
	checkElement.checked = true;
	var selector = this.GetSelector(checkElement);
	if(selector == null)
		return;
	selector.style.display = 'block';
	if(type != "" && type != null){		
		for(var i = 0; i<selector.length; i++){
			if(selector.options[i].text == type){
				selector.options[i].selected = true;
				break;
			}
		}
	}
	
}
/***********************************************************
Initialize the variables needed for this script
***********************************************************/
function Initialize(){
	field = document.getElementById('selected_fields');
	list = new ArrayList();
	init = true;	
}
/***********************************************************
Save the selections to the hidden input field
***********************************************************/
function Save(){
	var string = '';
	var items = this.list.Items();
	var count = items.length;
	for(var i = 0; i<count; i++){
		if(i == 0)
			string += items[i].ToString();
		else
			string += '|'+items[i].ToString();
	}
	//Write(string);
	this.field.value = string;
}