	
	// Usuwa osobe submitujac formularz
	
	function removePerson(_this) {
	  if(confirm('Czy na pewno chcesz usun±ć osobę?'))
		  _this.parentNode.submit();
	}	  
	// Usuwa dzial submitujac formularz
	
	function removeDepartment(_this) {
	  if(confirm('Czy na pewno chcesz usun±ć dział?'))
		  _this.parentNode.submit();
	}

  // Ukrywa/pokazuje wiersz z szczegolami osoby
	
	function showHideDetailsRow(_this, domain) {
		_this.src=_this.src==domain+'img/moreSN.gif' ? domain+'img/moreNS.gif' : domain+'img/moreSN.gif'; 
	  var tr=_this.parentNode.parentNode.nextSibling;
		tr.style.display=tr.style.display=='none' ? '' : 'none';
	}
	
	// Ukrywa/pokazuje tresc pod naglowkiem
	
	function showHideElement(_this) {
	  _this.nextSibling.style.display=_this.nextSibling.style.display=='none' ? '' : 'none';
	}
	
	// Tablica zawierajaca backup edytowanych wartosci
	
	var InputDataBackup=new Array();
	InputDataBackup['k_persons']=new Array();
	InputDataBackup['k_departments']=new Array();
	
	// Zmienia wartosc style.display z 'none' na '' i na odwrot kazdego z dzieci
	
	function showHideChilds(node) {
	  for(var i=0 in node.childNodes) {
		  if(node.childNodes[i].nodeType==1)
			  if(node.childNodes[i].style)
			    node.childNodes[i].style.display=node.childNodes[i].style.display=='none' ? '' : 'none';
		}		
	}
	
	// Zwraca pierwszego napotkanego rodzica, ktory jest <tr>
	
	function getFirstTrParent(_this) {
	  if(_this.parentNode)
		  return _this.parentNode.nodeName=='TR' ? _this.parentNode : getFirstTrParent(_this.parentNode);  
	}

	// Przelacza wiersz do edycji
	
	function editRow(_this) {
  	var tr=getFirstTrParent(_this);
		var id=new String();
		var table=new String();
		if(tr.getAttribute('departmentId')) {
		  id=tr.getAttribute('departmentId');
			table='k_departments';
		}
		if(tr.getAttribute('personId')) {
		  id=tr.getAttribute('personId');
			table='k_persons';
		}
		for(i in cells=getCells(tr.childNodes)) {
		  var td=cells[i];
			if(td.firstChild.nodeName.toLowerCase()=='p' && td.firstChild.nodeType==1) {
			  td.setAttribute('onclick', '');
				td.style.cursor='auto';
			  var content=td.firstChild.firstChild!=null ? td.firstChild.firstChild.nodeValue : ''
				var name=td.firstChild.getAttribute('name');
				if(InputDataBackup[table][id]) {
				  InputDataBackup[table][id][name]=content;
				} else {
				  InputDataBackup[table][id]=new Array();
					InputDataBackup[table][id][name]=content;					
				}
        td.innerHTML='<input type="text" class="Text" name="'+name+'" class="InputText" value="'+(content=='' ? '' : content)+'"/>';
			} else if('1'!=td.getAttribute('static')) {
			  showHideChilds(td);				  
			}	
		} // koniec petli
	}	

	// Przelacza wiersz edytowany jako readonly i jezli byly zmiany wprowadza je w bazie danych
	
	function makeReadonlyRow(_this) {
  	var tr=getFirstTrParent(_this);
		var id=new String();
		var table=new String();
		if(tr.getAttribute('departmentId')) {
		  id=tr.getAttribute('departmentId');
			table='k_departments';
		}
		if(tr.getAttribute('personId')) {
		  id=tr.getAttribute('personId');
			table='k_persons';
		}	
	  var save=_this.className=='ImgSave';
		for(i in cells=getCells(_this.parentNode.parentNode.childNodes)) {
		  var td=cells[i];
			if(td.firstChild.nodeName.toLowerCase()=='input' && td.firstChild.nodeType==1) {
			  td.setAttribute('onclick', 'editRow(this)');
				td.style.cursor='help';
			  var content=td.firstChild.value;
				var name=td.firstChild.getAttribute('name');	
				if(save && td.firstChild.value!=InputDataBackup[table][id][name]) {
				  actualize(id, name, td.firstChild.value, table);
				}	
				content=save ? td.firstChild.value : InputDataBackup[table][id][name];
			  td.innerHTML='<p name="'+name+'" onclick="editRow(this)">'+(content.length==0 ? '' : content)+'</p>';
			}
			else if('1'!=td.getAttribute('static'))
			  showHideChilds(td);  
		} // koniec petli
	}
	
	// Pobiera te dzieci tr, ktore sa td
	
	function getCells(tr) {
	  var result=new Array();
	  for(var i=0;i<tr.length;i++)
		  if(tr.item(i).nodeType==1 && tr.item(i).nodeName.toLowerCase()=='td')
			  result.push(tr.item(i))
		return result;
	}
	
	// Wybiera, ktore elementy wstawic do selecta
	
	function managerSelect(operation) {
	  if(getSelectedDepartmentId()==0) {
		  alert('Wybierz dział');
			return false;
		} else 
		  var DepId=getSelectedDepartmentId();	
		
  	var sJoin=document.getElementById("SJoin");
    var sPersons=document.getElementById("SPersons");
		
		if(operation=='add') {		 
		  for(var i=0;i<sPersons.length;i++)
		    if(sPersons.options[i].selected)
			    if(!isInSelect(id=sPersons.options[i].value, sJoin)) {
			      insertOption(makeOption(sPersons.options[i].text,id),sJoin);
				    setPersonsForDepartment(sPersons.options[i].value, DepId);						
				  }
    } else if(operation=='remove') {
		  for(var i=(sJoin.length-1);i>=0;i--)
			  if(sJoin.options[i].selected) {
			    removePersonsForDepartment(sJoin.options[i].value, DepId)
			    sJoin.remove(i);
			  }			  
		}											 
	}
	
	// Sprawdza, czy option o danym id jest juz w selekcie
	
	function isInSelect(_id, _select) {
	  for(var i=0;i<_select.length;i++)
		  if(_select.options[i].value==_id)
			  return true;
		return false;		
	}
	
	// Zwraca id zaznaczonego dzialu
	
	function getSelectedDepartmentId() {
	  var sDepartments=document.getElementById("SDepratments");
		for(var i=0;i<sDepartments.length;i++)
		  if(sDepartments.options[i].selected)
			  return sDepartments.options[i].value;
		return false;		
	}
	
	// Tworzy element option
	
	function makeOption(_text, _value) {
    var optionElem=document.createElement('option');
    optionElem.text=_text;
    optionElem.value=_value;
		return optionElem;
	}
	
	// Wstawia element option do selecta
	
	function insertOption(_optionElem, _select) {
    try {
      _select.add(_optionElem,null); // standards compliant
    } catch(ex) {
      _select.add(_optionElem); // IE only
    }
  } 
	
	// Aktualizuje selecta z osobami przypozadkowanymi do danej kategorii
	
	function makePersonsList() {
  	var sJoin=document.getElementById("SJoin");	  
	  var sDepartments=document.getElementById("SDepratments");
		for(var i=(sJoin.length-1);i>=0;i--)
		  sJoin.remove(i);
    getPersonsForDepartment(getSelectedDepartmentId())
	}
	
	// Zapisuje osobe do danej kategorii
	
	function setPersonsForDepartment(personId, departmentId) {
		advAJAX.get({
      url: "http://"+location.host+"/panel/m/94/ajax/setPersonsForDepartment.php?randomMurzynId="+Math.random(),
      uniqueParameter: "custom_unique_param",
	    parameters : {
        "personId" : personId,
				"departmentId" : departmentId
      },
      onSuccess : function(obj) { },
      onError : function(obj) { alert("Error: " + obj.status); }
		});	
	}	
	
	// Usuwa osobe z danej kategorii
	
	function removePersonsForDepartment(personId, departmentId) {
		advAJAX.get({
      url: "http://"+location.host+"/panel/m/94/ajax/removePersonsForDepartment.php?randomMurzynId="+Math.random(),
      uniqueParameter: "custom_unique_param",			
      parameters : {
        "personId" : personId,
				"departmentId" : departmentId
      },
      onSuccess : function(obj) { },
      onError : function(obj) { alert("Error: " + obj.status); }
		});	
	}		
	
	// Pobiera osoby, ktore sa przypozadkowane do danej kategorii, funkcjonalosc dla panelu
	
	function getPersonsForDepartment(id) {
	  var sJoin=document.getElementById("SJoin");	 
		advAJAX.get({
      url: "http://"+location.host+"/panel/m/94/ajax/getPersonsForDepartment.php?randomMurzynId="+Math.random(),
			uniqueParameter: "custom_unique_param",
      parameters : {
        "id" : id
      },
      onSuccess : function(obj) {
			  var persons=obj.responseXML.getElementsByTagName("persons").item(0);
				var personsList=persons.childNodes
				for(var i=0;i<personsList.length;i++) {
				  var nameNode=personsList[i].getElementsByTagName("name").item(0);
					var name=nameNode.firstChild.data;
				  var surnameNode=personsList[i].getElementsByTagName("surname").item(0);
					var surname=surnameNode.firstChild.data;
					var personId=personsList[i].getAttribute("id");
					insertOption(makeOption(name+' '+surname, personId), sJoin);			
				}
			},
      onError : function(obj) { alert("Error: " + obj.status); }
		});	
	}
	
	// Aktualzuje checkbox
	
	function actualizeCheckbox(_this) {
	  var value;
		if(_this.src.match('check_0')) {
		  value=1;
			_this.src=_this.src.replace('check_0','check_1');
		} else {
		  value=0;
			_this.src=_this.src.replace('check_1','check_0');		
		}
		actualize(_this.parentNode.parentNode.getAttribute('departmentId'), _this.name, value, 'k_departments'); 
	}
	
	// Przekazuje asynchronicznie zebrane dane do skryptu php
	
	function actualize(id, property, value, table) {
		advAJAX.get({
      url: "http://"+location.host+"/panel/m/94/ajax/setValue.php?randomMurzynId="+Math.random(),    
			uniqueParameter: "custom_unique_param",
      parameters : {
        "id" : id,
        "property" : property,
			  "value" : value,
				"table" : table
      },
      onSuccess : function() { },
      onError : function(obj) { alert("Error: " + obj.status); }
		});
	}
	
 // Dodaje zdjęcie
 
 function addPhoto(_this) {
   //_this.form.photo.value
	 _this.form.submit(); 
 }
 
 // Dodaje zdjęcie
 
 function removePhoto(_this) {
   _this.form.action.value='removePhoto';
	 _this.form.submit(); 
 } 
 
 
 /*
 
       SKRYPTY DO STRONY
			 
			 				 					    */
														
	  function showPersonForDepartment(id, _this, page, lang) {
		
		  var pageId=page ? page : false;
			var language=lang ? lang : false;
		  var contactDivNode=document.getElementById("ContactMenu");
			var contactDiv=contactDivNode.childNodes;
			for(var i=0;i<contactDiv.length;i++)
				contactDiv[i].style.color='rgb(103,101,103)';
								
				
			_this.style.color='rgb(181,17,25)';
		  
			var personsDivNode=document.getElementById("ContactPersons");
			
			personsDivNode.innerHTML='Pobierz osoby przypisane do dzialu o id: '+id;						
			
			getPersonsForDepartmentForPage(id, 'ContactPersons', page, language);
			
		}
		
	// Pobiera osoby, ktore sa przypozadkowane do danej kategorii, funkcjonalosc dla srtony
	
	function getPersonsForDepartmentForPage(id, containerId, pageid, language) {
	  var sJoin=document.getElementById("SJoin");	
		var container=document.getElementById(containerId);
		container.innerHTML='<br style="clear: both;"/>';
		advAJAX.get({
      url: "http://"+location.host+"/panel/m/94/ajax/getPersonsForDepartment.php",
      parameters : {
        "id" : id,
				"pageid" : pageid,
				"language" : language
      },
			onLoading : function(obj) { container.innerHTML='<div class="Loading"><p>&#x141;adowanie...</p></div>'; },
			onLoaded : function(obj) { container.innerHTML='<br style="clear: both;"/>'; },			
      onSuccess : function(obj) {
			  container.innerHTML='<br style="clear: both;"/>'; 
			  var persons=obj.responseXML.getElementsByTagName("persons").item(0);
				var personsList=persons.childNodes
				for(var i=0;i<personsList.length;i++) {
				  var nameNode=personsList[i].getElementsByTagName("name").item(0);
					var name=nameNode.firstChild ? nameNode.firstChild.data : '';
					
				  var surnameNode=personsList[i].getElementsByTagName("surname").item(0);
					var surname=surnameNode.firstChild ? surnameNode.firstChild.data : '';
					
				  var phoneNode=personsList[i].getElementsByTagName("phone").item(0);
					var phone=phoneNode.firstChild ? phoneNode.firstChild.data : '';
					
				  var mobilephoneNode=personsList[i].getElementsByTagName("mobilephone").item(0);
					var mobilephone=mobilephoneNode.firstChild ? mobilephoneNode.firstChild.data : '';
					
				  var emailNode=personsList[i].getElementsByTagName("email").item(0);
					var email=emailNode.firstChild ? emailNode.firstChild.data : '';
					
				  var photoNode=personsList[i].getElementsByTagName("photo").item(0);
					var photo=photoNode.firstChild ? photoNode.firstChild.data : '';					
					
					var personId=personsList[i].getAttribute("id");
					var div='<div class="Person">';
					if(photo)
					  div+='<img class="Photo" src="img/'+photo+'" alt="'+name+' '+surname+'"/>';
					div+='<div class="Data"><p class="NameSurname">'+name+' '+surname+'</p><p class="Email"><a href="mailto:'+email+'">'+email+'</a></p><p class="Phone">'+phone+'</p><p class="MobilePhone">'+mobilephone+'</p></div><br/></div>';
					container.innerHTML+=div;
				}
			},
      onError : function(obj) { alert("Error: " + obj.status); }
		});
	}															
