$(document).ready(function () {

	var fixHelper = function(e, ui) {
		ui.children().each(function() {
			$(this).width($(this).width());
		});
		return ui;
	};

	$( "#sortableBaner tbody" ).sortable({
		helper: fixHelper,
		revert: true,
		opacity: 0.5, 
		update: function() {
			serial = $('#sortableBaner tbody').sortable('serialize');
			//alert(serial);
		
			$.ajax({
			url: "ajax.gateway.php?req=posortujBanery",
			type: "post",
			data: serial,
			error: function(){
					alert("theres an error with AJAX");
				},
			success: function(){
					
				
					
				}
			});
		}
	});
	$("#sortableBaner tbody").disableSelection();
	
	
	$('#bannersTitle').nivoSlider({ effect: 'fade',directionNavHide:false, controlNav: false, pauseTime:5000, pauseOnHover:false });
		
});

function gotoQ(target,pytanie) {
	var answer = confirm(pytanie)
	if (answer){
		document.location.href = target;
	}
}

function checkSimpleQuery()
{
	var checkedField=1; //0
	
	/*
	for(var i=0;i<document.getElementById('queryForm').elements['filter[]'].length;i++)
	{
		if(document.getElementById('queryForm').elements['filter[]'][i].checked)
			{
			checkedField++;
			}
	}
	*/
	
	if(document.getElementById('query').value.length<1)
	{
		alert('Proszę wpisać frazę do wyszukania.');
	}
	else if(document.getElementById('query').value.length<3 || (document.getElementById('query').value.length==3 && document.getElementById('query').value.indexOf(' ')!=-1))
	{
		alert('Wprowadzona fraza musi posiadać przynajmniej 3 znaki.'); 
	}
	else
	{
			if(checkedField>0)
			{
				document.getElementById('queryForm').submit();
			}
			else
			{
				alert('Proszę wybrać przynajmniej jedno kryterium, względem którego mają zostać znalezione przedsiębiorstwa.');
			}
	}
	
}

function checkAdvancedQuery()
{
	var checkedField=0;
	
	if(document.getElementById('nazwaField').value.length<3 && document.getElementById('ulicaField').value.length<3 && document.getElementById('miastoField').value.length<3 && document.getElementById('branzaField').value.length<3)
	{
		alert('Proszę wpisać nazwę, branże, miasto albo ulicę.');
	}
	else
	{
		document.getElementById('queryForm').submit();
	}
	
}

function katalogToggle(mode,target)
{
	if(mode==1)
	{
	target.style.backgroundColor='#cd6260';
	target.getElementsByTagName('a')[0].style.color='#ffffff';
	}
	else
	{
	target.style.backgroundColor='#bc4a48';
	target.getElementsByTagName('a')[0].style.color='#ffffff';	
	}

}


function emailForm(){

var error=0;

var name = prompt("Podaj swoje imię:", ""); 

var remail = prompt("Podaj swój adres e-mail:", ""); 

var daReferrer = document.URL;
var email = prompt("Podaj adres osoby, której chcesz polecić stronę:", "");
var subject = "INFORMAX: link do poleconej strony";
var body_message = "<b>Witaj!</b>%0DUżytkownik serwisu Informax.pl o imieniu "+name+' polecił Tobie następującą stronę:%0D <a href="'+daReferrer+'">'+daReferrer+'</a>%0D%0D%0DAdres e-mail osoby polecającej: '+remail;

var mailto_link = 'mailto:'+email+'?subject='+subject+'&body='+body_message;

if(!name)
	{
	error=1;
	}
if(!email)
	{
	error=1;
	}
if(!remail)
	{
	error=1;
	}
	

	if(error!=1)
	{
		if(email.length<5 || remail.length<5 || name.length<1)
		{
			error=1;
		}
		else if(email.indexOf(".")<3 || email.indexOf("@")<2 || remail.indexOf(".")<3 || remail.indexOf("@")<2)
		{
			error=1;
		}
	}

	
	if(error==1)
	{
		alert('Niestety wprowadzono niepoprawne dane');
	}
	else
	{
		win = window.open(mailto_link,'emailWindow');
		if (win && win.open &&!win.closed) win.close();
	}
}


