// dodaj do ulubionych

function bookmark() {
	var fElem = document.getElementById("dodajDoUlubionych");
	if ( fElem ) {
		if ( fElem.nodeName.toUpperCase() == "A" ) {
			fElem.onclick = function() {
				if(window.external && typeof(window.external.AddFavorite) != 'undefined' ) {
					window.external.AddFavorite(fElem.getAttribute('href'), fElem.getAttribute('title'));
					return false;
				}
				return true;
			}
		}
	}
}


// pokaż- ukryj odpowiedź

function pokazUkryj(id)
{
	var pytanie = document.getElementById("pytanie" + id);
	var odpowiedz = document.getElementById("odpowiedz" + id);
	if(odpowiedz.className == "hide") {
		pytanie.className = "active";
		odpowiedz.className = "";
	}

	else {
		pytanie.className = "normal";
		odpowiedz.className = "hide";
	}
}


// pokaż- ukryj taryfy

function pokazUkryjTaryfe(id)
{
	var tabs = 0;
	var currentTab;
	var rexp ='';
	var i;
	var tmp;
	var countTab = 1;
	var tmpElem = null;
	var taryfa;
	var tabela;

	while ( tmpElem = document.getElementById('taryfa' + countTab) ) {
		countTab++;
		tabs++;
	}

	currentTab = id;

	for ( i=1 ; i<=tabs ; i++ ) {
		taryfa = document.getElementById("taryfa" + i);
		tabela = document.getElementById("tabela" + i);

		if ( i==currentTab ) {
			taryfa.className = "active";
			tabela.className = "";
		} else {
			taryfa.className = "normal";
			tabela.className = "hide";
		}
	}
}

function pokazUkryjTooltip(id)
{
	var currentObj;

	currentTab = document.getElementById(id);

	if ( currentTab.className == "tooltip hide" ) currentTab.className = "tooltip";
	else currentTab.className = "tooltip hide";
}

// zmiana value

function change() {
	var inp;
	var dflText;
	inp = document.getElementsByTagName('INPUT');
	for( var i=0 ; i < inp.length ; i++ ) {
		if ( inp[i].type == "text" && inp[i].className.indexOf('noChange')<0 ) {
			dflText = inp[i].value;
			if ( dflText != "" ) {
				inp[i].setAttribute('autocomplete','off');
				eval("inp[i].onfocus = function() {if( this.value == '"+dflText+"' ) this.value='';}");
				eval("inp[i].onblur = function() {if( this.value == '' ) this.value='"+dflText+"';}");
			}
		}
	}
}


//pops

