// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {

    if(!radioObj)
		return "";
		
	var radioLength = radioObj.length;
	
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function toggle_delhint(o, div) {
	if( document.getElementById(div).style.display != 'block' ) {
		document.getElementById(div).style.display = 'block';
		o.innerHTML = 'Ukryj podpowiedź &raquo;';
	} else {
		document.getElementById(div).style.display = 'none';
		o.innerHTML = 'Uwagi dodatkowe &raquo;';
	}
}

function toggle_availhint(o, div ){

	if( document.getElementById(div).style.display != 'block' ) {
		document.getElementById(div).style.display = 'block';
		o.innerHTML = 'Ukryj warianty &raquo;';
	} else {
		document.getElementById(div).style.display = 'none';
		o.innerHTML = 'Zobacz ceny innych wariantów &raquo;';
	}
	
}

function nstKrok(my_id) {
	if( document.getElementById('ngc') ) document.getElementById('ngc').style.display = 'none';
	document.getElementById('zpyt').style.display = 'none';
	if( document.getElementById('prz') ) document.getElementById('prz').style.display = 'none';

	document.getElementById(my_id).style.display = 'block';
}

function nstKrok_cofnij(my_id) {
	if( document.getElementById('ngc') ) document.getElementById('ngc').style.display = 'block';
	document.getElementById('zpyt').style.display = 'block';
	if( document.getElementById('prz') ) document.getElementById('prz').style.display = 'block';

	document.getElementById(my_id).style.display = 'none';
}

function op_panel(b) {
	if( !b ) document.getElementById('op_panel').style.display = 'none';
		else document.getElementById('op_panel').style.display = 'block';
}

function toggle(n) {
	if( document.getElementById(n).style.display == 'block' ) document.getElementById(n).style.display = 'none';
		else document.getElementById(n).style.display = 'block';
}

function przelicz(start) {

	var ilosc_rat = parseInt(document.getElementById('rat').value);
	if( ilosc_rat < 3 ) {
		ilosc_rat = 3;
		document.getElementById('rat').value = ilosc_rat;
	} else if( ilosc_rat > 24 ) {
		ilosc_rat = 24;
		document.getElementById('rat').value = ilosc_rat;
	}

	if( !document.getElementById('wplata') ) {
		var cena_prod = parseInt(document.getElementById('dcen').innerHTML);
		var op_man = 0;
		var kred_suma = Math.round(cena_prod*(1+ilosc_rat/100));
		if( cena_prod < 500 ) {
		  var rata = format(Math.round((kred_suma/ilosc_rat)*10)/10, true);
		  document.getElementById('wys_raty').innerHTML = rata;
		} else {
		  var rata = Math.round(kred_suma/ilosc_rat);
		  document.getElementById('wys_raty').innerHTML = rata;
		}
	} else {

		wplata = parseInt(document.getElementById('wplata').value);

		if(wplata < 0 || ilosc_rat < 0 || isNaN(ilosc_rat) || isNaN(wplata)) {
			wplata = 0;
			if( document.getElementById('wplata') ) document.getElementById('wplata').value = 0;
			ilosc_rat = 12;
			document.getElementById('rat').value = 12;
			//document.getElementById('wys').innerHTML = '--';
			//document.getElementById('k_cen').innerHTML = ' -- zł';
		}
		//} else {
			var cena_prod = parseInt(document.getElementById('dcen').innerHTML);
			if(wplata > cena_prod) var wplata = cena_prod;
			if( cena_prod < 500 ) op_man = 0; else op_man = 0;
			if(wplata > 0 && wplata <= cena_prod-55) {
				var cena_prod = cena_prod-wplata;
				document.getElementById('d_wpl').innerHTML = '<em class="bfr">'+wplata+'zł</em>- pierwsza wpłata';
			} else if( wplata >= cena_prod-55 ) {
				var wplata = cena_prod-55;
				var cena_prod = cena_prod-wplata;
				if( document.getElementById('wplata') ) document.getElementById('wplata').value = wplata;
				document.getElementById('d_wpl').innerHTML = '<em class="bfr">'+wplata+'zł</em>- pierwsza wpłata';
			} else {
				var wplata = 0;
				document.getElementById('d_wpl').innerHTML = '';
			}
			var kred_suma = Math.round(cena_prod*(1+ilosc_rat/100));
			if( cena_prod < 500 ) {
  		  var rata = format(Math.round((kred_suma/ilosc_rat)*10)/10, true);
  		  document.getElementById('d_rat_koszt').innerHTML = format(Math.round(ilosc_rat*rata*10)/10, true)+'zł';
  		  document.getElementById('k_cen').innerHTML = format(Math.round(ilosc_rat*rata*10)/10+op_man+0+wplata, true)+'zł';
  		} else {
  		  var rata = Math.round(kred_suma/ilosc_rat);
  		  document.getElementById('d_rat_koszt').innerHTML = ilosc_rat*rata+'zł';
  		  document.getElementById('k_cen').innerHTML = ilosc_rat*rata+op_man+0+wplata+'zł';
  		}

			document.getElementById('d_il_rat').innerHTML = ilosc_rat;
			document.getElementById('wys').innerHTML = rata+' zł';
			document.getElementById('wys_raty').innerHTML = rata;
			//document.cookie = poz+'='+pole.value+';expires='+ expires_date.toGMTString();
		//}
	}
}

