/****************
	cascade down choices
	
	for scheduled times
	
	
	we would have four select boxes
	
	4 hour = each shift
	
	<select maxAllowed=16 class="cascadeList" day="1" hour="1">
		<option value="">-Please Select Class-</option>
	</select>
	
	<select maxAllowed=4 class="cascadeList" day="1" hour="2">
	
	</select>
	
	<select maxAllowed=8 class="cascadeList" day="2" hour="1">
	
	</select>
	
	<select maxAllowed=4 class="cascadeList" day="2" hour="2">
	
	</select>
	
	
	var items = [
		{ name='underwater basket weaving', hours=16, onlyDay=1 },
		{ name='scuba school', hours=4 }
		{ name='skin diving', hours=8, onlyDay=2 },		
	];
****************/

ClassCascader =
{

items: new Array(),
className: "cascadeList",
days: new Object(),
selectOptionText: "--PLEASE SELECT CLASS--",
hoursPerSession: 4,
maxDays:3,
dayCount:0,
initialize: function(itmsArr)
{
	this.items = itmsArr;
	
	var elems = document.getElementsByClassName(this.className);
	for ( i = 0; i < elems.length; i++)
	{
		var tmpObj = this.getAttribs(elems[i]);
		if ( tmpObj.day )
		{
			if ( this.days[tmpObj.day] == undefined)
			{
				this.dayCount++;
				this.days[tmpObj.day] = new Object();
			}
			this.days[tmpObj.day][tmpObj.hour] = tmpObj;
		
			Event.observe(elems[i], "change", this.onSessionChange.bindAsEventListener(this));
		}			
	}
	this.populateOptions();
},

onSessionChange: function(event)
{
	var elem = Event.element(event);
	this.enableAllFollowing(elem);
	this.cascadeDown(elem);	
	this.populateOptions();

	this.checkForMaxDays(elem);
},

checkForMaxDays: function(elem)
{
	var dayCount = 0;
	if ( this.maxDays < this.dayCount )
	{
		if ( this.getCurrentItem(elem) != "" )
		{
			for ( var i in  this.days )
			{
				var bDayFound = false;
				for ( var j in this.days[i])
				{
					if ( this.getCurrentItem(this.days[i][j].elem) != "" )
					{
							bDayFound = true;
							break;
					}
				
				}
				if ( bDayFound)
				{
					dayCount++;
				}
			}
			
			if ( dayCount >= this.maxDays )
			{
				for ( var i in  this.days )
					{
						var bDayFound = false;
						for ( var j in this.days[i])
						{
							if ( this.getCurrentItem(this.days[i][j].elem) != "" )
							{
								bDayFound = true;
								break;
		
							}
						}
						if (  ! bDayFound )
						{
							for ( var j in this.days[i])
							{
								this.days[i][j].elem.disabled = true;
							}
						}
					}
			}
		} else {
			var dayNum = elem.getAttribute("day");
			var dayNotEmpty = false;
			for ( var j in this.days[dayNum] )
			{
				if ( this.getCurrentItem(this.days[dayNum][j].elem) != "")
					dayNotEmpty = true;
				
			}
			
			if ( ! dayNotEmpty ) 
			{
				for ( var i in this.days )
				{
					for ( var j in this.days[i] )
					{
						if ( this.getCurrentItem(this.days[i][j].elem) == "" )
						{
							this.days[i][j].elem.disabled = false;
						}
					}
				}
			}
		}
	
		
	}
},
cascadeDown: function(elem)
{
	var tmpObj = this.getAttribs(elem);
	if ( tmpObj.hours <= this.hoursPerSession)
		return;
	
	var selItem =this.getSelectedOptions(elem); 
	
	var cCount = selItem.hours / this.hoursPerSession;
	cCount--;
	var bFoundDay = false;
	var bFoundHour = false;
	var bDoneProcessing = false;
	/* Find All Following disabled boxes and re eanble them */
	for ( var day in this.days )
	{
		if ( day == tmpObj.day)
			bFoundDay = true;
			
		if ( bFoundDay ) 
			for ( var hour in this.days[day] )
			{
				if ( bFoundHour )
				{
					if ( cCount > 0 )
					{
						this.setValue(this.days[day][hour].elem, selItem.name);


						cCount--
					}else
					{
						bDoneProcessing = true;
						break;
					}
				} else {
					if ( hour == tmpObj.hour )
						bFoundHour = true;
				}
				
			}
		
		if ( bDoneProcessing)
			break;
		
	}
},
setValue : function(elem, value)
{
	elem.disabled = true;
	elem.options.length = 0;
	
	var tmpOpt = document.createElement("option");
	tmpOpt.value = value;
	Element.update(tmpOpt, value);
	elem.appendChild(tmpOpt);
},
enableAllFollowing: function(elem)
{
	var tmpObj = this.getAttribs(elem);
	var bFoundDay = false;
	var bFoundHour = false;
	var bDoneProcessing = false;
	/* Find All Following disabled boxes and re eanble them */
	for ( var day in this.days )
	{
		if ( day == tmpObj.day)
			bFoundDay = true;
			
		if ( bFoundDay ) 
			for ( var hour in this.days[day] )
			{
				if ( bFoundHour )
				{
					if ( this.days[day][hour].elem.disabled )
						this.days[day][hour].elem.disabled = false;
					else
					{
						bDoneProcessing = true;
						break;
					}
				} else {
					if ( hour == tmpObj.hour )
						bFoundHour = true;
				}
				
			}
		
		if ( bDoneProcessing)
			break;
		
	}
},

createOption : function(itm)
{
	var elem = document.createElement("option");
	
	Element.update(elem, itm.name);
	elem.value = itm.name;
	elem.setAttribute("hours", itm.hours);
	if ( itm.onlyDay )
		elem.setAttribute("onlyDay", itm.onlyDay);
		
	
	return elem;
},

getSelectedOptions: function(elemSB)
{
	var retStr = {};
		if ( elemSB.selectedIndex > 0 )
	{
		retStr.name  = elemSB.options[elemSB.selectedIndex].value;
		retStr.hours = elemSB.options[elemSB.selectedIndex].getAttribute("hours");
	}
	
	return retStr;
},

getCurrentItem: function(elemSB)
{
	var retStr = "";
	
		retStr = elemSB.options[elemSB.selectedIndex].value;
	
	return retStr;
},

getSelectedValue: function(elemSB)
{
	var retStr = "";
	if ( elemSB.selectedIndex > 0 )
	{
		retStr = elemSB.options[elemSB.selectedIndex].value;
	}
	return retStr;
},

getSelectedItems: function()
{
	var retItms = new Array();
	for ( var day in this.days )
	{
		for ( var hour in this.days[day] )
		{
			var tmpelem = this.days[day][hour].elem;
			
			if ( ! tmpelem.disabled && this.getSelectedValue(tmpelem) != "")
			{
				retItms.push(this.getSelectedValue(tmpelem));
			}
		}		
	}
	return retItms;
},

populateOptions: function()
{
	var tmpVal = "";
	var selItms = this.getSelectedItems();
	for ( var day in this.days )
	{
		for ( var hour in this.days[day] )
		{
			var tmpelem = this.days[day][hour].elem;
			tmpVal = this.getSelectedValue(tmpelem);
			if ( ! ( tmpelem.disabled ) )
			{
//				tmpelem.options.length = 0;
				Element.update(tmpelem, "");
			
				var elem = document.createElement("option");
				
				Element.update(elem, this.selectOptionText);
				elem.value = "";
				tmpelem.appendChild(elem);
					for ( var i =0; i < this.items.length; i++ )
				{
					if ( this.items[i].onlyDay == undefined || this.items[i].onlyDay == day )
						if ( this.items[i].hours <= this.days[day][hour].maxAllowed )
							if ( tmpVal == this.items[i].name || selItms.indexOf(this.items[i].name) == -1 )
								tmpelem.appendChild(this.createOption(this.items[i]) );
		
				}
				if ( tmpVal != "" )
				{
					this.setSelectedValue(tmpelem, tmpVal);
				}
			}
		}
	} 
},

setSelectedValue: function (tmpelem, tmpVal)
{
	for ( var i = 0; i < tmpelem.options.length; i++)
	{
		if ( tmpelem.options[i].value == tmpVal )
			tmpelem.options[i].selected = true;
			
	}
},

getAttribs: function(elem)
{
	var retObj = {};
	
	retObj.day = elem.getAttribute("day");
	retObj.hour = elem.getAttribute("hour");
	retObj.maxAllowed = elem.getAttribute("maxAllowed");
	retObj.elem = elem;
	
	return retObj;
}
}