
function ToggleAccountRowsVisibility(check) {
	if (check.checked) {
		document.getElementById('loginRow').style.display = '';
		document.getElementById('passwdRow').style.display = '';
		document.getElementById('passwd2Row').style.display = '';
	}
	else {
		document.getElementById('loginRow').style.display = 'none';
		document.getElementById('passwdRow').style.display = 'none';
		document.getElementById('passwd2Row').style.display = 'none';
	}
}

function ToggleCommentFormVisibility(zwin, rozwin, zawszeRozwijaj) {
	rozwiniete = document.getElementById('formularzContainer').style.display;
	if (rozwiniete == 'block' && zawszeRozwijaj == 0) {
		document.getElementById('formularzContainer').style.display = 'none';
		document.getElementById('formularzZwinRozwin').innerHTML = rozwin;
	}
	else {
		document.getElementById('formularzContainer').style.display = 'block';
		document.getElementById('formularzZwinRozwin').innerHTML = zwin;
	}
}

function ToggleCompanyRowsVisibility(pokaz) {
	if (pokaz) {
		document.getElementById('firmaRow').style.display = '';
		document.getElementById('nipRow').style.display = '';
	}
	else {
		document.getElementById('firmaRow').style.display = 'none';
		document.getElementById('nipRow').style.display = 'none';
	}
}

function handleEnter(field, event, goto) {
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (keyCode == 13) {
        document.getElementById(goto).click();
      	return false;
	}
	else return true;
}

function handleSubmit(field, event, goto, id) {
	id = (id == null || typeof(id) == "undefined") ? 0 : id;
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (keyCode == 13) {
        	document.getElementsByName(goto)[id].submit();
      		return false;
	} else return true;
}

var http='';

function getHTTPObject(){var xmlhttp; /*@cc_on @if (@_jscript_version>=5)try {xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");} catch(e){try{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");} catch(E){xmlhttp=false;}              } 
         @else
          xmlhttp = false;         @end @*/
			if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {try {xmlhttp = new XMLHttpRequest();           } catch (e) {xmlhttp = false;           }          }          return xmlhttp;        }

function httpResponse()
{
	if (http.readyState == 4) {
		var response=http.responseText;
		response=response.split('|');
		if(response[0]=='')
			return false;
		else
		{
			var item=response[0].split(":");
			var uid='';
			for(i=0;i<item.length-1;i++)
			{
					if(i>=5)
						uid+="&#"+item[i]+";";

			}
			document.getElementById(item[item.length-1]+'_info').children[0].innerHTML='<span>email:</span> <a href="mailto:'+uid+'@prz.edu.pl">'+uid+'@prz.edu.pl</a>';
		}
	}
}

function pracownicyParser(root,url)
{	
	for(var i=0;i<root.children.length;i++)
	{
		if(root.children[i].tagName.toLowerCase()!='div' && root.children[i].tagName.toLowerCase()!='h1')
		{
			root.children[i].onclick=function()
			{
				childrenOnClick(this,url)
			}
		}
	}
	var h3=root.getElementsByTagName("h3");
	if(h3.length==1 && root.getElementsByTagName("h1").length==0)
		childrenOnClick(h3[0],url);
}

function childrenOnClick(that,url)
{
	var item=document.getElementById('jednostki_pracownicy');
	for(var j=0;j<item.children.length;j++)
	{
		if(item.children[j]===that || item.children[j].nodeType!=1)
			continue;
		if(item.children[j].tagName.toLowerCase()!='div' && item.children[j].tagName.toLowerCase()!='h1')
		{
			document.getElementById(item.children[j].id+'_contener').style.display='none';
			//item.children[j].setAttribute('class',item.children[j].getAttribute('class').replace(' on','').replace('tree-minus','tree-plus'));
			item.children[j].className=item.children[j].className.replace(' on','').replace('tree-minus','tree-plus');
		}
	}
	if(document.getElementById(that.id+'_contener').style.display=='none')
	{
		if(document.getElementById(that.id+'_m')==null)
		{
			var contener=document.createElement("li");
			contener.setAttribute('id', that.id+'_m');
			if(document.getElementById(that.id+'_info').children.length>0)
				document.getElementById(that.id+'_info').insertBefore(contener,document.getElementById(that.id+'_info').children[0]);
			else
				document.getElementById(that.id+'_info').appendChild(contener);
			
			contener.innerHTML='&nbsp;';
			http=getHTTPObject();	
			http.open("POST",url+'go:szczegoly', true);
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			http.onreadystatechange = httpResponse;
			http.send('a='+encodeURIComponent('szczegoly')+'&id='+encodeURIComponent(that.id));
		}
		document.getElementById(that.id+'_contener').style.display='block';
		//that.setAttribute('class',that.getAttribute('class').replace('tree-plus','tree-minus')+' on');
		that.className=that.className.replace('tree-plus','tree-minus')+' on';
	}else if(document.getElementById(that.id+'_contener').style.display=='block')
	{
		document.getElementById(that.id+'_contener').style.display='none';
		//that.setAttribute('class',that.getAttribute('class').replace(' on','').replace('tree-minus','tree-plus'));
		that.className=that.className.replace(' on','').replace('tree-minus','tree-plus');
	}
}

