//$Id: CategoryDefinition.js,v 1.1 2006/09/08 10:22:26 acraja Exp $

/**
 * Adds a Category to the existing list. The information's to be passed are
 * @param	categoryID	the id of the current Category
 * @param	categoryName	the name of the current Category
 * @param	parentCategory	the parent category id of the current Category. Default's to ROOT
 */
function addCategory(categoryID, categoryName, parentCategory) {
	if(parentCategory == null) {
		parentCategory = "ROOT";
	}
	var category = new Array(categoryID, categoryName, parentCategory);
	parent["_CATEGORY_HOLDER"]["CAT" + categoryID] = category;
	var parentCat = parent["_CHILD_DETAILS"]["PAR_" + parentCategory];
	if(parentCat == null) {
		parentCat = new Array();
		parent["_CHILD_DETAILS"]["PAR_" + parentCategory] = parentCat;
	}
	parentCat[parentCat.length] = category;
}

/**
 * Returns the category name of the given categoryId.
 */
function getCategoryName(categoryId) {
	return parent["_CATEGORY_HOLDER"]["CAT" + categoryId][1];
}

/**
 * Creates a tree for the given parent category.
 * @param	parentCategory	the category Id for which the tree has to constructed. 
 */
function createCategoryTree(parentCategory) {
	var childs = getChildCategoryIds(parentCategory);
	var nameToCat = new Object();
	var names = new Array();
	for(var i=0; i<childs.length;i++) {
		var childCat = childs[i];
		nameToCat[childCat[1]] = childCat; 
		names[i] = childCat[1];
	}
	names = names.sort();
	for(var i=0; i<names.length;i++) {
		var catName = names[i];
		var childCat = nameToCat[catName];
		var catId = childCat[0];
		parent["_CATEGORY_TREE_ORDER"][parent["_CATEGORY_TREE_ORDER"].length] = catId;
		parent["C_" + catId ] = [catName, "javascript:populateCategory('" + catId + "')", null];
		patCat = parent["C_" + parentCategory];
		patCat[patCat.length] = parent["C_" + catId];
		var clds = getChildCategoryIds(catId);
		if(clds != null && clds != "Undefined") {
			createCategoryTree(catId);
		}
	}
}

/**
 * Populates the Category as a tree like structure in the given Select Element.
 * @param	parentCategory	the category Id for which the tree has to constructed. 
 * @param	selectElement	the select element obj where the population is to be done. 
 * @param	padding 	the padding to be used for indentation of the child. 
 * @param	prefix		the prefix to be used for child categories. 
 * @param	defaultSelected	the category Id that should be selected by default. 
 * @param	depth		the depth count for determining the padding that is required. 
 */
function populateCategoryForSelectElement(parentCategory, selectElement, padding, prefix, defaultSelected, depth) {
	if(depth == null || depth == "Undefined") {
		depth = 0;
	}
	var childs = getChildCategoryIds(parentCategory);
	var nameToCat = new Object();
	var names = new Array();
	for(var i=0; i<childs.length;i++) {
		var childCat = childs[i];
		nameToCat[childCat[1]] = childCat; 
		names[i] = childCat[1];
	}
	names = names.sort();
	for(var i=0; i<names.length;i++) {
		var catName = names[i];
		var childCat = nameToCat[catName];
		var catId = childCat[0];
		if(parentCategory != "ROOT") {
			catName = prefix + catName;
		}
		var opt  = new Option(catName, catId, false, false);
		for(var l=0; l<depth;l++) {
			catName = "&nbsp;&nbsp;" + catName;
		}
//		opt.style.padding = "0px 0px 0px " + ((padding*depth) + 5) + "px";
//		opt.setAttribute("style", "padding-left: " + ((padding*depth) + 5) + "px");
		selectElement[selectElement.options.length] = opt;
		opt.innerHTML = catName;
		if(defaultSelected == catId) {
				
			opt.selected = true;
		}
		var clds = getChildCategoryIds(catId);
		if(clds != null && clds != "Undefined") {
			populateCategoryForSelectElement(catId, selectElement, padding, prefix, defaultSelected, depth+1);
		}
	}
}

