function setCookie(key, value) {
  document.cookie = key+'='+escape(value)+'; path=/';
}

function setMenuCookies(expanded, selected) {
  setCookie("menu_expanded",expanded);
  setCookie("menu_selected",selected);
}

sfHover = function() {
  var sfEls = document.getElementById("menu_container").getElementsByTagName("li");
  for (var i=0; i<sfEls.length; i++) {
    sfEls[i].onmouseover=function() {
      this.className+=" sfhover";
    }
    sfEls[i].onmouseout=function() {
      this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
      this.className=this.className.replace(new RegExp(" sfhover"), "");
    }
  }
}
if (window.attachEvent) {
  window.attachEvent("onload", sfHover);
}

function openWindow(url) {
    window.open(url, "Window", "resizable,scrollbars,height=250,width=300");
}


function replaceEachOther(id1,id2)
{
  var tmp = $('#'+id1);
  var tmp2 = $('#'+id2);
  $('#'+id1).replaceWith('<div id=\"temp\"></div>');
  $('#'+id2).replaceWith(tmp);
  $('div#temp').replaceWith(tmp2);
  
  fieldListeners(id1);
  fieldListeners(id2);
}

function repairUpDownLink(element_id)
{
	//odkrywam wszystkie linki
	$('#'+element_id).find('span.upper').show();
	$('#'+element_id).find('span.lower').show();
	
	//dla głównych elementów
    $('#'+element_id).children('div:first').find('span.upper:first').hide();
    $('#'+element_id).children('div:last').find('span.lower:first').hide();
    
    //dla pytań w grupach
    $('#'+element_id+' div .group_inside').each(function(){
      $(this).children('div:first').find('span.upper:first').hide();
    });
    $('#'+element_id+' div .group_inside').each(function(){
      $(this).children('div:last').find('span.lower:first').hide();
    });
    
    //dla odpowiedzi pytań
    $('#'+element_id+' div .quest_inside').each(function(){
      $(this).children('div:first').find('span.upper:first').hide();
    });
    $('#'+element_id+' div .quest_inside').each(function(){
      $(this).children('div:last').find('span.lower:first').hide();
    });
    
    $('#'+element_id+' div .group_inside_answers').each(function(){
	  $(this).children('div:first').find('span.upper:first').hide();
	});
	$('#'+element_id+' div .group_inside_answers').each(function(){
	  $(this).children('div:last').find('span.lower:first').hide();
	});
}


function fieldListeners(element_id)
{
	function setEdited(){
		$('#edited').val(1);
	    $('span.edited').text('Dokonano zmian');
	    $('span.saved').text('');
	    $('#quest_save').addClass('edited');
	}
	
	$('#'+element_id+' input').keypress(function(k){
	  if(k.keyCode==0 || k.keyCode==8 || k.keyCode==46){
	    setEdited();
	  }
	});
	$('#'+element_id+' textarea').keypress(function(k){
	  if(k.keyCode==0 || k.keyCode==8 || k.keyCode==46){
	    setEdited();
      }
	});
	$('#'+element_id+' input').change(function(){
      setEdited();
	});
	$('#'+element_id+' textarea').change(function(){
	  setEdited();
	});
	$('#'+element_id+' select').change(function(){
	  setEdited();
	});
	
	$('.select_type_many').change(function(){
		$(this).parent().next().show();//przycisk
		if( $(this).val()==4 )//ocena
		{
			var tmp = $(this).parent().next().next();//div z odpowiedziami
			if( tmp.children().length > 1 )
			{
			  alert('Dla `Oceny` pozostaw tylko jedn± odpowiedĽ!');
			  $(this).val( $(this).attr('oldValue') );
			  return false;
			}
			if( tmp.children().length == 1 )
			{
				$(this).parent().next().hide();
			}
			$(this).next().show();//rating_text
		}
		else
		{
		  $(this).next().hide();//rating_text
		}
		if( $(this).val()==3 )//otwarte
		{
			var tmp = $(this).parent().next().next();//div z odpowiedziami
			if( tmp.children().length > 0 )
			{
			  alert('Dla pytań `otwartych` nie mog± być dodane odpowiedzi!');
			  $(this).val( $(this).attr('oldValue') );
			  return false;
			}
			else
			{
			  $(this).parent().next().hide();
			}
		}
		
		if( $(this).val()==1 || $(this).val()==2 )//wyboru
		{
			$(this).next().next().show();
		}
		else
		{
			$(this).next().next().hide();
		}
		
		$(this).attr('oldValue',$(this).val());
	});
	
	$('.quest button').click(function(){
	  var tmp = $(this).parent().prev().children('select');
	  if( tmp.val()==4 )//ocena
	  {
		  $(this).parent().hide();
	  }
	});
	
}

function showHide(element_id)
{
  //ukrywanie typu wariantu
  $('.group_inside_answers').each(function(){
	if( $(this).children('div').length>0 ){
	  $(this).prev().show();
	} else {
	  $(this).prev().hide();
	}
  });
  
  $('.quest button').each(function(){
    var tmp = $(this).parent().prev().children('select');
    $(this).parent().show();//przycisk
    
    if( tmp.val()==4 )//ocena
    {
      if( $(this).parent().next().children().length == 1 )
	  {
    	$(this).parent().hide();
	  }
      tmp.next().show();//rating_text
    }
    else
	{
    	tmp.next().hide();//rating_text
	}
    if( tmp.val()==3 )//otwarte
    {
      $(this).parent().hide();
    }
    if( tmp.val()==1 || tmp.val()==2 )//wyboru
	{
    	tmp.next().next().show();
	}
	else
	{
		tmp.next().next().hide();
	}
  });
}

function usunPytanie(id,grupa)
{
  if(grupa){
	txt='tę grupę';
  } else {
	txt='to pytanie';
  }
  if(confirm('Czy na pewno chcesz usun±ć '+txt+'?'))
  {
    xajax_usunPytanie(id);
  }
}
function usunOdpowiedz(id,grupa)
{ 
  if(grupa){
	txt='ten wariant';
  } else {
	txt='tę odpowiedĽ';
  }
  if(confirm('Czy na pewno chcesz usun±ć '+txt+'?'))
  {
    xajax_usunOdpowiedz(id);
  }
}

function zapiszPytania()
{
  xajax_zapiszPytania(xajax.getFormValues('quest_form'));
  $('.save_confirm').show('slow');
  setTimeout(function(){
	$('.save_confirm').hide('slow');
  },2000);
}