//formularz dodawania firmy
function ValidNIP(input_data)
{
	if(!input_data.match( /^[0-9]{3}-[0-9]{2}-[0-9]{2}-[0-9]{3}$/ ) && !input_data.match( /^[0-9]{3}-[0-9]{3}-[0-9]{2}-[0-9]{2}$/ ) && !input_data.match( /^[0-9]{10}$/ ))
		return false;
	var my_nums=input_data.replace(/-/g,'');
	var valid_nums="657234567";
	var sum=0;
	for(var temp=8;temp>=0;temp--)
		sum += (parseInt(valid_nums.charAt(temp)) * parseInt(my_nums.charAt(temp)));
	if ( (sum % 11) == 10 ? false : ((sum % 11) == parseInt(my_nums.charAt(9))) )
		return true;
	else
		return false;
}


function validateStep(step)
{
var form=document.getElementById('addForm');

//czyszczenie kolorowych pol
form.nazwa.style.backgroundColor='#ffffff';
form.miasto.style.backgroundColor='#ffffff';
form.ulica.style.backgroundColor='#ffffff';
form.kod.style.backgroundColor='#ffffff';
form.nip.style.backgroundColor='#ffffff';
form.telefon.style.backgroundColor='#ffffff';
form.branza1.style.backgroundColor='#ffffff';
form.typ1.style.backgroundColor='#ffffff';
form.emailAlboTelefon.style.backgroundColor='#ffffff';
form.imie.style.backgroundColor='#ffffff';
form.nazwisko.style.backgroundColor='#ffffff';
form.adresDoKorespondencji.style.backgroundColor='#ffffff';
form.daneDoFaktury.style.backgroundColor='#ffffff';



	switch(step)
	{
	case 1:
		if(form.nazwa.value.length<3)
			{
			alert("Proszę wpisać poprawną nazwę firmy!");
			form.nazwa.focus();
			form.nazwa.style.backgroundColor='#ffa7a7';
			return false;
			}
		if(form.miasto.value.length<3)
			{
			alert("Proszę wpisać poprawną nazwę miasta!");
			form.miasto.focus();
			form.miasto.style.backgroundColor='#ffa7a7';
			return false;
			}
		if(form.kod.value.length!=6 || form.kod.value.indexOf('-')==-1)
			{
			alert("Proszę wpisać poprawny kod pocztowy!");
			form.kod.focus();
			form.kod.style.backgroundColor='#ffa7a7';
			return false;
			}
		if(form.ulica.value.length<3)
			{
			alert("Proszę wpisać poprawną nazwę ulicy!");
			form.ulica.focus();
			form.ulica.style.backgroundColor='#ffa7a7';
			return false;
			}			
		if(form.nip.value.length<3 || !ValidNIP(form.nip.value))
			{
			alert("Proszę wpisać poprawny numer NIP!");
			form.nip.focus();
			form.nip.style.backgroundColor='#ffa7a7';
			return false;
			}	
		if(form.telefon.value.length<3)
			{
			alert("Proszę wpisać poprawny telefon kontaktowy do firmy!");
			form.telefon.focus();
			form.telefon.style.backgroundColor='#ffa7a7';
			return false;
			}				
	
	document.getElementById('step1div').style.display='none';
	document.getElementById('step2div').style.display='block';
	
	document.getElementById('step1').src='./images/step1.jpg';
	document.getElementById('step2').src='./images/step2A.jpg';
	break;

	case 2:

		if(form.branza1.options[form.branza1.selectedIndex].value=='')
			{
			alert("Proszę wybrać branżę podstawową!");
			form.branza1.focus();
			form.branza1.style.backgroundColor='#ffa7a7';
			return false;
			}
		if(form.typ1.options[form.typ1.selectedIndex].value=='')
			{
			alert("Proszę wybrać podstawowy typ działalności!");
			form.typ1.focus();
			form.typ1.style.backgroundColor='#ffa7a7';
			return false;
			}			
	
	document.getElementById('step2div').style.display='none';
	document.getElementById('step3div').style.display='block';
	
	document.getElementById('step2').src='./images/step2.jpg';
	document.getElementById('step3').src='./images/step3A.jpg';
	break;	
	
	case 3:

		if(!form.wyborPromocji[0].checked && !form.wyborPromocji[1].checked && !form.wyborPromocji[2].checked && !form.wyborPromocji[3].checked)
			{
			alert("Proszę wybrać ofertę!");
			form.wyborPromocji[0].focus();
			return false;
			}
	
	document.getElementById('step3div').style.display='none';
	document.getElementById('step4div').style.display='block';
	
	document.getElementById('step3').src='./images/step3.jpg';
	document.getElementById('step4').src='./images/step4A.jpg';
	break;		
	
	case 4:

		if(!form.daneDoFakturySkopiuj.checked && form.daneDoFaktury.value.length<3)
			{
			alert("Proszę podać dane do faktury albo zaznaczyć pole użycia danych z formularza!");
			form.daneDoFaktury.focus();
			form.daneDoFaktury.style.backgroundColor='#ffa7a7';			
			return false;
			}
			
		if(!form.adresDoKorespondencjiSkopiuj.checked && form.adresDoKorespondencji.value.length<3)
			{
			alert("Proszę podać adres do korespondencji albo zaznaczyć pole użycia danych z formularza!");
			form.adresDoKorespondencji.focus();
			form.adresDoKorespondencji.style.backgroundColor='#ffa7a7';			
			return false;
			}			
			
		if(!form.zgodaNaPrzetwarzanie.checked)
			{
			alert("Proszę wyrazić zgodę na przetwarzanie danych!");
			form.zgodaNaPrzetwarzanie.focus();	
			return false;
			}					

		if(form.imie.value.length<3)
			{
			alert("Proszę podać imię osoby do kontaktu!");
			form.imie.focus();
			form.imie.style.backgroundColor='#ffa7a7';			
			return false;
			}				

		if(form.nazwisko.value.length<3)
			{
			alert("Proszę podać nazwisko osoby do kontaktu!");
			form.nazwisko.focus();
			form.nazwisko.style.backgroundColor='#ffa7a7';			
			return false;
			}	

		if(form.emailAlboTelefon.value.length<3)
			{
			alert("Proszę podać e-mail albo numer telefonu!");
			form.emailAlboTelefon.focus();
			form.emailAlboTelefon.style.backgroundColor='#ffa7a7';			
			return false;
			}				
			
	form.submit();
			
	break;		
	
	}
	
}



