//SuckerTree Horizontal Menu (Sept 14th, 06)
//By Dynamic Drive: http://www.dynamicdrive.com/style/

var menuids=["top_nav"] //Enter id(s) of SuckerTree UL menus, separated by commas

function buildsubmenus_horizontal(){
for (var i=0; i<menuids.length; i++){
  var ultags=document.getElementById(menuids[i]).getElementsByTagName("ul")
    for (var t=0; t<ultags.length; t++){
		if (ultags[t].parentNode.parentNode.id==menuids[i]){ //if this is a first level submenu
			//ultags[t].style.top=ultags[t].parentNode.offsetHeight+"px" //dynamically position first level submenus to be height of main menu item
			//ultags[t].parentNode.getElementsByTagName("a")[0].className="mainfoldericon"
		}
		else{ //else if this is a sub level menu (ul)
		  ultags[t].style.left=ultags[t-1].getElementsByTagName("a")[0].offsetWidth+"px" //position menu to the right of menu item that activated it
    	ultags[t].parentNode.getElementsByTagName("a")[0].className="subfoldericon"
		}
    ultags[t].parentNode.onmouseover=function(){
    this.getElementsByTagName("ul")[0].style.visibility="visible"
    }
    ultags[t].parentNode.onmouseout=function(){
    this.getElementsByTagName("ul")[0].style.visibility="hidden"
    }
    }
  }
}

if (window.addEventListener)
window.addEventListener("load", buildsubmenus_horizontal, false)
else if (window.attachEvent)
window.attachEvent("onload", buildsubmenus_horizontal)

// potvrdenia
function jsconfirm(thetext){
	return confirm(thetext);
}

// Functions for manipulation with SELECT field

/**
 * find optgroup by label
 * @param object select_field - select, where to search
 * @param string label - label of searched optgroup
 * @return object return object or null if not found
**/
function findOptgroupByLabel(select_field, label)
{
	var optgroups, o;
	optgroups = select_field.getElementsByTagName('OPTGROUP');

	for ( o = 0; o < optgroups.length; o++) {
		if( optgroups[o].label == label)
		{
			return optgroups[o];
		}
	}
	return null;
}

function addOptgroupToSelect(select_field, optGroup)
{
	var i, j,optgroups;

	optgroups = select_field.getElementsByTagName('OPTGROUP');

	for (i=0; i< optgroups.length; i++)
	{

		if (optgroups[i].attributes.getNamedItem('value').value == optGroup.attributes.getNamedItem('value').value)
		{
			return;
		}
		else
		{
			if (parseInt(optgroups[i].attributes.getNamedItem('value').value) > parseInt(optGroup.attributes.getNamedItem('value').value))
			{
				select_field.insertBefore(optGroup, optgroups[i]);
				return;
			}
		}
	}
	select_field.appendChild(optGroup);
	return	true;
}

function moveOptionOptgroup(select_field_to, inserted_index, select_field_from, i)
{
	var find_optgroup, optGrou;

	if(select_field_from.options[i].parentNode && select_field_from.options[i].parentNode.tagName == 'OPTGROUP')
	{

		optGroup = findOptgroupByLabel(select_field_to, select_field_from.options[i].parentNode.label);
		find_optgroup = 0;
		if(optGroup)
		{
			if( !(select_field_to.options[inserted_index].parentNode && select_field_to.options[inserted_index].parentNode.tagName == 'OPTGROUP' &&
				select_field_from.options[i].parentNode.label == select_field_to.options[inserted_index].parentNode.label))
			{
				optGroup.appendChild(select_field_to.options[inserted_index]);
			}

			find_optgroup = 1;
		}

		if (find_optgroup == 0)
		{
			optGroup = document.createElement('optgroup');
			optGroup.label = select_field_from.options[i].parentNode.label;
			if(select_field_from.options[i].parentNode.attributes.getNamedItem('value'))
			{
				optGroup.setAttribute('value', select_field_from.options[i].parentNode.attributes.getNamedItem('value').value);
			}
			if(select_field_from.options[i].parentNode.attributes.getNamedItem('ondblclick'))
			{
				optGroup.setAttribute('ondblclick', select_field_from.options[i].parentNode.attributes.getNamedItem('ondblclick').value);
			}

			optGroup.appendChild(select_field_to.options[inserted_index]);
			addOptgroupToSelect(select_field_to, optGroup);
		}
	}
}