function pokaz_pole(obiekt) {
	document.getElementById(obiekt).style.display = 'block';
}

function ukryj_pole(obiekt) {
	document.getElementById(obiekt).style.display = 'none';
}

function clear_f(n) {
	document.getElementById(n).value = '';
}

function isMail(x)
{
	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  	var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  	return (!r1.test(x) && r2.test(x));
}

function email_f(v) {
	if( v != '' && !isMail(v) ) pokaz_pole('email'); else ukryj_pole('email');
}

function oper_ilosc(poz, act) {
	var pole = document.getElementById('il'+poz);
	var pole_cen = document.getElementById('cen_'+poz);
	var pol_cen = new String();
	var sum = new String();
	var today = new Date();
	today.setTime( today.getTime() );
	var expires_date = new Date( today.getTime() + (1000*60*30) );
	var max_il = document.getElementById('max_'+poz);
	if( (act == '+' && pole.value < 99) || (act == '-' && pole.value > 1) ) {
		if( max_il.value > 0 && act == '+' && pole.value == max_il.value ) {
			return true;
		}
		il = parseInt(pole.value);
		ceny = parseFloat(pole_cen.innerHTML);
		sum = parseFloat(document.getElementById('sum_cen').innerHTML);
		jed = parseFloat(document.getElementById('jed_'+poz).innerHTML);
		if( act == '+' ) pole.value = il+1; if( act == '-' ) pole.value = il-1;
		pol_cen = ""+jed*pole.value;
		if( pol_cen.indexOf('.') == -1 ) pol_cen = Math.round(pol_cen)+'.00'; else pol_cen = Math.round(pol_cen);
		pole_cen.innerHTML = pol_cen;
		if( act == '+' ) sum = ""+(sum+jed); if( act == '-' ) sum = ""+(sum-jed);
		if( sum.indexOf('.') == -1 ) sum = Math.round(sum)+'.00'; else sum = Math.round(sum*100)/100;
		document.getElementById('sum_cen').innerHTML = sum;
		document.cookie = poz+'='+pole.value+';expires='+ expires_date.toGMTString();
		document.cookie = 'il['+poz+']='+pole.value+';expires='+ expires_date.toGMTString();
	}
}