function arrayCompare(a1, a2)
{
	if (a1.length != a2.length) return false;
	var length = a2.length;
	for (var i = 0; i < length; i++)
	{
		if (a1[i] !== a2[i]) return false;
	}
	return true;
}

	
function inArray(needle, haystack)
{
	var length = haystack.length;
	for(var i = 0; i < length; i++)
	{
		if(typeof haystack[i] == 'object')
		{
			if(arrayCompare(haystack[i], needle)) return true;
		}else
		{
			if(haystack[i] == needle) return true;
		}
	}
	return false;
}

function treeParser(root)
{
	for(var i=0;i<root.children.length;i++)
	{
		//alert(root.innerHTML);
		if(root.children[i]===null)
			break;
		
		if(root.children[i].tagName.toLowerCase()=='div')
			continue;
		
		var subTagName=root.children[i].tagName;
		//alert(root.children[i].nextSibling+' | '+subTagName+' | '+root.children[i].nextSibling.tagName)
		if(root.children[i].nextSibling!=null && subTagName!=root.children[i].nextSibling.tagName)
		{
			var contener=document.createElement("div");
			//contener.setAttribute('class', 'contener'+'-'+root.children[i].tagName.toLowerCase());
			contener.className='contener'+'-'+root.children[i].tagName.toLowerCase();
			contener.setAttribute('id', root.children[i].id+'_contener');
			root.insertBefore(contener,root.children[i].nextSibling);
			i++;
			var j=i+1;
			for(;;)
			{
				if(root.children[j]!=null && root.children[j].tagName!=subTagName)
				{
					contener.appendChild(root.children[j].cloneNode(true));
					root.removeChild(root.children[j]);
				}else
					break;
			}
			treeParser(contener);
		}
	}
}

function lastBranch(root)
{
	for(var i=root.children.length-1;i>=0;i--)
	{
		if(root.children[i].tagName.toLowerCase()!='div')
		{
			//root.children[i].setAttribute('class',root.children[i].className+' tree-branch-last');
			root.children[i].className=root.children[i].className+' tree-branch-last';
			break;
		}
	}
	
	for(var i=0;i<root.children.length;i++)
	{
		if(root.children[i].tagName.toLowerCase()=='div')
		{
			if(root.children[i].children.length>1 && root.children[i].children[(root.children[i].children.length-1)].tagName.toLowerCase()=='div')
				root.children[i].children[(root.children[i].children.length-1)].className=root.children[i].children[(root.children[i].children.length-1)].className.replace(' root','')+'-last';
			lastBranch(root.children[i]);
		}
		if(root.children[i].tagName.toLowerCase()!='div' && document.getElementById(root.children[i].id+'_contener')==null)
			root.children[i].removeChild(root.children[i].children[0])
	}
}