function create_option(text, value, css_class)
{
	var my_option;

	my_option = new Option(text.replace(/(\s|&nbsp;)/g, "\xA0"), value); //replace = opera fix, \xA0 - hard space
	my_option.className 	= css_class;

	return my_option;
}

function createOptionFrom(source_option)
{
	return create_option(source_option.text, source_option.value, source_option.className);
}

function parseInt2( str )
{
	return str.substring(str.indexOf('_',0)+1);
}

function parseInt3( str, delimiter, count)
{
	if(!delimiter)
	{
		delimiter = '_';
	}

	if(!count || count == 0)
	{
		return str.substring(str.indexOf(delimiter,0)+1);
	}

   return str.substring(str.indexOf(delimiter,0)+1, str.indexOf(delimiter,0) + 1 + count);
}

function codeSelected(select_object)
{
	var i, codedString;

	codedString = '';
	for (i=0; i<select_object.options.length; i++)
	{
		codedString = codedString + ',' + parseInt2(select_object[i].value);
	}

	return codedString.substring(1);
}


function addItemToSelectFrom(select_field, select_option)
{
	return addItemToSelect(select_field, select_option.value, select_option.text, select_option.className);
}

// Add new item to select field
function addItemToSelect(select_field, value, description, css_class)
{
	var i, j, len, inserted_index = -1, optGroup, new_option;

	for (i=0; i<select_field.options.length; i++)
	{
		if (select_field.options[i].value == value)
		{
			inserted_index = i;
			break;
		}
		else
		{
			if ( parseInt(select_field.options[i].value) > parseInt(value))
			{

				if(select_field.options[i].parentNode && select_field.options[i].parentNode.tagName=='OPTGROUP'){
					len = select_field.options.length;
					new_option = create_option(description, value, css_class);
					optGroup = select_field.options[i].parentNode;
					select_field.options[len] = new_option;
					optGroup.insertBefore(new_option, select_field.options[i]);
				}
				else
				{
					new_option = create_option(description, value, css_class);
					len = select_field.options.length;
					select_field.options[len] = new_option;
					select_field.insertBefore(new_option, select_field.options[i]);

				}
				inserted_index = i;
				break;
			}

		}
	}

	if (i == select_field.options.length)
	{
		select_field.options[i] = create_option(description, value, css_class);
		inserted_index = i;
	}
	return	inserted_index;
}

// Remove field from select box
function removeItemFromSelect(select_field, index)
{
	select_field.options[index] = null;
}


//array[form name][select_name_to] = array of last selected values
var	last_moved_select_items = new Array();

// Move all selected fields from one select box to another
function __moveItemsFromSelectToSelect(select_field_from, select_field_to, do_selected)
{
	var i, inserted_index, last_index = 0, optGroup;

	last_moved_select_items[select_field_to.form.name] = new Array();
	last_moved_select_items[select_field_to.form.name][select_field_to.name] = new Array();


	for (i=select_field_from.options.length-1; i>=0; i--)
	{
		if (select_field_from.options[i].selected)
		{
			inserted_index = addItemToSelectFrom(select_field_to, select_field_from.options[i], do_selected);
			moveOptionOptgroup(select_field_to, inserted_index, select_field_from, i);

			select_field_to.options[inserted_index].selected = do_selected;

			last_moved_select_items[select_field_to.form.name][select_field_to.name][last_index] = select_field_from.options[i].value;
			last_index ++;
		}
	}

	for (i=select_field_from.options.length-1; i>=0; i--)
	{
		if (select_field_from.options[i] && select_field_from.options[i].selected ){

			optGroup = select_field_from.options[i].parentNode;
			removeItemFromSelect(select_field_from, i);

			if(optGroup && optGroup.tagName == 'OPTGROUP' && optGroup.getElementsByTagName('OPTION').length == 0)
			{
				select_field_from.removeChild(optGroup);
			}
		}
	}
}