function dcc_write(nazwa) {
	c = new Array();
	dcc = new Array();
	c[0]="Austria";dcc[0]="+43";c[1]="Belgium";dcc[1]="+32";c[2]="Bielarus";dcc[2]="+375";c[3]="Canada";dcc[3]="+1";c[4]="Czech";dcc[4]="+420";c[5]="Denmark";dcc[5]="+45";c[6]="Estonia";dcc[6]="+372";c[7]="Finland";dcc[7]="+358";c[8]="France";dcc[8]="+33";c[9]="Germany";dcc[9]="+49";c[10]="Great Britain";dcc[10]="+44";c[11]="Greece";dcc[11]="+30";c[12]="Hungary";dcc[12]="+36";c[13]="Iceland";dcc[13]="+354";c[14]="Ireland";dcc[14]="+353";c[15]="Italy";dcc[15]="+39";c[16]="Latvia";dcc[16]="+371";c[17]="Lithuania";dcc[17]="+370";c[18]="Luxembourg";dcc[18]="+352";c[19]="Netherlands";dcc[19]="+31";c[20]="Norway";dcc[20]="+47";c[21]="Polska";dcc[21]="+48";c[22]="Portugal";dcc[22]="+351";c[23]="Romania";dcc[23]="+40";c[24]="Russia";dcc[24]="+7";c[25]="Slovakia";dcc[25]="+421";c[26]="Slovenia";dcc[26]="+386";c[27]="Spain";dcc[27]="+34";c[28]="Sweden";dcc[28]="+46";c[29]="Switzerland";dcc[29]="+41";c[30]="Ukraine";dcc[30]="+380";c[31]="USA";dcc[31]="+1";

	var k_index = nazwa.selectedIndex;
	if( document.getElementById('kom') ) document.getElementById('kom').value = dcc[k_index];
	if( document.getElementById('sta') ) document.getElementById('sta').value = dcc[k_index];
}

function format(val, trailing) {
	var form = new String();
	var sub = new String();

    val = Math.round(val*100)/100;
	
	form = val+"";
	sub = form.lastIndexOf(".");
	var len = form.length-1;

	var pos_from_right=parseInt(len)-parseInt(sub);

    if(sub == -1  ){
	   if( trailing ) form += '.00';
    } else if( pos_from_right == 1 ) {
        form += '0';
	}
	
	return form;
}

function delextra(id,substr_only,delway) {
	var o = document.getElementById(id);
	if( isNaN(parseFloat(document.getElementById(id+'_v').innerHTML)) ) {
		var o_v = 0;
		var obj_v = document.getElementById(id+'_v');
	} else {
			var o_v = parseFloat(document.getElementById(id+'_v').innerHTML);
			var obj_v = document.getElementById(id+'_v');
		}
	var s_all = document.getElementById(delway+'_sumall');
	var s_all_v = parseFloat(s_all.innerHTML);

	if( o.checked ) {
		s_all.innerHTML = format(Math.round((s_all_v+o_v)*100)/100,true);
		obj_v.style.color = '#c00';
	} else {
		s_all.innerHTML = format(Math.round((s_all_v-o_v)*100)/100,true);
		obj_v.style.color = 'transparent';
	}

	if( id == 'ud_g' && document.getElementById('ud_w').checked && !substr_only ) {
		document.getElementById('ud_w').checked = '';
		delextra('ud_w', true, delway);
	} else if( id == 'ud_w' && document.getElementById('ud_g').checked && !substr_only ) {
		document.getElementById('ud_g').checked = '';
		delextra('ud_g', true, delway);
	}

	if( id== 'ud_g' ) {
		if( o.checked ) {
			document.forms[0].ud_g_h.disabled = ''; document.forms[0].ud_g_m.disabled = '';
		} else {
			document.forms[0].ud_g_h.disabled = 'disabled'; document.forms[0].ud_g_m.disabled = 'disabled';
		}
	}
}

function t_chk() {
	if( document.forms[0].ud_g_h.value == '22' ) document.forms[0].ud_g_m.value = '00';
}

function tw_block(block) {
	if( document.getElementById('ud_g').checked || block ) {
		document.forms[0].ud_d[0].disabled = 'disabled';
		document.forms[0].ud_d[1].disabled = 'disabled';
		document.forms[0].ud_d[2].disabled = 'disabled';
	} else {
		document.forms[0].ud_d[0].disabled = '';
		document.forms[0].ud_d[1].disabled = '';
		document.forms[0].ud_d[2].disabled = '';
	}
}

