/**************************************/
/*    COMTRON Ajax classifications    */
/**************************************/

var Classification = {
	ClassID: "",
	ClassI: "",
	ClassII: "",
	ClassIII: "",
	GroupID: "",
	Count: 0,
	Level: 0,
	Root: null,
	Button: null,
	arrClassifications: new Array(),

	Expand: function (elTarget)
	{
		return;
		// Get classification structure data
		this.Request(elTarget);

		// Crete ajax call
		var aRequest = new System.Net.Ajax.Request("POST", "/Include/ClassificationTreeHTML.asp", this.Expand_callback, true);		
			aRequest.AddParam("I", this.ClassI);
			
			if (this.ClassID.substr(2, 4) != "0000")
				aRequest.AddParam("II", this.ClassII);
				
			if (this.ClassID.substr(4, 4) != "0000")
				aRequest.AddParam("III", this.ClassIII);
				
			aRequest.AddParam("GroupID", this.GroupID);
			aRequest.AddParam("Level", this.Level);

		// Add request to caller	
		var aPageRequest = new System.Net.Ajax.PageRequests(aRequest);
		
		// Open connection to destination
		var aConnection = new System.Net.Ajax.Connection(aPageRequest);
            aConnection.Open();
	},
	
	Expand_callback: function(src)
	{
		// Connection ready
		if(src.ReadyState==4)
		{
			// Receiving data
			if(src.Status==200)
			{
				// Data received
				if(src.Complete)
				{
					if (src.ResponseText != " ")
					{
						/*var index = src.ResponseText.indexOf("<script");
						var content = src.ResponseText.substring(0, index);*/
						
						content = src.ResponseText;

						// Get classifications count
						Classification.ExpandClass(content);
					}
				}
			}
		}
	},
	
	ExpandClass: function(HTML)
	{
		try
		{
			// Create sub content
			var content = document.createElement("div");
				content.innerHTML = HTML;
				content.id = "AjaxClassification_" + this.ClassID + "_" + this.GroupID;

			// Identifier for old classification tab so we can
			// reference it from the array
			this.Root.id = "AjaxOldClassification_" + this.ClassID + "_" + this.GroupID;

			// Store old classification tab so we can later
			// restore the classification without any postbacks
			this.arrClassifications.push(this.Root);
			
			// Insert new classification with sub classifications
			this.Root.parentNode.insertBefore(content, this.Root.nextSibling);
			
			// Now after the first tab has been replaced remove
			// old classification tab from DOM structure
			this.Root.parentNode.removeChild(this.Root);
		} catch (ex) {; };
	},
	
	Collapse: function (elTarget)
	{
		return;
		try
		{
			// Get classification structure data
			this.Request(elTarget);
			
			// Get expandable classification
			var content = GetElement("AjaxClassification_" + this.ClassID + "_" + this.GroupID);

			// Insert back old classification tab
			var oldTab = this.FindAndRemove(this.ClassID, this.GroupID);
			content.parentNode.insertBefore(oldTab, content.nextSibling);
			
			// Remove ajax requested classifications
			content.parentNode.removeChild(content);
		} catch (ex) {; };
	},
	
	Request: function(elTarget)
	{
		// Get classification structure data
		var arrData = elTarget.id.split('_');
		this.ClassID = arrData[0];
		this.ClassI = this.ClassID.substr(0, 2) + "00000000";
		this.ClassII = this.ClassID.substr(0, 2) + this.ClassID.substr(2, 4) + "0000";
		this.ClassIII = this.ClassID;
		this.GroupID = arrData[1];
		this.Count = elTarget.getAttribute("count");
		this.Level = elTarget.getAttribute("level");
		this.Root = elTarget.parentNode.parentNode;
		this.Button = elTarget;
	},
	
	FindAndRemove: function(sClassID, sGroupID)
	{
		for (var i = 0; i < Classification.arrClassifications.length; i++)
		{
			if (Classification.arrClassifications[i].id == "AjaxOldClassification_" + sClassID + "_" + sGroupID)
			{
				var el = Classification.arrClassifications[i];
						 Classification.arrClassifications.splice(i, 1);
				return el;
			}
		}
	}
}
