var varFromUrl = location.search.slice(1).split("&");
for (var i=0,len=varFromUrl.length; i<len; i++){
	temp = varFromUrl[i].split("=");
	if(temp.length>1 && temp[0].substring(0,1)!='?') eval(temp[0]+"='"+temp[1]+"'");
}
var promo = promo || false;
var poch = poch || false;
var src = src || false;
var pochodzenie = pochodzenie || poch || src || promo;
var charset = charset || 'iso-8859-2';

var jQueryScriptOutputted = false;
function initJQuery() {    
    //if the jQuery object isn't available
    if (typeof(jQuery) == 'undefined') {
        if (! jQueryScriptOutputted) {
            //only output the script once..
            jQueryScriptOutputted = true;            
            //output the script (load it from google api)
            document.write("<scr" + "ipt type=\"text/javascript\" src=\"http://www.elettery.pl/_js/jquery-1.2.6.min.js\"></scr" + "ipt>");
        }
        setTimeout("initJQuery()", 50);
    }             
}
if (jQueryScriptOutputted != true) { 
	initJQuery();
}
if (typeof eletter_form != 'function'){
	function eletter_form (param) {
		if (param.container && (d = document.getElementById(param.container))){
			d.innerHTML = render()+getStyles(); 
		}
		else {
			document.write(render()+getStyles());	
		}
		
		// metody -------------------------------------------------------------------
		function get_komunikaty_content(){
			var content = "";
			var typy_komunikatow = {'potwierdzenie_nowy':0,'mail':1,'mail_link':2,'potwierdzenie_stary':3};
			for (var i in param.komunikaty) {
				var komunikat = param.komunikaty[i];
				var l = typy_komunikatow[i];
				content+="<input type='hidden' name='komunikat["+l+"]' value='"+komunikat+"' />";
			}
			return content;
		}
		function get_opcje_content(){
			if (!param.opcje) return "";
			var content = "";
			var nazwy = "";
			var blok = param.opcje;
			var opis = blok.opis; 
			if (opis) content +="<div class='opcje_opis' style='"+parse_style(opis.style)+"'>"+opis.tekst+"<\/div>";
			var styl = parse_style(blok.style_opcje);
			content+="<table style='width: 99%;'>";
			for (var i=0,len=blok.lista.length; i<len; i++) {
				var stan=i%blok.kolumny;
				if (stan==0) content+="<tr>\n"; 
				content+="<td class='eForm_opcja' style='"+styl+"'><input type='checkbox' value='1' name='opcja["+i+"]' />"+blok.lista[i]+"<\/td>\n";
				if (stan==(blok.kolumny-1) || (stan==i-1)) content+="<\/tr>";
			}
			content+="<\/table>";
			
			content+="<div style='clear:both;'><\/div>";
			content+="<input type='hidden' name='opcja|validate' value='' title='Wybierz przynajmniej jedną opcję' /><input type='hidden' name='nazwy_opcji' value='"+blok.lista.join(";")+"' />";
			content+="<input type='hidden' name='nazwa_pliku' value='"+blok.nazwa_pliku+"' />";
			content+="<input type='hidden' name='ilosc_opcji' value='"+(parseInt(i)+1)+"' />";
			return content;
		}
		function get_elettery_content(){
			var content = "";
			var blok = param.elettery || {};
			var hidden = false;
			var opis = blok.opis;
			if (opis) content +="<div class='elettery_tekst' style='"+parse_style(opis.style)+"'>"+opis.tekst+"<\/div>";
			var lista = new Array();
			var l = 0;
			for (var i=0,len=blok.lista.length; i<len; i++) {
				var eletter = blok.lista[i];
				if (eletter.nazwa == undefined){
					content+="<input type='checkbox' name='nlbox["+i+"]' value='"+eletter.nl+"' checked='checked' style='display: none;' \/>\n";
					hidden = true;
					}
				else {
					if (eletter.stan) var check = "checked='checked'";
					else var check = "";
					lista.push("<input type='checkbox' name='nlbox["+i+"]' value='"+eletter.nl+"' "+check+" />"+eletter.nazwa);
				}
			}
			if (blok.wszystkie) {
				var w = blok.wszystkie;
				if (!w.pozycja) w.pozycja="bottom";
				if (!w.tekst) w.tekst="Wszystkie";
				var wszystkie_check ="<input type='checkbox' onclick='eForm_wszystkie(\""+form_nazwa+"\",this,\"nlbox\")' /><span style='"+parse_style(w.style)+"'>"+w.tekst+"</span>";
				if (w.pozycja=='top') lista.splice(0, 0, wszystkie_check);
				else lista.push(wszystkie_check);
			}
			content+="<table style='width: 98%;'>";
			var style = (blok.tekst && blok.tekst.style) ? parse_style(blok.tekst.style) : "";
			for (var i=0,len=lista.length; i<len; i++) {
			  var stan=i%blok.kolumny;
			  if (stan==0) content+="<tr>\n"; 
			  content+="<td style='"+style+"'>"+lista[i]+"<\/td>\n";
			  if (stan==(blok.kolumny-1) || (stan==i-1)) content+="<\/tr>";
			}
			content+="<\/table>";
			var validate = "";
			if (hidden) validate = true;
			content+="<input type='hidden' title='Wybierz przynajmniej jeden E-letter' name='nlbox|validate' value='"+validate+"' />";
			return content;
		}
		function get_pola_content(){
			var content = "";
			var blok = param.pola || {};
			var tekst_style = (blok.tekst && blok.tekst.style) ? parse_style(blok.tekst.style) : "";
			var input_style = (blok.input && blok.input.style) ? parse_style(blok.input.style) : "";
			var inputParent_style = (blok.inputParent && blok.inputParent.style) ? "style='"+parse_style(blok.inputParent.style)+"'" : "";
			for (var i=0,len=blok.lista.length; i<len;i++){
			  var pole = blok.lista[i];
			  if (pole.wartosc || typeof (pole.wartosc)=='object') {
					var nazwaListy = form_nazwa+"_listaWartosci_"+pole.nazwa;
					var tPole = "<input name='"+pole.nazwa+"' style='"+input_style+"' type='text' title='Wypełnij poprawnie pole "+pole.opis+"' onfocus='eForm_pokaz_listaWartosci({lista:\""+nazwaListy+"\",pole:this,stan:true})' onblur='eForm_pokaz_listaWartosci({lista:\""+nazwaListy+"\",pole:this,stan:false})' /><br />";
					tPole +="<div id='"+nazwaListy+"' style='"+input_style+"' class='eForm_listaWartosci'>\n";
					for (var j=0; j < pole.wartosc.length; j++) {
					  tPole+="<a onmousedown='eForm_wstawWartosc(\""+form_nazwa+"\",\""+pole.nazwa+"\",\""+pole.wartosc[j]+"\")' href='javascript: void(1)' >"+pole.wartosc[j]+"<\/a>\n";
					  }
					tPole += "<\/div>\n";
					}
			  else {
				  pole.value = (pole.value ? "onfocus='this.value=\"\";' value='"+pole.value+"'" : '');
				  //if(pole.value) {pole.value= "onfocus='this.value=\"\";' value='"+pole.value+"'";} else {pole.value='';}  
				  var tPole="<input name='"+pole.nazwa+"' style='"+input_style+"' type='text' title='Wypełnij poprawnie pole "+pole.opis+"' "+pole.value+"/>";
			  }
			  content+="<tr>";
			  content+="<td class='eFormCont_tekst' style='"+tekst_style+"'>"+pole.opis+"<\/td>";
			  if (blok.tekst.pozycja=="top") content+="<\/tr><tr>";
			  content+="<td class='eFormCont_pole' "+inputParent_style+">"+tPole+"<\/td><\/tr>\n";
			}
			return "<table class='eFormCont_pozycja_"+blok.tekst.pozycja+"'>"+content+"<\/table>";
		}
		function get_przetwarzanie_content(o){
			return "Dane osobowe będą przetwarzane przez Wydawnictwo Wiedza i&nbsp;Praktyka&nbsp;sp.&nbsp;z&nbsp;o.o. z&nbsp;siedzibą w&nbsp;Warszawie, ul.&nbsp;Łotewska&nbsp;9A, w celach realizacji zamówienia lub marketingu własnych produktów i&nbsp;usług wydawnictwa. Każdej osobie, której dane dotyczą, przysługuje prawo dostępu do treści swoich danych i&nbsp;ich poprawiania. Podanie danych jest dobrowolne.";
		}
		function get_zgoda_content(){
			var blok = param.zgoda || {};
			var content = "";
			var zgodaTekst = "Zaznaczając pole obok, wyrażam zgodę na otrzymywanie od Wydawnictwa Wiedza i Praktyka sp. z o.o. informacji handlowych przesyłanych środkami komunikacji elektronicznej.";
			//var zgodaClass = "zgoda_"+.zgoda;
			var tekst_style = (blok && blok.tekst && blok.tekst.style) ? parse_style(blok.tekst.style) : "";
			var tekst_cont_style = (blok && blok.tekst_cont && blok.tekst_cont.style) ? parse_style(blok.tekst_cont.style) : "";
			//var wymog_tekst = blok.wymog_tekst || "Zgoda na wysyłanie informacji handlowych jest wymagana, aby zapisać sie na ten E-Letter";
			var wymog_tekst = blok.wymog_tekst || "Nie możemy zrealizować tej usługi bez Twojej zgody. Jeśli chcesz otrzymywać e-letter zaznacz pole checkbox.";
			content+="<table><tr>";
			content+="<td class='zgoda_checkbox'><input type='checkbox' name='zgoda' value='tak' title='"+wymog_tekst+"' checked='checked' /><\/td>";
			content+="<td class='td_zgoda_tekst' style='"+tekst_cont_style+"'><div class='zgoda_tekst' style='"+tekst_style+"'>"+zgodaTekst+"<\/div><\/td>";
			content+="<\/tr><\/table>";
			return content;
		}
		function get_regulamin_content(){
		  	var blok = param.regulamin;
			var content = '';
			$(function(){
				$('body').prepend('<div id="eForm-regulamin">'
						+'<div class="eForm-regulamin-content">'
							+'Wyrażam zgodę na otrzymywanie od Wydawnictwa Wiedza i Praktyka sp. z o.o. informacji handlowych przesyłanych środkami komunikacji elektronicznej.<br><Br> 	Dane osobowe będą przetwarzane przez Wydawnictwo Wiedza i&nbsp;Praktyka&nbsp;sp.&nbsp;z&nbsp;o.o. z&nbsp;siedzibą w&nbsp;Warszawie, ul.&nbsp;Łotewska&nbsp;9A, w celach realizacji zamówienia lub marketingu własnych produktów i&nbsp;usług wydawnictwa. Każdej osobie, której dane dotyczą, przysługuje prawo dostępu do treści swoich danych i&nbsp;ich poprawiania. Podanie danych jest dobrowolne.'
						+'</div>'
						+'<div class="eForm-regulamin-shadow-right"></div>'
						+'<div class="eForm-regulamin-shadow-bottom"></div>'
					+'</div>'
				);
				var $regBox = $('#eForm-regulamin');
				$('.eForm-regulamin-link').click(function(){
					var $this = $(this);
					var $form = $this.parents('.eForm');
					$form.find('input[name="zgoda"]').attr('checked','checked');
					return false;
				}).hover(function(){
					var $this = $(this);
					var $form = $this.parents('.eForm');
					var regBoxHeight = $regBox.width($form.width()).height();
					var thisTop = $this.offset().top;
					
					$regBox.css({
						'top': (thisTop-regBoxHeight-2)+'px',
						'left': $form.offset().left+'px'
					}).show();//.fadeIn();
				},function(){
					$regBox.hide();//fadeOut();
				});
			});
			content+='<div>'
				//+'<input type="hidden" name="zgoda" value="nie" />'
				+'<input type="checkbox" name="zgoda" value="tak" checked="checked" title="Akceptacja regulaminu jest wymagana, aby zapisać sie na ten E-Letter" />'
				+" <a class='eForm-regulamin-link' href='#'>akceptuję regulamin</a></div>";
			return content;
		}
		function get_submit_content(){
			var content = "";
			if (!param.submit) content = "<img src='http://emailing.wip.pl/grafiki/przyciski/tak_zapisuje_sie.gif' border='0' alt='wyślij' />";
			else if (param.submit.img) content = "<img src='"+param.submit.img+"' border='0' alt='wyślij' />";
			else if (param.submit.tekst) content = param.submit.tekst;
			return "<button type='submit' class='submit_button'>"+content+"<\/button>";
		}
		function parse_style(oStyle){ //funkcja przygotowująca style z podanych parametrów
			var style = "";
			for(var i in oStyle){
			  style+=i+":"+oStyle[i]+";";
			}
			return style;
		}
		function getValidate(){
			//podmiana nazw w walidacji, na ozpoznawane przez skrypt zapisujący.
			//if(param.walidacja==undefined) param.walidacja="opcje,elettery,imie,nazwisko,stanowisko,email,zgoda";
			var validators = [];
			var defs = param.walidacja.split(',');
			var v=0;
			for (var i=0, len=defs.length; i<len; ++i){
				var def = defs[i];
				if (def == 'opcje') def = 'opcja|validate';
				else if (def == 'elettery') def = 'nlbox|validate';
				else if (def == 'zgoda') continue;
				if (typeof def =='undefined' || def.length>0) validators[v++]=def;
			}
			validators[v]='zgoda';
			//console.log(validators);
			return validators.join(',');
		}
	//-----------------------------------------------------wyświetlenie formularza
		function render(){
			var form_nazwa = "eForm"+(Math.round(Math.random()*1000));
			var main_kontener_id = form_nazwa+"_kontener";
			
			param.walidacja = getValidate();
		
			//inicjalizacja zmiennych
			if (!param.elettery.kolumny) param.elettery.kolumny=1;
			this.ver = "3.5";
			
			var action = param.action || 'http://elettery.pl/add.php';
			var zmienne = param.zmienne || [];
	
			var content = "";
			if (!param.kolejnosc){
				param.kolejnosc = ["komunikaty","opcje","elettery","pola","przetwarzanie","zgoda","submit"];
			}
			if (!inArray(param.kolejnosc,'przetwarzanie') && !inArray(param.kolejnosc,'regulamin')) param.kolejnosc.push('przetwarzanie');
			                                                                                         
			//ustalanie styli dla kontenera głównego.
			var style = parse_style(param.style);
			var target = param.target || '_self';
			
			//content+="<link rel='stylesheet' href='http://media.wip.pl/_css/eletter_formularz_v3.css' type='text/css' />\n";
			content+="<div class='eFormCont' id='"+main_kontener_id+"' style='"+style+"'>\n";
			content+="<form name='"+form_nazwa+"' target='"+target+"' method='post' action='"+action+"' class='eForm' onsubmit='return eForm_validater({form:this,validate:\""+param.walidacja+"\"})'>\n";
			content+=getHiddenInputHtml('pochodzenie');
			content+=getHiddenInputHtml('promo');
			content+=getHiddenInputHtml('wipx');
			content+=getHiddenInputHtml('redirect', 'redirect.url');
	
			for (i in zmienne){
				content+="<input name='"+i+"' value='"+zmienne[i]+"' type='hidden' \/>\n";
			}
			content+="<input name='charset' value='"+charset+"' type='hidden' \/>\n";
			content+="<input name='ver' value='"+this.ver+"' type='hidden' />\n";
		 
			for (var i=0, len = param.kolejnosc.length; i < len; i++){
				var pozycja = param.kolejnosc[i];
				var oPozycja = eval('param.'+pozycja);
				var style = "";
				if (oPozycja) style = parse_style(oPozycja.style);
				var tresc = eval("get_"+pozycja+"_content(this);");
				if (tresc!="") content+="<div id='"+main_kontener_id+"_"+pozycja+"' class='eFormCont_"+pozycja+"' style='"+style+"'>"+tresc+"<\/div>";  
			}
			content+='<\/form><\/div>';
			return content;
		}
		function getHiddenInputHtml(name, path, params){
			if (!params) params = param;
			if (!path) path = name;
			
			var step = path.split('.');
			for (var i=0, len=step.length; i<len; ++i){
				var params = params[step[i]];
				if (typeof params == 'undefined') return '';
			}
	
			var val = params;
			if(!val) return '';
			return "<input name='"+name+"' value='"+val+"' type='hidden' />\n";
		}
		function getStyles(){
			if ($('#eForm-styles').size()>0) return '';
			return '<style type="text\/css" id="eForm-styles">'
				+'.eFormCont {width: 330px; margin: 2px auto; text-align: left; font-size: 10pt; font-family: Tahoma;}'
				+'.eFormCont .eForm {margin: 0px; padding: 0px;}'
				+'.eFormCont .input {margin: auto; padding: 0px;}'
				+'.eFormCont_elettery input {vertical-align: middle;}'
				+'.eFormCont_pola {clear: both; overflow: auto; padding: 2px; margin: 5px 0px;}'
				+'.eFormCont_pola table {width:99%; margin: auto; clear: both; border-collapse: collapse;}'
				+'.eFormCont_pozycja_left .eFormCont_tekst {width: 29%; text-align: center;}'
				+'.eFormCont_pozycja_left .eFormCont_pole {width: 68%; text-align: center;}'
				+'.eFormCont_pozycja_top .eFormCont_tekst {width: 98%; text-align: center;}'
				+'.eFormCont_pozycja_top .eFormCont_pole {width: 98%; text-align: center;}'
				+'.eFormCont .eFormCont_pole input {width: 98%; border: 1px solid black; margin: 1px auto;}'
				+'.eFormCont .eFormCont_pole select {width: 98%; border: 1px solid black; margin: 1px auto;}'
		
				+'.eFormCont_zgoda {clear: both; margin: 5px 0px;}'
				+'.eFormCont .zgoda_checkbox {text-align: center;}'
				+'.eFormCont .td_zgoda_tekst {font-size: 7pt; border: 1px solid black; background-color: white;}'
				+'.eFormCont div.zgoda_tekst {height: 35px; overflow: auto;}'
				+'#eForm-regulamin {display: none; position: absolute;}'
				+'.eForm-regulamin-content {border:1px solid black; background: ghostwhite; font-size: 10px; font-family: tahoma; padding: 5px; margin: 0px 6px 6px 0px; position:relative; z-index:2;}'
				+'.eForm-regulamin-shadow-right,.eForm-regulamin-shadow-bottom {position: absolute; background:gray; z-index:1;}'
				+'.eForm-regulamin-shadow-right {width:5px; top:6px; right:1px; bottom:1px;}'
				+'.eForm-regulamin-shadow-bottom {height:5px; left:6px; right:1px; bottom:1px;}'
				+'.eForm-regulamin-link {color: blue; text-decoration: underline;}'
				
		
				+'.eFormCont_submit {clear: both; text-align: center;}'
				+'.eFormCont_submit .submit_button {background-color: transparent; border:none; margin-top:4px; cursor: pointer; width: 100%;}'
		
				+'.eFormCont_przetwarzanie {text-align: center; font-size: 7pt; margin: 15px 0px;}'
				+'.eForm_listaWartosci {position:absolute; max-height: 80px; overflow:auto; background-color: white; border: 1px solid black; padding: 2px; display:none; margin-top: 0px;}'
				+'.eForm_listaWartosci a {display: block; color: black; text-decoration: none; width: 99%;}'
				+'.eForm_listaWartosci a:hover{background-color: gainsboro; cursor: pointer;}'
			+'<\/style>';
		}
	}
}
//------------------------------------------------------------------------------