var last_attempt;
last_attempt = (new Date()).getTime();
function moveItemsFromSelectToSelect(select_field_from, select_field_to, do_selected)
{
	var cur_attempt = (new Date()).getTime();
	if (cur_attempt - last_attempt < 500) {
		return false;
	}
	last_attempt = cur_attempt;
	if(select_field_to)
	{
		unselectAllItems(select_field_to);
	}
	__moveItemsFromSelectToSelect(select_field_from, select_field_to, do_selected);
}

// Select all fields in the select box
function selectAllItems(select_field)
{
	var i;

	for (i=0; i<select_field.options.length; i++)
	{
		select_field.options[i].selected = true;
	}
}

function unselectAllItems(select_field)
{
	var i;

	for (i=0; i<select_field.options.length; i++)
	{
		select_field.options[i].selected = false;
	}
}

function unselectAllSelectOneItem(select_field, value)
{
	var i;

	for (i=0; i<select_field.options.length; i++)
	{
		if(select_field.options[i].value == value)
		{
			select_field.options[i].selected = true;
			continue;
		}
		select_field.options[i].selected = false;
	}
}

// move all selected items up
function moveUp(select_field)
{
	var i, new_option;

	for (i=1; i<=select_field.options.length-1; i++)
	{
		if ((select_field.options[i].selected) && (!(select_field.options[i-1].selected)))
		{
					new_option = createOptionFrom(select_field.options[i-1]);
					select_field.options[i-1] = createOptionFrom(select_field.options[i]);
					select_field.options[i-1].selected = true;
					select_field.options[i] = new_option;
		}
	}
}

// move all selected items down
function moveDown(select_field)
{
	var i, new_option;

	for (i=select_field.options.length-2; i>=0; i--)
	{
		if ((select_field.options[i].selected) && (!(select_field.options[i+1].selected)))
		{
					new_option = createOptionFrom(select_field.options[i+1]);
					select_field.options[i+1] = createOptionFrom(select_field.options[i]);
					select_field.options[i+1].selected = true;
					select_field.options[i] = new_option;
		}
	}
}

// checks max allowed input count for education_levels
function checkMaxInput(from_field, to_field, max, alert_msg, alert_msg_end )
{
	var to_move, selected;

	selected = to_field.options.length;
	to_move = 0;

	for (i=0; i<from_field.options.length; i++)
	{
		if (from_field.options[i].selected)
		{
			to_move++;
		}
	}

	if (selected+to_move > max)
	{
		window.alert(alert_msg+' '+max + ' ' + alert_msg_end );
		return false;
	}

	return true;
}

/**
 * get positions cout in category position input
**/
function getInputPositionsCount(select_field)
{
	var selected = 0, i=0, pos_id=0;

	for (i=0; i<select_field.options.length; i++)
	{
		pos_id = getPosId(select_field.options[i].value)
		if(pos_id > 0)
		{
			selected ++;
		}
	}
	return selected;
}

/**
 * check function for category positions input
**/
function checkCatPosMaxInput(from_field, to_field, max, alert_msg, alert_msg_end )
{
	var to_move = 0, selected = 0, pos_id, all_cat_pos = 1;

	selected = getInputPositionsCount(to_field);

	for (i=0; i<from_field.options.length; i++)
	{
		pos_id = getPosId(from_field.options[i].value)

		if(from_field.options[i].selected && pos_id == 0)
		{
			all_cat_pos = 1;
		}
		else if (pos_id == 0)
		{
			all_cat_pos = 0;
		}

		if ( pos_id > 0 && (from_field.options[i].selected || all_cat_pos == 1 ) )
		{
			to_move++;
		}
	}

	if (selected+to_move > max)
	{
		window.alert(alert_msg+' '+max + ' ' + alert_msg_end );
		return false;
	}

	return true;
}

/**
 * get category id from cat pos value (e.g.: 5c1 = category_d=1)
**/
function getCatId(value)
{
	var pre_id;
	pre_id = parseInt2( value );
	return pre_id.substring(pre_id.indexOf('c')+1);
}

/**
 * get position id from cat pos value (e.g.: 5c1 = position_id=5)
**/
function getPosId(value)
{
	var pre_id, out;
	pre_id = parseInt2( value );

	out = pre_id.substring(0, pre_id.indexOf('c'));
	if(out == '')
	{
		return 0;
	}
	return out;
}