function pick_dest(v) {
	if( isNaN(v) ) return false;

	if( v == 1 ) a = 'stp_cart';
	else if( v == 2 ) a = 'stp_dane_zam';
	else if( v == 3 ) a = 'stp_platnosc';
	else if( v == 4 ) a = 'stp_dostawa';
	else if( v == 5 ) a = 'stp_potwierdzenie';
	else return false;

	document.getElementById('m_form').action = './index.php?show='+a;
	document.getElementById('m_form').submit();
}

var same = false;
function mime(v, t) {
	if( t == 'f' ) {
		if( document.forms[0].poczta.value == v ) same = true;
	} else {
		if( document.forms[0].poczta.value == '' || same ) document.forms[0].poczta.value = v;
		same = false;
	}
}

function pokaz_opis(f,lab) {
	if( document.getElementById('pay_go') ) document.getElementById('pay_go').style.display = 'none';
	if( document.getElementById('pay_po') ) document.getElementById('pay_po').style.display = 'none';
	if( document.getElementById('pay_pp') ) document.getElementById('pay_pp').style.display = 'none';
	if( document.getElementById('pay_tw') ) document.getElementById('pay_tw').style.display = 'none';
	if( document.getElementById('pay_ka') ) document.getElementById('pay_ka').style.display = 'none';
	if( document.getElementById('pay_py') ) document.getElementById('pay_py').style.display = 'none';
	if( document.getElementById('pay_ra') ) document.getElementById('pay_ra').style.display = 'none';
	if( document.getElementById('pay_lr') ) document.getElementById('pay_lr').style.display = 'none';
	if( document.getElementById('pay_ip') ) document.getElementById('pay_ip').style.display = 'none';
	if( document.getElementById('pay_sr') ) document.getElementById('pay_sr').style.display = 'none';
	if( f != 'pay_' ) {
		f = f.toLowerCase(f);
		document.getElementById(f).style.display = 'block';
		if( lab ) lab.firstChild.checked = 'checked';
	}
}

function auto_focus(o,t) {
	vs = new String(o.value);
	var next_o = o.nextSibling;
	next_o = next_o.nextSibling;

	if( t == 'c' ) {
		if( vs == '+48' || vs == '0048' ) next_o.focus();
	} else {
		if( t == 'k' ) {
			var w_zero_l = 4;
			var n_zero_l = 3;
		} else if( t == 's' ) {
			var w_zero_l = 3;
			var n_zero_l = 2;
		}
		var k_index = document.forms[0].kraj.selectedIndex;
		var k = document.forms[0].kraj.options[k_index].text;
		if( k == 'Polska' ) {
			if( vs.slice(0,1) == '0' && vs.length == w_zero_l ) next_o.focus();
				else if( vs.slice(0,1) != '0' && vs.length == n_zero_l ) next_o.focus();
		} else {
			if( vs.length == o.maxlength ) next_o.focus();
		}
	}
}

var IE;
var hpnl = '';
if( !window.opera && navigator.appName == "Microsoft Internet Explorer" ) IE = true; else IE = false;

function show_tip(o,spc) {
	var nxt = o.nextSibling;
	nxt = nxt.nextSibling;
	nxt = nxt.nextSibling;
	if( !IE ) nxt = nxt.nextSibling;
	nxt.className = 'nfo';
	if( spc == '2' ) document.getElementById('new_pswd').checked = 'checked';
}

function hide_tip(o,spc) {
	var prv = o.parentNode;
	prv.className = 'nfo_h';
	if( spc == '3' ) document.getElementById('new_pswd').checked = '';
}

function opcja_adresu(chk) {
	if( chk ) {
		pokaz_pole('opcja');
	} else {
		ukryj_pole('opcja');
		document.getElementById('opcja').checked = '';
	}
}

function submit_form(id) {
	document.getElementById(id).submit();
	return false;
}

function suckPhrase(o) {
	var phrase = document.getElementById('sfld').value;
	o.href = o.href+'?ss='+phrase;
}

function btover(o) {
	o.className = o.className+'_over';
}