///validacja - funkcja nie widzi wewnętrznych zmiennych - uruchamiana jest już po stworzeniu formularza i na jego obiektach działa
function eForm_validater(p){
	function validateEmail(email){
			if(email.length <= 0){return true;}
			var splitted = email.match("^(.+)@(.+)$");
			if(splitted == null) return false;
			if(splitted[1] != null ){var regexp_user=/^\"?[\w-_\.]*\"?$/;
				if(splitted[1].match(regexp_user) == null) return false;}
			if(splitted[2] != null){
				var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
				if(splitted[2].match(regexp_domain) == null){
  			var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
  			if(splitted[2].match(regexp_ip) == null) return false;
				}// if
				return true;
			}
		return false;
	}
	var pola = p.validate.split(",");
	if (!validateEmail(p.form.email.value)) {alert("Błędny adres E-mail."); return false;}
	for (var i=0; i < pola.length; i++){
		var pole=p.form.elements[pola[i]];
		if (typeof pole == 'undefined') continue;
		if (pole.type=='hidden') sprawdzCheckBox(p.form,pole.name);
		if (pole.value.length<2 ||(pole.type=='checkbox' && !pole.checked)) {alert(pole.title); return false;}
	}
	return true;
}

function sprawdzCheckBox(form,nazwaListy){
	var stan = "";
	var nazwaPola = nazwaListy.split("|")[0];
	var elementy = document.forms[form.name].elements;
	for (var i = 0; i<elementy.length; i++){
		var nazwa = elementy[i].name.split("[")[0];
		if(elementy[i].type=='checkbox' && nazwa==nazwaPola && elementy[i].checked) stan = true;
	}
	document.forms[form.name][nazwaListy].value=stan;
}
function inArray(arr,value){	
	for(var i=0,len=arr.length; i<len; ++i){
		if (arr[i]==value) return true;
	}
	return false;
}
function eForm_pokaz_listaWartosci(p){
	var o = document.getElementById(p.lista);
	var szer = p.pole.offsetWidth - 6;
	o.style.width = szer+"px";
	if (p.stan) o.style.display='block';
	else o.style.display='none';
}
function eForm_wstawWartosc(form,gdzie,co){
	var oGdzie = document.forms[form][gdzie];
	oGdzie.value = co;
}
function eForm_wszystkie(formName,box,nazwaGrupy){
	var inputs = document.forms[formName].getElementsByTagName("INPUT");
	for(var i=0, len=inputs.length; i<len; i++){
		if (inputs[i].name.split("[",1)[0]==nazwaGrupy) inputs[i].checked = box.checked;
	}
}
