﻿Type.registerNamespace("Finatica.Controls");
Type.registerNamespace("Finatica.Controls.HD");

Finatica.Controls.HDictionary = function(element) {
	Finatica.Controls.HDictionary.initializeBase(this, [element]);
	this.textBoxID = null;
	this.hiddenID = null;
	this.hiddenSortID = null;
	if(this.HDserviceProxy == undefined || this.HDserviceProxy == null)
		this.HDserviceProxy = new Finatica.WebServices.HDictionaryService();
}

Finatica.Controls.HDictionary.prototype =
{
	set_SingleSelectedValue: function(value, lang, dName) {
	if (this.HDserviceProxy != null) {
		$('#' + this.hiddenID.toString()).val(value);
		this.HDserviceProxy.GetNames(value, lang, dName, this.onGetSucceeded, this.onGetFailed, this.textBoxID);
		}
	},

	onGetFailed: function(result, userContext, methodName) {
		$find(Finatica.Common.NotificatorID).addMessage('error', 'Ошибка поиска');
	},

	onGetSucceeded: function(result, userContext, methodName) {
		$('#' + userContext.toString()).val(result);		
	},


	initialize: function() {
		Finatica.Controls.HDictionary.callBaseMethod(this, 'initialize');
	},

	dispose: function() {
		Finatica.Controls.HDictionary.callBaseMethod(this, 'dispose');
	}
}

Finatica.Controls.HDictionary.descriptor =
{
	properties: [{ name: 'textBoxID', type: String },
				 { name: 'hiddenID', type: String },
				 { name: 'hiddenSortID', type: String } ]
}

Finatica.Controls.HDictionary.registerClass("Finatica.Controls.HDictionary", Sys.UI.Control);


Finatica.Controls.HD.showHDPopup = function(divID) {

	if (Finatica.Controls.OnSectorsShow != undefined && Finatica.Controls.OnSectorsShow != null)
		Finatica.Controls.OnSectorsShow();
	if ($(".dialog_shadow").hasClass("fin-hidden") == true)
		$(".dialog_shadow").removeClass("fin-hidden");
	popupDiv = $get(divID);
	if (popupDiv != undefined && $(popupDiv).hasClass("fin-hidden") == true)
		$(popupDiv).removeClass("fin-hidden");
	$("#divDialog").append(popupDiv);
}

Finatica.Controls.HD.hasCheckedChildren = function(parent)
{
	var children = parent.get_nodes();
	for(var i=0; i<children.get_count();i++)
	{
		if(children.getNode(i).get_checked() == true)
			return true;
	}
	return false;
}

Finatica.Controls.HD.clearSelection = function(treeViewID, listBoxID, hiddenId, inputId, sortId) {
	if (treeViewID == undefined || treeViewID == null)
		return false;

	var treeView = $find(treeViewID);
	if (treeView == undefined || treeView == null)
		return false;

	var checkedNodes = treeView.get_checkedNodes();
	if (checkedNodes != null) {
		for (var i = 0; i < checkedNodes.length; i++) {
			if (checkedNodes[i].get_checkable() == false) {
				checkedNodes[i].set_checkable(true);
				checkedNodes[i].set_checked(false);
				checkedNodes[i].set_checkable(false);
			}
			else { checkedNodes[i].set_checked(false); }
		}
	}

	var listBox = $find(listBoxID);
	var items = listBox.get_items();

	if (items != null) {
		for (var i = items.get_count() - 1; i >= 0; i--)
			listBox.deleteItem(items.getItem(i));
	}

	$('#' + hiddenId).val('');
	$('#' + inputId).val('');
	$('#' + sortId).val('');

	return false;
}

