///poprawic validateform jesli jedno pole
var hist=new Array();

function keysubmit(e,fname){
	var keynum
	var keychar
	if(window.event){ //IE
		keynum = e.keyCode
	}else if(e.which){ // Netscape/Firefox/Opera
		keynum = e.which
	}
//	keychar=String.fromCharCode(keynum)
	if(keynum==13)document.forms[fname].submit();
}

Array.prototype.in_array=function(p_val) {
	for(var i=0, l=this.length; i<l;i++) {
		if(this[i]==p_val) {
			return true;
		}
	}
	return false;
}


function disableform(formname,ex,co){
	objElems = document.getElementById(formname).elements;
	var nco=(co==true ? false : true);
	for(i=0;i<objElems.length;i++){
		if(!ex || !ex.in_array(objElems[i].name))objElems[i].disabled=co;else objElems[i].disabled=nco;
//		else if(inc && exc && inc.in_array(objElems[i].name))objElems[i].disabled = co;
	}
}

function submenu(dzial,sub,ssub,asub,id_jezyk) {
	dest='inc/inc_menu.php';
	$('#smenu').hide(); //jguery
	try {
		xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
	}catch (e) {

	}
//	xmlhttp.onreadystatechange = submenutriggered;
	xmlhttp.onreadystatechange = function(){
		if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
			document.getElementById("smenu").innerHTML = xmlhttp.responseText;
		  $('#smenu').fadeIn('slow'); //jquery
		}
	}
	
	dest=dest+'?dzial='+dzial;
	dest=dest+'&sub='+sub;
	dest=dest+'&ssub='+ssub;
	dest=dest+'&asub='+asub;
	dest=dest+'&jezyk='+id_jezyk;
	xmlhttp.open("GET", dest);
	xmlhttp.send(null);
}

function submenutriggered() {
}

function show_field(layer_id,table,input_id,valuef,value,typ,cfield,custid,corder,id_jezyk) {
	dest='inc/inc_show_field.php';
//	$('#smenu').hide(); //jguery
//alert(table+' '+layer_id+' '+table+' '+input_id+' '+valuef+' '+value+' '+typ+' '+id_jezyk);
	try {
			xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
	}catch (e) {

	}
	xmlhttp.onreadystatechange = function(){
		if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
			document.getElementById(layer_id).innerHTML = xmlhttp.responseText;
//		  $('#smenu').fadeIn('slow'); //jquery
		}
	}
	
	dest=dest+'?tab='+table;
	dest=dest+'&inputid='+input_id;
	dest=dest+'&valuef='+valuef;
	dest=dest+'&value='+value;
	dest=dest+'&typ='+typ;
	dest=dest+'&cfield='+cfield;
	dest=dest+'&custid='+custid;
	dest=dest+'&corder='+corder;
	dest=dest+'&jezyk='+id_jezyk;

	xmlhttp.open("GET", dest);
	xmlhttp.send(null);
}

// popup

var ie = document.all;
var nn6 = document.getElementById &&! document.all;

var isdrag = false;
var x, y;
var dobj;

function movemouse( e ) {
  if( isdrag ) {
//	alert("tx"+tx+"e.cleint"+e.clientX+" x"+x);
//	xx=tx+e.clientX+x;
//	dobj.style.left=100+"px";
	dobj.style.left = nn6 ? tx + e.clientX - x +"px": tx + event.clientX - x+"px";
	dobj.style.top  = nn6 ? ty + e.clientY - y +"px": ty + event.clientY - y+"px";
	return false;
  }
}

function selectmouse( e ) {
  var fobj       = nn6 ? e.target : event.srcElement;
  var topelement = nn6 ? "HTML" : "BODY";

//	if(fobj.className == "dragme")alert(fobj.className);
  while (fobj.tagName != topelement && fobj.className != "dragme") {
    fobj = nn6 ? fobj.parentNode : fobj.parentElement;
  }

  if (fobj.className=="dragme") {
    isdrag = true;
	dobj = document.getElementById("myalert");
	tx = parseInt(dobj.style.left+0);
    ty = parseInt(dobj.style.top+0);
    x = nn6 ? e.clientX : event.clientX;
    y = nn6 ? e.clientY : event.clientY;
	document.onmousemove=movemouse;
	return false;
  }
}

function styledPopupClose() {
  document.getElementById("myalert").style.display = "none";
}

document.onmousedown=selectmouse;
document.onmouseup=new Function("isdrag=false");