function pops(){
	var ap=document.getElementsByTagName('a');
	for (var j=0;j<ap.length;j++) {
		var l=ap[j].href;
		if ( (ap[j].href.indexOf('.jpg')>-1) ||  (ap[j].href.indexOf('.JPG')>-1) || (ap[j].href.indexOf('.png')>-1) ) {
			ap[j].onclick=function() {
				popup(this.href); return false;
			}
		} else if ( ap[j].rev == "_blank" ) {
			ap[j].setAttribute('target','_blank');
		} else if ( !(ap[j].rev.indexOf('_popup') < 0) ) {
			ap[j].onclick=function() {
				var
				re,
				tmpWidth,
				tmpHeight,
				tmpArray,
				tmpParam;

				re = /width([0-9]{1,3})/;
				if ( re.test(this.rev) ) {
					tmpArray = re.exec(this.rev);
					tmpWidth = tmpArray[1];
				}
				re = /height([0-9]{1,3})/;
				if ( re.test(this.rev) ) {
					tmpArray = re.exec(this.rev);
					tmpHeight = tmpArray[1];
				}

				if ( tmpWidth && tmpHeight ) {
					tmpParam = "width=" + tmpWidth + ",height=" + tmpHeight;
				}

				window.open(this.href,'',tmpParam); return false;
			}
		} else if ( !(ap[j].rev.indexOf('_flash') < 0) ) {
			ap[j].onclick=function() {
				var
				re,
				tmpWidth,
				tmpHeight,
				tmpArray,
				tmpParam,
				flash;
				re = /width([0-9]{1,3})/;
				if ( re.test(this.rev) ) {
					tmpArray = re.exec(this.rev);
					tmpWidth = parseInt(tmpArray[1]);
				}
				re = /height([0-9]{1,3})/;
				if ( re.test(this.rev) ) {
					tmpArray = re.exec(this.rev);
					tmpHeight = parseInt(tmpArray[1]);
				}
				flash=window.open("","flash","width="+tmpWidth+",height="+(tmpHeight+21)+",location=no,menubar=no,status=no,toolbar=no,scrollbars=no,resizable=no,alwaysRaised=true,left=1,top=1,screenX=1,screenY=1");
				with(flash) {
					document.writeln('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">');
					document.writeln('<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pl">');
					document.writeln('<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />');
					document.writeln('<title>'+this.title+'</title>');
					document.writeln('<link href="css/dialog_popup.css" rel="stylesheet" type="text/css" /></head><body><div>');
					document.writeln('<!--[if IE]><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="'+tmpWidth+'" height="'+tmpHeight+'"><script>/*<![endif]-->');
					document.writeln('<object type="application/x-shockwave-flash" data="'+this.href+'" width="'+tmpWidth+'" height="'+tmpHeight+'">');
					document.writeln('<script type="text/javascript">/**/</script>');
					document.writeln('<param name="movie" value="'+this.href+'" />');
					document.writeln('<param name="loop" value="true" />');
					document.writeln('<param name="quality" value="best" />');
					document.writeln('</object>');
					document.writeln('<p style="text-align: center;"><a style="text-decoration: none; color: #6583B6;" href="javascript:self.close()"><strong>Zamknij</strong></a></p>');
					document.writeln('</div></body></html>');
					document.close();
				}
				flash.focus();
				return false;
			}
		} else if ( (ap[j].href.indexOf('.pdf')>-1) || (ap[j].href.indexOf('adv.pl')>-1) ) {
			ap[j].setAttribute('target','_blank');
		}
	}
}

//<a href="http://www.onet.pl" rev="_blank">www.onet.pl</a><br />
//<a href="http://www.wp.pl" rev="_popup-width200-height200" >www.wp.pl</a>
//<a href="http://www.wp.pl" rev="_flash-width200-height200" >www.wp.pl</a>

function popup(img) {
	foto1= new Image();
	foto1.src=img;
	conl(img);
}

function viewFoto(img) {
	var foto1,sz,wys,left,wywolaj,tp,tmp;
	foto1 = new Image();
	foto1.src = img;
	sz = foto1.width;
	wys = foto1.height+22;
	left = Math.floor((screen.width-sz)/2);
	tp = Math.floor((screen.height-wys)/2);
	wywolaj = "width="+sz+",height="+wys+",top="+tp+",left="+left;
	newwindow=window.open('','newwindow',wywolaj);
	tmp = newwindow.document;
	tmp.writeln('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">');
	tmp.writeln('<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pl">');
	tmp.writeln('<head>');
	tmp.writeln('	<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />');
	tmp.writeln('	<meta http-equiv="Content-language" content="pl" />');
	tmp.writeln('	<meta name="Author" content="adv.pl" />');
	tmp.writeln('	<title>popup</title>');
	tmp.writeln('	<link rel="stylesheet" type="text/css" media="screen" href="css/dialog_popup.css" />');
	tmp.writeln('	<link rel="shortcut icon" href="favicon.png" />');
	tmp.writeln('	<!--[if IE]><meta http-equiv="imagetoolbar" content="no" /><meta name="MSSmartTagsPreventParsing" content="true" /><![endif]-->');
	tmp.writeln('</head>');
	tmp.writeln('<body>');
	tmp.writeln('<div style="width:'+sz+'px">');
	tmp.writeln('	<a style="display: block;" href="javascript:self.close()"><img src="' + img + '" alt="" /></a>');
	tmp.writeln('	<p style="text-align: center;"><a style="text-decoration: none; color: #6583B6;" href="javascript:self.close()"><strong>Zamknij</strong></a></p>');
	tmp.writeln('</div>');
	tmp.writeln('</body>');
	tmp.writeln('</html>');
	tmp.close();
}