Finatica.Controls.HD.nodeChecked = function(sender, eventArgs) {
	var node = eventArgs.get_node();

	if (node.get_checked() == false) {
		if (Finatica.Controls.HD.hasCheckedChildren(node) == false) {
			var tree = node.get_treeView();
			var parent = node.get_parent();
			var check_state;
			if (parent != null) {
				check_state = Finatica.Controls.HD.hasCheckedChildren(parent);
			}

			if (parent != null && parent != tree && parent.get_checkBoxElement() == null)
				while (parent != null && parent != tree) {
				parent.set_checkable(true);
				parent.set_checked(check_state);
				parent.set_checkable(false);
				parent = parent.get_parent();
			}
		}
		else {
			Finatica.Controls.HD.unchekAllChildren(node);
		}
		if (Finatica.Controls.HD.RemoveNodeFromList != undefined && Finatica.Controls.HD.RemoveNodeFromList != null) {
			Finatica.Controls.HD.RemoveNodeFromList(node.get_value());
		}
	}
	else {
		if (Finatica.Controls.HD.MoveNodeToList != undefined && Finatica.Controls.HD.MoveNodeToList != null) {
			if (node.get_treeView().get_attributes().getAttribute("DictionaryName") == "Regions")
				return;
			Finatica.Controls.HD.MoveNodeToList(node.get_text(), node.get_value());
			node.select();
			var proxy = new Finatica.WebServices.HDictionaryService();
			proxy.GetSectorDescriptionByGuid(node.get_value(), Finatica.Controls.HD.OnSectorsSerchSuccess, Finatica.Controls.HD.OnSectorsSerchFail, node.get_value());
		}
	}
	return false;
}

Finatica.Controls.HD.unchekAllChildren = function(node)
{
	var childNodes = node.get_nodes();
	for(var i=0;i<childNodes.get_count();i++)
	{
		childNode = childNodes.getNode(i);
		childNode.set_checked(false);
		if (Finatica.Controls.HD.hasCheckedChildren(childNode) == true)
		{
			Finatica.Controls.HD.unchekAllChildren(childNode);
		}
	}
	return false;
}

Finatica.Controls.HD.nodePopulated = function(sender, eventArgs) {
	var node = eventArgs.get_node();
	var nodes = node.get_nodes();
	var treeView = node.get_treeView();
	var treeAttributes = treeView.get_attributes();
	var restrict = treeAttributes.getAttribute("restrict");
	for (var i = 0; i < nodes.get_count(); i++) {
		var childNode = nodes.getNode(i);
		var expandMode = childNode.get_expandMode();
		if (restrict == 'true' && expandMode == 3) {
			childNode.set_checkable(false);
		}
	}
	var node_value = node.get_value();
	if (restrict == 'false') {
		var checkedNodes = treeView.get_checkedNodes();
		for (var i = 0; i < checkedNodes.length; i++) {
			if (node_value == checkedNodes[i].get_value()) {
				$(node.get_checkBoxElement()).toggleClass("rtChecked");
				$(node.get_checkBoxElement()).toggleClass("rtUnchecked");
				Finatica.Controls.HD.expandNextNode(treeView);
				return false;
			}
		}
	}
	Finatica.Controls.HD.expandNextNode(treeView);
	return false;
}

Finatica.Controls.HD.expandNextNode = function(treeView) {
	if (Finatica.Controls.HD.nodesToExpand != null && Finatica.Controls.HD.nodesToExpand.length > 0)
	{
		var pop = Finatica.Controls.HD.nodesToExpand.pop();
		var node = treeView.findNodeByValue(pop);
		if (node != null)
		{
			if (Finatica.Controls.HD.nodesToExpand.length > 0)
				node.expand();
			else 
			{
				node.select();
				node.scrollIntoView();  
			}
		}
	}
}

Finatica.Controls.HD.nodePopulating = function(sender, eventArgs)
{
   var node = eventArgs.get_node();
   var treeView = node.get_treeView();
   var treeAttributes = treeView.get_attributes();		   
   var context = eventArgs.get_context();
   context["DictionaryName"] = treeAttributes.getAttribute("DictionaryName"); 
   context["Language"] = treeAttributes.getAttribute("LCode");   
}