/**
 * Returns the id's of the child categories for the given categoryId.
 */
function getChildCategoryIds(parentCategory) {
	return parent["_CHILD_DETAILS"]["PAR_" + parentCategory];
}

/**
 * Returns the parent category names as an array for the given category Id.
 */
function getParentCategoryNames(categoryId) {
	var parents = new Array();
	var parId = parent["_CATEGORY_HOLDER"]["CAT"+categoryId][2];
	var hasParent = true;
	if(parId == 'ROOT') {
		hasParent = false;
	}	
	while(hasParent == true) {
		parents.splice(0,0,parent["_CATEGORY_HOLDER"]["CAT"+parId][1]);
		parId = parent["_CATEGORY_HOLDER"]["CAT"+parId][2];
		if(parId == "ROOT") {
			hasParent = false;
		}
	}
	return parents;
}

/**
 * Returns the parent category Id's as an array for the given category Id.
 */
function getParentCategoryIds(categoryId) {
	var parents = new Array();
	var parId = parent["_CATEGORY_HOLDER"]["CAT"+categoryId][2];
	var hasParent = true;
	if(parId == 'ROOT') {
		hasParent = false;
	}	
	while(hasParent == true) {
		parents.splice(0,0,parent["_CATEGORY_HOLDER"]["CAT"+parId][0]);
		parId = parent["_CATEGORY_HOLDER"]["CAT"+parId][2];
		if(parId == "ROOT") {
			hasParent = false;
		}
	}
	return parents;
}

/**
 * Initializes the variables required for maintaining the Category Definitions.
 */
function initCategoryDetails() {
	parent["_CATEGORY_HOLDER"] = new Object();
	parent["_CHILD_DETAILS"] = new Object();
	parent["C_ROOT"] = [];
	parent["_CATEGORY_TREE_ORDER"] = [];
}

/**
 * Populates the Category as a tree like structure in the given Select Element.
 * @param	parentCategory	the category Id for which the tree has to constructed. 
 * @param	selectElement	the select element obj where the population is to be done. 
 * @param	padding 	the padding to be used for indentation of the child. 
 * @param	prefix		the prefix to be used for child categories. 
 * @param	defaultSelected	the category Id that should be selected by default. 
 * @param	depth		the depth count for determining the padding that is required. 
 */
function constructCategoryDefinitionTable(tableObj, deleteToolTip, editToolTip, padding, prefix) {
	alert(parent["_CATEGORY_TREE_ORDER"]);
}

/**
 * Displays the parents for the given category as a bread crumb. 
 */
function displayParentsForCategory(categoryID, destId, catClass, separator){
	var code = "";
	if(categoryID == null) {
        code = code.concat("<span class='" + catClass + "'> Not Assigned </span>");
	    document.getElementById(destId).innerHTML = code;
		return;
	}
	if(separator == null || separator == "Undefined") {
		separator = " > ";
	}
	var parCat = getParentCategoryNames(categoryID);
	if(parCat.length > 0) {
		for(var count = 0; count < parCat.length; count++) {
			code = code.concat(parCat[count] + separator);
		}
	}
	code = code.concat("<span class='" + catClass + "'>" + getCategoryName(categoryID) + "</span>");
	document.getElementById(destId).innerHTML = code;
}

/**
 * Displays the parents for the given category as a bread crumb. 
 */
function displayParentsForCategoryAsToolTip(categoryID, destId, catClass){
	if(categoryID == null) {
		return;
	}
	var parCat = getParentCategoryNames(categoryID);
	var code = "";
	var text = "";
	if(parCat.length > 0) {
		for(var count = 0; count < parCat.length; count++) {
			code = code.concat(parCat[count] + " > ");
		}
		text = "<span style='color:gray;font-weight:normal;'>" + parCat[0].substring(0,2) + "... > </span>";
	}
	text = text.concat(getCategoryName(categoryID));
	code = code.concat(getCategoryName(categoryID));
	document.getElementById(destId).innerHTML = text;
	document.getElementById(destId).title = code;
}