function conl(img) {
	if( (foto1.width!=0) && (foto1.height!=0) ) {
		viewFoto(img);
	}
	else {
		fun="conl('"+img+"')";
		inter=setTimeout(fun,20);
	}
}


// href dla SELECT

function selectOnChangeSubmit() {
	var tmpSelects = null;
	var selects = Array();
	var counter;

	tmpSelects = document.getElementsByTagName('SELECT');
	for ( counter=0 ; counter < tmpSelects.length ; counter++ ) {
		if ( tmpSelects[counter].className.indexOf('onChangeSubmit') > -1 ) {
			selects[selects.length] = tmpSelects[counter];
		}
	}

	if ( selects.length ) {
		for ( counter = 0 ; counter < selects.length ; counter++ ) {
			selects[counter].onchange = function() {
				if ( this.options[this.selectedIndex].value.toUpperCase().indexOf('HTTP://') == 0 ) {
					window.location = this.options[this.selectedIndex].value;
				} else if( this.options[this.selectedIndex].value.toUpperCase().indexOf('_HTTP://') == 0 ) {
					window.open(this.options[this.selectedIndex].value.replace(/^_/,''),'','');
				} else {
					this.form.submit();
				}
			}
		}
	}
}


// newsletter

/*Subskrypcja*/

function dodaj(){
var elem = document.getElementById('subform');
	if ( validate() ){
		with(elem) {
			location=action+'?job=dodaj_email&email='+(email.value)+'&groups_selected=1&grupa314=1&lista='+(lista.value)+'&redirect_brak_emaila='+(redirect_brak_emaila.value)+'&redirect_email_niepraw='+(redirect_email_niepraw.value)+'&redirect_email_juz_jest='+(redirect_email_juz_jest.value)+'&redirect_dodano='+(redirect_dodano.value)+'&redirect_usunieto='+(redirect_usunieto.value)+'&redirect_brak_w_bazie='+(redirect_brak_w_bazie.value);
		}
	}
}

function dodaj2(){
var elem = document.getElementById('subform2');
	if ( validate2() ){
		with(elem) {
			location=action+'?job=dodaj_email&email='+(email2.value)+'&groups_selected=1&grupa315=1&lista='+(lista.value)+'&redirect_brak_emaila='+(redirect_brak_emaila.value)+'&redirect_email_niepraw='+(redirect_email_niepraw.value)+'&redirect_email_juz_jest='+(redirect_email_juz_jest.value)+'&redirect_dodano='+(redirect_dodano.value)+'&redirect_usunieto='+(redirect_usunieto.value)+'&redirect_brak_w_bazie='+(redirect_brak_w_bazie.value);
		}
	}
}

function usun(){
var elem = document.getElementById('subform');
	if ( validate() ){
		with(elem) {
			location=action+'?job=usun_email&email='+(email.value)+'&lista='+(lista.value)+'&redirect_brak_emaila='+(redirect_brak_emaila.value)+'&redirect_email_niepraw='+(redirect_email_niepraw.value)+'&redirect_email_juz_jest='+(redirect_email_juz_jest.value)+'&redirect_dodano='+(redirect_dodano.value)+'&redirect_usunieto='+(redirect_usunieto.value)+'&redirect_brak_w_bazie='+(redirect_brak_w_bazie.value);
		}
	}
}

function usun2(){
var elem = document.getElementById('subform2');
	if ( validate2() ){
		with(elem) {
			location=action+'?job=usun_email&email='+(email2.value)+'&lista='+(lista.value)+'&redirect_brak_emaila='+(redirect_brak_emaila.value)+'&redirect_email_niepraw='+(redirect_email_niepraw.value)+'&redirect_email_juz_jest='+(redirect_email_juz_jest.value)+'&redirect_dodano='+(redirect_dodano.value)+'&redirect_usunieto='+(redirect_usunieto.value)+'&redirect_brak_w_bazie='+(redirect_brak_w_bazie.value);
		}
	}
}