Finatica.Controls.HD.saveValues = function(textId, hiddenId, treeviewID, hiddenSortID) {
	var textBox = $get(textId);
	var hidden = $get(hiddenId);

	var checkedText = "";
	var checkedID = "";

	var treeView = $find(treeviewID);
	var nodes = treeView.get_checkedNodes();
	var treeAttributes = treeView.get_attributes();
	var restrict = treeAttributes.getAttribute("restrict");

	if (nodes != undefined) {
		var addedNodes = [];
		for (var i = 0; i < nodes.length; i++) {
			if (Finatica.Controls.HD.hasCheckedChildren(nodes[i]) == false) {
				var arr = [];
				var j = 0;
				var parentNode = nodes[i];
				checkedText += nodes[i].get_text() + "; ";
				while (parentNode != treeView) {
					if (Finatica.Controls.HD.isAlreadyStored(addedNodes, parentNode) == true)
						break;
					checkedID += parentNode.get_value() + ";";
					addedNodes.push(parentNode.get_value());
					parentNode = parentNode.get_parent();
				}

			}
		}

		$(textBox).val(checkedText);
		$(hidden).val(checkedID);
	}

	if (hiddenSortID != undefined && hiddenSortID != null) { Finatica.Controls.HD.insertHiddenSortValues(hiddenSortID); }


	closePopup();
	return false;
}

Finatica.Controls.HD.isAlreadyStored = function(addedNodes, node)
{
	var toContinue = false;
	for(var l=0;l<addedNodes.length;l++)
	{
		if(addedNodes[l] == node.get_value())
		{
			toContinue = true;
			break;
		}
	}
	return toContinue;
}

Finatica.Controls.HD.historyStepForward = function() {
	if (Finatica.Controls.HD.currentHistoryPosition == Finatica.Controls.HD.history.length - 1)
		return false;
	Finatica.Controls.HD.currentHistoryPosition += 1;
	Finatica.Controls.HD.showCurrentHistoryItem();
	Finatica.Controls.disableArrows();
	return false;
}

Finatica.Controls.HD.historyStepBack = function() {
	if (Finatica.Controls.HD.currentHistoryPosition < 1)
		return false;
	Finatica.Controls.HD.currentHistoryPosition -= 1;
	Finatica.Controls.HD.showCurrentHistoryItem();
	Finatica.Controls.disableArrows();
	return false;
}

Finatica.Controls.HD.showCurrentHistoryItem = function() {
	var currentHistoryItem = Finatica.Controls.HD.history[Finatica.Controls.HD.currentHistoryPosition];
	if (currentHistoryItem.toString().length < 5)
		Finatica.Controls.HD.ShowSectorInfo(currentHistoryItem, null);
	else {
		var proxy = new Finatica.WebServices.HDictionaryService();
		proxy.GetSectorDescriptionByGuid(currentHistoryItem, Finatica.Controls.HD.OnSectorsSerchSuccess, Finatica.Controls.HD.OnSectorsSerchFail, null);
	}
	return false;
}

Finatica.Controls.HD.historySaveStep = function(sid) {
	if (Finatica.Controls.HD.currentHistoryPosition < Finatica.Controls.HD.history.length - 1) {
		Finatica.Controls.HD.history.length = Finatica.Controls.HD.currentHistoryPosition + 1;
	}
	Finatica.Controls.HD.currentHistoryPosition++;
	Finatica.Controls.HD.history[Finatica.Controls.HD.currentHistoryPosition] = sid;
	Finatica.Controls.disableArrows();
}

Finatica.Controls.disableArrows = function()
{
	if (Finatica.Controls.HD.currentHistoryPosition > 0) {
		$("div.arrows > a.hdBackArrow").removeClass('a-disabled');

	}
	else {
		$("div.arrows > a.hdBackArrow").addClass('a-disabled');

	}

	if (Finatica.Controls.HD.currentHistoryPosition == Finatica.Controls.HD.history.length - 1) {
		$("div.arrows > a.hdForwardArrow").addClass('a-disabled');

	}
	else {
		$("div.arrows > a.hdForwardArrow").removeClass('a-disabled');
	} 
}

Finatica.Controls.HD.nodesToExpand = null;
Finatica.Controls.HD.history = new Array();
Finatica.Controls.HD.currentHistoryPosition = -1;

