
// ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// 
// Coded by Travis Beckham
// http://www.squidfingers.com | http://www.podlob.com
// If want to use this code, feel free to do so, but please leave this message intact.
//
// ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// --- version date: 06/02/03 ---------------------------------------------------------

// ||||||||||||||||||||||||||||||||||||||||||||||||||
// Node Functions

if(!window.Node){
	var Node = {ELEMENT_NODE : 1, TEXT_NODE : 3};
}
function checkNode(node, filter){
//	if (filter == "A") alert(node.nodeName.toUpperCase().toUpperCase() + ' '+ filter.toUpperCase());
	return (filter == null || node.nodeType == Node[filter] || node.nodeName.toUpperCase() == filter.toUpperCase());
}
function getChildren(node, filter){
	var result = new Array();
	var children = node.childNodes;
	for(var i = 0; i < children.length; i++){
		if(checkNode(children[i], filter)) result[result.length] = children[i];
	}
	return result;
}
function getChildrenByElement(node){
	return getChildren(node, "ELEMENT_NODE");
}
function getFirstChild(node, filter){
	var child;
	var children = node.childNodes;
	for(var i = 0; i < children.length; i++){
		child = children[i];
		if(checkNode(child, filter)) return child;
	}
	return null;
}
function getFirstChildByText(node){
	return getFirstChild(node, "TEXT_NODE");
}
function getNextSibling(node, filter){
	for(var sibling = node.nextSibling; sibling != null; sibling = sibling.nextSibling){
		if(checkNode(sibling, filter)) return sibling;
	}
	return null;
}
function getNextSiblingByElement(node){
	return getNextSibling(node, "ELEMENT_NODE");
}

// ||||||||||||||||||||||||||||||||||||||||||||||||||
// Menu Functions & Properties

var activeMenu = null;
var activeSubMenu = null;

function showMenu(){
	if(activeMenu){
		activeMenu.parentNode.className = activeMenu.parentNode.className.replace('current','');
		getNextSiblingByElement(activeMenu).style.display = "none";
	}
	if(this == activeMenu){
		activeMenu = null;
	}else{
		this.parentNode.className += " current";
		getNextSiblingByElement(this).style.display = "block";
		activeMenu = this;
	}
	return false;
}

function initMenu(){
	if (!document.getElementById("order_check")) return false;
	var menus, menu, text, a, i, anode, sublist;
	menus = getChildrenByElement(document.getElementById("order_menu"));
	for(i = 0; i < menus.length; i++){
		menu = menus[i];
		anode = getFirstChild(menu, "A");
		if (anode)
		{
			anode.onclick = showItems;
			anode.onfocus = function(){this.blur()};
		}
	}
	
	initChecklist();	
}

// ||||||||||||||||||||||||||||||||||||||||||||||||||
if(document.createElement) window.onload = initMenu;

// ||||||||||||||||||||||||||||||||||||||||||||||||||
// Инициализация Checklist
function initChecklist() {
	if (document.all && document.getElementById) {
		// Get all unordered lists
		var lists = document.getElementsByTagName("ul");
	
		for (i = 0; i < lists.length; i++) {
			var theList = lists[i];
				
			// Only work with those having the class "checklist"
			if (theList.className.indexOf("checklist") > -1) {
				var labels = theList.getElementsByTagName("label");
						
				// Привязываем обработчик событьй к label
				for (var j = 0; j < labels.length; j++) {
					var theLabel = labels[j];
					theLabel.onmouseover = function() { this.className += " hover"; };
					theLabel.onmouseout = function() { this.className = this.className.replace(" hover", ""); };
				}
			}
		}
	}

	var lists = document.getElementsByTagName("ul");
	
	for (i = 0; i < lists.length; i++) {
		var theList = lists[i];
				
		// Only work with those having the class "checklist"
		if (theList.className.indexOf("checklist") > -1) {
			var boxes = theList.getElementsByTagName("input");				
						
			// Привязываем обработчик событьй к checkbox
			for (var j = 0; j < boxes.length; j++) {
				var theBox = boxes[j];
				theBox.onclick = function() { updateItemFields(this) };
			}
		}
	}
	
	
}