function validate(){
	var rx=new RegExp("^[\\w\.=-]+@[\\w\\.-]+\\.[a-zA-Z]{2,4}$");
	var errors = "";
	if (document.getElementById("email").value == "" || !rx.test(document.getElementById("email").value)) {
		errors += "Nie wypełniono pola \"e-mail\" lub podano nieprawidłowy adres e-mail\n";
	}
	if (errors == "") {
		return true;
	} else {
		alert("Proszę poprawić następujące błędy:\n\n"+errors);
		return false;
	}
}

function validate2(){
	var rx=new RegExp("^[\\w\.=-]+@[\\w\\.-]+\\.[a-zA-Z]{2,4}$");
	var errors = "";
	if (document.getElementById("email2").value == "" || !rx.test(document.getElementById("email2").value)) {
		errors += "Nie wypełniono pola \"e-mail\" lub podano nieprawidłowy adres e-mail\n";
	}
	if (errors == "") {
		return true;
	} else {
		alert("Proszę poprawić następujące błędy:\n\n"+errors);
		return false;
	}
}


//popupGlowny

function popupGlowny(url, width, height) {
	var okno = window.open(url,"popup",'width=' + width + ',height=' + height + ',resizable=0,scrollbars=no,menubar=no' );
	}


//czyszczenieFormularzaKsiążkiTelefonicznejPrywatnych

function clearFormPrivate(f) {
	f.nazwisko.value="";
	f.imie.value="";
	f.miejscowosc.value="";
	f.ulica.value="";
}

//czyszczenieFormularzaKsiążkiTelefonicznejFirm

function clearFormFirm(f) {
	f.nazwa_klienta.value="";
	f.branza_nazwa.value="";
	f.miejscowosc.value="";
	f.nazwa_ulicy.value="";
	f.numer_posesji.value="";
	f.numer_telefonu_z_kierunkowym.value="";
	f.wojewodztwo.selectedIndex="0";
}


//

var related = function() {
	var defs = {
		'm':['s'],
		'ulica':['s','m'],
		'nrb':['s','m'],
		'token':['s','m','nrb'],
		'end':['s','m','nrb','token']
	}
	var ok = 1;
	for(var i in defs) {
		for(var j=0;j<defs[i].length;j++) {
			($(defs[i][j]).value!=0 && !$(defs[i][j]).disabled)?
				($(i).disabled=false)&&(ok=1):
				($(i).disabled=true)&&(ok=0);
		}
	}
	(ok!=1)?$('end').style.visibility = 'hidden':$('end').style.visibility = '';
}


// befor onLoad

function init() {
	// quit if this function has already been called
	if (arguments.callee.done) return;

	// flag this function so we don't do the same thing twice
	arguments.callee.done = true;

	// kill the timer
	if (_timer) clearInterval(_timer);
	/*
	if (_timer) {
		clearInterval(_timer);
		_timer = null;
	}
	*/

	// do stuff
};

/* for Mozilla/Opera9 */
if (document.addEventListener) {
	document.addEventListener("DOMContentLoaded", init, false);
	document.addEventListener("DOMContentLoaded", change, false);
	document.addEventListener("DOMContentLoaded", pops, false);
	document.addEventListener("DOMContentLoaded", bookmark, false);
	document.addEventListener("DOMContentLoaded", selectOnChangeSubmit, false);
}


/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
	var _timer = setInterval(function() {
		if (/loaded|complete/.test(document.readyState)) {
			init(); // call the onload handler
			change();
			pops();
			bookmark();
			selectOnChangeSubmit();
		}
	}, 10);
}

//zamykanie toplayera

var zamknijTop = function(id){
	id=id||'topLayer';
	var TL = document.getElementById(id);
	return (TL ? TL.parentNode.removeChild(TL) : false)
}

//onload

window.onload = function() {
	init;
	change();
	pops();
	bookmark();
	selectOnChangeSubmit();
}