function btout(o) {
	o.className = o.className.slice(0,-5);
}

function onkey_przelicz(e,step) {
	if (e.keyCode) code = e.keyCode;
		else if (e.which) code = e.which;
	if( code == 13 ) przelicz(step);
}

var uwg_link_inner;

function uwg_write(link_o) {

	po = link_o.parentNode;
	aInp = po.getElementsByTagName('input');

	if( aInp[1].style.display != 'inline' ) {

		po.tresc.removeAttribute('readonly');
		po.tresc.readOnly = false;

		po.tresc.style.background = '#fff';
		po.tresc.rows = 6;
		po.tresc.style.cursor = 'text';
		po.tresc.focus();
		aInp[1].style.display = 'inline';
		if( link_o.innerHTML != '' ) uwg_link_inner = link_o.innerHTML;
		link_o.innerHTML = '&laquo; powrót';
	} else {
		po.tresc.setAttribute('readonly', 'readonly');
		po.tresc.readOnly = true;
		po.tresc.style.background = '#f3f3f3';
		po.tresc.rows = 3;
		po.tresc.style.cursor = 'default';
		aInp[1].style.display = 'none';
		link_o.innerHTML = uwg_link_inner;
	}
}

function checkConstrCount(trigg) {
  var oForm = document.getElementById('m_form');
  var ppvc = trigg.value
  var sum = parseFloat(document.getElementById('sum_cen').innerHTML);

  stan = oForm.elements['stan_opcja['+ppvc+']'].value;
  var count = stan.slice(0, stan.lastIndexOf("_"));
  var prd = stan.slice(stan.lastIndexOf("_")+1);
  var propIL = document.getElementById('il'+prd);

  sum = parseFloat(sum-parseFloat(document.getElementById('cen_'+prd).innerHTML));

  if( propIL.value > count && count > 0 ) propIL.value = count;
  document.getElementById('max_'+prd).value = count;

  var priceOpt = oForm.elements['cena_opcja['+ppvc+']'].value;

  var postPrice = parseFloat(Math.round(priceOpt*propIL.value)+'.00');
  document.getElementById('cen_'+prd).innerHTML = postPrice+'.00';
  document.getElementById('jed_'+prd).innerHTML = Math.round(priceOpt)+'.00';
  document.getElementById('sum_cen').innerHTML = Math.round(sum+postPrice)+'.00 ';
}

function addUsluga(trigg, u_id, suma, prd) {
  var targPrice = document.getElementById('u_c_'+u_id);
  var sum_uslug = document.getElementById('sum_uslug');
  var sum_cen = document.getElementById('sum_cen');
  var u_sum = document.getElementById('u_sum_'+prd);

  if( targPrice && sum_uslug && sum_cen && u_sum ) {
    //format sumy
    var suma = format(Math.round(suma*100)/100, true);
    if( trigg.checked == true ) {
      targPrice.innerHTML = suma;
      tmp = format(Math.round((parseFloat(sum_uslug.innerHTML)+parseFloat(suma))*100)/100, true);
      sum_uslug.innerHTML = tmp;
      tmp = format(Math.round((parseFloat(sum_cen.innerHTML)+parseFloat(suma))*100)/100, true);
      sum_cen.innerHTML = tmp;
      tmp = format(Math.round((parseFloat(u_sum.innerHTML)+parseFloat(suma))*100)/100, true);
      u_sum.innerHTML = tmp;
    } else {
      targPrice.innerHTML = '0.00';
      var tmp = format(Math.round((parseFloat(sum_uslug.innerHTML)-parseFloat(suma))*100)/100, true);
      sum_uslug.innerHTML = tmp;
      var tmp = format(Math.round((parseFloat(sum_cen.innerHTML)-parseFloat(suma))*100)/100, true);
      sum_cen.innerHTML = tmp;
      var tmp = format(Math.round((parseFloat(u_sum.innerHTML)-parseFloat(suma))*100)/100, true);
      u_sum.innerHTML = tmp;
    }
  }
}

