/**
 * Class: CategoryFactory
 * Last Modify By : DZ
 */
function CategoryFactory(name, categoryAreaName, subCatgoryAreaName, selectone, warnMsg) {

	this.Name = name;
	
	this.Categories = new Array();
	
	this._current = null;

	this.CategoryArea = eval("document.all['" + categoryAreaName + "']");

	this.SubCategoryArea = eval("document.all['" + subCatgoryAreaName + "']");

	if (this.CategoryArea == null || this.SubCategoryArea == null) {
		alert("Fatal Error Occurs on initalize category factory!");
		return;
	}
	
	this.CategorySelectorName = null;
	
	this.SubCategorySelectorName = null;
	
	this.addCat = function(Id, name, blurbId, desc) {
		this._current = new Category(Id, name, blurbId, desc);
		this.Categories[this.Categories.length] = this._current;
	}
	
	this.addSubCat = function(subId, name, blurbId, desc) {
		if (this._current == null) {
			return;
		}
		this._current.addSub(subId, name, blurbId, desc);
	}
	
	this.findCategoryByID = function(Id) {
		var result = null;
		var obj = null;
		for (i =0; i < this.Categories.length; i++) {
			obj = this.Categories[i];
			if (obj.ID == Id) {
				result = obj;
				break;
			}
		}
		return result;
	}
	
	this.findCategoryByName = function(name) {
		var result = null;
		var obj = null;
		for (i =0; i < this.Categories.length; i++) {
			obj = this.Categories[i];
			if (obj.Name == name) {
				result = obj;
				break;
			}
		}
		return result;
	}
	
	this.renderCatSelector = function(name, onchange) {
		this.CategorySelectorName = name;
		if (String(onchange)=="undefined") {
			onchange = this.Name + ".onCategoryChange(this.selectedIndex)";
		}
		
		var result = "<select name='" + name + "' onChange='" + onchange + "'>";
		result += "<option value=''>" + selectone + "</option>";
		var obj = null;
		for (i = 0; i < this.Categories.length; i++) {
			obj = this.Categories[i];
			if(this._current != null) {
				result += "<option value='" + obj.Name + "'" + (this._current.Name == obj.Name? "selected" :"") + ">" + obj.Description + "</option>";
			} else {
				result += "<option value='" + obj.Name + "'>" + obj.Description + "</option>";
			}
		}
		result += "</select>";
		this.CategoryArea.innerHTML = result;
	}
	
	this.renderSubCatSelector = function(name, onchange) {
		this.SubCategorySelectorName = name;
		if (String(onchange) == "undefined") {
			onchange = this.Name + ".onSubCategoryChange(this.selectedIndex)";
		}
		
		var result = "<select name='" + name + "' onChange='" + onchange + "'>";
		result += "<option value=''>" + selectone + "</option>";
		if (this._current != null) {
			var obj = null;
			for (i = 0; i < this._current.subCategories.length; i++) {
				obj = this._current.subCategories[i];
				if (this._current._current != null) {
					result += "<option value='" + obj.Name + "'" + (this._current._current.Name == obj.Name?"selected":"") + ">" + obj.Description + "</option>";
				} else {
					result += "<option value='" + obj.Name + "'>" + obj.Description + "</option>";
				}
			}
		}
		result += "</select>";
		this.SubCategoryArea.innerHTML = result;
	}
	
	this.onCategoryChange = function(index) {
		if (currentStep > 2 && window.confirm(warnMsg)) {
			this._current = this.Categories[index - 1];
			this.rollback(2);
			this.renderCatSelector(this.CategorySelectorName);
			step2next();
		} else if (currentStep > 2) {
			this.renderCatSelector(this.CategorySelectorName);
		} else {
			if (this.CategoryArea == null || index < 0 || index > this.Categories.length) {
				return;
			} else if(index == 0) {
				this._current = null;
				this.renderSubCatSelector(this.SubCategorySelectorName);
			} else {
				this._current = this.Categories[index - 1];
				this.renderSubCatSelector(this.SubCategorySelectorName);
			}
			step2next();
		}
	}
	
	this.onSubCategoryChange = function (index) {
		if (currentStep > 3 && window.confirm(warnMsg)) {
			this._current._current = this._current.getSub(index - 1);
			this.rollback(3);
		}	else if (currentStep > 3) {
			this.renderSubCatSelector(this.SubCategorySelectorName);
		}	else {
			if (index < 1) {
				this._current._current = null;
			}
			if (this._current != null) {
				if (index > 0) {
					this._current._current = this._current.getSub(index - 1);
				}
			}
		}
	}
	
	this.rollback = function(step) {
		if (step<2 || step>3) {
			return;
		}
		currentStep = step;
		pc.showStep(currentStep);
		with(takofrm) {
			switch(step) {
			    case 2: 
					this.onSubCategoryChange(0);
					this._current._current = null;
					step3Area.style.display = "none";
				case 3:
					atofficey.checked = null;
					atofficen.checked = null;
					geterrory.checked = null;
					geterrorn.checked = null;
					errorMsg.value = "";
					evcy.checked = null;
					evcn.checked = null;
					address.value = "";
					teacher.value = "";
					sltTimeZone.selectedIndex = 0;
					explain.value = "";
					files.innerHTML = "";
					step4Area.style.display = "none";
					step5Area.style.display = "none";
					break;
			}
		}
	}
}