// Показываем только элементы относящиеся к разделу
function showItems()
{
//	alert('showItems');
//	alert(this.getAttribute('rel'));
	t_href = this.getAttribute('rel');
	
	if(activeSubMenu){
		activeSubMenu.parentNode.className = activeSubMenu.parentNode.className.replace('current','');
	}
	if(this == activeSubMenu){
		activeSubMenu = null;
	}else{
		this.parentNode.className += " current";
		activeSubMenu = this;
	}
	
//	this.parentNode.className = this.parentNode.className + ' current';
	
	var lists = document.getElementsByTagName("ul");
				
	for (i = 0; i < lists.length; i++) {
		var theList = lists[i];
					
		// Only work with those having the class "checklist"
		if (theList.className.indexOf("checklist") > -1) {
			var labels = theList.getElementsByTagName("li");
						
			for (var j = 0; j < labels.length; j++) {
				var theLabel = labels[j];
//				alert(t_href + ' ' + l_href);
//				if (theLabel.getAttribute('rel') != selGroup.value )
				if (theLabel.getAttribute('rel') ==  this.getAttribute('rel'))
				{
					theLabel.style.display = "block";					
				}
				else
				{
					theLabel.style.display = "none";
				}
			}
		}
	}
	
	return false;
}

// Показываем поля заказа для выбранных элементов	
function updateItemFields(box)
{
//	var sDebug = document.getElementById("debag");
	
	
	// Получаем данные о элементе
	var itemTitle = box.parentNode.textContent ? box.parentNode.textContent : box.parentNode.innerText ;
	var itemId = box.id.replace("ch_","");
	
	var trPattern = document.getElementById("orderpattern");
	var tableOrder = trPattern.parentNode;
	
	// Если ВКЛ
	if (box.checked) {
		// Получаем шаблон строки таблицы
		
		var trNew = trPattern.cloneNode(true);
		trNew.id = '' ;
		trNew.setAttribute('rel', itemId);
	
		var tdNew = trNew.getElementsByTagName("td");
		
		var fieldTitle = tdNew[0].getElementsByTagName("input");
		fieldTitle[0].value = itemTitle;
		
		var tdTitle = tdNew[1];
		tdTitle.innerHTML = itemTitle;
	
		var newNode = tableOrder.appendChild(trNew);
	}
	// Если ВЫКЛ
	else {
		// Перебираем все имеющиеся строки
		var arrayTr = tableOrder.getElementsByTagName("tr");
		
		for (var j = 0; j < arrayTr.length; j++) {
			if (arrayTr[j].getAttribute('rel') == itemId)
				tableOrder.removeChild(arrayTr[j]);
		}
	}
	
	// Получаем все свойства объекта
	// for (var i in obj) result += "obj." + i + " = " +  "<br />\n";
	/*
	if (box.checked) {
		sDebug.value = sDebug.value + itemId + ' ' + itemTitle + ' checked\n';
	}
	else {
		sDebug.value = sDebug.value +  itemId + ' ' + itemTitle + ' unchecked\n';
	}
	*/
	
}

function removeItemFields(button)
{
	// Получаем данные о строке
	var theTr = button.parentNode.parentNode;
	var tableOrder = theTr.parentNode;
	var itemId = theTr.getAttribute('rel')
	
	// Проходим по checkbox-ам и убираем флаг с нужного
	var lists = document.getElementsByTagName("ul");
	
	for (i = 0; i < lists.length; i++) {
		var theList = lists[i];
				
		// Only work with those having the class "checklist"
		if (theList.className.indexOf("checklist") > -1) {
			var boxes = theList.getElementsByTagName("input");				

			for (var j = 0; j < boxes.length; j++) {
				var theBox = boxes[j];
				if (theBox.id == 'ch_'+itemId) theBox.checked = false;
			}
		}
	}	
	
	tableOrder.removeChild(theTr);
}