function myalert(co,title,button,pz,py){
//	document.getElementById('myalert').style.width='';
	document.getElementById('myalert').innerHTML = '<div class="dragme"><div class="title">'+title+'</div></div><div class="tresc">'+co+'</div><span class="rbtl"></span><span class="rbt" onclick="javascript:document.getElementById(\'myalert\').style.visibility=\'hidden\';">'+button+'</span><span class="rbtr"></span>';
//	document.getElementById('myalert').innerHTML = '<div class="dragme"><div class="title">'+title+'</div></div><div class="tresc">'+co+'</div>';
	document.getElementById('myalert').style.top=getScreenCenterY()-document.getElementById('myalert').offsetHeight/2+'px';
	document.getElementById('myalert').style.left=getScreenCenterX()-document.getElementById('myalert').offsetWidth/2+'px';
	document.getElementById('myalert').style.visibility='visible';
}

function myalertyesno(co,title,buttonno,buttonyes,fid,oid,f,dzial,sub,ssub,pz,py){
//	document.getElementById('myalert').style.width='';
	
	var newv=document.createElement("div"); //glupie IE
	newv.innerHTML='<div class="dragme"><div class="title">'+title+'</div></div><form method="POST" action="" id="lista_form_pop" name="lista_form_pop"><input type="hidden" name="form_name" value="lista_form_pop" /><input type="hidden" name="form_query" value="1" /><input type="hidden" name="d_id_oferta" value="'+oid+'" /><input type="hidden" name="fid" value="'+fid+'" /><input type="hidden" name="f" value="'+f+'" /><div class="tresc">'+co+'</div><span class="fright"><span class="rbtl"></span><span class="rbt" onclick="javascript:document.lista_form_pop.submit();">'+buttonyes+'</span><span class="rbtr"><span class="rbtl"></span></span><span class="fright"><span class="rbt" onclick="javascript:document.getElementById(\'myalert\').style.visibility=\'hidden\';">'+buttonno+'</span><span class="rbtr"></span></span></div></form>'; //glupie IE
	document.getElementById('myalert').appendChild(newv); //glupie IE

	document.getElementById('myalert').style.top=getScreenCenterY()-document.getElementById('myalert').offsetHeight/2+'px';
	document.getElementById('myalert').style.left=getScreenCenterX()-document.getElementById('myalert').offsetWidth/2+'px';
	document.getElementById('myalert').style.visibility='visible';
}



function r_check(id){
	if(document.getElementById(id).checked)return(1);else return(0);
}
function r_change(id){
	document.getElementById(id).checked=true;
}

function b_change(id,bkg){
	if(bkg)bkg='#F06A4C';else bkg='#FFFFFF';
	obj=document.getElementById(co);
	obj.style.backgroundColor=bkg;
}


function sprawdz_adres(){
	if(document.getElementById("d_id_kraj").value && document.getElementById("d_miasto").value && document.getElementById("d_ulica").value && document.getElementById("d_nr_domu").value)codeAddress()
}

function clear_latlng(){
	document.getElementById('d_wspolrzedne_lat').value='';
	document.getElementById('d_wspolrzedne_lng').value='';
}

var ktoremenu=0;

