Math.decimal = function(n, k) {
	var factor = Math.pow(10, k+1);
	n = Math.round(Math.round(n*factor)/10);

	return n/(factor/10);
}

function pokaz_okno(okno,szerokosc,wysokosc) {
	window.open(okno,'zlotowskie.pl','width='+szerokosc+',height='+wysokosc+',scrollbars=yes,resizable=no,status=yes,menubar=no');
}
function wydruk(modul,id_row) {
	window.open('/druk,'+modul+','+id_row+'.html','druk','width=620,height=494,scrollbars=yes,resizable=yes,status=yes,menubar=no');
}

function change_ceny(index) {
	if (document.formik.c_brutto.value!='0.00'&&index!="") {
		if (document.formik.publikuj.options[document.formik.publikuj.selectedIndex].value==1) {
			document.formik.c_brutto.value=tablica_cen_gazeta[index];
		} else if (document.formik.publikuj.options[document.formik.publikuj.selectedIndex].value==2) {
			document.formik.c_brutto.value=tablica_cen_portal[index];
		} else if (document.formik.publikuj.options[document.formik.publikuj.selectedIndex].value==3) {
			document.formik.c_brutto.value=(Math.decimal(tablica_cen_gazeta[index]+1.23, 2));
		}
	}
}

function wylicz_sume() {
	wylicz_liczbe_slow();
	wylicz_daty_emisji();
	var add_foto=0.00;
	var opcje=0.00;
	var opcje2=0.00;
	if (document.formik.c_brutto.value=='0.00') {
		document.formik.w_brutto.value='0.00';
	} else {
		if (document.formik.publikuj.options[document.formik.publikuj.selectedIndex].value==2||document.formik.publikuj.options[document.formik.publikuj.selectedIndex].value==3){// dolicz opłatę za zdjęcia
			if (document.formik.foto1.value!=""||(jest_foto_1==1 && document.formik.del_foto_1.checked==false))
				add_foto=add_foto+doplaty[4]
			if (document.formik.foto2.value!=""||(jest_foto_2==1 && document.formik.del_foto_2.checked==false))
				add_foto=add_foto+doplaty[5]
			if (document.formik.foto3.value!=""||(jest_foto_3==1 && document.formik.del_foto_3.checked==false))
				add_foto=add_foto+doplaty[6]
		}
		if (document.formik.od_do.value=='1') { // terminy edycji od do; opcje pogrubienia itp.
			if (document.formik.op_bold.checked==true) opcje=opcje+doplaty[0];
			if (document.formik.op_border.checked==true) opcje=opcje+doplaty[1];
			if (document.formik.op_top.checked==true) opcje=opcje+doplaty[2];
			if (document.formik.op_box.checked==true) opcje=opcje+doplaty[3];
		} else { // terminy edycji różne; opcje pogrubienia itp.
			if (document.formik.te_same.checked==true) {// te same opcje dla wszystkich edycji
				if (document.formik.op_bold_1.checked==true) opcje=opcje+doplaty[0];
				if (document.formik.op_border_1.checked==true) opcje=opcje+doplaty[1];
				if (document.formik.op_top_1.checked==true) opcje=opcje+doplaty[2];
				if (document.formik.op_box_1.checked==true) opcje=opcje+doplaty[3];
			} else {// różne opcje dla różnych edycji
				if (document.formik.op_bold_1.checked==true) opcje2=opcje2+doplaty[0];
				if (document.formik.op_border_1.checked==true) opcje2=opcje2+doplaty[1];
				if (document.formik.op_top_1.checked==true) opcje2=opcje2+doplaty[2];
				if (document.formik.op_box_1.checked==true) opcje2=opcje2+doplaty[3];
				var boldy=document.formik.elements['op_bold_[]'];
				var bordery=document.formik.elements['op_border_[]'];
				var topy=document.formik.elements['op_top_[]'];
				var boxy=document.formik.elements['op_box_[]'];
				for (i=0;i<=te;i++) {
					if (boldy[i].checked==true) opcje2=opcje2+doplaty[0];
					if (bordery[i].checked==true) opcje2=opcje2+doplaty[1];
					if (topy[i].checked==true) opcje2=opcje2+doplaty[2];
					if (boxy[i].checked==true) opcje2=opcje2+doplaty[3];
				}
			}
		}
		// liczymy całkowitą sumę
		il_ed=document.formik.l_ed.value;
		cena_jedn=document.formik.c_brutto.value;
		suma=parseInt(il_ed, 10)*(parseFloat(cena_jedn)+parseInt(add_foto, 10)+Math.decimal(opcje, 2)+parseInt(dop)*doplaty[7])+opcje2;
		document.formik.w_brutto.value=Math.round(suma*100)/100;
	}
}