function setDisabled(id, v) {

    var o = document.getElementById(id);
    if (!o) {
        return false;
    }

    if (typeof(v) != 'number') {
        o.disabled = !o.disabled;
    } else {
        if (v == 1) {
            o.disabled = 'disabled';
        } else {
            o.disabled = '';
        }
    }
}

$(document).ready(function(){
	$('#platnoscForm').submit(function(){
		//showPackPreloader();
		//return false;
	})
});


function showPackPreloader(){
	/*function updatePosition(){
		$('#infoDiv').css({
			position:'absolute',
			display: 'block',
			left: ($(window).width() - $('#infoDiv').width())/2,
			top: ($('#infoDiv').height())+$(window).scrollTop(),
		});
	}
	$(document).scroll(function(){
		updatePosition();
	});
	updatePosition();*/
}



$(document).ready(function() { 
	$("#topBar").css("position", "absolute"); 
}); 
$(window).scroll(function() { $("#topBar").css("top", $(window).scrollTop() + "px"); }); 


$(document).ready(function(){
	$('#dosForm').submit(function(){
		var inpostErrors='';
		if($('input[name="delway"]:checked').val()== 'IP'){
			inpostErrors='';
			if ($('[name="inpost[paczkomat_main]"]').val() == '0'){
				inpostErrors+='\n* Proszę wybrać paczkomat podstawowy';
			}
			if ($('[name="inpost[tel][selected]"]:checked').val() == 'default'){
				
			}else if ($('[name="inpost[tel][selected]"]:checked').val() == 'manual'){
				var phoneNumber=$('[name="inpost[tel][manual]"]').val();
				//alert(phoneNumber);
				$.ajax({
		            	type: "POST",
		            	async: false,
	                    data: "action=isMobilePhoneNumber&tel="+phoneNumber+"&"+Math.random(),
	                    url: "validatorsAjax.php",
	                    cache: false,
		            	success: function(msg){
			            	if (msg != "1"){
			            		inpostErrors+= '\n* Podany numer telefonu nie jest numerem telefonu komórkowego ';
			            	}
	           		 	}

	           		 }); // validate number
			}else{
				inpostErrors+='\n* Nie podano telefonu kontaktowego';
			}
			
			if ($('#inpostRegBox').length){
				if ($('#inpostRegCheckbox').attr('checked') == false){
					inpostErrors+='\n* Musisz zaakceptować regulamin firmy Inpost';
				}
			}
			
			if (inpostErrors != ''){
				alert(inpostErrors);
				return false;
			}else{
				return true;//true;//true
			}
		}else{
			return true;//true;//true
		}
	})
});

$(document).ready(function(){
	$('[name="inpost[paczkomat_main]"]').change(function(){
		var pId = $('[name="inpost[paczkomat_main]"]').val();
		$.ajax({
		            	type: "POST",
		            	async: false,
	                    data: "action=googleMaps&id="+pId+"&"+Math.random(),
	                    url: "validatorsAjax.php",
	                    cache: false,
		            	success: function(msg){
		            		$('#googleMainPaczkomat').html(msg);
	           		 	}
	           		 });
	});
	$('[name="inpost[paczkomat_opt]"]').change(function(){
		var pId = $('[name="inpost[paczkomat_opt]"]').val();
		$.ajax({
		            	type: "POST",
		            	async: false,
	                    data: "action=googleMaps&id="+pId+"&"+Math.random(),
	                    url: "validatorsAjax.php",
	                    cache: false,
		            	success: function(msg){
		            		$('#googleOptPaczkomat').html(msg);
	           		 	}
	           		 });
	});
});

$(document).ready(function(){
	loadOWgoogleMap();
	$('[name="delway"]').change(function(){
		loadOWgoogleMap();
	});
});