function gotoStep(step)
{
	switch(step)
	{
	case 1:
		document.getElementById('step1div').style.display='block';
		document.getElementById('step2div').style.display='none';		
		document.getElementById('step1').src='./images/step1A.jpg';
		document.getElementById('step2').src='./images/step2.jpg';		
	break;
	
	case 2:
		document.getElementById('step2div').style.display='block';
		document.getElementById('step3div').style.display='none';		
		document.getElementById('step2').src='./images/step2A.jpg';
		document.getElementById('step3').src='./images/step3.jpg';		
	break;	
	
	case 3:
		document.getElementById('step3div').style.display='block';
		document.getElementById('step4div').style.display='none';		
		document.getElementById('step3').src='./images/step3A.jpg';
		document.getElementById('step4').src='./images/step4.jpg';		
	break;		

	
	}
}

function toggleDoFaktury()
{
	if(document.getElementById('addForm').daneDoFakturySkopiuj.checked)
	{
		document.getElementById('addForm').daneDoFaktury.disabled=true;
		
		var form=document.getElementById('addForm');
		var dane='Nazwa: '+form.nazwa.value+'\n'+'Ulica: '+form.ulica.value+'\n'+'Kod i miasto: '+form.kod.value+' '+form.miasto.value+'\n'+'NIP: '+form.nip.value+'\n'+'Telefon: '+form.telefon.value;
		
		document.getElementById('addForm').daneDoFaktury.value=dane;
		
	}
	else
	{
		document.getElementById('addForm').daneDoFaktury.disabled=false;
	}
}

function toggleDoKorespondencji()
{
	if(document.getElementById('addForm').adresDoKorespondencjiSkopiuj.checked)
	{
		document.getElementById('addForm').adresDoKorespondencji.disabled=true;
		
		var form=document.getElementById('addForm');
		var dane='Nazwa: '+form.nazwa.value+'\n'+'Ulica: '+form.ulica.value+'\n'+'Kod i miasto: '+form.kod.value+' '+form.miasto.value;
		
		document.getElementById('addForm').adresDoKorespondencji.value=dane;		
	}
	else
	{
		document.getElementById('addForm').adresDoKorespondencji.disabled=false;
	}
}