var dop=0;
var liczbaSlow=0;
var maxCountWords=0;
var te=1; // ilość edycji dla różnych
function wylicz_liczbe_slow(){
	if (document.formik.tresc.value.length == 0){
		liczbaSlow = 0;
		dop = 0;
	}
	liczbaSlow = 0;
	znaki=0;
	licznik=0;
	for (i=0; i < document.formik.tresc.value.length; i++) 	//liczymy spacje
		if (document.formik.tresc.value.charAt(i) == ' ')
			liczbaSlow++;
	modu=liczbaSlow/10;
	if (maxCountWords>0) {
		for (i=0; i < document.formik.tresc.value.length; i++) 	{
			if (document.formik.tresc.value.charAt(i) == ' '&&licznik!=maxCountWords) {
				znaki=i;
				licznik++;
			}
		}
		if (liczbaSlow>=maxCountWords) {
			document.formik.tresc.value = document.formik.tresc.value.substr(0,znaki);
		}
	} else {
		if (modu>=1) {
			if (dop!=Math.floor(modu)) {
				dop=Math.floor(modu);
				alert('Ogłoszenie ma powyżej 10 słów!!!');
			}
		} else {
			dop=0;
		}
	}
}

function wylicz_daty_emisji() {
	var ilosc_edycji=0;
	if (document.formik.od_do.value=='1') { // terminy edycji od do
		d_start=new Date();
		d_end=new Date();
		d_s=document.formik.data_od.value;
		d_e=document.formik.data_do.value;
		temp1=d_s.split('-');
		temp2=d_e.split('-'); 		// tworzymy właściwe daty
		d_start.setFullYear(parseInt(temp1[0], 10),(parseInt(temp1[1], 10)-1),parseInt(temp1[2], 10));
		d_end.setFullYear(parseInt(temp2[0], 10),(parseInt(temp2[1], 10)-1),parseInt(temp2[2], 10));
		for (d_start;d_start<=d_end;d_start.setDate(d_start.getDate()+7)) {
			w_tablicy=0;
			d_porown=d_start.getFullYear()+"-"+(d_start.getMonth()<9?'0':'')+(d_start.getMonth()+1)+"-"+(d_start.getDate()<10?'0':'')+d_start.getDate();
			for (i=0; i<d_em.length; i++) { // sprawdzamy czy data jest w tablicy
				dt=d_em[i];
				if (dt==d_porown) w_tablicy=1;
			}
			if (w_tablicy==0) { // doliczamy edycję
				ilosc_edycji++;
			}
		}
	} else {		// terminy różne terminy edycji
		ilosc_edycji=te;
	}
	document.formik.l_ed.value=ilosc_edycji;
}

function change_terminy() {
	if ($('#od_do:visible')) {
		document.formik.od_do.value='0';
		$('#od_do').hide();
		$('#rozne').show();
	} else {
		document.formik.od_do.value='1';
		$('#od_do').show();
		$('#rozne').hide();
		// ukrywamy daty i opcje
		for (i=te;i>=2;i--) {
			$('#te_'+i).hide();
			$('#te_op_'+i).hide();
			te--;
		} 
	}
}

function add_termin() {
	if (te<49) {
		te++;
		$('#te_'+te).show();
		if (document.formik.te_same.checked!=true) {
			$('#te_op_'+te).show();
		}
	}
}
function remove_termin() {
	if (te>1) {
		$('#te_'+te).hide();
		$('#te_op_'+te).hide();
		te--;
	}
}

function potwierdzenie(pytanie){
	if (confirm(pytanie)) {
    	return true;
  	} else {
    	return false;
  	} 
}

function photo_ogloszenia(ido) {
	window.open('/photo_ogloszenie.php?ido='+ido,'powiekszenie','width=610,height=530,scrollbars=no,resizable=no,status=yes,menubar=no');
}
