function StatisticsCategory()
{
	this.id = null;
	this.codes = null; // StatisticsCode array
};

function StatisticsCode()
{
	this.name = null;
	this.label = null;
	this.selected = false;
	this.subCode = null; // StatisticsCategory
};

function StatisticsCodeList()
{
	//this.codeDocument = XmlDocument;
	this.isIE = true;
	this.prefix = null;  
	this.resolver = null;
	this.BrowserEnum = {
			none:0, 
			ie8:1, 
			ie7:2, 
			ie6:3, 
			ie:4, 
			firefox:5, 
			safari:6, 
			chrome:7, 
			opera:8, 
			other:9}; 
	this.browser = this.BrowserEnum.none;
	this.codeList = null;
	
	this.init = function(xmlDocument)
	{
		if(xmlDocument == null)
			return;
		
		//alert(navigator.userAgent);
		if(navigator.userAgent.toLowerCase().indexOf("msie 8") > 0)
			this.browser = this.BrowserEnum.ie8;
		else if(navigator.userAgent.toLowerCase().indexOf("msie 7") > 0)
			this.browser = this.BrowserEnum.ie7;
		else if(navigator.userAgent.toLowerCase().indexOf("msie 6") > 0)
			this.browser = this.BrowserEnum.ie6;
		else if(navigator.userAgent.toLowerCase().indexOf("msie") > 0)
			this.browser = this.BrowserEnum.ie;
		else if(navigator.userAgent.toLowerCase().indexOf("firefox") > 0)
			this.browser = this.BrowserEnum.firefox;
		else if(navigator.userAgent.toLowerCase().indexOf("shiretoko") > 0)
			this.browser = this.BrowserEnum.firefox;
		else if(navigator.userAgent.toLowerCase().indexOf("safari") > 0)
			this.browser = this.BrowserEnum.safari;
		else if(navigator.userAgent.toLowerCase().indexOf("chrome") > 0)
			this.browser = this.BrowserEnum.chrome;
		else
			this.browser = this.BrowserEnum.other;
		
		this.isIE = (this.browser == this.BrowserEnum.ie8 || this.browser == this.BrowserEnum.ie7 ||
			this.browser == this.BrowserEnum.ie6 ||this.browser == this.BrowserEnum.ie);

		if(this.isIE == false)
		{
			var evaluator = new XPathEvaluator();
			this.resolver = evaluator.createNSResolver(xmlDocument.documentElement);
		}
		
		this.codeList = new Array();
		this.prefix = findNamespacePrefix(xmlDocument.documentElement);

		var i = 0;
		var child = null;
		var category = null;
		var count = 0;
		for(; i < xmlDocument.documentElement.childNodes.length; i++)
		{
			if(xmlDocument.documentElement.childNodes.item(i).nodeType != 1)
				continue;
			child = xmlDocument.documentElement.childNodes.item(i);
			if(child.nodeName == this.prefix + ":Category")
			{
				category = new StatisticsCategory();
				category.id = child.getAttribute("id");
				category.codes = new Array();
				makeCodes(category, child, this.prefix);
				this.codeList[count++] = category;
			}
		}
		//for(i = 0; i < this.codeList.length; i++)
		//	alert(this.codeList[i].id);
	};
	
	var makeCodes = function(category, elemCategory, prefix)
	{
		var i = 0;
		var child = null;
		var code = null;
		var count = 0;
		for(; i < elemCategory.childNodes.length; i++)
		{
			if(elemCategory.childNodes.item(i).nodeType != 1)
				continue;
			child = elemCategory.childNodes.item(i);
			if(child.nodeName == prefix + ":Code")
			{
				code = makeCode(child, prefix);
				category.codes[count++] = code;
			}
		}
	};
	
	var makeCode = function(elemCode, prefix)
	{
		var code = new StatisticsCode();
		code.name = elemCode.getAttribute("name");
		var i = 0;
		var child = null;
		for(; i < elemCode.childNodes.length; i++)
		{
			if(elemCode.childNodes.item(i).nodeType != 1)
				continue;
			child = elemCode.childNodes.item(i);
			if(child.nodeName == prefix + ":Label")
				code.label = getElementText(child);
			else if(child.nodeName == prefix + ":SubCodes")
				makeSubCode(code, child, prefix);
		}
		
		return code;
	};
	
	var makeSubCode = function(code, elemSubCodes, prefix)
	{
		var subCode = new StatisticsCategory();
		subCode.id = elemSubCodes.getAttribute("id");
		code.subCode = subCode;
		
		if(elemSubCodes.childNodes.length == 0)
			return null;
		
		subCode.codes = new Array();
		
		var i = 0;
		var child = null;
		var count = 0;
		for(; i < elemSubCodes.childNodes.length; i++)
		{
			if(elemSubCodes.childNodes.item(i).nodeType != 1)
				continue;
			child = elemSubCodes.childNodes.item(i);
			if(child.nodeName == prefix + ":Code")
			{
				subCode.codes[count++] = makeCode(child, prefix);
			}
		}
	};
	
	var findNamespacePrefix = function(documentElement)
	{
		var i = 0;
		for(; i < documentElement.attributes.length; i++)
		{
			node = documentElement.attributes.item(i);
			if(node.nodeValue == "http://www.dreamntech.com/2009/business-code")
			{
				return node.nodeName.substring(node.nodeName.indexOf(":") + 1, node.nodeName.length);
			}
		}
		return null;
	};
	
	var getElementText = function(element)
	{
		var value = "";
		var i = 0;
		for(; i < element.childNodes.length; i++)
		{
			if(element.childNodes.item(i).nodeType == 3)
				value += element.childNodes.item(i).nodeValue;
		}
		return value;
	};
	
	this.getCodeList = function(categoryId)
	{
		if(categoryId == null)
			return null;
		
		var i = 0; 
		for(; i < this.codeList.length; i++)
		{
			if(this.codeList[i].id == categoryId)
				return this.codeList[i].codes;
		}
		
		return null;
	};
	
	/*this.getCodeList = function(categoryId)
	{
		var codeList = new Array();
		var path = "/" + this.prefix + ":CodeList/" + 
			this.prefix + ":Category[@id='" + categoryId + "']/" +
			this.prefix + ":Code";
		try
		{
			if(this.isIE)
			{
				var list = this.codeDocument.documentElement.selectNodes(path);
				var i = 0;
				for(; i < list.length; i++)
				{
					codeList[i] = makeCode(list.item(i), this.prefix); 
				}
			}
			else
			{
				list = this.codeDocument.evaluate(path, this.codeDocument.documentElement, 
					this.resolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
				if(list != null)
				{ 
					i = 0;
					for(; i < list.snapshotLength; i++)
						codeList[i] = makeCode(list.snapshotItem(i), this.prefix);
				}
			}
		}
		catch(ex)
		{
			alert("Scripting error: " + ex);
		}
		
		return codeList;
	};*/
	
	this.getSubCodeList = function(categoryId, codeId)
	{
		if(categoryId == null || codeId == null)
			return null;
		
		var codeList = this.getCodeList(categoryId);
		if(codeList == null)
			return null;
		var i = 0, j = 0;
		for(; i < codeList.length; i++)
		{
			if(codeList[i].name == codeId)
			{
				return (codeList[i].subCode == null) ? null : codeList[i].subCode.codes;
			}
		}
		
		return null;
	};
	
	this.selectCode = function(categoryId, codeId, value)
	{
		if(categoryId == null || codeId == null)
			return;
		
		var codeList = this.getCodeList(categoryId);
		if(codeList == null)
			return;
		var i = 0, j = 0;
		for(; i < codeList.length; i++)
		{
			if(codeList[i].name == codeId)
			{
				codeList[i].selected = value;
				/*if(codeList[i].subCode == null || codeList[i].subCode.codes == null)
					break;;
				for(j = 0; j < codeList[i].subCode.codes.length; j++)
					codeList[i].subCode.codes[j].selected = value;
				break;*/
			}
		}
	};
	
	this.selectAllCodes = function(categoryId, value)
	{
		if(categoryId == null)
			return;
		
		var codeList = this.getCodeList(categoryId);
		if(codeList == null)
			return;
		var i = 0, j = 0;
		for(; i < codeList.length; i++)
		{
			codeList[i].selected = value;
			/*if(codeList[i].subCode == null || codeList[i].subCode.codes == null)
				continue;
			for(j = 0; j < codeList[i].subCode.codes.length; j++)
				codeList[i].subCode.codes[j].selected = value;*/
		}
	};
	
	this.getSelectedCodes = function(categoryId)
	{
		var codes = new Array();
		if(categoryId == null)
			return codes;

		var codeList = this.getCodeList(categoryId);
		if(codeList == null)
			return codes;
		
		var i = 0, j = 0;
		for(; i < codeList.length; i++)
		{
			if(codeList[i].selected)
				codes[j++] = codeList[i];
		}

		return codes;
	};
	
	this.selectSubCode = function(categoryId, codeId, subCodeId, value)
	{
		if(categoryId == null || codeId == null || subCodeId == null)
			return;
		
		var codeList = this.getCodeList(categoryId);
		if(codeList == null)
			return;
		
		var i = 0, j = 0;
		for(; i < codeList.length; i++)
		{
			if(codeList[i].name == codeId)
			{
				codeList[i].selected = value;
				if(codeList[i].subCode == null || codeList[i].subCode.codes == null)
					continue;
				for(j = 0; j < codeList[i].subCode.codes.length; j++)
				{
					if(codeList[i].subCode.codes[j].name == subCodeId)
					{
						codeList[i].subCode.codes[j].selected = value;
						break;
					}
				}
				break;
			}
		}
	};
	
	this.selectAllSubCodes = function(categoryId, codeId, value)
	{
		if(categoryId == null || codeId == null)
			return;
		
		var codeList = this.getCodeList(categoryId);
		if(codeList == null)
			return;
		var i = 0, j = 0;
		for(; i < codeList.length; i++)
		{
			if(codeList[i].subCode == null || codeList[i].subCode.codes == null)
				continue;
			for(j = 0; j < codeList[i].subCode.codes.length; j++)
				codeList[i].subCode.codes[j].selected = value;
		}
	};
	
	this.getSelectedSubCodes = function(categoryId, codeId)
	{
		var codes = new Array();
		if(categoryId == null)
			return codes;
		
		var codeList = this.getCodeList(categoryId);
		if(codeList == null)
			return codes;
		var i = 0, j = 0, k = 0;
		for(; i < codeList.length; i++)
		{
			if(codeList[i].name == codeId)
			{
				if(codeList[i].subCode == null || codeList[i].subCode.codes == null)
					return codes;
				for(j = 0; j < codeList[i].subCode.codes.length; j++)
				{
					if(codeList[i].subCode.codes[j].selected)
						codes[k++] = codeList[i].subCode.codes[j];
				}
				break;
			}
		}
		
		return codes;
	};
	
	this.isAllCodesSelected = function(categoryId)
	{
		var codeList = this.getCodeList(categoryId);
		if(codeList == null)
			return false;
		
		var count = codeList.length;
		var i = 0;
		for(; i < codeList.length; i++)
		{
			if(codeList[i].selected == false)
				break;
		}
		
		return count == i;
	};
	
	this.isAllSubCodesSelected = function(categoryId, codeId)
	{
		var codeList = this.getCodeList(categoryId);
		if(codeList == null)
			return false;
		
		var i = 0, j = 0, count = 0;
		for(; i < codeList.length; i++)
		{
			if(codeList[i].name == codeId)
			{
				if(codeList[i].subCode == null || codeList[i].subCode.codes == null)
					return false;
				
				count = codeList[i].subCode.codes.length;
				for(j = 0; j < codeList[i].subCode.codes.length; j++)
				{
					if(codeList[i].subCode.codes[j].selected == false)
						break;
				}
				break;
			}
		}
		
		return count == j;
	};
}