function itemsTree(root,action,ex)
{	
	if(document.getElementById('tree_button')!=null)
	{		
		if(root.getElementsByTagName("h3").length>0)
		{
			var uButton=document.getElementById('tree_button');
			uButton.parentNode.style.display='block';
			if(action=='showTree')
			{
				
				uButton.innerHTML = (typeof button_value != 'undefined') ? button_value[0] : 'Przywróć stan początkowy';
				uButton.onclick=function(){itemsTree(tree_root,'hideTree',ex);}
				
			}else
			{
				uButton.innerHTML = (typeof button_value != 'undefined') ? button_value[1] : 'Rozwiń wszystkie pozycje';
				uButton.onclick=function(){itemsTree(tree_root,'showTree',ex);}
			}
		}
	}
	var dsp = (action=='showTree') ? 'block' : 'none';
	
	for(var i=0;i<root.children.length;i++)
	{			
		if(root.children[i].className.indexOf('contener')!=-1 && (root.children[i-1].tagName.toLowerCase()!='div' || i-1<0))
		{
			if(i-1<0 || (root.children[i-1].id!=tree_root.children[0].id))
			{
				if(root.children[i].id.split("_")[0]!=tree_root.children[0].id)
				{
					drawTree(root.children[i-1],root.children[i-1].className,action);
					for(var j=i+1;j<root.children.length;j++)
					{
						if(root.children[j].tagName.toLowerCase()=='div')
							root.children[j].style.display=dsp;
						else
							break;
					}
					document.getElementById(root.children[i-1].id+'_contener').style.display=dsp;
				}
				
				if(inArray(root.children[i].id.split("_")[0],ex))
				{
					drawTree(document.getElementById(root.children[i].id.split("_")[0]),document.getElementById(root.children[i].id.split("_")[0]).className,'showTree');
					root.children[i].style.display='block';
				}
					
			}
		}
		if(root.children[i].tagName.toLowerCase()=='div')
				itemsTree(root.children[i],action,ex);
	}
}

function drawTree(pNode,cAtr,action)
{
	if(action=='showTree')
		var item=new Array('plus','minus');
	else
		var item=new Array('minus','plus');

	if(cAtr.indexOf('ree-branch-last')!=-1)
	{
		//pNode.setAttribute('class',pNode.className.replace('tree-branch-last','tree-'+item[1]+'-last'));
		pNode.className=pNode.className.replace('tree-branch-last','tree-'+item[1]+'-last');
	}else if(cAtr.indexOf('tree-'+item[0]+'-next')!=-1)
	{
		//pNode.setAttribute('class',pNode.className.replace('tree-'+item[0]+'-next','tree-'+item[1]+'-next'));
		pNode.className=pNode.className.replace('tree-'+item[0]+'-next','tree-'+item[1]+'-next');
	}else if(cAtr.indexOf('tree-'+item[0]+'-last')!=-1)
	{		
		//pNode.setAttribute('class',pNode.className.replace('tree-'+item[0]+'-last','tree-'+item[1]+'-last'));
		pNode.className=pNode.className.replace('tree-'+item[0]+'-last','tree-'+item[1]+'-last');
	}else if(cAtr.indexOf('tree-'+item[1]+'-next')!=-1)
		return true;
	else if(cAtr.indexOf('tree-'+item[1]+'-last')!=-1)
		return true;
	else if(pNode.className.indexOf('root')!=-1)
	{
		//pNode.setAttribute('class',pNode.className+' tree-'+item[1]+'-next');
		pNode.className=pNode.className+' tree-'+item[1]+'-next';
	}else
	{
		//pNode.setAttribute('class','tree-'+item[1]+'-next');
		pNode.className='tree-'+item[1]+'-next';
	}
}

function itemOnClick(item)
{
	
	if(document.getElementById(item.parentNode.id+'_contener').style.display=='none')
	{
		document.getElementById(item.parentNode.id+'_contener').style.display='block';
		if(document.getElementById(item.parentNode.id+'_contener').nextSibling!=null && document.getElementById(item.parentNode.id+'_contener').nextSibling.tagName.toLowerCase()=='div')
		{
			if(document.getElementById(document.getElementById(item.parentNode.id+'_contener').nextSibling.id.split("_")[0]).className.replace('root','').split(" ")[1].split('-')[1]=='minus')
				document.getElementById(item.parentNode.id+'_contener').nextSibling.style.display='block';
		}
		drawTree(item.parentNode,item.parentNode.className,'showTree');
	}else
	{
		document.getElementById(item.parentNode.id+'_contener').style.display='none';
		if(document.getElementById(item.parentNode.id+'_contener').nextSibling!=null &&  document.getElementById(item.parentNode.id+'_contener').nextSibling.tagName.toLowerCase()=='div')
		{
			document.getElementById(item.parentNode.id+'_contener').nextSibling.style.display='none';
		}
		drawTree(item.parentNode,item.parentNode.className,'hideTree');
	}
}