function showLayer(whichLayer,pokaz){
	var elem, vis;
	if(document.getElementById){
		elem = document.getElementById(whichLayer);
	}
	else if(document.all){
		elem = document.all[whichLayer];
	}
	else if(document.layers){
		elem = document.layers[whichLayer];
	}
	vis = elem.style;
	if(pokaz==1)vis.display='block';else vis.display='none';
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function moveLayer(whichLayer,x,y){
	var elem, vis;
	if(document.getElementById){
		elem = document.getElementById(whichLayer);
	}
	else if(document.all){
		elem = document.all[whichLayer];
	}
	else if(document.layers){
		elem = document.layers[whichLayer];
	}
	vis = elem.style;
	if(x)vis.left=x+'px';
	if(y)vis.top=y+'px';
}

function getXY(whichLayer,which){
	var elem, vis;
	if(document.getElementById){
		elem = document.getElementById(whichLayer);
	}
	else if(document.all){
		elem = document.all[whichLayer];
	}
	else if(document.layers){
		elem = document.layers[whichLayer];
	}
	vis = elem.style;
	if(which=='top')return (vis.top);
	if(which=='left')return (vis.left);
}


function confirmLink(url) {
    var leave = window.confirm("Usunąć (operacja nieodwracalna)?");
	if (leave) window.location.assign(url);
	else return;
}

function confirmLinkmsg(url,msg) {
    var leave = window.confirm(msg);
	if (leave) window.location.assign(url);
	else return;
}

//function in_array_like($co,$array){
//	foreach($array as $ref){
//		if(strstr($co,$ref))return true;
//	}
//	return false;
//} 

function validateform(formularz,tryb){
	var ok=true;
	var pola='';
	if(formularz=='firma_dodaj'){
		var polat=new Array('d_nazwa','d_imienazwisko','d_tel','d_fax','d_telkom','d_mail','d_ulica','d_nr_domu','d_kod','d_miasto','d_id_wojewodztwo','d_id_kraj','d_wspolrzedne_lat','d_wspolrzedne_lng','d_nazwa_adres','d_zgoda','d_nip');
		var polan=new Array(1,1,11,11,11,13,1,1,1,1,3,3,1,1,1,2,10);
		var polar=new Array(1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1);
		var polao=new Array('<li>Nazwa firmy</li>','<li>Imię i nazwisko</li>','<li>Telefon</li>','<li>Fax</li>','<li>Telefon komórkowy</li>','<li>Adres mail</li>','<li>Ulica</li>','<li>Nr domu</li>','<li>Kod</li>','<li>Miasto</li>','<li>Województwo</li>','<li>Kraj</li>','<li>Wprowadź poprawny adres, lub wskaż punkt na mapie</li>','','<li>Nazwa lokalizacji</li>','<li>Zgoda na przetwarzanie danych osobowych</li>','<li>Prawidłowy numer NIP</li>');
	}
	if(formularz=='konto_zapomnialem'){
		var polat=new Array('d_nazwa','d_nazwa');
		var polan=new Array(1,1);
		var polar=new Array(1,1);
		var polao=new Array('<li>Nazwa konta lub adres e-mail</li>','');
	}
	if(formularz=='pomoc_wiadomosc'){
		var polat=new Array('d_mail','d_mail');
		var polan=new Array(13,13);
		var polar=new Array(1,1);
		var polao=new Array('<li>Adres e-mail</li>','');
	}
	if(formularz=='firma_haslo'){
		var polat=new Array('d_login','d_imienazwisko','d_pass','d_ppass');
		var polan=new Array(14,1,1,1);
		var polar=new Array(1,1,1,1);
		var polao=new Array('<li>Nazwa użytkownika (dozwolone znaki "1-9","a-z",".","-","_")</li>','<li>Imię i nazwisko</li>','<li>Hasło</li>','<li>Potwierdzenie hasła</li>');
	}
	if(formularz=='lokalizacja'){
		var polat=new Array('d_ulica','d_nr_domu','d_miasto','d_id_kraj');
		var polan=new Array(1,1,1,3);
		var polar=new Array(1,1,1,1);
		var polao=new Array('<li>Ulica</li>','<li>Nr domu</li>','<li>Miasto</li>','<li>Kraj</li>');
	}
	if(formularz=='lokalizacja_submit'){
		var polat=new Array('d_ulica','d_nr_domu','d_miasto','d_id_kraj','d_nazwa_adres','d_wspolrzedne_lat','d_wspolrzedne_lng');
		var polan=new Array(1,1,1,3,1,1,1);
		var polar=new Array(1,1,1,1,1,1,1);
		var polao=new Array('<li>Ulica</li>','<li>Nr domu</li>','<li>Miasto</li>','<li>Kraj</li>','<li>Nazwa lokalizacji</li>','<li>Wprowadź poprawny adres, lub wskaż punkt na mapie</li>','');
	}

	if(formularz=='lokalizacja_cord'){
		var polat=new Array('d_wspolrzedne_lat','d_wspolrzedne_lng');
		var polan=new Array(1,1);
		var polar=new Array(1,1);
		var polao=new Array('<li>Współrzędne lat</li>','<li>Współrzędne lng</li>');
	}
	if(formularz=='kontrakt_dodaj'){
		var polat=new Array('d_nazwa_kontrakt','d_id_adres','d_typ');
		var polan=new Array(1,3,3);
		var polar=new Array(1,1,1);
		var polao=new Array('<li>Nazwa kontraktu</li>','<li>Lokalizacja</li>','<li>Typ</li>');
	}
	if(formularz=='oferta_dodaj'){
		var polat=new Array('d_nazwa_oferta','d_data','d_typ');
		var polan=new Array(1,1,3);
		var polar=new Array(1,1,1);
		var polao=new Array('<li>Nazwa oferty</li>','<li>Data (rrrr-mm-dd)</li>','<li>Typ</li>');
	}
	if(formularz=='roslina_dodaj'){
		var polat=new Array('d_id_oferta','s_kod');
		var polan=new Array(3,1);
		var polar=new Array(1,1);
		var polao=new Array('<li>Nie wybrano oferty.</li>','<li>Niepoprawny kod rośliny, proszę prawidłowo wypełnić wszystkie pola formularza, lub wpisać poprawny kod ręcznie.</li>');
	}
	if(formularz=='rosliny_import'){
		var polat=new Array('d_id_oferta','d_plik');
		var polan=new Array(3,1);
		var polar=new Array(1,1);
		var polao=new Array('<li>Nie wybrano oferty.</li>','<li>Nie wybrano pliku do importu.</li>');
	}

	if(formularz=='zapytanie_dodaj'){
		var polat=new Array('d_id_kontrakt','d_typ');
		var polan=new Array(3,1);
		var polar=new Array(1,1);
		var polao=new Array('<li>Wybierz kontrakt</li>','<li>Typ zapytania</li>');
	}
	if(formularz=='form_szukaj'){
		var polat=new Array('s_nazwa');
		var polan=new Array(1);
		var polar=new Array(1);
		var polao=new Array('<li>Wpisz człon nazwy polskiej lub łacińskiej</li>');
	}
	if(formularz=='baner_form'){
		var polat=new Array('d_tytul','d_link','d_typ','d_aktywny');
		var polan=new Array(1,1,3,3);
		var polar=new Array(1,1,1,1);
		var polao=new Array('<li>Wpisz tytuł</li>','<li>Wpisz link</li>','<li>Wybierz typ banera</li>','<li>Wybierz czy aktywny</li>');
	}
	if(formularz=='baner_form_propozycje'){
		var polat=new Array('s_nazwa','d_ilosc','d_aktywny');
		var polan=new Array(1,1,3);
		var polar=new Array(1,1,1);
		var polao=new Array('<li>Wpisz nazwę łacińską</li>','<li>Wpisz ilość</li>','<li>Wybierz czy aktywny</li>');
	}

	if(formularz=='user_form'){
		var polat=new Array('d_login','d_imienazwisko','d_typ');
		var polan=new Array(1,1,3);
		var polar=new Array(1,1,1);
		var polao=new Array('<li>Wpisz login</li>','<li>Wpisz imie i nazwisko</li>','<li>Wybierz typ konta</li>');
	}
	if(formularz=='firma_form'){
		var polat=new Array('d_nazwa','d_mail','d_nip');
		var polan=new Array(1,13,1);
		var polar=new Array(1,1,1);
		var polao=new Array('<li>Nazwa firmy</li>','<li>Adres mail</li>','<li>Numer NIP</li>');
	}


	if(polat.length!=polan.length || polat.length!=polao.length || polat.length!=polar.length){alert('za malo w tablicy');ok=false;}
	for(i=0;i<polat.length;i++)if(sprawdzpole(polat[i],polar[i],polan[i],tryb)==1){ok=false;pola=pola+polao[i];}

	if(formularz=='firma_haslo' && document.getElementById(polat[2]).value!=document.getElementById(polat[3]).value){ok=false;pola=pola+'<li>Oba hasła muszą być takie same</li>';}
	if(formularz=='firma_dodaj' && !document.getElementById('d_tel').value && !document.getElementById('d_telkom').value){ok=false;pola=pola+'<li>Podaj telefon lub telefon komórkowy</li>';}
	if(formularz=='roslina_dodaj' && document.getElementById(polat[0]).value.search(/\?/i)!=-1){ok=false;pola=pola+'<li>Prosze wypełnić prawidłowo wszystkie parametry opisujące roślinę.</li>';}
	if(formularz=='rosliny_import' && (!document.getElementById('d_typ_dodania1').checked && !document.getElementById('d_typ_dodania2').checked && !document.getElementById('d_typ_dodania3').checked)){ok=false;pola=pola+'<li>Nie wybrano typu importu</li>';}
	if(ok==false && tryb!='silent')myalert('<p>Proszę wypełnić następujące pola:</p><ul>'+pola+'</ul>','BŁĘDY W FORMULARZU','OK',0,0);
	return(ok);
}

function validate_email(field,alerttxt)
{
with (field)
{
apos=value.indexOf("@");
dotpos=value.lastIndexOf(".");
if (apos<1||dotpos-apos<2) 
  {alert(alerttxt);return false;}
else {return true;}
}
}

function validatedate(co){
	data=new Date();
	var ok=1;
	if(co.length==10){
		if(co.substr(0,4)>1 && co.substr(0,4)<=data.getFullYear()){if(ok==1){ok=1;}}else ok=0;
		if(co.substr(5,2)>0 && co.substr(5,2)<=12){if(ok==1){ok=1;}}else ok=0;
		if(co.substr(8,2)>0 && co.substr(8,2)<=31){if(ok==1){ok=1;}}else ok=0;
		if(co[4]=='-' && co[7]=='-'){if(ok==1){ok=1;}}else ok=0;
	}else ok=0;
	return(ok);
}

function validate_nip(co){
	kod=co.replace(/[^A-Z]+/g,"")
	kod=kod.substr(0,2)
	co=co.replace(/[^0-9]+/g,"")
//	alert(kod+co)
	if(!kod || kod=='PL'){
		if(co.length!=10)return(0);
		arrSteps=new Array(6,5,7,2,3,4,5,6,7);
		intSum=0;
		for(k=0;k<9;k++)intSum+=arrSteps[k]*co[k];
		int=intSum % 11;
		intControlNr=(int==10)?0:int;
		if(intControlNr==co[9])return(kod+co);
		return(0);
	}else return(kod+co);
}

function validate_mail(co){
	co=co.match(/^[^@]+@[a-zA-Z0-9\._\-]+\.[a-zA-Z]+$/g,"")
	return(co)
}
function validate_tel(co){
	stacjonarne=new Array(11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,31,32,33,34,35,36,37,38,39,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,61,62,63,64,65,67,68,70,71,72,73,74,75,76,77,80,81,82,83,84,85,86,87,89,90,91,92,93,94,95,96,97,98,99)

	co=co.replace(/[^0-9]+/g,"")
	if(!co.match(/^00\d{2}/)){
		if(co.match(/^[1-9]/))co='0048'+co;
		else co=co.replace(/^0/,'0048')
	}
	if(co.length!=13)return(0);

	if(stacjonarne.in_array(co.substr(4,2)))return(co.replace(/^([0-9]{4})([0-9]{2})([0-9]{3})([0-9]{2})([0-9]{2})/,"($1) $2 $3 $4 $5"));
	else return(co.replace(/^([0-9]{4})([0-9]{3})([0-9]{3})([0-9]{3})/,"($1) $2 $3 $4"))
}


function sprawdzpole(co,req,typ,tryb){
	var blad=0;
	obj=document.getElementById(co);
	if(typ==1){ //input
		if(obj.value=='' && req){
			if(tryb!='silent')obj.style.backgroundColor='#F06A4C';
			blad=1;
		}else if(tryb!='silent')obj.style.backgroundColor='#FFFFFF';
	}
	if(typ==14){ //input login - ograniczenie do a-z . _ -
		if(!obj.value.match(/^[a-zA-Z0-9\._\-]+$/g,"") && req){
			if(tryb!='silent')obj.style.backgroundColor='#F06A4C';
			blad=1;
		}else if(tryb!='silent')obj.style.backgroundColor='#FFFFFF';
	}

	if(typ==2){ //checkbox
		if(obj.checked==0 && req){
			if(tryb!='silent')obj.style.backgroundColor='#F06A4C';
			blad=1;
		}else if(tryb!='silent')obj.style.backgroundColor='#FFFFFF';
	}
	if(typ==3 && req){ //select
		if(!obj.options[obj.selectedIndex].value){
			if(tryb!='silent')obj.style.backgroundColor='#F06A4C';
			blad=1;
		}else if(tryb!='silent')obj.style.backgroundColor='#FFFFFF';
	}
	if(typ==10){ //NIP
		pom=validate_nip(obj.value)
//		pom='aaa'
		if((!pom && obj.value) || (!pom && req)){
			if(tryb!='silent')obj.style.backgroundColor='#F06A4C';
			blad=1;
		}else{
			if(tryb!='silent'){
				obj.style.backgroundColor='#FFFFFF';
				if(pom)obj.value=pom
			}
		}
	}
	if(typ==11){ //telefon
		pom=validate_tel(obj.value)
		if((!pom && obj.value) || (!pom && req)){
			if(tryb!='silent')obj.style.backgroundColor='#F06A4C';
			blad=1;
		}else{
			if(tryb!='silent'){
				obj.style.backgroundColor='#FFFFFF';
				if(pom)obj.value=pom
			}
		}
	}
	if(typ==13){ //mail
		pom=validate_mail(obj.value)
		if((!pom && obj.value) || (!pom && req)){
			if(tryb!='silent')obj.style.backgroundColor='#F06A4C';
			blad=1;
		}else{
			if(tryb!='silent')obj.style.backgroundColor='#FFFFFF';
		}
	}
	if(!req){
		if(obj.className=='data' || obj.className=='datar'){
			if(validatedate(obj.value)==0 && obj.value!=''){
				if(tryb!='silent')obj.style.backgroundColor='#F06A4C';
				blad=1;
			}else if(tryb!='silent')obj.style.backgroundColor='#FFFFFF';
		}
	}
	return(blad);
}

function zamien(co,na){
	document.images[co].src=(document.images[co].src).substr(0,(document.images[co].src).length-5)+na+(document.images[co].src).substr((document.images[co].src).length-4,(document.images[co].src).length);
}

function zamienauto(co){
	var na=0;
//	alert(document.getElementById(co).src.substr(document.getElementById(co).src.length-5,1));
	if(document.getElementById(co).src.substr(document.getElementById(co).src.length-5,1)=='0')na=1;else na=0;
	document.getElementById(co).src=(document.getElementById(co).src).substr(0,(document.getElementById(co).src).length-5)+na+(document.getElementById(co).src).substr((document.getElementById(co).src).length-4,(document.getElementById(co).src).length);
}


function toggleLayer(whichLayer,title){
	var elem, vis;
	if(document.getElementById){
		title=document.getElementById('t-'+whichLayer);
		elem = document.getElementById(whichLayer);
	}
	else if(document.all){
		title=document.all['t-'+whichLayer];
		elem = document.all[whichLayer];
	}
	else if(document.layers){
		title=document.layers['t-'+whichLayer];
		elem = document.layers[whichLayer];
	}
	vis = elem.style;
	if(vis.display=='' && elem.offsetWidth!=undefined && elem.offsetHeight!=undefined)
	vis.display=(elem.offsetWidth!=0 && elem.offsetHeight!=0)?'block':'none';
	vis.display=(vis.display=='' || vis.display=='block')?'none':'block';
	//if(vis.display=='none')title.className='szarystrzalka';else title.className='strzalka';
}

function findPosition(oElement){
	if(typeof(oElement.offsetParent)!='undefined'){
		for(var posX=0,posY=0;oElement;oElement=oElement.offsetParent) {
			posX+=oElement.offsetLeft;
			posY+=oElement.offsetTop;
		}
		return [posX,posY];
	}else{
		return [ oElement.x, oElement.y ];
	}
}


function myalertmy(query,name,pole) {
	dest='inc/search_popup.php';
	try {
		xmlhttpdr = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
	}catch (e) {
	}


	xmlhttpdr.onreadystatechange = myalertmy_dr;

	if(pole=='dodaj_rosline'){
		dest+='?d_id_rosliny='+name[0];
		dest+='&d_id_firmy='+name[1];
		dest+='&table='+name[2];
		xmlhttpdr.open("GET", dest);
		xmlhttpdr.send(null);
	}
	if(pole=='roslina_info'){
		dest+='?d_id_rosliny='+name;
		dest+='&table=tru';
		xmlhttpdr.open("GET", dest);
		xmlhttpdr.send(null);
	}

}

function myalertmy_dr(){
	if ((xmlhttpdr.readyState == 4) && (xmlhttpdr.status == 200)) {
		oldDiv = document.getElementById('myalert');
		newDiv = document.createElement(oldDiv.tagName);
		newDiv.id = oldDiv.id;
		newDiv.className = oldDiv.className;
		newDiv.innerHTML = xmlhttpdr.responseText;;
		oldDiv.parentNode.replaceChild(newDiv, oldDiv); 

		document.getElementById('myalert').style.top=getScreenCenterY()-document.getElementById('myalert').offsetHeight/2+'px';
		document.getElementById('myalert').style.left=getScreenCenterX()-document.getElementById('myalert').offsetWidth/2+'px';
//		alert(xmlhttpdr.responseText);
//		document.getElementById('myalert').innerHTML=xmlhttpdr.responseText;
//		document.getElementById('myalert').appendChild(document.createTextNode(xmlhttpdr.responseText));


		document.getElementById('myalert').style.visibility='visible';
	}
}

function sleep(ms){
	var dt = new Date();
	dt.setTime(dt.getTime() + ms);
	while (new Date().getTime() < dt.getTime());
}


function addslashes(str) {
	if(str){
		str=str.replace(/\\/g,'\\\\');
		str=str.replace(/\'/g,'\\\'');
		str=str.replace(/\"/g,'\\"');
		str=str.replace(/\0/g,'\\0');
	}
return str;
}
function stripslashes(str) {
	if(str){
		str=str.replace(/\\'/g,'\'');
		str=str.replace(/\\"/g,'"');
		str=str.replace(/\\0/g,'\0');
		str=str.replace(/\\\\/g,'\\');
	}
	return str;
}

function loadurl(query,name,pole) {
	if(pole=='podpowiedz')dest='inc/search_zapytanie.php';
	if(pole=='zakoduj')dest='inc/search_kod.php';
	if(pole=='dekoduj')dest='inc/search_dekod.php';
	if(pole=='lista' || pole=='back')dest='inc/search_lista.php';
	if((pole=='back' && hist[hist.length-2]) || pole=='lista')$('#lista').fadeTo('fast',0.25)
	try {
		if(pole=='podpowiedz')xmlhttppo = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
		if(pole=='zakoduj')xmlhttpza = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
		if(pole=='dekoduj')xmlhttpde = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
		if(pole=='lista' || pole=='back')xmlhttplr = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
//		xmlhttplu = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
	}catch (e) {

	}

/*	xmlhttp.onreadystatechange = function() {
		if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
			document.getElementById(pole).innerHTML = xmlhttp.responseText;
			if(pole=='podpowiedz'){
				if(document.getElementById('hintt1'))document.getElementById(pole).style.visibility='visible';
				else document.getElementById(pole).style.visibility='hidden';
				document.getElementById(pole).style.visibility='visible';
			}
		}
	}*/
	if(pole=='podpowiedz')xmlhttppo.onreadystatechange = loadurl_podpowiedz;
	if(pole=='zakoduj')xmlhttpza.onreadystatechange = loadurl_zakoduj;
	if(pole=='dekoduj')xmlhttpde.onreadystatechange = loadurl_dekoduj;
	if(pole=='lista' || pole=='back')xmlhttplr.onreadystatechange = loadurl_lista;
//	if(pole=='podpowiedz')xmlhttplu.onreadystatechange = loadurl_podpowiedz;
	if(pole=='podpowiedz'){
		dest+='?query='+query;
		dest+='&field='+name;
	}
	if(pole=='zakoduj'){
		pola=new Array('s_nazwa','s_poj','s_poj_wiel','s_poj_wiel_c','s_wiel','s_wmin','s_wmax','s_for','s_for_wiel','s_for_wiel_c','s_ftyp','s_fmin','s_fmax');
		for(i=0;i<pola.length;i++){
			if(i==0)pom='?';
			else pom='&';
//			if(document.getElementById(pola[i]))dest+=pom+pola[i]+'='+document.getElementById(pola[i]).value;
			if(document.getElementById(pola[i]))dest+=pom+pola[i]+'='+encodeURI(document.getElementById(pola[i]).value);
		}
	}
	if(pole=='dekoduj'){
		dest+='?kod_rosliny='+query;
	}

	if(pole=='lista'){
		dest+='?d_id_cecha='+name[0];
		dest+='&s='+name[1];
		dest+='&sort='+name[2];
		dest+='&table='+name[3];
		dest+='&filtr='+encodeURI(addslashes(name[4]));
	}

	if(pole=='podpowiedz'){
		xmlhttppo.open("GET", dest);
		xmlhttppo.send(null);
	}
	if(pole=='zakoduj'){
		xmlhttpza.open("GET", dest);
		xmlhttpza.send(null);
	}

	if(pole=='dekoduj'){
		xmlhttpde.open("GET", dest);
		xmlhttpde.send(null);
	}

	if(pole=='lista'){
		i=hist.length;
		hist[i]=dest;
		xmlhttplr.open("GET", dest);
		xmlhttplr.send(null);
	}
	if(pole=='back'){
		dest=hist[hist.length-2];
		ok=0;
//		len=dest.length;
		for(i=0;i<dest.length;i++)if(dest.charAt(i)=='\\')ok=1;
		if(ok==0)dest=addslashes(dest);
		if(dest){
			hist.pop();
			xmlhttplr.open("GET", dest);
			xmlhttplr.send(null);
		}
	}
//	alert(hist[i]+'\n'+hist[i-1]);

}

function loadurl_podpowiedz(){
	if ((xmlhttppo.readyState == 4) && (xmlhttppo.status == 200)) {
		document.getElementById('podpowiedz').innerHTML = xmlhttppo.responseText;
		if(document.getElementById('hintt1'))document.getElementById('podpowiedz').style.visibility='visible';
		else document.getElementById('podpowiedz').style.visibility='hidden';
		document.getElementById('podpowiedz').style.visibility='visible';
	}
}
function loadurl_zakoduj(){
	if ((xmlhttpza.readyState == 4) && (xmlhttpza.status == 200)) {
		document.getElementById('zakoduj').innerHTML = xmlhttpza.responseText;
		loadurl(document.getElementById('s_kod').value,'','dekoduj');
	}
}

function loadurl_dekoduj(){
	if ((xmlhttpde.readyState == 4) && (xmlhttpde.status == 200)) {
		document.getElementById('dekoduj').innerHTML = xmlhttpde.responseText;
	}
}

function loadurl_lista(){
	if ((xmlhttplr.readyState == 4) && (xmlhttplr.status == 200)) {
		document.getElementById('lista').innerHTML = xmlhttplr.responseText;
		if(hist.length>1 && document.getElementById('back'))document.getElementById('back').className='bt';
		$('#lista').fadeTo('fast',1)
	}
}


function klawisz(pom,zaw,name){
	var kod=pom.keyCode? pom.keyCode : pom.charCode
	if(kod==38 || kod==40){
		if(kod==40){
			if(ktoremenu>0)document.getElementById("hintt"+ktoremenu).className="podp";
			pom1=ktoremenu+1;
			if(document.getElementById("hintt"+pom1))ktoremenu++;	
		}
		if(kod==38 && ktoremenu>0){
			if(ktoremenu>0)document.getElementById("hintt"+ktoremenu).className="podp";	
			ktoremenu--;
		}
		if(ktoremenu>0)document.getElementById("hintt"+ktoremenu).className="podpover";
	}
	if(kod==13){
		if(ktoremenu>0)document.getElementById(name).value=document.getElementById("hintt"+ktoremenu).innerHTML;
		document.getElementById('podpowiedz').style.visibility='hidden';
	}
	
	if(name!=''){
//		alert(findPosition(document.getElementById(name))[0]);
		document.getElementById('podpowiedz').style.left=(findPosition(document.getElementById(name))[0])+'px';
		document.getElementById('podpowiedz').style.top=(findPosition(document.getElementById(name))[1]+22)+'px';
	}
	if(kod!=13 && kod!=38 && kod !=40)loadurl(zaw,name,'podpowiedz');
}

function klik(pom,field){
	//if(field=="nazwa_polska")
	document.getElementById(field).value=pom;
	if(document.getElementById('zakoduj'))loadurl('','','zakoduj');
	document.getElementById('podpowiedz').style.visibility='hidden';
}

function schowaj(){
	document.getElementById('podpowiedz').style.visibility='hidden';
}

function addElement() {
  var ni = document.getElementById('formularz');
  var numi = document.getElementById('nazwa_polska');
  var num = (document.getElementById('nazwa_polska').value -1)+ 2;
  numi.value = num;
  var newdiv = document.createElement('input');
  var divIdName = 'synonimpl'+num;
  newdiv.setAttribute('name',divIdName);
  newdiv.innerHTML = 'Element Number '+num+' has been added! <a href=\'#\' onclick=\'removeElement('+divIdName+')\'>Remove the div "'+divIdName+'"</a>';
  ni.appendChild(newdiv);
}






function getScreenCenterY() {
var y = 0;

y = getScrollOffset()+(getInnerHeight()/2);

return(y);
}

function getScreenCenterX() {
return(document.body.clientWidth/2);
}

function getInnerHeight() {
var y;
if (self.innerHeight) // all except Explorer
{
y = self.innerHeight;
}
else if (document.documentElement && document.documentElement.clientHeight)
// Explorer 6 Strict Mode
{
y = document.documentElement.clientHeight;
}
else if (document.body) // other Explorers
{
y = document.body.clientHeight;
}
return(y);
}

function getScrollOffset() {
var y;
if (self.pageYOffset) // all except Explorer
{
y = self.pageYOffset;
}
else if (document.documentElement && document.documentElement.scrollTop)
// Explorer 6 Strict
{
y = document.documentElement.scrollTop;
}
else if (document.body) // all other Explorers
{
y = document.body.scrollTop;
}
return(y);
}


function makePOSTRequest(url, parameters){
	http_request = false;
	if(window.XMLHttpRequest){ // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if(http_request.overrideMimeType){
			http_request.overrideMimeType('text/html');
		}
	}else if(window.ActiveXObject){ // IE
		try{
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e){
			try{
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){}
		}
	}
	if (!http_request){
		alert('Cannot create XMLHTTP instance');
		return false;
	}
	http_request.onreadystatechange = alertContents;
	http_request.open('POST', url, true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
	http_request.setRequestHeader("Content-length", parameters.length);
	http_request.setRequestHeader("Connection", "close");
	http_request.send(parameters);
}

function alertContents(){
	if (http_request.readyState == 4){
		if (http_request.status == 200){
			//alert(http_request.responseText);
			result = http_request.responseText;
			document.getElementById("myalert").style.visibility="hidden";
			document.getElementById('myalert').innerHTML = result;            
//			if(document.getElementById("form_id") && document.getElementById("form_lang")){
//				div_rewrite('form_content',document.getElementById("form_id").value,0,0,document.getElementById("form_lang").value,'form_content');
//				alert('rewrite');
//			}else{
//				div_rewrite('form_content',0,0,0,1,'form_content');
//				alert('norewrite');
//			}
		
		}else{
			alert('There was a problem with the request.');
		}
	}
}




function get(obj){
	var poststr = "popup_engine_funkcja=" + encodeURI(document.getElementById("popup_engine_funkcja").value)+
	"&popup_engine_id=" + encodeURI(document.getElementById("popup_engine_id").value)+
	"&popup_engine_iid=" + encodeURI(document.getElementById("popup_engine_iid").value)+
	"&popup_engine_iiid=" + encodeURI(document.getElementById("popup_engine_iiid").value)
	if(document.getElementById("popup_engine_ilosc"))poststr+="&popup_engine_ilosc="+encodeURI(document.getElementById("popup_engine_ilosc").value.replace(/&/g,"%26"));
//	if(document.getElementById("fck_engine_tresc"))poststr+="&fck_engine_tresc="+encodeURI(document.getElementById("fck_engine_tresc").value.replace(/&/g,"%26"));
//	if(document.getElementById("form_engine_typ_pola"))poststr+="&form_engine_typ_pola="+encodeURI(document.getElementById("form_engine_typ_pola").value.replace(/&/g,"%26"));
//	if(document.getElementById("form_engine_wymagane"))poststr+="&form_engine_wymagane="+encodeURI(document.getElementById("form_engine_wymagane").value.replace(/&/g,"%26"));
//	if(document.getElementById("form_engine_mail"))poststr+="&form_engine_mail="+encodeURI(document.getElementById("form_engine_mail").value.replace(/&/g,"%26"));
	makePOSTRequest('inc/search_popup_submit.php', poststr);
}