$(document).ready(function(){
	var FB_SlidingRightInitial = $("#FB_sliding").css('right'); // pierwotny prawy margines ślizgacza	
    var FB_SlidingPosition = 'slid'; // pierwotnie wsunięty
	$("#FB_notice").click(function() {
		if (FB_SlidingPosition == 'slid') {// jak wsunięty, to wyjeżdżamy
			$("#FB_sliding").animate( {
				right : '-10' // pozostawiamy wsunięty o 10px poza margines
			}, 500, function() {
				FB_SlidingPosition = 'open';
			});
		} else {
			$("#FB_sliding").animate( {
				right : '' + FB_SlidingRightInitial + '' // wracamy margines do pierwotnej wartości
			}, 50, function() {
				FB_SlidingPosition = 'slid';
			});
		}
	});	
});

$(document).ready(function(){
	if (($('#ud_s').attr('checked'))){
		//ud_s_box
	}

});

$(document).ready(function(){
	var elements = $('#m_form input:checkbox');
	for(var i=0;i<elements.length;i++){
		if (elements[i].id.indexOf('elvia_checkbox') === 0){
			$('#'+elements[i].id).click(function(){
				showUslugiPotwBox();
			});
		}
	}
	elements = $("[$name='opcja[']:radio");
	if (elements.length>0){
		var element;
		var testRadioBtns;
		for(var i=0;i<elements.length;i++){
			element = $("[name='"+elements[i].name+"']");
			element.click(function(){
				$('#nfoBox').css('display','none');
				var subelements = $("input:[$name='opcja[']:radio");
				var radiosArray = new Object();
				var key;
				for(var j=0;j<subelements.length;j++){
					key = subelements[j].name;
					if (!radiosArray[key]){
						radiosArray[key] = subelements[j].checked;
					}
				}
				for (var j in radiosArray){	
					if (!radiosArray[j]){
						$('#nfoBox').css('display','block');
						//alert('pokaz box');
						break;
					}
				}	
			});
		}
	}
});

$(document).ready(function(){
	showUslugiPotwBox();
});

function showUslugiPotwBox(){
	var elements = $("input:[name^='usluga[']");
	//alert(elements.length);
	var isAnyChecked = false;
	for(var j=0;j<elements.length;j++){
		if (elements[j].checked){
			isAnyChecked= true;
		}
	}
	if (isAnyChecked){
		$('#acceptUslBox').css('display','block');
	}else{
		$('#acceptUslBox').css('display','none');
	}
}


function loadOWgoogleMap(){

	if ($('[name="delway"]:checked').val() == 'OW' && $('#owm').html() == '1'){
		$('#owm').html('');
//<iframe width="480" height="450" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.pl/maps?f=q&amp;source=s_q&amp;hl=pl&amp;geocode=&amp;q=51.099731,16.990419&amp;ie=UTF8&amp;t=h&amp;z=15&amp;output=embed"></iframe><br /><small><a href="http://maps.google.pl/maps?f=q&amp;source=embed&amp;hl=pl&amp;geocode=&amp;q=51.099731,16.990419&amp;ie=UTF8&amp;t=h&amp;z=15" style="color:#0000FF;text-align:left">Wyświetl większą mapę</a></small>
	}

}

function openSimulator(url) {		
  window.open('blank.html','newwin');
} 

function switchOdRekiView(){
	var ob = document.getElementById('odrekicont');
	if (ob.style.display=='block'){
		ob.style.display='none';
	}else{
		ob.style.display='block'
	}
	//$(').css('display','block');
	return false;
}

function checkCode(numberOfRespBox){
	var code = $('#codeBar'+numberOfRespBox).val();
	var codeResponse;
	$.ajax({
     	type: "POST",
     	async: false,
            data: "action=checkCode&id="+code+"&"+Math.random(),
            url: "checkCoupon.php",
            cache: false,
     		success: function(msg){
     			codeResponse = msg;
     			if (codeResponse!='1'){
     				$('#couponStatus'+numberOfRespBox).html(msg);
     			}
   		 	}
   	});
   //	$('#couponStatus'+numberOfRespBox).val();
   //	alert(codeResponse);
   	if (codeResponse == '1'){
   		return true;
   	}else{
   		return false;
   	}
}