// position reordering
function categoryPositionUpdateForm(){
	var i,j, option;
	var left_req_categories	= new Array();
	var right_req_categories= new Array();
	var all_to_left	 = new Array();
	var all_to_right = new Array();
	if(0) return;
	// left colum
	if(last_moved_select_items['form'] && last_moved_select_items['form']['positions_all'])
	{
		for(i=0; i<last_moved_select_items['form']['positions_all'].length; i++)
		{
			option 	= last_moved_select_items['form']['positions_all'][i];
			cat_id 	= getCatId(option);
			pos_id 	= getPosId(option);
			if(pos_id > 0){	continue; } //position
			all_to_left[cat_id] 		= cat_id;
			left_req_categories[cat_id] = 1;
			right_req_categories[cat_id]= 0;
		}
	}
	for(i=0; i<document.form.elements['positions_all'].length; i++)
	{
		option 	= document.form.elements['positions_all'][i];
		cat_id 	= getCatId(option.value);
		pos_id 	= getPosId(option.value);
		if(!left_req_categories[cat_id])
		{
			left_req_categories[cat_id]=0;
		}
		if(!all_to_left[cat_id] && pos_id > 0)//position
		{
			left_req_categories[cat_id]++;
		}
	}
	// right colum
	if(last_moved_select_items['form'] && last_moved_select_items['form']['positions']){
	for(i=0; i<last_moved_select_items['form']['positions'].length; i++)
	{
		option 	= last_moved_select_items['form']['positions'][i];
		cat_id 	= getCatId(option);
		pos_id 	= getPosId(option);
		if(pos_id > 0){	continue; } //position
		all_to_right[cat_id] 		= cat_id;
		right_req_categories[cat_id]= 1;
		left_req_categories[cat_id] = 0;
	}}
	for(i=0; i<document.form.elements['positions'].length; i++)
	{
		option 	= document.form.elements['positions'][i];
		cat_id 	= getCatId(option.value);
		pos_id 	= getPosId(option.value);
		if(!right_req_categories[cat_id])
		{
			right_req_categories[cat_id]=0;
		}
		if(all_to_left[cat_id] && all_to_left[cat_id] > 0)
		{
			addItemToSelectFrom(document.form.elements['positions_all'], option, false);
			removeItemFromSelect(document.form.elements['positions'], i);
			i-=1;
		}
		if(!all_to_left[cat_id] && pos_id > 0) //position
		{
			right_req_categories[cat_id]++;
		}
		//left requested category - copy from right to left
		if(pos_id == 0 && left_req_categories[cat_id] && left_req_categories[cat_id] > 0)
		{
			addItemToSelectFrom(document.form.elements['positions_all'], option, false);
		}
	}
	// left colum
	for(i=0; i<document.form.elements['positions_all'].length; i++)
	{
		option 	= document.form.elements['positions_all'][i];
		cat_id 	= getCatId(option.value);
		pos_id 	= getPosId(option.value);
		if(all_to_right[cat_id] && all_to_right[cat_id] > 0)
		{
			addItemToSelectFrom(document.form.elements['positions'], option, false);
			removeItemFromSelect(document.form.elements['positions_all'], i);
			i-=1;
		}
		if(pos_id > 0){ continue; } //position
		if(right_req_categories[cat_id] && right_req_categories[cat_id] > 0)
		{
			addItemToSelectFrom(document.form.elements['positions'], option, false);
		}
		if(!left_req_categories[cat_id] || left_req_categories[cat_id] == 0)
		{
			removeItemFromSelect(document.form.elements['positions_all'], i);
			i-=1;
		}
	}
	// miss category check
	for(i=0; i<document.form.elements['positions'].length; i++)
	{
		option 	= document.form.elements['positions'][i];
		cat_id 	= getCatId(option.value);
		pos_id 	= getPosId(option.value);
		if(pos_id > 0){	continue; } //position
		//category
		if(!right_req_categories[cat_id] || right_req_categories[cat_id] == 0)
		{
			removeItemFromSelect(document.form.elements['positions'], i);
			i-=1;
		}
	}

	document.form.positions_coded.value=codeSelected(document.form.positions);
	return 1;
}
