
var APP = new Object;

APP.initIndex = function() {
	$(document.body).ready(function(){
		
		$('#tabpanel .navi a').each(function(idx, val) {
			$(this).click(function() {
				APP.indexTabPanelClick(idx);				
			});
		});
		
		APP.indexTabPanelClick(0);
	});		
}

APP.indexTabPanelClick = function(idx) {
	$('#tabpanel ul li:eq('+idx+')').attr('class', 'current');
	$('#tabpanel ul li:eq('+idx+')').siblings().removeAttr('class');
	
	
	$('#tabpanel .panel:eq('+idx+')').show();
	$('#tabpanel .panel:eq('+idx+')').siblings('div').hide();
}

function totalCost(total){
	var str = total;
	var przesylka = $('#cl_2 p')[0].textContent;
	var priceTotal= str.replace(",", ".");
	var amount = parseFloat(priceTotal)+parseFloat(przesylka);
	$('#cl_2 p')[2].childNodes[0].textContent = amount.toString().replace(".", ",");
}

function formaZaplaty(total){
	if($('input:radio[name=dostawa]:checked').val() == 'gls'){
		var koszt = 0;
		optVal = $('#formaZaplaty option:selected').val();
		if(optVal == 'przelew'){
			koszt = 15;
			$('#cl_2 p')[0].textContent = koszt;
			totalCost(total);
		}
		else if(optVal == 'gotowka'){
			koszt = 25;
			$('#cl_2 p')[0].textContent = koszt;
			totalCost(total);
		}
	}
	else {
		$('#cl_2 p')[0].textContent = 0;
		totalCost(total);
	}
}

function ecko(total){
	console.log(total);
}

$(document).ready(function() {
	$('#kupujeJako').change(function() {
		optionVal = $('#kupujeJako option:selected').val();
		if(optionVal == 'osoba') {
			$('#df_1 label').replaceWith('<label>* Imię:</label>');
			$('#df_2 label').replaceWith('<label>* Nazwisko:</label>');
		}
		else {
			$('#df_1 label').replaceWith('<label>* Nazwa firmy:</label>');
			$('#df_2 label').replaceWith('<label>* NIP:</label>');
		}
	});
	
	
	$('#order').bind('submit', function() {
		var valid = true;
		$(':text.required', this).each(function() {
			if($(this).val() == ''){
				$(this).prev().addClass('err');
				
				valid = false;
			}
			else {
				$(this).prev().removeClass('err');
                
			}
		});
		$('p.err').remove();
		if(!valid){
			$('div.order_p3').prepend('<p class="err">Wypełnij wszystkie oznaczone pola i spróbuj ponownie.</p>');
			return false;
        }
	});
});
