

//ToolTip
function showTip(oEvent, txt)
{
	var tip = document.getElementById(TipId);
	//tip.innerHTML = txt;
	tip.style.visibility = "visible";
	tip.style.left = oEvent.clientX - 5 + "px";
	tip.style.top = oEvent.clientY - 5 + "px";
}
function hideTip(oEvent)
{
	var tip = document.getElementById(TipId);
	tip.style.visibility = "hidden";
}

//########################## DO WYKRYWANIA PRZEGLĄDAREK  ####################################
//niekompletne działa dobrze tylko dla IE, nalezy dopisac reszte w wolnej chwili
//Wykrywanie przeglądarki:
function whatBrowserIsThis()
{
	var sUserAgent = navigator.userAgent;
	var fAppVersion = parseFloat(navigator.appVersion);
	var bIsOpera = sUserAgent.indexOf("Opera") > -1;
	var bIsIE = sUserAgent.indexOf("compatible") > -1 && sUserAgent.indexOf("MSIE") > -1 && !bIsOpera;
	var actualBrowser = new browser();
	if(bIsIE)
	{	
		var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
		reIE.test(sUserAgent);
		var fIEVersion = parseFloat(RegExp["$1"]);
		
		actualBrowser.bVersion = fIEVersion;
		actualBrowser.bType = "IE";
	}else
	{
	
		actualBrowser.bType = "OTHER";
	}
	
	return actualBrowser;
	
}

//obiekt typu przeglądarki. przechowuje zmienne wersja i typ
function browser()
{
	this.bVersion = -1;
	this.bType = "undefined";
}

function getHeight() {
  var myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}
function getWidth() {
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = document.body.clientWidth;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }
  return myWidth;
}

function getScrollX() {
  var scrOfX = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfX = document.documentElement.scrollLeft;
  }
  return scrOfX;
}
function getScrollY() {
  var scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
  }
  return scrOfY;
}	



function pozycjonujMenu()
{
	var menu = document.getElementById('menu');	
	var top = getScrollY();	
	if((getFullHeight() - getScrollY()) > 250)
		menu.style.top = top + 30 + "px";	
}
	
function showMenu()
{
	var menu = document.getElementById('menu');		
	menu.style.left = 0 + "px";
	menu.style.width = '300px';
	var menuButton = document.getElementById('menuButton');
	menuButton.onclick = function(){hideMenu();};	
}

function hideMenu()
{
	var menu = document.getElementById('menu');
	menu.style.left = 0 + "px";
	menu.style.width = '20px';
	var menuButton = document.getElementById('menuButton');
	menuButton.onclick = function() {showMenu();};
}
	
	
	
function getScrollBarWidth() 
{
	document.body.style.overflow = 'hidden';
	var width = document.body.clientWidth;
	document.body.style.overflow = 'scroll';
	width -= document.body.clientWidth;
	if(!width) width = document.body.offsetWidth-document.body.clientWidth;
	document.body.style.overflow = '';
	return width;
}

function getFullHeight() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}
//#############################	 DO ZARZADZANIA STYLAMI ###################################


function findCssSheet(csss, c)
{
	var i = 0;
	while(csss[i])
	{
		if(csss[i].selectorText == c)
			return csss[i];
		i++;
	}
}

	
		
//##########################  WALIDACJA DANYCH OD UZYTKOWNIKA   #################################





function isValidMail(sText)
{
	var reEmail = /^(?:\w+\.?)*\w+@(?:\w+\.)+\w+$/;
	return reEmail.test(sText);
}


//Udostępnia mozliwosc wykorzystania w css hover dla kazdej przegladarki
function enableHover()
{
	var b = new browser();
	b = whatBrowserIsThis();
	
	if(b.bType == 'IE' && b.bVersion < 7)
	{
		var oCSSRules = document.styleSheets[0].cssRule || document.styleSheets[0].rules;	
		var rule = findCssSheet(oCSSRules, 'BODY');
		rule.style.behavior = 'url("tpl/js/csshover3.htc");';
	}
}
	
	function validate()
{
	var form = document.getElementById('zgloszenie');
	var sMail = form["mail"].value;
	if(isValidMail(sMail))
	{
		form.submit();
		return true;
	}
	else
	{
		alert("Twoj mail jest NIEPRAWIDLOWY. Prosze go poprawic.");
		return false;
	}

}




//################# function to use ajax ############

//Wczytuje strone przez AJAXA
function page(p)
{  

	var random = Math.floor(Math.random()*8756874)
	dhtmlHistory.add(random, p);
	redirect(p);
}
function historyChange(newLocation, historyData)
{
	redirect(historyData);
}

function redirect(p)
{

  new Ajax.Request(p,
    {
      method:'get',
      onSuccess: function(transport){
        var response = transport.responseText;    
        var b = new browser();
        b = whatBrowserIsThis();
        if(b.bType == "IE")
        	document.getElementById("table").outerHTML = response;
        else
        	document.getElementById("table").innerHTML = response;
				        
      },
      onFailure: function(){ alert('Something went wrong...') }
    });
}



	

//################# komunikaty ############

function showBox(id)
{
	var box = document.getElementById("msgBox");
	box.style.display = "inline";
	var boxContent = document.getElementById("msgBoxContent");

	new Ajax.Request('?p=def/extraInfo/' + id,
    {
      method:'get',
      onSuccess: function(transport){
        var response = transport.responseText;        
        boxContent.innerHTML = response;        
      },
      onFailure: function(){ alert('Something went wrong...') }
    });
	
	
}

function closeMsgBox()
{
	var box = document.getElementById("msgBox");
	box.style.display = "none";
}




function openNewWindow(url)
{
window.open(url,'mywindow','width=435,height=550,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,copyhistory=no,resizable=no'); 
}













	
//sortowanie tabel
function sort(kolumna, obj)
{
	var table = obj.parentNode.parentNode.parentNode; //glowna tabela
	var rows = table.childNodes;
	var len = rows.length;
	var i = 0;
	while(i < len)
	{
		if(rows[i].nodeType == 3)
			rows.removeChild(rows[i]);
		i++;
	}
	len = rows.length;
	i = 0;
	alert("usuwanie zakonczone");
	//sortowanie bąbelkowe elementów tablicy
	//   for i=0 to r-2 do
  //      for j=r-1 downto i+1 do
  //           if (tab[j-1]>tab[j])
  //                zamień elementy tab[j] i tab[j-1]
	
	while(i < len)
	{
		alert(rows[i].childNodes[0].innerHTML);
	}
	
}
	
	
//zakładki
var list = ["menuContent_form", "menuContent_search"];


function hideAllTabs()
{
	var len = list.length;
	var i = 0;
	while(i < len)
	{
		document.getElementById(list[i]).style.display = "none";
		i++;
	}
}
function showTab(tab)
{
	hideAllTabs();
	document.getElementById(tab).style.display = "inline";
}

//############### czesc zalezna od aplikacji

function addNewRecord()
{	
	$("formAdd").request({
      method:'post',
			onComplete: function(transport){ 
			alert(transport.responseText); 
			$("formAdd").reset();
			$("formAdd").focusFirstElement();
			},
      onFailure: function(){ alert('Something went wrong...'); }
    });
}

function search()
{	
			hideMenu();
			var s = $("formSearch").value;			
			page('?p=def/search/' + s);
		
}