function clickCount(id){
	$.ajax({
     	type: "POST",
     	async: true,
        data: "action=countDownloads&id="+id+"&"+Math.random(),
        url: "http://"+location.host+"/poradnikiAJAX.php",
        cache: false,
 		success: function(msg){
 			//alert(msg);
	 	}
   	});
}

voteKey = '';
$(document).ready(function(){
	var voteBox = $('#voteBox');
	var opinionsBox = $('#opinionsBox');
	activateLinks();
	
	$('#closeVoteBoxBtn').click(function(){
		voteBox.css('display','none');
	});
	
	$('#closeOpinionsBoxBtn').click(function(){
		opinionsBox.css('display','none');
	});
	$('#sendVoteBtn').click(function(){
		//voteKey = this.id;
		addVote(voteKey);
		$('#'+voteKey).css('display','none');
		voteBox.css('display','none');
		//alert(document.cookie);
	});
	$('.bestArtOrderChBox').change(function(){
		//alert('ok');
		$.ajax({
	     	type: "POST",
	     	async: true,
	        data: "action=bestArticles&order="+this.value+"&"+Math.random(),
	        url: "http://"+location.host+"/poradnikiAJAX.php",
	        cache: false,
	 		success: function(msg){
	 			$('#bestArticlesBox').html(msg);
	 			activateLinks();
		 	}
   		});
	});
});

function activateLinks(){
	var voteBox = $('#voteBox');
	var opinionsBox = $('#opinionsBox');

	$(".ocenLink").click(function(e){
		clearVotes();
		voteBox.css('left',e.pageX);
		voteBox.css('top',e.pageY);
		voteBox.css('display','block');
		$.ajax({
	     	type: "POST",
	     	async: true,
	        data: "action=getTitle&id="+this.id+"&"+Math.random(),
	        url: "http://"+location.host+"/poradnikiAJAX.php",
	        cache: false,
	 		success: function(msg){
				$('#voteTitle').html(msg);
			}
   		});
		voteKey = this.id;
	});
	
	$(".seeOpinions").click(function(e){
		//alert(location.host);
		//clearVotes();
		opinionsBox.css('left',e.pageX);
		opinionsBox.css('top',e.pageY);
		$.ajax({
	     	type: "POST",
	     	//async: true,
	        data: {"action": "seeOpinions","id" : this.id},
	        url: "http://"+location.host+"/poradnikiAJAX.php",
	        //cache: true,
		dataType: "html",
		success: function(msg){
		    
			$('#opinionsContentBox').html(msg);
		  },
		error:function (xhr, ajaxOptions, thrownError, request, error){
                    /*alert('xrs.status = ' + xhr.status + '\n' + 
			  'thrown error = ' + thrownError + '\n' +
			  'xhr.statusText = '  + xhr.statusText + '\n' +
			  'request = ' + request + '\n' +
			  'error = ' + error);*/
                } 
   		});
		opinionsBox.css('display','block');
	});
}

function clearVotes(){
	var inputs = $('.votePoints');
	for(var i=0; i< inputs.length;i++){
		inputs[i].value='';
	}
	var current_ratings = $('.current-rating');
	for(var i=0; i< current_ratings.length;i++){
		$(current_ratings[i]).css('width','0');
	}
	$('#pomyslNaArt').val('');
	$('#opiniaArt').val('');
}

function addVote(porKey){
	var inputs = $('.votePoints');
	var details= '';
	var pomysl = $('#pomyslNaArt').val();
	var opinia = $('#opiniaArt').val();
	var nick = $('#nick').val();
	
	for(var i=0; i< inputs.length;i++){
		details += '&';
		details += inputs[i].name+'='+inputs[i].value;
	}

	$.ajax({
     	type: "POST",
     	async: true,
        data: "action=addVote&"+Math.random()+details+'&opinia='+opinia+'&pomysl='+pomysl+'&porKey='+porKey+'&nick='+nick,
        url: "http://"+location.host+"/poradnikiAJAX.php",
        cache: false,
 		success: function(msg){
 			createCookie(porKey,'true',366);
	 	}
   	});
   	return false;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

